konva 10.3.0 → 10.3.2
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 +33 -43
- package/konva.js +1158 -681
- package/konva.min.js +4 -4
- package/lib/BezierFunctions.d.ts +2 -0
- package/lib/BezierFunctions.js +46 -0
- package/lib/Canvas.d.ts +1 -0
- package/lib/Canvas.js +6 -0
- package/lib/Container.js +22 -8
- package/lib/Context.js +6 -0
- package/lib/DragAndDrop.d.ts +13 -10
- package/lib/DragAndDrop.js +18 -13
- package/lib/Factory.d.ts +1 -1
- package/lib/Global.js +1 -1
- package/lib/Layer.d.ts +1 -0
- package/lib/Layer.js +15 -4
- package/lib/Node.d.ts +6 -2
- package/lib/Node.js +51 -18
- package/lib/PointerEvents.js +12 -2
- package/lib/Shape.js +6 -5
- package/lib/Stage.d.ts +2 -0
- package/lib/Stage.js +34 -19
- package/lib/Tween.js +10 -4
- package/lib/Util.d.ts +1 -7
- package/lib/Util.js +135 -135
- package/lib/_CoreInternals.d.ts +4 -9
- package/lib/_FullInternals.d.ts +4 -9
- package/lib/shapes/Arc.js +11 -0
- package/lib/shapes/Line.d.ts +2 -0
- package/lib/shapes/Line.js +39 -59
- package/lib/shapes/Path.js +10 -6
- package/lib/shapes/Text.d.ts +2 -0
- package/lib/shapes/Text.js +21 -4
- package/lib/shapes/Transformer.d.ts +5 -1
- package/lib/shapes/Transformer.js +80 -37
- package/package.json +13 -1
package/lib/BezierFunctions.d.ts
CHANGED
|
@@ -4,3 +4,5 @@ export declare const binomialCoefficients: number[][];
|
|
|
4
4
|
export declare const getCubicArcLength: (xs: number[], ys: number[], t: number) => number;
|
|
5
5
|
export declare const getQuadraticArcLength: (xs: number[], ys: number[], t: number) => number;
|
|
6
6
|
export declare const t2length: (length: number, totalLength: number, func: (t: number) => number) => number;
|
|
7
|
+
export declare const getQuadraticExtremaPoints: (x0: number, y0: number, x1: number, y1: number, x2: number, y2: number) => number[];
|
|
8
|
+
export declare const getCubicExtremaPoints: (x0: number, y0: number, x1: number, y1: number, x2: number, y2: number, x3: number, y3: number) => number[];
|
package/lib/BezierFunctions.js
CHANGED
|
@@ -782,3 +782,49 @@ export const t2length = (length, totalLength, func) => {
|
|
|
782
782
|
}
|
|
783
783
|
return t;
|
|
784
784
|
};
|
|
785
|
+
const quadraticAt = (p0, p1, p2, t) => {
|
|
786
|
+
const mt = 1 - t;
|
|
787
|
+
return mt * mt * p0 + 2 * mt * t * p1 + t * t * p2;
|
|
788
|
+
};
|
|
789
|
+
export const getQuadraticExtremaPoints = (x0, y0, x1, y1, x2, y2) => {
|
|
790
|
+
const extrema = [];
|
|
791
|
+
for (const axis of [
|
|
792
|
+
[x0, x1, x2],
|
|
793
|
+
[y0, y1, y2],
|
|
794
|
+
]) {
|
|
795
|
+
const t = (axis[0] - axis[1]) / (axis[0] - 2 * axis[1] + axis[2]);
|
|
796
|
+
if (t > 0 && t < 1) {
|
|
797
|
+
extrema.push(quadraticAt(x0, x1, x2, t), quadraticAt(y0, y1, y2, t));
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
return extrema;
|
|
801
|
+
};
|
|
802
|
+
const cubicAt = (p0, p1, p2, p3, t) => {
|
|
803
|
+
const mt = 1 - t;
|
|
804
|
+
return (mt * mt * mt * p0 +
|
|
805
|
+
3 * mt * mt * t * p1 +
|
|
806
|
+
3 * mt * t * t * p2 +
|
|
807
|
+
t * t * t * p3);
|
|
808
|
+
};
|
|
809
|
+
export const getCubicExtremaPoints = (x0, y0, x1, y1, x2, y2, x3, y3) => {
|
|
810
|
+
const extrema = [];
|
|
811
|
+
for (const axis of [
|
|
812
|
+
[x0, x1, x2, x3],
|
|
813
|
+
[y0, y1, y2, y3],
|
|
814
|
+
]) {
|
|
815
|
+
const a = -3 * axis[0] + 9 * axis[1] - 9 * axis[2] + 3 * axis[3];
|
|
816
|
+
const b = 6 * axis[0] - 12 * axis[1] + 6 * axis[2];
|
|
817
|
+
const c = -3 * axis[0] + 3 * axis[1];
|
|
818
|
+
const discriminant = b * b - 4 * a * c;
|
|
819
|
+
if (discriminant < 0) {
|
|
820
|
+
continue;
|
|
821
|
+
}
|
|
822
|
+
const q = -(b + (b < 0 ? -1 : 1) * Math.sqrt(discriminant)) / 2;
|
|
823
|
+
for (const t of [q / a, c / q]) {
|
|
824
|
+
if (t > 0 && t < 1) {
|
|
825
|
+
extrema.push(cubicAt(x0, x1, x2, x3, t), cubicAt(y0, y1, y2, y3, t));
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
return extrema;
|
|
830
|
+
};
|
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
|
@@ -144,13 +144,27 @@ export class Container extends Node {
|
|
|
144
144
|
}
|
|
145
145
|
_clearSelfAndDescendantCache(attr) {
|
|
146
146
|
var _a;
|
|
147
|
-
|
|
148
|
-
if (
|
|
149
|
-
|
|
147
|
+
const isAbsTransform = attr === 'absoluteTransform';
|
|
148
|
+
if (isAbsTransform)
|
|
149
|
+
Node._absTransformCascadeDepth++;
|
|
150
|
+
try {
|
|
151
|
+
super._clearSelfAndDescendantCache(attr);
|
|
152
|
+
if (this.isCached())
|
|
153
|
+
return;
|
|
154
|
+
(_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (node) {
|
|
155
|
+
node._clearSelfAndDescendantCache(attr);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
finally {
|
|
159
|
+
if (isAbsTransform && --Node._absTransformCascadeDepth === 0) {
|
|
160
|
+
const callbacks = Node._pendingAfterCascade;
|
|
161
|
+
if (callbacks.length) {
|
|
162
|
+
Node._pendingAfterCascade = [];
|
|
163
|
+
for (let i = 0; i < callbacks.length; i++)
|
|
164
|
+
callbacks[i]();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
150
167
|
}
|
|
151
|
-
(_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (node) {
|
|
152
|
-
node._clearSelfAndDescendantCache(attr);
|
|
153
|
-
});
|
|
154
168
|
}
|
|
155
169
|
_setChildrenIndices() {
|
|
156
170
|
var _a;
|
|
@@ -181,12 +195,12 @@ export class Container extends Node {
|
|
|
181
195
|
if (!this.shouldDrawHit(top)) {
|
|
182
196
|
return this;
|
|
183
197
|
}
|
|
184
|
-
const layer = this.getLayer(), canvas = can || (layer && layer.hitCanvas), context = canvas && canvas.getContext(),
|
|
198
|
+
const layer = this.getLayer(), canvas = can || (layer && layer.hitCanvas), context = canvas && canvas.getContext(), cachedHitCanvas = this._getCachedHitCanvas(top);
|
|
185
199
|
if (cachedHitCanvas) {
|
|
186
200
|
context.save();
|
|
187
201
|
const m = this.getAbsoluteTransform(top).getMatrix();
|
|
188
202
|
context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
|
|
189
|
-
this._drawCachedHitCanvas(context);
|
|
203
|
+
this._drawCachedHitCanvas(context, cachedHitCanvas);
|
|
190
204
|
context.restore();
|
|
191
205
|
}
|
|
192
206
|
else {
|
package/lib/Context.js
CHANGED
|
@@ -419,6 +419,12 @@ export class SceneContext extends Context {
|
|
|
419
419
|
shape._fillFunc(this);
|
|
420
420
|
}
|
|
421
421
|
_fillPattern(shape) {
|
|
422
|
+
const context = this._context;
|
|
423
|
+
if ('patternQuality' in context) {
|
|
424
|
+
context.patternQuality = context.imageSmoothingEnabled
|
|
425
|
+
? 'good'
|
|
426
|
+
: 'nearest';
|
|
427
|
+
}
|
|
422
428
|
this.setAttr('fillStyle', shape._getFillPattern());
|
|
423
429
|
shape._fillFunc(this);
|
|
424
430
|
}
|
package/lib/DragAndDrop.d.ts
CHANGED
|
@@ -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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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 {};
|
package/lib/DragAndDrop.js
CHANGED
|
@@ -19,11 +19,26 @@ export const DD = {
|
|
|
19
19
|
return node;
|
|
20
20
|
},
|
|
21
21
|
_dragElements: new Map(),
|
|
22
|
-
|
|
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/Factory.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ type Attr<T extends Constructor> = EnforceString<keyof InstanceType<T>>;
|
|
|
6
6
|
type AfterFunc<T extends Constructor> = (this: InstanceType<T>) => void;
|
|
7
7
|
type ExtractGetSet<T> = T extends GetSet<infer U, any> ? U : never;
|
|
8
8
|
type Value<T extends Constructor, U extends Attr<T>> = ExtractGetSet<InstanceType<T>[U]>;
|
|
9
|
-
type ValidatorFunc<T> = (val:
|
|
9
|
+
type ValidatorFunc<T> = (val: T, attr: string) => T;
|
|
10
10
|
type ExtractComponents<T extends Constructor, U extends Attr<T>> = Value<T, U> extends Record<string, any> ? EnforceString<keyof Value<T, U>>[] : never;
|
|
11
11
|
export declare const Factory: {
|
|
12
12
|
addGetterSetter<T extends Constructor, U extends Attr<T>>(constructor: T, attr: U, def?: Value<T, U>, validator?: ValidatorFunc<Value<T, U>>, after?: AfterFunc<T>): void;
|
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.
|
|
16
|
+
version: '10.3.2',
|
|
17
17
|
isBrowser: detectBrowser(),
|
|
18
18
|
isUnminified: /param/.test(function (param) { }.toString()),
|
|
19
19
|
dblClickWindow: 400,
|
package/lib/Layer.d.ts
CHANGED
package/lib/Layer.js
CHANGED
|
@@ -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.
|
|
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 (
|
|
262
|
-
layer.
|
|
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';
|
|
@@ -95,12 +95,16 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
|
|
|
95
95
|
_calculate(name: string, deps: Array<string>, getter: Function): any;
|
|
96
96
|
_getCanvasCache(): any;
|
|
97
97
|
_clearSelfAndDescendantCache(attr?: string): void;
|
|
98
|
+
static _absTransformCascadeDepth: number;
|
|
99
|
+
static _pendingAfterCascade: Array<() => void>;
|
|
100
|
+
static _runAfterAbsTransformCascade(cb: () => void): void;
|
|
98
101
|
clearCache(): this;
|
|
99
102
|
cache(config?: CanvasConfig & {
|
|
100
103
|
drawBorder?: boolean;
|
|
101
104
|
offset?: number;
|
|
102
105
|
hitCanvasPixelRatio?: number;
|
|
103
106
|
}): this | undefined;
|
|
107
|
+
_getCachedHitCanvas(top?: Node): HitCanvas | null;
|
|
104
108
|
isCached(): boolean;
|
|
105
109
|
abstract drawScene(canvas?: Canvas, top?: Node, bufferCanvas?: Canvas): void;
|
|
106
110
|
abstract drawHit(canvas?: Canvas, top?: Node): void;
|
|
@@ -122,7 +126,7 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
|
|
|
122
126
|
height: number;
|
|
123
127
|
};
|
|
124
128
|
_drawCachedSceneCanvas(context: Context): void;
|
|
125
|
-
_drawCachedHitCanvas(context: Context): void;
|
|
129
|
+
_drawCachedHitCanvas(context: Context, hitCanvas: HitCanvas): void;
|
|
126
130
|
_getCachedSceneCanvas(): Canvas;
|
|
127
131
|
on<K extends keyof NodeEventMap>(evtStr: K, handler: KonvaEventListener<this, NodeEventMap[K]>): this;
|
|
128
132
|
on<K extends keyof NodeEventMap>(evtStr: K, selector: string, handler: KonvaEventListener<Node, NodeEventMap[K]>): this;
|
package/lib/Node.js
CHANGED
|
@@ -152,10 +152,18 @@ export class Node {
|
|
|
152
152
|
this.fire('absoluteTransformChange');
|
|
153
153
|
}
|
|
154
154
|
}
|
|
155
|
+
static _runAfterAbsTransformCascade(cb) {
|
|
156
|
+
if (Node._absTransformCascadeDepth > 0) {
|
|
157
|
+
Node._pendingAfterCascade.push(cb);
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
cb();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
155
163
|
clearCache() {
|
|
156
164
|
if (this._cache.has(CANVAS)) {
|
|
157
165
|
const { scene, filter, hit } = this._cache.get(CANVAS);
|
|
158
|
-
Util.releaseCanvas(scene._canvas, filter._canvas, hit._canvas);
|
|
166
|
+
Util.releaseCanvas(scene._canvas, filter._canvas, ...(hit ? [hit._canvas] : []));
|
|
159
167
|
this._cache.delete(CANVAS);
|
|
160
168
|
}
|
|
161
169
|
this._clearSelfAndDescendantCache();
|
|
@@ -194,17 +202,12 @@ export class Node {
|
|
|
194
202
|
width: 0,
|
|
195
203
|
height: 0,
|
|
196
204
|
willReadFrequently: true,
|
|
197
|
-
}),
|
|
198
|
-
pixelRatio: hitCanvasPixelRatio,
|
|
199
|
-
width: width,
|
|
200
|
-
height: height,
|
|
201
|
-
}), sceneContext = cachedSceneCanvas.getContext(), hitContext = cachedHitCanvas.getContext();
|
|
205
|
+
}), sceneContext = cachedSceneCanvas.getContext();
|
|
202
206
|
const bufferCanvas = new SceneCanvas({
|
|
203
207
|
width: cachedSceneCanvas.width / cachedSceneCanvas.pixelRatio + Math.abs(x),
|
|
204
208
|
height: cachedSceneCanvas.height / cachedSceneCanvas.pixelRatio + Math.abs(y),
|
|
205
209
|
pixelRatio: cachedSceneCanvas.pixelRatio,
|
|
206
210
|
}), bufferContext = bufferCanvas.getContext();
|
|
207
|
-
cachedHitCanvas.isCache = true;
|
|
208
211
|
cachedSceneCanvas.isCache = true;
|
|
209
212
|
this._cache.delete(CANVAS);
|
|
210
213
|
this._filterUpToDate = false;
|
|
@@ -213,10 +216,8 @@ export class Node {
|
|
|
213
216
|
cachedFilterCanvas.getContext()._context.imageSmoothingEnabled = false;
|
|
214
217
|
}
|
|
215
218
|
sceneContext.save();
|
|
216
|
-
hitContext.save();
|
|
217
219
|
bufferContext.save();
|
|
218
220
|
sceneContext.translate(-x, -y);
|
|
219
|
-
hitContext.translate(-x, -y);
|
|
220
221
|
bufferContext.translate(-x, -y);
|
|
221
222
|
bufferCanvas.x = x;
|
|
222
223
|
bufferCanvas.y = y;
|
|
@@ -224,10 +225,8 @@ export class Node {
|
|
|
224
225
|
this._clearSelfAndDescendantCache(ABSOLUTE_OPACITY);
|
|
225
226
|
this._clearSelfAndDescendantCache(ABSOLUTE_SCALE);
|
|
226
227
|
this.drawScene(cachedSceneCanvas, this, bufferCanvas);
|
|
227
|
-
this.drawHit(cachedHitCanvas, this);
|
|
228
228
|
this._isUnderCache = false;
|
|
229
229
|
sceneContext.restore();
|
|
230
|
-
hitContext.restore();
|
|
231
230
|
if (drawBorder) {
|
|
232
231
|
sceneContext.save();
|
|
233
232
|
sceneContext.beginPath();
|
|
@@ -242,13 +241,39 @@ export class Node {
|
|
|
242
241
|
this._cache.set(CANVAS, {
|
|
243
242
|
scene: cachedSceneCanvas,
|
|
244
243
|
filter: cachedFilterCanvas,
|
|
245
|
-
hit:
|
|
244
|
+
hit: null,
|
|
245
|
+
hitConfig: {
|
|
246
|
+
pixelRatio: hitCanvasPixelRatio,
|
|
247
|
+
width: width,
|
|
248
|
+
height: height,
|
|
249
|
+
},
|
|
246
250
|
x: x,
|
|
247
251
|
y: y,
|
|
248
252
|
});
|
|
249
253
|
this._requestDraw();
|
|
250
254
|
return this;
|
|
251
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
|
+
}
|
|
252
277
|
isCached() {
|
|
253
278
|
return this._cache.has(CANVAS);
|
|
254
279
|
}
|
|
@@ -293,8 +318,8 @@ export class Node {
|
|
|
293
318
|
context.drawImage(cacheCanvas._canvas, 0, 0, cacheCanvas.width / ratio, cacheCanvas.height / ratio);
|
|
294
319
|
context.restore();
|
|
295
320
|
}
|
|
296
|
-
_drawCachedHitCanvas(context) {
|
|
297
|
-
const canvasCache = this._getCanvasCache()
|
|
321
|
+
_drawCachedHitCanvas(context, hitCanvas) {
|
|
322
|
+
const canvasCache = this._getCanvasCache();
|
|
298
323
|
context.save();
|
|
299
324
|
context.translate(canvasCache.x, canvasCache.y);
|
|
300
325
|
context.drawImage(hitCanvas._canvas, 0, 0, hitCanvas.width / hitCanvas.pixelRatio, hitCanvas.height / hitCanvas.pixelRatio);
|
|
@@ -1284,10 +1309,17 @@ export class Node {
|
|
|
1284
1309
|
let events = allListeners === null || allListeners === void 0 ? void 0 : allListeners[eventType];
|
|
1285
1310
|
if (events === undefined) {
|
|
1286
1311
|
events = [];
|
|
1312
|
+
const seen = new Set();
|
|
1287
1313
|
let obj = Object.getPrototypeOf(this);
|
|
1288
1314
|
while (obj) {
|
|
1289
1315
|
const hierarchyEvents = (_b = (_a = obj.eventListeners) === null || _a === void 0 ? void 0 : _a[eventType]) !== null && _b !== void 0 ? _b : [];
|
|
1290
|
-
|
|
1316
|
+
for (let i = 0; i < hierarchyEvents.length; i++) {
|
|
1317
|
+
const entry = hierarchyEvents[i];
|
|
1318
|
+
if (!seen.has(entry)) {
|
|
1319
|
+
seen.add(entry);
|
|
1320
|
+
events.push(entry);
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1291
1323
|
obj = Object.getPrototypeOf(obj);
|
|
1292
1324
|
}
|
|
1293
1325
|
allListeners[eventType] = events;
|
|
@@ -1301,9 +1333,8 @@ export class Node {
|
|
|
1301
1333
|
evt.type = eventType;
|
|
1302
1334
|
const topListeners = this._getProtoListeners(eventType);
|
|
1303
1335
|
if (topListeners) {
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
list[i].handler.call(this, evt);
|
|
1336
|
+
for (let i = 0; i < topListeners.length; i++) {
|
|
1337
|
+
topListeners[i].handler.call(this, evt);
|
|
1307
1338
|
}
|
|
1308
1339
|
}
|
|
1309
1340
|
const selfListeners = this.eventListeners[eventType];
|
|
@@ -1491,6 +1522,8 @@ export class Node {
|
|
|
1491
1522
|
return no;
|
|
1492
1523
|
}
|
|
1493
1524
|
}
|
|
1525
|
+
Node._absTransformCascadeDepth = 0;
|
|
1526
|
+
Node._pendingAfterCascade = [];
|
|
1494
1527
|
Node.protoListenerMap = new Map();
|
|
1495
1528
|
Node.prototype.nodeType = 'Node';
|
|
1496
1529
|
Node.prototype._attrsAffectingSize = [];
|
package/lib/PointerEvents.js
CHANGED
|
@@ -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();
|
|
@@ -355,6 +355,7 @@ export class Shape extends Node {
|
|
|
355
355
|
bufferContext.clear();
|
|
356
356
|
}
|
|
357
357
|
bufferContext.save();
|
|
358
|
+
bufferContext.imageSmoothingEnabled = context.imageSmoothingEnabled;
|
|
358
359
|
bufferContext._applyLineJoin(this);
|
|
359
360
|
bufferContext._applyMiterLimit(this);
|
|
360
361
|
const o = this.getAbsoluteTransform(top).getMatrix();
|
|
@@ -392,7 +393,7 @@ export class Shape extends Node {
|
|
|
392
393
|
if (!this.shouldDrawHit(top, skipDragCheck)) {
|
|
393
394
|
return this;
|
|
394
395
|
}
|
|
395
|
-
const layer = this.getLayer(), canvas = can || layer.hitCanvas, context = canvas && canvas.getContext(), drawFunc = this.hitFunc() || this.sceneFunc(),
|
|
396
|
+
const layer = this.getLayer(), canvas = can || layer.hitCanvas, context = canvas && canvas.getContext(), drawFunc = this.hitFunc() || this.sceneFunc(), cachedHitCanvas = this._getCachedHitCanvas(top);
|
|
396
397
|
if (!this.colorKey) {
|
|
397
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()');
|
|
398
399
|
}
|
|
@@ -400,7 +401,7 @@ export class Shape extends Node {
|
|
|
400
401
|
context.save();
|
|
401
402
|
const m = this.getAbsoluteTransform(top).getMatrix();
|
|
402
403
|
context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
|
|
403
|
-
this._drawCachedHitCanvas(context);
|
|
404
|
+
this._drawCachedHitCanvas(context, cachedHitCanvas);
|
|
404
405
|
context.restore();
|
|
405
406
|
return this;
|
|
406
407
|
}
|
|
@@ -420,7 +421,7 @@ export class Shape extends Node {
|
|
|
420
421
|
return this;
|
|
421
422
|
}
|
|
422
423
|
drawHitFromCache(alphaThreshold = 0) {
|
|
423
|
-
const
|
|
424
|
+
const sceneCanvas = this._getCachedSceneCanvas(), hitCanvas = this._getCachedHitCanvas(), hitContext = hitCanvas.getContext(), hitWidth = hitCanvas.getWidth(), hitHeight = hitCanvas.getHeight();
|
|
424
425
|
hitContext.clear();
|
|
425
426
|
hitContext.drawImage(sceneCanvas._canvas, 0, 0, hitWidth, hitHeight);
|
|
426
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;
|