image.js 3.0 KB

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