@nasser-sw/fabric 7.0.1-beta3 → 7.0.1-beta4

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.
Files changed (105) hide show
  1. package/0 +0 -0
  2. package/dist/index.js +323 -155
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.min.js +1 -1
  5. package/dist/index.min.js.map +1 -1
  6. package/dist/index.min.mjs +1 -1
  7. package/dist/index.min.mjs.map +1 -1
  8. package/dist/index.mjs +323 -155
  9. package/dist/index.mjs.map +1 -1
  10. package/dist/index.node.cjs +323 -155
  11. package/dist/index.node.cjs.map +1 -1
  12. package/dist/index.node.mjs +323 -155
  13. package/dist/index.node.mjs.map +1 -1
  14. package/dist/package.json.min.mjs +1 -1
  15. package/dist/package.json.mjs +1 -1
  16. package/dist/src/shapes/Line.d.ts +31 -86
  17. package/dist/src/shapes/Line.d.ts.map +1 -1
  18. package/dist/src/shapes/Line.min.mjs +1 -1
  19. package/dist/src/shapes/Line.min.mjs.map +1 -1
  20. package/dist/src/shapes/Line.mjs +323 -154
  21. package/dist/src/shapes/Line.mjs.map +1 -1
  22. package/dist-extensions/src/shapes/CustomLine.d.ts +10 -0
  23. package/dist-extensions/src/shapes/CustomLine.d.ts.map +1 -0
  24. package/dist-extensions/src/shapes/Line.d.ts +31 -86
  25. package/dist-extensions/src/shapes/Line.d.ts.map +1 -1
  26. package/fabric-test-editor.html +157 -8
  27. package/fabric-test2.html +513 -0
  28. package/fabric.ts +182 -182
  29. package/package.json +1 -1
  30. package/src/shapes/Line.ts +372 -158
  31. package/debug/konva/CHANGELOG.md +0 -1474
  32. package/debug/konva/LICENSE +0 -22
  33. package/debug/konva/README.md +0 -205
  34. package/debug/konva/gulpfile.mjs +0 -110
  35. package/debug/konva/package.json +0 -139
  36. package/debug/konva/release.sh +0 -65
  37. package/debug/konva/resources/doc-includes/ContainerParams.txt +0 -6
  38. package/debug/konva/resources/doc-includes/NodeParams.txt +0 -20
  39. package/debug/konva/resources/doc-includes/ShapeParams.txt +0 -53
  40. package/debug/konva/resources/jsdoc.conf.json +0 -28
  41. package/debug/konva/rollup.config.mjs +0 -32
  42. package/debug/konva/src/Animation.ts +0 -237
  43. package/debug/konva/src/BezierFunctions.ts +0 -826
  44. package/debug/konva/src/Canvas.ts +0 -193
  45. package/debug/konva/src/Container.ts +0 -649
  46. package/debug/konva/src/Context.ts +0 -1017
  47. package/debug/konva/src/Core.ts +0 -5
  48. package/debug/konva/src/DragAndDrop.ts +0 -173
  49. package/debug/konva/src/Factory.ts +0 -246
  50. package/debug/konva/src/FastLayer.ts +0 -29
  51. package/debug/konva/src/Global.ts +0 -210
  52. package/debug/konva/src/Group.ts +0 -31
  53. package/debug/konva/src/Layer.ts +0 -546
  54. package/debug/konva/src/Node.ts +0 -3477
  55. package/debug/konva/src/PointerEvents.ts +0 -67
  56. package/debug/konva/src/Shape.ts +0 -2081
  57. package/debug/konva/src/Stage.ts +0 -1000
  58. package/debug/konva/src/Tween.ts +0 -811
  59. package/debug/konva/src/Util.ts +0 -1123
  60. package/debug/konva/src/Validators.ts +0 -210
  61. package/debug/konva/src/_CoreInternals.ts +0 -85
  62. package/debug/konva/src/_FullInternals.ts +0 -171
  63. package/debug/konva/src/canvas-backend.ts +0 -36
  64. package/debug/konva/src/filters/Blur.ts +0 -388
  65. package/debug/konva/src/filters/Brighten.ts +0 -48
  66. package/debug/konva/src/filters/Brightness.ts +0 -30
  67. package/debug/konva/src/filters/Contrast.ts +0 -75
  68. package/debug/konva/src/filters/Emboss.ts +0 -207
  69. package/debug/konva/src/filters/Enhance.ts +0 -154
  70. package/debug/konva/src/filters/Grayscale.ts +0 -25
  71. package/debug/konva/src/filters/HSL.ts +0 -108
  72. package/debug/konva/src/filters/HSV.ts +0 -106
  73. package/debug/konva/src/filters/Invert.ts +0 -23
  74. package/debug/konva/src/filters/Kaleidoscope.ts +0 -274
  75. package/debug/konva/src/filters/Mask.ts +0 -220
  76. package/debug/konva/src/filters/Noise.ts +0 -44
  77. package/debug/konva/src/filters/Pixelate.ts +0 -107
  78. package/debug/konva/src/filters/Posterize.ts +0 -46
  79. package/debug/konva/src/filters/RGB.ts +0 -82
  80. package/debug/konva/src/filters/RGBA.ts +0 -103
  81. package/debug/konva/src/filters/Sepia.ts +0 -27
  82. package/debug/konva/src/filters/Solarize.ts +0 -29
  83. package/debug/konva/src/filters/Threshold.ts +0 -44
  84. package/debug/konva/src/index.ts +0 -3
  85. package/debug/konva/src/shapes/Arc.ts +0 -176
  86. package/debug/konva/src/shapes/Arrow.ts +0 -231
  87. package/debug/konva/src/shapes/Circle.ts +0 -76
  88. package/debug/konva/src/shapes/Ellipse.ts +0 -121
  89. package/debug/konva/src/shapes/Image.ts +0 -319
  90. package/debug/konva/src/shapes/Label.ts +0 -386
  91. package/debug/konva/src/shapes/Line.ts +0 -364
  92. package/debug/konva/src/shapes/Path.ts +0 -1013
  93. package/debug/konva/src/shapes/Rect.ts +0 -79
  94. package/debug/konva/src/shapes/RegularPolygon.ts +0 -167
  95. package/debug/konva/src/shapes/Ring.ts +0 -94
  96. package/debug/konva/src/shapes/Sprite.ts +0 -370
  97. package/debug/konva/src/shapes/Star.ts +0 -125
  98. package/debug/konva/src/shapes/Text.ts +0 -1065
  99. package/debug/konva/src/shapes/TextPath.ts +0 -583
  100. package/debug/konva/src/shapes/Transformer.ts +0 -1889
  101. package/debug/konva/src/shapes/Wedge.ts +0 -129
  102. package/debug/konva/src/skia-backend.ts +0 -35
  103. package/debug/konva/src/types.ts +0 -84
  104. package/debug/konva/tsconfig.json +0 -31
  105. package/debug/konva/tsconfig.test.json +0 -7
@@ -1,274 +0,0 @@
1
- import { Factory } from '../Factory.ts';
2
- import type { Filter } from '../Node.ts';
3
- import { Node } from '../Node.ts';
4
- import { Util } from '../Util.ts';
5
- import { getNumberValidator } from '../Validators.ts';
6
-
7
- /*
8
- * ToPolar Filter. Converts image data to polar coordinates. Performs
9
- * w*h*4 pixel reads and w*h pixel writes. The r axis is placed along
10
- * what would be the y axis and the theta axis along the x axis.
11
- * @function
12
- * @author ippo615
13
- * @memberof Konva.Filters
14
- * @param {ImageData} src, the source image data (what will be transformed)
15
- * @param {ImageData} dst, the destination image data (where it will be saved)
16
- * @param {Object} opt
17
- * @param {Number} [opt.polarCenterX] horizontal location for the center of the circle,
18
- * default is in the middle
19
- * @param {Number} [opt.polarCenterY] vertical location for the center of the circle,
20
- * default is in the middle
21
- */
22
-
23
- const ToPolar = function (src, dst, opt) {
24
- const srcPixels = src.data,
25
- dstPixels = dst.data,
26
- xSize = src.width,
27
- ySize = src.height,
28
- xMid = opt.polarCenterX || xSize / 2,
29
- yMid = opt.polarCenterY || ySize / 2;
30
-
31
- // Find the largest radius
32
- let rMax = Math.sqrt(xMid * xMid + yMid * yMid);
33
- let x = xSize - xMid;
34
- let y = ySize - yMid;
35
- const rad = Math.sqrt(x * x + y * y);
36
- rMax = rad > rMax ? rad : rMax;
37
-
38
- // We'll be uisng y as the radius, and x as the angle (theta=t)
39
- const rSize = ySize,
40
- tSize = xSize;
41
-
42
- // We want to cover all angles (0-360) and we need to convert to
43
- // radians (*PI/180)
44
- const conversion = ((360 / tSize) * Math.PI) / 180;
45
-
46
- // var x1, x2, x1i, x2i, y1, y2, y1i, y2i, scale;
47
-
48
- for (let theta = 0; theta < tSize; theta += 1) {
49
- const sin = Math.sin(theta * conversion);
50
- const cos = Math.cos(theta * conversion);
51
- for (let radius = 0; radius < rSize; radius += 1) {
52
- x = Math.floor(xMid + ((rMax * radius) / rSize) * cos);
53
- y = Math.floor(yMid + ((rMax * radius) / rSize) * sin);
54
- let i = (y * xSize + x) * 4;
55
- const r = srcPixels[i + 0];
56
- const g = srcPixels[i + 1];
57
- const b = srcPixels[i + 2];
58
- const a = srcPixels[i + 3];
59
-
60
- // Store it
61
- //i = (theta * xSize + radius) * 4;
62
- i = (theta + radius * xSize) * 4;
63
- dstPixels[i + 0] = r;
64
- dstPixels[i + 1] = g;
65
- dstPixels[i + 2] = b;
66
- dstPixels[i + 3] = a;
67
- }
68
- }
69
- };
70
-
71
- /*
72
- * FromPolar Filter. Converts image data from polar coordinates back to rectangular.
73
- * Performs w*h*4 pixel reads and w*h pixel writes.
74
- * @function
75
- * @author ippo615
76
- * @memberof Konva.Filters
77
- * @param {ImageData} src, the source image data (what will be transformed)
78
- * @param {ImageData} dst, the destination image data (where it will be saved)
79
- * @param {Object} opt
80
- * @param {Number} [opt.polarCenterX] horizontal location for the center of the circle,
81
- * default is in the middle
82
- * @param {Number} [opt.polarCenterY] vertical location for the center of the circle,
83
- * default is in the middle
84
- * @param {Number} [opt.polarRotation] amount to rotate the image counterclockwis,
85
- * 0 is no rotation, 360 degrees is a full rotation
86
- */
87
-
88
- const FromPolar = function (src, dst, opt) {
89
- const srcPixels = src.data,
90
- dstPixels = dst.data,
91
- xSize = src.width,
92
- ySize = src.height,
93
- xMid = opt.polarCenterX || xSize / 2,
94
- yMid = opt.polarCenterY || ySize / 2;
95
-
96
- // Find the largest radius
97
- let rMax = Math.sqrt(xMid * xMid + yMid * yMid);
98
- let x = xSize - xMid;
99
- let y = ySize - yMid;
100
- const rad = Math.sqrt(x * x + y * y);
101
- rMax = rad > rMax ? rad : rMax;
102
-
103
- // We'll be uisng x as the radius, and y as the angle (theta=t)
104
- const rSize = ySize,
105
- tSize = xSize,
106
- phaseShift = opt.polarRotation || 0;
107
-
108
- // We need to convert to degrees and we need to make sure
109
- // it's between (0-360)
110
- // var conversion = tSize/360*180/Math.PI;
111
- //var conversion = tSize/360*180/Math.PI;
112
-
113
- let x1, y1;
114
-
115
- for (x = 0; x < xSize; x += 1) {
116
- for (y = 0; y < ySize; y += 1) {
117
- const dx = x - xMid;
118
- const dy = y - yMid;
119
- const radius = (Math.sqrt(dx * dx + dy * dy) * rSize) / rMax;
120
- let theta =
121
- ((Math.atan2(dy, dx) * 180) / Math.PI + 360 + phaseShift) % 360;
122
- theta = (theta * tSize) / 360;
123
- x1 = Math.floor(theta);
124
- y1 = Math.floor(radius);
125
- let i = (y1 * xSize + x1) * 4;
126
- const r = srcPixels[i + 0];
127
- const g = srcPixels[i + 1];
128
- const b = srcPixels[i + 2];
129
- const a = srcPixels[i + 3];
130
-
131
- // Store it
132
- i = (y * xSize + x) * 4;
133
- dstPixels[i + 0] = r;
134
- dstPixels[i + 1] = g;
135
- dstPixels[i + 2] = b;
136
- dstPixels[i + 3] = a;
137
- }
138
- }
139
- };
140
-
141
- //Konva.Filters.ToPolar = Util._FilterWrapDoubleBuffer(ToPolar);
142
- //Konva.Filters.FromPolar = Util._FilterWrapDoubleBuffer(FromPolar);
143
-
144
- // create a temporary canvas for working - shared between multiple calls
145
-
146
- /*
147
- * Kaleidoscope Filter.
148
- * @function
149
- * @name Kaleidoscope
150
- * @author ippo615
151
- * @memberof Konva.Filters
152
- * @example
153
- * node.cache();
154
- * node.filters([Konva.Filters.Kaleidoscope]);
155
- * node.kaleidoscopePower(3);
156
- * node.kaleidoscopeAngle(45);
157
- */
158
- export const Kaleidoscope: Filter = function (imageData) {
159
- const xSize = imageData.width,
160
- ySize = imageData.height;
161
-
162
- let x, y, xoff, i, r, g, b, a, srcPos, dstPos;
163
- let power = Math.round(this.kaleidoscopePower());
164
- const angle = Math.round(this.kaleidoscopeAngle());
165
- const offset = Math.floor((xSize * (angle % 360)) / 360);
166
-
167
- if (power < 1) {
168
- return;
169
- }
170
-
171
- // Work with our shared buffer canvas
172
- const tempCanvas = Util.createCanvasElement();
173
- tempCanvas.width = xSize;
174
- tempCanvas.height = ySize;
175
- const scratchData = tempCanvas
176
- .getContext('2d')!
177
- .getImageData(0, 0, xSize, ySize);
178
- Util.releaseCanvas(tempCanvas);
179
- // Convert thhe original to polar coordinates
180
- ToPolar(imageData, scratchData, {
181
- polarCenterX: xSize / 2,
182
- polarCenterY: ySize / 2,
183
- });
184
-
185
- // Determine how big each section will be, if it's too small
186
- // make it bigger
187
- let minSectionSize = xSize / Math.pow(2, power);
188
- while (minSectionSize <= 8) {
189
- minSectionSize = minSectionSize * 2;
190
- power -= 1;
191
- }
192
- minSectionSize = Math.ceil(minSectionSize);
193
- let sectionSize = minSectionSize;
194
-
195
- // Copy the offset region to 0
196
- // Depending on the size of filter and location of the offset we may need
197
- // to copy the section backwards to prevent it from rewriting itself
198
- let xStart = 0,
199
- xEnd = sectionSize,
200
- xDelta = 1;
201
- if (offset + minSectionSize > xSize) {
202
- xStart = sectionSize;
203
- xEnd = 0;
204
- xDelta = -1;
205
- }
206
- for (y = 0; y < ySize; y += 1) {
207
- for (x = xStart; x !== xEnd; x += xDelta) {
208
- xoff = Math.round(x + offset) % xSize;
209
- srcPos = (xSize * y + xoff) * 4;
210
- r = scratchData.data[srcPos + 0];
211
- g = scratchData.data[srcPos + 1];
212
- b = scratchData.data[srcPos + 2];
213
- a = scratchData.data[srcPos + 3];
214
- dstPos = (xSize * y + x) * 4;
215
- scratchData.data[dstPos + 0] = r;
216
- scratchData.data[dstPos + 1] = g;
217
- scratchData.data[dstPos + 2] = b;
218
- scratchData.data[dstPos + 3] = a;
219
- }
220
- }
221
-
222
- // Perform the actual effect
223
- for (y = 0; y < ySize; y += 1) {
224
- sectionSize = Math.floor(minSectionSize);
225
- for (i = 0; i < power; i += 1) {
226
- for (x = 0; x < sectionSize + 1; x += 1) {
227
- srcPos = (xSize * y + x) * 4;
228
- r = scratchData.data[srcPos + 0];
229
- g = scratchData.data[srcPos + 1];
230
- b = scratchData.data[srcPos + 2];
231
- a = scratchData.data[srcPos + 3];
232
- dstPos = (xSize * y + sectionSize * 2 - x - 1) * 4;
233
- scratchData.data[dstPos + 0] = r;
234
- scratchData.data[dstPos + 1] = g;
235
- scratchData.data[dstPos + 2] = b;
236
- scratchData.data[dstPos + 3] = a;
237
- }
238
- sectionSize *= 2;
239
- }
240
- }
241
-
242
- // Convert back from polar coordinates
243
- FromPolar(scratchData, imageData, { polarRotation: 0 });
244
- };
245
-
246
- /**
247
- * get/set kaleidoscope power. Use with {@link Konva.Filters.Kaleidoscope} filter.
248
- * @name Konva.Node#kaleidoscopePower
249
- * @method
250
- * @param {Integer} power of kaleidoscope
251
- * @returns {Integer}
252
- */
253
- Factory.addGetterSetter(
254
- Node,
255
- 'kaleidoscopePower',
256
- 2,
257
- getNumberValidator(),
258
- Factory.afterSetFilter
259
- );
260
-
261
- /**
262
- * get/set kaleidoscope angle. Use with {@link Konva.Filters.Kaleidoscope} filter.
263
- * @name Konva.Node#kaleidoscopeAngle
264
- * @method
265
- * @param {Integer} degrees
266
- * @returns {Integer}
267
- */
268
- Factory.addGetterSetter(
269
- Node,
270
- 'kaleidoscopeAngle',
271
- 0,
272
- getNumberValidator(),
273
- Factory.afterSetFilter
274
- );
@@ -1,220 +0,0 @@
1
- import { Factory } from '../Factory.ts';
2
- import type { Filter } from '../Node.ts';
3
- import { Node } from '../Node.ts';
4
- import { getNumberValidator } from '../Validators.ts';
5
-
6
- function pixelAt(idata, x: number, y: number) {
7
- let idx = (y * idata.width + x) * 4;
8
- const d: Array<number> = [];
9
- d.push(
10
- idata.data[idx++],
11
- idata.data[idx++],
12
- idata.data[idx++],
13
- idata.data[idx++]
14
- );
15
- return d;
16
- }
17
-
18
- function rgbDistance(p1, p2) {
19
- return Math.sqrt(
20
- Math.pow(p1[0] - p2[0], 2) +
21
- Math.pow(p1[1] - p2[1], 2) +
22
- Math.pow(p1[2] - p2[2], 2)
23
- );
24
- }
25
-
26
- function rgbMean(pTab) {
27
- const m = [0, 0, 0];
28
-
29
- for (let i = 0; i < pTab.length; i++) {
30
- m[0] += pTab[i][0];
31
- m[1] += pTab[i][1];
32
- m[2] += pTab[i][2];
33
- }
34
-
35
- m[0] /= pTab.length;
36
- m[1] /= pTab.length;
37
- m[2] /= pTab.length;
38
-
39
- return m;
40
- }
41
-
42
- function backgroundMask(idata, threshold) {
43
- const rgbv_no = pixelAt(idata, 0, 0);
44
- const rgbv_ne = pixelAt(idata, idata.width - 1, 0);
45
- const rgbv_so = pixelAt(idata, 0, idata.height - 1);
46
- const rgbv_se = pixelAt(idata, idata.width - 1, idata.height - 1);
47
-
48
- const thres = threshold || 10;
49
- if (
50
- rgbDistance(rgbv_no, rgbv_ne) < thres &&
51
- rgbDistance(rgbv_ne, rgbv_se) < thres &&
52
- rgbDistance(rgbv_se, rgbv_so) < thres &&
53
- rgbDistance(rgbv_so, rgbv_no) < thres
54
- ) {
55
- // Mean color
56
- const mean = rgbMean([rgbv_ne, rgbv_no, rgbv_se, rgbv_so]);
57
-
58
- // Mask based on color distance
59
- const mask: Array<number> = [];
60
- for (let i = 0; i < idata.width * idata.height; i++) {
61
- const d = rgbDistance(mean, [
62
- idata.data[i * 4],
63
- idata.data[i * 4 + 1],
64
- idata.data[i * 4 + 2],
65
- ]);
66
- mask[i] = d < thres ? 0 : 255;
67
- }
68
-
69
- return mask;
70
- }
71
- }
72
-
73
- function applyMask(idata, mask) {
74
- for (let i = 0; i < idata.width * idata.height; i++) {
75
- idata.data[4 * i + 3] = mask[i];
76
- }
77
- }
78
-
79
- function erodeMask(mask, sw, sh) {
80
- const weights = [1, 1, 1, 1, 0, 1, 1, 1, 1];
81
- const side = Math.round(Math.sqrt(weights.length));
82
- const halfSide = Math.floor(side / 2);
83
-
84
- const maskResult: Array<number> = [];
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
-
94
- if (scy >= 0 && scy < sh && scx >= 0 && scx < sw) {
95
- const srcOff = scy * sw + scx;
96
- const wt = weights[cy * side + cx];
97
-
98
- a += mask[srcOff] * wt;
99
- }
100
- }
101
- }
102
-
103
- maskResult[so] = a === 255 * 8 ? 255 : 0;
104
- }
105
- }
106
-
107
- return maskResult;
108
- }
109
-
110
- function dilateMask(mask, sw, sh) {
111
- const weights = [1, 1, 1, 1, 1, 1, 1, 1, 1];
112
- const side = Math.round(Math.sqrt(weights.length));
113
- const halfSide = Math.floor(side / 2);
114
-
115
- const maskResult: Array<number> = [];
116
- for (let y = 0; y < sh; y++) {
117
- for (let x = 0; x < sw; x++) {
118
- const so = y * sw + x;
119
- let a = 0;
120
- for (let cy = 0; cy < side; cy++) {
121
- for (let cx = 0; cx < side; cx++) {
122
- const scy = y + cy - halfSide;
123
- const scx = x + cx - halfSide;
124
-
125
- if (scy >= 0 && scy < sh && scx >= 0 && scx < sw) {
126
- const srcOff = scy * sw + scx;
127
- const wt = weights[cy * side + cx];
128
-
129
- a += mask[srcOff] * wt;
130
- }
131
- }
132
- }
133
-
134
- maskResult[so] = a >= 255 * 4 ? 255 : 0;
135
- }
136
- }
137
-
138
- return maskResult;
139
- }
140
-
141
- function smoothEdgeMask(mask, sw: number, sh: number) {
142
- const weights = [
143
- 1 / 9,
144
- 1 / 9,
145
- 1 / 9,
146
- 1 / 9,
147
- 1 / 9,
148
- 1 / 9,
149
- 1 / 9,
150
- 1 / 9,
151
- 1 / 9,
152
- ];
153
- const side = Math.round(Math.sqrt(weights.length));
154
- const halfSide = Math.floor(side / 2);
155
-
156
- const maskResult: Array<number> = [];
157
- for (let y = 0; y < sh; y++) {
158
- for (let x = 0; x < sw; x++) {
159
- const so = y * sw + x;
160
- let a = 0;
161
- for (let cy = 0; cy < side; cy++) {
162
- for (let cx = 0; cx < side; cx++) {
163
- const scy = y + cy - halfSide;
164
- const scx = x + cx - halfSide;
165
-
166
- if (scy >= 0 && scy < sh && scx >= 0 && scx < sw) {
167
- const srcOff = scy * sw + scx;
168
- const wt = weights[cy * side + cx];
169
-
170
- a += mask[srcOff] * wt;
171
- }
172
- }
173
- }
174
-
175
- maskResult[so] = a;
176
- }
177
- }
178
-
179
- return maskResult;
180
- }
181
-
182
- /**
183
- * Mask Filter
184
- * @function
185
- * @name Mask
186
- * @memberof Konva.Filters
187
- * @param {Object} imageData
188
- * @example
189
- * node.cache();
190
- * node.filters([Konva.Filters.Mask]);
191
- * node.threshold(200);
192
- */
193
- export const Mask: Filter = function (imageData) {
194
- // Detect pixels close to the background color
195
- const threshold = this.threshold();
196
- let mask = backgroundMask(imageData, threshold);
197
- if (mask) {
198
- // Erode
199
- mask = erodeMask(mask, imageData.width, imageData.height);
200
-
201
- // Dilate
202
- mask = dilateMask(mask, imageData.width, imageData.height);
203
-
204
- // Gradient
205
- mask = smoothEdgeMask(mask, imageData.width, imageData.height);
206
-
207
- // Apply mask
208
- applyMask(imageData, mask);
209
- }
210
-
211
- return imageData;
212
- };
213
-
214
- Factory.addGetterSetter(
215
- Node,
216
- 'threshold',
217
- 0,
218
- getNumberValidator(),
219
- Factory.afterSetFilter
220
- );
@@ -1,44 +0,0 @@
1
- import { Factory } from '../Factory.ts';
2
- import type { Filter } from '../Node.ts';
3
- import { Node } from '../Node.ts';
4
- import { getNumberValidator } from '../Validators.ts';
5
-
6
- /**
7
- * Noise Filter. Randomly adds or substracts to the color channels
8
- * @function
9
- * @name Noise
10
- * @memberof Konva.Filters
11
- * @param {Object} imageData
12
- * @author ippo615
13
- * @example
14
- * node.cache();
15
- * node.filters([Konva.Filters.Noise]);
16
- * node.noise(0.8);
17
- */
18
- export const Noise: Filter = function (imageData) {
19
- const amount = this.noise() * 255,
20
- data = imageData.data,
21
- nPixels = data.length,
22
- half = amount / 2;
23
-
24
- for (let i = 0; i < nPixels; i += 4) {
25
- data[i + 0] += half - 2 * half * Math.random();
26
- data[i + 1] += half - 2 * half * Math.random();
27
- data[i + 2] += half - 2 * half * Math.random();
28
- }
29
- };
30
-
31
- Factory.addGetterSetter(
32
- Node,
33
- 'noise',
34
- 0.2,
35
- getNumberValidator(),
36
- Factory.afterSetFilter
37
- );
38
- /**
39
- * get/set noise amount. Must be a value between 0 and 1. Use with {@link Konva.Filters.Noise} filter.
40
- * @name Konva.Node#noise
41
- * @method
42
- * @param {Number} noise
43
- * @returns {Number}
44
- */
@@ -1,107 +0,0 @@
1
- import { Factory } from '../Factory.ts';
2
- import { Util } from '../Util.ts';
3
- import type { Filter } from '../Node.ts';
4
- import { Node } from '../Node.ts';
5
- import { getNumberValidator } from '../Validators.ts';
6
-
7
- /**
8
- * Pixelate Filter. Averages groups of pixels and redraws
9
- * them as larger pixels
10
- * @function
11
- * @name Pixelate
12
- * @memberof Konva.Filters
13
- * @param {Object} imageData
14
- * @author ippo615
15
- * @example
16
- * node.cache();
17
- * node.filters([Konva.Filters.Pixelate]);
18
- * node.pixelSize(10);
19
- */
20
-
21
- export const Pixelate: Filter = function (imageData) {
22
- let pixelSize = Math.ceil(this.pixelSize()),
23
- width = imageData.width,
24
- height = imageData.height,
25
- //pixelsPerBin = pixelSize * pixelSize,
26
- nBinsX = Math.ceil(width / pixelSize),
27
- nBinsY = Math.ceil(height / pixelSize),
28
- data = imageData.data;
29
-
30
- if (pixelSize <= 0) {
31
- Util.error('pixelSize value can not be <= 0');
32
- return;
33
- }
34
-
35
- for (let xBin = 0; xBin < nBinsX; xBin += 1) {
36
- for (let yBin = 0; yBin < nBinsY; yBin += 1) {
37
- // Initialize the color accumlators to 0
38
- let red = 0;
39
- let green = 0;
40
- let blue = 0;
41
- let alpha = 0;
42
-
43
- // Determine which pixels are included in this bin
44
- const xBinStart = xBin * pixelSize;
45
- const xBinEnd = xBinStart + pixelSize;
46
- const yBinStart = yBin * pixelSize;
47
- const yBinEnd = yBinStart + pixelSize;
48
-
49
- // Add all of the pixels to this bin!
50
- let pixelsInBin = 0;
51
- for (let x = xBinStart; x < xBinEnd; x += 1) {
52
- if (x >= width) {
53
- continue;
54
- }
55
- for (let y = yBinStart; y < yBinEnd; y += 1) {
56
- if (y >= height) {
57
- continue;
58
- }
59
- const i = (width * y + x) * 4;
60
- red += data[i + 0];
61
- green += data[i + 1];
62
- blue += data[i + 2];
63
- alpha += data[i + 3];
64
- pixelsInBin += 1;
65
- }
66
- }
67
-
68
- // Make sure the channels are between 0-255
69
- red = red / pixelsInBin;
70
- green = green / pixelsInBin;
71
- blue = blue / pixelsInBin;
72
- alpha = alpha / pixelsInBin;
73
-
74
- // Draw this bin
75
- for (let x = xBinStart; x < xBinEnd; x += 1) {
76
- if (x >= width) {
77
- continue;
78
- }
79
- for (let y = yBinStart; y < yBinEnd; y += 1) {
80
- if (y >= height) {
81
- continue;
82
- }
83
- const i = (width * y + x) * 4;
84
- data[i + 0] = red;
85
- data[i + 1] = green;
86
- data[i + 2] = blue;
87
- data[i + 3] = alpha;
88
- }
89
- }
90
- }
91
- }
92
- };
93
-
94
- Factory.addGetterSetter(
95
- Node,
96
- 'pixelSize',
97
- 8,
98
- getNumberValidator(),
99
- Factory.afterSetFilter
100
- );
101
- /**
102
- * get/set pixel size. Use with {@link Konva.Filters.Pixelate} filter.
103
- * @name Konva.Node#pixelSize
104
- * @method
105
- * @param {Integer} pixelSize
106
- * @returns {Integer}
107
- */
@@ -1,46 +0,0 @@
1
- import { Factory } from '../Factory.ts';
2
- import type { Filter } from '../Node.ts';
3
- import { Node } from '../Node.ts';
4
- import { getNumberValidator } from '../Validators.ts';
5
-
6
- /**
7
- * Posterize Filter. Adjusts the channels so that there are no more
8
- * than n different values for that channel. This is also applied
9
- * to the alpha channel.
10
- * @function
11
- * @name Posterize
12
- * @author ippo615
13
- * @memberof Konva.Filters
14
- * @param {Object} imageData
15
- * @example
16
- * node.cache();
17
- * node.filters([Konva.Filters.Posterize]);
18
- * node.levels(0.8); // between 0 and 1
19
- */
20
- export const Posterize: Filter = function (imageData) {
21
- // level must be between 1 and 255
22
- const levels = Math.round(this.levels() * 254) + 1,
23
- data = imageData.data,
24
- len = data.length,
25
- scale = 255 / levels;
26
-
27
- for (let i = 0; i < len; i += 1) {
28
- data[i] = Math.floor(data[i] / scale) * scale;
29
- }
30
- };
31
-
32
- Factory.addGetterSetter(
33
- Node,
34
- 'levels',
35
- 0.5,
36
- getNumberValidator(),
37
- Factory.afterSetFilter
38
- );
39
-
40
- /**
41
- * get/set levels. Must be a number between 0 and 1. Use with {@link Konva.Filters.Posterize} filter.
42
- * @name Konva.Node#levels
43
- * @method
44
- * @param {Number} level between 0 and 1
45
- * @returns {Number}
46
- */