konva 10.9.1 → 10.11.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 +22 -10
- package/konva.js +19184 -18924
- package/konva.min.js +50 -50
- package/lib/Animation.js +3 -3
- package/lib/Canvas.js +11 -3
- package/lib/Container.js +5 -4
- package/lib/Context.d.ts +1 -2
- package/lib/Context.js +12 -39
- package/lib/Core.js +2 -2
- package/lib/DragAndDrop.js +1 -1
- package/lib/Factory.js +2 -2
- package/lib/FastLayer.js +3 -3
- package/lib/Global.d.ts +0 -1
- package/lib/Global.js +13 -7
- package/lib/Group.js +5 -5
- package/lib/Layer.d.ts +1 -1
- package/lib/Layer.js +8 -8
- package/lib/Node.d.ts +3 -1
- package/lib/Node.js +105 -66
- package/lib/PointerEvents.js +1 -1
- package/lib/Shape.d.ts +8 -1
- package/lib/Shape.js +129 -32
- package/lib/Stage.js +8 -8
- package/lib/Tween.js +4 -4
- package/lib/Util.d.ts +67 -3
- package/lib/Util.js +123 -42
- package/lib/Validators.js +2 -2
- package/lib/_CoreInternals.d.ts +15 -11
- package/lib/_CoreInternals.js +16 -14
- package/lib/_FullInternals.d.ts +14 -10
- package/lib/_FullInternals.js +38 -38
- package/lib/canvas-backend.js +6 -15
- package/lib/filters/Blur.js +3 -3
- package/lib/filters/Brighten.js +3 -3
- package/lib/filters/Brightness.js +2 -2
- package/lib/filters/Contrast.js +3 -3
- package/lib/filters/Emboss.js +10 -9
- package/lib/filters/Enhance.js +3 -3
- package/lib/filters/HSL.js +3 -3
- package/lib/filters/HSV.js +3 -3
- package/lib/filters/Kaleidoscope.js +3 -3
- package/lib/filters/Mask.js +3 -3
- package/lib/filters/Noise.js +3 -3
- package/lib/filters/Pixelate.js +4 -4
- package/lib/filters/Posterize.js +3 -3
- package/lib/filters/RGB.js +3 -3
- package/lib/filters/RGBA.js +3 -3
- package/lib/filters/Threshold.js +3 -3
- package/lib/index.js +1 -1
- package/lib/napi-backend.d.ts +2 -0
- package/lib/napi-backend.js +15 -0
- package/lib/shapes/Arc.js +5 -5
- package/lib/shapes/Arrow.js +5 -5
- package/lib/shapes/Circle.js +4 -4
- package/lib/shapes/Ellipse.js +4 -4
- package/lib/shapes/Image.js +5 -5
- package/lib/shapes/Label.js +7 -7
- package/lib/shapes/Line.js +6 -6
- package/lib/shapes/Path.js +5 -5
- package/lib/shapes/Rect.js +5 -5
- package/lib/shapes/RegularPolygon.js +5 -5
- package/lib/shapes/Ring.js +4 -4
- package/lib/shapes/Sprite.js +5 -5
- package/lib/shapes/Star.js +4 -4
- package/lib/shapes/Text.d.ts +0 -1
- package/lib/shapes/Text.js +32 -30
- package/lib/shapes/TextPath.js +7 -7
- package/lib/shapes/Transformer.js +12 -11
- package/lib/shapes/Wedge.js +5 -5
- package/lib/skia-backend.js +17 -25
- package/package.json +31 -34
package/lib/Shape.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Konva } from
|
|
2
|
-
import { Transform, Util } from
|
|
3
|
-
import { Factory } from
|
|
4
|
-
import { Node } from
|
|
5
|
-
import { getNumberValidator, getNumberOrAutoValidator, getStringValidator, getBooleanValidator, getStringOrGradientValidator, } from
|
|
6
|
-
import { _boundedShapes, _registerNode } from
|
|
7
|
-
import * as PointerEvents from
|
|
1
|
+
import { Konva } from './Global.js';
|
|
2
|
+
import { Transform, Util, getDummyContext } from './Util.js';
|
|
3
|
+
import { Factory } from './Factory.js';
|
|
4
|
+
import { Node } from './Node.js';
|
|
5
|
+
import { getNumberValidator, getNumberOrAutoValidator, getStringValidator, getBooleanValidator, getStringOrGradientValidator, } from './Validators.js';
|
|
6
|
+
import { _boundedShapes, _registerNode } from './Global.js';
|
|
7
|
+
import * as PointerEvents from './PointerEvents.js';
|
|
8
8
|
const HAS_SHADOW = 'hasShadow';
|
|
9
9
|
const HAS_FILL = 'hasFill';
|
|
10
10
|
const HAS_STROKE = 'hasStroke';
|
|
@@ -12,16 +12,8 @@ const SHADOW_RGBA = 'shadowRGBA';
|
|
|
12
12
|
const patternImage = 'patternImage';
|
|
13
13
|
const linearGradient = 'linearGradient';
|
|
14
14
|
const radialGradient = 'radialGradient';
|
|
15
|
-
let dummyContext;
|
|
16
|
-
function getDummyContext() {
|
|
17
|
-
if (dummyContext) {
|
|
18
|
-
return dummyContext;
|
|
19
|
-
}
|
|
20
|
-
dummyContext = Util.createCanvasElement().getContext('2d');
|
|
21
|
-
return dummyContext;
|
|
22
|
-
}
|
|
23
15
|
export const shapes = {};
|
|
24
|
-
function
|
|
16
|
+
function fillPath(context) {
|
|
25
17
|
const fillRule = this.attrs.fillRule;
|
|
26
18
|
if (fillRule) {
|
|
27
19
|
context.fill(fillRule);
|
|
@@ -30,6 +22,46 @@ function _fillFunc(context) {
|
|
|
30
22
|
context.fill();
|
|
31
23
|
}
|
|
32
24
|
}
|
|
25
|
+
// Below this scale the ellipse is under a millionth of its radius across, so
|
|
26
|
+
// only its last color stop shows.
|
|
27
|
+
const MIN_GRADIENT_SCALE = 1e-6;
|
|
28
|
+
// Fills the current path. A canvas maps the path when it is built and the
|
|
29
|
+
// fill style when it is painted, so a transform set here moves only the
|
|
30
|
+
// gradient: that is how fillRadialGradientScale makes an ellipse.
|
|
31
|
+
function _fillFunc(context) {
|
|
32
|
+
const scaleX = this.fillRadialGradientScaleX();
|
|
33
|
+
const scaleY = this.fillRadialGradientScaleY();
|
|
34
|
+
if ((scaleX === 1 && scaleY === 1) ||
|
|
35
|
+
this._getFillType() !== 'radial-gradient') {
|
|
36
|
+
fillPath.call(this, context);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const min = Math.min(Math.abs(scaleX), Math.abs(scaleY));
|
|
40
|
+
context.save();
|
|
41
|
+
try {
|
|
42
|
+
if (min < MIN_GRADIENT_SCALE) {
|
|
43
|
+
const stops = this.fillRadialGradientColorStops();
|
|
44
|
+
context.setAttr('fillStyle', stops[stops.length - 1]);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
// The smaller scale of the transform is made 1 and the gradient shrinks
|
|
48
|
+
// by that factor instead. It is the same ellipse, but a pixel far from
|
|
49
|
+
// it stays close to it in gradient space: node-canvas on Linux (Cairo
|
|
50
|
+
// fixed point) paints nothing, or breaks the context, past ~32k there.
|
|
51
|
+
const sx = scaleX / min;
|
|
52
|
+
const sy = scaleY / min;
|
|
53
|
+
const { x, y } = this.fillRadialGradientEndPoint();
|
|
54
|
+
context.transform(sx, 0, 0, sy, x - sx * x, y - sy * y);
|
|
55
|
+
if (min !== 1) {
|
|
56
|
+
context.setAttr('fillStyle', this.__getRadialGradient(min));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
fillPath.call(this, context);
|
|
60
|
+
}
|
|
61
|
+
finally {
|
|
62
|
+
context.restore();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
33
65
|
function _strokeFunc(context) {
|
|
34
66
|
context.stroke();
|
|
35
67
|
}
|
|
@@ -138,6 +170,29 @@ export class Shape extends Node {
|
|
|
138
170
|
this.shadowOffsetX() ||
|
|
139
171
|
this.shadowOffsetY()));
|
|
140
172
|
}
|
|
173
|
+
// The fill the shape paints with, by fillPriority, then the first one set.
|
|
174
|
+
_getFillType(color = this.fill()) {
|
|
175
|
+
const priority = this.fillPriority();
|
|
176
|
+
if (color && priority === 'color')
|
|
177
|
+
return 'color';
|
|
178
|
+
const pattern = this.fillPatternImage();
|
|
179
|
+
if (pattern && priority === 'pattern')
|
|
180
|
+
return 'pattern';
|
|
181
|
+
const linear = this.fillLinearGradientColorStops();
|
|
182
|
+
if (linear && priority === 'linear-gradient')
|
|
183
|
+
return 'linear-gradient';
|
|
184
|
+
const radial = this.fillRadialGradientColorStops();
|
|
185
|
+
if (radial && priority === 'radial-gradient')
|
|
186
|
+
return 'radial-gradient';
|
|
187
|
+
if (color)
|
|
188
|
+
return 'color';
|
|
189
|
+
if (pattern)
|
|
190
|
+
return 'pattern';
|
|
191
|
+
if (linear)
|
|
192
|
+
return 'linear-gradient';
|
|
193
|
+
if (radial)
|
|
194
|
+
return 'radial-gradient';
|
|
195
|
+
}
|
|
141
196
|
_toStyleSpace(point) {
|
|
142
197
|
return this._styleTransform ? this._styleTransform.point(point) : point;
|
|
143
198
|
}
|
|
@@ -160,18 +215,7 @@ export class Shape extends Node {
|
|
|
160
215
|
tr.rotate(Konva.getAngle(this.fillPatternRotation()));
|
|
161
216
|
tr.scale(this.fillPatternScaleX(), this.fillPatternScaleY());
|
|
162
217
|
tr.translate(-1 * this.fillPatternOffsetX(), -1 * this.fillPatternOffsetY());
|
|
163
|
-
|
|
164
|
-
const matrix = typeof DOMMatrix === 'undefined'
|
|
165
|
-
? {
|
|
166
|
-
a: m[0], // Horizontal scaling. A value of 1 results in no scaling.
|
|
167
|
-
b: m[1], // Vertical skewing.
|
|
168
|
-
c: m[2], // Horizontal skewing.
|
|
169
|
-
d: m[3],
|
|
170
|
-
e: m[4], // Horizontal translation (moving).
|
|
171
|
-
f: m[5], // Vertical translation (moving).
|
|
172
|
-
}
|
|
173
|
-
: new DOMMatrix(m);
|
|
174
|
-
pattern.setTransform(matrix);
|
|
218
|
+
pattern.setTransform(Util.createDOMMatrix(tr.getMatrix()));
|
|
175
219
|
}
|
|
176
220
|
return pattern;
|
|
177
221
|
}
|
|
@@ -199,14 +243,15 @@ export class Shape extends Node {
|
|
|
199
243
|
return this.__getRadialGradient();
|
|
200
244
|
return this._getCache(radialGradient, this.__getRadialGradient);
|
|
201
245
|
}
|
|
202
|
-
|
|
246
|
+
// shrink scales the gradient about its end point (see _fillFunc)
|
|
247
|
+
__getRadialGradient(shrink = 1) {
|
|
203
248
|
const colorStops = this.fillRadialGradientColorStops();
|
|
204
249
|
if (colorStops) {
|
|
205
250
|
const ctx = getDummyContext();
|
|
206
251
|
// a rigid transform keeps the radii
|
|
207
252
|
const start = this._toStyleSpace(this.fillRadialGradientStartPoint());
|
|
208
253
|
const end = this._toStyleSpace(this.fillRadialGradientEndPoint());
|
|
209
|
-
const grd = ctx.createRadialGradient(start.x, start.y, this.fillRadialGradientStartRadius(), end.x, end.y, this.fillRadialGradientEndRadius());
|
|
254
|
+
const grd = ctx.createRadialGradient(end.x + (start.x - end.x) * shrink, end.y + (start.y - end.y) * shrink, this.fillRadialGradientStartRadius() * shrink, end.x, end.y, this.fillRadialGradientEndRadius() * shrink);
|
|
210
255
|
// build color stops
|
|
211
256
|
for (let n = 0; n < colorStops.length; n += 2) {
|
|
212
257
|
grd.addColorStop(colorStops[n], colorStops[n + 1]);
|
|
@@ -661,8 +706,7 @@ export class Shape extends Node {
|
|
|
661
706
|
}
|
|
662
707
|
Shape.prototype._fillFunc = _fillFunc;
|
|
663
708
|
Shape.prototype._strokeFunc = _strokeFunc;
|
|
664
|
-
|
|
665
|
-
Shape.prototype._fillFuncHit = _fillFunc;
|
|
709
|
+
Shape.prototype._fillFuncHit = fillPath;
|
|
666
710
|
Shape.prototype._strokeFuncHit = _strokeFunc;
|
|
667
711
|
Shape.prototype._centroid = false;
|
|
668
712
|
Shape.prototype.nodeType = 'Shape';
|
|
@@ -1167,6 +1211,59 @@ Factory.addGetterSetter(Shape, 'fillRadialGradientColorStops');
|
|
|
1167
1211
|
* // halfway through, and then changes to green
|
|
1168
1212
|
* shape.fillRadialGradientColorStops(0, 'red', 0.5, 'blue', 1, 'green');
|
|
1169
1213
|
*/
|
|
1214
|
+
Factory.addComponentsGetterSetter(Shape, 'fillRadialGradientScale', ['x', 'y']);
|
|
1215
|
+
/**
|
|
1216
|
+
* get/set fill radial gradient scale. The whole gradient, both of its circles,
|
|
1217
|
+
* is scaled about the end point, so a scale that differs on the two axes makes
|
|
1218
|
+
* an elliptical gradient. A scale of 0 paints the last color stop. Applies to
|
|
1219
|
+
* the path a shape builds and fills with `context.fillShape()` or
|
|
1220
|
+
* `context.fillStrokeShape()`: built-in shapes and custom `sceneFunc` shapes.
|
|
1221
|
+
* Shapes with their own fill function ignore it, among them `Konva.Text` and
|
|
1222
|
+
* `Konva.TextPath`, which fill glyphs.
|
|
1223
|
+
* @name Konva.Shape#fillRadialGradientScale
|
|
1224
|
+
* @method
|
|
1225
|
+
* @param {Object} scale
|
|
1226
|
+
* @param {Number} scale.x
|
|
1227
|
+
* @param {Number} scale.y
|
|
1228
|
+
* @returns {Object}
|
|
1229
|
+
* @example
|
|
1230
|
+
* // get fill radial gradient scale
|
|
1231
|
+
* var gradientScale = shape.fillRadialGradientScale();
|
|
1232
|
+
*
|
|
1233
|
+
* // an ellipse half as tall as it is wide
|
|
1234
|
+
* shape.fillRadialGradientScale({
|
|
1235
|
+
* x: 1,
|
|
1236
|
+
* y: 0.5
|
|
1237
|
+
* });
|
|
1238
|
+
*/
|
|
1239
|
+
Factory.addGetterSetter(Shape, 'fillRadialGradientScaleX', 1, getNumberValidator());
|
|
1240
|
+
/**
|
|
1241
|
+
* get/set fill radial gradient scale x
|
|
1242
|
+
* @name Konva.Shape#fillRadialGradientScaleX
|
|
1243
|
+
* @method
|
|
1244
|
+
* @param {Number} x
|
|
1245
|
+
* @returns {Number}
|
|
1246
|
+
* @example
|
|
1247
|
+
* // get fill radial gradient scale x
|
|
1248
|
+
* var gradientScaleX = shape.fillRadialGradientScaleX();
|
|
1249
|
+
*
|
|
1250
|
+
* // set fill radial gradient scale x
|
|
1251
|
+
* shape.fillRadialGradientScaleX(2);
|
|
1252
|
+
*/
|
|
1253
|
+
Factory.addGetterSetter(Shape, 'fillRadialGradientScaleY', 1, getNumberValidator());
|
|
1254
|
+
/**
|
|
1255
|
+
* get/set fill radial gradient scale y
|
|
1256
|
+
* @name Konva.Shape#fillRadialGradientScaleY
|
|
1257
|
+
* @method
|
|
1258
|
+
* @param {Number} y
|
|
1259
|
+
* @returns {Number}
|
|
1260
|
+
* @example
|
|
1261
|
+
* // get fill radial gradient scale y
|
|
1262
|
+
* var gradientScaleY = shape.fillRadialGradientScaleY();
|
|
1263
|
+
*
|
|
1264
|
+
* // set fill radial gradient scale y
|
|
1265
|
+
* shape.fillRadialGradientScaleY(0.5);
|
|
1266
|
+
*/
|
|
1170
1267
|
Factory.addGetterSetter(Shape, 'fillPatternRepeat', 'repeat');
|
|
1171
1268
|
/**
|
|
1172
1269
|
* get/set fill pattern repeat. Can be 'repeat', 'repeat-x', 'repeat-y', or 'no-repeat'. The default is 'repeat'
|
package/lib/Stage.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { Util } from
|
|
2
|
-
import { Factory } from
|
|
3
|
-
import { Container } from
|
|
4
|
-
import { Konva } from
|
|
5
|
-
import { SceneCanvas, HitCanvas } from
|
|
6
|
-
import { DD } from
|
|
7
|
-
import { _registerNode } from
|
|
8
|
-
import * as PointerEvents from
|
|
1
|
+
import { Util } from './Util.js';
|
|
2
|
+
import { Factory } from './Factory.js';
|
|
3
|
+
import { Container } from './Container.js';
|
|
4
|
+
import { Konva } from './Global.js';
|
|
5
|
+
import { SceneCanvas, HitCanvas } from './Canvas.js';
|
|
6
|
+
import { DD } from './DragAndDrop.js';
|
|
7
|
+
import { _registerNode } from './Global.js';
|
|
8
|
+
import * as PointerEvents from './PointerEvents.js';
|
|
9
9
|
// CONSTANTS
|
|
10
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 = [
|
|
11
11
|
[MOUSEENTER, '_pointerenter'],
|
package/lib/Tween.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Util } from
|
|
2
|
-
import { Animation } from
|
|
3
|
-
import { Node } from
|
|
4
|
-
import { Konva } from
|
|
1
|
+
import { Util } from './Util.js';
|
|
2
|
+
import { Animation } from './Animation.js';
|
|
3
|
+
import { Node } from './Node.js';
|
|
4
|
+
import { Konva } from './Global.js';
|
|
5
5
|
// every config key that is not an attribute to tween
|
|
6
6
|
const blacklist = {
|
|
7
7
|
node: 1,
|
package/lib/Util.d.ts
CHANGED
|
@@ -130,10 +130,28 @@ export declare const Util: {
|
|
|
130
130
|
_isBoolean(obj: any): obj is boolean;
|
|
131
131
|
isObject(val: any): val is object;
|
|
132
132
|
isValidSelector(selector: any): boolean;
|
|
133
|
-
_sign(number: number): 1 |
|
|
133
|
+
_sign(number: number): -1 | 1;
|
|
134
134
|
requestAnimFrame(callback: Function, win?: Window | null): void;
|
|
135
135
|
createCanvasElement(): HTMLCanvasElement;
|
|
136
136
|
createImageElement(): HTMLImageElement;
|
|
137
|
+
/**
|
|
138
|
+
* Make a DOMMatrix of the canvas library Konva draws with. Without a createDOMMatrix
|
|
139
|
+
* in Konva.setCanvasBackend(), the class comes from the library's own context.
|
|
140
|
+
* @method
|
|
141
|
+
* @memberof Konva.Util
|
|
142
|
+
* @param {Array} [init] the six numbers a, b, c, d, e, f
|
|
143
|
+
* @returns {DOMMatrix}
|
|
144
|
+
*/
|
|
145
|
+
createDOMMatrix(init?: number[]): DOMMatrix;
|
|
146
|
+
/**
|
|
147
|
+
* Make a Path2D of the canvas library Konva draws with, for example for a clipFunc.
|
|
148
|
+
* Without a createPath2D in Konva.setCanvasBackend(), it is the global Path2D.
|
|
149
|
+
* @method
|
|
150
|
+
* @memberof Konva.Util
|
|
151
|
+
* @param {String} [path] SVG path data
|
|
152
|
+
* @returns {Path2D}
|
|
153
|
+
*/
|
|
154
|
+
createPath2D(path?: string): Path2D;
|
|
137
155
|
_urlToImage(url: string, callback: Function, onError?: (error: Error) => void): void;
|
|
138
156
|
_rgbToHex(r: number, g: number, b: number): string;
|
|
139
157
|
_hexToRgb(hex: string): RGB;
|
|
@@ -151,7 +169,7 @@ export declare const Util: {
|
|
|
151
169
|
* @memberof Konva.Util
|
|
152
170
|
* @returns {Boolean}
|
|
153
171
|
*/
|
|
154
|
-
isCanvasFarblingActive()
|
|
172
|
+
isCanvasFarblingActive: () => boolean;
|
|
155
173
|
/**
|
|
156
174
|
* Get a random color for hit detection (snapped to the hit color grid)
|
|
157
175
|
* @method
|
|
@@ -266,11 +284,57 @@ export declare const Util: {
|
|
|
266
284
|
_prepareArrayForTween(startArray: any, endArray: any, isClosed: any): number[];
|
|
267
285
|
_prepareToStringify<T>(obj: any, ancestors?: Set<object>): T;
|
|
268
286
|
_assign<T, U>(target: T, source: U): T & U;
|
|
269
|
-
_getEventType(type: string):
|
|
287
|
+
_getEventType(type: string): 'mouse' | 'touch' | 'pointer';
|
|
270
288
|
_getFirstPointerId(evt: any): any;
|
|
271
289
|
releaseCanvas(...canvases: HTMLCanvasElement[]): void;
|
|
272
290
|
_cornerRadii(cornerRadius: number | number[], width: number, height: number): [number, number, number, number];
|
|
273
291
|
drawRoundedRectPath(context: Context, width: number, height: number, cornerRadius: number | number[]): void;
|
|
274
292
|
drawRoundedPolygonPath(context: Context, points: Vector2d[], sides: number, radius: number, cornerRadius: number | number[]): void;
|
|
275
293
|
};
|
|
294
|
+
export interface CanvasBackend {
|
|
295
|
+
/** what Konva.getCanvasBackend() reports */
|
|
296
|
+
name: string;
|
|
297
|
+
/** makes a canvas with a 2d context; Konva sets its size */
|
|
298
|
+
createCanvas: () => object;
|
|
299
|
+
/** makes an image that loads from its src; without it Konva can not load images */
|
|
300
|
+
createImage?: () => object;
|
|
301
|
+
/** makes a DOMMatrix; by default its class comes from the canvas */
|
|
302
|
+
createDOMMatrix?: (init?: number[]) => object;
|
|
303
|
+
/** makes a Path2D; by default the global Path2D */
|
|
304
|
+
createPath2D?: (path?: string) => object;
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Set the canvas library Konva draws with: Konva makes every canvas and image through it.
|
|
308
|
+
* Applies to everything Konva creates afterwards, including the context it measures text
|
|
309
|
+
* and makes gradients with. Stages and nodes created before keep their canvases, so
|
|
310
|
+
* create them again after a switch.
|
|
311
|
+
* @method
|
|
312
|
+
* @name Konva.setCanvasBackend
|
|
313
|
+
* @param {Object} backend
|
|
314
|
+
* @param {String} backend.name reported by Konva.getCanvasBackend()
|
|
315
|
+
* @param {Function} backend.createCanvas returns a new canvas
|
|
316
|
+
* @param {Function} [backend.createImage] returns a new image
|
|
317
|
+
* @param {Function} [backend.createDOMMatrix] returns a new DOMMatrix, see Konva.Util.createDOMMatrix
|
|
318
|
+
* @param {Function} [backend.createPath2D] returns a new Path2D, see Konva.Util.createPath2D
|
|
319
|
+
* @example
|
|
320
|
+
* import { Canvas, Image } from '@napi-rs/canvas';
|
|
321
|
+
* Konva.setCanvasBackend({
|
|
322
|
+
* name: 'my-napi-rs',
|
|
323
|
+
* createCanvas: () => new Canvas(300, 150),
|
|
324
|
+
* createImage: () => new Image(),
|
|
325
|
+
* });
|
|
326
|
+
*/
|
|
327
|
+
export declare function setCanvasBackend(backend: CanvasBackend): void;
|
|
328
|
+
/**
|
|
329
|
+
* The name of the canvas backend Konva draws with: "web" in a browser, "offscreen-canvas" in a
|
|
330
|
+
* Web Worker, the name given to Konva.setCanvasBackend(), "custom" when
|
|
331
|
+
* Konva.Util.createCanvasElement was replaced directly, or null when there is none, as in
|
|
332
|
+
* Node.js before a backend is set.
|
|
333
|
+
* @method
|
|
334
|
+
* @name Konva.getCanvasBackend
|
|
335
|
+
* @returns {String|null}
|
|
336
|
+
*/
|
|
337
|
+
export declare function getCanvasBackend(): string | null;
|
|
338
|
+
export declare function _perCanvasBackend<T>(compute: () => T): () => T;
|
|
339
|
+
export declare const getDummyContext: () => CanvasRenderingContext2D;
|
|
276
340
|
export type AnyString<T> = T | (string & {});
|
package/lib/Util.js
CHANGED
|
@@ -1,18 +1,5 @@
|
|
|
1
|
-
import { Konva } from
|
|
2
|
-
const NODE_ERROR =
|
|
3
|
-
|
|
4
|
-
Looks like you are trying to use Konva.js in Node.js environment (or in a Web Worker), because "document" object is undefined.
|
|
5
|
-
|
|
6
|
-
To use Konva.js in Node.js environment, you need to use the "canvas-backend" or "skia-backend" module.
|
|
7
|
-
|
|
8
|
-
bash: npm install canvas
|
|
9
|
-
js: import "konva/canvas-backend";
|
|
10
|
-
|
|
11
|
-
or
|
|
12
|
-
|
|
13
|
-
bash: npm install skia-canvas
|
|
14
|
-
js: import "konva/skia-backend";
|
|
15
|
-
`;
|
|
1
|
+
import { Konva } from './Global.js';
|
|
2
|
+
const NODE_ERROR = 'Konva needs a canvas outside the browser. Import "konva/canvas-backend", "konva/skia-backend" or "konva/napi-backend", or call Konva.setCanvasBackend().';
|
|
16
3
|
const ensureBrowser = () => {
|
|
17
4
|
if (typeof document === 'undefined') {
|
|
18
5
|
throw new Error(NODE_ERROR);
|
|
@@ -282,13 +269,11 @@ export class Transform {
|
|
|
282
269
|
result.skewY = 0;
|
|
283
270
|
}
|
|
284
271
|
else if (c != 0 || d != 0) {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
result.
|
|
289
|
-
result.scaleY =
|
|
290
|
-
result.skewX = 0;
|
|
291
|
-
result.skewY = delta && (a * c + b * d) / delta;
|
|
272
|
+
// a = b = 0: the x axis is collapsed, so scaleX and the skew stay 0 and
|
|
273
|
+
// only the y axis, the scaled unit vector (-sin, cos), carries the
|
|
274
|
+
// rotation. "+ 0" turns the -0 that atan2 gives for a zero angle into 0
|
|
275
|
+
result.rotation = Math.atan2(-c, d) + 0;
|
|
276
|
+
result.scaleY = Math.sqrt(c * c + d * d);
|
|
292
277
|
}
|
|
293
278
|
else {
|
|
294
279
|
// a = b = c = d = 0
|
|
@@ -449,8 +434,6 @@ const OBJECT_ARRAY = '[object Array]', OBJECT_NUMBER = '[object Number]', OBJECT
|
|
|
449
434
|
yellow: [255, 255, 0],
|
|
450
435
|
yellowgreen: [154, 205, 50],
|
|
451
436
|
};
|
|
452
|
-
// Cache for canvas farbling detection
|
|
453
|
-
let _isCanvasFarblingActive = null;
|
|
454
437
|
// A stage may be rendered in another window than the one Konva was imported
|
|
455
438
|
// into, and a window that is not visible gives almost no frames. So frames are
|
|
456
439
|
// asked from the window of the stage, and the callbacks are kept per window
|
|
@@ -552,7 +535,7 @@ export const Util = {
|
|
|
552
535
|
return val instanceof Object;
|
|
553
536
|
},
|
|
554
537
|
isValidSelector(selector) {
|
|
555
|
-
if (typeof selector !== 'string') {
|
|
538
|
+
if (typeof selector !== 'string' || !selector) {
|
|
556
539
|
return false;
|
|
557
540
|
}
|
|
558
541
|
const firstChar = selector[0];
|
|
@@ -590,18 +573,36 @@ export const Util = {
|
|
|
590
573
|
},
|
|
591
574
|
createCanvasElement() {
|
|
592
575
|
ensureBrowser();
|
|
593
|
-
|
|
594
|
-
// on some environments canvas.style is readonly
|
|
595
|
-
try {
|
|
596
|
-
canvas.style = canvas.style || {};
|
|
597
|
-
}
|
|
598
|
-
catch (e) { }
|
|
599
|
-
return canvas;
|
|
576
|
+
return document.createElement('canvas');
|
|
600
577
|
},
|
|
601
578
|
createImageElement() {
|
|
602
579
|
ensureBrowser();
|
|
603
580
|
return document.createElement('img');
|
|
604
581
|
},
|
|
582
|
+
/**
|
|
583
|
+
* Make a DOMMatrix of the canvas library Konva draws with. Without a createDOMMatrix
|
|
584
|
+
* in Konva.setCanvasBackend(), the class comes from the library's own context.
|
|
585
|
+
* @method
|
|
586
|
+
* @memberof Konva.Util
|
|
587
|
+
* @param {Array} [init] the six numbers a, b, c, d, e, f
|
|
588
|
+
* @returns {DOMMatrix}
|
|
589
|
+
*/
|
|
590
|
+
createDOMMatrix(init) {
|
|
591
|
+
// node-canvas accepts only its own DOMMatrix, so the global one may not fit
|
|
592
|
+
const Matrix = getDummyContext().getTransform().constructor;
|
|
593
|
+
return new Matrix(init);
|
|
594
|
+
},
|
|
595
|
+
/**
|
|
596
|
+
* Make a Path2D of the canvas library Konva draws with, for example for a clipFunc.
|
|
597
|
+
* Without a createPath2D in Konva.setCanvasBackend(), it is the global Path2D.
|
|
598
|
+
* @method
|
|
599
|
+
* @memberof Konva.Util
|
|
600
|
+
* @param {String} [path] SVG path data
|
|
601
|
+
* @returns {Path2D}
|
|
602
|
+
*/
|
|
603
|
+
createPath2D(path) {
|
|
604
|
+
return new Path2D(path);
|
|
605
|
+
},
|
|
605
606
|
/*
|
|
606
607
|
* arg can be an image object or image data
|
|
607
608
|
*/
|
|
@@ -648,15 +649,15 @@ export const Util = {
|
|
|
648
649
|
* @memberof Konva.Util
|
|
649
650
|
* @returns {Boolean}
|
|
650
651
|
*/
|
|
651
|
-
isCanvasFarblingActive() {
|
|
652
|
-
|
|
653
|
-
|
|
652
|
+
isCanvasFarblingActive: _perCanvasBackend(() => {
|
|
653
|
+
let c;
|
|
654
|
+
try {
|
|
655
|
+
c = Util.createCanvasElement();
|
|
654
656
|
}
|
|
655
|
-
|
|
656
|
-
|
|
657
|
+
catch (e) {
|
|
658
|
+
// no canvas to read, e.g. Node.js without a backend
|
|
657
659
|
return false;
|
|
658
660
|
}
|
|
659
|
-
const c = this.createCanvasElement();
|
|
660
661
|
c.width = 10;
|
|
661
662
|
c.height = 10;
|
|
662
663
|
const ctx = c.getContext('2d', {
|
|
@@ -676,10 +677,9 @@ export const Util = {
|
|
|
676
677
|
break;
|
|
677
678
|
}
|
|
678
679
|
}
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
},
|
|
680
|
+
Util.releaseCanvas(c);
|
|
681
|
+
return isFarbling;
|
|
682
|
+
}),
|
|
683
683
|
/**
|
|
684
684
|
* Get a random color for hit detection (snapped to the hit color grid)
|
|
685
685
|
* @method
|
|
@@ -1220,3 +1220,84 @@ export const Util = {
|
|
|
1220
1220
|
}
|
|
1221
1221
|
},
|
|
1222
1222
|
};
|
|
1223
|
+
let activeBackend = { name: 'web', createCanvas: Util.createCanvasElement };
|
|
1224
|
+
const { createDOMMatrix, createPath2D } = Util;
|
|
1225
|
+
/**
|
|
1226
|
+
* Set the canvas library Konva draws with: Konva makes every canvas and image through it.
|
|
1227
|
+
* Applies to everything Konva creates afterwards, including the context it measures text
|
|
1228
|
+
* and makes gradients with. Stages and nodes created before keep their canvases, so
|
|
1229
|
+
* create them again after a switch.
|
|
1230
|
+
* @method
|
|
1231
|
+
* @name Konva.setCanvasBackend
|
|
1232
|
+
* @param {Object} backend
|
|
1233
|
+
* @param {String} backend.name reported by Konva.getCanvasBackend()
|
|
1234
|
+
* @param {Function} backend.createCanvas returns a new canvas
|
|
1235
|
+
* @param {Function} [backend.createImage] returns a new image
|
|
1236
|
+
* @param {Function} [backend.createDOMMatrix] returns a new DOMMatrix, see Konva.Util.createDOMMatrix
|
|
1237
|
+
* @param {Function} [backend.createPath2D] returns a new Path2D, see Konva.Util.createPath2D
|
|
1238
|
+
* @example
|
|
1239
|
+
* import { Canvas, Image } from '@napi-rs/canvas';
|
|
1240
|
+
* Konva.setCanvasBackend({
|
|
1241
|
+
* name: 'my-napi-rs',
|
|
1242
|
+
* createCanvas: () => new Canvas(300, 150),
|
|
1243
|
+
* createImage: () => new Image(),
|
|
1244
|
+
* });
|
|
1245
|
+
*/
|
|
1246
|
+
export function setCanvasBackend(backend) {
|
|
1247
|
+
var _a, _b;
|
|
1248
|
+
const { name, createCanvas, createImage } = backend;
|
|
1249
|
+
Util.createCanvasElement = createCanvas;
|
|
1250
|
+
Util.createDOMMatrix = ((_a = backend.createDOMMatrix) !== null && _a !== void 0 ? _a : createDOMMatrix);
|
|
1251
|
+
Util.createPath2D = ((_b = backend.createPath2D) !== null && _b !== void 0 ? _b : createPath2D);
|
|
1252
|
+
Util.createImageElement = createImage
|
|
1253
|
+
? createImage
|
|
1254
|
+
: () => {
|
|
1255
|
+
throw new Error(`Konva: the "${name}" canvas backend has no createImage(), so it can not load images.`);
|
|
1256
|
+
};
|
|
1257
|
+
activeBackend = { name, createCanvas: Util.createCanvasElement };
|
|
1258
|
+
}
|
|
1259
|
+
/**
|
|
1260
|
+
* The name of the canvas backend Konva draws with: "web" in a browser, "offscreen-canvas" in a
|
|
1261
|
+
* Web Worker, the name given to Konva.setCanvasBackend(), "custom" when
|
|
1262
|
+
* Konva.Util.createCanvasElement was replaced directly, or null when there is none, as in
|
|
1263
|
+
* Node.js before a backend is set.
|
|
1264
|
+
* @method
|
|
1265
|
+
* @name Konva.getCanvasBackend
|
|
1266
|
+
* @returns {String|null}
|
|
1267
|
+
*/
|
|
1268
|
+
export function getCanvasBackend() {
|
|
1269
|
+
if (Util.createCanvasElement !== activeBackend.createCanvas) {
|
|
1270
|
+
return 'custom';
|
|
1271
|
+
}
|
|
1272
|
+
// checked on each call: a document can appear after Konva loads (jsdom)
|
|
1273
|
+
return activeBackend.name === 'web' && typeof document === 'undefined'
|
|
1274
|
+
? null
|
|
1275
|
+
: activeBackend.name;
|
|
1276
|
+
}
|
|
1277
|
+
// a Web Worker has no document, but it has OffscreenCanvas
|
|
1278
|
+
if (typeof document === 'undefined' && typeof OffscreenCanvas !== 'undefined') {
|
|
1279
|
+
setCanvasBackend({
|
|
1280
|
+
name: 'offscreen-canvas',
|
|
1281
|
+
createCanvas: () => new OffscreenCanvas(300, 150),
|
|
1282
|
+
createImage: () => {
|
|
1283
|
+
throw new Error('Konva: a Web Worker has no image element. Load the image with createImageBitmap() and pass the ImageBitmap to Konva.Image.');
|
|
1284
|
+
},
|
|
1285
|
+
});
|
|
1286
|
+
}
|
|
1287
|
+
// A value Konva computes from a canvas, such as a feature check, belongs to
|
|
1288
|
+
// the library that made the canvas. It is recomputed when the canvas factory
|
|
1289
|
+
// changes, by setCanvasBackend() or by assigning Util.createCanvasElement
|
|
1290
|
+
export function _perCanvasBackend(compute) {
|
|
1291
|
+
let factory;
|
|
1292
|
+
let value;
|
|
1293
|
+
return () => {
|
|
1294
|
+
if (factory !== Util.createCanvasElement) {
|
|
1295
|
+
value = compute();
|
|
1296
|
+
factory = Util.createCanvasElement;
|
|
1297
|
+
}
|
|
1298
|
+
return value;
|
|
1299
|
+
};
|
|
1300
|
+
}
|
|
1301
|
+
// The context Konva measures text and makes gradients and patterns with. A
|
|
1302
|
+
// gradient from one canvas library is ignored by the context of another
|
|
1303
|
+
export const getDummyContext = _perCanvasBackend(() => Util.createCanvasElement().getContext('2d'));
|
package/lib/Validators.js
CHANGED