element.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. "use strict";
  2. var Util = require('../util/common');
  3. var MatrixUtil = require('./util/matrix');
  4. var Vector2 = require('./util/vector2');
  5. var StyleUtil = require('./util/style-parse');
  6. function isUnchanged(m) {
  7. return m[0] === 1 && m[1] === 0 && m[2] === 0 && m[3] === 1 && m[4] === 0 && m[5] === 0;
  8. }
  9. var ALIAS_ATTRS_MAP = {
  10. stroke: 'strokeStyle',
  11. fill: 'fillStyle',
  12. opacity: 'globalAlpha'
  13. };
  14. var SHAPE_ATTRS = ['fillStyle', 'font', 'globalAlpha', 'lineCap', 'lineWidth', 'lineJoin', 'miterLimit', 'shadowBlur', 'shadowColor', 'shadowOffsetX', 'shadowOffsetY', 'strokeStyle', 'textAlign', 'textBaseline', 'lineDash', 'shadow' // 兼容支付宝小程序
  15. ];
  16. var CLIP_SHAPES = ['circle', 'sector', 'polygon', 'rect', 'polyline'];
  17. var Element = /*#__PURE__*/function () {
  18. var _proto = Element.prototype;
  19. _proto._initProperties = function _initProperties() {
  20. this._attrs = {
  21. zIndex: 0,
  22. visible: true,
  23. destroyed: false
  24. };
  25. };
  26. function Element(cfg) {
  27. this._initProperties();
  28. Util.mix(this._attrs, cfg);
  29. var attrs = this._attrs.attrs;
  30. if (attrs) {
  31. this.initAttrs(attrs);
  32. }
  33. this.initTransform();
  34. }
  35. _proto.get = function get(name) {
  36. return this._attrs[name];
  37. };
  38. _proto.set = function set(name, value) {
  39. this._attrs[name] = value;
  40. };
  41. _proto.isGroup = function isGroup() {
  42. return this.get('isGroup');
  43. };
  44. _proto.isShape = function isShape() {
  45. return this.get('isShape');
  46. };
  47. _proto.initAttrs = function initAttrs(attrs) {
  48. this.attr(Util.mix(this.getDefaultAttrs(), attrs));
  49. };
  50. _proto.getDefaultAttrs = function getDefaultAttrs() {
  51. return {};
  52. };
  53. _proto._setAttr = function _setAttr(name, value) {
  54. var attrs = this._attrs.attrs;
  55. if (name === 'clip') {
  56. value = this._setAttrClip(value);
  57. } else {
  58. var alias = ALIAS_ATTRS_MAP[name];
  59. if (alias) {
  60. attrs[alias] = value;
  61. }
  62. }
  63. attrs[name] = value;
  64. };
  65. _proto._getAttr = function _getAttr(name) {
  66. return this._attrs.attrs[name];
  67. } // _afterAttrsSet() {}
  68. ;
  69. _proto._setAttrClip = function _setAttrClip(clip) {
  70. if (clip && CLIP_SHAPES.indexOf(clip._attrs.type) > -1) {
  71. if (clip.get('canvas') === null) {
  72. clip = Object.assign({}, clip);
  73. }
  74. clip.set('parent', this.get('parent'));
  75. clip.set('context', this.get('context'));
  76. return clip;
  77. }
  78. return null;
  79. };
  80. _proto.attr = function attr(name, value) {
  81. var self = this;
  82. if (self.get('destroyed')) return null;
  83. var argumentsLen = arguments.length;
  84. if (argumentsLen === 0) {
  85. return self._attrs.attrs;
  86. }
  87. if (Util.isObject(name)) {
  88. this._attrs.bbox = null;
  89. for (var k in name) {
  90. self._setAttr(k, name[k]);
  91. }
  92. if (self._afterAttrsSet) {
  93. self._afterAttrsSet();
  94. }
  95. return self;
  96. }
  97. if (argumentsLen === 2) {
  98. this._attrs.bbox = null;
  99. self._setAttr(name, value);
  100. if (self._afterAttrsSet) {
  101. self._afterAttrsSet();
  102. }
  103. return self;
  104. }
  105. return self._getAttr(name);
  106. };
  107. _proto.getParent = function getParent() {
  108. return this.get('parent');
  109. };
  110. _proto.draw = function draw(context) {
  111. if (this.get('destroyed')) {
  112. return;
  113. }
  114. if (this.get('visible')) {
  115. this.setContext(context);
  116. this.drawInner(context);
  117. this.restoreContext(context);
  118. }
  119. };
  120. _proto.setContext = function setContext(context) {
  121. var clip = this._attrs.attrs.clip;
  122. context.save();
  123. if (clip) {
  124. clip.resetTransform(context);
  125. clip.createPath(context);
  126. context.clip();
  127. }
  128. this.resetContext(context);
  129. this.resetTransform(context);
  130. };
  131. _proto.restoreContext = function restoreContext(context) {
  132. context.restore();
  133. };
  134. _proto.resetContext = function resetContext(context) {
  135. var elAttrs = this._attrs.attrs;
  136. if (!this._attrs.isGroup) {
  137. for (var k in elAttrs) {
  138. if (SHAPE_ATTRS.indexOf(k) > -1) {
  139. var v = elAttrs[k];
  140. if (k === 'fillStyle' || k === 'strokeStyle') {
  141. v = StyleUtil.parseStyle(v, this, context);
  142. }
  143. if (k === 'lineDash' && context.setLineDash && Util.isArray(v)) {
  144. context.setLineDash(v);
  145. } else {
  146. context[k] = v;
  147. }
  148. }
  149. }
  150. }
  151. };
  152. _proto.hasFill = function hasFill() {
  153. return this.get('canFill') && this._attrs.attrs.fillStyle;
  154. };
  155. _proto.hasStroke = function hasStroke() {
  156. return this.get('canStroke') && this._attrs.attrs.strokeStyle;
  157. };
  158. _proto.drawInner = function drawInner()
  159. /* context */
  160. {};
  161. _proto.show = function show() {
  162. this.set('visible', true);
  163. return this;
  164. };
  165. _proto.hide = function hide() {
  166. this.set('visible', false);
  167. return this;
  168. };
  169. _proto.isVisible = function isVisible() {
  170. return this.get('visible');
  171. };
  172. _proto._removeFromParent = function _removeFromParent() {
  173. var parent = this.get('parent');
  174. if (parent) {
  175. var children = parent.get('children');
  176. Util.Array.remove(children, this);
  177. }
  178. return this;
  179. };
  180. _proto.remove = function remove(destroy) {
  181. if (destroy) {
  182. this.destroy();
  183. } else {
  184. this._removeFromParent();
  185. }
  186. };
  187. _proto.destroy = function destroy() {
  188. var destroyed = this.get('destroyed');
  189. if (destroyed) {
  190. return null;
  191. }
  192. this._removeFromParent();
  193. this._attrs = {};
  194. this.set('destroyed', true);
  195. };
  196. _proto.getBBox = function getBBox() {
  197. return {
  198. minX: 0,
  199. maxX: 0,
  200. minY: 0,
  201. maxY: 0,
  202. width: 0,
  203. height: 0
  204. };
  205. };
  206. _proto.initTransform = function initTransform() {
  207. var attrs = this._attrs.attrs || {};
  208. if (!attrs.matrix) {
  209. attrs.matrix = [1, 0, 0, 1, 0, 0];
  210. }
  211. this._attrs.attrs = attrs;
  212. };
  213. _proto.getMatrix = function getMatrix() {
  214. return this._attrs.attrs.matrix;
  215. };
  216. _proto.setMatrix = function setMatrix(m) {
  217. this._attrs.attrs.matrix = [m[0], m[1], m[2], m[3], m[4], m[5]];
  218. };
  219. _proto.transform = function transform(actions) {
  220. var matrix = this._attrs.attrs.matrix;
  221. this._attrs.attrs.matrix = MatrixUtil.transform(matrix, actions);
  222. return this;
  223. };
  224. _proto.setTransform = function setTransform(actions) {
  225. this._attrs.attrs.matrix = [1, 0, 0, 1, 0, 0];
  226. return this.transform(actions);
  227. };
  228. _proto.translate = function translate(x, y) {
  229. var matrix = this._attrs.attrs.matrix;
  230. MatrixUtil.translate(matrix, matrix, [x, y]);
  231. };
  232. _proto.rotate = function rotate(rad) {
  233. var matrix = this._attrs.attrs.matrix;
  234. MatrixUtil.rotate(matrix, matrix, rad);
  235. };
  236. _proto.scale = function scale(sx, sy) {
  237. var matrix = this._attrs.attrs.matrix;
  238. MatrixUtil.scale(matrix, matrix, [sx, sy]);
  239. };
  240. _proto.moveTo = function moveTo(x, y) {
  241. var cx = this._attrs.x || 0;
  242. var cy = this._attrs.y || 0;
  243. this.translate(x - cx, y - cy);
  244. this.set('x', x);
  245. this.set('y', y);
  246. };
  247. _proto.apply = function apply(v) {
  248. var m = this._attrs.attrs.matrix;
  249. Vector2.transformMat2d(v, v, m);
  250. return this;
  251. };
  252. _proto.resetTransform = function resetTransform(context) {
  253. var mo = this._attrs.attrs.matrix;
  254. if (!isUnchanged(mo)) {
  255. context.transform(mo[0], mo[1], mo[2], mo[3], mo[4], mo[5]);
  256. }
  257. };
  258. _proto.isDestroyed = function isDestroyed() {
  259. return this.get('destroyed');
  260. };
  261. return Element;
  262. }();
  263. module.exports = Element;