konva 10.0.9 → 10.0.11
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/konva.js +90 -44
- package/konva.min.js +4 -4
- package/lib/Canvas.js +0 -30
- package/lib/Global.js +1 -1
- package/lib/Layer.js +2 -2
- package/lib/Shape.js +8 -1
- package/lib/Util.d.ts +4 -0
- package/lib/Util.js +43 -0
- package/lib/_CoreInternals.d.ts +4 -0
- package/lib/_FullInternals.d.ts +4 -0
- package/package.json +1 -1
package/lib/Canvas.js
CHANGED
|
@@ -97,41 +97,11 @@ export class SceneCanvas extends Canvas {
|
|
|
97
97
|
this.setSize(config.width, config.height);
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
|
-
function isCanvasFarblingActive() {
|
|
101
|
-
const c = Util.createCanvasElement();
|
|
102
|
-
c.width = 1;
|
|
103
|
-
c.height = 1;
|
|
104
|
-
const ctx = c.getContext('2d', {
|
|
105
|
-
willReadFrequently: true,
|
|
106
|
-
});
|
|
107
|
-
ctx.clearRect(0, 0, 1, 1);
|
|
108
|
-
ctx.fillStyle = 'rgba(255,0,255,1)';
|
|
109
|
-
ctx.fillRect(0, 0, 1, 1);
|
|
110
|
-
const d = ctx.getImageData(0, 0, 1, 1).data;
|
|
111
|
-
const exact = d[0] === 255 && d[1] === 0 && d[2] === 255 && d[3] === 255;
|
|
112
|
-
return !exact;
|
|
113
|
-
}
|
|
114
|
-
function isBraveBrowser() {
|
|
115
|
-
var _a, _b;
|
|
116
|
-
if (typeof navigator === 'undefined') {
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
return (_b = (_a = navigator.brave) === null || _a === void 0 ? void 0 : _a.isBrave()) !== null && _b !== void 0 ? _b : false;
|
|
120
|
-
}
|
|
121
|
-
let warned = false;
|
|
122
|
-
function checkHitCanvasSupport() {
|
|
123
|
-
if (isBraveBrowser() && isCanvasFarblingActive() && !warned) {
|
|
124
|
-
warned = true;
|
|
125
|
-
Util.error('Looks like you have "Brave shield" enabled in your browser. It breaks KonvaJS internals. Please disable it. You may need to ask your users to do the same.');
|
|
126
|
-
}
|
|
127
|
-
return isBraveBrowser() && isCanvasFarblingActive();
|
|
128
|
-
}
|
|
129
100
|
export class HitCanvas extends Canvas {
|
|
130
101
|
constructor(config = { width: 0, height: 0 }) {
|
|
131
102
|
super(config);
|
|
132
103
|
this.hitCanvas = true;
|
|
133
104
|
this.context = new HitContext(this);
|
|
134
105
|
this.setSize(config.width, config.height);
|
|
135
|
-
checkHitCanvasSupport();
|
|
136
106
|
}
|
|
137
107
|
}
|
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.0.
|
|
16
|
+
version: '10.0.11',
|
|
17
17
|
isBrowser: detectBrowser(),
|
|
18
18
|
isUnminified: /param/.test(function (param) { }.toString()),
|
|
19
19
|
dblClickWindow: 400,
|
package/lib/Layer.js
CHANGED
|
@@ -224,8 +224,8 @@ export class Layer extends Container {
|
|
|
224
224
|
const p = this.hitCanvas.context.getImageData(Math.round(pos.x * ratio), Math.round(pos.y * ratio), 1, 1).data;
|
|
225
225
|
const p3 = p[3];
|
|
226
226
|
if (p3 === 255) {
|
|
227
|
-
const colorKey = Util.
|
|
228
|
-
const shape = shapes[
|
|
227
|
+
const colorKey = Util.getHitColorKey(p[0], p[1], p[2]);
|
|
228
|
+
const shape = shapes[colorKey];
|
|
229
229
|
if (shape) {
|
|
230
230
|
return {
|
|
231
231
|
shape: shape,
|
package/lib/Shape.js
CHANGED
|
@@ -62,11 +62,18 @@ export class Shape extends Node {
|
|
|
62
62
|
constructor(config) {
|
|
63
63
|
super(config);
|
|
64
64
|
let key;
|
|
65
|
+
let attempts = 0;
|
|
65
66
|
while (true) {
|
|
66
|
-
key = Util.
|
|
67
|
+
key = Util.getHitColor();
|
|
67
68
|
if (key && !(key in shapes)) {
|
|
68
69
|
break;
|
|
69
70
|
}
|
|
71
|
+
attempts++;
|
|
72
|
+
if (attempts >= 10000) {
|
|
73
|
+
Util.warn('Failed to find a unique color key for a shape. Konva may work incorrectly. Most likely your browser is using canvas farbling. Consider disabling it.');
|
|
74
|
+
key = Util.getRandomColor();
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
70
77
|
}
|
|
71
78
|
this.colorKey = key;
|
|
72
79
|
shapes[key] = this;
|
package/lib/Util.d.ts
CHANGED
|
@@ -51,6 +51,10 @@ export declare const Util: {
|
|
|
51
51
|
_rgbToHex(r: number, g: number, b: number): string;
|
|
52
52
|
_hexToRgb(hex: string): RGB;
|
|
53
53
|
getRandomColor(): string;
|
|
54
|
+
isCanvasFarblingActive(): boolean;
|
|
55
|
+
getHitColor(): string;
|
|
56
|
+
getHitColorKey(r: number, g: number, b: number): string;
|
|
57
|
+
getSnappedHexColor(hex: string): string;
|
|
54
58
|
getRGB(color: string): RGB;
|
|
55
59
|
colorToRGBA(str: string): {
|
|
56
60
|
r: number;
|
package/lib/Util.js
CHANGED
|
@@ -317,6 +317,7 @@ const OBJECT_ARRAY = '[object Array]', OBJECT_NUMBER = '[object Number]', OBJECT
|
|
|
317
317
|
yellowgreen: [154, 205, 5],
|
|
318
318
|
}, RGB_REGEX = /rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)/;
|
|
319
319
|
let animQueue = [];
|
|
320
|
+
let _isCanvasFarblingActive = null;
|
|
320
321
|
const req = (typeof requestAnimationFrame !== 'undefined' && requestAnimationFrame) ||
|
|
321
322
|
function (f) {
|
|
322
323
|
setTimeout(f, 60);
|
|
@@ -427,6 +428,48 @@ export const Util = {
|
|
|
427
428
|
}
|
|
428
429
|
return HASH + randColor;
|
|
429
430
|
},
|
|
431
|
+
isCanvasFarblingActive() {
|
|
432
|
+
if (_isCanvasFarblingActive !== null) {
|
|
433
|
+
return _isCanvasFarblingActive;
|
|
434
|
+
}
|
|
435
|
+
if (typeof document === 'undefined') {
|
|
436
|
+
_isCanvasFarblingActive = false;
|
|
437
|
+
return false;
|
|
438
|
+
}
|
|
439
|
+
const c = this.createCanvasElement();
|
|
440
|
+
c.width = 1;
|
|
441
|
+
c.height = 1;
|
|
442
|
+
const ctx = c.getContext('2d', {
|
|
443
|
+
willReadFrequently: true,
|
|
444
|
+
});
|
|
445
|
+
ctx.clearRect(0, 0, 1, 1);
|
|
446
|
+
ctx.fillStyle = 'rgba(255,0,255,1)';
|
|
447
|
+
ctx.fillRect(0, 0, 1, 1);
|
|
448
|
+
const d = ctx.getImageData(0, 0, 1, 1).data;
|
|
449
|
+
const exact = d[0] === 255 && d[1] === 0 && d[2] === 255 && d[3] === 255;
|
|
450
|
+
_isCanvasFarblingActive = !exact;
|
|
451
|
+
this.releaseCanvas(c);
|
|
452
|
+
return _isCanvasFarblingActive;
|
|
453
|
+
},
|
|
454
|
+
getHitColor() {
|
|
455
|
+
const color = this.getRandomColor();
|
|
456
|
+
return this.isCanvasFarblingActive()
|
|
457
|
+
? this.getSnappedHexColor(color)
|
|
458
|
+
: color;
|
|
459
|
+
},
|
|
460
|
+
getHitColorKey(r, g, b) {
|
|
461
|
+
if (this.isCanvasFarblingActive()) {
|
|
462
|
+
r = Math.round(r / 5) * 5;
|
|
463
|
+
g = Math.round(g / 5) * 5;
|
|
464
|
+
b = Math.round(b / 5) * 5;
|
|
465
|
+
}
|
|
466
|
+
return HASH + this._rgbToHex(r, g, b);
|
|
467
|
+
},
|
|
468
|
+
getSnappedHexColor(hex) {
|
|
469
|
+
const rgb = this._hexToRgb(hex);
|
|
470
|
+
return (HASH +
|
|
471
|
+
this._rgbToHex(Math.round(rgb.r / 5) * 5, Math.round(rgb.g / 5) * 5, Math.round(rgb.b / 5) * 5));
|
|
472
|
+
},
|
|
430
473
|
getRGB(color) {
|
|
431
474
|
let rgb;
|
|
432
475
|
if (color in COLORS) {
|
package/lib/_CoreInternals.d.ts
CHANGED
|
@@ -64,6 +64,10 @@ export declare const Konva: {
|
|
|
64
64
|
_rgbToHex(r: number, g: number, b: number): string;
|
|
65
65
|
_hexToRgb(hex: string): import("./types.ts").RGB;
|
|
66
66
|
getRandomColor(): string;
|
|
67
|
+
isCanvasFarblingActive(): boolean;
|
|
68
|
+
getHitColor(): string;
|
|
69
|
+
getHitColorKey(r: number, g: number, b: number): string;
|
|
70
|
+
getSnappedHexColor(hex: string): string;
|
|
67
71
|
getRGB(color: string): import("./types.ts").RGB;
|
|
68
72
|
colorToRGBA(str: string): {
|
|
69
73
|
r: number;
|
package/lib/_FullInternals.d.ts
CHANGED
|
@@ -70,6 +70,10 @@ export declare const Konva: {
|
|
|
70
70
|
_rgbToHex(r: number, g: number, b: number): string;
|
|
71
71
|
_hexToRgb(hex: string): import("./types.ts").RGB;
|
|
72
72
|
getRandomColor(): string;
|
|
73
|
+
isCanvasFarblingActive(): boolean;
|
|
74
|
+
getHitColor(): string;
|
|
75
|
+
getHitColorKey(r: number, g: number, b: number): string;
|
|
76
|
+
getSnappedHexColor(hex: string): string;
|
|
73
77
|
getRGB(color: string): import("./types.ts").RGB;
|
|
74
78
|
colorToRGBA(str: string): {
|
|
75
79
|
r: number;
|