image.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * @fileoverview image
  3. * @author dengfuping_develop@163.com
  4. */
  5. import { __assign, __extends } from "tslib";
  6. import { each, isString } from '@antv/util';
  7. import { SVG_ATTR_MAP } from '../constant';
  8. import ShapeBase from './base';
  9. var Image = /** @class */ (function (_super) {
  10. __extends(Image, _super);
  11. function Image() {
  12. var _this = _super !== null && _super.apply(this, arguments) || this;
  13. _this.type = 'image';
  14. _this.canFill = false;
  15. _this.canStroke = false;
  16. return _this;
  17. }
  18. Image.prototype.getDefaultAttrs = function () {
  19. var attrs = _super.prototype.getDefaultAttrs.call(this);
  20. return __assign(__assign({}, attrs), { x: 0, y: 0, width: 0, height: 0 });
  21. };
  22. Image.prototype.createPath = function (context, targetAttrs) {
  23. var _this = this;
  24. var attrs = this.attr();
  25. var el = this.get('el');
  26. each(targetAttrs || attrs, function (value, attr) {
  27. if (attr === 'img') {
  28. _this._setImage(attrs.img);
  29. }
  30. else if (SVG_ATTR_MAP[attr]) {
  31. el.setAttribute(SVG_ATTR_MAP[attr], value);
  32. }
  33. });
  34. };
  35. Image.prototype.setAttr = function (name, value) {
  36. this.attrs[name] = value;
  37. if (name === 'img') {
  38. this._setImage(value);
  39. }
  40. };
  41. Image.prototype._setImage = function (img) {
  42. var attrs = this.attr();
  43. var el = this.get('el');
  44. if (isString(img)) {
  45. el.setAttribute('href', img);
  46. }
  47. else if (img instanceof window.Image) {
  48. if (!attrs.width) {
  49. el.setAttribute('width', img.width);
  50. this.attr('width', img.width);
  51. }
  52. if (!attrs.height) {
  53. el.setAttribute('height', img.height);
  54. this.attr('height', img.height);
  55. }
  56. el.setAttribute('href', img.src);
  57. }
  58. else if (img instanceof HTMLElement && isString(img.nodeName) && img.nodeName.toUpperCase() === 'CANVAS') {
  59. // @ts-ignore
  60. el.setAttribute('href', img.toDataURL());
  61. }
  62. else if (img instanceof ImageData) {
  63. var canvas = document.createElement('canvas');
  64. canvas.setAttribute('width', "" + img.width);
  65. canvas.setAttribute('height', "" + img.height);
  66. canvas.getContext('2d').putImageData(img, 0, 0);
  67. if (!attrs.width) {
  68. el.setAttribute('width', "" + img.width);
  69. this.attr('width', img.width);
  70. }
  71. if (!attrs.height) {
  72. el.setAttribute('height', "" + img.height);
  73. this.attr('height', img.height);
  74. }
  75. el.setAttribute('href', canvas.toDataURL());
  76. }
  77. };
  78. return Image;
  79. }(ShapeBase));
  80. export default Image;
  81. //# sourceMappingURL=image.js.map