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,9 +2,12 @@ import { getCubicExtremaPoints, getQuadraticExtremaPoints, } from "../BezierFunc
2
2
  import { Factory } from "../Factory.js";
3
3
  import { _registerNode } from "../Global.js";
4
4
  import { Shape } from "../Shape.js";
5
+ import { Util } from "../Util.js";
5
6
  import { getNumberArrayValidator, getNumberValidator } from "../Validators.js";
6
7
  function getControlPoints(x0, y0, x1, y1, x2, y2, t) {
7
8
  const d01 = Math.sqrt(Math.pow(x1 - x0, 2) + Math.pow(y1 - y0, 2)), d12 = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)), dSum = d01 + d12;
9
+ // all three points are the same, so the control points collapse into that
10
+ // point. Without this the divisions below are 0 / 0 = NaN.
8
11
  if (dSum === 0) {
9
12
  return [x1, y1, x1, y1];
10
13
  }
@@ -29,11 +32,41 @@ function expandPoints(p, tension) {
29
32
  }
30
33
  function getBezierExtremaPoints(points) {
31
34
  const extrema = [];
35
+ // points[0], points[1] is the starting point; every subsequent group of
36
+ // 6 values is one cubic bezier segment (cp1x, cp1y, cp2x, cp2y, x, y),
37
+ // whose start is the previous segment's end point (matching the layout
38
+ // Line#_sceneFunc feeds to context.bezierCurveTo in a loop).
32
39
  for (let n = 0; n + 7 < points.length; n += 6) {
40
+ // the end point of the segment. It is a joint with the next segment, and
41
+ // the curve can reach its highest or lowest value there without the
42
+ // derivative going to zero on either side.
33
43
  extrema.push(points[n + 6], points[n + 7], ...getCubicExtremaPoints(points[n], points[n + 1], points[n + 2], points[n + 3], points[n + 4], points[n + 5], points[n + 6], points[n + 7]));
34
44
  }
35
45
  return extrema;
36
46
  }
47
+ /**
48
+ * Line constructor.&nbsp; Lines are defined by an array of points and
49
+ * a tension
50
+ * @constructor
51
+ * @memberof Konva
52
+ * @augments Konva.Shape
53
+ * @param {Object} config
54
+ * @param {Array} config.points Flat array of points coordinates. You should define them as [x1, y1, x2, y2, x3, y3].
55
+ * @param {Number} [config.tension] Higher values will result in a more curvy line. A value of 0 will result in no interpolation.
56
+ * The default is 0
57
+ * @param {Boolean} [config.closed] defines whether or not the line shape is closed, creating a polygon or blob
58
+ * @param {Boolean} [config.bezier] if no tension is provided but bezier=true, we draw the line as a bezier using the passed points
59
+ * @@shapeParams
60
+ * @@nodeParams
61
+ * @example
62
+ * var line = new Konva.Line({
63
+ * x: 100,
64
+ * y: 50,
65
+ * points: [73, 70, 340, 23, 450, 60, 500, 20],
66
+ * stroke: 'red',
67
+ * tension: 1
68
+ * });
69
+ */
37
70
  export class Line extends Shape {
38
71
  constructor(config) {
39
72
  super(config);
@@ -44,9 +77,19 @@ export class Line extends Shape {
44
77
  _hasTension() {
45
78
  return this.tension() !== 0 && this.points().length > 4;
46
79
  }
80
+ /**
81
+ * Report every curve segment of a line with a tension, in draw order. Both
82
+ * the scene function and the bounding rect read the shape through this, so
83
+ * that they can not disagree about which curve the line is.
84
+ *
85
+ * The handlers take plain numbers, because this runs on every frame.
86
+ */
47
87
  _eachTensionSegment(onQuadratic, onCubic) {
48
88
  const points = this.points(), length = points.length, closed = this.closed(), tp = this.getTensionPoints(), len = tp.length;
49
- let x0 = points[0], y0 = points[1], n = closed ? 0 : 4;
89
+ let x0 = points[0], y0 = points[1],
90
+ // a closed line has a curve on both sides of the first point, so it
91
+ // starts on a full cubic. An open one is capped by a quadratic instead
92
+ n = closed ? 0 : 4;
50
93
  if (!closed) {
51
94
  onQuadratic(x0, y0, tp[0], tp[1], tp[2], tp[3]);
52
95
  x0 = tp[2];
@@ -70,25 +113,30 @@ export class Line extends Shape {
70
113
  let n = 0;
71
114
  context.beginPath();
72
115
  context.moveTo(points[0], points[1]);
116
+ // tension
73
117
  if (this._hasTension()) {
74
118
  this._eachTensionSegment((_x0, _y0, cpx, cpy, x, y) => context.quadraticCurveTo(cpx, cpy, x, y), (_x0, _y0, cp1x, cp1y, cp2x, cp2y, x, y) => context.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y));
75
119
  }
76
120
  else if (bezier) {
121
+ // no tension but bezier
77
122
  n = 2;
78
123
  while (n < length) {
79
124
  context.bezierCurveTo(points[n++], points[n++], points[n++], points[n++], points[n++], points[n++]);
80
125
  }
81
126
  }
82
127
  else {
128
+ // no tension
83
129
  for (n = 2; n < length; n += 2) {
84
130
  context.lineTo(points[n], points[n + 1]);
85
131
  }
86
132
  }
133
+ // closed e.g. polygons and blobs
87
134
  if (closed) {
88
135
  context.closePath();
89
136
  context.fillStrokeShape(this);
90
137
  }
91
138
  else {
139
+ // open e.g. lines and splines
92
140
  context.strokeShape(this);
93
141
  }
94
142
  }
@@ -126,6 +174,7 @@ export class Line extends Shape {
126
174
  getHeight() {
127
175
  return this.getSelfRect().height;
128
176
  }
177
+ // overload size detection
129
178
  getSelfRect() {
130
179
  let points = this.points();
131
180
  if (points.length < 4) {
@@ -137,38 +186,85 @@ export class Line extends Shape {
137
186
  };
138
187
  }
139
188
  if (this._hasTension()) {
189
+ // the two end points of every segment, plus the points where it turns
190
+ // back on either axis. Together they are the exact bounds of the curve
140
191
  const bounds = [points[0], points[1]];
141
192
  this._eachTensionSegment((x0, y0, cpx, cpy, x, y) => bounds.push(x, y, ...getQuadraticExtremaPoints(x0, y0, cpx, cpy, x, y)), (x0, y0, cp1x, cp1y, cp2x, cp2y, x, y) => bounds.push(x, y, ...getCubicExtremaPoints(x0, y0, cp1x, cp1y, cp2x, cp2y, x, y)));
142
193
  points = bounds;
143
194
  }
144
195
  else if (this.bezier()) {
196
+ // no trailing point here: the extrema already carry the end point of
197
+ // every segment that is drawn. Adding the last pair of the array back
198
+ // would include a control point of a trailing partial segment, which
199
+ // _sceneFunc never draws.
145
200
  points = [points[0], points[1], ...getBezierExtremaPoints(points)];
146
201
  }
147
- let minX = points[0];
148
- let maxX = points[0];
149
- let minY = points[1];
150
- let maxY = points[1];
151
- let x, y;
152
- for (let i = 0; i < points.length / 2; i++) {
153
- x = points[i * 2];
154
- y = points[i * 2 + 1];
155
- minX = Math.min(minX, x);
156
- maxX = Math.max(maxX, x);
157
- minY = Math.min(minY, y);
158
- maxY = Math.max(maxY, y);
159
- }
160
- return {
161
- x: minX,
162
- y: minY,
163
- width: maxX - minX,
164
- height: maxY - minY,
165
- };
202
+ return Util._getPointsRect(points);
166
203
  }
167
204
  }
168
205
  Line.prototype.className = 'Line';
169
206
  Line.prototype._attrsAffectingSize = ['points', 'bezier', 'tension'];
170
207
  _registerNode(Line);
208
+ // add getters setters
171
209
  Factory.addGetterSetter(Line, 'closed', false);
210
+ /**
211
+ * get/set closed flag. The default is false
212
+ * @name Konva.Line#closed
213
+ * @method
214
+ * @param {Boolean} closed
215
+ * @returns {Boolean}
216
+ * @example
217
+ * // get closed flag
218
+ * var closed = line.closed();
219
+ *
220
+ * // close the shape
221
+ * line.closed(true);
222
+ *
223
+ * // open the shape
224
+ * line.closed(false);
225
+ */
172
226
  Factory.addGetterSetter(Line, 'bezier', false);
227
+ /**
228
+ * get/set bezier flag. The default is false
229
+ * @name Konva.Line#bezier
230
+ * @method
231
+ * @param {Boolean} bezier
232
+ * @returns {Boolean}
233
+ * @example
234
+ * // get whether the line is a bezier
235
+ * var isBezier = line.bezier();
236
+ *
237
+ * // set whether the line is a bezier
238
+ * line.bezier(true);
239
+ */
173
240
  Factory.addGetterSetter(Line, 'tension', 0, getNumberValidator());
241
+ /**
242
+ * get/set tension
243
+ * @name Konva.Line#tension
244
+ * @method
245
+ * @param {Number} tension Higher values will result in a more curvy line. A value of 0 will result in no interpolation. The default is 0
246
+ * @returns {Number}
247
+ * @example
248
+ * // get tension
249
+ * var tension = line.tension();
250
+ *
251
+ * // set tension
252
+ * line.tension(3);
253
+ */
174
254
  Factory.addGetterSetter(Line, 'points', [], getNumberArrayValidator());
255
+ /**
256
+ * get/set points array. Points is a flat array [x1, y1, x2, y2]. It is flat for performance reasons.
257
+ * @name Konva.Line#points
258
+ * @method
259
+ * @param {Array} points
260
+ * @returns {Array}
261
+ * @example
262
+ * // get points
263
+ * var points = line.points();
264
+ *
265
+ * // set points
266
+ * line.points([10, 20, 30, 40, 50, 60]);
267
+ *
268
+ * // push a new point
269
+ * line.points(line.points().concat([70, 80]));
270
+ */
@@ -4,19 +4,53 @@ import type { GetSet, PathSegment } from '../types.ts';
4
4
  export interface PathConfig extends ShapeConfig {
5
5
  data?: string;
6
6
  }
7
+ /**
8
+ * Path constructor.
9
+ * @author Jason Follas
10
+ * @constructor
11
+ * @memberof Konva
12
+ * @augments Konva.Shape
13
+ * @param {Object} config
14
+ * @param {String} config.data SVG data string
15
+ * @@shapeParams
16
+ * @@nodeParams
17
+ * @example
18
+ * var path = new Konva.Path({
19
+ * x: 240,
20
+ * y: 40,
21
+ * data: 'M12.582,9.551C3.251,16.237,0.921,29.021,7.08,38.564l-2.36,1.689l4.893,2.262l4.893,2.262l-0.568-5.36l-0.567-5.359l-2.365,1.694c-4.657-7.375-2.83-17.185,4.352-22.33c7.451-5.338,17.817-3.625,23.156,3.824c5.337,7.449,3.625,17.813-3.821,23.152l2.857,3.988c9.617-6.893,11.827-20.277,4.935-29.896C35.591,4.87,22.204,2.658,12.582,9.551z',
22
+ * fill: 'green',
23
+ * scaleX: 2,
24
+ * scaleY: 2
25
+ * });
26
+ */
7
27
  export declare class Path extends Shape<PathConfig> {
8
28
  dataArray: PathSegment[];
9
29
  pathLength: number;
10
30
  constructor(config?: PathConfig);
11
31
  _readDataAttribute(): void;
12
32
  _sceneFunc(context: any): void;
13
- getSelfRect(): {
14
- x: number;
15
- y: number;
16
- width: number;
17
- height: number;
18
- };
33
+ getWidth(): number;
34
+ getHeight(): number;
35
+ getSelfRect(): import("../types.ts").IRect;
36
+ /**
37
+ * Return length of the path.
38
+ * @method
39
+ * @name Konva.Path#getLength
40
+ * @returns {Number} length
41
+ * @example
42
+ * var length = path.getLength();
43
+ */
19
44
  getLength(): number;
45
+ /**
46
+ * Get point on path at specific length of the path
47
+ * @method
48
+ * @name Konva.Path#getPointAtLength
49
+ * @param {Number} length length
50
+ * @returns {Object} point {x,y} point
51
+ * @example
52
+ * var point = path.getPointAtLength(10);
53
+ */
20
54
  getPointAtLength(length: number): {
21
55
  x: number;
22
56
  y: number;