canvas.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { __assign, __extends } from "tslib";
  2. import { AbstractCanvas } from '@antv/g-base';
  3. import { SHAPE_TO_TAGS } from './constant';
  4. import { drawChildren } from './util/draw';
  5. import { setTransform, setClip } from './util/svg';
  6. import { sortDom, createSVGElement } from './util/dom';
  7. import * as Shape from './shape';
  8. import Group from './group';
  9. import Defs from './defs';
  10. var Canvas = /** @class */ (function (_super) {
  11. __extends(Canvas, _super);
  12. function Canvas(cfg) {
  13. return _super.call(this, __assign(__assign({}, cfg), { autoDraw: true,
  14. // 设置渲染引擎为 canvas,只读属性
  15. renderer: 'svg' })) || this;
  16. }
  17. Canvas.prototype.getShapeBase = function () {
  18. return Shape;
  19. };
  20. Canvas.prototype.getGroupBase = function () {
  21. return Group;
  22. };
  23. // 覆盖 Container 中通过遍历的方式获取 shape 对象的逻辑,直接走 SVG 的 dom 拾取即可
  24. Canvas.prototype.getShape = function (x, y, ev) {
  25. var target = ev.target || ev.srcElement;
  26. if (!SHAPE_TO_TAGS[target.tagName]) {
  27. var parent_1 = target.parentNode;
  28. while (parent_1 && !SHAPE_TO_TAGS[parent_1.tagName]) {
  29. parent_1 = parent_1.parentNode;
  30. }
  31. target = parent_1;
  32. }
  33. return this.find(function (child) { return child.get('el') === target; });
  34. };
  35. // 复写基类的方法生成标签
  36. Canvas.prototype.createDom = function () {
  37. var element = createSVGElement('svg');
  38. var context = new Defs(element);
  39. element.setAttribute('width', "" + this.get('width'));
  40. element.setAttribute('height', "" + this.get('height'));
  41. // 缓存 context 对象
  42. this.set('context', context);
  43. return element;
  44. };
  45. /**
  46. * 一些方法调用会引起画布变化
  47. * @param {ChangeType} changeType 改变的类型
  48. */
  49. Canvas.prototype.onCanvasChange = function (changeType) {
  50. var context = this.get('context');
  51. var el = this.get('el');
  52. if (changeType === 'sort') {
  53. var children_1 = this.get('children');
  54. if (children_1 && children_1.length) {
  55. sortDom(this, function (a, b) {
  56. return children_1.indexOf(a) - children_1.indexOf(b) ? 1 : 0;
  57. });
  58. }
  59. }
  60. else if (changeType === 'clear') {
  61. // el maybe null for canvas
  62. if (el) {
  63. // 清空 SVG 元素
  64. el.innerHTML = '';
  65. var defsEl = context.el;
  66. // 清空 defs 元素
  67. defsEl.innerHTML = '';
  68. // 将清空后的 defs 元素挂载到 el 下
  69. el.appendChild(defsEl);
  70. }
  71. }
  72. else if (changeType === 'matrix') {
  73. setTransform(this);
  74. }
  75. else if (changeType === 'clip') {
  76. setClip(this, context);
  77. }
  78. else if (changeType === 'changeSize') {
  79. el.setAttribute('width', "" + this.get('width'));
  80. el.setAttribute('height', "" + this.get('height'));
  81. }
  82. };
  83. // 复写基类的 draw 方法
  84. Canvas.prototype.draw = function () {
  85. var context = this.get('context');
  86. var children = this.getChildren();
  87. setClip(this, context);
  88. if (children.length) {
  89. drawChildren(context, children);
  90. }
  91. };
  92. return Canvas;
  93. }(AbstractCanvas));
  94. export default Canvas;
  95. //# sourceMappingURL=canvas.js.map