shadow.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * @fileoverview shadow
  3. * @author dengfuping_develop@163.com
  4. */
  5. import { each, uniqueId } from '@antv/util';
  6. import { createSVGElement } from '../util/dom';
  7. var ATTR_MAP = {
  8. shadowColor: 'color',
  9. shadowOpacity: 'opacity',
  10. shadowBlur: 'blur',
  11. shadowOffsetX: 'dx',
  12. shadowOffsetY: 'dy',
  13. };
  14. var SHADOW_DIMENSION = {
  15. x: '-40%',
  16. y: '-40%',
  17. width: '200%',
  18. height: '200%',
  19. };
  20. var Shadow = /** @class */ (function () {
  21. function Shadow(cfg) {
  22. this.type = 'filter';
  23. this.cfg = {};
  24. this.type = 'filter';
  25. var el = createSVGElement('filter');
  26. // expand the filter region to fill in shadows
  27. each(SHADOW_DIMENSION, function (v, k) {
  28. el.setAttribute(k, v);
  29. });
  30. this.el = el;
  31. this.id = uniqueId('filter_');
  32. this.el.id = this.id;
  33. this.cfg = cfg;
  34. this._parseShadow(cfg, el);
  35. return this;
  36. }
  37. Shadow.prototype.match = function (type, cfg) {
  38. if (this.type !== type) {
  39. return false;
  40. }
  41. var flag = true;
  42. var config = this.cfg;
  43. each(Object.keys(config), function (attr) {
  44. if (config[attr] !== cfg[attr]) {
  45. flag = false;
  46. return false;
  47. }
  48. });
  49. return flag;
  50. };
  51. Shadow.prototype.update = function (name, value) {
  52. var config = this.cfg;
  53. config[ATTR_MAP[name]] = value;
  54. this._parseShadow(config, this.el);
  55. return this;
  56. };
  57. Shadow.prototype._parseShadow = function (config, el) {
  58. var child = "<feDropShadow\n dx=\"" + (config.dx || 0) + "\"\n dy=\"" + (config.dy || 0) + "\"\n stdDeviation=\"" + (config.blur ? config.blur / 10 : 0) + "\"\n flood-color=\"" + (config.color ? config.color : '#000') + "\"\n flood-opacity=\"" + (config.opacity ? config.opacity : 1) + "\"\n />";
  59. el.innerHTML = child;
  60. };
  61. return Shadow;
  62. }());
  63. export default Shadow;
  64. //# sourceMappingURL=shadow.js.map