konva 10.3.1 → 10.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/Canvas.d.ts CHANGED
@@ -21,6 +21,7 @@ export declare class Canvas {
21
21
  getWidth(): number;
22
22
  getHeight(): number;
23
23
  setSize(width: any, height: any): void;
24
+ setSizeIfChanged(width: number, height: number): void;
24
25
  toDataURL(mimeType: any, quality: any): string;
25
26
  }
26
27
  export declare class SceneCanvas extends Canvas {
package/lib/Canvas.js CHANGED
@@ -71,6 +71,12 @@ export class Canvas {
71
71
  this.setWidth(width || 0);
72
72
  this.setHeight(height || 0);
73
73
  }
74
+ setSizeIfChanged(width, height) {
75
+ if (this.width !== width * this.pixelRatio ||
76
+ this.height !== height * this.pixelRatio) {
77
+ this.setSize(width, height);
78
+ }
79
+ }
74
80
  toDataURL(mimeType, quality) {
75
81
  try {
76
82
  return this._canvas.toDataURL(mimeType, quality);
package/lib/Container.js CHANGED
@@ -195,12 +195,12 @@ export class Container extends Node {
195
195
  if (!this.shouldDrawHit(top)) {
196
196
  return this;
197
197
  }
198
- const layer = this.getLayer(), canvas = can || (layer && layer.hitCanvas), context = canvas && canvas.getContext(), cachedCanvas = this._getCanvasCache(), cachedHitCanvas = cachedCanvas && cachedCanvas.hit;
198
+ const layer = this.getLayer(), canvas = can || (layer && layer.hitCanvas), context = canvas && canvas.getContext(), cachedHitCanvas = this._getCachedHitCanvas(top);
199
199
  if (cachedHitCanvas) {
200
200
  context.save();
201
201
  const m = this.getAbsoluteTransform(top).getMatrix();
202
202
  context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
203
- this._drawCachedHitCanvas(context);
203
+ this._drawCachedHitCanvas(context, cachedHitCanvas);
204
204
  context.restore();
205
205
  }
206
206
  else {
@@ -1,18 +1,21 @@
1
1
  import type { Node } from './Node.ts';
2
2
  import type { Vector2d } from './types.ts';
3
+ type DragElement = {
4
+ node: Node;
5
+ startPointerPos: Vector2d;
6
+ offset: Vector2d;
7
+ pointerId?: number;
8
+ startEvent?: any;
9
+ dragStatus: 'ready' | 'dragging' | 'stopped';
10
+ };
3
11
  export declare const DD: {
4
12
  readonly isDragging: boolean;
5
13
  justDragged: boolean;
6
14
  readonly node: Node<import("./Node.ts").NodeConfig> | undefined;
7
- _dragElements: Map<number, {
8
- node: Node;
9
- startPointerPos: Vector2d;
10
- offset: Vector2d;
11
- pointerId?: number;
12
- startEvent?: any;
13
- dragStatus: "ready" | "dragging" | "stopped";
14
- }>;
15
- _drag(evt: any): void;
16
- _endDragBefore(evt?: any): void;
15
+ _dragElements: Map<number, DragElement>;
16
+ _listenToWindow(win: Window): void;
17
+ _drag(evt: any, win?: Window): void;
18
+ _endDragBefore(evt?: any, win?: Window): void;
17
19
  _endDragAfter(evt: any): void;
18
20
  };
21
+ export {};
@@ -19,11 +19,26 @@ export const DD = {
19
19
  return node;
20
20
  },
21
21
  _dragElements: new Map(),
22
- _drag(evt) {
22
+ _listenToWindow(win) {
23
+ const endDragBefore = (evt) => DD._endDragBefore(evt, win);
24
+ const drag = (evt) => DD._drag(evt, win);
25
+ win.addEventListener('mouseup', endDragBefore, true);
26
+ win.addEventListener('touchend', endDragBefore, true);
27
+ win.addEventListener('touchcancel', endDragBefore, true);
28
+ win.addEventListener('mousemove', drag);
29
+ win.addEventListener('touchmove', drag);
30
+ win.addEventListener('mouseup', DD._endDragAfter, false);
31
+ win.addEventListener('touchend', DD._endDragAfter, false);
32
+ win.addEventListener('touchcancel', DD._endDragAfter, false);
33
+ },
34
+ _drag(evt, win) {
23
35
  const nodesToFireEvents = [];
24
36
  DD._dragElements.forEach((elem, key) => {
25
37
  const { node } = elem;
26
38
  const stage = node.getStage();
39
+ if (win && stage._getOwnerWindow() !== win) {
40
+ return;
41
+ }
27
42
  stage.setPointersPositions(evt);
28
43
  if (elem.pointerId === undefined) {
29
44
  elem.pointerId = Util._getFirstPointerId(evt);
@@ -57,12 +72,12 @@ export const DD = {
57
72
  }, true);
58
73
  });
59
74
  },
60
- _endDragBefore(evt) {
75
+ _endDragBefore(evt, win) {
61
76
  const drawNodes = [];
62
77
  DD._dragElements.forEach((elem) => {
63
78
  const { node } = elem;
64
79
  const stage = node.getStage();
65
- if (evt) {
80
+ if (evt && (!win || stage._getOwnerWindow() === win)) {
66
81
  stage.setPointersPositions(evt);
67
82
  }
68
83
  const pos = stage._changedPointerPositions.find((pos) => pos.id === elem.pointerId);
@@ -101,13 +116,3 @@ export const DD = {
101
116
  });
102
117
  },
103
118
  };
104
- if (Konva.isBrowser) {
105
- window.addEventListener('mouseup', DD._endDragBefore, true);
106
- window.addEventListener('touchend', DD._endDragBefore, true);
107
- window.addEventListener('touchcancel', DD._endDragBefore, true);
108
- window.addEventListener('mousemove', DD._drag);
109
- window.addEventListener('touchmove', DD._drag);
110
- window.addEventListener('mouseup', DD._endDragAfter, false);
111
- window.addEventListener('touchend', DD._endDragAfter, false);
112
- window.addEventListener('touchcancel', DD._endDragAfter, false);
113
- }
package/lib/Global.js CHANGED
@@ -13,7 +13,7 @@ export const glob = typeof global !== 'undefined'
13
13
  : {};
14
14
  export const Konva = {
15
15
  _global: glob,
16
- version: '10.3.1',
16
+ version: '10.3.3',
17
17
  isBrowser: detectBrowser(),
18
18
  isUnminified: /param/.test(function (param) { }.toString()),
19
19
  dblClickWindow: 400,
package/lib/Layer.d.ts CHANGED
@@ -34,6 +34,7 @@ export declare class Layer extends Container<Group | Shape> {
34
34
  width: any;
35
35
  height: any;
36
36
  }): this;
37
+ _syncHitCanvasSize(): void;
37
38
  _validateAdd(child: any): void;
38
39
  _toKonvaCanvas(config: any): SceneCanvas;
39
40
  _checkVisibility(): void;
package/lib/Layer.js CHANGED
@@ -6,7 +6,7 @@ import { SceneCanvas, HitCanvas } from "./Canvas.js";
6
6
  import { getBooleanValidator } from "./Validators.js";
7
7
  import { shapes } from "./Shape.js";
8
8
  import { _registerNode } from "./Global.js";
9
- const HASH = '#', BEFORE_DRAW = 'beforeDraw', DRAW = 'draw', INTERSECTION_OFFSETS = [
9
+ const BEFORE_DRAW = 'beforeDraw', DRAW = 'draw', INTERSECTION_OFFSETS = [
10
10
  { x: 0, y: 0 },
11
11
  { x: -1, y: -1 },
12
12
  { x: 1, y: -1 },
@@ -132,10 +132,14 @@ export class Layer extends Container {
132
132
  }
133
133
  setSize({ width, height }) {
134
134
  this.canvas.setSize(width, height);
135
- this.hitCanvas.setSize(width, height);
135
+ this._syncHitCanvasSize();
136
136
  this._setSmoothEnabled();
137
137
  return this;
138
138
  }
139
+ _syncHitCanvasSize() {
140
+ const listening = this.isListening();
141
+ this.hitCanvas.setSizeIfChanged((listening ? this.getWidth() : 0) || 0, (listening ? this.getHeight() : 0) || 0);
142
+ }
139
143
  _validateAdd(child) {
140
144
  const type = child.getType();
141
145
  if (type !== 'Group' && type !== 'Shape') {
@@ -180,12 +184,13 @@ export class Layer extends Container {
180
184
  Util.warn('Can not change height of layer. Use "stage.height(value)" function instead.');
181
185
  }
182
186
  batchDraw() {
187
+ var _a;
183
188
  if (!this._waitingForDraw) {
184
189
  this._waitingForDraw = true;
185
190
  Util.requestAnimFrame(() => {
186
191
  this.draw();
187
192
  this._waitingForDraw = false;
188
- });
193
+ }, (_a = this.getStage()) === null || _a === void 0 ? void 0 : _a._getOwnerWindow());
189
194
  }
190
195
  return this;
191
196
  }
@@ -220,6 +225,9 @@ export class Layer extends Container {
220
225
  }
221
226
  }
222
227
  _getIntersection(pos) {
228
+ if (!this.hitCanvas.width || !this.hitCanvas.height) {
229
+ return {};
230
+ }
223
231
  const ratio = this.hitCanvas.pixelRatio;
224
232
  const p = this.hitCanvas.context.getImageData(Math.round(pos.x * ratio), Math.round(pos.y * ratio), 1, 1).data;
225
233
  const p3 = p[3];
@@ -258,8 +266,11 @@ export class Layer extends Container {
258
266
  }
259
267
  drawHit(can, top) {
260
268
  const layer = this.getLayer(), canvas = can || (layer && layer.hitCanvas);
261
- if (layer && layer.clearBeforeDraw()) {
262
- layer.getHitCanvas().getContext().clear();
269
+ if (!can && layer) {
270
+ layer._syncHitCanvasSize();
271
+ if (layer.clearBeforeDraw()) {
272
+ layer.getHitCanvas().getContext().clear();
273
+ }
263
274
  }
264
275
  Container.prototype.drawHit.call(this, canvas, top);
265
276
  return this;
package/lib/Node.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Canvas } from './Canvas.ts';
2
- import { SceneCanvas } from './Canvas.ts';
2
+ import { HitCanvas, SceneCanvas } from './Canvas.ts';
3
3
  import type { Container } from './Container.ts';
4
4
  import type { Context } from './Context.ts';
5
5
  import type { Layer } from './Layer.ts';
@@ -104,6 +104,7 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
104
104
  offset?: number;
105
105
  hitCanvasPixelRatio?: number;
106
106
  }): this | undefined;
107
+ _getCachedHitCanvas(top?: Node): HitCanvas | null;
107
108
  isCached(): boolean;
108
109
  abstract drawScene(canvas?: Canvas, top?: Node, bufferCanvas?: Canvas): void;
109
110
  abstract drawHit(canvas?: Canvas, top?: Node): void;
@@ -125,7 +126,7 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
125
126
  height: number;
126
127
  };
127
128
  _drawCachedSceneCanvas(context: Context): void;
128
- _drawCachedHitCanvas(context: Context): void;
129
+ _drawCachedHitCanvas(context: Context, hitCanvas: HitCanvas): void;
129
130
  _getCachedSceneCanvas(): Canvas;
130
131
  on<K extends keyof NodeEventMap>(evtStr: K, handler: KonvaEventListener<this, NodeEventMap[K]>): this;
131
132
  on<K extends keyof NodeEventMap>(evtStr: K, selector: string, handler: KonvaEventListener<Node, NodeEventMap[K]>): this;
package/lib/Node.js CHANGED
@@ -163,7 +163,7 @@ export class Node {
163
163
  clearCache() {
164
164
  if (this._cache.has(CANVAS)) {
165
165
  const { scene, filter, hit } = this._cache.get(CANVAS);
166
- Util.releaseCanvas(scene._canvas, filter._canvas, hit._canvas);
166
+ Util.releaseCanvas(scene._canvas, filter._canvas, ...(hit ? [hit._canvas] : []));
167
167
  this._cache.delete(CANVAS);
168
168
  }
169
169
  this._clearSelfAndDescendantCache();
@@ -202,17 +202,12 @@ export class Node {
202
202
  width: 0,
203
203
  height: 0,
204
204
  willReadFrequently: true,
205
- }), cachedHitCanvas = new HitCanvas({
206
- pixelRatio: hitCanvasPixelRatio,
207
- width: width,
208
- height: height,
209
- }), sceneContext = cachedSceneCanvas.getContext(), hitContext = cachedHitCanvas.getContext();
205
+ }), sceneContext = cachedSceneCanvas.getContext();
210
206
  const bufferCanvas = new SceneCanvas({
211
207
  width: cachedSceneCanvas.width / cachedSceneCanvas.pixelRatio + Math.abs(x),
212
208
  height: cachedSceneCanvas.height / cachedSceneCanvas.pixelRatio + Math.abs(y),
213
209
  pixelRatio: cachedSceneCanvas.pixelRatio,
214
210
  }), bufferContext = bufferCanvas.getContext();
215
- cachedHitCanvas.isCache = true;
216
211
  cachedSceneCanvas.isCache = true;
217
212
  this._cache.delete(CANVAS);
218
213
  this._filterUpToDate = false;
@@ -221,10 +216,8 @@ export class Node {
221
216
  cachedFilterCanvas.getContext()._context.imageSmoothingEnabled = false;
222
217
  }
223
218
  sceneContext.save();
224
- hitContext.save();
225
219
  bufferContext.save();
226
220
  sceneContext.translate(-x, -y);
227
- hitContext.translate(-x, -y);
228
221
  bufferContext.translate(-x, -y);
229
222
  bufferCanvas.x = x;
230
223
  bufferCanvas.y = y;
@@ -232,10 +225,8 @@ export class Node {
232
225
  this._clearSelfAndDescendantCache(ABSOLUTE_OPACITY);
233
226
  this._clearSelfAndDescendantCache(ABSOLUTE_SCALE);
234
227
  this.drawScene(cachedSceneCanvas, this, bufferCanvas);
235
- this.drawHit(cachedHitCanvas, this);
236
228
  this._isUnderCache = false;
237
229
  sceneContext.restore();
238
- hitContext.restore();
239
230
  if (drawBorder) {
240
231
  sceneContext.save();
241
232
  sceneContext.beginPath();
@@ -250,13 +241,39 @@ export class Node {
250
241
  this._cache.set(CANVAS, {
251
242
  scene: cachedSceneCanvas,
252
243
  filter: cachedFilterCanvas,
253
- hit: cachedHitCanvas,
244
+ hit: null,
245
+ hitConfig: {
246
+ pixelRatio: hitCanvasPixelRatio,
247
+ width: width,
248
+ height: height,
249
+ },
254
250
  x: x,
255
251
  y: y,
256
252
  });
257
253
  this._requestDraw();
258
254
  return this;
259
255
  }
256
+ _getCachedHitCanvas(top) {
257
+ if (top === this) {
258
+ return null;
259
+ }
260
+ const cache = this._getCanvasCache();
261
+ if (!cache) {
262
+ return null;
263
+ }
264
+ if (cache.hit) {
265
+ return cache.hit;
266
+ }
267
+ const hitCanvas = new HitCanvas(cache.hitConfig);
268
+ hitCanvas.isCache = true;
269
+ const hitContext = hitCanvas.getContext();
270
+ hitContext.save();
271
+ hitContext.translate(-cache.x, -cache.y);
272
+ this.drawHit(hitCanvas, this);
273
+ hitContext.restore();
274
+ cache.hit = hitCanvas;
275
+ return hitCanvas;
276
+ }
260
277
  isCached() {
261
278
  return this._cache.has(CANVAS);
262
279
  }
@@ -301,8 +318,8 @@ export class Node {
301
318
  context.drawImage(cacheCanvas._canvas, 0, 0, cacheCanvas.width / ratio, cacheCanvas.height / ratio);
302
319
  context.restore();
303
320
  }
304
- _drawCachedHitCanvas(context) {
305
- const canvasCache = this._getCanvasCache(), hitCanvas = canvasCache.hit;
321
+ _drawCachedHitCanvas(context, hitCanvas) {
322
+ const canvasCache = this._getCanvasCache();
306
323
  context.save();
307
324
  context.translate(canvasCache.x, canvasCache.y);
308
325
  context.drawImage(hitCanvas._canvas, 0, 0, hitCanvas.width / hitCanvas.pixelRatio, hitCanvas.height / hitCanvas.pixelRatio);
@@ -14,24 +14,34 @@ export function hasPointerCapture(pointerId, shape) {
14
14
  return Captures.get(pointerId) === shape;
15
15
  }
16
16
  export function setPointerCapture(pointerId, shape) {
17
+ var _a;
17
18
  releaseCapture(pointerId);
18
19
  const stage = shape.getStage();
19
20
  if (!stage)
20
21
  return;
21
22
  Captures.set(pointerId, shape);
22
23
  if (SUPPORT_POINTER_EVENTS) {
24
+ try {
25
+ (_a = stage.content) === null || _a === void 0 ? void 0 : _a.setPointerCapture(pointerId);
26
+ }
27
+ catch (e) {
28
+ }
23
29
  shape._fire('gotpointercapture', createEvent(new PointerEvent('gotpointercapture')));
24
30
  }
25
31
  }
26
32
  export function releaseCapture(pointerId, target) {
33
+ var _a;
27
34
  const shape = Captures.get(pointerId);
28
35
  if (!shape)
29
36
  return;
30
37
  const stage = shape.getStage();
31
- if (stage && stage.content) {
32
- }
33
38
  Captures.delete(pointerId);
34
39
  if (SUPPORT_POINTER_EVENTS) {
40
+ try {
41
+ (_a = stage === null || stage === void 0 ? void 0 : stage.content) === null || _a === void 0 ? void 0 : _a.releasePointerCapture(pointerId);
42
+ }
43
+ catch (e) {
44
+ }
35
45
  shape._fire('lostpointercapture', createEvent(new PointerEvent('lostpointercapture')));
36
46
  }
37
47
  }
package/lib/Shape.js CHANGED
@@ -223,7 +223,7 @@ export class Shape extends Node {
223
223
  if (!stage) {
224
224
  return false;
225
225
  }
226
- const bufferHitCanvas = stage.bufferHitCanvas;
226
+ const bufferHitCanvas = stage._syncBufferSize(stage.bufferHitCanvas);
227
227
  bufferHitCanvas.getContext().clear();
228
228
  this.drawHit(bufferHitCanvas, undefined, true);
229
229
  const p = bufferHitCanvas.context.getImageData(Math.round(point.x), Math.round(point.y), 1, 1).data;
@@ -343,7 +343,7 @@ export class Shape extends Node {
343
343
  context.save();
344
344
  if (this._useBufferCanvas() && !skipBuffer) {
345
345
  stage = this.getStage();
346
- const bc = bufferCanvas || stage.bufferCanvas;
346
+ const bc = bufferCanvas || stage._syncBufferSize(stage.bufferCanvas);
347
347
  const bufferContext = bc.getContext();
348
348
  if (bufferCanvas) {
349
349
  bufferContext.save();
@@ -393,7 +393,7 @@ export class Shape extends Node {
393
393
  if (!this.shouldDrawHit(top, skipDragCheck)) {
394
394
  return this;
395
395
  }
396
- const layer = this.getLayer(), canvas = can || layer.hitCanvas, context = canvas && canvas.getContext(), drawFunc = this.hitFunc() || this.sceneFunc(), cachedCanvas = this._getCanvasCache(), cachedHitCanvas = cachedCanvas && cachedCanvas.hit;
396
+ const layer = this.getLayer(), canvas = can || layer.hitCanvas, context = canvas && canvas.getContext(), drawFunc = this.hitFunc() || this.sceneFunc(), cachedHitCanvas = this._getCachedHitCanvas(top);
397
397
  if (!this.colorKey) {
398
398
  Util.warn('Looks like your canvas has a destroyed shape in it. Do not reuse shape after you destroyed it. If you want to reuse shape you should call remove() instead of destroy()');
399
399
  }
@@ -401,7 +401,7 @@ export class Shape extends Node {
401
401
  context.save();
402
402
  const m = this.getAbsoluteTransform(top).getMatrix();
403
403
  context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
404
- this._drawCachedHitCanvas(context);
404
+ this._drawCachedHitCanvas(context, cachedHitCanvas);
405
405
  context.restore();
406
406
  return this;
407
407
  }
@@ -421,7 +421,7 @@ export class Shape extends Node {
421
421
  return this;
422
422
  }
423
423
  drawHitFromCache(alphaThreshold = 0) {
424
- const cachedCanvas = this._getCanvasCache(), sceneCanvas = this._getCachedSceneCanvas(), hitCanvas = cachedCanvas.hit, hitContext = hitCanvas.getContext(), hitWidth = hitCanvas.getWidth(), hitHeight = hitCanvas.getHeight();
424
+ const sceneCanvas = this._getCachedSceneCanvas(), hitCanvas = this._getCachedHitCanvas(), hitContext = hitCanvas.getContext(), hitWidth = hitCanvas.getWidth(), hitHeight = hitCanvas.getHeight();
425
425
  hitContext.clear();
426
426
  hitContext.drawImage(sceneCanvas._canvas, 0, 0, hitWidth, hitHeight);
427
427
  try {
package/lib/Stage.d.ts CHANGED
@@ -34,6 +34,7 @@ export declare class Stage extends Container<Layer, StageConfig> {
34
34
  constructor(config: StageConfig);
35
35
  _validateAdd(child: any): void;
36
36
  _checkVisibility(): void;
37
+ _getOwnerWindow(): Window | null;
37
38
  setContainer(container: any): this;
38
39
  shouldDrawHit(): boolean;
39
40
  clear(): this;
@@ -51,6 +52,7 @@ export declare class Stage extends Container<Layer, StageConfig> {
51
52
  _toKonvaCanvas(config: any): SceneCanvas;
52
53
  getIntersection(pos: Vector2d): Shape<import("./Shape.ts").ShapeConfig> | null;
53
54
  _resizeDOM(): void;
55
+ _syncBufferSize<T extends SceneCanvas | HitCanvas>(canvas: T): T;
54
56
  add(layer: Layer, ...rest: any[]): this;
55
57
  getParent(): null;
56
58
  getLayer(): null;
package/lib/Stage.js CHANGED
@@ -93,6 +93,19 @@ function checkNoClip(attrs = {}) {
93
93
  }
94
94
  const NO_POINTERS_MESSAGE = `Pointer position is missing and not registered by the stage. Looks like it is outside of the stage container. You can set it manually from event: stage.setPointersPositions(event);`;
95
95
  export const stages = [];
96
+ const listeningWindows = new WeakSet();
97
+ const listenToWindow = (win) => {
98
+ if (!win || listeningWindows.has(win)) {
99
+ return;
100
+ }
101
+ listeningWindows.add(win);
102
+ DD._listenToWindow(win);
103
+ win.document.addEventListener('visibilitychange', () => {
104
+ stages.forEach((stage) => {
105
+ stage.batchDraw();
106
+ });
107
+ });
108
+ };
96
109
  export class Stage extends Container {
97
110
  constructor(config) {
98
111
  super(checkNoClip(config));
@@ -123,6 +136,11 @@ export class Stage extends Container {
123
136
  const style = this.visible() ? '' : 'none';
124
137
  this.content.style.display = style;
125
138
  }
139
+ _getOwnerWindow() {
140
+ var _a;
141
+ const element = this.content || this.container();
142
+ return (((_a = element === null || element === void 0 ? void 0 : element.ownerDocument) === null || _a === void 0 ? void 0 : _a.defaultView) || (Konva.isBrowser ? window : null));
143
+ }
126
144
  setContainer(container) {
127
145
  if (typeof container === STRING) {
128
146
  let id;
@@ -149,6 +167,7 @@ export class Stage extends Container {
149
167
  this.content.parentElement.removeChild(this.content);
150
168
  }
151
169
  container.appendChild(this.content);
170
+ listenToWindow(this._getOwnerWindow());
152
171
  }
153
172
  return this;
154
173
  }
@@ -251,13 +270,15 @@ export class Stage extends Container {
251
270
  this.content.style.width = width + PX;
252
271
  this.content.style.height = height + PX;
253
272
  }
254
- this.bufferCanvas.setSize(width, height);
255
- this.bufferHitCanvas.setSize(width, height);
256
273
  this.children.forEach((layer) => {
257
274
  layer.setSize({ width, height });
258
275
  layer.draw();
259
276
  });
260
277
  }
278
+ _syncBufferSize(canvas) {
279
+ canvas.setSizeIfChanged(this.width(), this.height());
280
+ return canvas;
281
+ }
261
282
  add(layer, ...rest) {
262
283
  if (arguments.length > 1) {
263
284
  for (let i = 0; i < arguments.length; i++) {
@@ -484,6 +505,8 @@ export class Stage extends Container {
484
505
  return;
485
506
  }
486
507
  this.setPointersPositions(evt);
508
+ const wasDragged = DD.justDragged || (eventType === 'pointer' && Konva.isDragging());
509
+ const listenClick = Konva['_' + eventType + 'ListenClick'] && !wasDragged;
487
510
  const clickStartShape = this[eventType + 'ClickStartShape'];
488
511
  const clickEndShape = this[eventType + 'ClickEndShape'];
489
512
  const processedShapesIds = {};
@@ -505,7 +528,7 @@ export class Stage extends Container {
505
528
  fireDblClick = true;
506
529
  clearTimeout(this[eventType + 'DblTimeout']);
507
530
  }
508
- else if (!DD.justDragged) {
531
+ else if (!wasDragged) {
509
532
  Konva['_' + eventType + 'InDblClickWindow'] = true;
510
533
  clearTimeout(this[eventType + 'DblTimeout']);
511
534
  }
@@ -516,9 +539,7 @@ export class Stage extends Container {
516
539
  skipPointerUpTrigger = true;
517
540
  this[eventType + 'ClickEndShape'] = shape;
518
541
  shape._fireAndBubble(events.pointerup, { ...event });
519
- if (Konva['_' + eventType + 'ListenClick'] &&
520
- clickStartShape &&
521
- clickStartShape === shape) {
542
+ if (listenClick && clickStartShape === shape) {
522
543
  shape._fireAndBubble(events.pointerclick, { ...event });
523
544
  if (fireDblClick && clickEndShape && clickEndShape === shape) {
524
545
  shape._fireAndBubble(events.pointerdblclick, { ...event });
@@ -536,7 +557,7 @@ export class Stage extends Container {
536
557
  });
537
558
  skipPointerUpTrigger = true;
538
559
  }
539
- if (Konva['_' + eventType + 'ListenClick']) {
560
+ if (listenClick) {
540
561
  this._fire(events.pointerclick, {
541
562
  evt: evt,
542
563
  target: this,
@@ -665,13 +686,13 @@ export class Stage extends Container {
665
686
  }
666
687
  _buildDOM() {
667
688
  this.bufferCanvas = new SceneCanvas({
668
- width: this.width(),
669
- height: this.height(),
689
+ width: 0,
690
+ height: 0,
670
691
  });
671
692
  this.bufferHitCanvas = new HitCanvas({
672
693
  pixelRatio: 1,
673
- width: this.width(),
674
- height: this.height(),
694
+ width: 0,
695
+ height: 0,
675
696
  });
676
697
  if (!Konva.isBrowser) {
677
698
  return;
@@ -681,12 +702,13 @@ export class Stage extends Container {
681
702
  throw 'Stage has no container. A container is required.';
682
703
  }
683
704
  container.innerHTML = '';
684
- this.content = document.createElement('div');
705
+ this.content = container.ownerDocument.createElement('div');
685
706
  this.content.style.position = 'relative';
686
707
  this.content.style.userSelect = 'none';
687
708
  this.content.className = 'konvajs-content';
688
709
  this.content.setAttribute('role', 'presentation');
689
710
  container.appendChild(this.content);
711
+ listenToWindow(this._getOwnerWindow());
690
712
  this._resizeDOM();
691
713
  }
692
714
  cache() {
@@ -706,10 +728,3 @@ export class Stage extends Container {
706
728
  Stage.prototype.nodeType = STAGE;
707
729
  _registerNode(Stage);
708
730
  Factory.addGetterSetter(Stage, 'container');
709
- if (Konva.isBrowser) {
710
- document.addEventListener('visibilitychange', () => {
711
- stages.forEach((stage) => {
712
- stage.batchDraw();
713
- });
714
- });
715
- }
package/lib/Util.d.ts CHANGED
@@ -43,7 +43,7 @@ export declare const Util: {
43
43
  isObject(val: any): val is object;
44
44
  isValidSelector(selector: any): boolean;
45
45
  _sign(number: number): 1 | -1;
46
- requestAnimFrame(callback: Function): void;
46
+ requestAnimFrame(callback: Function, win?: Window | null): void;
47
47
  createCanvasElement(): HTMLCanvasElement;
48
48
  createImageElement(): HTMLImageElement;
49
49
  _isInDocument(el: any): boolean;
package/lib/Util.js CHANGED
@@ -316,12 +316,20 @@ const OBJECT_ARRAY = '[object Array]', OBJECT_NUMBER = '[object Number]', OBJECT
316
316
  yellow: [255, 255, 0],
317
317
  yellowgreen: [154, 205, 50],
318
318
  };
319
- let animQueue = [];
320
319
  let _isCanvasFarblingActive = null;
321
- const req = (typeof requestAnimationFrame !== 'undefined' && requestAnimationFrame) ||
322
- function (f) {
320
+ const defaultWindow = typeof window !== 'undefined' ? window : {};
321
+ const animQueues = new WeakMap();
322
+ const requestFrame = (win, f) => {
323
+ if (typeof win.requestAnimationFrame === 'function') {
324
+ win.requestAnimationFrame(f);
325
+ }
326
+ else if (typeof requestAnimationFrame !== 'undefined') {
327
+ requestAnimationFrame(f);
328
+ }
329
+ else {
323
330
  setTimeout(f, 16);
324
- };
331
+ }
332
+ };
325
333
  const capitalizeCache = new Map();
326
334
  const splitColorComponents = (str) => {
327
335
  const components = str.trim();
@@ -394,17 +402,20 @@ export const Util = {
394
402
  return -1;
395
403
  }
396
404
  },
397
- requestAnimFrame(callback) {
398
- animQueue.push(callback);
399
- if (animQueue.length === 1) {
400
- req(function () {
401
- const queue = animQueue;
402
- animQueue = [];
405
+ requestAnimFrame(callback, win) {
406
+ const target = (win && !win.closed && win) || defaultWindow;
407
+ let queue = animQueues.get(target);
408
+ if (!queue) {
409
+ queue = [];
410
+ animQueues.set(target, queue);
411
+ requestFrame(target, function () {
412
+ animQueues.delete(target);
403
413
  queue.forEach(function (cb) {
404
414
  cb();
405
415
  });
406
416
  });
407
417
  }
418
+ queue.push(callback);
408
419
  },
409
420
  createCanvasElement() {
410
421
  ensureBrowser();
@@ -420,12 +431,7 @@ export const Util = {
420
431
  return document.createElement('img');
421
432
  },
422
433
  _isInDocument(el) {
423
- while ((el = el.parentNode)) {
424
- if (el == document) {
425
- return true;
426
- }
427
- }
428
- return false;
434
+ return !!el.isConnected;
429
435
  },
430
436
  _urlToImage(url, callback) {
431
437
  const imageObj = Util.createImageElement();
@@ -56,7 +56,7 @@ export declare const Konva: {
56
56
  isObject(val: any): val is object;
57
57
  isValidSelector(selector: any): boolean;
58
58
  _sign(number: number): 1 | -1;
59
- requestAnimFrame(callback: Function): void;
59
+ requestAnimFrame(callback: Function, win?: Window | null): void;
60
60
  createCanvasElement(): HTMLCanvasElement;
61
61
  createImageElement(): HTMLImageElement;
62
62
  _isInDocument(el: any): boolean;
@@ -161,8 +161,9 @@ export declare const Konva: {
161
161
  startEvent?: any;
162
162
  dragStatus: "ready" | "dragging" | "stopped";
163
163
  }>;
164
- _drag(evt: any): void;
165
- _endDragBefore(evt?: any): void;
164
+ _listenToWindow(win: Window): void;
165
+ _drag(evt: any, win?: Window): void;
166
+ _endDragBefore(evt?: any, win?: Window): void;
166
167
  _endDragAfter(evt: any): void;
167
168
  };
168
169
  Shape: typeof Shape;