konva 10.5.0 → 10.6.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 (62) hide show
  1. package/README.md +4 -4
  2. package/konva.js +3247 -3746
  3. package/konva.min.js +4 -4
  4. package/lib/Animation.d.ts +2 -2
  5. package/lib/Animation.js +2 -2
  6. package/lib/BezierFunctions.d.ts +1 -1
  7. package/lib/BezierFunctions.js +76 -116
  8. package/lib/Canvas.d.ts +1 -1
  9. package/lib/Canvas.js +8 -5
  10. package/lib/Container.js +9 -6
  11. package/lib/Context.d.ts +4 -5
  12. package/lib/Context.js +77 -72
  13. package/lib/DragAndDrop.d.ts +4 -3
  14. package/lib/DragAndDrop.js +39 -25
  15. package/lib/Factory.d.ts +3 -2
  16. package/lib/Factory.js +10 -9
  17. package/lib/Global.d.ts +0 -6
  18. package/lib/Global.js +2 -8
  19. package/lib/Layer.js +9 -6
  20. package/lib/Node.d.ts +21 -12
  21. package/lib/Node.js +159 -126
  22. package/lib/PointerEvents.d.ts +1 -2
  23. package/lib/PointerEvents.js +20 -18
  24. package/lib/Shape.d.ts +2 -2
  25. package/lib/Shape.js +51 -7
  26. package/lib/Stage.d.ts +19 -13
  27. package/lib/Stage.js +182 -86
  28. package/lib/Tween.js +59 -66
  29. package/lib/Util.d.ts +2 -2
  30. package/lib/Util.js +21 -16
  31. package/lib/_CoreInternals.d.ts +23 -11
  32. package/lib/_FullInternals.d.ts +23 -11
  33. package/lib/filters/Blur.js +16 -12
  34. package/lib/filters/Brighten.d.ts +1 -0
  35. package/lib/filters/Brighten.js +1 -0
  36. package/lib/filters/Brightness.d.ts +2 -1
  37. package/lib/filters/Brightness.js +4 -4
  38. package/lib/filters/Emboss.js +16 -58
  39. package/lib/filters/Kaleidoscope.js +8 -15
  40. package/lib/filters/Mask.js +8 -8
  41. package/lib/filters/Pixelate.js +16 -29
  42. package/lib/filters/Threshold.d.ts +1 -1
  43. package/lib/filters/Threshold.js +1 -1
  44. package/lib/shapes/Arc.js +5 -3
  45. package/lib/shapes/Arrow.d.ts +2 -6
  46. package/lib/shapes/Arrow.js +68 -46
  47. package/lib/shapes/Image.js +16 -19
  48. package/lib/shapes/Label.d.ts +2 -3
  49. package/lib/shapes/Label.js +7 -21
  50. package/lib/shapes/Line.d.ts +1 -1
  51. package/lib/shapes/Line.js +1 -1
  52. package/lib/shapes/Path.d.ts +11 -2
  53. package/lib/shapes/Path.js +132 -142
  54. package/lib/shapes/RegularPolygon.js +1 -1
  55. package/lib/shapes/Sprite.js +2 -6
  56. package/lib/shapes/Text.d.ts +12 -4
  57. package/lib/shapes/Text.js +96 -54
  58. package/lib/shapes/TextPath.d.ts +4 -3
  59. package/lib/shapes/TextPath.js +40 -26
  60. package/lib/shapes/Transformer.js +88 -91
  61. package/lib/types.d.ts +2 -2
  62. package/package.json +4 -3
@@ -1,4 +1,3 @@
1
- import { Konva } from "./Global.js";
2
1
  import { Util } from "./Util.js";
3
2
  export const DD = {
4
3
  get isDragging() {
@@ -10,13 +9,14 @@ export const DD = {
10
9
  });
11
10
  return flag;
12
11
  },
13
- justDragged: false,
14
12
  get node() {
15
- // return first dragging node
16
13
  let node;
17
- DD._dragElements.forEach((elem) => {
14
+ for (const elem of DD._dragElements.values()) {
15
+ if (elem.dragStatus === 'dragging')
16
+ return elem.node;
18
17
  node = elem.node;
19
- });
18
+ }
19
+ // isDragReady() also uses this getter before movement starts.
20
20
  return node;
21
21
  },
22
22
  _dragElements: new Map(),
@@ -78,10 +78,18 @@ export const DD = {
78
78
  stage.setPointersPositions(evt);
79
79
  positioned.add(stage);
80
80
  }
81
+ if (elem.pointerEventType &&
82
+ elem.pointerEventType !== stage._pointerEventType) {
83
+ return;
84
+ }
81
85
  // it is possible that user call startDrag without any event
82
86
  // it that case we need to detect first movable pointer and attach it into the node
83
87
  if (elem.pointerId === undefined) {
84
88
  elem.pointerId = Util._getFirstPointerId(evt);
89
+ elem.pointerEventType = stage._pointerEventType;
90
+ if (elem.dragStatus === 'dragging') {
91
+ stage._cancelClick(elem.pointerId, elem.pointerEventType);
92
+ }
85
93
  }
86
94
  const pos = stage._changedPointerPositions.find((pos) => pos.id === elem.pointerId);
87
95
  // not related pointer
@@ -118,11 +126,16 @@ export const DD = {
118
126
  },
119
127
  // dragBefore and dragAfter allows us to set correct order of events
120
128
  // setup all in dragbefore, and stop dragging only after pointerup triggered.
121
- _endDragBefore(evt, win) {
129
+ _endDragBefore(evt, win, only) {
122
130
  const drawNodes = [];
123
131
  const positioned = new Set();
124
132
  DD._dragElements.forEach((elem, key) => {
125
133
  const { node } = elem;
134
+ // node.stopDrag() ends only the drags of the same gesture: a Transformer
135
+ // drags its other nodes with the same pointer, another pointer is its own
136
+ if (only && elem !== only && elem.pointerId !== only.pointerId) {
137
+ return;
138
+ }
126
139
  // we need to find pointer relative to that node
127
140
  const stage = node.getStage();
128
141
  // a pointer released in another window ends the drag too - the pointer
@@ -133,31 +146,31 @@ export const DD = {
133
146
  stage.setPointersPositions(evt);
134
147
  positioned.add(stage);
135
148
  }
136
- // a drag started without an event (node.startDrag()) that has not
137
- // moved yet has no pointer: any release ends it
138
- const released = elem.pointerId === undefined ||
139
- stage._changedPointerPositions.some((pos) => pos.id === elem.pointerId);
140
- // that pointer is not related: a "ready" element of another pointer
141
- // waits for its own release
142
- if (!released) {
143
- return;
149
+ if (!only) {
150
+ if (evt &&
151
+ elem.pointerEventType &&
152
+ elem.pointerEventType !== Util._getEventType(evt.type)) {
153
+ return;
154
+ }
155
+ // a drag started without an event (node.startDrag()) that has not
156
+ // moved yet has no pointer: any release ends it
157
+ const released = elem.pointerId === undefined ||
158
+ stage._changedPointerPositions.some((pos) => pos.id === elem.pointerId);
159
+ // that pointer is not related: a "ready" element of another pointer
160
+ // waits for its own release
161
+ if (!released) {
162
+ return;
163
+ }
144
164
  }
145
165
  if (elem.dragStatus === 'ready') {
146
166
  DD._dragElements.delete(key);
147
167
  }
148
168
  else {
149
169
  // if a node is stopped manually we still need to reset events:
150
- DD.justDragged = true;
151
- Konva._mouseListenClick = false;
152
- Konva._touchListenClick = false;
153
- // for real pointer input this reset comes too late — "pointerup" fired
154
- // before this handler — so Stage._pointerup also checks the live drag
155
- // state for the pointer family (issue #1756)
156
- Konva._pointerListenClick = false;
170
+ stage._cancelClick(elem.pointerId, elem.pointerEventType);
157
171
  elem.dragStatus = 'stopped';
158
172
  // a node that has not moved needs no redraw
159
- const drawNode = elem.node.getLayer() ||
160
- (elem.node instanceof Konva['Stage'] && elem.node);
173
+ const drawNode = node.getLayer() || stage;
161
174
  if (drawNode && drawNodes.indexOf(drawNode) === -1) {
162
175
  drawNodes.push(drawNode);
163
176
  }
@@ -170,9 +183,10 @@ export const DD = {
170
183
  drawNode.draw();
171
184
  });
172
185
  },
173
- _endDragAfter(evt) {
186
+ _endDragAfter(evt, only) {
174
187
  DD._dragElements.forEach((elem, key) => {
175
- if (elem.dragStatus !== 'stopped') {
188
+ if (elem.dragStatus !== 'stopped' ||
189
+ (only && elem !== only && elem.pointerId !== only.pointerId)) {
176
190
  return;
177
191
  }
178
192
  elem.node.fire('dragend', {
package/lib/Factory.d.ts CHANGED
@@ -17,9 +17,10 @@ type Attr<T extends Constructor> = EnforceString<keyof InstanceType<T>>;
17
17
  */
18
18
  type AfterFunc<T extends Constructor> = (this: InstanceType<T>) => void;
19
19
  /**
20
- * Extracts the type of a GetSet.
20
+ * Extracts the type a GetSet returns. The type the setter accepts can be wider,
21
+ * so it is matched with `any` instead of being inferred from the getter.
21
22
  */
22
- type ExtractGetSet<T> = T extends GetSet<infer U, any> ? U : never;
23
+ type ExtractGetSet<T> = T extends GetSet<infer U, any, any> ? U : never;
23
24
  /**
24
25
  * Extracts the type of a GetSet class attribute.
25
26
  */
package/lib/Factory.js CHANGED
@@ -10,12 +10,15 @@ export const Factory = {
10
10
  },
11
11
  addGetter(constructor, attr, def) {
12
12
  const method = GET + Util._capitalize(attr);
13
- constructor.prototype[method] =
14
- constructor.prototype[method] ||
15
- function () {
16
- const val = this.attrs[attr];
17
- return val === undefined ? def : val;
18
- };
13
+ // array defaults are shared between instances, so hand out a copy
14
+ const isArr = Array.isArray(def);
15
+ const getter = function () {
16
+ const val = this.attrs[attr];
17
+ return val === undefined ? (isArr ? def.slice() : def) : val;
18
+ };
19
+ // toObject() compares an attr with the default of the generated getter
20
+ getter._def = def;
21
+ constructor.prototype[method] = constructor.prototype[method] || getter;
19
22
  },
20
23
  addSetter(constructor, attr, validator, after) {
21
24
  const method = SET + Util._capitalize(attr);
@@ -52,7 +55,7 @@ export const Factory = {
52
55
  const basicValidator = getComponentValidator(components);
53
56
  // setter
54
57
  constructor.prototype[setter] = function (val) {
55
- const oldVal = this.attrs[attr];
58
+ const oldVal = this[getter]();
56
59
  if (validator) {
57
60
  val = validator.call(this, val, attr);
58
61
  }
@@ -89,8 +92,6 @@ export const Factory = {
89
92
  // getting
90
93
  return this[getter]();
91
94
  };
92
- // marks the accessor for toObject()
93
- accessor._isAttrAccessor = true;
94
95
  constructor.prototype[attr] = accessor;
95
96
  },
96
97
  backCompat(constructor, methods) {
package/lib/Global.d.ts CHANGED
@@ -42,12 +42,6 @@ export declare const Konva: {
42
42
  * Konva.capturePointerEventsEnabled = true;
43
43
  */
44
44
  capturePointerEventsEnabled: boolean;
45
- _mouseListenClick: boolean;
46
- _touchListenClick: boolean;
47
- _pointerListenClick: boolean;
48
- _mouseInDblClickWindow: boolean;
49
- _touchInDblClickWindow: boolean;
50
- _pointerInDblClickWindow: boolean;
51
45
  _renderBackend: string;
52
46
  /**
53
47
  * Use legacy text rendering. with "middle" baseline by default.
package/lib/Global.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Konva JavaScript Framework v10.5.0
2
+ * Konva JavaScript Framework v10.6.0
3
3
  * http://konvajs.org/
4
4
  * Licensed under the MIT
5
5
  * Date: @@date
@@ -29,7 +29,7 @@ export const glob = typeof global !== 'undefined'
29
29
  : {};
30
30
  export const Konva = {
31
31
  _global: glob,
32
- version: '10.5.0',
32
+ version: '10.6.0',
33
33
  isBrowser: detectBrowser(),
34
34
  isUnminified: /param/.test(function (param) { }.toString()),
35
35
  dblClickWindow: 400,
@@ -72,12 +72,6 @@ export const Konva = {
72
72
  * Konva.capturePointerEventsEnabled = true;
73
73
  */
74
74
  capturePointerEventsEnabled: false,
75
- _mouseListenClick: false,
76
- _touchListenClick: false,
77
- _pointerListenClick: false,
78
- _mouseInDblClickWindow: false,
79
- _touchInDblClickWindow: false,
80
- _pointerInDblClickWindow: false,
81
75
  _renderBackend: 'web', // web, node-canvas, skia-canvas
82
76
  /**
83
77
  * Use legacy text rendering. with "middle" baseline by default.
package/lib/Layer.js CHANGED
@@ -177,11 +177,14 @@ export class Layer extends Container {
177
177
  return this;
178
178
  }
179
179
  remove() {
180
- const _canvas = this.getNativeCanvasElement();
181
- Node.prototype.remove.call(this);
182
- if (_canvas && _canvas.parentNode && Util._isInDocument(_canvas)) {
183
- _canvas.parentNode.removeChild(_canvas);
184
- }
180
+ var _a, _b;
181
+ super.remove();
182
+ // both canvases live in stage.content: the scene one always, the hit one
183
+ // while toggleHitCanvas() shows it
184
+ const scene = this.getNativeCanvasElement();
185
+ const hit = this.getHitCanvas()._canvas;
186
+ (_a = scene.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(scene);
187
+ (_b = hit.parentNode) === null || _b === void 0 ? void 0 : _b.removeChild(hit);
185
188
  return this;
186
189
  }
187
190
  getStage() {
@@ -398,7 +401,7 @@ export class Layer extends Container {
398
401
  let underDrag = false;
399
402
  DD._dragElements.forEach((elem) => {
400
403
  if (elem.dragStatus === 'dragging' &&
401
- (elem.node.nodeType === 'Stage' || elem.node.getLayer() === this)) {
404
+ (elem.node === this.getStage() || elem.node.getLayer() === this)) {
402
405
  underDrag = true;
403
406
  }
404
407
  });
package/lib/Node.d.ts CHANGED
@@ -8,7 +8,7 @@ import type { Stage } from './Stage.ts';
8
8
  import type { Tween, TweenConfig } from './Tween.ts';
9
9
  import type { GetSet, IRect, Vector2d } from './types.ts';
10
10
  import { Transform, type AnyString } from './Util.ts';
11
- export type FilterFunction = (this: Node, imageData: ImageData) => void;
11
+ export type FilterFunction = (this: Node, imageData: ImageData, pixelRatio?: number) => void;
12
12
  export type Filter = FilterFunction | string;
13
13
  type Filters = Array<FilterFunction | string>;
14
14
  type globalCompositeOperationType = '' | 'source-over' | 'source-in' | 'source-out' | 'source-atop' | 'destination-over' | 'destination-in' | 'destination-out' | 'destination-atop' | 'lighter' | 'copy' | 'xor' | 'multiply' | 'screen' | 'overlay' | 'darken' | 'lighten' | 'color-dodge' | 'color-burn' | 'hard-light' | 'soft-light' | 'difference' | 'exclusion' | 'hue' | 'saturation' | 'color' | 'luminosity';
@@ -207,6 +207,8 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
207
207
  /**
208
208
  * Return client rectangle {x, y, width, height} of node. This rectangle also include all styling (strokes, shadows, etc).
209
209
  * The purpose of the method is similar to getBoundingClientRect API of the DOM.
210
+ * Non-scaling stroke padding is converted from its drawing canvas to the requested coordinates.
211
+ * Cached strokes use the cache's coordinates; their pixels scale with the cached bitmap.
210
212
  * @method
211
213
  * @name Konva.Node#getClientRect
212
214
  * @param {Object} config
@@ -705,8 +707,9 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
705
707
  * @param {Number} [config.width] width of canvas section
706
708
  * @param {Number} [config.height] height of canvas section
707
709
  * @param {Number} [config.pixelRatio] pixelRatio of output canvas. Default is 1.
708
- * You can use that property to increase quality of the image, for example for super hight quality exports
709
- * or usage on retina (or similar) displays. pixelRatio will be used to multiply the size of exported image.
710
+ * Higher pixel ratios increase export resolution. Cached nodes keep their cache resolution.
711
+ * Rebuild caches at the export pixel ratio to preserve detail.
712
+ * The pixel ratio multiplies the dimensions of the exported image.
710
713
  * If you export to 500x500 size with pixelRatio = 2, then produced image will have size 1000x1000.
711
714
  * @param {Boolean} [config.imageSmoothingEnabled] set this to false if you want to disable imageSmoothing
712
715
  * @example
@@ -730,8 +733,9 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
730
733
  * you can specify the quality from 0 to 1, where 0 is very poor quality and 1
731
734
  * is very high quality
732
735
  * @param {Number} [config.pixelRatio] pixelRatio of output image url. Default is 1.
733
- * You can use that property to increase quality of the image, for example for super hight quality exports
734
- * or usage on retina (or similar) displays. pixelRatio will be used to multiply the size of exported image.
736
+ * Higher pixel ratios increase export resolution. Cached nodes keep their cache resolution.
737
+ * Rebuild caches at the export pixel ratio to preserve detail.
738
+ * The pixel ratio multiplies the dimensions of the exported image.
735
739
  * If you export to 500x500 size with pixelRatio = 2, then produced image will have size 1000x1000.
736
740
  * @param {Boolean} [config.imageSmoothingEnabled] set this to false if you want to disable imageSmoothing
737
741
  * @returns {String}
@@ -758,8 +762,9 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
758
762
  * you can specify the quality from 0 to 1, where 0 is very poor quality and 1
759
763
  * is very high quality
760
764
  * @param {Number} [config.pixelRatio] pixelRatio of output image. Default is 1.
761
- * You can use that property to increase quality of the image, for example for super hight quality exports
762
- * or usage on retina (or similar) displays. pixelRatio will be used to multiply the size of exported image.
765
+ * Higher pixel ratios increase export resolution. Cached nodes keep their cache resolution.
766
+ * Rebuild caches at the export pixel ratio to preserve detail.
767
+ * The pixel ratio multiplies the dimensions of the exported image.
763
768
  * If you export to 500x500 size with pixelRatio = 2, then produced image will have size 1000x1000.
764
769
  * @param {Boolean} [config.imageSmoothingEnabled] set this to false if you want to disable imageSmoothing
765
770
  * @return {Promise<Image>}
@@ -786,8 +791,9 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
786
791
  * @param {Number} [config.width] width of canvas section
787
792
  * @param {Number} [config.height] height of canvas section
788
793
  * @param {Number} [config.pixelRatio] pixelRatio of output canvas. Default is 1.
789
- * You can use that property to increase quality of the image, for example for super hight quality exports
790
- * or usage on retina (or similar) displays. pixelRatio will be used to multiply the size of exported image.
794
+ * Higher pixel ratios increase export resolution. Cached nodes keep their cache resolution.
795
+ * Rebuild caches at the export pixel ratio to preserve detail.
796
+ * The pixel ratio multiplies the dimensions of the exported image.
791
797
  * If you export to 500x500 size with pixelRatio = 2, then produced image will have size 1000x1000.
792
798
  * @param {Boolean} [config.imageSmoothingEnabled] set this to false if you want to disable imageSmoothing
793
799
  * @example
@@ -951,12 +957,15 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
951
957
  embossWhiteLevel: GetSet<number, this>;
952
958
  enhance: GetSet<number, this>;
953
959
  filters: GetSet<Filters, this>;
954
- position: GetSet<Vector2d, this>;
955
- absolutePosition: GetSet<Vector2d, this>;
960
+ position: GetSet<Vector2d, this, Vector2d>;
961
+ absolutePosition: GetSet<Vector2d, this, Vector2d>;
956
962
  size: GetSet<{
957
963
  width: number;
958
964
  height: number;
959
- }, this>;
965
+ }, this, {
966
+ width: number;
967
+ height: number;
968
+ }>;
960
969
  id: GetSet<string, this>;
961
970
  listening: GetSet<boolean, this>;
962
971
  name: GetSet<string, this>;