element.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. "use strict";
  2. exports.__esModule = true;
  3. exports["default"] = void 0;
  4. var _common = require("../../util/common");
  5. var _matrix = _interopRequireDefault(require("../util/matrix"));
  6. var _vector = _interopRequireDefault(require("../util/vector2"));
  7. var _styleParse = require("../util/style-parse");
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  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. (0, _common.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((0, _common.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 ((0, _common.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. for (var k in elAttrs) {
  137. if (SHAPE_ATTRS.indexOf(k) > -1) {
  138. var v = elAttrs[k];
  139. if ((k === 'fillStyle' || k === 'strokeStyle') && v) {
  140. v = (0, _styleParse.parseStyle)(v, this, context);
  141. }
  142. if (k === 'lineDash' && context.setLineDash && (0, _common.isArray)(v)) {
  143. context.setLineDash(v);
  144. } else {
  145. context[k] = v;
  146. }
  147. }
  148. }
  149. };
  150. _proto.hasFill = function hasFill() {
  151. return this.get('canFill') && this._attrs.attrs.fillStyle;
  152. };
  153. _proto.hasStroke = function hasStroke() {
  154. return this.get('canStroke') && this._attrs.attrs.strokeStyle;
  155. };
  156. _proto.drawInner = function drawInner()
  157. /* context */
  158. {};
  159. _proto.show = function show() {
  160. this.set('visible', true);
  161. return this;
  162. };
  163. _proto.hide = function hide() {
  164. this.set('visible', false);
  165. return this;
  166. };
  167. _proto.isVisible = function isVisible() {
  168. return this.get('visible');
  169. };
  170. _proto.getAriaLabel = function getAriaLabel() {
  171. var _this$_attrs = this._attrs,
  172. destroyed = _this$_attrs.destroyed,
  173. visible = _this$_attrs.visible,
  174. isShape = _this$_attrs.isShape,
  175. aria = _this$_attrs.aria;
  176. if (destroyed || !visible || isShape && !aria) {
  177. return;
  178. }
  179. return this._getAriaLabel();
  180. };
  181. _proto._getAriaLabel = function _getAriaLabel() {
  182. return this._attrs.ariaLabel;
  183. };
  184. _proto._removeFromParent = function _removeFromParent() {
  185. var parent = this.get('parent');
  186. if (parent) {
  187. var children = parent.get('children');
  188. _common.Array.remove(children, this);
  189. }
  190. return this;
  191. };
  192. _proto.remove = function remove(destroy) {
  193. if (destroy) {
  194. this.destroy();
  195. } else {
  196. this._removeFromParent();
  197. }
  198. };
  199. _proto.destroy = function destroy() {
  200. var destroyed = this.get('destroyed');
  201. if (destroyed) {
  202. return null;
  203. }
  204. this._removeFromParent();
  205. this._attrs = {};
  206. this.set('destroyed', true);
  207. };
  208. _proto.getBBox = function getBBox() {
  209. return {
  210. minX: 0,
  211. maxX: 0,
  212. minY: 0,
  213. maxY: 0,
  214. width: 0,
  215. height: 0
  216. };
  217. };
  218. _proto.initTransform = function initTransform() {
  219. var attrs = this._attrs.attrs || {};
  220. if (!attrs.matrix) {
  221. attrs.matrix = [1, 0, 0, 1, 0, 0];
  222. }
  223. this._attrs.attrs = attrs;
  224. };
  225. _proto.getMatrix = function getMatrix() {
  226. return this._attrs.attrs.matrix;
  227. };
  228. _proto.setMatrix = function setMatrix(m) {
  229. this._attrs.attrs.matrix = [m[0], m[1], m[2], m[3], m[4], m[5]];
  230. };
  231. _proto.transform = function transform(actions) {
  232. var matrix = this._attrs.attrs.matrix;
  233. this._attrs.attrs.matrix = _matrix["default"].transform(matrix, actions);
  234. return this;
  235. };
  236. _proto.setTransform = function setTransform(actions) {
  237. this._attrs.attrs.matrix = [1, 0, 0, 1, 0, 0];
  238. return this.transform(actions);
  239. };
  240. _proto.translate = function translate(x, y) {
  241. var matrix = this._attrs.attrs.matrix;
  242. _matrix["default"].translate(matrix, matrix, [x, y]);
  243. };
  244. _proto.rotate = function rotate(rad) {
  245. var matrix = this._attrs.attrs.matrix;
  246. _matrix["default"].rotate(matrix, matrix, rad);
  247. };
  248. _proto.scale = function scale(sx, sy) {
  249. var matrix = this._attrs.attrs.matrix;
  250. _matrix["default"].scale(matrix, matrix, [sx, sy]);
  251. };
  252. _proto.moveTo = function moveTo(x, y) {
  253. var cx = this._attrs.x || 0;
  254. var cy = this._attrs.y || 0;
  255. this.translate(x - cx, y - cy);
  256. this.set('x', x);
  257. this.set('y', y);
  258. };
  259. _proto.apply = function apply(v) {
  260. var m = this._attrs.attrs.matrix;
  261. _vector["default"].transformMat2d(v, v, m);
  262. return this;
  263. };
  264. _proto.resetTransform = function resetTransform(context) {
  265. var mo = this._attrs.attrs.matrix;
  266. if (_matrix["default"].isChanged(mo)) {
  267. context.transform(mo[0], mo[1], mo[2], mo[3], mo[4], mo[5]);
  268. }
  269. };
  270. _proto.isDestroyed = function isDestroyed() {
  271. return this.get('destroyed');
  272. };
  273. return Element;
  274. }();
  275. var _default = Element;
  276. exports["default"] = _default;