konva 10.2.5 → 10.3.1

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.
@@ -1,9 +1,14 @@
1
+ import { getCubicExtremaPoints, getQuadraticExtremaPoints, } from "../BezierFunctions.js";
1
2
  import { Factory } from "../Factory.js";
2
3
  import { _registerNode } from "../Global.js";
3
4
  import { Shape } from "../Shape.js";
4
5
  import { getNumberArrayValidator, getNumberValidator } from "../Validators.js";
5
6
  function getControlPoints(x0, y0, x1, y1, x2, y2, t) {
6
- 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)), fa = (t * d01) / (d01 + d12), fb = (t * d12) / (d01 + d12), p1x = x1 - fa * (x2 - x0), p1y = y1 - fa * (y2 - y0), p2x = x1 + fb * (x2 - x0), p2y = y1 + fb * (y2 - y0);
7
+ 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;
8
+ if (dSum === 0) {
9
+ return [x1, y1, x1, y1];
10
+ }
11
+ const fa = (t * d01) / dSum, fb = (t * d12) / dSum, p1x = x1 - fa * (x2 - x0), p1y = y1 - fa * (y2 - y0), p2x = x1 + fb * (x2 - x0), p2y = y1 + fb * (y2 - y0);
7
12
  return [p1x, p1y, p2x, p2y];
8
13
  }
9
14
  function expandPoints(p, tension) {
@@ -23,33 +28,11 @@ function expandPoints(p, tension) {
23
28
  return allPoints;
24
29
  }
25
30
  function getBezierExtremaPoints(points) {
26
- const axisPoints = [
27
- [points[0], points[2], points[4], points[6]],
28
- [points[1], points[3], points[5], points[7]],
29
- ];
30
- const extremaTs = [];
31
- for (const axis of axisPoints) {
32
- const a = -3 * axis[0] + 9 * axis[1] - 9 * axis[2] + 3 * axis[3];
33
- if (a !== 0) {
34
- const b = 6 * axis[0] - 12 * axis[1] + 6 * axis[2];
35
- const c = -3 * axis[0] + 3 * axis[1];
36
- const discriminant = b * b - 4 * a * c;
37
- if (discriminant >= 0) {
38
- const d = Math.sqrt(discriminant);
39
- extremaTs.push((-b + d) / (2 * a));
40
- extremaTs.push((-b - d) / (2 * a));
41
- }
42
- }
31
+ const extrema = [];
32
+ for (let n = 0; n + 7 < points.length; n += 6) {
33
+ 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]));
43
34
  }
44
- return extremaTs
45
- .filter((t) => t > 0 && t < 1)
46
- .flatMap((t) => axisPoints.map((axis) => {
47
- const mt = 1 - t;
48
- return (mt * mt * mt * axis[0] +
49
- 3 * mt * mt * t * axis[1] +
50
- 3 * mt * t * t * axis[2] +
51
- t * t * t * axis[3]);
52
- }));
35
+ return extrema;
53
36
  }
54
37
  export class Line extends Shape {
55
38
  constructor(config) {
@@ -58,27 +41,37 @@ export class Line extends Shape {
58
41
  this._clearCache('tensionPoints');
59
42
  });
60
43
  }
44
+ _hasTension() {
45
+ return this.tension() !== 0 && this.points().length > 4;
46
+ }
47
+ _eachTensionSegment(onQuadratic, onCubic) {
48
+ 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;
50
+ if (!closed) {
51
+ onQuadratic(x0, y0, tp[0], tp[1], tp[2], tp[3]);
52
+ x0 = tp[2];
53
+ y0 = tp[3];
54
+ }
55
+ while (n < len - 2) {
56
+ const cp1x = tp[n++], cp1y = tp[n++], cp2x = tp[n++], cp2y = tp[n++], x = tp[n++], y = tp[n++];
57
+ onCubic(x0, y0, cp1x, cp1y, cp2x, cp2y, x, y);
58
+ x0 = x;
59
+ y0 = y;
60
+ }
61
+ if (!closed) {
62
+ onQuadratic(x0, y0, tp[len - 2], tp[len - 1], points[length - 2], points[length - 1]);
63
+ }
64
+ }
61
65
  _sceneFunc(context) {
62
- const points = this.points(), length = points.length, tension = this.tension(), closed = this.closed(), bezier = this.bezier();
66
+ const points = this.points(), length = points.length, closed = this.closed(), bezier = this.bezier();
63
67
  if (!length) {
64
68
  return;
65
69
  }
66
70
  let n = 0;
67
71
  context.beginPath();
68
72
  context.moveTo(points[0], points[1]);
69
- if (tension !== 0 && length > 4) {
70
- const tp = this.getTensionPoints();
71
- const len = tp.length;
72
- n = closed ? 0 : 4;
73
- if (!closed) {
74
- context.quadraticCurveTo(tp[0], tp[1], tp[2], tp[3]);
75
- }
76
- while (n < len - 2) {
77
- context.bezierCurveTo(tp[n++], tp[n++], tp[n++], tp[n++], tp[n++], tp[n++]);
78
- }
79
- if (!closed) {
80
- context.quadraticCurveTo(tp[len - 2], tp[len - 1], points[length - 2], points[length - 1]);
81
- }
73
+ if (this._hasTension()) {
74
+ 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));
82
75
  }
83
76
  else if (bezier) {
84
77
  n = 2;
@@ -143,26 +136,13 @@ export class Line extends Shape {
143
136
  height: 0,
144
137
  };
145
138
  }
146
- if (this.tension() !== 0) {
147
- points = [
148
- points[0],
149
- points[1],
150
- ...this._getTensionPoints(),
151
- points[points.length - 2],
152
- points[points.length - 1],
153
- ];
139
+ if (this._hasTension()) {
140
+ const bounds = [points[0], points[1]];
141
+ 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
+ points = bounds;
154
143
  }
155
144
  else if (this.bezier()) {
156
- points = [
157
- points[0],
158
- points[1],
159
- ...getBezierExtremaPoints(this.points()),
160
- points[points.length - 2],
161
- points[points.length - 1],
162
- ];
163
- }
164
- else {
165
- points = this.points();
145
+ points = [points[0], points[1], ...getBezierExtremaPoints(points)];
166
146
  }
167
147
  let minX = points[0];
168
148
  let maxX = points[0];
@@ -1,7 +1,7 @@
1
1
  import { Factory } from "../Factory.js";
2
2
  import { _registerNode } from "../Global.js";
3
3
  import { Shape } from "../Shape.js";
4
- import { getCubicArcLength, getQuadraticArcLength, t2length, } from "../BezierFunctions.js";
4
+ import { getCubicArcLength, getCubicExtremaPoints, getQuadraticArcLength, getQuadraticExtremaPoints, t2length, } from "../BezierFunctions.js";
5
5
  export class Path extends Shape {
6
6
  constructor(config) {
7
7
  super(config);
@@ -87,10 +87,10 @@ export class Path extends Shape {
87
87
  }
88
88
  }
89
89
  else if (data.command === 'C') {
90
- for (let t = 0.0; t <= 1; t += 0.01) {
91
- const point = Path.getPointOnCubicBezier(t, data.start.x, data.start.y, data.points[0], data.points[1], data.points[2], data.points[3], data.points[4], data.points[5]);
92
- points.push(point.x, point.y);
93
- }
90
+ 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
+ }
92
+ else if (data.command === 'Q') {
93
+ 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
94
  }
95
95
  else {
96
96
  points = points.concat(data.points);
@@ -146,7 +146,11 @@ export class Path extends Shape {
146
146
  ++i;
147
147
  }
148
148
  if (i === ii) {
149
- points = dataArray[i - 1].points.slice(-2);
149
+ let j = i - 1;
150
+ while (j > 0 && dataArray[j].points.length < 2) {
151
+ j--;
152
+ }
153
+ points = dataArray[j].points.slice(-2);
150
154
  return {
151
155
  x: points[0],
152
156
  y: points[1],
@@ -347,9 +347,10 @@ export class Text extends Shape {
347
347
  let lineWidth = this._getTextWidth(line);
348
348
  if (fixedWidth && lineWidth > maxWidth) {
349
349
  while (line.length > 0) {
350
- let low = 0, high = stringToArray(line).length, match = '', matchWidth = 0;
350
+ const lineArray = stringToArray(line);
351
+ let low = 0, high = lineArray.length, match = '', matchWidth = 0;
351
352
  while (low < high) {
352
- const mid = (low + high) >>> 1, lineArray = stringToArray(line), substr = lineArray.slice(0, mid + 1).join(''), substrWidth = this._getTextWidth(substr);
353
+ const mid = (low + high) >>> 1, substr = lineArray.slice(0, mid + 1).join(''), substrWidth = this._getTextWidth(substr);
353
354
  const shouldConsiderEllipsis = shouldAddEllipsis &&
354
355
  fixedHeight &&
355
356
  currentHeightPx + lineHeightPx > maxHeightPx;
@@ -367,7 +368,6 @@ export class Text extends Shape {
367
368
  }
368
369
  if (match) {
369
370
  if (wrapAtWord) {
370
- const lineArray = stringToArray(line);
371
371
  const matchArray = stringToArray(match);
372
372
  const nextChar = lineArray[matchArray.length];
373
373
  const nextIsSpaceOrDash = nextChar === SPACE || nextChar === DASH;
@@ -395,7 +395,6 @@ export class Text extends Shape {
395
395
  this._tryToAddEllipsisToLastLine();
396
396
  break;
397
397
  }
398
- const lineArray = stringToArray(line);
399
398
  line = lineArray.slice(low).join('').trimLeft();
400
399
  if (line.length > 0) {
401
400
  lineWidth = this._getTextWidth(line);
@@ -14,6 +14,7 @@ export interface TextPathConfig extends ShapeConfig {
14
14
  letterSpacing?: number;
15
15
  textBaseline?: string;
16
16
  textDecoration?: string;
17
+ direction?: string;
17
18
  kerningFunc?: (leftChar: string, rightChar: string) => number;
18
19
  lineHeight?: number;
19
20
  }
@@ -69,4 +70,5 @@ export declare class TextPath extends Shape<TextPathConfig> {
69
70
  kerningFunc: GetSet<(leftChar: string, rightChar: string) => number, this>;
70
71
  textBaseline: GetSet<string, this>;
71
72
  textDecoration: GetSet<string, this>;
73
+ direction: GetSet<string, this>;
72
74
  }
@@ -22,7 +22,7 @@ export class TextPath extends Shape {
22
22
  this._readDataAttribute();
23
23
  this._setTextData();
24
24
  });
25
- this.on('textChange.konva alignChange.konva letterSpacingChange.konva kerningFuncChange.konva fontSizeChange.konva fontFamilyChange.konva', this._setTextData);
25
+ this.on('textChange.konva alignChange.konva letterSpacingChange.konva kerningFuncChange.konva fontSizeChange.konva fontFamilyChange.konva directionChange.konva', this._setTextData);
26
26
  this._setTextData();
27
27
  }
28
28
  _getTextPathLength() {
@@ -137,6 +137,9 @@ export class TextPath extends Shape {
137
137
  }
138
138
  _setTextData() {
139
139
  const charArr = stringToArray(this.text());
140
+ if (this.direction() === 'rtl') {
141
+ charArr.reverse();
142
+ }
140
143
  const chars = [];
141
144
  let width = 0;
142
145
  for (let i = 0; i < charArr.length; i++) {
@@ -271,3 +274,4 @@ Factory.addGetterSetter(TextPath, 'fontVariant', NORMAL);
271
274
  Factory.addGetterSetter(TextPath, 'text', EMPTY_STRING);
272
275
  Factory.addGetterSetter(TextPath, 'textDecoration', '');
273
276
  Factory.addGetterSetter(TextPath, 'kerningFunc', undefined);
277
+ Factory.addGetterSetter(TextPath, 'direction', 'inherit');
@@ -47,6 +47,7 @@ export declare class Transformer extends Group {
47
47
  cos: number;
48
48
  _cursorChange: boolean;
49
49
  _elementsCreated: boolean;
50
+ _updateScheduled: boolean;
50
51
  static isTransforming: () => boolean;
51
52
  constructor(config?: TransformerConfig);
52
53
  attachTo(node: Node): this;
@@ -87,6 +88,8 @@ export declare class Transformer extends Group {
87
88
  getAbsoluteTransform(): Transform;
88
89
  _removeEvents(e?: any): void;
89
90
  _fitNodesInto(newAttrs: any, evt?: any): void;
91
+ _doFitNodesInto(newAttrs: any, evt?: any): void;
92
+ _scheduleUpdate(): void;
90
93
  forceUpdate(): void;
91
94
  _batchChangeChild(selector: string, attrs: any): void;
92
95
  update(): void;
@@ -147,6 +147,7 @@ export class Transformer extends Group {
147
147
  this._movingAnchorName = null;
148
148
  this._transforming = false;
149
149
  this._elementsCreated = false;
150
+ this._updateScheduled = false;
150
151
  this._createElements();
151
152
  this._handleMouseMove = this._handleMouseMove.bind(this);
152
153
  this._handleMouseUp = this._handleMouseUp.bind(this);
@@ -190,12 +191,14 @@ export class Transformer extends Group {
190
191
  }
191
192
  this._nodes.forEach((node) => {
192
193
  const onChange = () => {
194
+ if (this._transforming)
195
+ return;
193
196
  if (this.nodes().length === 1 && this.useSingleNodeRotation()) {
194
197
  this.rotation(this.nodes()[0].getAbsoluteRotation());
195
198
  }
196
199
  this._resetTransformCache();
197
- if (!this._transforming && !this.isDragging()) {
198
- this.update();
200
+ if (!this.isDragging()) {
201
+ this._scheduleUpdate();
199
202
  }
200
203
  };
201
204
  if (node._attrsAffectingSize.length) {
@@ -718,6 +721,16 @@ export class Transformer extends Group {
718
721
  }
719
722
  }
720
723
  _fitNodesInto(newAttrs, evt) {
724
+ const prevAutoDraw = Konva.autoDrawEnabled;
725
+ Konva.autoDrawEnabled = false;
726
+ try {
727
+ return this._doFitNodesInto(newAttrs, evt);
728
+ }
729
+ finally {
730
+ Konva.autoDrawEnabled = prevAutoDraw;
731
+ }
732
+ }
733
+ _doFitNodesInto(newAttrs, evt) {
721
734
  const oldAttrs = this._getNodeRect();
722
735
  const minSize = 1;
723
736
  if (Util._inRange(newAttrs.width, -this.padding() * 2 - minSize, minSize)) {
@@ -811,8 +824,8 @@ export class Transformer extends Group {
811
824
  newTr.scale(newScaleX, newScaleY);
812
825
  }
813
826
  const delta = newTr.multiply(oldTr.invert());
827
+ const layersToDraw = new Set();
814
828
  this._nodes.forEach((node) => {
815
- var _a;
816
829
  if (!node.getStage()) {
817
830
  return;
818
831
  }
@@ -827,7 +840,10 @@ export class Transformer extends Group {
827
840
  .multiply(localTransform);
828
841
  const attrs = newLocalTransform.decompose();
829
842
  node.setAttrs(attrs);
830
- (_a = node.getLayer()) === null || _a === void 0 ? void 0 : _a.batchDraw();
843
+ const layer = node.getLayer();
844
+ if (layer) {
845
+ layersToDraw.add(layer);
846
+ }
831
847
  });
832
848
  this.rotation(Util._getRotation(newAttrs.rotation));
833
849
  this._nodes.forEach((node) => {
@@ -836,7 +852,21 @@ export class Transformer extends Group {
836
852
  });
837
853
  this._resetTransformCache();
838
854
  this.update();
839
- this.getLayer().batchDraw();
855
+ layersToDraw.add(this.getLayer());
856
+ layersToDraw.forEach((layer) => layer && layer.batchDraw());
857
+ }
858
+ _scheduleUpdate() {
859
+ if (this._updateScheduled)
860
+ return;
861
+ this._updateScheduled = true;
862
+ Node._runAfterAbsTransformCascade(() => {
863
+ var _a;
864
+ this._updateScheduled = false;
865
+ if (!((_a = this._nodes) === null || _a === void 0 ? void 0 : _a.length) || this._transforming || this.isDragging()) {
866
+ return;
867
+ }
868
+ this.update();
869
+ });
840
870
  }
841
871
  forceUpdate() {
842
872
  this._resetTransformCache();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "konva",
3
- "version": "10.2.5",
3
+ "version": "10.3.1",
4
4
  "description": "HTML5 2d canvas library.",
5
5
  "author": "Anton Lavrenov",
6
6
  "type": "module",
@@ -109,7 +109,7 @@
109
109
  "process": "^0.11.10",
110
110
  "rimraf": "^6.1.2",
111
111
  "rollup": "^4.55.1",
112
- "rollup-plugin-typescript2": "^0.36.0",
112
+ "rollup-plugin-typescript2": "^0.37.0",
113
113
  "size-limit": "^11.2.0",
114
114
  "skia-canvas": "^3.0.4",
115
115
  "ts-mocha": "^11.1.0",
@@ -117,6 +117,18 @@
117
117
  "tsx": "^4.21.0",
118
118
  "typescript": "^5.9.3"
119
119
  },
120
+ "peerDependencies": {
121
+ "canvas": "^3.0.0",
122
+ "skia-canvas": "^3.0.0"
123
+ },
124
+ "peerDependenciesMeta": {
125
+ "canvas": {
126
+ "optional": true
127
+ },
128
+ "skia-canvas": {
129
+ "optional": true
130
+ }
131
+ },
120
132
  "keywords": [
121
133
  "canvas",
122
134
  "animations",