1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.Image = void 0;
|
---|
4 | const Util_1 = require("../Util");
|
---|
5 | const Factory_1 = require("../Factory");
|
---|
6 | const Shape_1 = require("../Shape");
|
---|
7 | const Global_1 = require("../Global");
|
---|
8 | const Validators_1 = require("../Validators");
|
---|
9 | class Image extends Shape_1.Shape {
|
---|
10 | constructor(attrs) {
|
---|
11 | super(attrs);
|
---|
12 | this.on('imageChange.konva', () => {
|
---|
13 | this._setImageLoad();
|
---|
14 | });
|
---|
15 | this._setImageLoad();
|
---|
16 | }
|
---|
17 | _setImageLoad() {
|
---|
18 | const image = this.image();
|
---|
19 | if (image && image.complete) {
|
---|
20 | return;
|
---|
21 | }
|
---|
22 | if (image && image.readyState === 4) {
|
---|
23 | return;
|
---|
24 | }
|
---|
25 | if (image && image['addEventListener']) {
|
---|
26 | image['addEventListener']('load', () => {
|
---|
27 | this._requestDraw();
|
---|
28 | });
|
---|
29 | }
|
---|
30 | }
|
---|
31 | _useBufferCanvas() {
|
---|
32 | const hasCornerRadius = !!this.cornerRadius();
|
---|
33 | const hasShadow = this.hasShadow();
|
---|
34 | if (hasCornerRadius && hasShadow) {
|
---|
35 | return true;
|
---|
36 | }
|
---|
37 | return super._useBufferCanvas(true);
|
---|
38 | }
|
---|
39 | _sceneFunc(context) {
|
---|
40 | const width = this.getWidth();
|
---|
41 | const height = this.getHeight();
|
---|
42 | const cornerRadius = this.cornerRadius();
|
---|
43 | const image = this.attrs.image;
|
---|
44 | let params;
|
---|
45 | if (image) {
|
---|
46 | const cropWidth = this.attrs.cropWidth;
|
---|
47 | const cropHeight = this.attrs.cropHeight;
|
---|
48 | if (cropWidth && cropHeight) {
|
---|
49 | params = [
|
---|
50 | image,
|
---|
51 | this.cropX(),
|
---|
52 | this.cropY(),
|
---|
53 | cropWidth,
|
---|
54 | cropHeight,
|
---|
55 | 0,
|
---|
56 | 0,
|
---|
57 | width,
|
---|
58 | height,
|
---|
59 | ];
|
---|
60 | }
|
---|
61 | else {
|
---|
62 | params = [image, 0, 0, width, height];
|
---|
63 | }
|
---|
64 | }
|
---|
65 | if (this.hasFill() || this.hasStroke() || cornerRadius) {
|
---|
66 | context.beginPath();
|
---|
67 | cornerRadius
|
---|
68 | ? Util_1.Util.drawRoundedRectPath(context, width, height, cornerRadius)
|
---|
69 | : context.rect(0, 0, width, height);
|
---|
70 | context.closePath();
|
---|
71 | context.fillStrokeShape(this);
|
---|
72 | }
|
---|
73 | if (image) {
|
---|
74 | if (cornerRadius) {
|
---|
75 | context.clip();
|
---|
76 | }
|
---|
77 | context.drawImage.apply(context, params);
|
---|
78 | }
|
---|
79 | }
|
---|
80 | _hitFunc(context) {
|
---|
81 | var width = this.width(), height = this.height(), cornerRadius = this.cornerRadius();
|
---|
82 | context.beginPath();
|
---|
83 | if (!cornerRadius) {
|
---|
84 | context.rect(0, 0, width, height);
|
---|
85 | }
|
---|
86 | else {
|
---|
87 | Util_1.Util.drawRoundedRectPath(context, width, height, cornerRadius);
|
---|
88 | }
|
---|
89 | context.closePath();
|
---|
90 | context.fillStrokeShape(this);
|
---|
91 | }
|
---|
92 | getWidth() {
|
---|
93 | var _a, _b;
|
---|
94 | return (_a = this.attrs.width) !== null && _a !== void 0 ? _a : (_b = this.image()) === null || _b === void 0 ? void 0 : _b.width;
|
---|
95 | }
|
---|
96 | getHeight() {
|
---|
97 | var _a, _b;
|
---|
98 | return (_a = this.attrs.height) !== null && _a !== void 0 ? _a : (_b = this.image()) === null || _b === void 0 ? void 0 : _b.height;
|
---|
99 | }
|
---|
100 | static fromURL(url, callback, onError = null) {
|
---|
101 | var img = Util_1.Util.createImageElement();
|
---|
102 | img.onload = function () {
|
---|
103 | var image = new Image({
|
---|
104 | image: img,
|
---|
105 | });
|
---|
106 | callback(image);
|
---|
107 | };
|
---|
108 | img.onerror = onError;
|
---|
109 | img.crossOrigin = 'Anonymous';
|
---|
110 | img.src = url;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | exports.Image = Image;
|
---|
114 | Image.prototype.className = 'Image';
|
---|
115 | (0, Global_1._registerNode)(Image);
|
---|
116 | Factory_1.Factory.addGetterSetter(Image, 'cornerRadius', 0, (0, Validators_1.getNumberOrArrayOfNumbersValidator)(4));
|
---|
117 | Factory_1.Factory.addGetterSetter(Image, 'image');
|
---|
118 | Factory_1.Factory.addComponentsGetterSetter(Image, 'crop', ['x', 'y', 'width', 'height']);
|
---|
119 | Factory_1.Factory.addGetterSetter(Image, 'cropX', 0, (0, Validators_1.getNumberValidator)());
|
---|
120 | Factory_1.Factory.addGetterSetter(Image, 'cropY', 0, (0, Validators_1.getNumberValidator)());
|
---|
121 | Factory_1.Factory.addGetterSetter(Image, 'cropWidth', 0, (0, Validators_1.getNumberValidator)());
|
---|
122 | Factory_1.Factory.addGetterSetter(Image, 'cropHeight', 0, (0, Validators_1.getNumberValidator)());
|
---|