draw.js 2.6 KB

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