konva 10.4.0 → 10.6.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 +4 -4
- package/konva.js +3432 -3746
- package/konva.min.js +4 -4
- package/lib/Animation.d.ts +2 -2
- package/lib/Animation.js +2 -2
- package/lib/BezierFunctions.d.ts +1 -1
- package/lib/BezierFunctions.js +76 -116
- package/lib/Canvas.d.ts +1 -1
- package/lib/Canvas.js +8 -5
- package/lib/Container.js +9 -6
- package/lib/Context.d.ts +4 -5
- package/lib/Context.js +77 -72
- package/lib/DragAndDrop.d.ts +5 -3
- package/lib/DragAndDrop.js +65 -29
- package/lib/Factory.d.ts +3 -2
- package/lib/Factory.js +10 -9
- package/lib/Global.d.ts +0 -6
- package/lib/Global.js +2 -8
- package/lib/Layer.js +9 -6
- package/lib/Node.d.ts +33 -15
- package/lib/Node.js +179 -135
- package/lib/PointerEvents.d.ts +1 -2
- package/lib/PointerEvents.js +20 -18
- package/lib/Shape.d.ts +2 -2
- package/lib/Shape.js +51 -7
- package/lib/Stage.d.ts +23 -14
- package/lib/Stage.js +221 -93
- package/lib/Tween.js +59 -66
- package/lib/Util.d.ts +3 -2
- package/lib/Util.js +43 -16
- package/lib/_CoreInternals.d.ts +25 -11
- package/lib/_FullInternals.d.ts +25 -11
- package/lib/filters/Blur.js +16 -12
- package/lib/filters/Brighten.d.ts +1 -0
- package/lib/filters/Brighten.js +1 -0
- package/lib/filters/Brightness.d.ts +2 -1
- package/lib/filters/Brightness.js +4 -4
- package/lib/filters/Emboss.js +16 -58
- package/lib/filters/Kaleidoscope.js +8 -15
- package/lib/filters/Mask.js +8 -8
- package/lib/filters/Pixelate.js +16 -29
- package/lib/filters/Threshold.d.ts +1 -1
- package/lib/filters/Threshold.js +1 -1
- package/lib/shapes/Arc.js +5 -3
- package/lib/shapes/Arrow.d.ts +2 -6
- package/lib/shapes/Arrow.js +68 -46
- package/lib/shapes/Image.js +16 -19
- package/lib/shapes/Label.d.ts +2 -3
- package/lib/shapes/Label.js +7 -21
- package/lib/shapes/Line.d.ts +1 -1
- package/lib/shapes/Line.js +1 -1
- package/lib/shapes/Path.d.ts +27 -2
- package/lib/shapes/Path.js +148 -142
- package/lib/shapes/RegularPolygon.js +1 -1
- package/lib/shapes/Sprite.js +2 -6
- package/lib/shapes/Text.d.ts +12 -4
- package/lib/shapes/Text.js +96 -54
- package/lib/shapes/TextPath.d.ts +4 -3
- package/lib/shapes/TextPath.js +40 -26
- package/lib/shapes/Transformer.d.ts +16 -1
- package/lib/shapes/Transformer.js +209 -130
- package/lib/types.d.ts +2 -2
- package/package.json +4 -3
package/lib/filters/Emboss.js
CHANGED
|
@@ -45,85 +45,43 @@ export const Emboss = function (imageData) {
|
|
|
45
45
|
const cy = Math.sin(dirRad);
|
|
46
46
|
const SCALE = (128 / 1020) * strength; // ≈0.1255 * strength
|
|
47
47
|
// Precompute luminance (Rec.709)
|
|
48
|
-
const src = new Uint8ClampedArray(data); // snapshot
|
|
49
48
|
const lum = new Float32Array(w * h);
|
|
50
49
|
for (let p = 0, i = 0; i < data.length; i += 4, p++) {
|
|
51
|
-
lum[p] = 0.2126 *
|
|
50
|
+
lum[p] = 0.2126 * data[i] + 0.7152 * data[i + 1] + 0.0722 * data[i + 2];
|
|
52
51
|
}
|
|
53
52
|
// Sobel kernels (flattened)
|
|
54
53
|
const Gx = [-1, 0, 1, -2, 0, 2, -1, 0, 1];
|
|
55
54
|
const Gy = [-1, -2, -1, 0, 0, 0, 1, 2, 1];
|
|
56
|
-
// neighbor offsets around center pixel in lum space
|
|
57
|
-
const OFF = [-w - 1, -w, -w + 1, -1, 0, 1, w - 1, w, w + 1];
|
|
58
|
-
// Helpers
|
|
59
55
|
const clamp8 = (v) => (v < 0 ? 0 : v > 255 ? 255 : v);
|
|
60
|
-
//
|
|
61
|
-
for (let y =
|
|
62
|
-
for (let x =
|
|
56
|
+
// Extend the edge pixels so the same kernel applies at every position.
|
|
57
|
+
for (let y = 0; y < h; y++) {
|
|
58
|
+
for (let x = 0; x < w; x++) {
|
|
59
|
+
let sx = 0, sy = 0, k = 0;
|
|
60
|
+
for (let dy = -1; dy <= 1; dy++) {
|
|
61
|
+
const row = Math.max(0, Math.min(h - 1, y + dy)) * w;
|
|
62
|
+
for (let dx = -1; dx <= 1; dx++, k++) {
|
|
63
|
+
const sample = lum[row + Math.max(0, Math.min(w - 1, x + dx))];
|
|
64
|
+
sx += sample * Gx[k];
|
|
65
|
+
sy += sample * Gy[k];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
63
68
|
const p = y * w + x;
|
|
64
|
-
// Directional derivative = (cosθ * Gx + sinθ * Gy) • neighborhood(lum)
|
|
65
|
-
let sx = 0, sy = 0;
|
|
66
|
-
// unroll loop for speed
|
|
67
|
-
sx += lum[p + OFF[0]] * Gx[0];
|
|
68
|
-
sy += lum[p + OFF[0]] * Gy[0];
|
|
69
|
-
sx += lum[p + OFF[1]] * Gx[1];
|
|
70
|
-
sy += lum[p + OFF[1]] * Gy[1];
|
|
71
|
-
sx += lum[p + OFF[2]] * Gx[2];
|
|
72
|
-
sy += lum[p + OFF[2]] * Gy[2];
|
|
73
|
-
sx += lum[p + OFF[3]] * Gx[3];
|
|
74
|
-
sy += lum[p + OFF[3]] * Gy[3];
|
|
75
|
-
// center has 0 weights in both Sobel masks; can skip if desired
|
|
76
|
-
sx += lum[p + OFF[5]] * Gx[5];
|
|
77
|
-
sy += lum[p + OFF[5]] * Gy[5];
|
|
78
|
-
sx += lum[p + OFF[6]] * Gx[6];
|
|
79
|
-
sy += lum[p + OFF[6]] * Gy[6];
|
|
80
|
-
sx += lum[p + OFF[7]] * Gx[7];
|
|
81
|
-
sy += lum[p + OFF[7]] * Gy[7];
|
|
82
|
-
sx += lum[p + OFF[8]] * Gx[8];
|
|
83
|
-
sy += lum[p + OFF[8]] * Gy[8];
|
|
84
69
|
const r = cx * sx + cy * sy; // directional response
|
|
85
70
|
const outGray = clamp8(bias + r * SCALE); // biased, scaled, clamped
|
|
86
71
|
const o = p * 4;
|
|
87
72
|
if (blend) {
|
|
88
73
|
// Add the emboss "relief" around chosen bias to original RGB
|
|
89
74
|
const delta = outGray - bias; // symmetric around whiteLevel
|
|
90
|
-
data[o] = clamp8(
|
|
91
|
-
data[o + 1] = clamp8(
|
|
92
|
-
data[o + 2] = clamp8(
|
|
93
|
-
data[o + 3] = src[o + 3];
|
|
75
|
+
data[o] = clamp8(data[o] + delta);
|
|
76
|
+
data[o + 1] = clamp8(data[o + 1] + delta);
|
|
77
|
+
data[o + 2] = clamp8(data[o + 2] + delta);
|
|
94
78
|
}
|
|
95
79
|
else {
|
|
96
80
|
// Grayscale embossed output
|
|
97
81
|
data[o] = data[o + 1] = data[o + 2] = outGray;
|
|
98
|
-
data[o + 3] = src[o + 3];
|
|
99
82
|
}
|
|
100
83
|
}
|
|
101
84
|
}
|
|
102
|
-
// Copy border (untouched) to keep edges clean
|
|
103
|
-
// top & bottom rows
|
|
104
|
-
for (let x = 0; x < w; x++) {
|
|
105
|
-
let oTop = x * 4, oBot = ((h - 1) * w + x) * 4;
|
|
106
|
-
data[oTop] = src[oTop];
|
|
107
|
-
data[oTop + 1] = src[oTop + 1];
|
|
108
|
-
data[oTop + 2] = src[oTop + 2];
|
|
109
|
-
data[oTop + 3] = src[oTop + 3];
|
|
110
|
-
data[oBot] = src[oBot];
|
|
111
|
-
data[oBot + 1] = src[oBot + 1];
|
|
112
|
-
data[oBot + 2] = src[oBot + 2];
|
|
113
|
-
data[oBot + 3] = src[oBot + 3];
|
|
114
|
-
}
|
|
115
|
-
// left & right columns
|
|
116
|
-
for (let y = 1; y < h - 1; y++) {
|
|
117
|
-
let oL = y * w * 4, oR = (y * w + (w - 1)) * 4;
|
|
118
|
-
data[oL] = src[oL];
|
|
119
|
-
data[oL + 1] = src[oL + 1];
|
|
120
|
-
data[oL + 2] = src[oL + 2];
|
|
121
|
-
data[oL + 3] = src[oL + 3];
|
|
122
|
-
data[oR] = src[oR];
|
|
123
|
-
data[oR + 1] = src[oR + 1];
|
|
124
|
-
data[oR + 2] = src[oR + 2];
|
|
125
|
-
data[oR + 3] = src[oR + 3];
|
|
126
|
-
}
|
|
127
85
|
return imageData;
|
|
128
86
|
};
|
|
129
87
|
Factory.addGetterSetter(Node, 'embossStrength', 0.5, getNumberValidator(), Factory.afterSetFilter);
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Factory } from "../Factory.js";
|
|
2
2
|
import { Node } from "../Node.js";
|
|
3
|
-
import { Util } from "../Util.js";
|
|
4
3
|
import { getNumberValidator } from "../Validators.js";
|
|
5
4
|
/*
|
|
6
5
|
* ToPolar Filter. Converts image data to polar coordinates. Performs
|
|
@@ -25,7 +24,7 @@ const ToPolar = function (src, dst, opt) {
|
|
|
25
24
|
let y = ySize - yMid;
|
|
26
25
|
const rad = Math.sqrt(x * x + y * y);
|
|
27
26
|
rMax = rad > rMax ? rad : rMax;
|
|
28
|
-
// We'll be
|
|
27
|
+
// We'll be using y as the radius, and x as the angle (theta=t)
|
|
29
28
|
const rSize = ySize, tSize = xSize;
|
|
30
29
|
// We want to cover all angles (0-360) and we need to convert to
|
|
31
30
|
// radians (*PI/180)
|
|
@@ -106,9 +105,6 @@ const FromPolar = function (src, dst, opt) {
|
|
|
106
105
|
}
|
|
107
106
|
}
|
|
108
107
|
};
|
|
109
|
-
//Konva.Filters.ToPolar = Util._FilterWrapDoubleBuffer(ToPolar);
|
|
110
|
-
//Konva.Filters.FromPolar = Util._FilterWrapDoubleBuffer(FromPolar);
|
|
111
|
-
// create a temporary canvas for working - shared between multiple calls
|
|
112
108
|
/**
|
|
113
109
|
* Kaleidoscope Filter.
|
|
114
110
|
* @function
|
|
@@ -126,19 +122,16 @@ export const Kaleidoscope = function (imageData) {
|
|
|
126
122
|
let x, y, xoff, i, r, g, b, a, srcPos, dstPos;
|
|
127
123
|
let power = Math.round(this.kaleidoscopePower());
|
|
128
124
|
const angle = Math.round(this.kaleidoscopeAngle());
|
|
129
|
-
const offset = Math.floor((xSize * (angle % 360)) / 360);
|
|
125
|
+
const offset = Math.floor((xSize * (((angle % 360) + 360) % 360)) / 360);
|
|
130
126
|
if (power < 1) {
|
|
131
127
|
return;
|
|
132
128
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
.getImageData(0, 0, xSize, ySize);
|
|
140
|
-
Util.releaseCanvas(tempCanvas);
|
|
141
|
-
// Convert thhe original to polar coordinates
|
|
129
|
+
const scratchData = {
|
|
130
|
+
width: xSize,
|
|
131
|
+
height: ySize,
|
|
132
|
+
data: new Uint8ClampedArray(xSize * ySize * 4),
|
|
133
|
+
};
|
|
134
|
+
// Convert the original to polar coordinates.
|
|
142
135
|
ToPolar(imageData, scratchData, {
|
|
143
136
|
polarCenterX: xSize / 2,
|
|
144
137
|
polarCenterY: ySize / 2,
|
package/lib/filters/Mask.js
CHANGED
|
@@ -29,11 +29,10 @@ function backgroundMask(idata, threshold) {
|
|
|
29
29
|
const rgbv_ne = pixelAt(idata, idata.width - 1, 0);
|
|
30
30
|
const rgbv_so = pixelAt(idata, 0, idata.height - 1);
|
|
31
31
|
const rgbv_se = pixelAt(idata, idata.width - 1, idata.height - 1);
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
rgbDistance(
|
|
35
|
-
rgbDistance(
|
|
36
|
-
rgbDistance(rgbv_so, rgbv_no) < thres) {
|
|
32
|
+
if (rgbDistance(rgbv_no, rgbv_ne) < threshold &&
|
|
33
|
+
rgbDistance(rgbv_ne, rgbv_se) < threshold &&
|
|
34
|
+
rgbDistance(rgbv_se, rgbv_so) < threshold &&
|
|
35
|
+
rgbDistance(rgbv_so, rgbv_no) < threshold) {
|
|
37
36
|
// Mean color
|
|
38
37
|
const mean = rgbMean([rgbv_ne, rgbv_no, rgbv_se, rgbv_so]);
|
|
39
38
|
// Mask based on color distance
|
|
@@ -44,7 +43,7 @@ function backgroundMask(idata, threshold) {
|
|
|
44
43
|
idata.data[i * 4 + 1],
|
|
45
44
|
idata.data[i * 4 + 2],
|
|
46
45
|
]);
|
|
47
|
-
mask[i] = d <
|
|
46
|
+
mask[i] = d < threshold ? 0 : 255;
|
|
48
47
|
}
|
|
49
48
|
return mask;
|
|
50
49
|
}
|
|
@@ -151,8 +150,9 @@ function smoothEdgeMask(mask, sw, sh) {
|
|
|
151
150
|
* node.threshold(200);
|
|
152
151
|
*/
|
|
153
152
|
export const Mask = function (imageData) {
|
|
153
|
+
var _a;
|
|
154
154
|
// Detect pixels close to the background color
|
|
155
|
-
const threshold = this.threshold
|
|
155
|
+
const threshold = (_a = this.attrs.threshold) !== null && _a !== void 0 ? _a : 10;
|
|
156
156
|
let mask = backgroundMask(imageData, threshold);
|
|
157
157
|
if (mask) {
|
|
158
158
|
// Erode
|
|
@@ -166,4 +166,4 @@ export const Mask = function (imageData) {
|
|
|
166
166
|
}
|
|
167
167
|
return imageData;
|
|
168
168
|
};
|
|
169
|
-
Factory.addGetterSetter(Node, 'threshold', 0, getNumberValidator(), Factory.afterSetFilter);
|
|
169
|
+
Factory.addGetterSetter(Node, 'threshold', 0.5, getNumberValidator(), Factory.afterSetFilter);
|
package/lib/filters/Pixelate.js
CHANGED
|
@@ -15,10 +15,8 @@ import { getNumberValidator } from "../Validators.js";
|
|
|
15
15
|
* node.filters([Konva.Filters.Pixelate]);
|
|
16
16
|
* node.pixelSize(10);
|
|
17
17
|
*/
|
|
18
|
-
export const Pixelate = function (imageData) {
|
|
19
|
-
let pixelSize = Math.ceil(this.pixelSize()), width = imageData.width, height = imageData.height,
|
|
20
|
-
//pixelsPerBin = pixelSize * pixelSize,
|
|
21
|
-
nBinsX = Math.ceil(width / pixelSize), nBinsY = Math.ceil(height / pixelSize), data = imageData.data;
|
|
18
|
+
export const Pixelate = function (imageData, pixelRatio = 1) {
|
|
19
|
+
let pixelSize = Math.ceil(this.pixelSize() * pixelRatio), width = imageData.width, height = imageData.height, nBinsX = Math.ceil(width / pixelSize), nBinsY = Math.ceil(height / pixelSize), data = imageData.data;
|
|
22
20
|
if (pixelSize <= 0) {
|
|
23
21
|
Util.error('pixelSize value can not be <= 0');
|
|
24
22
|
return;
|
|
@@ -32,41 +30,30 @@ export const Pixelate = function (imageData) {
|
|
|
32
30
|
let alpha = 0;
|
|
33
31
|
// Determine which pixels are included in this bin
|
|
34
32
|
const xBinStart = xBin * pixelSize;
|
|
35
|
-
const xBinEnd = xBinStart + pixelSize;
|
|
33
|
+
const xBinEnd = Math.min(xBinStart + pixelSize, width);
|
|
36
34
|
const yBinStart = yBin * pixelSize;
|
|
37
|
-
const yBinEnd = yBinStart + pixelSize;
|
|
35
|
+
const yBinEnd = Math.min(yBinStart + pixelSize, height);
|
|
38
36
|
// Add all of the pixels to this bin!
|
|
39
|
-
let pixelsInBin = 0;
|
|
40
37
|
for (let x = xBinStart; x < xBinEnd; x += 1) {
|
|
41
|
-
if (x >= width) {
|
|
42
|
-
continue;
|
|
43
|
-
}
|
|
44
38
|
for (let y = yBinStart; y < yBinEnd; y += 1) {
|
|
45
|
-
if (y >= height) {
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
39
|
const i = (width * y + x) * 4;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
40
|
+
const a = data[i + 3];
|
|
41
|
+
red += data[i + 0] * a;
|
|
42
|
+
green += data[i + 1] * a;
|
|
43
|
+
blue += data[i + 2] * a;
|
|
44
|
+
alpha += a;
|
|
54
45
|
}
|
|
55
46
|
}
|
|
56
|
-
//
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
47
|
+
// average premultiplied colour so transparent pixels do not darken the block
|
|
48
|
+
if (alpha) {
|
|
49
|
+
red = red / alpha;
|
|
50
|
+
green = green / alpha;
|
|
51
|
+
blue = blue / alpha;
|
|
52
|
+
}
|
|
53
|
+
alpha = alpha / ((xBinEnd - xBinStart) * (yBinEnd - yBinStart));
|
|
61
54
|
// Draw this bin
|
|
62
55
|
for (let x = xBinStart; x < xBinEnd; x += 1) {
|
|
63
|
-
if (x >= width) {
|
|
64
|
-
continue;
|
|
65
|
-
}
|
|
66
56
|
for (let y = yBinStart; y < yBinEnd; y += 1) {
|
|
67
|
-
if (y >= height) {
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
57
|
const i = (width * y + x) * 4;
|
|
71
58
|
data[i + 0] = red;
|
|
72
59
|
data[i + 1] = green;
|
|
@@ -15,7 +15,7 @@ import type { Filter } from '../Node.ts';
|
|
|
15
15
|
*/
|
|
16
16
|
export declare const Threshold: Filter;
|
|
17
17
|
/**
|
|
18
|
-
* get/set threshold. With {@link Konva.Filters.Threshold} it is a value between 0 and 1. With {@link Konva.Filters.Mask} it is the colour distance to the background, 0 to 255, default 10.
|
|
18
|
+
* get/set threshold. With {@link Konva.Filters.Threshold} it is a value between 0 and 1, default 0.5. With {@link Konva.Filters.Mask} it is the colour distance to the background, 0 to 255, default 10.
|
|
19
19
|
* @name threshold
|
|
20
20
|
* @method
|
|
21
21
|
* @memberof Konva.Node.prototype
|
package/lib/filters/Threshold.js
CHANGED
|
@@ -23,7 +23,7 @@ export const Threshold = function (imageData) {
|
|
|
23
23
|
};
|
|
24
24
|
Factory.addGetterSetter(Node, 'threshold', 0.5, getNumberValidator(), Factory.afterSetFilter);
|
|
25
25
|
/**
|
|
26
|
-
* get/set threshold. With {@link Konva.Filters.Threshold} it is a value between 0 and 1. With {@link Konva.Filters.Mask} it is the colour distance to the background, 0 to 255, default 10.
|
|
26
|
+
* get/set threshold. With {@link Konva.Filters.Threshold} it is a value between 0 and 1, default 0.5. With {@link Konva.Filters.Mask} it is the colour distance to the background, 0 to 255, default 10.
|
|
27
27
|
* @name threshold
|
|
28
28
|
* @method
|
|
29
29
|
* @memberof Konva.Node.prototype
|
package/lib/shapes/Arc.js
CHANGED
|
@@ -53,8 +53,8 @@ export class Arc extends Shape {
|
|
|
53
53
|
const outerRadius = Math.abs(this.outerRadius());
|
|
54
54
|
const clockwise = this.clockwise();
|
|
55
55
|
// canvas draws a full circle for any non-zero full-turn end angle regardless
|
|
56
|
-
// of direction, and nothing for angle 0.
|
|
57
|
-
//
|
|
56
|
+
// of direction, and nothing for angle 0. Keep exact turns distinct from
|
|
57
|
+
// the modulo-normalized partial sweep below.
|
|
58
58
|
const rawAngle = Konva.getAngle(this.angle());
|
|
59
59
|
if (rawAngle % (Math.PI * 2) === 0) {
|
|
60
60
|
return rawAngle !== 0
|
|
@@ -66,7 +66,9 @@ export class Arc extends Shape {
|
|
|
66
66
|
}
|
|
67
67
|
: { x: innerRadius, y: 0, width: outerRadius - innerRadius, height: 0 };
|
|
68
68
|
}
|
|
69
|
-
const
|
|
69
|
+
const turn = Math.PI * 2;
|
|
70
|
+
const sweep = clockwise ? -rawAngle : rawAngle;
|
|
71
|
+
const angle = sweep >= turn ? turn : ((sweep % turn) + turn) % turn;
|
|
70
72
|
const boundLeftRatio = Math.cos(Math.min(angle, Math.PI));
|
|
71
73
|
const boundRightRatio = 1;
|
|
72
74
|
const boundTopRatio = Math.sin(Math.min(Math.max(Math.PI, angle), (3 * Math.PI) / 2));
|
package/lib/shapes/Arrow.d.ts
CHANGED
|
@@ -36,13 +36,9 @@ export interface ArrowConfig extends LineConfig {
|
|
|
36
36
|
*/
|
|
37
37
|
export declare class Arrow extends Line<ArrowConfig> {
|
|
38
38
|
_sceneFunc(ctx: Context): void;
|
|
39
|
+
_getPointerAngle(atBeginning: boolean): number;
|
|
39
40
|
__fillStroke(ctx: Context): void;
|
|
40
|
-
getSelfRect():
|
|
41
|
-
x: number;
|
|
42
|
-
y: number;
|
|
43
|
-
width: number;
|
|
44
|
-
height: number;
|
|
45
|
-
};
|
|
41
|
+
getSelfRect(): import("../types.ts").IRect;
|
|
46
42
|
pointerLength: GetSet<number, this>;
|
|
47
43
|
pointerWidth: GetSet<number, this>;
|
|
48
44
|
pointerAtEnding: GetSet<boolean, this>;
|
package/lib/shapes/Arrow.js
CHANGED
|
@@ -30,41 +30,17 @@ import { Path } from "./Path.js";
|
|
|
30
30
|
export class Arrow extends Line {
|
|
31
31
|
_sceneFunc(ctx) {
|
|
32
32
|
super._sceneFunc(ctx);
|
|
33
|
-
const PI2 = Math.PI * 2;
|
|
34
33
|
const points = this.points();
|
|
35
|
-
let tp = points;
|
|
36
|
-
const fromTension = this.tension() !== 0 && points.length > 4;
|
|
37
|
-
if (fromTension) {
|
|
38
|
-
tp = this.getTensionPoints();
|
|
39
|
-
}
|
|
40
|
-
const length = this.pointerLength();
|
|
41
34
|
const n = points.length;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
tp[tp.length - 4],
|
|
46
|
-
tp[tp.length - 3],
|
|
47
|
-
tp[tp.length - 2],
|
|
48
|
-
tp[tp.length - 1],
|
|
49
|
-
points[n - 2],
|
|
50
|
-
points[n - 1],
|
|
51
|
-
];
|
|
52
|
-
const lastLength = Path.calcLength(tp[tp.length - 4], tp[tp.length - 3], 'C', lp);
|
|
53
|
-
const previous = Path.getPointOnQuadraticBezier(Math.min(1, 1 - length / lastLength), lp[0], lp[1], lp[2], lp[3], lp[4], lp[5]);
|
|
54
|
-
dx = points[n - 2] - previous.x;
|
|
55
|
-
dy = points[n - 1] - previous.y;
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
dx = points[n - 2] - points[n - 4];
|
|
59
|
-
dy = points[n - 1] - points[n - 3];
|
|
60
|
-
}
|
|
61
|
-
const radians = (Math.atan2(dy, dx) + PI2) % PI2;
|
|
35
|
+
if (n < 4)
|
|
36
|
+
return;
|
|
37
|
+
const length = this.pointerLength();
|
|
62
38
|
const width = this.pointerWidth();
|
|
63
39
|
if (this.pointerAtEnding()) {
|
|
64
40
|
ctx.save();
|
|
65
41
|
ctx.beginPath();
|
|
66
42
|
ctx.translate(points[n - 2], points[n - 1]);
|
|
67
|
-
ctx.rotate(
|
|
43
|
+
ctx.rotate(this._getPointerAngle(false));
|
|
68
44
|
ctx.moveTo(0, 0);
|
|
69
45
|
ctx.lineTo(-length, width / 2);
|
|
70
46
|
ctx.lineTo(-length, -width / 2);
|
|
@@ -76,15 +52,7 @@ export class Arrow extends Line {
|
|
|
76
52
|
ctx.save();
|
|
77
53
|
ctx.beginPath();
|
|
78
54
|
ctx.translate(points[0], points[1]);
|
|
79
|
-
|
|
80
|
-
dx = (tp[0] + tp[2]) / 2 - points[0];
|
|
81
|
-
dy = (tp[1] + tp[3]) / 2 - points[1];
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
dx = points[2] - points[0];
|
|
85
|
-
dy = points[3] - points[1];
|
|
86
|
-
}
|
|
87
|
-
ctx.rotate((Math.atan2(-dy, -dx) + PI2) % PI2);
|
|
55
|
+
ctx.rotate(this._getPointerAngle(true));
|
|
88
56
|
ctx.moveTo(0, 0);
|
|
89
57
|
ctx.lineTo(-length, width / 2);
|
|
90
58
|
ctx.lineTo(-length, -width / 2);
|
|
@@ -93,6 +61,39 @@ export class Arrow extends Line {
|
|
|
93
61
|
this.__fillStroke(ctx);
|
|
94
62
|
}
|
|
95
63
|
}
|
|
64
|
+
_getPointerAngle(atBeginning) {
|
|
65
|
+
const points = this.points();
|
|
66
|
+
const n = points.length;
|
|
67
|
+
const fromTension = this.tension() !== 0 && n > 4;
|
|
68
|
+
const tp = fromTension ? this.getTensionPoints() : points;
|
|
69
|
+
const ex = atBeginning ? points[0] : points[n - 2], ey = atBeginning ? points[1] : points[n - 1];
|
|
70
|
+
let dx = 0, dy = 0;
|
|
71
|
+
if (fromTension && atBeginning) {
|
|
72
|
+
dx = ex - (tp[0] + tp[2]) / 2;
|
|
73
|
+
dy = ey - (tp[1] + tp[3]) / 2;
|
|
74
|
+
}
|
|
75
|
+
else if (fromTension) {
|
|
76
|
+
const x = tp[tp.length - 4], y = tp[tp.length - 3];
|
|
77
|
+
const controlX = tp[tp.length - 2], controlY = tp[tp.length - 1];
|
|
78
|
+
const lastLength = Path.calcLength(x, y, 'Q', [
|
|
79
|
+
controlX,
|
|
80
|
+
controlY,
|
|
81
|
+
ex,
|
|
82
|
+
ey,
|
|
83
|
+
]);
|
|
84
|
+
const previous = Path.getPointOnQuadraticBezier(lastLength ? Math.max(0, 1 - this.pointerLength() / lastLength) : 0, x, y, controlX, controlY, ex, ey);
|
|
85
|
+
dx = ex - previous.x;
|
|
86
|
+
dy = ey - previous.y;
|
|
87
|
+
}
|
|
88
|
+
// repeated endpoints give a zero tangent, so walk towards the other end
|
|
89
|
+
// of the line until a distinct point is found
|
|
90
|
+
for (let i = atBeginning ? 0 : tp.length - 2; !dx && !dy && i >= 0 && i < tp.length; i += atBeginning ? 2 : -2) {
|
|
91
|
+
dx = ex - tp[i];
|
|
92
|
+
dy = ey - tp[i + 1];
|
|
93
|
+
}
|
|
94
|
+
const turn = Math.PI * 2;
|
|
95
|
+
return (Math.atan2(dy, dx) + turn) % turn;
|
|
96
|
+
}
|
|
96
97
|
__fillStroke(ctx) {
|
|
97
98
|
// here is a tricky part
|
|
98
99
|
// we need to disable dash for arrow pointers
|
|
@@ -112,16 +113,37 @@ export class Arrow extends Line {
|
|
|
112
113
|
}
|
|
113
114
|
getSelfRect() {
|
|
114
115
|
const lineRect = super.getSelfRect();
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
116
|
+
const points = this.points();
|
|
117
|
+
if (points.length < 4)
|
|
118
|
+
return lineRect;
|
|
119
|
+
let minX = lineRect.x, minY = lineRect.y;
|
|
120
|
+
let maxX = minX + lineRect.width, maxY = minY + lineRect.height;
|
|
121
|
+
for (const beginning of [false, true]) {
|
|
122
|
+
if (!(beginning ? this.pointerAtBeginning() : this.pointerAtEnding()))
|
|
123
|
+
continue;
|
|
124
|
+
const index = beginning ? 0 : points.length - 2;
|
|
125
|
+
const angle = this._getPointerAngle(beginning);
|
|
126
|
+
const cos = Math.cos(angle), sin = Math.sin(angle);
|
|
127
|
+
const x = points[index] - this.pointerLength() * cos;
|
|
128
|
+
const y = points[index + 1] - this.pointerLength() * sin;
|
|
129
|
+
const dx = Math.abs((this.pointerWidth() * sin) / 2);
|
|
130
|
+
const dy = Math.abs((this.pointerWidth() * cos) / 2);
|
|
131
|
+
minX = Math.min(minX, x - dx);
|
|
132
|
+
minY = Math.min(minY, y - dy);
|
|
133
|
+
maxX = Math.max(maxX, x + dx);
|
|
134
|
+
maxY = Math.max(maxY, y + dy);
|
|
135
|
+
}
|
|
136
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
122
137
|
}
|
|
123
138
|
}
|
|
124
139
|
Arrow.prototype.className = 'Arrow';
|
|
140
|
+
Arrow.prototype._attrsAffectingSize = [
|
|
141
|
+
...Line.prototype._attrsAffectingSize,
|
|
142
|
+
'pointerLength',
|
|
143
|
+
'pointerWidth',
|
|
144
|
+
'pointerAtBeginning',
|
|
145
|
+
'pointerAtEnding',
|
|
146
|
+
];
|
|
125
147
|
_registerNode(Arrow);
|
|
126
148
|
/**
|
|
127
149
|
* get/set pointerLength
|
|
@@ -156,7 +178,7 @@ Factory.addGetterSetter(Arrow, 'pointerWidth', 10, getNumberValidator());
|
|
|
156
178
|
* get/set pointerAtBeginning
|
|
157
179
|
* @name Konva.Arrow#pointerAtBeginning
|
|
158
180
|
* @method
|
|
159
|
-
* @param {
|
|
181
|
+
* @param {Boolean} pointerAtBeginning Whether to draw a pointer at the beginning of the arrow. The default is false.
|
|
160
182
|
* @returns {Boolean}
|
|
161
183
|
* @example
|
|
162
184
|
* // get value
|
|
@@ -170,7 +192,7 @@ Factory.addGetterSetter(Arrow, 'pointerAtBeginning', false);
|
|
|
170
192
|
* get/set pointerAtEnding
|
|
171
193
|
* @name Konva.Arrow#pointerAtEnding
|
|
172
194
|
* @method
|
|
173
|
-
* @param {
|
|
195
|
+
* @param {Boolean} pointerAtEnding Whether to draw a pointer at the end of the arrow. The default is true.
|
|
174
196
|
* @returns {Boolean}
|
|
175
197
|
* @example
|
|
176
198
|
* // get value
|
package/lib/shapes/Image.js
CHANGED
|
@@ -39,23 +39,18 @@ export class Image extends Shape {
|
|
|
39
39
|
this._setImageLoad();
|
|
40
40
|
}
|
|
41
41
|
_setImageLoad() {
|
|
42
|
+
var _a;
|
|
42
43
|
const image = this.image();
|
|
43
|
-
//
|
|
44
|
-
|
|
44
|
+
// A fresh `new Image()` with no src already reports complete === true,
|
|
45
|
+
// so it still needs the listener.
|
|
46
|
+
if (((image === null || image === void 0 ? void 0 : image.complete) && image.src) || (image === null || image === void 0 ? void 0 : image.readyState) >= 2) {
|
|
45
47
|
return;
|
|
46
48
|
}
|
|
47
|
-
|
|
48
|
-
if (image && image.readyState === 4) {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
if (image && image['addEventListener']) {
|
|
52
|
-
image['addEventListener']('load', this._loadListener);
|
|
53
|
-
}
|
|
49
|
+
(_a = image === null || image === void 0 ? void 0 : image.addEventListener) === null || _a === void 0 ? void 0 : _a.call(image, 'videoWidth' in image ? 'loadeddata' : 'load', this._loadListener);
|
|
54
50
|
}
|
|
55
51
|
_removeImageLoad(image) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
52
|
+
var _a;
|
|
53
|
+
(_a = image === null || image === void 0 ? void 0 : image.removeEventListener) === null || _a === void 0 ? void 0 : _a.call(image, 'videoWidth' in image ? 'loadeddata' : 'load', this._loadListener);
|
|
59
54
|
}
|
|
60
55
|
destroy() {
|
|
61
56
|
this._removeImageLoad(this.image());
|
|
@@ -63,6 +58,8 @@ export class Image extends Shape {
|
|
|
63
58
|
return this;
|
|
64
59
|
}
|
|
65
60
|
_useBufferCanvas() {
|
|
61
|
+
if (this.attrs.perfectDrawEnabled === false)
|
|
62
|
+
return false;
|
|
66
63
|
const hasCornerRadius = !!this.cornerRadius();
|
|
67
64
|
const hasShadow = this.hasShadow();
|
|
68
65
|
if (hasCornerRadius && hasShadow) {
|
|
@@ -112,11 +109,13 @@ export class Image extends Shape {
|
|
|
112
109
|
}
|
|
113
110
|
getWidth() {
|
|
114
111
|
var _a, _b, _c;
|
|
115
|
-
|
|
112
|
+
const image = this.image();
|
|
113
|
+
return (_c = (_b = (_a = this.attrs.width) !== null && _a !== void 0 ? _a : image === null || image === void 0 ? void 0 : image.videoWidth) !== null && _b !== void 0 ? _b : image === null || image === void 0 ? void 0 : image.width) !== null && _c !== void 0 ? _c : 0;
|
|
116
114
|
}
|
|
117
115
|
getHeight() {
|
|
118
116
|
var _a, _b, _c;
|
|
119
|
-
|
|
117
|
+
const image = this.image();
|
|
118
|
+
return (_c = (_b = (_a = this.attrs.height) !== null && _a !== void 0 ? _a : image === null || image === void 0 ? void 0 : image.videoHeight) !== null && _b !== void 0 ? _b : image === null || image === void 0 ? void 0 : image.height) !== null && _c !== void 0 ? _c : 0;
|
|
120
119
|
}
|
|
121
120
|
/**
|
|
122
121
|
* load image from given url and create `Konva.Image` instance
|
|
@@ -133,11 +132,9 @@ export class Image extends Shape {
|
|
|
133
132
|
*/
|
|
134
133
|
static fromURL(url, callback, onError = null) {
|
|
135
134
|
const img = Util.createImageElement();
|
|
136
|
-
img.onload =
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
});
|
|
140
|
-
callback(image);
|
|
135
|
+
img.onload = () => {
|
|
136
|
+
img.onload = img.onerror = null;
|
|
137
|
+
callback(new Image({ image: img }));
|
|
141
138
|
};
|
|
142
139
|
img.onerror = onError;
|
|
143
140
|
img.crossOrigin = 'Anonymous';
|
package/lib/shapes/Label.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { Group } from '../Group.ts';
|
|
|
4
4
|
import type { Context } from '../Context.ts';
|
|
5
5
|
import type { ContainerConfig } from '../Container.ts';
|
|
6
6
|
import type { GetSet } from '../types.ts';
|
|
7
|
-
import
|
|
7
|
+
import { Text } from './Text.ts';
|
|
8
8
|
export interface LabelConfig extends ContainerConfig {
|
|
9
9
|
}
|
|
10
10
|
declare const NONE = "none";
|
|
@@ -28,7 +28,7 @@ declare const NONE = "none";
|
|
|
28
28
|
* stroke: '#333',
|
|
29
29
|
* shadowColor: 'black',
|
|
30
30
|
* shadowBlur: 10,
|
|
31
|
-
* shadowOffset:
|
|
31
|
+
* shadowOffset: { x: 10, y: 10 },
|
|
32
32
|
* shadowOpacity: 0.2,
|
|
33
33
|
* lineJoin: 'round',
|
|
34
34
|
* pointerDirection: 'up',
|
|
@@ -64,7 +64,6 @@ export declare class Label extends Group {
|
|
|
64
64
|
* @method
|
|
65
65
|
*/
|
|
66
66
|
getTag(): Tag;
|
|
67
|
-
_addListeners(text: any): void;
|
|
68
67
|
getWidth(): number;
|
|
69
68
|
getHeight(): number;
|
|
70
69
|
_sync(): void;
|
package/lib/shapes/Label.js
CHANGED
|
@@ -4,22 +4,16 @@ import { Shape } from "../Shape.js";
|
|
|
4
4
|
import { Group } from "../Group.js";
|
|
5
5
|
import { getNumberOrArrayOfNumbersValidator, getNumberValidator, } from "../Validators.js";
|
|
6
6
|
import { _registerNode } from "../Global.js";
|
|
7
|
+
import { Text } from "./Text.js";
|
|
7
8
|
// constants
|
|
8
9
|
const ATTR_CHANGE_LIST = [
|
|
9
|
-
|
|
10
|
-
'fontSize',
|
|
11
|
-
'fontStyle',
|
|
12
|
-
'padding',
|
|
13
|
-
'lineHeight',
|
|
14
|
-
'text',
|
|
10
|
+
...Text.prototype._attrsAffectingSize,
|
|
15
11
|
'width',
|
|
16
12
|
'height',
|
|
17
13
|
'pointerDirection',
|
|
18
14
|
'pointerWidth',
|
|
19
15
|
'pointerHeight',
|
|
20
|
-
],
|
|
21
|
-
// cached variables
|
|
22
|
-
attrChangeListLen = ATTR_CHANGE_LIST.length;
|
|
16
|
+
], CHANGE_EVENTS = ATTR_CHANGE_LIST.map((attr) => attr + 'Change.konva').join(' '), NONE = 'none', UP = 'up', RIGHT = 'right', DOWN = 'down', LEFT = 'left';
|
|
23
17
|
/**
|
|
24
18
|
* Label constructor. Labels are groups that contain a Text and Tag shape
|
|
25
19
|
* @constructor
|
|
@@ -40,7 +34,7 @@ attrChangeListLen = ATTR_CHANGE_LIST.length;
|
|
|
40
34
|
* stroke: '#333',
|
|
41
35
|
* shadowColor: 'black',
|
|
42
36
|
* shadowBlur: 10,
|
|
43
|
-
* shadowOffset:
|
|
37
|
+
* shadowOffset: { x: 10, y: 10 },
|
|
44
38
|
* shadowOpacity: 0.2,
|
|
45
39
|
* lineJoin: 'round',
|
|
46
40
|
* pointerDirection: 'up',
|
|
@@ -61,8 +55,10 @@ attrChangeListLen = ATTR_CHANGE_LIST.length;
|
|
|
61
55
|
export class Label extends Group {
|
|
62
56
|
constructor(config) {
|
|
63
57
|
super(config);
|
|
58
|
+
this._sync = this._sync.bind(this);
|
|
64
59
|
this.on('add.konva', function (evt) {
|
|
65
|
-
|
|
60
|
+
// re-adding the same child must not stack another listener
|
|
61
|
+
evt.child.off(CHANGE_EVENTS, this._sync).on(CHANGE_EVENTS, this._sync);
|
|
66
62
|
this._sync();
|
|
67
63
|
});
|
|
68
64
|
}
|
|
@@ -86,16 +82,6 @@ export class Label extends Group {
|
|
|
86
82
|
getTag() {
|
|
87
83
|
return this.find('Tag')[0];
|
|
88
84
|
}
|
|
89
|
-
_addListeners(text) {
|
|
90
|
-
let that = this, n;
|
|
91
|
-
const func = function () {
|
|
92
|
-
that._sync();
|
|
93
|
-
};
|
|
94
|
-
// update text data for certain attr changes
|
|
95
|
-
for (n = 0; n < attrChangeListLen; n++) {
|
|
96
|
-
text.on(ATTR_CHANGE_LIST[n] + CHANGE_KONVA, func);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
85
|
getWidth() {
|
|
100
86
|
return this.getText().width();
|
|
101
87
|
}
|
package/lib/shapes/Line.d.ts
CHANGED
|
@@ -53,7 +53,7 @@ export declare class Line<Config extends LineConfig = LineConfig> extends Shape<
|
|
|
53
53
|
closed: GetSet<boolean, this>;
|
|
54
54
|
bezier: GetSet<boolean, this>;
|
|
55
55
|
tension: GetSet<number, this>;
|
|
56
|
-
points: GetSet<number[], this, LinePoints>;
|
|
56
|
+
points: GetSet<number[], this, LinePoints | null | undefined>;
|
|
57
57
|
}
|
|
58
58
|
/**
|
|
59
59
|
* get/set points array. Points is a flat array [x1, y1, x2, y2]. It is flat for performance reasons.
|