pattern.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * @fileoverview pattern
  3. * @author dengfuping_develop@163.com
  4. */
  5. import { uniqueId } from '@antv/util';
  6. import { createSVGElement } from '../util/dom';
  7. var regexPR = /^p\s*\(\s*([axyn])\s*\)\s*(.*)/i;
  8. var Pattern = /** @class */ (function () {
  9. function Pattern(cfg) {
  10. this.cfg = {};
  11. var el = createSVGElement('pattern');
  12. el.setAttribute('patternUnits', 'userSpaceOnUse');
  13. var child = createSVGElement('image');
  14. el.appendChild(child);
  15. var id = uniqueId('pattern_');
  16. el.id = id;
  17. this.el = el;
  18. this.id = id;
  19. this.cfg = cfg;
  20. var arr = regexPR.exec(cfg);
  21. var source = arr[2];
  22. child.setAttribute('href', source);
  23. var img = new Image();
  24. if (!source.match(/^data:/i)) {
  25. img.crossOrigin = 'Anonymous';
  26. }
  27. img.src = source;
  28. function onload() {
  29. el.setAttribute('width', "" + img.width);
  30. el.setAttribute('height', "" + img.height);
  31. }
  32. if (img.complete) {
  33. onload();
  34. }
  35. else {
  36. img.onload = onload;
  37. // Fix onload() bug in IE9
  38. img.src = img.src;
  39. }
  40. return this;
  41. }
  42. Pattern.prototype.match = function (type, attr) {
  43. return this.cfg === attr;
  44. };
  45. return Pattern;
  46. }());
  47. export default Pattern;
  48. //# sourceMappingURL=pattern.js.map