UNPKG

@antv/g-mobile-canvas

Version:

A renderer implemented with Canvas2D API in mobile environment

206 lines (197 loc) 7.53 kB
/*! * @antv/g-mobile-canvas * @description A renderer implemented with Canvas2D API in mobile environment * @version 1.0.35 * @date 1/17/2025, 2:09:53 PM * @author AntVis * @docs https://g.antv.antgroup.com/ */ import _createClass from '@babel/runtime/helpers/createClass'; import _classCallCheck from '@babel/runtime/helpers/classCallCheck'; import _callSuper from '@babel/runtime/helpers/callSuper'; import _inherits from '@babel/runtime/helpers/inherits'; import { AbstractRendererPlugin, AbstractRenderer } from '@antv/g-lite'; import * as CanvasPathGenerator from '@antv/g-plugin-canvas-path-generator'; import * as CanvasPicker from '@antv/g-plugin-canvas-picker'; export { CanvasPicker }; import * as CanvasRenderer from '@antv/g-plugin-canvas-renderer'; export { CanvasRenderer }; import * as DragDropEvent from '@antv/g-plugin-dragndrop'; import * as ImageLoader from '@antv/g-plugin-image-loader'; import * as MobileInteraction from '@antv/g-plugin-mobile-interaction'; import * as GesturePlugin from '@antv/g-plugin-gesture'; import { isNil } from '@antv/util'; import _regeneratorRuntime from '@babel/runtime/helpers/regeneratorRuntime'; import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator'; function isCanvasElement(el) { if (!el || typeof el !== 'object') return false; if (el.nodeType === 1 && el.nodeName) { // HTMLCanvasElement return true; } // CanvasElement return !!el.isCanvasElement; } var Canvas2DContextService = /*#__PURE__*/function () { function Canvas2DContextService(context) { _classCallCheck(this, Canvas2DContextService); this.canvasConfig = context.config; } return _createClass(Canvas2DContextService, [{ key: "init", value: function () { var _init = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() { var _this$canvasConfig, canvas, devicePixelRatio, dpr; return _regeneratorRuntime().wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: _this$canvasConfig = this.canvasConfig, canvas = _this$canvasConfig.canvas, devicePixelRatio = _this$canvasConfig.devicePixelRatio; this.$canvas = canvas; // 实际获取到小程序环境的上下文 this.context = this.$canvas.getContext('2d'); // use user-defined dpr first dpr = devicePixelRatio || 1; dpr = dpr >= 1 ? Math.ceil(dpr) : 1; this.dpr = dpr; this.resize(this.canvasConfig.width, this.canvasConfig.height); case 7: case "end": return _context.stop(); } }, _callee, this); })); function init() { return _init.apply(this, arguments); } return init; }() }, { key: "getContext", value: function getContext() { return this.context; } }, { key: "getDomElement", value: function getDomElement() { return this.$canvas; } }, { key: "getDPR", value: function getDPR() { return this.dpr; } }, { key: "getBoundingClientRect", value: function getBoundingClientRect() { if (this.$canvas.getBoundingClientRect) { return this.$canvas.getBoundingClientRect(); } } }, { key: "destroy", value: function destroy() { // TODO: 小程序环境销毁 context this.context = null; this.$canvas = null; } }, { key: "resize", value: function resize(width, height) { var devicePixelRatio = this.canvasConfig.devicePixelRatio; var pixelRatio = devicePixelRatio; var canvasDOM = this.$canvas; // HTMLCanvasElement or canvasElement // 浏览器环境设置style样式 if (canvasDOM.style) { canvasDOM.style.width = "".concat(width, "px"); canvasDOM.style.height = "".concat(height, "px"); } if (isCanvasElement(canvasDOM)) { canvasDOM.width = width * pixelRatio; canvasDOM.height = height * pixelRatio; if (pixelRatio !== 1) { this.context.scale(pixelRatio, pixelRatio); } } } }, { key: "applyCursorStyle", value: function applyCursorStyle(cursor) { // 小程序环境无需设置鼠标样式 } }, { key: "toDataURL", value: function () { var _toDataURL = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(options) { var type, encoderOptions; return _regeneratorRuntime().wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: type = options.type, encoderOptions = options.encoderOptions; return _context2.abrupt("return", this.context.canvas.toDataURL(type, encoderOptions)); case 2: case "end": return _context2.stop(); } }, _callee2, this); })); function toDataURL(_x) { return _toDataURL.apply(this, arguments); } return toDataURL; }() }]); }(); var ContextRegisterPlugin = /*#__PURE__*/function (_AbstractRendererPlug) { function ContextRegisterPlugin() { var _this; _classCallCheck(this, ContextRegisterPlugin); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _callSuper(this, ContextRegisterPlugin, [].concat(args)); _this.name = 'mobile-canvas-context-register'; return _this; } _inherits(ContextRegisterPlugin, _AbstractRendererPlug); return _createClass(ContextRegisterPlugin, [{ key: "init", value: function init() { this.context.ContextService = Canvas2DContextService; } }, { key: "destroy", value: function destroy() { delete this.context.ContextService; } }]); }(AbstractRendererPlugin); var Renderer = /*#__PURE__*/function (_AbstractRenderer) { function Renderer(config) { var _this; _classCallCheck(this, Renderer); _this = _callSuper(this, Renderer, [config]); // register Canvas2DContext _this.registerPlugin(new ContextRegisterPlugin()); _this.registerPlugin(new ImageLoader.Plugin()); _this.registerPlugin(new CanvasPathGenerator.Plugin()); // enable rendering with Canvas2D API _this.registerPlugin(new CanvasRenderer.Plugin()); _this.registerPlugin(new MobileInteraction.Plugin()); // enable picking with Canvas2D API _this.registerPlugin(new CanvasPicker.Plugin()); _this.registerPlugin(new DragDropEvent.Plugin({ isDocumentDraggable: isNil(config === null || config === void 0 ? void 0 : config.isDocumentDraggable) ? true : config.isDocumentDraggable, isDocumentDroppable: isNil(config === null || config === void 0 ? void 0 : config.isDocumentDroppable) ? true : config.isDocumentDroppable, dragstartDistanceThreshold: isNil(config === null || config === void 0 ? void 0 : config.dragstartDistanceThreshold) ? 10 : config.dragstartDistanceThreshold, dragstartTimeThreshold: isNil(config === null || config === void 0 ? void 0 : config.dragstartTimeThreshold) ? 50 : config.dragstartTimeThreshold })); _this.registerPlugin(new GesturePlugin.Plugin({ isDocumentGestureEnabled: true })); return _this; } _inherits(Renderer, _AbstractRenderer); return _createClass(Renderer); }(AbstractRenderer); export { Renderer }; //# sourceMappingURL=index.esm.js.map