konva 10.3.3 → 10.5.0
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/README.md +1 -1
- package/konva.js +1576 -1636
- package/konva.min.js +4 -4
- package/lib/Animation.d.ts +63 -3
- package/lib/Animation.js +86 -65
- package/lib/BezierFunctions.d.ts +10 -0
- package/lib/BezierFunctions.js +32 -0
- package/lib/Canvas.d.ts +53 -0
- package/lib/Canvas.js +93 -34
- package/lib/Container.d.ts +156 -9
- package/lib/Container.js +260 -25
- package/lib/Context.d.ts +247 -0
- package/lib/Context.js +312 -74
- package/lib/Core.js +1 -0
- package/lib/DragAndDrop.d.ts +2 -0
- package/lib/DragAndDrop.js +92 -21
- package/lib/Factory.d.ts +24 -1
- package/lib/Factory.js +14 -20
- package/lib/FastLayer.d.ts +14 -1
- package/lib/FastLayer.js +14 -1
- package/lib/Global.d.ts +114 -3
- package/lib/Global.js +134 -5
- package/lib/Group.d.ts +11 -0
- package/lib/Group.js +11 -0
- package/lib/Layer.d.ts +128 -3
- package/lib/Layer.js +260 -49
- package/lib/Node.d.ts +703 -32
- package/lib/Node.js +1545 -202
- package/lib/PointerEvents.d.ts +2 -1
- package/lib/PointerEvents.js +19 -1
- package/lib/Shape.d.ts +99 -14
- package/lib/Shape.js +1101 -67
- package/lib/Stage.d.ts +85 -2
- package/lib/Stage.js +205 -14
- package/lib/Tween.d.ts +159 -4
- package/lib/Tween.js +243 -24
- package/lib/Util.d.ts +148 -4
- package/lib/Util.js +354 -82
- package/lib/Validators.d.ts +0 -2
- package/lib/Validators.js +3 -25
- package/lib/_CoreInternals.d.ts +12 -6
- package/lib/_CoreInternals.js +1 -0
- package/lib/_FullInternals.d.ts +17 -6
- package/lib/_FullInternals.js +7 -0
- package/lib/canvas-backend.js +6 -0
- package/lib/filters/Blur.d.ts +18 -0
- package/lib/filters/Blur.js +97 -24
- package/lib/filters/Brighten.d.ts +21 -0
- package/lib/filters/Brighten.js +24 -0
- package/lib/filters/Brightness.d.ts +11 -0
- package/lib/filters/Brightness.js +16 -0
- package/lib/filters/Contrast.d.ts +10 -0
- package/lib/filters/Contrast.js +21 -0
- package/lib/filters/Emboss.d.ts +20 -0
- package/lib/filters/Emboss.js +68 -8
- package/lib/filters/Enhance.d.ts +14 -0
- package/lib/filters/Enhance.js +34 -0
- package/lib/filters/Grayscale.d.ts +9 -0
- package/lib/filters/Grayscale.js +12 -0
- package/lib/filters/HSL.d.ts +17 -0
- package/lib/filters/HSL.js +48 -5
- package/lib/filters/HSV.d.ts +18 -0
- package/lib/filters/HSV.js +49 -5
- package/lib/filters/Invert.d.ts +9 -0
- package/lib/filters/Invert.js +12 -0
- package/lib/filters/Kaleidoscope.d.ts +12 -0
- package/lib/filters/Kaleidoscope.js +83 -0
- package/lib/filters/Mask.d.ts +11 -0
- package/lib/filters/Mask.js +18 -0
- package/lib/filters/Noise.d.ts +19 -0
- package/lib/filters/Noise.js +19 -0
- package/lib/filters/Pixelate.d.ts +20 -0
- package/lib/filters/Pixelate.js +28 -1
- package/lib/filters/Posterize.d.ts +21 -0
- package/lib/filters/Posterize.js +22 -0
- package/lib/filters/RGB.d.ts +21 -0
- package/lib/filters/RGB.js +43 -28
- package/lib/filters/RGBA.d.ts +23 -0
- package/lib/filters/RGBA.js +28 -41
- package/lib/filters/Sepia.d.ts +9 -0
- package/lib/filters/Sepia.js +10 -0
- package/lib/filters/Solarize.d.ts +10 -0
- package/lib/filters/Solarize.js +11 -0
- package/lib/filters/Threshold.d.ts +22 -0
- package/lib/filters/Threshold.js +22 -0
- package/lib/shapes/Arc.d.ts +40 -0
- package/lib/shapes/Arc.js +89 -6
- package/lib/shapes/Arrow.d.ts +24 -1
- package/lib/shapes/Arrow.js +83 -0
- package/lib/shapes/Circle.d.ts +18 -0
- package/lib/shapes/Circle.js +36 -9
- package/lib/shapes/Ellipse.d.ts +34 -2
- package/lib/shapes/Ellipse.js +67 -9
- package/lib/shapes/Image.d.ts +49 -0
- package/lib/shapes/Image.js +151 -22
- package/lib/shapes/Label.d.ts +65 -1
- package/lib/shapes/Label.js +113 -20
- package/lib/shapes/Line.d.ts +50 -8
- package/lib/shapes/Line.js +116 -20
- package/lib/shapes/Path.d.ts +56 -6
- package/lib/shapes/Path.js +203 -128
- package/lib/shapes/Rect.d.ts +18 -0
- package/lib/shapes/Rect.js +36 -0
- package/lib/shapes/RegularPolygon.d.ts +22 -0
- package/lib/shapes/RegularPolygon.js +70 -0
- package/lib/shapes/Ring.d.ts +19 -0
- package/lib/shapes/Ring.js +51 -5
- package/lib/shapes/Sprite.d.ts +78 -0
- package/lib/shapes/Sprite.js +238 -25
- package/lib/shapes/Star.d.ts +23 -0
- package/lib/shapes/Star.js +62 -0
- package/lib/shapes/Text.d.ts +59 -1
- package/lib/shapes/Text.js +481 -97
- package/lib/shapes/TextPath.d.ts +49 -3
- package/lib/shapes/TextPath.js +278 -34
- package/lib/shapes/Transformer.d.ts +124 -12
- package/lib/shapes/Transformer.js +864 -140
- package/lib/shapes/Wedge.d.ts +22 -0
- package/lib/shapes/Wedge.js +67 -3
- package/lib/skia-backend.js +9 -0
- package/lib/types.d.ts +2 -23
- package/lib/types.js +1 -21
- package/package.json +12 -16
package/lib/Stage.d.ts
CHANGED
|
@@ -6,8 +6,24 @@ import type { Shape } from './Shape.ts';
|
|
|
6
6
|
import type { Layer } from './Layer.ts';
|
|
7
7
|
export interface StageConfig extends ContainerConfig {
|
|
8
8
|
container?: HTMLDivElement | string;
|
|
9
|
+
eventBatchFunc?: (callback: () => void) => void;
|
|
9
10
|
}
|
|
10
11
|
export declare const stages: Stage[];
|
|
12
|
+
/**
|
|
13
|
+
* Stage constructor. A stage is used to contain multiple layers
|
|
14
|
+
* @constructor
|
|
15
|
+
* @memberof Konva
|
|
16
|
+
* @augments Konva.Container
|
|
17
|
+
* @param {Object} config
|
|
18
|
+
* @param {String|Element} config.container Container selector or DOM element
|
|
19
|
+
* @@nodeParams
|
|
20
|
+
* @example
|
|
21
|
+
* var stage = new Konva.Stage({
|
|
22
|
+
* width: 500,
|
|
23
|
+
* height: 800,
|
|
24
|
+
* container: 'containerId' // or "#containerId" or ".containerClass"
|
|
25
|
+
* });
|
|
26
|
+
*/
|
|
11
27
|
export declare class Stage extends Container<Layer, StageConfig> {
|
|
12
28
|
content: HTMLDivElement;
|
|
13
29
|
pointerPos: Vector2d | null;
|
|
@@ -34,12 +50,36 @@ export declare class Stage extends Container<Layer, StageConfig> {
|
|
|
34
50
|
constructor(config: StageConfig);
|
|
35
51
|
_validateAdd(child: any): void;
|
|
36
52
|
_checkVisibility(): void;
|
|
53
|
+
/**
|
|
54
|
+
* The window the stage is rendered in. It is not always the window Konva was
|
|
55
|
+
* imported into: the container may belong to an iframe, or to a window
|
|
56
|
+
* opened with `window.open`.
|
|
57
|
+
*/
|
|
37
58
|
_getOwnerWindow(): Window | null;
|
|
59
|
+
/**
|
|
60
|
+
* set container dom element which contains the stage wrapper div element
|
|
61
|
+
* @method
|
|
62
|
+
* @name Konva.Stage#setContainer
|
|
63
|
+
* @param {DomElement} container can pass in a dom element or id string
|
|
64
|
+
*/
|
|
38
65
|
setContainer(container: any): this;
|
|
39
66
|
shouldDrawHit(): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* clear all layers
|
|
69
|
+
* @method
|
|
70
|
+
* @name Konva.Stage#clear
|
|
71
|
+
*/
|
|
40
72
|
clear(): this;
|
|
41
73
|
clone(obj?: any): this;
|
|
42
74
|
destroy(): this;
|
|
75
|
+
/**
|
|
76
|
+
* returns ABSOLUTE pointer position which can be a touch position or mouse position
|
|
77
|
+
* pointer position doesn't include any transforms (such as scale) of the stage
|
|
78
|
+
* it is just a plain position of pointer relative to top-left corner of the canvas
|
|
79
|
+
* @method
|
|
80
|
+
* @name Konva.Stage#getPointerPosition
|
|
81
|
+
* @returns {Vector2d|null}
|
|
82
|
+
*/
|
|
43
83
|
getPointerPosition(): Vector2d | null;
|
|
44
84
|
_getPointerById(id?: number): (Vector2d & {
|
|
45
85
|
id?: number;
|
|
@@ -50,6 +90,22 @@ export declare class Stage extends Container<Layer, StageConfig> {
|
|
|
50
90
|
getStage(): this;
|
|
51
91
|
getContent(): HTMLDivElement;
|
|
52
92
|
_toKonvaCanvas(config: any): SceneCanvas;
|
|
93
|
+
/**
|
|
94
|
+
* get visible intersection shape. This is the preferred
|
|
95
|
+
* method for determining if a point intersects a shape or not.
|
|
96
|
+
* It reads the hit canvas, so it finds what pointer events would: nodes with
|
|
97
|
+
* listening set to false or invisible nodes are not detected, a shape with opacity 0
|
|
98
|
+
* is, and `hitStrokeWidth` counts. The position is relative to the top left corner of the
|
|
99
|
+
* stage container, like `stage.getPointerPosition()`, without the stage transform
|
|
100
|
+
* @method
|
|
101
|
+
* @name Konva.Stage#getIntersection
|
|
102
|
+
* @param {Object} pos
|
|
103
|
+
* @param {Number} pos.x
|
|
104
|
+
* @param {Number} pos.y
|
|
105
|
+
* @returns {Konva.Node}
|
|
106
|
+
* @example
|
|
107
|
+
* var shape = stage.getIntersection({x: 50, y: 50});
|
|
108
|
+
*/
|
|
53
109
|
getIntersection(pos: Vector2d): Shape<import("./Shape.ts").ShapeConfig> | null;
|
|
54
110
|
_resizeDOM(): void;
|
|
55
111
|
_syncBufferSize<T extends SceneCanvas | HitCanvas>(canvas: T): T;
|
|
@@ -59,8 +115,14 @@ export declare class Stage extends Container<Layer, StageConfig> {
|
|
|
59
115
|
hasPointerCapture(pointerId: number): boolean;
|
|
60
116
|
setPointerCapture(pointerId: number): void;
|
|
61
117
|
releaseCapture(pointerId: number): void;
|
|
118
|
+
/**
|
|
119
|
+
* returns an array of layers
|
|
120
|
+
* @method
|
|
121
|
+
* @name Konva.Stage#getLayers
|
|
122
|
+
*/
|
|
62
123
|
getLayers(): Layer[];
|
|
63
124
|
_bindContentEvents(): void;
|
|
125
|
+
_batchEvents(callback: () => void): void;
|
|
64
126
|
_pointerenter(evt: PointerEvent): void;
|
|
65
127
|
_pointerover(evt: any): void;
|
|
66
128
|
_getTargetShape(evenType: any): Shape<import("./Shape.ts").ShapeConfig> | null;
|
|
@@ -70,8 +132,22 @@ export declare class Stage extends Container<Layer, StageConfig> {
|
|
|
70
132
|
_pointerup(evt: any): void;
|
|
71
133
|
_contextmenu(evt: any): void;
|
|
72
134
|
_wheel(evt: any): void;
|
|
73
|
-
_pointercancel(evt: PointerEvent): void;
|
|
135
|
+
_pointercancel(evt: PointerEvent | TouchEvent): void;
|
|
74
136
|
_lostpointercapture(evt: PointerEvent): void;
|
|
137
|
+
/**
|
|
138
|
+
* manually register pointers positions (mouse/touch) in the stage.
|
|
139
|
+
* So you can use stage.getPointerPosition(). Usually you don't need to use that method
|
|
140
|
+
* because all internal events are automatically registered. It may be useful if event
|
|
141
|
+
* is triggered outside of the stage, but you still want to use Konva methods to get pointers position.
|
|
142
|
+
* @method
|
|
143
|
+
* @name Konva.Stage#setPointersPositions
|
|
144
|
+
* @param {Object} evt Event object
|
|
145
|
+
* @example
|
|
146
|
+
*
|
|
147
|
+
* window.addEventListener('mousemove', (e) => {
|
|
148
|
+
* stage.setPointersPositions(e);
|
|
149
|
+
* });
|
|
150
|
+
*/
|
|
75
151
|
setPointersPositions(evt: any): void;
|
|
76
152
|
_setPointerPosition(evt: any): void;
|
|
77
153
|
_getContentPosition(): {
|
|
@@ -83,6 +159,13 @@ export declare class Stage extends Container<Layer, StageConfig> {
|
|
|
83
159
|
_buildDOM(): void;
|
|
84
160
|
cache(): this;
|
|
85
161
|
clearCache(): this;
|
|
162
|
+
/**
|
|
163
|
+
* batch draw
|
|
164
|
+
* @method
|
|
165
|
+
* @name Konva.Stage#batchDraw
|
|
166
|
+
* @return {Konva.Stage} this
|
|
167
|
+
*/
|
|
86
168
|
batchDraw(): this;
|
|
87
|
-
container: GetSet<HTMLDivElement, this>;
|
|
169
|
+
container: GetSet<HTMLDivElement, this, HTMLDivElement | string>;
|
|
170
|
+
eventBatchFunc: GetSet<((callback: () => void) => void) | undefined, this>;
|
|
88
171
|
}
|
package/lib/Stage.js
CHANGED
|
@@ -6,6 +6,7 @@ import { SceneCanvas, HitCanvas } from "./Canvas.js";
|
|
|
6
6
|
import { DD } from "./DragAndDrop.js";
|
|
7
7
|
import { _registerNode } from "./Global.js";
|
|
8
8
|
import * as PointerEvents from "./PointerEvents.js";
|
|
9
|
+
// CONSTANTS
|
|
9
10
|
const STAGE = 'Stage', STRING = 'string', PX = 'px', MOUSEOUT = 'mouseout', MOUSELEAVE = 'mouseleave', MOUSEOVER = 'mouseover', MOUSEENTER = 'mouseenter', MOUSEMOVE = 'mousemove', MOUSEDOWN = 'mousedown', MOUSEUP = 'mouseup', POINTERMOVE = 'pointermove', POINTERDOWN = 'pointerdown', POINTERUP = 'pointerup', POINTERCANCEL = 'pointercancel', LOSTPOINTERCAPTURE = 'lostpointercapture', POINTEROUT = 'pointerout', POINTERLEAVE = 'pointerleave', POINTEROVER = 'pointerover', POINTERENTER = 'pointerenter', CONTEXTMENU = 'contextmenu', TOUCHSTART = 'touchstart', TOUCHEND = 'touchend', TOUCHMOVE = 'touchmove', TOUCHCANCEL = 'touchcancel', WHEEL = 'wheel', MAX_LAYERS_NUMBER = 5, EVENTS = [
|
|
10
11
|
[MOUSEENTER, '_pointerenter'],
|
|
11
12
|
[MOUSEDOWN, '_pointerdown'],
|
|
@@ -93,6 +94,10 @@ function checkNoClip(attrs = {}) {
|
|
|
93
94
|
}
|
|
94
95
|
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
96
|
export const stages = [];
|
|
97
|
+
// A stage may be rendered in another window than the one Konva was imported
|
|
98
|
+
// into: an iframe, or a window opened with `window.open`. Such a window sends
|
|
99
|
+
// its own events and has its own visibility state, so Konva listens to every
|
|
100
|
+
// window it renders a stage in
|
|
96
101
|
const listeningWindows = new WeakSet();
|
|
97
102
|
const listenToWindow = (win) => {
|
|
98
103
|
if (!win || listeningWindows.has(win)) {
|
|
@@ -100,12 +105,29 @@ const listenToWindow = (win) => {
|
|
|
100
105
|
}
|
|
101
106
|
listeningWindows.add(win);
|
|
102
107
|
DD._listenToWindow(win);
|
|
108
|
+
// chrome is clearing canvas in inactive browser window, causing layer content to be erased
|
|
109
|
+
// so let's redraw layers as soon as window becomes active
|
|
103
110
|
win.document.addEventListener('visibilitychange', () => {
|
|
104
111
|
stages.forEach((stage) => {
|
|
105
112
|
stage.batchDraw();
|
|
106
113
|
});
|
|
107
114
|
});
|
|
108
115
|
};
|
|
116
|
+
/**
|
|
117
|
+
* Stage constructor. A stage is used to contain multiple layers
|
|
118
|
+
* @constructor
|
|
119
|
+
* @memberof Konva
|
|
120
|
+
* @augments Konva.Container
|
|
121
|
+
* @param {Object} config
|
|
122
|
+
* @param {String|Element} config.container Container selector or DOM element
|
|
123
|
+
* @@nodeParams
|
|
124
|
+
* @example
|
|
125
|
+
* var stage = new Konva.Stage({
|
|
126
|
+
* width: 500,
|
|
127
|
+
* height: 800,
|
|
128
|
+
* container: 'containerId' // or "#containerId" or ".containerClass"
|
|
129
|
+
* });
|
|
130
|
+
*/
|
|
109
131
|
export class Stage extends Container {
|
|
110
132
|
constructor(config) {
|
|
111
133
|
super(checkNoClip(config));
|
|
@@ -136,11 +158,24 @@ export class Stage extends Container {
|
|
|
136
158
|
const style = this.visible() ? '' : 'none';
|
|
137
159
|
this.content.style.display = style;
|
|
138
160
|
}
|
|
161
|
+
/**
|
|
162
|
+
* The window the stage is rendered in. It is not always the window Konva was
|
|
163
|
+
* imported into: the container may belong to an iframe, or to a window
|
|
164
|
+
* opened with `window.open`.
|
|
165
|
+
*/
|
|
139
166
|
_getOwnerWindow() {
|
|
140
167
|
var _a;
|
|
141
168
|
const element = this.content || this.container();
|
|
169
|
+
// a document with no browsing context, such as one of `DOMParser`, has no
|
|
170
|
+
// window of its own. The window Konva was imported into is used then
|
|
142
171
|
return (((_a = element === null || element === void 0 ? void 0 : element.ownerDocument) === null || _a === void 0 ? void 0 : _a.defaultView) || (Konva.isBrowser ? window : null));
|
|
143
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* set container dom element which contains the stage wrapper div element
|
|
175
|
+
* @method
|
|
176
|
+
* @name Konva.Stage#setContainer
|
|
177
|
+
* @param {DomElement} container can pass in a dom element or id string
|
|
178
|
+
*/
|
|
144
179
|
setContainer(container) {
|
|
145
180
|
if (typeof container === STRING) {
|
|
146
181
|
let id;
|
|
@@ -167,6 +202,7 @@ export class Stage extends Container {
|
|
|
167
202
|
this.content.parentElement.removeChild(this.content);
|
|
168
203
|
}
|
|
169
204
|
container.appendChild(this.content);
|
|
205
|
+
// the new container may be in another window, with its own events
|
|
170
206
|
listenToWindow(this._getOwnerWindow());
|
|
171
207
|
}
|
|
172
208
|
return this;
|
|
@@ -174,6 +210,11 @@ export class Stage extends Container {
|
|
|
174
210
|
shouldDrawHit() {
|
|
175
211
|
return true;
|
|
176
212
|
}
|
|
213
|
+
/**
|
|
214
|
+
* clear all layers
|
|
215
|
+
* @method
|
|
216
|
+
* @name Konva.Stage#clear
|
|
217
|
+
*/
|
|
177
218
|
clear() {
|
|
178
219
|
const layers = this.children, len = layers.length;
|
|
179
220
|
for (let n = 0; n < len; n++) {
|
|
@@ -191,6 +232,12 @@ export class Stage extends Container {
|
|
|
191
232
|
}
|
|
192
233
|
destroy() {
|
|
193
234
|
super.destroy();
|
|
235
|
+
// a pending double-click timer is what closes the global double-click
|
|
236
|
+
// window, so close the window when the timer goes
|
|
237
|
+
['mouse', 'touch', 'pointer'].forEach((type) => {
|
|
238
|
+
clearTimeout(this['_' + type + 'DblTimeout']);
|
|
239
|
+
Konva['_' + type + 'InDblClickWindow'] = false;
|
|
240
|
+
});
|
|
194
241
|
const content = this.content;
|
|
195
242
|
if (content && Util._isInDocument(content)) {
|
|
196
243
|
this.container().removeChild(content);
|
|
@@ -202,6 +249,14 @@ export class Stage extends Container {
|
|
|
202
249
|
Util.releaseCanvas(this.bufferCanvas._canvas, this.bufferHitCanvas._canvas);
|
|
203
250
|
return this;
|
|
204
251
|
}
|
|
252
|
+
/**
|
|
253
|
+
* returns ABSOLUTE pointer position which can be a touch position or mouse position
|
|
254
|
+
* pointer position doesn't include any transforms (such as scale) of the stage
|
|
255
|
+
* it is just a plain position of pointer relative to top-left corner of the canvas
|
|
256
|
+
* @method
|
|
257
|
+
* @name Konva.Stage#getPointerPosition
|
|
258
|
+
* @returns {Vector2d|null}
|
|
259
|
+
*/
|
|
205
260
|
getPointerPosition() {
|
|
206
261
|
const pos = this._pointerPositions[0] || this._changedPointerPositions[0];
|
|
207
262
|
if (!pos) {
|
|
@@ -241,15 +296,34 @@ export class Stage extends Container {
|
|
|
241
296
|
if (config.x || config.y) {
|
|
242
297
|
_context.translate(-1 * config.x, -1 * config.y);
|
|
243
298
|
}
|
|
299
|
+
// every layer renders on its own canvas, like on screen, so a
|
|
300
|
+
// globalCompositeOperation inside a layer does not reach the layers below
|
|
244
301
|
layers.forEach(function (layer) {
|
|
245
302
|
if (!layer.isVisible()) {
|
|
246
303
|
return;
|
|
247
304
|
}
|
|
248
305
|
const layerCanvas = layer._toKonvaCanvas(config);
|
|
249
306
|
_context.drawImage(layerCanvas._canvas, config.x, config.y, layerCanvas.getWidth() / layerCanvas.getPixelRatio(), layerCanvas.getHeight() / layerCanvas.getPixelRatio());
|
|
307
|
+
Util.releaseCanvas(layerCanvas._canvas);
|
|
250
308
|
});
|
|
251
309
|
return canvas;
|
|
252
310
|
}
|
|
311
|
+
/**
|
|
312
|
+
* get visible intersection shape. This is the preferred
|
|
313
|
+
* method for determining if a point intersects a shape or not.
|
|
314
|
+
* It reads the hit canvas, so it finds what pointer events would: nodes with
|
|
315
|
+
* listening set to false or invisible nodes are not detected, a shape with opacity 0
|
|
316
|
+
* is, and `hitStrokeWidth` counts. The position is relative to the top left corner of the
|
|
317
|
+
* stage container, like `stage.getPointerPosition()`, without the stage transform
|
|
318
|
+
* @method
|
|
319
|
+
* @name Konva.Stage#getIntersection
|
|
320
|
+
* @param {Object} pos
|
|
321
|
+
* @param {Number} pos.x
|
|
322
|
+
* @param {Number} pos.y
|
|
323
|
+
* @returns {Konva.Node}
|
|
324
|
+
* @example
|
|
325
|
+
* var shape = stage.getIntersection({x: 50, y: 50});
|
|
326
|
+
*/
|
|
253
327
|
getIntersection(pos) {
|
|
254
328
|
if (!pos) {
|
|
255
329
|
return null;
|
|
@@ -267,14 +341,19 @@ export class Stage extends Container {
|
|
|
267
341
|
const width = this.width();
|
|
268
342
|
const height = this.height();
|
|
269
343
|
if (this.content) {
|
|
344
|
+
// set content dimensions
|
|
270
345
|
this.content.style.width = width + PX;
|
|
271
346
|
this.content.style.height = height + PX;
|
|
272
347
|
}
|
|
348
|
+
// set layer dimensions
|
|
273
349
|
this.children.forEach((layer) => {
|
|
274
|
-
layer.
|
|
350
|
+
layer._setSize({ width, height });
|
|
275
351
|
layer.draw();
|
|
276
352
|
});
|
|
277
353
|
}
|
|
354
|
+
// "perfect drawing" uses bufferCanvas, shape.intersects() uses
|
|
355
|
+
// bufferHitCanvas; both are lazy - created at 0x0 and sized to the stage
|
|
356
|
+
// on first use
|
|
278
357
|
_syncBufferSize(canvas) {
|
|
279
358
|
canvas.setSizeIfChanged(this.width(), this.height());
|
|
280
359
|
return canvas;
|
|
@@ -293,11 +372,13 @@ export class Stage extends Container {
|
|
|
293
372
|
length +
|
|
294
373
|
' layers. Recommended maximum number of layers is 3-5. Adding more layers into the stage may drop the performance. Rethink your tree structure, you can use Konva.Group.');
|
|
295
374
|
}
|
|
296
|
-
layer.
|
|
375
|
+
layer._setSize({ width: this.width(), height: this.height() });
|
|
376
|
+
// draw layer and append canvas to container
|
|
297
377
|
layer.draw();
|
|
298
378
|
if (Konva.isBrowser) {
|
|
299
379
|
this.content.appendChild(layer.canvas._canvas);
|
|
300
380
|
}
|
|
381
|
+
// chainable
|
|
301
382
|
return this;
|
|
302
383
|
}
|
|
303
384
|
getParent() {
|
|
@@ -315,8 +396,13 @@ export class Stage extends Container {
|
|
|
315
396
|
releaseCapture(pointerId) {
|
|
316
397
|
PointerEvents.releaseCapture(pointerId, this);
|
|
317
398
|
}
|
|
399
|
+
/**
|
|
400
|
+
* returns an array of layers
|
|
401
|
+
* @method
|
|
402
|
+
* @name Konva.Stage#getLayers
|
|
403
|
+
*/
|
|
318
404
|
getLayers() {
|
|
319
|
-
return this.
|
|
405
|
+
return this.getChildren();
|
|
320
406
|
}
|
|
321
407
|
_bindContentEvents() {
|
|
322
408
|
if (!Konva.isBrowser) {
|
|
@@ -324,10 +410,13 @@ export class Stage extends Container {
|
|
|
324
410
|
}
|
|
325
411
|
EVENTS.forEach(([event, methodName]) => {
|
|
326
412
|
this.content.addEventListener(event, (evt) => {
|
|
327
|
-
this[methodName](evt);
|
|
413
|
+
this._batchEvents(() => this[methodName](evt));
|
|
328
414
|
}, { passive: false });
|
|
329
415
|
});
|
|
330
416
|
}
|
|
417
|
+
_batchEvents(callback) {
|
|
418
|
+
Util._batchEvents(this.eventBatchFunc(), callback);
|
|
419
|
+
}
|
|
331
420
|
_pointerenter(evt) {
|
|
332
421
|
this.setPointersPositions(evt);
|
|
333
422
|
const events = getEventsMap(evt.type);
|
|
@@ -402,7 +491,9 @@ export class Stage extends Container {
|
|
|
402
491
|
this._changedPointerPositions.forEach((pos) => {
|
|
403
492
|
const shape = this.getIntersection(pos);
|
|
404
493
|
DD.justDragged = false;
|
|
494
|
+
// probably we are staring a click
|
|
405
495
|
Konva['_' + eventType + 'ListenClick'] = true;
|
|
496
|
+
// no shape detected? do nothing
|
|
406
497
|
if (!shape || !shape.isListening()) {
|
|
407
498
|
this[eventType + 'ClickStartShape'] = undefined;
|
|
408
499
|
return;
|
|
@@ -410,17 +501,20 @@ export class Stage extends Container {
|
|
|
410
501
|
if (Konva.capturePointerEventsEnabled) {
|
|
411
502
|
shape.setPointerCapture(pos.id);
|
|
412
503
|
}
|
|
504
|
+
// save where we started the click
|
|
413
505
|
this[eventType + 'ClickStartShape'] = shape;
|
|
414
506
|
shape._fireAndBubble(events.pointerdown, {
|
|
415
507
|
evt: evt,
|
|
416
508
|
pointerId: pos.id,
|
|
417
509
|
});
|
|
418
510
|
triggeredOnShape = true;
|
|
511
|
+
// only call preventDefault if the shape is listening for events
|
|
419
512
|
const isTouch = evt.type.indexOf('touch') >= 0;
|
|
420
513
|
if (shape.preventDefault() && evt.cancelable && isTouch) {
|
|
421
514
|
evt.preventDefault();
|
|
422
515
|
}
|
|
423
516
|
});
|
|
517
|
+
// trigger down on stage if not already
|
|
424
518
|
if (!triggeredOnShape) {
|
|
425
519
|
this._fire(events.pointerdown, {
|
|
426
520
|
evt: evt,
|
|
@@ -436,6 +530,8 @@ export class Stage extends Container {
|
|
|
436
530
|
if (!events) {
|
|
437
531
|
return;
|
|
438
532
|
}
|
|
533
|
+
// prevent default only for touch-based interactions to avoid blocking
|
|
534
|
+
// native mouse wheel scrolling during drag on desktop
|
|
439
535
|
const isTouchPointer = evt.type.indexOf('touch') >= 0 ||
|
|
440
536
|
evt.pointerType === 'touch';
|
|
441
537
|
if (Konva.isDragging() &&
|
|
@@ -505,6 +601,11 @@ export class Stage extends Container {
|
|
|
505
601
|
return;
|
|
506
602
|
}
|
|
507
603
|
this.setPointersPositions(evt);
|
|
604
|
+
// the mouse/touch families get their "listen click" flags reset by
|
|
605
|
+
// DD._endDragBefore, but native "pointerup" fires before the
|
|
606
|
+
// "mouseup"/"touchend" that ends the drag, so for the pointer family the
|
|
607
|
+
// flags are still set — check the live drag state as well, otherwise
|
|
608
|
+
// "pointerclick" fires after a drag while "click" does not (issue #1756)
|
|
508
609
|
const wasDragged = DD.justDragged || (eventType === 'pointer' && Konva.isDragging());
|
|
509
610
|
const listenClick = Konva['_' + eventType + 'ListenClick'] && !wasDragged;
|
|
510
611
|
const clickStartShape = this[eventType + 'ClickStartShape'];
|
|
@@ -526,19 +627,21 @@ export class Stage extends Container {
|
|
|
526
627
|
let fireDblClick = false;
|
|
527
628
|
if (Konva['_' + eventType + 'InDblClickWindow']) {
|
|
528
629
|
fireDblClick = true;
|
|
529
|
-
clearTimeout(this[eventType + 'DblTimeout']);
|
|
630
|
+
clearTimeout(this['_' + eventType + 'DblTimeout']);
|
|
530
631
|
}
|
|
531
632
|
else if (!wasDragged) {
|
|
633
|
+
// don't set inDblClickWindow after dragging
|
|
532
634
|
Konva['_' + eventType + 'InDblClickWindow'] = true;
|
|
533
|
-
clearTimeout(this[eventType + 'DblTimeout']);
|
|
635
|
+
clearTimeout(this['_' + eventType + 'DblTimeout']);
|
|
534
636
|
}
|
|
535
|
-
this[eventType + 'DblTimeout'] = setTimeout(function () {
|
|
637
|
+
this['_' + eventType + 'DblTimeout'] = setTimeout(function () {
|
|
536
638
|
Konva['_' + eventType + 'InDblClickWindow'] = false;
|
|
537
639
|
}, Konva.dblClickWindow);
|
|
538
640
|
if (shape && shape.isListening()) {
|
|
539
641
|
skipPointerUpTrigger = true;
|
|
540
642
|
this[eventType + 'ClickEndShape'] = shape;
|
|
541
643
|
shape._fireAndBubble(events.pointerup, { ...event });
|
|
644
|
+
// detect if click or double click occurred
|
|
542
645
|
if (listenClick && clickStartShape === shape) {
|
|
543
646
|
shape._fireAndBubble(events.pointerclick, { ...event });
|
|
544
647
|
if (fireDblClick && clickEndShape && clickEndShape === shape) {
|
|
@@ -584,6 +687,10 @@ export class Stage extends Container {
|
|
|
584
687
|
});
|
|
585
688
|
}
|
|
586
689
|
Konva['_' + eventType + 'ListenClick'] = false;
|
|
690
|
+
// always call preventDefault for desktop events because some browsers
|
|
691
|
+
// try to drag and drop the canvas element
|
|
692
|
+
// do not call this function on mobile because it prevent "click" event on all parent containers
|
|
693
|
+
// but apps may listen to it.
|
|
587
694
|
if (evt.cancelable && eventType !== 'touch' && eventType !== 'pointer') {
|
|
588
695
|
evt.preventDefault();
|
|
589
696
|
}
|
|
@@ -618,21 +725,51 @@ export class Stage extends Container {
|
|
|
618
725
|
}
|
|
619
726
|
_pointercancel(evt) {
|
|
620
727
|
this.setPointersPositions(evt);
|
|
621
|
-
const
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
728
|
+
const events = getEventsMap(evt.type);
|
|
729
|
+
this._changedPointerPositions.forEach((pos) => {
|
|
730
|
+
const shape = PointerEvents.getCapturedShape(pos.id) || this.getIntersection(pos);
|
|
731
|
+
const event = { evt, pointerId: pos.id };
|
|
732
|
+
if (shape) {
|
|
733
|
+
if (events)
|
|
734
|
+
shape._fireAndBubble(events.pointercancel, { ...event });
|
|
735
|
+
// Keep the historical pointerup notification for cancellation listeners.
|
|
736
|
+
shape._fireAndBubble(POINTERUP, { ...event });
|
|
737
|
+
}
|
|
738
|
+
else if (events) {
|
|
739
|
+
this._fire(events.pointercancel, {
|
|
740
|
+
...event,
|
|
741
|
+
target: this,
|
|
742
|
+
currentTarget: this,
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
PointerEvents.releaseCapture(pos.id);
|
|
746
|
+
});
|
|
627
747
|
}
|
|
628
748
|
_lostpointercapture(evt) {
|
|
629
749
|
PointerEvents.releaseCapture(evt.pointerId);
|
|
630
750
|
}
|
|
751
|
+
/**
|
|
752
|
+
* manually register pointers positions (mouse/touch) in the stage.
|
|
753
|
+
* So you can use stage.getPointerPosition(). Usually you don't need to use that method
|
|
754
|
+
* because all internal events are automatically registered. It may be useful if event
|
|
755
|
+
* is triggered outside of the stage, but you still want to use Konva methods to get pointers position.
|
|
756
|
+
* @method
|
|
757
|
+
* @name Konva.Stage#setPointersPositions
|
|
758
|
+
* @param {Object} evt Event object
|
|
759
|
+
* @example
|
|
760
|
+
*
|
|
761
|
+
* window.addEventListener('mousemove', (e) => {
|
|
762
|
+
* stage.setPointersPositions(e);
|
|
763
|
+
* });
|
|
764
|
+
*/
|
|
631
765
|
setPointersPositions(evt) {
|
|
632
766
|
const contentPosition = this._getContentPosition();
|
|
633
767
|
let x = null, y = null;
|
|
634
768
|
evt = evt ? evt : window.event;
|
|
769
|
+
// touch events
|
|
635
770
|
if (evt.touches !== undefined) {
|
|
771
|
+
// touchlist has not support for map method
|
|
772
|
+
// so we have to iterate
|
|
636
773
|
this._pointerPositions = [];
|
|
637
774
|
this._changedPointerPositions = [];
|
|
638
775
|
Array.prototype.forEach.call(evt.touches, (touch) => {
|
|
@@ -651,6 +788,7 @@ export class Stage extends Container {
|
|
|
651
788
|
});
|
|
652
789
|
}
|
|
653
790
|
else {
|
|
791
|
+
// mouse events
|
|
654
792
|
x = (evt.clientX - contentPosition.left) / contentPosition.scaleX;
|
|
655
793
|
y = (evt.clientY - contentPosition.top) / contentPosition.scaleY;
|
|
656
794
|
this.pointerPos = {
|
|
@@ -680,11 +818,15 @@ export class Stage extends Container {
|
|
|
680
818
|
return {
|
|
681
819
|
top: rect.top,
|
|
682
820
|
left: rect.left,
|
|
821
|
+
// sometimes clientWidth can be equals to 0
|
|
822
|
+
// i saw it in react-konva test, looks like it is because of hidden testing element
|
|
683
823
|
scaleX: rect.width / this.content.clientWidth || 1,
|
|
684
824
|
scaleY: rect.height / this.content.clientHeight || 1,
|
|
685
825
|
};
|
|
686
826
|
}
|
|
687
827
|
_buildDOM() {
|
|
828
|
+
// lazy buffer canvases, see _syncBufferSize
|
|
829
|
+
// https://github.com/konvajs/konva/issues/2009
|
|
688
830
|
this.bufferCanvas = new SceneCanvas({
|
|
689
831
|
width: 0,
|
|
690
832
|
height: 0,
|
|
@@ -701,7 +843,11 @@ export class Stage extends Container {
|
|
|
701
843
|
if (!container) {
|
|
702
844
|
throw 'Stage has no container. A container is required.';
|
|
703
845
|
}
|
|
846
|
+
// clear content inside container
|
|
704
847
|
container.innerHTML = '';
|
|
848
|
+
// content
|
|
849
|
+
// the container may belong to another document - an iframe, or a popout
|
|
850
|
+
// window - so the content is created in the document of the container
|
|
705
851
|
this.content = container.ownerDocument.createElement('div');
|
|
706
852
|
this.content.style.position = 'relative';
|
|
707
853
|
this.content.style.userSelect = 'none';
|
|
@@ -711,6 +857,7 @@ export class Stage extends Container {
|
|
|
711
857
|
listenToWindow(this._getOwnerWindow());
|
|
712
858
|
this._resizeDOM();
|
|
713
859
|
}
|
|
860
|
+
// currently cache function is now working for stage, because stage has no its own canvas element
|
|
714
861
|
cache() {
|
|
715
862
|
Util.warn('Cache function is not allowed for stage. You may use cache only for layers, groups and shapes.');
|
|
716
863
|
return this;
|
|
@@ -718,8 +865,17 @@ export class Stage extends Container {
|
|
|
718
865
|
clearCache() {
|
|
719
866
|
return this;
|
|
720
867
|
}
|
|
868
|
+
/**
|
|
869
|
+
* batch draw
|
|
870
|
+
* @method
|
|
871
|
+
* @name Konva.Stage#batchDraw
|
|
872
|
+
* @return {Konva.Stage} this
|
|
873
|
+
*/
|
|
721
874
|
batchDraw() {
|
|
722
|
-
|
|
875
|
+
var _a;
|
|
876
|
+
// the config applied by the Node constructor already requests a draw,
|
|
877
|
+
// before the children array exists
|
|
878
|
+
(_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (layer) {
|
|
723
879
|
layer.batchDraw();
|
|
724
880
|
});
|
|
725
881
|
return this;
|
|
@@ -727,4 +883,39 @@ export class Stage extends Container {
|
|
|
727
883
|
}
|
|
728
884
|
Stage.prototype.nodeType = STAGE;
|
|
729
885
|
_registerNode(Stage);
|
|
886
|
+
/**
|
|
887
|
+
* get/set container DOM element. The container may belong to another window
|
|
888
|
+
* than the one Konva was imported into: an iframe, or a window opened with
|
|
889
|
+
* `window.open`. The stage then uses the events and the frames of that window.
|
|
890
|
+
* @method
|
|
891
|
+
* @name Konva.Stage#container
|
|
892
|
+
* @returns {DomElement} container
|
|
893
|
+
* @example
|
|
894
|
+
* // get container
|
|
895
|
+
* var container = stage.container();
|
|
896
|
+
* // set container
|
|
897
|
+
* var container = document.createElement('div');
|
|
898
|
+
* body.appendChild(container);
|
|
899
|
+
* stage.container(container);
|
|
900
|
+
*
|
|
901
|
+
* // a stage in a popout window
|
|
902
|
+
* var popout = window.open('', '', 'width=600,height=400');
|
|
903
|
+
* stage.container(popout.document.body);
|
|
904
|
+
*/
|
|
730
905
|
Factory.addGetterSetter(Stage, 'container');
|
|
906
|
+
/**
|
|
907
|
+
* Get/set the callback used by framework integrations to batch native input.
|
|
908
|
+
* The callback must invoke its argument exactly once, synchronously. Existing
|
|
909
|
+
* node events keep their order and cancellation behavior. Direct node.fire()
|
|
910
|
+
* and programmatic mutations do not create a batch.
|
|
911
|
+
* Missing calls warn; duplicate or late calls warn and are ignored. Exceptions
|
|
912
|
+
* propagate and skipped input is not replayed, so an invalid callback can
|
|
913
|
+
* prevent input processing and drag cleanup.
|
|
914
|
+
* Each native Konva listener creates its own scope; a DOM event may invoke
|
|
915
|
+
* several listeners. Batching does not reorder or merge those listeners.
|
|
916
|
+
* @method
|
|
917
|
+
* @name Konva.Stage#eventBatchFunc
|
|
918
|
+
* @example
|
|
919
|
+
* stage.eventBatchFunc((run) => frameworkBatch(run));
|
|
920
|
+
*/
|
|
921
|
+
Factory.addGetterSetter(Stage, 'eventBatchFunc');
|