konva 10.3.2 → 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 +5 -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 +24 -21
@@ -1,7 +1,41 @@
1
1
  import { Factory } from "../Factory.js";
2
2
  import { _registerNode } from "../Global.js";
3
3
  import { Shape } from "../Shape.js";
4
+ import { Util } from "../Util.js";
4
5
  import { getCubicArcLength, getCubicExtremaPoints, getQuadraticArcLength, getQuadraticExtremaPoints, t2length, } from "../BezierFunctions.js";
6
+ // how many numbers each path command takes
7
+ const PARAM_COUNT = {
8
+ m: 2,
9
+ l: 2,
10
+ h: 1,
11
+ v: 1,
12
+ c: 6,
13
+ s: 4,
14
+ q: 4,
15
+ t: 2,
16
+ a: 7,
17
+ z: 0,
18
+ };
19
+ /**
20
+ * Path constructor.
21
+ * @author Jason Follas
22
+ * @constructor
23
+ * @memberof Konva
24
+ * @augments Konva.Shape
25
+ * @param {Object} config
26
+ * @param {String} config.data SVG data string
27
+ * @@shapeParams
28
+ * @@nodeParams
29
+ * @example
30
+ * var path = new Konva.Path({
31
+ * x: 240,
32
+ * y: 40,
33
+ * 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',
34
+ * fill: 'green',
35
+ * scaleX: 2,
36
+ * scaleY: 2
37
+ * });
38
+ */
5
39
  export class Path extends Shape {
6
40
  constructor(config) {
7
41
  super(config);
@@ -18,6 +52,7 @@ export class Path extends Shape {
18
52
  }
19
53
  _sceneFunc(context) {
20
54
  const ca = this.dataArray;
55
+ // context position
21
56
  context.beginPath();
22
57
  let isClosed = false;
23
58
  for (let n = 0; n < ca.length; n++) {
@@ -62,67 +97,78 @@ export class Path extends Shape {
62
97
  context.fillStrokeShape(this);
63
98
  }
64
99
  }
100
+ getWidth() {
101
+ return this.getSelfRect().width;
102
+ }
103
+ getHeight() {
104
+ return this.getSelfRect().height;
105
+ }
65
106
  getSelfRect() {
66
107
  let points = [];
67
108
  this.dataArray.forEach(function (data) {
68
109
  if (data.command === 'A') {
110
+ // Approximates by breaking curve into line segments
69
111
  const start = data.points[4];
112
+ // 4 = theta
70
113
  const dTheta = data.points[5];
114
+ // 5 = dTheta
71
115
  const end = data.points[4] + dTheta;
72
116
  let inc = Math.PI / 180.0;
117
+ // 1 degree resolution
73
118
  if (Math.abs(start - end) < inc) {
74
119
  inc = Math.abs(start - end);
75
120
  }
76
121
  if (dTheta < 0) {
122
+ // clockwise
77
123
  for (let t = start - inc; t > end; t -= inc) {
78
- const point = Path.getPointOnEllipticalArc(data.points[0], data.points[1], data.points[2], data.points[3], t, 0);
124
+ const point = Path.getPointOnEllipticalArc(data.points[0], data.points[1], data.points[2], data.points[3], t, data.points[6]);
79
125
  points.push(point.x, point.y);
80
126
  }
81
127
  }
82
128
  else {
129
+ // counter-clockwise
83
130
  for (let t = start + inc; t < end; t += inc) {
84
- const point = Path.getPointOnEllipticalArc(data.points[0], data.points[1], data.points[2], data.points[3], t, 0);
131
+ const point = Path.getPointOnEllipticalArc(data.points[0], data.points[1], data.points[2], data.points[3], t, data.points[6]);
85
132
  points.push(point.x, point.y);
86
133
  }
87
134
  }
88
135
  }
89
136
  else if (data.command === 'C') {
137
+ // the two end points, plus the points where the curve turns back on
138
+ // either axis. Together they are the exact bounds of the segment
90
139
  points.push(data.start.x, data.start.y, data.points[4], data.points[5], ...getCubicExtremaPoints(data.start.x, data.start.y, data.points[0], data.points[1], data.points[2], data.points[3], data.points[4], data.points[5]));
91
140
  }
92
141
  else if (data.command === 'Q') {
142
+ // same as 'C'. Note that 'q', 'T' and 't' are all normalised to 'Q' by
143
+ // the parser, so this one branch covers every quadratic segment
93
144
  points.push(data.start.x, data.start.y, data.points[2], data.points[3], ...getQuadraticExtremaPoints(data.start.x, data.start.y, data.points[0], data.points[1], data.points[2], data.points[3]));
94
145
  }
95
146
  else {
96
147
  points = points.concat(data.points);
97
148
  }
98
149
  });
99
- let minX = points[0];
100
- let maxX = points[0];
101
- let minY = points[1];
102
- let maxY = points[1];
103
- let x, y;
104
- for (let i = 0; i < points.length / 2; i++) {
105
- x = points[i * 2];
106
- y = points[i * 2 + 1];
107
- if (!isNaN(x)) {
108
- minX = Math.min(minX, x);
109
- maxX = Math.max(maxX, x);
110
- }
111
- if (!isNaN(y)) {
112
- minY = Math.min(minY, y);
113
- maxY = Math.max(maxY, y);
114
- }
115
- }
116
- return {
117
- x: minX,
118
- y: minY,
119
- width: maxX - minX,
120
- height: maxY - minY,
121
- };
150
+ return Util._getPointsRect(points);
122
151
  }
152
+ /**
153
+ * Return length of the path.
154
+ * @method
155
+ * @name Konva.Path#getLength
156
+ * @returns {Number} length
157
+ * @example
158
+ * var length = path.getLength();
159
+ */
123
160
  getLength() {
124
161
  return this.pathLength;
125
162
  }
163
+ /**
164
+ * Get point on path at specific length of the path
165
+ * @method
166
+ * @name Konva.Path#getPointAtLength
167
+ * @param {Number} length length
168
+ * @returns {Object} point {x,y} point
169
+ * @example
170
+ * var point = path.getPointAtLength(10);
171
+ */
126
172
  getPointAtLength(length) {
127
173
  return Path.getPointAtLengthOfDataArray(length, this.dataArray);
128
174
  }
@@ -146,15 +192,12 @@ export class Path extends Shape {
146
192
  ++i;
147
193
  }
148
194
  if (i === ii) {
149
- let j = i - 1;
150
- while (j > 0 && dataArray[j].points.length < 2) {
151
- j--;
195
+ // past the end: the end of the last segment
196
+ i--;
197
+ while (i > 0 && dataArray[i].points.length < 2) {
198
+ i--;
152
199
  }
153
- points = dataArray[j].points.slice(-2);
154
- return {
155
- x: points[0],
156
- y: points[1],
157
- };
200
+ length = dataArray[i].pathLength;
158
201
  }
159
202
  if (length < 0.01) {
160
203
  const cmd = dataArray[i].command;
@@ -178,11 +221,11 @@ export class Path extends Shape {
178
221
  case 'L':
179
222
  return Path.getPointOnLine(length, cp.start.x, cp.start.y, p[0], p[1]);
180
223
  case 'C':
181
- return Path.getPointOnCubicBezier(t2length(length, Path.getPathLength(dataArray), (i) => {
224
+ return Path.getPointOnCubicBezier(t2length(length, cp.pathLength, (i) => {
182
225
  return getCubicArcLength([cp.start.x, p[0], p[2], p[4]], [cp.start.y, p[1], p[3], p[5]], i);
183
226
  }), cp.start.x, cp.start.y, p[0], p[1], p[2], p[3], p[4], p[5]);
184
227
  case 'Q':
185
- return Path.getPointOnQuadraticBezier(t2length(length, Path.getPathLength(dataArray), (i) => {
228
+ return Path.getPointOnQuadraticBezier(t2length(length, cp.pathLength, (i) => {
186
229
  return getQuadraticArcLength([cp.start.x, p[0], p[2]], [cp.start.y, p[1], p[3]], i);
187
230
  }), cp.start.x, cp.start.y, p[0], p[1], p[2], p[3]);
188
231
  case 'A':
@@ -201,6 +244,7 @@ export class Path extends Shape {
201
244
  return { x: P1x, y: P1y };
202
245
  }
203
246
  if (P2x === P1x) {
247
+ // Vertical line
204
248
  return { x: fromX, y: fromY + (P2y > P1y ? dist : -dist) };
205
249
  }
206
250
  const m = (P2y - P1y) / (P2x - P1x);
@@ -260,11 +304,41 @@ export class Path extends Shape {
260
304
  y: cy + (pt.x * sinPsi + pt.y * cosPsi),
261
305
  };
262
306
  }
307
+ /*
308
+ * get parsed data array from the data
309
+ * string. V, v, H, h, and l data are converted to
310
+ * L data for the purpose of high performance Path
311
+ * rendering
312
+ */
263
313
  static parsePathData(data) {
314
+ // Path Data Segment must begin with a moveTo
315
+ //m (x y)+ Relative moveTo (subsequent points are treated as lineTo)
316
+ //M (x y)+ Absolute moveTo (subsequent points are treated as lineTo)
317
+ //l (x y)+ Relative lineTo
318
+ //L (x y)+ Absolute LineTo
319
+ //h (x)+ Relative horizontal lineTo
320
+ //H (x)+ Absolute horizontal lineTo
321
+ //v (y)+ Relative vertical lineTo
322
+ //V (y)+ Absolute vertical lineTo
323
+ //z (closepath)
324
+ //Z (closepath)
325
+ //c (x1 y1 x2 y2 x y)+ Relative Bezier curve
326
+ //C (x1 y1 x2 y2 x y)+ Absolute Bezier curve
327
+ //q (x1 y1 x y)+ Relative Quadratic Bezier
328
+ //Q (x1 y1 x y)+ Absolute Quadratic Bezier
329
+ //t (x y)+ Shorthand/Smooth Relative Quadratic Bezier
330
+ //T (x y)+ Shorthand/Smooth Absolute Quadratic Bezier
331
+ //s (x2 y2 x y)+ Shorthand/Smooth Relative Bezier curve
332
+ //S (x2 y2 x y)+ Shorthand/Smooth Absolute Bezier curve
333
+ //a (rx ry x-axis-rotation large-arc-flag sweep-flag x y)+ Relative Elliptical Arc
334
+ //A (rx ry x-axis-rotation large-arc-flag sweep-flag x y)+ Absolute Elliptical Arc
335
+ // return early if data is not defined
264
336
  if (!data) {
265
337
  return [];
266
338
  }
339
+ // command string
267
340
  let cs = data;
341
+ // command chars
268
342
  const cc = [
269
343
  'm',
270
344
  'M',
@@ -287,13 +361,17 @@ export class Path extends Shape {
287
361
  'a',
288
362
  'A',
289
363
  ];
364
+ // convert white spaces to commas
290
365
  cs = cs.replace(new RegExp(' ', 'g'), ',');
366
+ // create pipes so that we can split the data
291
367
  for (let n = 0; n < cc.length; n++) {
292
368
  cs = cs.replace(new RegExp(cc[n], 'g'), '|' + cc[n]);
293
369
  }
370
+ // create array
294
371
  const arr = cs.split('|');
295
372
  const ca = [];
296
373
  const coords = [];
374
+ // init context point
297
375
  let cpx = 0;
298
376
  let cpy = 0;
299
377
  const re = /([-+]?((\d+\.\d+)|((\d+)|(\.\d+)))(?:e[-+]?\d+)?)/gi;
@@ -307,93 +385,40 @@ export class Path extends Shape {
307
385
  coords.push(match[0]);
308
386
  }
309
387
  let p = [];
388
+ // Track param position for A/a commands: 0..6 => rx, ry, psi, fa, fs, x, y
310
389
  let arcParamIndex = c === 'A' || c === 'a' ? 0 : -1;
311
390
  for (let j = 0, jlen = coords.length; j < jlen; j++) {
312
- const token = coords[j];
313
- if (token === '00') {
314
- p.push(0, 0);
315
- if (arcParamIndex >= 0) {
316
- arcParamIndex += 2;
317
- if (arcParamIndex >= 7)
318
- arcParamIndex -= 7;
319
- }
320
- continue;
391
+ let token = coords[j];
392
+ // SVGO merges the arc flags with the number that follows them:
393
+ // "01.5.5" is fa=0 fs=1 x=.5 y=.5
394
+ while ((arcParamIndex === 3 || arcParamIndex === 4) &&
395
+ token.length > 1 &&
396
+ (token[0] === '0' || token[0] === '1')) {
397
+ p.push(+token[0]);
398
+ arcParamIndex++;
399
+ token = token.slice(1);
321
400
  }
401
+ const parsed = parseFloat(token);
402
+ p.push(isNaN(parsed) ? 0 : parsed);
322
403
  if (arcParamIndex >= 0) {
323
- if (arcParamIndex === 3) {
324
- if (/^[01]{2}\d+(?:\.\d+)?$/.test(token)) {
325
- p.push(parseInt(token[0], 10));
326
- p.push(parseInt(token[1], 10));
327
- p.push(parseFloat(token.slice(2)));
328
- arcParamIndex += 3;
329
- if (arcParamIndex >= 7)
330
- arcParamIndex -= 7;
331
- continue;
332
- }
333
- if (token === '11' || token === '10' || token === '01') {
334
- p.push(parseInt(token[0], 10));
335
- p.push(parseInt(token[1], 10));
336
- arcParamIndex += 2;
337
- if (arcParamIndex >= 7)
338
- arcParamIndex -= 7;
339
- continue;
340
- }
341
- if (token === '0' || token === '1') {
342
- p.push(parseInt(token, 10));
343
- arcParamIndex += 1;
344
- if (arcParamIndex >= 7)
345
- arcParamIndex -= 7;
346
- continue;
347
- }
348
- }
349
- else if (arcParamIndex === 4) {
350
- if (/^[01]\d+(?:\.\d+)?$/.test(token)) {
351
- p.push(parseInt(token[0], 10));
352
- p.push(parseFloat(token.slice(1)));
353
- arcParamIndex += 2;
354
- if (arcParamIndex >= 7)
355
- arcParamIndex -= 7;
356
- continue;
357
- }
358
- if (token === '0' || token === '1') {
359
- p.push(parseInt(token, 10));
360
- arcParamIndex += 1;
361
- if (arcParamIndex >= 7)
362
- arcParamIndex -= 7;
363
- continue;
364
- }
365
- }
366
- const parsedArc = parseFloat(token);
367
- if (!isNaN(parsedArc)) {
368
- p.push(parsedArc);
369
- }
370
- else {
371
- p.push(0);
372
- }
373
- arcParamIndex += 1;
374
- if (arcParamIndex >= 7)
375
- arcParamIndex -= 7;
376
- }
377
- else {
378
- const parsed = parseFloat(token);
379
- if (!isNaN(parsed)) {
380
- p.push(parsed);
381
- }
382
- else {
383
- p.push(0);
384
- }
404
+ arcParamIndex = (arcParamIndex + 1) % 7;
385
405
  }
386
406
  }
387
407
  while (p.length > 0) {
388
- if (isNaN(p[0])) {
408
+ // z takes no numbers, and a command with too few of them ("L20"
409
+ // with no y) is dropped rather than parsed into a NaN segment
410
+ if (p.length < PARAM_COUNT[c.toLowerCase()] || c === 'z' || c === 'Z') {
389
411
  break;
390
412
  }
391
413
  let cmd = '';
392
414
  let points = [];
393
415
  const startX = cpx, startY = cpy;
394
- let prevCmd, ctlPtx, ctlPty;
395
- let rx, ry, psi, fa, fs, x1, y1;
416
+ // Move var from within the switch to up here (jshint)
417
+ let prevCmd, ctlPtx, ctlPty; // Ss, Tt
418
+ let rx, ry, psi, fa, fs, x1, y1; // Aa
419
+ // convert l, H, h, V, and v to L
396
420
  switch (c) {
421
+ // Note: Keep the lineTo's above the moveTo's in this switch
397
422
  case 'l':
398
423
  cpx += p.shift();
399
424
  cpy += p.shift();
@@ -405,12 +430,15 @@ export class Path extends Shape {
405
430
  cpy = p.shift();
406
431
  points.push(cpx, cpy);
407
432
  break;
433
+ // Note: lineTo handlers need to be above this point
408
434
  case 'm':
409
435
  const dx = p.shift();
410
436
  const dy = p.shift();
411
437
  cpx += dx;
412
438
  cpy += dy;
413
439
  cmd = 'M';
440
+ // After closing the path move the current position
441
+ // to the the first point of the path (if any).
414
442
  if (ca.length > 2 && ca[ca.length - 1].command === 'z') {
415
443
  for (let idx = ca.length - 2; idx >= 0; idx--) {
416
444
  if (ca[idx].command === 'M') {
@@ -422,6 +450,7 @@ export class Path extends Shape {
422
450
  }
423
451
  points.push(cpx, cpy);
424
452
  c = 'l';
453
+ // subsequent points are treated as relative lineTo
425
454
  break;
426
455
  case 'M':
427
456
  cpx = p.shift();
@@ -429,6 +458,7 @@ export class Path extends Shape {
429
458
  cmd = 'M';
430
459
  points.push(cpx, cpy);
431
460
  c = 'L';
461
+ // subsequent points are treated as absolute lineTo
432
462
  break;
433
463
  case 'h':
434
464
  cpx += p.shift();
@@ -467,7 +497,7 @@ export class Path extends Shape {
467
497
  ctlPtx = cpx;
468
498
  ctlPty = cpy;
469
499
  prevCmd = ca[ca.length - 1];
470
- if (prevCmd.command === 'C') {
500
+ if ((prevCmd === null || prevCmd === void 0 ? void 0 : prevCmd.command) === 'C') {
471
501
  ctlPtx = cpx + (cpx - prevCmd.points[2]);
472
502
  ctlPty = cpy + (cpy - prevCmd.points[3]);
473
503
  }
@@ -481,7 +511,7 @@ export class Path extends Shape {
481
511
  ctlPtx = cpx;
482
512
  ctlPty = cpy;
483
513
  prevCmd = ca[ca.length - 1];
484
- if (prevCmd.command === 'C') {
514
+ if ((prevCmd === null || prevCmd === void 0 ? void 0 : prevCmd.command) === 'C') {
485
515
  ctlPtx = cpx + (cpx - prevCmd.points[2]);
486
516
  ctlPty = cpy + (cpy - prevCmd.points[3]);
487
517
  }
@@ -508,7 +538,7 @@ export class Path extends Shape {
508
538
  ctlPtx = cpx;
509
539
  ctlPty = cpy;
510
540
  prevCmd = ca[ca.length - 1];
511
- if (prevCmd.command === 'Q') {
541
+ if ((prevCmd === null || prevCmd === void 0 ? void 0 : prevCmd.command) === 'Q') {
512
542
  ctlPtx = cpx + (cpx - prevCmd.points[0]);
513
543
  ctlPty = cpy + (cpy - prevCmd.points[1]);
514
544
  }
@@ -521,7 +551,7 @@ export class Path extends Shape {
521
551
  ctlPtx = cpx;
522
552
  ctlPty = cpy;
523
553
  prevCmd = ca[ca.length - 1];
524
- if (prevCmd.command === 'Q') {
554
+ if ((prevCmd === null || prevCmd === void 0 ? void 0 : prevCmd.command) === 'Q') {
525
555
  ctlPtx = cpx + (cpx - prevCmd.points[0]);
526
556
  ctlPty = cpy + (cpy - prevCmd.points[1]);
527
557
  }
@@ -531,18 +561,6 @@ export class Path extends Shape {
531
561
  points.push(ctlPtx, ctlPty, cpx, cpy);
532
562
  break;
533
563
  case 'A':
534
- rx = p.shift();
535
- ry = p.shift();
536
- psi = p.shift();
537
- fa = p.shift();
538
- fs = p.shift();
539
- x1 = cpx;
540
- y1 = cpy;
541
- cpx = p.shift();
542
- cpy = p.shift();
543
- cmd = 'A';
544
- points = this.convertEndpointToCenterParameterization(x1, y1, cpx, cpy, fa, fs, rx, ry, psi);
545
- break;
546
564
  case 'a':
547
565
  rx = p.shift();
548
566
  ry = p.shift();
@@ -551,10 +569,27 @@ export class Path extends Shape {
551
569
  fs = p.shift();
552
570
  x1 = cpx;
553
571
  y1 = cpy;
554
- cpx += p.shift();
555
- cpy += p.shift();
572
+ if (c === 'a') {
573
+ cpx += p.shift();
574
+ cpy += p.shift();
575
+ }
576
+ else {
577
+ cpx = p.shift();
578
+ cpy = p.shift();
579
+ }
556
580
  cmd = 'A';
557
- points = this.convertEndpointToCenterParameterization(x1, y1, cpx, cpy, fa, fs, rx, ry, psi);
581
+ // per SVG, an arc between coincident end points is omitted
582
+ // and a zero radius makes it a straight line
583
+ if (cpx === x1 && cpy === y1) {
584
+ continue;
585
+ }
586
+ else if (!rx || !ry) {
587
+ cmd = 'L';
588
+ points.push(cpx, cpy);
589
+ }
590
+ else {
591
+ points = this.convertEndpointToCenterParameterization(x1, y1, cpx, cpy, fa, fs, rx, ry, psi);
592
+ }
558
593
  break;
559
594
  }
560
595
  ca.push({
@@ -589,16 +624,22 @@ export class Path extends Shape {
589
624
  case 'Q':
590
625
  return getQuadraticArcLength([x, points[0], points[2]], [y, points[1], points[3]], 1);
591
626
  case 'A':
627
+ // Approximates by breaking curve into line segments
592
628
  len = 0.0;
593
629
  const start = points[4];
630
+ // 4 = theta
594
631
  const dTheta = points[5];
632
+ // 5 = dTheta
595
633
  const end = points[4] + dTheta;
596
634
  let inc = Math.PI / 180.0;
635
+ // 1 degree resolution
597
636
  if (Math.abs(start - end) < inc) {
598
637
  inc = Math.abs(start - end);
599
638
  }
639
+ // the arc length does not depend on the x-axis rotation psi, so it is left out
600
640
  p1 = path.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], start, 0);
601
641
  if (dTheta < 0) {
642
+ // clockwise
602
643
  for (t = start - inc; t > end; t -= inc) {
603
644
  p2 = path.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], t, 0);
604
645
  len += path.getLineLength(p1.x, p1.y, p2.x, p2.y);
@@ -606,6 +647,7 @@ export class Path extends Shape {
606
647
  }
607
648
  }
608
649
  else {
650
+ // counter-clockwise
609
651
  for (t = start + inc; t < end; t += inc) {
610
652
  p2 = path.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], t, 0);
611
653
  len += path.getLineLength(p1.x, p1.y, p2.x, p2.y);
@@ -619,6 +661,7 @@ export class Path extends Shape {
619
661
  return 0;
620
662
  }
621
663
  static convertEndpointToCenterParameterization(x1, y1, x2, y2, fa, fs, rx, ry, psiDeg) {
664
+ // Derived from: http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
622
665
  const psi = psiDeg * (Math.PI / 180.0);
623
666
  const xp = (Math.cos(psi) * (x1 - x2)) / 2.0 + (Math.sin(psi) * (y1 - y2)) / 2.0;
624
667
  const yp = (-1 * Math.sin(psi) * (x1 - x2)) / 2.0 +
@@ -671,4 +714,20 @@ export class Path extends Shape {
671
714
  Path.prototype.className = 'Path';
672
715
  Path.prototype._attrsAffectingSize = ['data'];
673
716
  _registerNode(Path);
717
+ /**
718
+ * get/set SVG path data string. This method
719
+ * also automatically parses the data string
720
+ * into a data array. Currently supported SVG data:
721
+ * M, m, L, l, H, h, V, v, Q, q, T, t, C, c, S, s, A, a, Z, z
722
+ * @name Konva.Path#data
723
+ * @method
724
+ * @param {String} data svg path string
725
+ * @returns {String}
726
+ * @example
727
+ * // get data
728
+ * var data = path.data();
729
+ *
730
+ * // set data
731
+ * path.data('M200,100h100v50z');
732
+ */
674
733
  Factory.addGetterSetter(Path, 'data');
@@ -5,6 +5,24 @@ import type { Context } from '../Context.ts';
5
5
  export type RectConfig = ShapeConfig & {
6
6
  cornerRadius?: number | number[];
7
7
  };
8
+ /**
9
+ * Rect constructor
10
+ * @constructor
11
+ * @memberof Konva
12
+ * @augments Konva.Shape
13
+ * @param {Object} config
14
+ * @param {Number} [config.cornerRadius]
15
+ * @@shapeParams
16
+ * @@nodeParams
17
+ * @example
18
+ * var rect = new Konva.Rect({
19
+ * width: 100,
20
+ * height: 50,
21
+ * fill: 'red',
22
+ * stroke: 'black',
23
+ * strokeWidth: 5
24
+ * });
25
+ */
8
26
  export declare class Rect extends Shape<RectConfig> {
9
27
  _sceneFunc(context: Context): void;
10
28
  cornerRadius: GetSet<number | number[], this>;
@@ -3,11 +3,30 @@ import { Shape } from "../Shape.js";
3
3
  import { _registerNode } from "../Global.js";
4
4
  import { Util } from "../Util.js";
5
5
  import { getNumberOrArrayOfNumbersValidator } from "../Validators.js";
6
+ /**
7
+ * Rect constructor
8
+ * @constructor
9
+ * @memberof Konva
10
+ * @augments Konva.Shape
11
+ * @param {Object} config
12
+ * @param {Number} [config.cornerRadius]
13
+ * @@shapeParams
14
+ * @@nodeParams
15
+ * @example
16
+ * var rect = new Konva.Rect({
17
+ * width: 100,
18
+ * height: 50,
19
+ * fill: 'red',
20
+ * stroke: 'black',
21
+ * strokeWidth: 5
22
+ * });
23
+ */
6
24
  export class Rect extends Shape {
7
25
  _sceneFunc(context) {
8
26
  const cornerRadius = this.cornerRadius(), width = this.width(), height = this.height();
9
27
  context.beginPath();
10
28
  if (!cornerRadius) {
29
+ // simple rect - don't bother doing all that complicated maths stuff.
11
30
  context.rect(0, 0, width, height);
12
31
  }
13
32
  else {
@@ -19,4 +38,21 @@ export class Rect extends Shape {
19
38
  }
20
39
  Rect.prototype.className = 'Rect';
21
40
  _registerNode(Rect);
41
+ /**
42
+ * get/set corner radius
43
+ * @method
44
+ * @name Konva.Rect#cornerRadius
45
+ * @param {Number} cornerRadius
46
+ * @returns {Number}
47
+ * @example
48
+ * // get corner radius
49
+ * var cornerRadius = rect.cornerRadius();
50
+ *
51
+ * // set corner radius
52
+ * rect.cornerRadius(10);
53
+ *
54
+ * // set different corner radius values
55
+ * // top-left, top-right, bottom-right, bottom-left
56
+ * rect.cornerRadius([0, 10, 20, 30]);
57
+ */
22
58
  Factory.addGetterSetter(Rect, 'cornerRadius', 0, getNumberOrArrayOfNumbersValidator(4));
@@ -7,6 +7,28 @@ export interface RegularPolygonConfig extends ShapeConfig {
7
7
  radius: number;
8
8
  cornerRadius?: number | number[];
9
9
  }
10
+ /**
11
+ * RegularPolygon constructor. Examples include triangles, squares, pentagons, hexagons, etc.
12
+ * @constructor
13
+ * @memberof Konva
14
+ * @augments Konva.Shape
15
+ * @param {Object} config
16
+ * @param {Number} [config.cornerRadius]
17
+ * @param {Number} config.sides
18
+ * @param {Number} config.radius
19
+ * @@shapeParams
20
+ * @@nodeParams
21
+ * @example
22
+ * var hexagon = new Konva.RegularPolygon({
23
+ * x: 100,
24
+ * y: 200,
25
+ * sides: 6,
26
+ * radius: 70,
27
+ * fill: 'red',
28
+ * stroke: 'black',
29
+ * strokeWidth: 4
30
+ * });
31
+ */
10
32
  export declare class RegularPolygon extends Shape<RegularPolygonConfig> {
11
33
  _sceneFunc(context: Context): void;
12
34
  _getPoints(): Vector2d[];