ShadowManager.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { __extends } from "tslib";
  2. import Definable from './Definable.js';
  3. import { getIdURL, getShadowKey, hasShadow, normalizeColor } from '../../svg/helper.js';
  4. import { createElement } from '../../svg/core.js';
  5. var ShadowManager = (function (_super) {
  6. __extends(ShadowManager, _super);
  7. function ShadowManager(zrId, svgRoot) {
  8. var _this = _super.call(this, zrId, svgRoot, ['filter'], '__filter_in_use__', '_shadowDom') || this;
  9. _this._shadowDomMap = {};
  10. _this._shadowDomPool = [];
  11. return _this;
  12. }
  13. ShadowManager.prototype._getFromPool = function () {
  14. var shadowDom = this._shadowDomPool.pop();
  15. if (!shadowDom) {
  16. shadowDom = createElement('filter');
  17. shadowDom.setAttribute('id', 'zr' + this._zrId + '-shadow-' + this.nextId++);
  18. var domChild = createElement('feDropShadow');
  19. shadowDom.appendChild(domChild);
  20. this.addDom(shadowDom);
  21. }
  22. return shadowDom;
  23. };
  24. ShadowManager.prototype.update = function (svgElement, displayable) {
  25. var style = displayable.style;
  26. if (hasShadow(style)) {
  27. var shadowKey = getShadowKey(displayable);
  28. var shadowDom = displayable._shadowDom = this._shadowDomMap[shadowKey];
  29. if (!shadowDom) {
  30. shadowDom = this._getFromPool();
  31. this._shadowDomMap[shadowKey] = shadowDom;
  32. }
  33. this.updateDom(svgElement, displayable, shadowDom);
  34. }
  35. else {
  36. this.remove(svgElement, displayable);
  37. }
  38. };
  39. ShadowManager.prototype.remove = function (svgElement, displayable) {
  40. if (displayable._shadowDom != null) {
  41. displayable._shadowDom = null;
  42. svgElement.removeAttribute('filter');
  43. }
  44. };
  45. ShadowManager.prototype.updateDom = function (svgElement, displayable, shadowDom) {
  46. var domChild = shadowDom.children[0];
  47. var style = displayable.style;
  48. var globalScale = displayable.getGlobalScale();
  49. var scaleX = globalScale[0];
  50. var scaleY = globalScale[1];
  51. if (!scaleX || !scaleY) {
  52. return;
  53. }
  54. var offsetX = style.shadowOffsetX || 0;
  55. var offsetY = style.shadowOffsetY || 0;
  56. var blur = style.shadowBlur;
  57. var normalizedColor = normalizeColor(style.shadowColor);
  58. domChild.setAttribute('dx', offsetX / scaleX + '');
  59. domChild.setAttribute('dy', offsetY / scaleY + '');
  60. domChild.setAttribute('flood-color', normalizedColor.color);
  61. domChild.setAttribute('flood-opacity', normalizedColor.opacity + '');
  62. var stdDx = blur / 2 / scaleX;
  63. var stdDy = blur / 2 / scaleY;
  64. var stdDeviation = stdDx + ' ' + stdDy;
  65. domChild.setAttribute('stdDeviation', stdDeviation);
  66. shadowDom.setAttribute('x', '-100%');
  67. shadowDom.setAttribute('y', '-100%');
  68. shadowDom.setAttribute('width', '300%');
  69. shadowDom.setAttribute('height', '300%');
  70. displayable._shadowDom = shadowDom;
  71. svgElement.setAttribute('filter', getIdURL(shadowDom.getAttribute('id')));
  72. };
  73. ShadowManager.prototype.removeUnused = function () {
  74. var defs = this.getDefs(false);
  75. if (!defs) {
  76. return;
  77. }
  78. var shadowDomsPool = this._shadowDomPool;
  79. var shadowDomMap = this._shadowDomMap;
  80. for (var key in shadowDomMap) {
  81. if (shadowDomMap.hasOwnProperty(key)) {
  82. shadowDomsPool.push(shadowDomMap[key]);
  83. }
  84. }
  85. this._shadowDomMap = {};
  86. };
  87. return ShadowManager;
  88. }(Definable));
  89. export default ShadowManager;