konva 10.0.10 → 10.0.12
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 +20 -11
- package/konva.min.js +3 -3
- package/lib/Animation.d.ts +24 -0
- package/lib/Animation.js +142 -0
- package/lib/BezierFunctions.d.ts +6 -0
- package/lib/BezierFunctions.js +784 -0
- package/lib/Canvas.d.ts +33 -0
- package/lib/Canvas.js +107 -0
- package/lib/Container.d.ts +54 -0
- package/lib/Container.js +316 -0
- package/lib/Context.d.ts +95 -0
- package/lib/Context.js +561 -0
- package/lib/Core.d.ts +3 -0
- package/lib/Core.js +3 -0
- package/lib/DragAndDrop.d.ts +17 -0
- package/lib/DragAndDrop.js +110 -0
- package/lib/Factory.d.ts +22 -0
- package/lib/Factory.js +123 -0
- package/lib/FastLayer.d.ts +4 -0
- package/lib/FastLayer.js +12 -0
- package/lib/Global.d.ts +37 -0
- package/lib/Global.js +66 -0
- package/lib/Group.d.ts +9 -0
- package/lib/Group.js +13 -0
- package/lib/Layer.d.ts +62 -0
- package/lib/Layer.js +305 -0
- package/lib/Node.d.ts +323 -0
- package/lib/Node.js +1532 -0
- package/lib/PointerEvents.d.ts +11 -0
- package/lib/PointerEvents.js +37 -0
- package/lib/Shape.d.ts +196 -0
- package/lib/Shape.js +555 -0
- package/lib/Stage.d.ts +86 -0
- package/lib/Stage.js +715 -0
- package/lib/Tween.d.ts +90 -0
- package/lib/Tween.js +531 -0
- package/lib/Util.d.ts +137 -0
- package/lib/Util.js +918 -0
- package/lib/Validators.d.ts +11 -0
- package/lib/Validators.js +178 -0
- package/lib/_CoreInternals.d.ts +224 -0
- package/lib/_CoreInternals.js +34 -0
- package/lib/_FullInternals.d.ts +306 -0
- package/lib/_FullInternals.js +80 -0
- package/lib/canvas-backend.d.ts +2 -0
- package/lib/canvas-backend.js +27 -0
- package/lib/filters/Blur.d.ts +2 -0
- package/lib/filters/Blur.js +235 -0
- package/lib/filters/Brighten.d.ts +2 -0
- package/lib/filters/Brighten.js +12 -0
- package/lib/filters/Brightness.d.ts +2 -0
- package/lib/filters/Brightness.js +8 -0
- package/lib/filters/Contrast.d.ts +2 -0
- package/lib/filters/Contrast.js +35 -0
- package/lib/filters/Emboss.d.ts +2 -0
- package/lib/filters/Emboss.js +101 -0
- package/lib/filters/Enhance.d.ts +2 -0
- package/lib/filters/Enhance.js +84 -0
- package/lib/filters/Grayscale.d.ts +2 -0
- package/lib/filters/Grayscale.js +9 -0
- package/lib/filters/HSL.d.ts +2 -0
- package/lib/filters/HSL.js +24 -0
- package/lib/filters/HSV.d.ts +2 -0
- package/lib/filters/HSV.js +23 -0
- package/lib/filters/Invert.d.ts +2 -0
- package/lib/filters/Invert.js +8 -0
- package/lib/filters/Kaleidoscope.d.ts +2 -0
- package/lib/filters/Kaleidoscope.js +133 -0
- package/lib/filters/Mask.d.ts +2 -0
- package/lib/filters/Mask.js +151 -0
- package/lib/filters/Noise.d.ts +2 -0
- package/lib/filters/Noise.js +12 -0
- package/lib/filters/Pixelate.d.ts +2 -0
- package/lib/filters/Pixelate.js +60 -0
- package/lib/filters/Posterize.d.ts +2 -0
- package/lib/filters/Posterize.js +10 -0
- package/lib/filters/RGB.d.ts +2 -0
- package/lib/filters/RGB.js +38 -0
- package/lib/filters/RGBA.d.ts +2 -0
- package/lib/filters/RGBA.js +49 -0
- package/lib/filters/Sepia.d.ts +2 -0
- package/lib/filters/Sepia.js +11 -0
- package/lib/filters/Solarize.d.ts +2 -0
- package/lib/filters/Solarize.js +14 -0
- package/lib/filters/Threshold.d.ts +2 -0
- package/lib/filters/Threshold.js +10 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/shapes/Arc.d.ts +27 -0
- package/lib/shapes/Arc.js +60 -0
- package/lib/shapes/Arrow.d.ts +27 -0
- package/lib/shapes/Arrow.js +99 -0
- package/lib/shapes/Circle.d.ts +15 -0
- package/lib/shapes/Circle.js +33 -0
- package/lib/shapes/Ellipse.d.ts +18 -0
- package/lib/shapes/Ellipse.js +37 -0
- package/lib/shapes/Image.d.ts +29 -0
- package/lib/shapes/Image.js +131 -0
- package/lib/shapes/Label.d.ts +39 -0
- package/lib/shapes/Label.js +173 -0
- package/lib/shapes/Line.d.ts +29 -0
- package/lib/shapes/Line.js +156 -0
- package/lib/shapes/Path.d.ts +50 -0
- package/lib/shapes/Path.js +670 -0
- package/lib/shapes/Rect.d.ts +11 -0
- package/lib/shapes/Rect.js +22 -0
- package/lib/shapes/RegularPolygon.d.ts +26 -0
- package/lib/shapes/RegularPolygon.js +72 -0
- package/lib/shapes/Ring.d.ts +17 -0
- package/lib/shapes/Ring.js +33 -0
- package/lib/shapes/Sprite.d.ts +32 -0
- package/lib/shapes/Sprite.js +108 -0
- package/lib/shapes/Star.d.ts +19 -0
- package/lib/shapes/Star.js +38 -0
- package/lib/shapes/Text.d.ts +90 -0
- package/lib/shapes/Text.js +461 -0
- package/lib/shapes/TextPath.d.ts +72 -0
- package/lib/shapes/TextPath.js +268 -0
- package/lib/shapes/Transformer.d.ts +130 -0
- package/lib/shapes/Transformer.js +1009 -0
- package/lib/shapes/Wedge.d.ts +19 -0
- package/lib/shapes/Wedge.js +38 -0
- package/lib/skia-backend.d.ts +2 -0
- package/lib/skia-backend.js +24 -0
- package/lib/types.d.ts +56 -0
- package/lib/types.js +21 -0
- package/package.json +1 -1
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const Brightness = function (imageData) {
|
|
2
|
+
const brightness = this.brightness(), data = imageData.data, len = data.length;
|
|
3
|
+
for (let i = 0; i < len; i += 4) {
|
|
4
|
+
data[i] = Math.min(255, data[i] * brightness);
|
|
5
|
+
data[i + 1] = Math.min(255, data[i + 1] * brightness);
|
|
6
|
+
data[i + 2] = Math.min(255, data[i + 2] * brightness);
|
|
7
|
+
}
|
|
8
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Factory } from "../Factory.js";
|
|
2
|
+
import { Node } from "../Node.js";
|
|
3
|
+
import { getNumberValidator } from "../Validators.js";
|
|
4
|
+
export const Contrast = function (imageData) {
|
|
5
|
+
const adjust = Math.pow((this.contrast() + 100) / 100, 2);
|
|
6
|
+
const data = imageData.data, nPixels = data.length;
|
|
7
|
+
let red = 150, green = 150, blue = 150;
|
|
8
|
+
for (let i = 0; i < nPixels; i += 4) {
|
|
9
|
+
red = data[i];
|
|
10
|
+
green = data[i + 1];
|
|
11
|
+
blue = data[i + 2];
|
|
12
|
+
red /= 255;
|
|
13
|
+
red -= 0.5;
|
|
14
|
+
red *= adjust;
|
|
15
|
+
red += 0.5;
|
|
16
|
+
red *= 255;
|
|
17
|
+
green /= 255;
|
|
18
|
+
green -= 0.5;
|
|
19
|
+
green *= adjust;
|
|
20
|
+
green += 0.5;
|
|
21
|
+
green *= 255;
|
|
22
|
+
blue /= 255;
|
|
23
|
+
blue -= 0.5;
|
|
24
|
+
blue *= adjust;
|
|
25
|
+
blue += 0.5;
|
|
26
|
+
blue *= 255;
|
|
27
|
+
red = red < 0 ? 0 : red > 255 ? 255 : red;
|
|
28
|
+
green = green < 0 ? 0 : green > 255 ? 255 : green;
|
|
29
|
+
blue = blue < 0 ? 0 : blue > 255 ? 255 : blue;
|
|
30
|
+
data[i] = red;
|
|
31
|
+
data[i + 1] = green;
|
|
32
|
+
data[i + 2] = blue;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
Factory.addGetterSetter(Node, 'contrast', 0, getNumberValidator(), Factory.afterSetFilter);
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { Factory } from "../Factory.js";
|
|
2
|
+
import { Node } from "../Node.js";
|
|
3
|
+
import { getNumberValidator } from "../Validators.js";
|
|
4
|
+
export const Emboss = function (imageData) {
|
|
5
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
6
|
+
const data = imageData.data;
|
|
7
|
+
const w = imageData.width;
|
|
8
|
+
const h = imageData.height;
|
|
9
|
+
const strength01 = Math.min(1, Math.max(0, (_b = (_a = this.embossStrength) === null || _a === void 0 ? void 0 : _a.call(this)) !== null && _b !== void 0 ? _b : 0.5));
|
|
10
|
+
const whiteLevel01 = Math.min(1, Math.max(0, (_d = (_c = this.embossWhiteLevel) === null || _c === void 0 ? void 0 : _c.call(this)) !== null && _d !== void 0 ? _d : 0.5));
|
|
11
|
+
const directionMap = {
|
|
12
|
+
'top-left': 315,
|
|
13
|
+
top: 270,
|
|
14
|
+
'top-right': 225,
|
|
15
|
+
right: 180,
|
|
16
|
+
'bottom-right': 135,
|
|
17
|
+
bottom: 90,
|
|
18
|
+
'bottom-left': 45,
|
|
19
|
+
left: 0,
|
|
20
|
+
};
|
|
21
|
+
const directionDeg = (_g = directionMap[(_f = (_e = this.embossDirection) === null || _e === void 0 ? void 0 : _e.call(this)) !== null && _f !== void 0 ? _f : 'top-left']) !== null && _g !== void 0 ? _g : 315;
|
|
22
|
+
const blend = !!((_j = (_h = this.embossBlend) === null || _h === void 0 ? void 0 : _h.call(this)) !== null && _j !== void 0 ? _j : false);
|
|
23
|
+
const strength = strength01 * 10;
|
|
24
|
+
const bias = whiteLevel01 * 255;
|
|
25
|
+
const dirRad = (directionDeg * Math.PI) / 180;
|
|
26
|
+
const cx = Math.cos(dirRad);
|
|
27
|
+
const cy = Math.sin(dirRad);
|
|
28
|
+
const SCALE = (128 / 1020) * strength;
|
|
29
|
+
const src = new Uint8ClampedArray(data);
|
|
30
|
+
const lum = new Float32Array(w * h);
|
|
31
|
+
for (let p = 0, i = 0; i < data.length; i += 4, p++) {
|
|
32
|
+
lum[p] = 0.2126 * src[i] + 0.7152 * src[i + 1] + 0.0722 * src[i + 2];
|
|
33
|
+
}
|
|
34
|
+
const Gx = [-1, 0, 1, -2, 0, 2, -1, 0, 1];
|
|
35
|
+
const Gy = [-1, -2, -1, 0, 0, 0, 1, 2, 1];
|
|
36
|
+
const OFF = [-w - 1, -w, -w + 1, -1, 0, 1, w - 1, w, w + 1];
|
|
37
|
+
const clamp8 = (v) => (v < 0 ? 0 : v > 255 ? 255 : v);
|
|
38
|
+
for (let y = 1; y < h - 1; y++) {
|
|
39
|
+
for (let x = 1; x < w - 1; x++) {
|
|
40
|
+
const p = y * w + x;
|
|
41
|
+
let sx = 0, sy = 0;
|
|
42
|
+
sx += lum[p + OFF[0]] * Gx[0];
|
|
43
|
+
sy += lum[p + OFF[0]] * Gy[0];
|
|
44
|
+
sx += lum[p + OFF[1]] * Gx[1];
|
|
45
|
+
sy += lum[p + OFF[1]] * Gy[1];
|
|
46
|
+
sx += lum[p + OFF[2]] * Gx[2];
|
|
47
|
+
sy += lum[p + OFF[2]] * Gy[2];
|
|
48
|
+
sx += lum[p + OFF[3]] * Gx[3];
|
|
49
|
+
sy += lum[p + OFF[3]] * Gy[3];
|
|
50
|
+
sx += lum[p + OFF[5]] * Gx[5];
|
|
51
|
+
sy += lum[p + OFF[5]] * Gy[5];
|
|
52
|
+
sx += lum[p + OFF[6]] * Gx[6];
|
|
53
|
+
sy += lum[p + OFF[6]] * Gy[6];
|
|
54
|
+
sx += lum[p + OFF[7]] * Gx[7];
|
|
55
|
+
sy += lum[p + OFF[7]] * Gy[7];
|
|
56
|
+
sx += lum[p + OFF[8]] * Gx[8];
|
|
57
|
+
sy += lum[p + OFF[8]] * Gy[8];
|
|
58
|
+
const r = cx * sx + cy * sy;
|
|
59
|
+
const outGray = clamp8(bias + r * SCALE);
|
|
60
|
+
const o = p * 4;
|
|
61
|
+
if (blend) {
|
|
62
|
+
const delta = outGray - bias;
|
|
63
|
+
data[o] = clamp8(src[o] + delta);
|
|
64
|
+
data[o + 1] = clamp8(src[o + 1] + delta);
|
|
65
|
+
data[o + 2] = clamp8(src[o + 2] + delta);
|
|
66
|
+
data[o + 3] = src[o + 3];
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
data[o] = data[o + 1] = data[o + 2] = outGray;
|
|
70
|
+
data[o + 3] = src[o + 3];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
for (let x = 0; x < w; x++) {
|
|
75
|
+
let oTop = x * 4, oBot = ((h - 1) * w + x) * 4;
|
|
76
|
+
data[oTop] = src[oTop];
|
|
77
|
+
data[oTop + 1] = src[oTop + 1];
|
|
78
|
+
data[oTop + 2] = src[oTop + 2];
|
|
79
|
+
data[oTop + 3] = src[oTop + 3];
|
|
80
|
+
data[oBot] = src[oBot];
|
|
81
|
+
data[oBot + 1] = src[oBot + 1];
|
|
82
|
+
data[oBot + 2] = src[oBot + 2];
|
|
83
|
+
data[oBot + 3] = src[oBot + 3];
|
|
84
|
+
}
|
|
85
|
+
for (let y = 1; y < h - 1; y++) {
|
|
86
|
+
let oL = y * w * 4, oR = (y * w + (w - 1)) * 4;
|
|
87
|
+
data[oL] = src[oL];
|
|
88
|
+
data[oL + 1] = src[oL + 1];
|
|
89
|
+
data[oL + 2] = src[oL + 2];
|
|
90
|
+
data[oL + 3] = src[oL + 3];
|
|
91
|
+
data[oR] = src[oR];
|
|
92
|
+
data[oR + 1] = src[oR + 1];
|
|
93
|
+
data[oR + 2] = src[oR + 2];
|
|
94
|
+
data[oR + 3] = src[oR + 3];
|
|
95
|
+
}
|
|
96
|
+
return imageData;
|
|
97
|
+
};
|
|
98
|
+
Factory.addGetterSetter(Node, 'embossStrength', 0.5, getNumberValidator(), Factory.afterSetFilter);
|
|
99
|
+
Factory.addGetterSetter(Node, 'embossWhiteLevel', 0.5, getNumberValidator(), Factory.afterSetFilter);
|
|
100
|
+
Factory.addGetterSetter(Node, 'embossDirection', 'top-left', undefined, Factory.afterSetFilter);
|
|
101
|
+
Factory.addGetterSetter(Node, 'embossBlend', false, undefined, Factory.afterSetFilter);
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Factory } from "../Factory.js";
|
|
2
|
+
import { Node } from "../Node.js";
|
|
3
|
+
import { getNumberValidator } from "../Validators.js";
|
|
4
|
+
function remap(fromValue, fromMin, fromMax, toMin, toMax) {
|
|
5
|
+
const fromRange = fromMax - fromMin, toRange = toMax - toMin;
|
|
6
|
+
if (fromRange === 0) {
|
|
7
|
+
return toMin + toRange / 2;
|
|
8
|
+
}
|
|
9
|
+
if (toRange === 0) {
|
|
10
|
+
return toMin;
|
|
11
|
+
}
|
|
12
|
+
let toValue = (fromValue - fromMin) / fromRange;
|
|
13
|
+
toValue = toRange * toValue + toMin;
|
|
14
|
+
return toValue;
|
|
15
|
+
}
|
|
16
|
+
export const Enhance = function (imageData) {
|
|
17
|
+
const data = imageData.data, nSubPixels = data.length;
|
|
18
|
+
let rMin = data[0], rMax = rMin, r, gMin = data[1], gMax = gMin, g, bMin = data[2], bMax = bMin, b;
|
|
19
|
+
const enhanceAmount = this.enhance();
|
|
20
|
+
if (enhanceAmount === 0) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
for (let i = 0; i < nSubPixels; i += 4) {
|
|
24
|
+
r = data[i + 0];
|
|
25
|
+
if (r < rMin) {
|
|
26
|
+
rMin = r;
|
|
27
|
+
}
|
|
28
|
+
else if (r > rMax) {
|
|
29
|
+
rMax = r;
|
|
30
|
+
}
|
|
31
|
+
g = data[i + 1];
|
|
32
|
+
if (g < gMin) {
|
|
33
|
+
gMin = g;
|
|
34
|
+
}
|
|
35
|
+
else if (g > gMax) {
|
|
36
|
+
gMax = g;
|
|
37
|
+
}
|
|
38
|
+
b = data[i + 2];
|
|
39
|
+
if (b < bMin) {
|
|
40
|
+
bMin = b;
|
|
41
|
+
}
|
|
42
|
+
else if (b > bMax) {
|
|
43
|
+
bMax = b;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (rMax === rMin) {
|
|
47
|
+
rMax = 255;
|
|
48
|
+
rMin = 0;
|
|
49
|
+
}
|
|
50
|
+
if (gMax === gMin) {
|
|
51
|
+
gMax = 255;
|
|
52
|
+
gMin = 0;
|
|
53
|
+
}
|
|
54
|
+
if (bMax === bMin) {
|
|
55
|
+
bMax = 255;
|
|
56
|
+
bMin = 0;
|
|
57
|
+
}
|
|
58
|
+
let rGoalMax, rGoalMin, gGoalMax, gGoalMin, bGoalMax, bGoalMin;
|
|
59
|
+
if (enhanceAmount > 0) {
|
|
60
|
+
rGoalMax = rMax + enhanceAmount * (255 - rMax);
|
|
61
|
+
rGoalMin = rMin - enhanceAmount * (rMin - 0);
|
|
62
|
+
gGoalMax = gMax + enhanceAmount * (255 - gMax);
|
|
63
|
+
gGoalMin = gMin - enhanceAmount * (gMin - 0);
|
|
64
|
+
bGoalMax = bMax + enhanceAmount * (255 - bMax);
|
|
65
|
+
bGoalMin = bMin - enhanceAmount * (bMin - 0);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
const rMid = (rMax + rMin) * 0.5;
|
|
69
|
+
rGoalMax = rMax + enhanceAmount * (rMax - rMid);
|
|
70
|
+
rGoalMin = rMin + enhanceAmount * (rMin - rMid);
|
|
71
|
+
const gMid = (gMax + gMin) * 0.5;
|
|
72
|
+
gGoalMax = gMax + enhanceAmount * (gMax - gMid);
|
|
73
|
+
gGoalMin = gMin + enhanceAmount * (gMin - gMid);
|
|
74
|
+
const bMid = (bMax + bMin) * 0.5;
|
|
75
|
+
bGoalMax = bMax + enhanceAmount * (bMax - bMid);
|
|
76
|
+
bGoalMin = bMin + enhanceAmount * (bMin - bMid);
|
|
77
|
+
}
|
|
78
|
+
for (let i = 0; i < nSubPixels; i += 4) {
|
|
79
|
+
data[i + 0] = remap(data[i + 0], rMin, rMax, rGoalMin, rGoalMax);
|
|
80
|
+
data[i + 1] = remap(data[i + 1], gMin, gMax, gGoalMin, gGoalMax);
|
|
81
|
+
data[i + 2] = remap(data[i + 2], bMin, bMax, bGoalMin, bGoalMax);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
Factory.addGetterSetter(Node, 'enhance', 0, getNumberValidator(), Factory.afterSetFilter);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const Grayscale = function (imageData) {
|
|
2
|
+
const data = imageData.data, len = data.length;
|
|
3
|
+
for (let i = 0; i < len; i += 4) {
|
|
4
|
+
const brightness = 0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2];
|
|
5
|
+
data[i] = brightness;
|
|
6
|
+
data[i + 1] = brightness;
|
|
7
|
+
data[i + 2] = brightness;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Factory } from "../Factory.js";
|
|
2
|
+
import { Node } from "../Node.js";
|
|
3
|
+
import { getNumberValidator } from "../Validators.js";
|
|
4
|
+
Factory.addGetterSetter(Node, 'hue', 0, getNumberValidator(), Factory.afterSetFilter);
|
|
5
|
+
Factory.addGetterSetter(Node, 'saturation', 0, getNumberValidator(), Factory.afterSetFilter);
|
|
6
|
+
Factory.addGetterSetter(Node, 'luminance', 0, getNumberValidator(), Factory.afterSetFilter);
|
|
7
|
+
export const HSL = function (imageData) {
|
|
8
|
+
const data = imageData.data, nPixels = data.length, v = 1, s = Math.pow(2, this.saturation()), h = Math.abs(this.hue() + 360) % 360, l = this.luminance() * 127;
|
|
9
|
+
const vsu = v * s * Math.cos((h * Math.PI) / 180), vsw = v * s * Math.sin((h * Math.PI) / 180);
|
|
10
|
+
const rr = 0.299 * v + 0.701 * vsu + 0.167 * vsw, rg = 0.587 * v - 0.587 * vsu + 0.33 * vsw, rb = 0.114 * v - 0.114 * vsu - 0.497 * vsw;
|
|
11
|
+
const gr = 0.299 * v - 0.299 * vsu - 0.328 * vsw, gg = 0.587 * v + 0.413 * vsu + 0.035 * vsw, gb = 0.114 * v - 0.114 * vsu + 0.293 * vsw;
|
|
12
|
+
const br = 0.299 * v - 0.3 * vsu + 1.25 * vsw, bg = 0.587 * v - 0.586 * vsu - 1.05 * vsw, bb = 0.114 * v + 0.886 * vsu - 0.2 * vsw;
|
|
13
|
+
let r, g, b, a;
|
|
14
|
+
for (let i = 0; i < nPixels; i += 4) {
|
|
15
|
+
r = data[i + 0];
|
|
16
|
+
g = data[i + 1];
|
|
17
|
+
b = data[i + 2];
|
|
18
|
+
a = data[i + 3];
|
|
19
|
+
data[i + 0] = rr * r + rg * g + rb * b + l;
|
|
20
|
+
data[i + 1] = gr * r + gg * g + gb * b + l;
|
|
21
|
+
data[i + 2] = br * r + bg * g + bb * b + l;
|
|
22
|
+
data[i + 3] = a;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Factory } from "../Factory.js";
|
|
2
|
+
import { Node } from "../Node.js";
|
|
3
|
+
import { getNumberValidator } from "../Validators.js";
|
|
4
|
+
export const HSV = function (imageData) {
|
|
5
|
+
const data = imageData.data, nPixels = data.length, v = Math.pow(2, this.value()), s = Math.pow(2, this.saturation()), h = Math.abs(this.hue() + 360) % 360;
|
|
6
|
+
const vsu = v * s * Math.cos((h * Math.PI) / 180), vsw = v * s * Math.sin((h * Math.PI) / 180);
|
|
7
|
+
const rr = 0.299 * v + 0.701 * vsu + 0.167 * vsw, rg = 0.587 * v - 0.587 * vsu + 0.33 * vsw, rb = 0.114 * v - 0.114 * vsu - 0.497 * vsw;
|
|
8
|
+
const gr = 0.299 * v - 0.299 * vsu - 0.328 * vsw, gg = 0.587 * v + 0.413 * vsu + 0.035 * vsw, gb = 0.114 * v - 0.114 * vsu + 0.293 * vsw;
|
|
9
|
+
const br = 0.299 * v - 0.3 * vsu + 1.25 * vsw, bg = 0.587 * v - 0.586 * vsu - 1.05 * vsw, bb = 0.114 * v + 0.886 * vsu - 0.2 * vsw;
|
|
10
|
+
for (let i = 0; i < nPixels; i += 4) {
|
|
11
|
+
const r = data[i + 0];
|
|
12
|
+
const g = data[i + 1];
|
|
13
|
+
const b = data[i + 2];
|
|
14
|
+
const a = data[i + 3];
|
|
15
|
+
data[i + 0] = rr * r + rg * g + rb * b;
|
|
16
|
+
data[i + 1] = gr * r + gg * g + gb * b;
|
|
17
|
+
data[i + 2] = br * r + bg * g + bb * b;
|
|
18
|
+
data[i + 3] = a;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
Factory.addGetterSetter(Node, 'hue', 0, getNumberValidator(), Factory.afterSetFilter);
|
|
22
|
+
Factory.addGetterSetter(Node, 'saturation', 0, getNumberValidator(), Factory.afterSetFilter);
|
|
23
|
+
Factory.addGetterSetter(Node, 'value', 0, getNumberValidator(), Factory.afterSetFilter);
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Factory } from "../Factory.js";
|
|
2
|
+
import { Node } from "../Node.js";
|
|
3
|
+
import { Util } from "../Util.js";
|
|
4
|
+
import { getNumberValidator } from "../Validators.js";
|
|
5
|
+
const ToPolar = function (src, dst, opt) {
|
|
6
|
+
const srcPixels = src.data, dstPixels = dst.data, xSize = src.width, ySize = src.height, xMid = opt.polarCenterX || xSize / 2, yMid = opt.polarCenterY || ySize / 2;
|
|
7
|
+
let rMax = Math.sqrt(xMid * xMid + yMid * yMid);
|
|
8
|
+
let x = xSize - xMid;
|
|
9
|
+
let y = ySize - yMid;
|
|
10
|
+
const rad = Math.sqrt(x * x + y * y);
|
|
11
|
+
rMax = rad > rMax ? rad : rMax;
|
|
12
|
+
const rSize = ySize, tSize = xSize;
|
|
13
|
+
const conversion = ((360 / tSize) * Math.PI) / 180;
|
|
14
|
+
for (let theta = 0; theta < tSize; theta += 1) {
|
|
15
|
+
const sin = Math.sin(theta * conversion);
|
|
16
|
+
const cos = Math.cos(theta * conversion);
|
|
17
|
+
for (let radius = 0; radius < rSize; radius += 1) {
|
|
18
|
+
x = Math.floor(xMid + ((rMax * radius) / rSize) * cos);
|
|
19
|
+
y = Math.floor(yMid + ((rMax * radius) / rSize) * sin);
|
|
20
|
+
let i = (y * xSize + x) * 4;
|
|
21
|
+
const r = srcPixels[i + 0];
|
|
22
|
+
const g = srcPixels[i + 1];
|
|
23
|
+
const b = srcPixels[i + 2];
|
|
24
|
+
const a = srcPixels[i + 3];
|
|
25
|
+
i = (theta + radius * xSize) * 4;
|
|
26
|
+
dstPixels[i + 0] = r;
|
|
27
|
+
dstPixels[i + 1] = g;
|
|
28
|
+
dstPixels[i + 2] = b;
|
|
29
|
+
dstPixels[i + 3] = a;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const FromPolar = function (src, dst, opt) {
|
|
34
|
+
const srcPixels = src.data, dstPixels = dst.data, xSize = src.width, ySize = src.height, xMid = opt.polarCenterX || xSize / 2, yMid = opt.polarCenterY || ySize / 2;
|
|
35
|
+
let rMax = Math.sqrt(xMid * xMid + yMid * yMid);
|
|
36
|
+
let x = xSize - xMid;
|
|
37
|
+
let y = ySize - yMid;
|
|
38
|
+
const rad = Math.sqrt(x * x + y * y);
|
|
39
|
+
rMax = rad > rMax ? rad : rMax;
|
|
40
|
+
const rSize = ySize, tSize = xSize, phaseShift = opt.polarRotation || 0;
|
|
41
|
+
let x1, y1;
|
|
42
|
+
for (x = 0; x < xSize; x += 1) {
|
|
43
|
+
for (y = 0; y < ySize; y += 1) {
|
|
44
|
+
const dx = x - xMid;
|
|
45
|
+
const dy = y - yMid;
|
|
46
|
+
const radius = (Math.sqrt(dx * dx + dy * dy) * rSize) / rMax;
|
|
47
|
+
let theta = ((Math.atan2(dy, dx) * 180) / Math.PI + 360 + phaseShift) % 360;
|
|
48
|
+
theta = (theta * tSize) / 360;
|
|
49
|
+
x1 = Math.floor(theta);
|
|
50
|
+
y1 = Math.floor(radius);
|
|
51
|
+
let i = (y1 * xSize + x1) * 4;
|
|
52
|
+
const r = srcPixels[i + 0];
|
|
53
|
+
const g = srcPixels[i + 1];
|
|
54
|
+
const b = srcPixels[i + 2];
|
|
55
|
+
const a = srcPixels[i + 3];
|
|
56
|
+
i = (y * xSize + x) * 4;
|
|
57
|
+
dstPixels[i + 0] = r;
|
|
58
|
+
dstPixels[i + 1] = g;
|
|
59
|
+
dstPixels[i + 2] = b;
|
|
60
|
+
dstPixels[i + 3] = a;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
export const Kaleidoscope = function (imageData) {
|
|
65
|
+
const xSize = imageData.width, ySize = imageData.height;
|
|
66
|
+
let x, y, xoff, i, r, g, b, a, srcPos, dstPos;
|
|
67
|
+
let power = Math.round(this.kaleidoscopePower());
|
|
68
|
+
const angle = Math.round(this.kaleidoscopeAngle());
|
|
69
|
+
const offset = Math.floor((xSize * (angle % 360)) / 360);
|
|
70
|
+
if (power < 1) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const tempCanvas = Util.createCanvasElement();
|
|
74
|
+
tempCanvas.width = xSize;
|
|
75
|
+
tempCanvas.height = ySize;
|
|
76
|
+
const scratchData = tempCanvas
|
|
77
|
+
.getContext('2d')
|
|
78
|
+
.getImageData(0, 0, xSize, ySize);
|
|
79
|
+
Util.releaseCanvas(tempCanvas);
|
|
80
|
+
ToPolar(imageData, scratchData, {
|
|
81
|
+
polarCenterX: xSize / 2,
|
|
82
|
+
polarCenterY: ySize / 2,
|
|
83
|
+
});
|
|
84
|
+
let minSectionSize = xSize / Math.pow(2, power);
|
|
85
|
+
while (minSectionSize <= 8) {
|
|
86
|
+
minSectionSize = minSectionSize * 2;
|
|
87
|
+
power -= 1;
|
|
88
|
+
}
|
|
89
|
+
minSectionSize = Math.ceil(minSectionSize);
|
|
90
|
+
let sectionSize = minSectionSize;
|
|
91
|
+
let xStart = 0, xEnd = sectionSize, xDelta = 1;
|
|
92
|
+
if (offset + minSectionSize > xSize) {
|
|
93
|
+
xStart = sectionSize;
|
|
94
|
+
xEnd = 0;
|
|
95
|
+
xDelta = -1;
|
|
96
|
+
}
|
|
97
|
+
for (y = 0; y < ySize; y += 1) {
|
|
98
|
+
for (x = xStart; x !== xEnd; x += xDelta) {
|
|
99
|
+
xoff = Math.round(x + offset) % xSize;
|
|
100
|
+
srcPos = (xSize * y + xoff) * 4;
|
|
101
|
+
r = scratchData.data[srcPos + 0];
|
|
102
|
+
g = scratchData.data[srcPos + 1];
|
|
103
|
+
b = scratchData.data[srcPos + 2];
|
|
104
|
+
a = scratchData.data[srcPos + 3];
|
|
105
|
+
dstPos = (xSize * y + x) * 4;
|
|
106
|
+
scratchData.data[dstPos + 0] = r;
|
|
107
|
+
scratchData.data[dstPos + 1] = g;
|
|
108
|
+
scratchData.data[dstPos + 2] = b;
|
|
109
|
+
scratchData.data[dstPos + 3] = a;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
for (y = 0; y < ySize; y += 1) {
|
|
113
|
+
sectionSize = Math.floor(minSectionSize);
|
|
114
|
+
for (i = 0; i < power; i += 1) {
|
|
115
|
+
for (x = 0; x < sectionSize + 1; x += 1) {
|
|
116
|
+
srcPos = (xSize * y + x) * 4;
|
|
117
|
+
r = scratchData.data[srcPos + 0];
|
|
118
|
+
g = scratchData.data[srcPos + 1];
|
|
119
|
+
b = scratchData.data[srcPos + 2];
|
|
120
|
+
a = scratchData.data[srcPos + 3];
|
|
121
|
+
dstPos = (xSize * y + sectionSize * 2 - x - 1) * 4;
|
|
122
|
+
scratchData.data[dstPos + 0] = r;
|
|
123
|
+
scratchData.data[dstPos + 1] = g;
|
|
124
|
+
scratchData.data[dstPos + 2] = b;
|
|
125
|
+
scratchData.data[dstPos + 3] = a;
|
|
126
|
+
}
|
|
127
|
+
sectionSize *= 2;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
FromPolar(scratchData, imageData, { polarRotation: 0 });
|
|
131
|
+
};
|
|
132
|
+
Factory.addGetterSetter(Node, 'kaleidoscopePower', 2, getNumberValidator(), Factory.afterSetFilter);
|
|
133
|
+
Factory.addGetterSetter(Node, 'kaleidoscopeAngle', 0, getNumberValidator(), Factory.afterSetFilter);
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { Factory } from "../Factory.js";
|
|
2
|
+
import { Node } from "../Node.js";
|
|
3
|
+
import { getNumberValidator } from "../Validators.js";
|
|
4
|
+
function pixelAt(idata, x, y) {
|
|
5
|
+
let idx = (y * idata.width + x) * 4;
|
|
6
|
+
const d = [];
|
|
7
|
+
d.push(idata.data[idx++], idata.data[idx++], idata.data[idx++], idata.data[idx++]);
|
|
8
|
+
return d;
|
|
9
|
+
}
|
|
10
|
+
function rgbDistance(p1, p2) {
|
|
11
|
+
return Math.sqrt(Math.pow(p1[0] - p2[0], 2) +
|
|
12
|
+
Math.pow(p1[1] - p2[1], 2) +
|
|
13
|
+
Math.pow(p1[2] - p2[2], 2));
|
|
14
|
+
}
|
|
15
|
+
function rgbMean(pTab) {
|
|
16
|
+
const m = [0, 0, 0];
|
|
17
|
+
for (let i = 0; i < pTab.length; i++) {
|
|
18
|
+
m[0] += pTab[i][0];
|
|
19
|
+
m[1] += pTab[i][1];
|
|
20
|
+
m[2] += pTab[i][2];
|
|
21
|
+
}
|
|
22
|
+
m[0] /= pTab.length;
|
|
23
|
+
m[1] /= pTab.length;
|
|
24
|
+
m[2] /= pTab.length;
|
|
25
|
+
return m;
|
|
26
|
+
}
|
|
27
|
+
function backgroundMask(idata, threshold) {
|
|
28
|
+
const rgbv_no = pixelAt(idata, 0, 0);
|
|
29
|
+
const rgbv_ne = pixelAt(idata, idata.width - 1, 0);
|
|
30
|
+
const rgbv_so = pixelAt(idata, 0, idata.height - 1);
|
|
31
|
+
const rgbv_se = pixelAt(idata, idata.width - 1, idata.height - 1);
|
|
32
|
+
const thres = threshold || 10;
|
|
33
|
+
if (rgbDistance(rgbv_no, rgbv_ne) < thres &&
|
|
34
|
+
rgbDistance(rgbv_ne, rgbv_se) < thres &&
|
|
35
|
+
rgbDistance(rgbv_se, rgbv_so) < thres &&
|
|
36
|
+
rgbDistance(rgbv_so, rgbv_no) < thres) {
|
|
37
|
+
const mean = rgbMean([rgbv_ne, rgbv_no, rgbv_se, rgbv_so]);
|
|
38
|
+
const mask = [];
|
|
39
|
+
for (let i = 0; i < idata.width * idata.height; i++) {
|
|
40
|
+
const d = rgbDistance(mean, [
|
|
41
|
+
idata.data[i * 4],
|
|
42
|
+
idata.data[i * 4 + 1],
|
|
43
|
+
idata.data[i * 4 + 2],
|
|
44
|
+
]);
|
|
45
|
+
mask[i] = d < thres ? 0 : 255;
|
|
46
|
+
}
|
|
47
|
+
return mask;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function applyMask(idata, mask) {
|
|
51
|
+
for (let i = 0; i < idata.width * idata.height; i++) {
|
|
52
|
+
idata.data[4 * i + 3] = mask[i];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function erodeMask(mask, sw, sh) {
|
|
56
|
+
const weights = [1, 1, 1, 1, 0, 1, 1, 1, 1];
|
|
57
|
+
const side = Math.round(Math.sqrt(weights.length));
|
|
58
|
+
const halfSide = Math.floor(side / 2);
|
|
59
|
+
const maskResult = [];
|
|
60
|
+
for (let y = 0; y < sh; y++) {
|
|
61
|
+
for (let x = 0; x < sw; x++) {
|
|
62
|
+
const so = y * sw + x;
|
|
63
|
+
let a = 0;
|
|
64
|
+
for (let cy = 0; cy < side; cy++) {
|
|
65
|
+
for (let cx = 0; cx < side; cx++) {
|
|
66
|
+
const scy = y + cy - halfSide;
|
|
67
|
+
const scx = x + cx - halfSide;
|
|
68
|
+
if (scy >= 0 && scy < sh && scx >= 0 && scx < sw) {
|
|
69
|
+
const srcOff = scy * sw + scx;
|
|
70
|
+
const wt = weights[cy * side + cx];
|
|
71
|
+
a += mask[srcOff] * wt;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
maskResult[so] = a === 255 * 8 ? 255 : 0;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return maskResult;
|
|
79
|
+
}
|
|
80
|
+
function dilateMask(mask, sw, sh) {
|
|
81
|
+
const weights = [1, 1, 1, 1, 1, 1, 1, 1, 1];
|
|
82
|
+
const side = Math.round(Math.sqrt(weights.length));
|
|
83
|
+
const halfSide = Math.floor(side / 2);
|
|
84
|
+
const maskResult = [];
|
|
85
|
+
for (let y = 0; y < sh; y++) {
|
|
86
|
+
for (let x = 0; x < sw; x++) {
|
|
87
|
+
const so = y * sw + x;
|
|
88
|
+
let a = 0;
|
|
89
|
+
for (let cy = 0; cy < side; cy++) {
|
|
90
|
+
for (let cx = 0; cx < side; cx++) {
|
|
91
|
+
const scy = y + cy - halfSide;
|
|
92
|
+
const scx = x + cx - halfSide;
|
|
93
|
+
if (scy >= 0 && scy < sh && scx >= 0 && scx < sw) {
|
|
94
|
+
const srcOff = scy * sw + scx;
|
|
95
|
+
const wt = weights[cy * side + cx];
|
|
96
|
+
a += mask[srcOff] * wt;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
maskResult[so] = a >= 255 * 4 ? 255 : 0;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return maskResult;
|
|
104
|
+
}
|
|
105
|
+
function smoothEdgeMask(mask, sw, sh) {
|
|
106
|
+
const weights = [
|
|
107
|
+
1 / 9,
|
|
108
|
+
1 / 9,
|
|
109
|
+
1 / 9,
|
|
110
|
+
1 / 9,
|
|
111
|
+
1 / 9,
|
|
112
|
+
1 / 9,
|
|
113
|
+
1 / 9,
|
|
114
|
+
1 / 9,
|
|
115
|
+
1 / 9,
|
|
116
|
+
];
|
|
117
|
+
const side = Math.round(Math.sqrt(weights.length));
|
|
118
|
+
const halfSide = Math.floor(side / 2);
|
|
119
|
+
const maskResult = [];
|
|
120
|
+
for (let y = 0; y < sh; y++) {
|
|
121
|
+
for (let x = 0; x < sw; x++) {
|
|
122
|
+
const so = y * sw + x;
|
|
123
|
+
let a = 0;
|
|
124
|
+
for (let cy = 0; cy < side; cy++) {
|
|
125
|
+
for (let cx = 0; cx < side; cx++) {
|
|
126
|
+
const scy = y + cy - halfSide;
|
|
127
|
+
const scx = x + cx - halfSide;
|
|
128
|
+
if (scy >= 0 && scy < sh && scx >= 0 && scx < sw) {
|
|
129
|
+
const srcOff = scy * sw + scx;
|
|
130
|
+
const wt = weights[cy * side + cx];
|
|
131
|
+
a += mask[srcOff] * wt;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
maskResult[so] = a;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return maskResult;
|
|
139
|
+
}
|
|
140
|
+
export const Mask = function (imageData) {
|
|
141
|
+
const threshold = this.threshold();
|
|
142
|
+
let mask = backgroundMask(imageData, threshold);
|
|
143
|
+
if (mask) {
|
|
144
|
+
mask = erodeMask(mask, imageData.width, imageData.height);
|
|
145
|
+
mask = dilateMask(mask, imageData.width, imageData.height);
|
|
146
|
+
mask = smoothEdgeMask(mask, imageData.width, imageData.height);
|
|
147
|
+
applyMask(imageData, mask);
|
|
148
|
+
}
|
|
149
|
+
return imageData;
|
|
150
|
+
};
|
|
151
|
+
Factory.addGetterSetter(Node, 'threshold', 0, getNumberValidator(), Factory.afterSetFilter);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Factory } from "../Factory.js";
|
|
2
|
+
import { Node } from "../Node.js";
|
|
3
|
+
import { getNumberValidator } from "../Validators.js";
|
|
4
|
+
export const Noise = function (imageData) {
|
|
5
|
+
const amount = this.noise() * 255, data = imageData.data, nPixels = data.length, half = amount / 2;
|
|
6
|
+
for (let i = 0; i < nPixels; i += 4) {
|
|
7
|
+
data[i + 0] += half - 2 * half * Math.random();
|
|
8
|
+
data[i + 1] += half - 2 * half * Math.random();
|
|
9
|
+
data[i + 2] += half - 2 * half * Math.random();
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
Factory.addGetterSetter(Node, 'noise', 0.2, getNumberValidator(), Factory.afterSetFilter);
|