draw.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.refreshElement = exports.drawChildren = void 0;
  4. var svg_1 = require("./svg");
  5. var dom_1 = require("./dom");
  6. function drawChildren(context, children) {
  7. children.forEach(function (child) {
  8. child.draw(context);
  9. });
  10. }
  11. exports.drawChildren = drawChildren;
  12. /**
  13. * 更新元素,包括 group 和 shape
  14. * @param {IElement} element SVG 元素
  15. * @param {ChangeType} changeType 更新类型
  16. */
  17. function refreshElement(element, changeType) {
  18. // 对于还没有挂载到画布下的元素,canvas 可能为空
  19. var canvas = element.get('canvas');
  20. // 只有挂载到画布下,才对元素进行实际渲染
  21. if (canvas && canvas.get('autoDraw')) {
  22. var context = canvas.get('context');
  23. var parent_1 = element.getParent();
  24. var parentChildren = parent_1 ? parent_1.getChildren() : [canvas];
  25. var el = element.get('el');
  26. if (changeType === 'remove') {
  27. var isClipShape = element.get('isClipShape');
  28. // 对于 clip,不仅需要将 clipShape 对于的 SVG 元素删除,还需要将上层的 clipPath 元素也删除
  29. if (isClipShape) {
  30. var clipPathEl = el && el.parentNode;
  31. var defsEl = clipPathEl && clipPathEl.parentNode;
  32. if (clipPathEl && defsEl) {
  33. defsEl.removeChild(clipPathEl);
  34. }
  35. }
  36. else if (el && el.parentNode) {
  37. el.parentNode.removeChild(el);
  38. }
  39. }
  40. else if (changeType === 'show') {
  41. el.setAttribute('visibility', 'visible');
  42. }
  43. else if (changeType === 'hide') {
  44. el.setAttribute('visibility', 'hidden');
  45. }
  46. else if (changeType === 'zIndex') {
  47. dom_1.moveTo(el, parentChildren.indexOf(element));
  48. }
  49. else if (changeType === 'sort') {
  50. var children_1 = element.get('children');
  51. if (children_1 && children_1.length) {
  52. dom_1.sortDom(element, function (a, b) {
  53. return children_1.indexOf(a) - children_1.indexOf(b) ? 1 : 0;
  54. });
  55. }
  56. }
  57. else if (changeType === 'clear') {
  58. // el maybe null for group
  59. if (el) {
  60. el.innerHTML = '';
  61. }
  62. }
  63. else if (changeType === 'matrix') {
  64. svg_1.setTransform(element);
  65. }
  66. else if (changeType === 'clip') {
  67. svg_1.setClip(element, context);
  68. }
  69. else if (changeType === 'attr') {
  70. // 已在 afterAttrsChange 进行了处理,此处 do nothing
  71. }
  72. else if (changeType === 'add') {
  73. element.draw(context);
  74. }
  75. }
  76. }
  77. exports.refreshElement = refreshElement;
  78. //# sourceMappingURL=draw.js.map