konva 10.3.3 → 10.4.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.
Files changed (123) hide show
  1. package/README.md +1 -1
  2. package/konva.js +1327 -1572
  3. package/konva.min.js +4 -4
  4. package/lib/Animation.d.ts +63 -3
  5. package/lib/Animation.js +86 -65
  6. package/lib/BezierFunctions.d.ts +10 -0
  7. package/lib/BezierFunctions.js +32 -0
  8. package/lib/Canvas.d.ts +53 -0
  9. package/lib/Canvas.js +93 -34
  10. package/lib/Container.d.ts +156 -9
  11. package/lib/Container.js +260 -25
  12. package/lib/Context.d.ts +247 -0
  13. package/lib/Context.js +312 -74
  14. package/lib/Core.js +1 -0
  15. package/lib/DragAndDrop.d.ts +1 -0
  16. package/lib/DragAndDrop.js +66 -17
  17. package/lib/Factory.d.ts +24 -1
  18. package/lib/Factory.js +14 -20
  19. package/lib/FastLayer.d.ts +14 -1
  20. package/lib/FastLayer.js +14 -1
  21. package/lib/Global.d.ts +114 -3
  22. package/lib/Global.js +134 -5
  23. package/lib/Group.d.ts +11 -0
  24. package/lib/Group.js +11 -0
  25. package/lib/Layer.d.ts +128 -3
  26. package/lib/Layer.js +260 -49
  27. package/lib/Node.d.ts +692 -30
  28. package/lib/Node.js +1534 -202
  29. package/lib/PointerEvents.d.ts +2 -1
  30. package/lib/PointerEvents.js +19 -1
  31. package/lib/Shape.d.ts +99 -14
  32. package/lib/Shape.js +1101 -67
  33. package/lib/Stage.d.ts +81 -1
  34. package/lib/Stage.js +166 -7
  35. package/lib/Tween.d.ts +159 -4
  36. package/lib/Tween.js +243 -24
  37. package/lib/Util.d.ts +147 -4
  38. package/lib/Util.js +332 -82
  39. package/lib/Validators.d.ts +0 -2
  40. package/lib/Validators.js +3 -25
  41. package/lib/_CoreInternals.d.ts +10 -6
  42. package/lib/_CoreInternals.js +1 -0
  43. package/lib/_FullInternals.d.ts +15 -6
  44. package/lib/_FullInternals.js +7 -0
  45. package/lib/canvas-backend.js +6 -0
  46. package/lib/filters/Blur.d.ts +18 -0
  47. package/lib/filters/Blur.js +97 -24
  48. package/lib/filters/Brighten.d.ts +21 -0
  49. package/lib/filters/Brighten.js +24 -0
  50. package/lib/filters/Brightness.d.ts +11 -0
  51. package/lib/filters/Brightness.js +16 -0
  52. package/lib/filters/Contrast.d.ts +10 -0
  53. package/lib/filters/Contrast.js +21 -0
  54. package/lib/filters/Emboss.d.ts +20 -0
  55. package/lib/filters/Emboss.js +68 -8
  56. package/lib/filters/Enhance.d.ts +14 -0
  57. package/lib/filters/Enhance.js +34 -0
  58. package/lib/filters/Grayscale.d.ts +9 -0
  59. package/lib/filters/Grayscale.js +12 -0
  60. package/lib/filters/HSL.d.ts +17 -0
  61. package/lib/filters/HSL.js +48 -5
  62. package/lib/filters/HSV.d.ts +18 -0
  63. package/lib/filters/HSV.js +49 -5
  64. package/lib/filters/Invert.d.ts +9 -0
  65. package/lib/filters/Invert.js +12 -0
  66. package/lib/filters/Kaleidoscope.d.ts +12 -0
  67. package/lib/filters/Kaleidoscope.js +83 -0
  68. package/lib/filters/Mask.d.ts +11 -0
  69. package/lib/filters/Mask.js +18 -0
  70. package/lib/filters/Noise.d.ts +19 -0
  71. package/lib/filters/Noise.js +19 -0
  72. package/lib/filters/Pixelate.d.ts +20 -0
  73. package/lib/filters/Pixelate.js +28 -1
  74. package/lib/filters/Posterize.d.ts +21 -0
  75. package/lib/filters/Posterize.js +22 -0
  76. package/lib/filters/RGB.d.ts +21 -0
  77. package/lib/filters/RGB.js +43 -28
  78. package/lib/filters/RGBA.d.ts +23 -0
  79. package/lib/filters/RGBA.js +28 -41
  80. package/lib/filters/Sepia.d.ts +9 -0
  81. package/lib/filters/Sepia.js +10 -0
  82. package/lib/filters/Solarize.d.ts +10 -0
  83. package/lib/filters/Solarize.js +11 -0
  84. package/lib/filters/Threshold.d.ts +22 -0
  85. package/lib/filters/Threshold.js +22 -0
  86. package/lib/shapes/Arc.d.ts +40 -0
  87. package/lib/shapes/Arc.js +89 -6
  88. package/lib/shapes/Arrow.d.ts +24 -1
  89. package/lib/shapes/Arrow.js +83 -0
  90. package/lib/shapes/Circle.d.ts +18 -0
  91. package/lib/shapes/Circle.js +36 -9
  92. package/lib/shapes/Ellipse.d.ts +34 -2
  93. package/lib/shapes/Ellipse.js +67 -9
  94. package/lib/shapes/Image.d.ts +49 -0
  95. package/lib/shapes/Image.js +151 -22
  96. package/lib/shapes/Label.d.ts +65 -1
  97. package/lib/shapes/Label.js +113 -20
  98. package/lib/shapes/Line.d.ts +50 -8
  99. package/lib/shapes/Line.js +116 -20
  100. package/lib/shapes/Path.d.ts +40 -6
  101. package/lib/shapes/Path.js +187 -128
  102. package/lib/shapes/Rect.d.ts +18 -0
  103. package/lib/shapes/Rect.js +36 -0
  104. package/lib/shapes/RegularPolygon.d.ts +22 -0
  105. package/lib/shapes/RegularPolygon.js +70 -0
  106. package/lib/shapes/Ring.d.ts +19 -0
  107. package/lib/shapes/Ring.js +51 -5
  108. package/lib/shapes/Sprite.d.ts +78 -0
  109. package/lib/shapes/Sprite.js +238 -25
  110. package/lib/shapes/Star.d.ts +23 -0
  111. package/lib/shapes/Star.js +62 -0
  112. package/lib/shapes/Text.d.ts +59 -1
  113. package/lib/shapes/Text.js +481 -97
  114. package/lib/shapes/TextPath.d.ts +49 -3
  115. package/lib/shapes/TextPath.js +278 -34
  116. package/lib/shapes/Transformer.d.ts +108 -11
  117. package/lib/shapes/Transformer.js +744 -102
  118. package/lib/shapes/Wedge.d.ts +22 -0
  119. package/lib/shapes/Wedge.js +67 -3
  120. package/lib/skia-backend.js +9 -0
  121. package/lib/types.d.ts +2 -23
  122. package/lib/types.js +1 -21
  123. package/package.json +12 -16
@@ -2,15 +2,35 @@ import { Factory } from "../Factory.js";
2
2
  import { Node } from "../Node.js";
3
3
  import { Util } from "../Util.js";
4
4
  import { getNumberValidator } from "../Validators.js";
5
+ /*
6
+ * ToPolar Filter. Converts image data to polar coordinates. Performs
7
+ * w*h*4 pixel reads and w*h pixel writes. The r axis is placed along
8
+ * what would be the y axis and the theta axis along the x axis.
9
+ * @function
10
+ * @author ippo615
11
+ * @memberof Konva.Filters
12
+ * @param {ImageData} src, the source image data (what will be transformed)
13
+ * @param {ImageData} dst, the destination image data (where it will be saved)
14
+ * @param {Object} opt
15
+ * @param {Number} [opt.polarCenterX] horizontal location for the center of the circle,
16
+ * default is in the middle
17
+ * @param {Number} [opt.polarCenterY] vertical location for the center of the circle,
18
+ * default is in the middle
19
+ */
5
20
  const ToPolar = function (src, dst, opt) {
6
21
  const srcPixels = src.data, dstPixels = dst.data, xSize = src.width, ySize = src.height, xMid = opt.polarCenterX || xSize / 2, yMid = opt.polarCenterY || ySize / 2;
22
+ // Find the largest radius
7
23
  let rMax = Math.sqrt(xMid * xMid + yMid * yMid);
8
24
  let x = xSize - xMid;
9
25
  let y = ySize - yMid;
10
26
  const rad = Math.sqrt(x * x + y * y);
11
27
  rMax = rad > rMax ? rad : rMax;
28
+ // We'll be uisng y as the radius, and x as the angle (theta=t)
12
29
  const rSize = ySize, tSize = xSize;
30
+ // We want to cover all angles (0-360) and we need to convert to
31
+ // radians (*PI/180)
13
32
  const conversion = ((360 / tSize) * Math.PI) / 180;
33
+ // var x1, x2, x1i, x2i, y1, y2, y1i, y2i, scale;
14
34
  for (let theta = 0; theta < tSize; theta += 1) {
15
35
  const sin = Math.sin(theta * conversion);
16
36
  const cos = Math.cos(theta * conversion);
@@ -22,6 +42,8 @@ const ToPolar = function (src, dst, opt) {
22
42
  const g = srcPixels[i + 1];
23
43
  const b = srcPixels[i + 2];
24
44
  const a = srcPixels[i + 3];
45
+ // Store it
46
+ //i = (theta * xSize + radius) * 4;
25
47
  i = (theta + radius * xSize) * 4;
26
48
  dstPixels[i + 0] = r;
27
49
  dstPixels[i + 1] = g;
@@ -30,14 +52,36 @@ const ToPolar = function (src, dst, opt) {
30
52
  }
31
53
  }
32
54
  };
55
+ /*
56
+ * FromPolar Filter. Converts image data from polar coordinates back to rectangular.
57
+ * Performs w*h*4 pixel reads and w*h pixel writes.
58
+ * @function
59
+ * @author ippo615
60
+ * @memberof Konva.Filters
61
+ * @param {ImageData} src, the source image data (what will be transformed)
62
+ * @param {ImageData} dst, the destination image data (where it will be saved)
63
+ * @param {Object} opt
64
+ * @param {Number} [opt.polarCenterX] horizontal location for the center of the circle,
65
+ * default is in the middle
66
+ * @param {Number} [opt.polarCenterY] vertical location for the center of the circle,
67
+ * default is in the middle
68
+ * @param {Number} [opt.polarRotation] amount to rotate the image counterclockwis,
69
+ * 0 is no rotation, 360 degrees is a full rotation
70
+ */
33
71
  const FromPolar = function (src, dst, opt) {
34
72
  const srcPixels = src.data, dstPixels = dst.data, xSize = src.width, ySize = src.height, xMid = opt.polarCenterX || xSize / 2, yMid = opt.polarCenterY || ySize / 2;
73
+ // Find the largest radius
35
74
  let rMax = Math.sqrt(xMid * xMid + yMid * yMid);
36
75
  let x = xSize - xMid;
37
76
  let y = ySize - yMid;
38
77
  const rad = Math.sqrt(x * x + y * y);
39
78
  rMax = rad > rMax ? rad : rMax;
79
+ // We'll be uisng x as the radius, and y as the angle (theta=t)
40
80
  const rSize = ySize, tSize = xSize, phaseShift = opt.polarRotation || 0;
81
+ // We need to convert to degrees and we need to make sure
82
+ // it's between (0-360)
83
+ // var conversion = tSize/360*180/Math.PI;
84
+ //var conversion = tSize/360*180/Math.PI;
41
85
  let x1, y1;
42
86
  for (x = 0; x < xSize; x += 1) {
43
87
  for (y = 0; y < ySize; y += 1) {
@@ -53,6 +97,7 @@ const FromPolar = function (src, dst, opt) {
53
97
  const g = srcPixels[i + 1];
54
98
  const b = srcPixels[i + 2];
55
99
  const a = srcPixels[i + 3];
100
+ // Store it
56
101
  i = (y * xSize + x) * 4;
57
102
  dstPixels[i + 0] = r;
58
103
  dstPixels[i + 1] = g;
@@ -61,6 +106,21 @@ const FromPolar = function (src, dst, opt) {
61
106
  }
62
107
  }
63
108
  };
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
+ /**
113
+ * Kaleidoscope Filter.
114
+ * @function
115
+ * @name Kaleidoscope
116
+ * @author ippo615
117
+ * @memberof Konva.Filters
118
+ * @example
119
+ * node.cache();
120
+ * node.filters([Konva.Filters.Kaleidoscope]);
121
+ * node.kaleidoscopePower(3);
122
+ * node.kaleidoscopeAngle(45);
123
+ */
64
124
  export const Kaleidoscope = function (imageData) {
65
125
  const xSize = imageData.width, ySize = imageData.height;
66
126
  let x, y, xoff, i, r, g, b, a, srcPos, dstPos;
@@ -70,6 +130,7 @@ export const Kaleidoscope = function (imageData) {
70
130
  if (power < 1) {
71
131
  return;
72
132
  }
133
+ // Work with our shared buffer canvas
73
134
  const tempCanvas = Util.createCanvasElement();
74
135
  tempCanvas.width = xSize;
75
136
  tempCanvas.height = ySize;
@@ -77,10 +138,13 @@ export const Kaleidoscope = function (imageData) {
77
138
  .getContext('2d')
78
139
  .getImageData(0, 0, xSize, ySize);
79
140
  Util.releaseCanvas(tempCanvas);
141
+ // Convert thhe original to polar coordinates
80
142
  ToPolar(imageData, scratchData, {
81
143
  polarCenterX: xSize / 2,
82
144
  polarCenterY: ySize / 2,
83
145
  });
146
+ // Determine how big each section will be, if it's too small
147
+ // make it bigger
84
148
  let minSectionSize = xSize / Math.pow(2, power);
85
149
  while (minSectionSize <= 8) {
86
150
  minSectionSize = minSectionSize * 2;
@@ -88,6 +152,9 @@ export const Kaleidoscope = function (imageData) {
88
152
  }
89
153
  minSectionSize = Math.ceil(minSectionSize);
90
154
  let sectionSize = minSectionSize;
155
+ // Copy the offset region to 0
156
+ // Depending on the size of filter and location of the offset we may need
157
+ // to copy the section backwards to prevent it from rewriting itself
91
158
  let xStart = 0, xEnd = sectionSize, xDelta = 1;
92
159
  if (offset + minSectionSize > xSize) {
93
160
  xStart = sectionSize;
@@ -109,6 +176,7 @@ export const Kaleidoscope = function (imageData) {
109
176
  scratchData.data[dstPos + 3] = a;
110
177
  }
111
178
  }
179
+ // Perform the actual effect
112
180
  for (y = 0; y < ySize; y += 1) {
113
181
  sectionSize = Math.floor(minSectionSize);
114
182
  for (i = 0; i < power; i += 1) {
@@ -127,7 +195,22 @@ export const Kaleidoscope = function (imageData) {
127
195
  sectionSize *= 2;
128
196
  }
129
197
  }
198
+ // Convert back from polar coordinates
130
199
  FromPolar(scratchData, imageData, { polarRotation: 0 });
131
200
  };
201
+ /**
202
+ * get/set kaleidoscope power. Use with {@link Konva.Filters.Kaleidoscope} filter.
203
+ * @name Konva.Node#kaleidoscopePower
204
+ * @method
205
+ * @param {Integer} power of kaleidoscope
206
+ * @returns {Integer}
207
+ */
132
208
  Factory.addGetterSetter(Node, 'kaleidoscopePower', 2, getNumberValidator(), Factory.afterSetFilter);
209
+ /**
210
+ * get/set kaleidoscope angle. Use with {@link Konva.Filters.Kaleidoscope} filter.
211
+ * @name Konva.Node#kaleidoscopeAngle
212
+ * @method
213
+ * @param {Integer} degrees
214
+ * @returns {Integer}
215
+ */
133
216
  Factory.addGetterSetter(Node, 'kaleidoscopeAngle', 0, getNumberValidator(), Factory.afterSetFilter);
@@ -1,2 +1,13 @@
1
1
  import type { Filter } from '../Node.ts';
2
+ /**
3
+ * Mask Filter
4
+ * @function
5
+ * @name Mask
6
+ * @memberof Konva.Filters
7
+ * @param {Object} imageData
8
+ * @example
9
+ * node.cache();
10
+ * node.filters([Konva.Filters.Mask]);
11
+ * node.threshold(200);
12
+ */
2
13
  export declare const Mask: Filter;
@@ -34,7 +34,9 @@ function backgroundMask(idata, threshold) {
34
34
  rgbDistance(rgbv_ne, rgbv_se) < thres &&
35
35
  rgbDistance(rgbv_se, rgbv_so) < thres &&
36
36
  rgbDistance(rgbv_so, rgbv_no) < thres) {
37
+ // Mean color
37
38
  const mean = rgbMean([rgbv_ne, rgbv_no, rgbv_se, rgbv_so]);
39
+ // Mask based on color distance
38
40
  const mask = [];
39
41
  for (let i = 0; i < idata.width * idata.height; i++) {
40
42
  const d = rgbDistance(mean, [
@@ -137,13 +139,29 @@ function smoothEdgeMask(mask, sw, sh) {
137
139
  }
138
140
  return maskResult;
139
141
  }
142
+ /**
143
+ * Mask Filter
144
+ * @function
145
+ * @name Mask
146
+ * @memberof Konva.Filters
147
+ * @param {Object} imageData
148
+ * @example
149
+ * node.cache();
150
+ * node.filters([Konva.Filters.Mask]);
151
+ * node.threshold(200);
152
+ */
140
153
  export const Mask = function (imageData) {
154
+ // Detect pixels close to the background color
141
155
  const threshold = this.threshold();
142
156
  let mask = backgroundMask(imageData, threshold);
143
157
  if (mask) {
158
+ // Erode
144
159
  mask = erodeMask(mask, imageData.width, imageData.height);
160
+ // Dilate
145
161
  mask = dilateMask(mask, imageData.width, imageData.height);
162
+ // Gradient
146
163
  mask = smoothEdgeMask(mask, imageData.width, imageData.height);
164
+ // Apply mask
147
165
  applyMask(imageData, mask);
148
166
  }
149
167
  return imageData;
@@ -1,2 +1,21 @@
1
1
  import type { Filter } from '../Node.ts';
2
+ /**
3
+ * Noise Filter. Randomly adds or substracts to the color channels
4
+ * @function
5
+ * @name Noise
6
+ * @memberof Konva.Filters
7
+ * @param {Object} imageData
8
+ * @author ippo615
9
+ * @example
10
+ * node.cache();
11
+ * node.filters([Konva.Filters.Noise]);
12
+ * node.noise(0.8);
13
+ */
2
14
  export declare const Noise: Filter;
15
+ /**
16
+ * get/set noise amount. Must be a value between 0 and 1. Use with {@link Konva.Filters.Noise} filter.
17
+ * @name Konva.Node#noise
18
+ * @method
19
+ * @param {Number} noise
20
+ * @returns {Number}
21
+ */
@@ -1,6 +1,18 @@
1
1
  import { Factory } from "../Factory.js";
2
2
  import { Node } from "../Node.js";
3
3
  import { getNumberValidator } from "../Validators.js";
4
+ /**
5
+ * Noise Filter. Randomly adds or substracts to the color channels
6
+ * @function
7
+ * @name Noise
8
+ * @memberof Konva.Filters
9
+ * @param {Object} imageData
10
+ * @author ippo615
11
+ * @example
12
+ * node.cache();
13
+ * node.filters([Konva.Filters.Noise]);
14
+ * node.noise(0.8);
15
+ */
4
16
  export const Noise = function (imageData) {
5
17
  const amount = this.noise() * 255, data = imageData.data, nPixels = data.length, half = amount / 2;
6
18
  for (let i = 0; i < nPixels; i += 4) {
@@ -10,3 +22,10 @@ export const Noise = function (imageData) {
10
22
  }
11
23
  };
12
24
  Factory.addGetterSetter(Node, 'noise', 0.2, getNumberValidator(), Factory.afterSetFilter);
25
+ /**
26
+ * get/set noise amount. Must be a value between 0 and 1. Use with {@link Konva.Filters.Noise} filter.
27
+ * @name Konva.Node#noise
28
+ * @method
29
+ * @param {Number} noise
30
+ * @returns {Number}
31
+ */
@@ -1,2 +1,22 @@
1
1
  import type { Filter } from '../Node.ts';
2
+ /**
3
+ * Pixelate Filter. Averages groups of pixels and redraws
4
+ * them as larger pixels
5
+ * @function
6
+ * @name Pixelate
7
+ * @memberof Konva.Filters
8
+ * @param {Object} imageData
9
+ * @author ippo615
10
+ * @example
11
+ * node.cache();
12
+ * node.filters([Konva.Filters.Pixelate]);
13
+ * node.pixelSize(10);
14
+ */
2
15
  export declare const Pixelate: Filter;
16
+ /**
17
+ * get/set pixel size. Use with {@link Konva.Filters.Pixelate} filter.
18
+ * @name Konva.Node#pixelSize
19
+ * @method
20
+ * @param {Integer} pixelSize
21
+ * @returns {Integer}
22
+ */
@@ -2,22 +2,40 @@ import { Factory } from "../Factory.js";
2
2
  import { Util } from "../Util.js";
3
3
  import { Node } from "../Node.js";
4
4
  import { getNumberValidator } from "../Validators.js";
5
+ /**
6
+ * Pixelate Filter. Averages groups of pixels and redraws
7
+ * them as larger pixels
8
+ * @function
9
+ * @name Pixelate
10
+ * @memberof Konva.Filters
11
+ * @param {Object} imageData
12
+ * @author ippo615
13
+ * @example
14
+ * node.cache();
15
+ * node.filters([Konva.Filters.Pixelate]);
16
+ * node.pixelSize(10);
17
+ */
5
18
  export const Pixelate = function (imageData) {
6
- let pixelSize = Math.ceil(this.pixelSize()), width = imageData.width, height = imageData.height, nBinsX = Math.ceil(width / pixelSize), nBinsY = Math.ceil(height / pixelSize), data = imageData.data;
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;
7
22
  if (pixelSize <= 0) {
8
23
  Util.error('pixelSize value can not be <= 0');
9
24
  return;
10
25
  }
11
26
  for (let xBin = 0; xBin < nBinsX; xBin += 1) {
12
27
  for (let yBin = 0; yBin < nBinsY; yBin += 1) {
28
+ // Initialize the color accumlators to 0
13
29
  let red = 0;
14
30
  let green = 0;
15
31
  let blue = 0;
16
32
  let alpha = 0;
33
+ // Determine which pixels are included in this bin
17
34
  const xBinStart = xBin * pixelSize;
18
35
  const xBinEnd = xBinStart + pixelSize;
19
36
  const yBinStart = yBin * pixelSize;
20
37
  const yBinEnd = yBinStart + pixelSize;
38
+ // Add all of the pixels to this bin!
21
39
  let pixelsInBin = 0;
22
40
  for (let x = xBinStart; x < xBinEnd; x += 1) {
23
41
  if (x >= width) {
@@ -35,10 +53,12 @@ export const Pixelate = function (imageData) {
35
53
  pixelsInBin += 1;
36
54
  }
37
55
  }
56
+ // Make sure the channels are between 0-255
38
57
  red = red / pixelsInBin;
39
58
  green = green / pixelsInBin;
40
59
  blue = blue / pixelsInBin;
41
60
  alpha = alpha / pixelsInBin;
61
+ // Draw this bin
42
62
  for (let x = xBinStart; x < xBinEnd; x += 1) {
43
63
  if (x >= width) {
44
64
  continue;
@@ -58,3 +78,10 @@ export const Pixelate = function (imageData) {
58
78
  }
59
79
  };
60
80
  Factory.addGetterSetter(Node, 'pixelSize', 8, getNumberValidator(), Factory.afterSetFilter);
81
+ /**
82
+ * get/set pixel size. Use with {@link Konva.Filters.Pixelate} filter.
83
+ * @name Konva.Node#pixelSize
84
+ * @method
85
+ * @param {Integer} pixelSize
86
+ * @returns {Integer}
87
+ */
@@ -1,2 +1,23 @@
1
1
  import type { Filter } from '../Node.ts';
2
+ /**
3
+ * Posterize Filter. Adjusts the channels so that there are no more
4
+ * than n different values for that channel. This is also applied
5
+ * to the alpha channel.
6
+ * @function
7
+ * @name Posterize
8
+ * @author ippo615
9
+ * @memberof Konva.Filters
10
+ * @param {Object} imageData
11
+ * @example
12
+ * node.cache();
13
+ * node.filters([Konva.Filters.Posterize]);
14
+ * node.levels(0.8); // between 0 and 1
15
+ */
2
16
  export declare const Posterize: Filter;
17
+ /**
18
+ * get/set levels. Must be a number between 0 and 1. Use with {@link Konva.Filters.Posterize} filter.
19
+ * @name Konva.Node#levels
20
+ * @method
21
+ * @param {Number} level between 0 and 1
22
+ * @returns {Number}
23
+ */
@@ -1,10 +1,32 @@
1
1
  import { Factory } from "../Factory.js";
2
2
  import { Node } from "../Node.js";
3
3
  import { getNumberValidator } from "../Validators.js";
4
+ /**
5
+ * Posterize Filter. Adjusts the channels so that there are no more
6
+ * than n different values for that channel. This is also applied
7
+ * to the alpha channel.
8
+ * @function
9
+ * @name Posterize
10
+ * @author ippo615
11
+ * @memberof Konva.Filters
12
+ * @param {Object} imageData
13
+ * @example
14
+ * node.cache();
15
+ * node.filters([Konva.Filters.Posterize]);
16
+ * node.levels(0.8); // between 0 and 1
17
+ */
4
18
  export const Posterize = function (imageData) {
19
+ // level must be between 1 and 255
5
20
  const levels = Math.round(this.levels() * 254) + 1, data = imageData.data, len = data.length, scale = 255 / levels;
6
21
  for (let i = 0; i < len; i += 1) {
7
22
  data[i] = Math.floor(data[i] / scale) * scale;
8
23
  }
9
24
  };
10
25
  Factory.addGetterSetter(Node, 'levels', 0.5, getNumberValidator(), Factory.afterSetFilter);
26
+ /**
27
+ * get/set levels. Must be a number between 0 and 1. Use with {@link Konva.Filters.Posterize} filter.
28
+ * @name Konva.Node#levels
29
+ * @method
30
+ * @param {Number} level between 0 and 1
31
+ * @returns {Number}
32
+ */
@@ -1,2 +1,23 @@
1
1
  import type { Filter } from '../Node.ts';
2
+ /**
3
+ * RGB Filter
4
+ * @function
5
+ * @name RGB
6
+ * @memberof Konva.Filters
7
+ * @param {Object} imageData
8
+ * @author ippo615
9
+ * @example
10
+ * node.cache();
11
+ * node.filters([Konva.Filters.RGB]);
12
+ * node.blue(120);
13
+ * node.green(200);
14
+ */
2
15
  export declare const RGB: Filter;
16
+ /**
17
+ * get/set filter blue value. Use with {@link Konva.Filters.RGB} or {@link Konva.Filters.RGBA} filter.
18
+ * @name blue
19
+ * @method
20
+ * @memberof Konva.Node.prototype
21
+ * @param {Integer} blue value between 0 and 255
22
+ * @returns {Integer}
23
+ */
@@ -1,38 +1,53 @@
1
1
  import { Factory } from "../Factory.js";
2
2
  import { Node } from "../Node.js";
3
3
  import { RGBComponent } from "../Validators.js";
4
+ /**
5
+ * RGB Filter
6
+ * @function
7
+ * @name RGB
8
+ * @memberof Konva.Filters
9
+ * @param {Object} imageData
10
+ * @author ippo615
11
+ * @example
12
+ * node.cache();
13
+ * node.filters([Konva.Filters.RGB]);
14
+ * node.blue(120);
15
+ * node.green(200);
16
+ */
4
17
  export const RGB = function (imageData) {
5
18
  const data = imageData.data, nPixels = data.length, red = this.red(), green = this.green(), blue = this.blue();
6
19
  for (let i = 0; i < nPixels; i += 4) {
7
20
  const brightness = (0.34 * data[i] + 0.5 * data[i + 1] + 0.16 * data[i + 2]) / 255;
8
- data[i] = brightness * red;
9
- data[i + 1] = brightness * green;
10
- data[i + 2] = brightness * blue;
11
- data[i + 3] = data[i + 3];
21
+ data[i] = brightness * red; // r
22
+ data[i + 1] = brightness * green; // g
23
+ data[i + 2] = brightness * blue; // b
24
+ data[i + 3] = data[i + 3]; // alpha
12
25
  }
13
26
  };
14
- Factory.addGetterSetter(Node, 'red', 0, function (val) {
15
- this._filterUpToDate = false;
16
- if (val > 255) {
17
- return 255;
18
- }
19
- else if (val < 0) {
20
- return 0;
21
- }
22
- else {
23
- return Math.round(val);
24
- }
25
- });
26
- Factory.addGetterSetter(Node, 'green', 0, function (val) {
27
- this._filterUpToDate = false;
28
- if (val > 255) {
29
- return 255;
30
- }
31
- else if (val < 0) {
32
- return 0;
33
- }
34
- else {
35
- return Math.round(val);
36
- }
37
- });
27
+ Factory.addGetterSetter(Node, 'red', 0, RGBComponent, Factory.afterSetFilter);
28
+ /**
29
+ * get/set filter red value. Use with {@link Konva.Filters.RGB} or {@link Konva.Filters.RGBA} filter.
30
+ * @name red
31
+ * @method
32
+ * @memberof Konva.Node.prototype
33
+ * @param {Integer} red value between 0 and 255
34
+ * @returns {Integer}
35
+ */
36
+ Factory.addGetterSetter(Node, 'green', 0, RGBComponent, Factory.afterSetFilter);
37
+ /**
38
+ * get/set filter green value. Use with {@link Konva.Filters.RGB} or {@link Konva.Filters.RGBA} filter.
39
+ * @name green
40
+ * @method
41
+ * @memberof Konva.Node.prototype
42
+ * @param {Integer} green value between 0 and 255
43
+ * @returns {Integer}
44
+ */
38
45
  Factory.addGetterSetter(Node, 'blue', 0, RGBComponent, Factory.afterSetFilter);
46
+ /**
47
+ * get/set filter blue value. Use with {@link Konva.Filters.RGB} or {@link Konva.Filters.RGBA} filter.
48
+ * @name blue
49
+ * @method
50
+ * @memberof Konva.Node.prototype
51
+ * @param {Integer} blue value between 0 and 255
52
+ * @returns {Integer}
53
+ */
@@ -1,2 +1,25 @@
1
1
  import type { Filter } from '../Node.ts';
2
+ import './RGB.ts';
3
+ /**
4
+ * RGBA Filter
5
+ * @function
6
+ * @name RGBA
7
+ * @memberof Konva.Filters
8
+ * @param {Object} imageData
9
+ * @author codefo
10
+ * @example
11
+ * node.cache();
12
+ * node.filters([Konva.Filters.RGBA]);
13
+ * node.blue(120);
14
+ * node.green(200);
15
+ * node.alpha(0.3);
16
+ */
2
17
  export declare const RGBA: Filter;
18
+ /**
19
+ * get/set filter alpha value. Use with {@link Konva.Filters.RGBA} filter.
20
+ * @name alpha
21
+ * @method
22
+ * @memberof Konva.Node.prototype
23
+ * @param {Float} alpha value between 0 and 1
24
+ * @returns {Float}
25
+ */
@@ -1,49 +1,36 @@
1
1
  import { Factory } from "../Factory.js";
2
2
  import { Node } from "../Node.js";
3
- import { RGBComponent } from "../Validators.js";
3
+ // red, green and blue are registered there
4
+ import "./RGB.js";
5
+ /**
6
+ * RGBA Filter
7
+ * @function
8
+ * @name RGBA
9
+ * @memberof Konva.Filters
10
+ * @param {Object} imageData
11
+ * @author codefo
12
+ * @example
13
+ * node.cache();
14
+ * node.filters([Konva.Filters.RGBA]);
15
+ * node.blue(120);
16
+ * node.green(200);
17
+ * node.alpha(0.3);
18
+ */
4
19
  export const RGBA = function (imageData) {
5
20
  const data = imageData.data, nPixels = data.length, red = this.red(), green = this.green(), blue = this.blue(), alpha = this.alpha();
6
21
  for (let i = 0; i < nPixels; i += 4) {
7
22
  const ia = 1 - alpha;
8
- data[i] = red * alpha + data[i] * ia;
9
- data[i + 1] = green * alpha + data[i + 1] * ia;
10
- data[i + 2] = blue * alpha + data[i + 2] * ia;
23
+ data[i] = red * alpha + data[i] * ia; // r
24
+ data[i + 1] = green * alpha + data[i + 1] * ia; // g
25
+ data[i + 2] = blue * alpha + data[i + 2] * ia; // b
11
26
  }
12
27
  };
13
- Factory.addGetterSetter(Node, 'red', 0, function (val) {
14
- this._filterUpToDate = false;
15
- if (val > 255) {
16
- return 255;
17
- }
18
- else if (val < 0) {
19
- return 0;
20
- }
21
- else {
22
- return Math.round(val);
23
- }
24
- });
25
- Factory.addGetterSetter(Node, 'green', 0, function (val) {
26
- this._filterUpToDate = false;
27
- if (val > 255) {
28
- return 255;
29
- }
30
- else if (val < 0) {
31
- return 0;
32
- }
33
- else {
34
- return Math.round(val);
35
- }
36
- });
37
- Factory.addGetterSetter(Node, 'blue', 0, RGBComponent, Factory.afterSetFilter);
38
- Factory.addGetterSetter(Node, 'alpha', 1, function (val) {
39
- this._filterUpToDate = false;
40
- if (val > 1) {
41
- return 1;
42
- }
43
- else if (val < 0) {
44
- return 0;
45
- }
46
- else {
47
- return val;
48
- }
49
- });
28
+ Factory.addGetterSetter(Node, 'alpha', 1, (val) => Math.min(1, Math.max(0, val)), Factory.afterSetFilter);
29
+ /**
30
+ * get/set filter alpha value. Use with {@link Konva.Filters.RGBA} filter.
31
+ * @name alpha
32
+ * @method
33
+ * @memberof Konva.Node.prototype
34
+ * @param {Float} alpha value between 0 and 1
35
+ * @returns {Float}
36
+ */
@@ -1,2 +1,11 @@
1
1
  import type { Filter } from '../Node.ts';
2
+ /**
3
+ * @function
4
+ * @name Sepia
5
+ * @memberof Konva.Filters
6
+ * @param {Object} imageData
7
+ * @example
8
+ * node.cache();
9
+ * node.filters([Konva.Filters.Sepia]);
10
+ */
2
11
  export declare const Sepia: Filter;
@@ -1,3 +1,13 @@
1
+ // based on https://stackoverflow.com/questions/1061093/how-is-a-sepia-tone-created
2
+ /**
3
+ * @function
4
+ * @name Sepia
5
+ * @memberof Konva.Filters
6
+ * @param {Object} imageData
7
+ * @example
8
+ * node.cache();
9
+ * node.filters([Konva.Filters.Sepia]);
10
+ */
1
11
  export const Sepia = function (imageData) {
2
12
  const data = imageData.data, nPixels = data.length;
3
13
  for (let i = 0; i < nPixels; i += 4) {
@@ -1,2 +1,12 @@
1
1
  import type { Filter } from '../Node.ts';
2
+ /**
3
+ * Solarize Filter
4
+ * @function
5
+ * @name Solarize
6
+ * @memberof Konva.Filters
7
+ * @param {Object} imageData
8
+ * @example
9
+ * node.cache();
10
+ * node.filters([Konva.Filters.Solarize]);
11
+ */
2
12
  export declare const Solarize: Filter;