konva 10.5.0 → 10.7.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 +16 -12
- package/konva.js +3623 -3819
- 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 +10 -3
- package/lib/Canvas.js +91 -10
- package/lib/Container.d.ts +3 -2
- package/lib/Container.js +31 -17
- package/lib/Context.d.ts +9 -7
- package/lib/Context.js +99 -73
- package/lib/DragAndDrop.d.ts +4 -3
- package/lib/DragAndDrop.js +39 -25
- package/lib/Factory.d.ts +3 -2
- package/lib/Factory.js +10 -9
- package/lib/Global.d.ts +2 -7
- package/lib/Global.js +8 -9
- package/lib/Group.d.ts +7 -1
- package/lib/Group.js +61 -0
- package/lib/Layer.d.ts +1 -1
- package/lib/Layer.js +13 -8
- package/lib/Node.d.ts +25 -19
- package/lib/Node.js +178 -169
- package/lib/PointerEvents.d.ts +1 -2
- package/lib/PointerEvents.js +20 -18
- package/lib/Shape.d.ts +9 -15
- package/lib/Shape.js +190 -86
- package/lib/Stage.d.ts +19 -15
- package/lib/Stage.js +184 -99
- package/lib/Tween.js +59 -66
- package/lib/Util.d.ts +3 -2
- package/lib/Util.js +46 -16
- package/lib/_CoreInternals.d.ts +23 -11
- package/lib/_FullInternals.d.ts +23 -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 +8 -6
- package/lib/shapes/Arrow.d.ts +3 -6
- package/lib/shapes/Arrow.js +75 -47
- package/lib/shapes/Circle.d.ts +1 -0
- package/lib/shapes/Circle.js +4 -1
- package/lib/shapes/Ellipse.d.ts +1 -0
- package/lib/shapes/Ellipse.js +4 -1
- package/lib/shapes/Image.d.ts +2 -1
- package/lib/shapes/Image.js +22 -22
- package/lib/shapes/Label.d.ts +2 -3
- package/lib/shapes/Label.js +25 -41
- package/lib/shapes/Line.d.ts +2 -1
- package/lib/shapes/Line.js +5 -2
- package/lib/shapes/Path.d.ts +11 -2
- package/lib/shapes/Path.js +133 -143
- package/lib/shapes/Rect.d.ts +1 -0
- package/lib/shapes/Rect.js +4 -1
- package/lib/shapes/RegularPolygon.js +2 -2
- package/lib/shapes/Ring.d.ts +7 -0
- package/lib/shapes/Ring.js +8 -1
- package/lib/shapes/Sprite.d.ts +2 -1
- package/lib/shapes/Sprite.js +8 -9
- package/lib/shapes/Star.d.ts +6 -0
- package/lib/shapes/Star.js +5 -1
- package/lib/shapes/Text.d.ts +16 -5
- package/lib/shapes/Text.js +134 -65
- package/lib/shapes/TextPath.d.ts +11 -3
- package/lib/shapes/TextPath.js +56 -27
- package/lib/shapes/Transformer.js +95 -96
- package/lib/shapes/Wedge.js +1 -1
- package/lib/types.d.ts +2 -2
- package/package.json +5 -20
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
|
@@ -49,12 +49,12 @@ export class Arc extends Shape {
|
|
|
49
49
|
this.outerRadius(height / 2);
|
|
50
50
|
}
|
|
51
51
|
getSelfRect() {
|
|
52
|
-
const
|
|
53
|
-
const
|
|
52
|
+
const r1 = Math.abs(this.innerRadius()), r2 = Math.abs(this.outerRadius());
|
|
53
|
+
const innerRadius = Math.min(r1, r2), outerRadius = Math.max(r1, r2);
|
|
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));
|
|
@@ -91,7 +93,7 @@ Arc.prototype._attrsAffectingSize = [
|
|
|
91
93
|
'angle',
|
|
92
94
|
'clockwise',
|
|
93
95
|
];
|
|
94
|
-
_registerNode(Arc);
|
|
96
|
+
_registerNode(Arc, true);
|
|
95
97
|
// add getters setters
|
|
96
98
|
Factory.addGetterSetter(Arc, 'innerRadius', 0, getNumberValidator());
|
|
97
99
|
/**
|
package/lib/shapes/Arrow.d.ts
CHANGED
|
@@ -35,14 +35,11 @@ export interface ArrowConfig extends LineConfig {
|
|
|
35
35
|
* });
|
|
36
36
|
*/
|
|
37
37
|
export declare class Arrow extends Line<ArrowConfig> {
|
|
38
|
+
_getStrokePadding(): number;
|
|
38
39
|
_sceneFunc(ctx: Context): void;
|
|
40
|
+
_getPointerAngle(atBeginning: boolean): number;
|
|
39
41
|
__fillStroke(ctx: Context): void;
|
|
40
|
-
getSelfRect():
|
|
41
|
-
x: number;
|
|
42
|
-
y: number;
|
|
43
|
-
width: number;
|
|
44
|
-
height: number;
|
|
45
|
-
};
|
|
42
|
+
getSelfRect(): import("../types.ts").IRect;
|
|
46
43
|
pointerLength: GetSet<number, this>;
|
|
47
44
|
pointerWidth: GetSet<number, this>;
|
|
48
45
|
pointerAtEnding: GetSet<boolean, this>;
|
package/lib/shapes/Arrow.js
CHANGED
|
@@ -28,43 +28,25 @@ import { Path } from "./Path.js";
|
|
|
28
28
|
* });
|
|
29
29
|
*/
|
|
30
30
|
export class Arrow extends Line {
|
|
31
|
+
_getStrokePadding() {
|
|
32
|
+
// Arrowheads have joins even when the shaft is a single straight line.
|
|
33
|
+
return super._getStrokePadding(this.pointerAtBeginning() || this.pointerAtEnding()
|
|
34
|
+
? this.miterLimit() || 10
|
|
35
|
+
: undefined);
|
|
36
|
+
}
|
|
31
37
|
_sceneFunc(ctx) {
|
|
32
38
|
super._sceneFunc(ctx);
|
|
33
|
-
const PI2 = Math.PI * 2;
|
|
34
39
|
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
40
|
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;
|
|
41
|
+
if (n < 4)
|
|
42
|
+
return;
|
|
43
|
+
const length = this.pointerLength();
|
|
62
44
|
const width = this.pointerWidth();
|
|
63
45
|
if (this.pointerAtEnding()) {
|
|
64
46
|
ctx.save();
|
|
65
47
|
ctx.beginPath();
|
|
66
48
|
ctx.translate(points[n - 2], points[n - 1]);
|
|
67
|
-
ctx.rotate(
|
|
49
|
+
ctx.rotate(this._getPointerAngle(false));
|
|
68
50
|
ctx.moveTo(0, 0);
|
|
69
51
|
ctx.lineTo(-length, width / 2);
|
|
70
52
|
ctx.lineTo(-length, -width / 2);
|
|
@@ -76,15 +58,7 @@ export class Arrow extends Line {
|
|
|
76
58
|
ctx.save();
|
|
77
59
|
ctx.beginPath();
|
|
78
60
|
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);
|
|
61
|
+
ctx.rotate(this._getPointerAngle(true));
|
|
88
62
|
ctx.moveTo(0, 0);
|
|
89
63
|
ctx.lineTo(-length, width / 2);
|
|
90
64
|
ctx.lineTo(-length, -width / 2);
|
|
@@ -93,6 +67,39 @@ export class Arrow extends Line {
|
|
|
93
67
|
this.__fillStroke(ctx);
|
|
94
68
|
}
|
|
95
69
|
}
|
|
70
|
+
_getPointerAngle(atBeginning) {
|
|
71
|
+
const points = this.points();
|
|
72
|
+
const n = points.length;
|
|
73
|
+
const fromTension = this.tension() !== 0 && n > 4;
|
|
74
|
+
const tp = fromTension ? this.getTensionPoints() : points;
|
|
75
|
+
const ex = atBeginning ? points[0] : points[n - 2], ey = atBeginning ? points[1] : points[n - 1];
|
|
76
|
+
let dx = 0, dy = 0;
|
|
77
|
+
if (fromTension && atBeginning) {
|
|
78
|
+
dx = ex - (tp[0] + tp[2]) / 2;
|
|
79
|
+
dy = ey - (tp[1] + tp[3]) / 2;
|
|
80
|
+
}
|
|
81
|
+
else if (fromTension) {
|
|
82
|
+
const x = tp[tp.length - 4], y = tp[tp.length - 3];
|
|
83
|
+
const controlX = tp[tp.length - 2], controlY = tp[tp.length - 1];
|
|
84
|
+
const lastLength = Path.calcLength(x, y, 'Q', [
|
|
85
|
+
controlX,
|
|
86
|
+
controlY,
|
|
87
|
+
ex,
|
|
88
|
+
ey,
|
|
89
|
+
]);
|
|
90
|
+
const previous = Path.getPointOnQuadraticBezier(lastLength ? Math.max(0, 1 - this.pointerLength() / lastLength) : 0, x, y, controlX, controlY, ex, ey);
|
|
91
|
+
dx = ex - previous.x;
|
|
92
|
+
dy = ey - previous.y;
|
|
93
|
+
}
|
|
94
|
+
// repeated endpoints give a zero tangent, so walk towards the other end
|
|
95
|
+
// of the line until a distinct point is found
|
|
96
|
+
for (let i = atBeginning ? 0 : tp.length - 2; !dx && !dy && i >= 0 && i < tp.length; i += atBeginning ? 2 : -2) {
|
|
97
|
+
dx = ex - tp[i];
|
|
98
|
+
dy = ey - tp[i + 1];
|
|
99
|
+
}
|
|
100
|
+
const turn = Math.PI * 2;
|
|
101
|
+
return (Math.atan2(dy, dx) + turn) % turn;
|
|
102
|
+
}
|
|
96
103
|
__fillStroke(ctx) {
|
|
97
104
|
// here is a tricky part
|
|
98
105
|
// we need to disable dash for arrow pointers
|
|
@@ -112,17 +119,38 @@ export class Arrow extends Line {
|
|
|
112
119
|
}
|
|
113
120
|
getSelfRect() {
|
|
114
121
|
const lineRect = super.getSelfRect();
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
+
const points = this.points();
|
|
123
|
+
if (points.length < 4)
|
|
124
|
+
return lineRect;
|
|
125
|
+
let minX = lineRect.x, minY = lineRect.y;
|
|
126
|
+
let maxX = minX + lineRect.width, maxY = minY + lineRect.height;
|
|
127
|
+
for (const beginning of [false, true]) {
|
|
128
|
+
if (!(beginning ? this.pointerAtBeginning() : this.pointerAtEnding()))
|
|
129
|
+
continue;
|
|
130
|
+
const index = beginning ? 0 : points.length - 2;
|
|
131
|
+
const angle = this._getPointerAngle(beginning);
|
|
132
|
+
const cos = Math.cos(angle), sin = Math.sin(angle);
|
|
133
|
+
const x = points[index] - this.pointerLength() * cos;
|
|
134
|
+
const y = points[index + 1] - this.pointerLength() * sin;
|
|
135
|
+
const dx = Math.abs((this.pointerWidth() * sin) / 2);
|
|
136
|
+
const dy = Math.abs((this.pointerWidth() * cos) / 2);
|
|
137
|
+
minX = Math.min(minX, x - dx);
|
|
138
|
+
minY = Math.min(minY, y - dy);
|
|
139
|
+
maxX = Math.max(maxX, x + dx);
|
|
140
|
+
maxY = Math.max(maxY, y + dy);
|
|
141
|
+
}
|
|
142
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
122
143
|
}
|
|
123
144
|
}
|
|
124
145
|
Arrow.prototype.className = 'Arrow';
|
|
125
|
-
|
|
146
|
+
Arrow.prototype._attrsAffectingSize = [
|
|
147
|
+
...Line.prototype._attrsAffectingSize,
|
|
148
|
+
'pointerLength',
|
|
149
|
+
'pointerWidth',
|
|
150
|
+
'pointerAtBeginning',
|
|
151
|
+
'pointerAtEnding',
|
|
152
|
+
];
|
|
153
|
+
_registerNode(Arrow, true);
|
|
126
154
|
/**
|
|
127
155
|
* get/set pointerLength
|
|
128
156
|
* @name Konva.Arrow#pointerLength
|
|
@@ -156,7 +184,7 @@ Factory.addGetterSetter(Arrow, 'pointerWidth', 10, getNumberValidator());
|
|
|
156
184
|
* get/set pointerAtBeginning
|
|
157
185
|
* @name Konva.Arrow#pointerAtBeginning
|
|
158
186
|
* @method
|
|
159
|
-
* @param {
|
|
187
|
+
* @param {Boolean} pointerAtBeginning Whether to draw a pointer at the beginning of the arrow. The default is false.
|
|
160
188
|
* @returns {Boolean}
|
|
161
189
|
* @example
|
|
162
190
|
* // get value
|
|
@@ -170,7 +198,7 @@ Factory.addGetterSetter(Arrow, 'pointerAtBeginning', false);
|
|
|
170
198
|
* get/set pointerAtEnding
|
|
171
199
|
* @name Konva.Arrow#pointerAtEnding
|
|
172
200
|
* @method
|
|
173
|
-
* @param {
|
|
201
|
+
* @param {Boolean} pointerAtEnding Whether to draw a pointer at the end of the arrow. The default is true.
|
|
174
202
|
* @returns {Boolean}
|
|
175
203
|
* @example
|
|
176
204
|
* // get value
|
package/lib/shapes/Circle.d.ts
CHANGED
package/lib/shapes/Circle.js
CHANGED
|
@@ -21,6 +21,9 @@ import { _registerNode } from "../Global.js";
|
|
|
21
21
|
* });
|
|
22
22
|
*/
|
|
23
23
|
export class Circle extends Shape {
|
|
24
|
+
_getStrokePadding() {
|
|
25
|
+
return super._getStrokePadding(1);
|
|
26
|
+
}
|
|
24
27
|
_sceneFunc(context) {
|
|
25
28
|
context.beginPath();
|
|
26
29
|
context.arc(0, 0, Math.abs(this.attrs.radius || 0), 0, Math.PI * 2, false);
|
|
@@ -43,7 +46,7 @@ export class Circle extends Shape {
|
|
|
43
46
|
Circle.prototype._centroid = true;
|
|
44
47
|
Circle.prototype.className = 'Circle';
|
|
45
48
|
Circle.prototype._attrsAffectingSize = ['radius'];
|
|
46
|
-
_registerNode(Circle);
|
|
49
|
+
_registerNode(Circle, true);
|
|
47
50
|
/**
|
|
48
51
|
* get/set radius
|
|
49
52
|
* @name Konva.Circle#radius
|
package/lib/shapes/Ellipse.d.ts
CHANGED
package/lib/shapes/Ellipse.js
CHANGED
|
@@ -21,6 +21,9 @@ import { _registerNode } from "../Global.js";
|
|
|
21
21
|
* });
|
|
22
22
|
*/
|
|
23
23
|
export class Ellipse extends Shape {
|
|
24
|
+
_getStrokePadding() {
|
|
25
|
+
return super._getStrokePadding(1);
|
|
26
|
+
}
|
|
24
27
|
_sceneFunc(context) {
|
|
25
28
|
const rx = Math.abs(this.radiusX()), ry = Math.abs(this.radiusY());
|
|
26
29
|
context.beginPath();
|
|
@@ -44,7 +47,7 @@ export class Ellipse extends Shape {
|
|
|
44
47
|
Ellipse.prototype.className = 'Ellipse';
|
|
45
48
|
Ellipse.prototype._centroid = true;
|
|
46
49
|
Ellipse.prototype._attrsAffectingSize = ['radiusX', 'radiusY'];
|
|
47
|
-
_registerNode(Ellipse);
|
|
50
|
+
_registerNode(Ellipse, true);
|
|
48
51
|
// add getters setters
|
|
49
52
|
Factory.addComponentsGetterSetter(Ellipse, 'radius', ['x', 'y']);
|
|
50
53
|
/**
|
package/lib/shapes/Image.d.ts
CHANGED
|
@@ -36,7 +36,8 @@ export declare class Image extends Shape<ImageConfig> {
|
|
|
36
36
|
_setImageLoad(): void;
|
|
37
37
|
_removeImageLoad(image: any): void;
|
|
38
38
|
destroy(): this;
|
|
39
|
-
_useBufferCanvas(): boolean;
|
|
39
|
+
_useBufferCanvas(forceFill?: boolean, opacity?: any): boolean;
|
|
40
|
+
_getStrokePadding(): number;
|
|
40
41
|
_sceneFunc(context: Context): void;
|
|
41
42
|
_hitFunc(context: Context): void;
|
|
42
43
|
getWidth(): any;
|