"use strict"; var Util = require('../util/common'); var MatrixUtil = require('./util/matrix'); var Vector2 = require('./util/vector2'); var StyleUtil = require('./util/style-parse'); function isUnchanged(m) { return m[0] === 1 && m[1] === 0 && m[2] === 0 && m[3] === 1 && m[4] === 0 && m[5] === 0; } var ALIAS_ATTRS_MAP = { stroke: 'strokeStyle', fill: 'fillStyle', opacity: 'globalAlpha' }; var SHAPE_ATTRS = ['fillStyle', 'font', 'globalAlpha', 'lineCap', 'lineWidth', 'lineJoin', 'miterLimit', 'shadowBlur', 'shadowColor', 'shadowOffsetX', 'shadowOffsetY', 'strokeStyle', 'textAlign', 'textBaseline', 'lineDash', 'shadow' // 兼容支付宝小程序 ]; var CLIP_SHAPES = ['circle', 'sector', 'polygon', 'rect', 'polyline']; var Element = /*#__PURE__*/function () { var _proto = Element.prototype; _proto._initProperties = function _initProperties() { this._attrs = { zIndex: 0, visible: true, destroyed: false }; }; function Element(cfg) { this._initProperties(); Util.mix(this._attrs, cfg); var attrs = this._attrs.attrs; if (attrs) { this.initAttrs(attrs); } this.initTransform(); } _proto.get = function get(name) { return this._attrs[name]; }; _proto.set = function set(name, value) { this._attrs[name] = value; }; _proto.isGroup = function isGroup() { return this.get('isGroup'); }; _proto.isShape = function isShape() { return this.get('isShape'); }; _proto.initAttrs = function initAttrs(attrs) { this.attr(Util.mix(this.getDefaultAttrs(), attrs)); }; _proto.getDefaultAttrs = function getDefaultAttrs() { return {}; }; _proto._setAttr = function _setAttr(name, value) { var attrs = this._attrs.attrs; if (name === 'clip') { value = this._setAttrClip(value); } else { var alias = ALIAS_ATTRS_MAP[name]; if (alias) { attrs[alias] = value; } } attrs[name] = value; }; _proto._getAttr = function _getAttr(name) { return this._attrs.attrs[name]; } // _afterAttrsSet() {} ; _proto._setAttrClip = function _setAttrClip(clip) { if (clip && CLIP_SHAPES.indexOf(clip._attrs.type) > -1) { if (clip.get('canvas') === null) { clip = Object.assign({}, clip); } clip.set('parent', this.get('parent')); clip.set('context', this.get('context')); return clip; } return null; }; _proto.attr = function attr(name, value) { var self = this; if (self.get('destroyed')) return null; var argumentsLen = arguments.length; if (argumentsLen === 0) { return self._attrs.attrs; } if (Util.isObject(name)) { this._attrs.bbox = null; for (var k in name) { self._setAttr(k, name[k]); } if (self._afterAttrsSet) { self._afterAttrsSet(); } return self; } if (argumentsLen === 2) { this._attrs.bbox = null; self._setAttr(name, value); if (self._afterAttrsSet) { self._afterAttrsSet(); } return self; } return self._getAttr(name); }; _proto.getParent = function getParent() { return this.get('parent'); }; _proto.draw = function draw(context) { if (this.get('destroyed')) { return; } if (this.get('visible')) { this.setContext(context); this.drawInner(context); this.restoreContext(context); } }; _proto.setContext = function setContext(context) { var clip = this._attrs.attrs.clip; context.save(); if (clip) { clip.resetTransform(context); clip.createPath(context); context.clip(); } this.resetContext(context); this.resetTransform(context); }; _proto.restoreContext = function restoreContext(context) { context.restore(); }; _proto.resetContext = function resetContext(context) { var elAttrs = this._attrs.attrs; if (!this._attrs.isGroup) { for (var k in elAttrs) { if (SHAPE_ATTRS.indexOf(k) > -1) { var v = elAttrs[k]; if (k === 'fillStyle' || k === 'strokeStyle') { v = StyleUtil.parseStyle(v, this, context); } if (k === 'lineDash' && context.setLineDash && Util.isArray(v)) { context.setLineDash(v); } else { context[k] = v; } } } } }; _proto.hasFill = function hasFill() { return this.get('canFill') && this._attrs.attrs.fillStyle; }; _proto.hasStroke = function hasStroke() { return this.get('canStroke') && this._attrs.attrs.strokeStyle; }; _proto.drawInner = function drawInner() /* context */ {}; _proto.show = function show() { this.set('visible', true); return this; }; _proto.hide = function hide() { this.set('visible', false); return this; }; _proto.isVisible = function isVisible() { return this.get('visible'); }; _proto._removeFromParent = function _removeFromParent() { var parent = this.get('parent'); if (parent) { var children = parent.get('children'); Util.Array.remove(children, this); } return this; }; _proto.remove = function remove(destroy) { if (destroy) { this.destroy(); } else { this._removeFromParent(); } }; _proto.destroy = function destroy() { var destroyed = this.get('destroyed'); if (destroyed) { return null; } this._removeFromParent(); this._attrs = {}; this.set('destroyed', true); }; _proto.getBBox = function getBBox() { return { minX: 0, maxX: 0, minY: 0, maxY: 0, width: 0, height: 0 }; }; _proto.initTransform = function initTransform() { var attrs = this._attrs.attrs || {}; if (!attrs.matrix) { attrs.matrix = [1, 0, 0, 1, 0, 0]; } this._attrs.attrs = attrs; }; _proto.getMatrix = function getMatrix() { return this._attrs.attrs.matrix; }; _proto.setMatrix = function setMatrix(m) { this._attrs.attrs.matrix = [m[0], m[1], m[2], m[3], m[4], m[5]]; }; _proto.transform = function transform(actions) { var matrix = this._attrs.attrs.matrix; this._attrs.attrs.matrix = MatrixUtil.transform(matrix, actions); return this; }; _proto.setTransform = function setTransform(actions) { this._attrs.attrs.matrix = [1, 0, 0, 1, 0, 0]; return this.transform(actions); }; _proto.translate = function translate(x, y) { var matrix = this._attrs.attrs.matrix; MatrixUtil.translate(matrix, matrix, [x, y]); }; _proto.rotate = function rotate(rad) { var matrix = this._attrs.attrs.matrix; MatrixUtil.rotate(matrix, matrix, rad); }; _proto.scale = function scale(sx, sy) { var matrix = this._attrs.attrs.matrix; MatrixUtil.scale(matrix, matrix, [sx, sy]); }; _proto.moveTo = function moveTo(x, y) { var cx = this._attrs.x || 0; var cy = this._attrs.y || 0; this.translate(x - cx, y - cy); this.set('x', x); this.set('y', y); }; _proto.apply = function apply(v) { var m = this._attrs.attrs.matrix; Vector2.transformMat2d(v, v, m); return this; }; _proto.resetTransform = function resetTransform(context) { var mo = this._attrs.attrs.matrix; if (!isUnchanged(mo)) { context.transform(mo[0], mo[1], mo[2], mo[3], mo[4], mo[5]); } }; _proto.isDestroyed = function isDestroyed() { return this.get('destroyed'); }; return Element; }(); module.exports = Element;