1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.Layer = void 0;
|
---|
4 | const Util_1 = require("./Util");
|
---|
5 | const Container_1 = require("./Container");
|
---|
6 | const Node_1 = require("./Node");
|
---|
7 | const Factory_1 = require("./Factory");
|
---|
8 | const Canvas_1 = require("./Canvas");
|
---|
9 | const Validators_1 = require("./Validators");
|
---|
10 | const Shape_1 = require("./Shape");
|
---|
11 | const Global_1 = require("./Global");
|
---|
12 | var HASH = '#', BEFORE_DRAW = 'beforeDraw', DRAW = 'draw', INTERSECTION_OFFSETS = [
|
---|
13 | { x: 0, y: 0 },
|
---|
14 | { x: -1, y: -1 },
|
---|
15 | { x: 1, y: -1 },
|
---|
16 | { x: 1, y: 1 },
|
---|
17 | { x: -1, y: 1 },
|
---|
18 | ], INTERSECTION_OFFSETS_LEN = INTERSECTION_OFFSETS.length;
|
---|
19 | class Layer extends Container_1.Container {
|
---|
20 | constructor(config) {
|
---|
21 | super(config);
|
---|
22 | this.canvas = new Canvas_1.SceneCanvas();
|
---|
23 | this.hitCanvas = new Canvas_1.HitCanvas({
|
---|
24 | pixelRatio: 1,
|
---|
25 | });
|
---|
26 | this._waitingForDraw = false;
|
---|
27 | this.on('visibleChange.konva', this._checkVisibility);
|
---|
28 | this._checkVisibility();
|
---|
29 | this.on('imageSmoothingEnabledChange.konva', this._setSmoothEnabled);
|
---|
30 | this._setSmoothEnabled();
|
---|
31 | }
|
---|
32 | createPNGStream() {
|
---|
33 | const c = this.canvas._canvas;
|
---|
34 | return c.createPNGStream();
|
---|
35 | }
|
---|
36 | getCanvas() {
|
---|
37 | return this.canvas;
|
---|
38 | }
|
---|
39 | getNativeCanvasElement() {
|
---|
40 | return this.canvas._canvas;
|
---|
41 | }
|
---|
42 | getHitCanvas() {
|
---|
43 | return this.hitCanvas;
|
---|
44 | }
|
---|
45 | getContext() {
|
---|
46 | return this.getCanvas().getContext();
|
---|
47 | }
|
---|
48 | clear(bounds) {
|
---|
49 | this.getContext().clear(bounds);
|
---|
50 | this.getHitCanvas().getContext().clear(bounds);
|
---|
51 | return this;
|
---|
52 | }
|
---|
53 | setZIndex(index) {
|
---|
54 | super.setZIndex(index);
|
---|
55 | var stage = this.getStage();
|
---|
56 | if (stage && stage.content) {
|
---|
57 | stage.content.removeChild(this.getNativeCanvasElement());
|
---|
58 | if (index < stage.children.length - 1) {
|
---|
59 | stage.content.insertBefore(this.getNativeCanvasElement(), stage.children[index + 1].getCanvas()._canvas);
|
---|
60 | }
|
---|
61 | else {
|
---|
62 | stage.content.appendChild(this.getNativeCanvasElement());
|
---|
63 | }
|
---|
64 | }
|
---|
65 | return this;
|
---|
66 | }
|
---|
67 | moveToTop() {
|
---|
68 | Node_1.Node.prototype.moveToTop.call(this);
|
---|
69 | var stage = this.getStage();
|
---|
70 | if (stage && stage.content) {
|
---|
71 | stage.content.removeChild(this.getNativeCanvasElement());
|
---|
72 | stage.content.appendChild(this.getNativeCanvasElement());
|
---|
73 | }
|
---|
74 | return true;
|
---|
75 | }
|
---|
76 | moveUp() {
|
---|
77 | var moved = Node_1.Node.prototype.moveUp.call(this);
|
---|
78 | if (!moved) {
|
---|
79 | return false;
|
---|
80 | }
|
---|
81 | var stage = this.getStage();
|
---|
82 | if (!stage || !stage.content) {
|
---|
83 | return false;
|
---|
84 | }
|
---|
85 | stage.content.removeChild(this.getNativeCanvasElement());
|
---|
86 | if (this.index < stage.children.length - 1) {
|
---|
87 | stage.content.insertBefore(this.getNativeCanvasElement(), stage.children[this.index + 1].getCanvas()._canvas);
|
---|
88 | }
|
---|
89 | else {
|
---|
90 | stage.content.appendChild(this.getNativeCanvasElement());
|
---|
91 | }
|
---|
92 | return true;
|
---|
93 | }
|
---|
94 | moveDown() {
|
---|
95 | if (Node_1.Node.prototype.moveDown.call(this)) {
|
---|
96 | var stage = this.getStage();
|
---|
97 | if (stage) {
|
---|
98 | var children = stage.children;
|
---|
99 | if (stage.content) {
|
---|
100 | stage.content.removeChild(this.getNativeCanvasElement());
|
---|
101 | stage.content.insertBefore(this.getNativeCanvasElement(), children[this.index + 1].getCanvas()._canvas);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 | return false;
|
---|
107 | }
|
---|
108 | moveToBottom() {
|
---|
109 | if (Node_1.Node.prototype.moveToBottom.call(this)) {
|
---|
110 | var stage = this.getStage();
|
---|
111 | if (stage) {
|
---|
112 | var children = stage.children;
|
---|
113 | if (stage.content) {
|
---|
114 | stage.content.removeChild(this.getNativeCanvasElement());
|
---|
115 | stage.content.insertBefore(this.getNativeCanvasElement(), children[1].getCanvas()._canvas);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | return true;
|
---|
119 | }
|
---|
120 | return false;
|
---|
121 | }
|
---|
122 | getLayer() {
|
---|
123 | return this;
|
---|
124 | }
|
---|
125 | remove() {
|
---|
126 | var _canvas = this.getNativeCanvasElement();
|
---|
127 | Node_1.Node.prototype.remove.call(this);
|
---|
128 | if (_canvas && _canvas.parentNode && Util_1.Util._isInDocument(_canvas)) {
|
---|
129 | _canvas.parentNode.removeChild(_canvas);
|
---|
130 | }
|
---|
131 | return this;
|
---|
132 | }
|
---|
133 | getStage() {
|
---|
134 | return this.parent;
|
---|
135 | }
|
---|
136 | setSize({ width, height }) {
|
---|
137 | this.canvas.setSize(width, height);
|
---|
138 | this.hitCanvas.setSize(width, height);
|
---|
139 | this._setSmoothEnabled();
|
---|
140 | return this;
|
---|
141 | }
|
---|
142 | _validateAdd(child) {
|
---|
143 | var type = child.getType();
|
---|
144 | if (type !== 'Group' && type !== 'Shape') {
|
---|
145 | Util_1.Util.throw('You may only add groups and shapes to a layer.');
|
---|
146 | }
|
---|
147 | }
|
---|
148 | _toKonvaCanvas(config) {
|
---|
149 | config = config || {};
|
---|
150 | config.width = config.width || this.getWidth();
|
---|
151 | config.height = config.height || this.getHeight();
|
---|
152 | config.x = config.x !== undefined ? config.x : this.x();
|
---|
153 | config.y = config.y !== undefined ? config.y : this.y();
|
---|
154 | return Node_1.Node.prototype._toKonvaCanvas.call(this, config);
|
---|
155 | }
|
---|
156 | _checkVisibility() {
|
---|
157 | const visible = this.visible();
|
---|
158 | if (visible) {
|
---|
159 | this.canvas._canvas.style.display = 'block';
|
---|
160 | }
|
---|
161 | else {
|
---|
162 | this.canvas._canvas.style.display = 'none';
|
---|
163 | }
|
---|
164 | }
|
---|
165 | _setSmoothEnabled() {
|
---|
166 | this.getContext()._context.imageSmoothingEnabled =
|
---|
167 | this.imageSmoothingEnabled();
|
---|
168 | }
|
---|
169 | getWidth() {
|
---|
170 | if (this.parent) {
|
---|
171 | return this.parent.width();
|
---|
172 | }
|
---|
173 | }
|
---|
174 | setWidth() {
|
---|
175 | Util_1.Util.warn('Can not change width of layer. Use "stage.width(value)" function instead.');
|
---|
176 | }
|
---|
177 | getHeight() {
|
---|
178 | if (this.parent) {
|
---|
179 | return this.parent.height();
|
---|
180 | }
|
---|
181 | }
|
---|
182 | setHeight() {
|
---|
183 | Util_1.Util.warn('Can not change height of layer. Use "stage.height(value)" function instead.');
|
---|
184 | }
|
---|
185 | batchDraw() {
|
---|
186 | if (!this._waitingForDraw) {
|
---|
187 | this._waitingForDraw = true;
|
---|
188 | Util_1.Util.requestAnimFrame(() => {
|
---|
189 | this.draw();
|
---|
190 | this._waitingForDraw = false;
|
---|
191 | });
|
---|
192 | }
|
---|
193 | return this;
|
---|
194 | }
|
---|
195 | getIntersection(pos) {
|
---|
196 | if (!this.isListening() || !this.isVisible()) {
|
---|
197 | return null;
|
---|
198 | }
|
---|
199 | var spiralSearchDistance = 1;
|
---|
200 | var continueSearch = false;
|
---|
201 | while (true) {
|
---|
202 | for (let i = 0; i < INTERSECTION_OFFSETS_LEN; i++) {
|
---|
203 | const intersectionOffset = INTERSECTION_OFFSETS[i];
|
---|
204 | const obj = this._getIntersection({
|
---|
205 | x: pos.x + intersectionOffset.x * spiralSearchDistance,
|
---|
206 | y: pos.y + intersectionOffset.y * spiralSearchDistance,
|
---|
207 | });
|
---|
208 | const shape = obj.shape;
|
---|
209 | if (shape) {
|
---|
210 | return shape;
|
---|
211 | }
|
---|
212 | continueSearch = !!obj.antialiased;
|
---|
213 | if (!obj.antialiased) {
|
---|
214 | break;
|
---|
215 | }
|
---|
216 | }
|
---|
217 | if (continueSearch) {
|
---|
218 | spiralSearchDistance += 1;
|
---|
219 | }
|
---|
220 | else {
|
---|
221 | return null;
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|
225 | _getIntersection(pos) {
|
---|
226 | const ratio = this.hitCanvas.pixelRatio;
|
---|
227 | const p = this.hitCanvas.context.getImageData(Math.round(pos.x * ratio), Math.round(pos.y * ratio), 1, 1).data;
|
---|
228 | const p3 = p[3];
|
---|
229 | if (p3 === 255) {
|
---|
230 | const colorKey = Util_1.Util._rgbToHex(p[0], p[1], p[2]);
|
---|
231 | const shape = Shape_1.shapes[HASH + colorKey];
|
---|
232 | if (shape) {
|
---|
233 | return {
|
---|
234 | shape: shape,
|
---|
235 | };
|
---|
236 | }
|
---|
237 | return {
|
---|
238 | antialiased: true,
|
---|
239 | };
|
---|
240 | }
|
---|
241 | else if (p3 > 0) {
|
---|
242 | return {
|
---|
243 | antialiased: true,
|
---|
244 | };
|
---|
245 | }
|
---|
246 | return {};
|
---|
247 | }
|
---|
248 | drawScene(can, top) {
|
---|
249 | var layer = this.getLayer(), canvas = can || (layer && layer.getCanvas());
|
---|
250 | this._fire(BEFORE_DRAW, {
|
---|
251 | node: this,
|
---|
252 | });
|
---|
253 | if (this.clearBeforeDraw()) {
|
---|
254 | canvas.getContext().clear();
|
---|
255 | }
|
---|
256 | Container_1.Container.prototype.drawScene.call(this, canvas, top);
|
---|
257 | this._fire(DRAW, {
|
---|
258 | node: this,
|
---|
259 | });
|
---|
260 | return this;
|
---|
261 | }
|
---|
262 | drawHit(can, top) {
|
---|
263 | var layer = this.getLayer(), canvas = can || (layer && layer.hitCanvas);
|
---|
264 | if (layer && layer.clearBeforeDraw()) {
|
---|
265 | layer.getHitCanvas().getContext().clear();
|
---|
266 | }
|
---|
267 | Container_1.Container.prototype.drawHit.call(this, canvas, top);
|
---|
268 | return this;
|
---|
269 | }
|
---|
270 | enableHitGraph() {
|
---|
271 | this.hitGraphEnabled(true);
|
---|
272 | return this;
|
---|
273 | }
|
---|
274 | disableHitGraph() {
|
---|
275 | this.hitGraphEnabled(false);
|
---|
276 | return this;
|
---|
277 | }
|
---|
278 | setHitGraphEnabled(val) {
|
---|
279 | Util_1.Util.warn('hitGraphEnabled method is deprecated. Please use layer.listening() instead.');
|
---|
280 | this.listening(val);
|
---|
281 | }
|
---|
282 | getHitGraphEnabled(val) {
|
---|
283 | Util_1.Util.warn('hitGraphEnabled method is deprecated. Please use layer.listening() instead.');
|
---|
284 | return this.listening();
|
---|
285 | }
|
---|
286 | toggleHitCanvas() {
|
---|
287 | if (!this.parent || !this.parent['content']) {
|
---|
288 | return;
|
---|
289 | }
|
---|
290 | var parent = this.parent;
|
---|
291 | var added = !!this.hitCanvas._canvas.parentNode;
|
---|
292 | if (added) {
|
---|
293 | parent.content.removeChild(this.hitCanvas._canvas);
|
---|
294 | }
|
---|
295 | else {
|
---|
296 | parent.content.appendChild(this.hitCanvas._canvas);
|
---|
297 | }
|
---|
298 | }
|
---|
299 | destroy() {
|
---|
300 | Util_1.Util.releaseCanvas(this.getNativeCanvasElement(), this.getHitCanvas()._canvas);
|
---|
301 | return super.destroy();
|
---|
302 | }
|
---|
303 | }
|
---|
304 | exports.Layer = Layer;
|
---|
305 | Layer.prototype.nodeType = 'Layer';
|
---|
306 | (0, Global_1._registerNode)(Layer);
|
---|
307 | Factory_1.Factory.addGetterSetter(Layer, 'imageSmoothingEnabled', true);
|
---|
308 | Factory_1.Factory.addGetterSetter(Layer, 'clearBeforeDraw', true);
|
---|
309 | Factory_1.Factory.addGetterSetter(Layer, 'hitGraphEnabled', true, (0, Validators_1.getBooleanValidator)());
|
---|