"use strict"; exports.__esModule = true; exports["default"] = void 0; var _emit = _interopRequireDefault(require("../event/emit")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } var CanvasElement = /*#__PURE__*/function (_EventEmit) { _inheritsLoose(CanvasElement, _EventEmit); function CanvasElement(ctx) { var _this; _this = _EventEmit.call(this) || this; _this.context = ctx; // canvas实际的宽高 (width/height) * pixelRatio _this.width = 0; _this.height = 0; _this.style = {}; _this.currentStyle = {}; _this.attrs = {}; // 用来标识是CanvasElement实例 _this.isCanvasElement = true; return _this; } var _proto = CanvasElement.prototype; _proto.getContext = function getContext() /* type */ { return this.context; }; _proto.getBoundingClientRect = function getBoundingClientRect() { var width = this.width; var height = this.height; // 默认都处理成可视窗口的顶部位置 return { top: 0, right: width, bottom: height, left: 0 }; }; _proto.setAttribute = function setAttribute(key, value) { this.attrs[key] = value; }; _proto.addEventListener = function addEventListener(type, listener) { this.on(type, listener); }; _proto.removeEventListener = function removeEventListener(type, listener) { this.off(type, listener); }; _proto.dispatchEvent = function dispatchEvent(type, e) { this.emit(type, e); }; return CanvasElement; }(_emit["default"]); function supportEventListener(canvas) { if (!canvas) { return false; } // 非 HTMLCanvasElement if (canvas.nodeType !== 1 || !canvas.nodeName || canvas.nodeName.toLowerCase() !== 'canvas') { return false; } // 微信小程序canvas.getContext('2d')时也是CanvasRenderingContext2D // 也会有ctx.canvas, 而且nodeType也是1,所以还要在看下是否支持addEventListener var support = false; try { canvas.addEventListener('eventTest', function () { support = true; }); canvas.dispatchEvent(new Event('eventTest')); } catch (error) { support = false; } return support; } var _default = { create: function create(ctx) { if (!ctx) { return null; } if (supportEventListener(ctx.canvas)) { return ctx.canvas; } return new CanvasElement(ctx); } }; exports["default"] = _default;