konva 10.3.3 → 10.5.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 +1576 -1636
  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 +2 -0
  16. package/lib/DragAndDrop.js +92 -21
  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 +703 -32
  28. package/lib/Node.js +1545 -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 +85 -2
  34. package/lib/Stage.js +205 -14
  35. package/lib/Tween.d.ts +159 -4
  36. package/lib/Tween.js +243 -24
  37. package/lib/Util.d.ts +148 -4
  38. package/lib/Util.js +354 -82
  39. package/lib/Validators.d.ts +0 -2
  40. package/lib/Validators.js +3 -25
  41. package/lib/_CoreInternals.d.ts +12 -6
  42. package/lib/_CoreInternals.js +1 -0
  43. package/lib/_FullInternals.d.ts +17 -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 +56 -6
  101. package/lib/shapes/Path.js +203 -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 +124 -12
  117. package/lib/shapes/Transformer.js +864 -140
  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
package/lib/Node.d.ts CHANGED
@@ -5,6 +5,7 @@ import type { Context } from './Context.ts';
5
5
  import type { Layer } from './Layer.ts';
6
6
  import type { Shape } from './Shape.ts';
7
7
  import type { Stage } from './Stage.ts';
8
+ import type { Tween, TweenConfig } from './Tween.ts';
8
9
  import type { GetSet, IRect, Vector2d } from './types.ts';
9
10
  import { Transform, type AnyString } from './Util.ts';
10
11
  export type FilterFunction = (this: Node, imageData: ImageData) => void;
@@ -33,7 +34,7 @@ export type NodeConfig = {
33
34
  offsetY?: number;
34
35
  draggable?: boolean;
35
36
  dragDistance?: number;
36
- dragBoundFunc?: (this: Node, pos: Vector2d) => Vector2d;
37
+ dragBoundFunc?: (this: Node, pos: Vector2d, evt: any) => Vector2d;
37
38
  preventDefault?: boolean;
38
39
  globalCompositeOperation?: globalCompositeOperationType;
39
40
  filters?: Filters;
@@ -50,6 +51,14 @@ export interface KonvaEventObject<EventType, This = Node> {
50
51
  currentTarget: This;
51
52
  cancelBubble: boolean;
52
53
  child?: Node;
54
+ oldVal?: any;
55
+ newVal?: any;
56
+ }
57
+ export interface GetClientRectConfig {
58
+ skipTransform?: boolean;
59
+ skipShadow?: boolean;
60
+ skipStroke?: boolean;
61
+ relativeTo?: Node;
53
62
  }
54
63
  export type KonvaEventListener<This, EventType> = (this: This, ev: KonvaEventObject<EventType, This>) => void;
55
64
  export type CanvasConfig = {
@@ -64,6 +73,14 @@ export type ImageConfig = CanvasConfig & {
64
73
  mimeType?: string;
65
74
  quality?: number;
66
75
  };
76
+ /**
77
+ * Node constructor. Nodes are entities that can be transformed, layered,
78
+ * and have bound events. The stage, layers, groups, and shapes all extend Node.
79
+ * @constructor
80
+ * @memberof Konva
81
+ * @param {Object} config
82
+ * @@nodeParams
83
+ */
67
84
  export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
68
85
  _id: number;
69
86
  eventListeners: {
@@ -74,12 +91,22 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
74
91
  };
75
92
  attrs: any;
76
93
  index: number;
77
- _allEventListeners: null | Array<Function>;
78
94
  parent: Container | null;
79
- _cache: Map<string, any>;
80
- _attachedDepsListeners: Map<string, boolean>;
81
- _lastPos: Vector2d | null;
95
+ _cache: Record<string, any>;
96
+ _canvasCache: {
97
+ scene: SceneCanvas;
98
+ filter: SceneCanvas;
99
+ hit: HitCanvas | null;
100
+ hitConfig: {
101
+ pixelRatio: number;
102
+ width: number;
103
+ height: number;
104
+ };
105
+ x: number;
106
+ y: number;
107
+ } | null;
82
108
  _attrsAffectingSize: string[];
109
+ _attrsVersion?: number;
83
110
  _batchingTransformChange: boolean;
84
111
  _needClearTransformCache: boolean;
85
112
  _filterUpToDate: boolean;
@@ -91,34 +118,129 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
91
118
  constructor(config?: Config);
92
119
  hasChildren(): boolean;
93
120
  _clearCache(attr?: string): void;
121
+ _releaseCanvasCache(): void;
94
122
  _getCache(attr: string, privateGetter: Function): any;
95
- _calculate(name: string, deps: Array<string>, getter: Function): any;
96
- _getCanvasCache(): any;
123
+ _getCanvasCache(): {
124
+ scene: SceneCanvas;
125
+ filter: SceneCanvas;
126
+ hit: HitCanvas | null;
127
+ hitConfig: {
128
+ pixelRatio: number;
129
+ width: number;
130
+ height: number;
131
+ };
132
+ x: number;
133
+ y: number;
134
+ } | null;
97
135
  _clearSelfAndDescendantCache(attr?: string): void;
98
136
  static _absTransformCascadeDepth: number;
99
137
  static _pendingAfterCascade: Array<() => void>;
100
138
  static _runAfterAbsTransformCascade(cb: () => void): void;
139
+ /**
140
+ * clear cached canvas
141
+ * @method
142
+ * @name Konva.Node#clearCache
143
+ * @returns {Konva.Node}
144
+ * @example
145
+ * node.clearCache();
146
+ */
101
147
  clearCache(): this;
148
+ /**
149
+ * cache node to improve drawing performance, apply filters, or create more accurate
150
+ * hit regions. For all basic shapes size of cache canvas will be automatically detected.
151
+ * If you need to cache your custom `Konva.Shape` instance you have to pass shape's bounding box
152
+ * properties. Look at [https://konvajs.org/docs/performance/Shape_Caching.html](https://konvajs.org/docs/performance/Shape_Caching.html) for more information.
153
+ * @method
154
+ * @name Konva.Node#cache
155
+ * @param {Object} [config]
156
+ * @param {Number} [config.x]
157
+ * @param {Number} [config.y]
158
+ * @param {Number} [config.width]
159
+ * @param {Number} [config.height]
160
+ * @param {Number} [config.offset] increase canvas size by `offset` pixel in all directions.
161
+ * @param {Boolean} [config.drawBorder] when set to true, a red border will be drawn around the cached
162
+ * region for debugging purposes
163
+ * @param {Number} [config.pixelRatio] pixel ratio of the cache canvas, the canvas pixels per CSS pixel of the node. Default is `Konva.pixelRatio`, the device pixel ratio. A higher value keeps the cache sharp when the node is scaled up.
164
+ * @param {Boolean} [config.imageSmoothingEnabled] control imageSmoothingEnabled property of created canvas for cache
165
+ * @param {Number} [config.hitCanvasPixelRatio] change quality (or pixel ratio) of cached hit canvas.
166
+ * @returns {Konva.Node}
167
+ * @example
168
+ * // cache a shape with the x,y position of the bounding box at the center and
169
+ * // the width and height of the bounding box equal to the width and height of
170
+ * // the shape obtained from shape.width() and shape.height()
171
+ * image.cache();
172
+ *
173
+ * // cache a node and define the bounding box position and size
174
+ * node.cache({
175
+ * x: -30,
176
+ * y: -30,
177
+ * width: 100,
178
+ * height: 200
179
+ * });
180
+ *
181
+ * // cache a node and draw a red border around the bounding box
182
+ * // for debugging purposes
183
+ * node.cache({
184
+ * x: -30,
185
+ * y: -30,
186
+ * width: 100,
187
+ * height: 200,
188
+ * offset : 10,
189
+ * drawBorder: true
190
+ * });
191
+ */
102
192
  cache(config?: CanvasConfig & {
103
193
  drawBorder?: boolean;
104
194
  offset?: number;
105
195
  hitCanvasPixelRatio?: number;
106
- }): this | undefined;
196
+ }): this;
107
197
  _getCachedHitCanvas(top?: Node): HitCanvas | null;
198
+ /**
199
+ * determine if node is currently cached
200
+ * @method
201
+ * @name Konva.Node#isCached
202
+ * @returns {Boolean}
203
+ */
108
204
  isCached(): boolean;
109
205
  abstract drawScene(canvas?: Canvas, top?: Node, bufferCanvas?: Canvas): void;
110
206
  abstract drawHit(canvas?: Canvas, top?: Node): void;
111
- getClientRect(config?: {
112
- skipTransform?: boolean;
113
- skipShadow?: boolean;
114
- skipStroke?: boolean;
115
- relativeTo?: Container;
116
- }): {
117
- x: number;
118
- y: number;
119
- width: number;
120
- height: number;
121
- };
207
+ /**
208
+ * Return client rectangle {x, y, width, height} of node. This rectangle also include all styling (strokes, shadows, etc).
209
+ * The purpose of the method is similar to getBoundingClientRect API of the DOM.
210
+ * @method
211
+ * @name Konva.Node#getClientRect
212
+ * @param {Object} config
213
+ * @param {Boolean} [config.skipTransform] should we apply transform to node for calculating rect?
214
+ * @param {Boolean} [config.skipShadow] should we apply shadow to the node for calculating bound box?
215
+ * @param {Boolean} [config.skipStroke] should we apply stroke to the node for calculating bound box?
216
+ * @param {Object} [config.relativeTo] calculate client rect relative to one of the parents
217
+ * @returns {Object} rect with {x, y, width, height} properties
218
+ * @example
219
+ * var rect = new Konva.Rect({
220
+ * width : 100,
221
+ * height : 100,
222
+ * x : 50,
223
+ * y : 50,
224
+ * strokeWidth : 4,
225
+ * stroke : 'black',
226
+ * offsetX : 50,
227
+ * scaleY : 2
228
+ * });
229
+ *
230
+ * // get client rect without think off transformations (position, rotation, scale, offset, etc)
231
+ * rect.getClientRect({ skipTransform: true});
232
+ * // returns {
233
+ * // x : -2, // two pixels for stroke / 2
234
+ * // y : -2,
235
+ * // width : 104, // increased by 4 for stroke
236
+ * // height : 104
237
+ * //}
238
+ *
239
+ * // get client rect with transformation applied
240
+ * rect.getClientRect();
241
+ * // returns Object {x: -2, y: 46, width: 104, height: 208}
242
+ */
243
+ getClientRect(config?: GetClientRectConfig): IRect;
122
244
  _transformedRect(rect: IRect, top?: Node | null): {
123
245
  x: number;
124
246
  y: number;
@@ -130,28 +252,164 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
130
252
  _getCachedSceneCanvas(): Canvas;
131
253
  on<K extends keyof NodeEventMap>(evtStr: K, handler: KonvaEventListener<this, NodeEventMap[K]>): this;
132
254
  on<K extends keyof NodeEventMap>(evtStr: K, selector: string, handler: KonvaEventListener<Node, NodeEventMap[K]>): this;
255
+ /**
256
+ * remove event bindings from the node. Pass in a string of
257
+ * event types delimmited by a space to remove multiple event
258
+ * bindings at once such as 'mousedown mouseup mousemove'.
259
+ * include a namespace to remove an event binding by name
260
+ * such as 'click.foobar'. If you only give a name like '.foobar',
261
+ * all events in that namespace will be removed.
262
+ * @method
263
+ * @name Konva.Node#off
264
+ * @param {String} evtStr e.g. 'click', 'mousedown touchstart', '.foobar'
265
+ * @returns {Konva.Node}
266
+ * @example
267
+ * // remove listener
268
+ * node.off('click');
269
+ *
270
+ * // remove multiple listeners
271
+ * node.off('click touchstart');
272
+ *
273
+ * // remove listener by name
274
+ * node.off('click.foo');
275
+ */
276
+ _prepareListeners(): void;
133
277
  off(evtStr?: string, callback?: Function): this;
134
278
  dispatchEvent(evt: any): this;
135
279
  addEventListener(type: string, handler: (e: Event) => void): this;
136
280
  removeEventListener(type: string): this;
137
281
  _delegate(event: string, selector: string, handler: (e: Event) => void): this;
282
+ /**
283
+ * remove a node from parent, but don't destroy. You can reuse the node later.
284
+ * @method
285
+ * @name Konva.Node#remove
286
+ * @returns {Konva.Node}
287
+ * @example
288
+ * node.remove();
289
+ */
138
290
  remove(): this;
139
291
  _clearCaches(): void;
140
292
  _remove(): void;
293
+ /**
294
+ * remove and destroy a node. Kill it and delete forever! You should not reuse node after destroy().
295
+ * If the node is a container (Group, Stage or Layer) it will destroy all children too.
296
+ * The node fires a `destroy` event first, so anything holding a reference to it can let go.
297
+ * When an ancestor is being destroyed the node is already detached from its parent at that point.
298
+ * @method
299
+ * @name Konva.Node#destroy
300
+ * @example
301
+ * node.on('destroy', () => console.log('gone'));
302
+ * node.destroy();
303
+ */
141
304
  destroy(): this;
305
+ /**
306
+ * get attr
307
+ * @method
308
+ * @name Konva.Node#getAttr
309
+ * @param {String} attr
310
+ * @returns {Integer|String|Object|Array}
311
+ * @example
312
+ * var x = node.getAttr('x');
313
+ */
142
314
  getAttr<AttrConfig extends Config, K extends AnyString<keyof Config>>(attr: K): K extends keyof AttrConfig ? AttrConfig[K] : any;
315
+ /**
316
+ * get ancestors
317
+ * @method
318
+ * @name Konva.Node#getAncestors
319
+ * @returns {Array}
320
+ * @example
321
+ * shape.getAncestors().forEach(function(node) {
322
+ * console.log(node.id());
323
+ * })
324
+ */
143
325
  getAncestors(): Node<NodeConfig>[];
326
+ /**
327
+ * get attrs object literal
328
+ * @method
329
+ * @name Konva.Node#getAttrs
330
+ * @returns {Object}
331
+ */
144
332
  getAttrs(): Config;
333
+ /**
334
+ * set multiple attrs at once using an object literal
335
+ * @method
336
+ * @name Konva.Node#setAttrs
337
+ * @param {Object} config object containing key value pairs
338
+ * @returns {Konva.Node}
339
+ * @example
340
+ * node.setAttrs({
341
+ * x: 5,
342
+ * fill: 'red'
343
+ * });
344
+ */
145
345
  setAttrs(config?: Config): this;
346
+ /**
347
+ * determine if node is listening for events by taking into account ancestors.
348
+ *
349
+ * Parent | Self | isListening
350
+ * listening | listening |
351
+ * ----------+-----------+------------
352
+ * T | T | T
353
+ * T | F | F
354
+ * F | T | F
355
+ * F | F | F
356
+ *
357
+ * @method
358
+ * @name Konva.Node#isListening
359
+ * @returns {Boolean}
360
+ */
146
361
  isListening(): any;
147
362
  _isListening(relativeTo?: Node): boolean;
363
+ /**
364
+ * determine if node is visible by taking into account ancestors.
365
+ *
366
+ * Parent | Self | isVisible
367
+ * visible | visible |
368
+ * ----------+-----------+------------
369
+ * T | T | T
370
+ * T | F | F
371
+ * F | T | F
372
+ * F | F | F
373
+ * @method
374
+ * @name Konva.Node#isVisible
375
+ * @returns {Boolean}
376
+ */
148
377
  isVisible(): any;
149
378
  _isVisible(relativeTo?: Node): boolean;
150
- shouldDrawHit(top?: Node, skipDragCheck?: boolean): any;
379
+ shouldDrawHit(top?: Node): any;
380
+ /**
381
+ * show node. set visible = true
382
+ * @method
383
+ * @name Konva.Node#show
384
+ * @returns {Konva.Node}
385
+ */
151
386
  show(): this;
387
+ /**
388
+ * hide node. Hidden nodes are no longer detectable
389
+ * @method
390
+ * @name Konva.Node#hide
391
+ * @returns {Konva.Node}
392
+ */
152
393
  hide(): this;
153
394
  getZIndex(): number;
395
+ /**
396
+ * get absolute z-index which takes into account sibling
397
+ * and ancestor indices
398
+ * @method
399
+ * @name Konva.Node#getAbsoluteZIndex
400
+ * @returns {Integer}
401
+ */
154
402
  getAbsoluteZIndex(): number;
403
+ /**
404
+ * get node depth in node tree. Returns an integer.
405
+ * e.g. Stage depth will always be 0. Layers will always be 1. Groups and Shapes will always
406
+ * be >= 2
407
+ * @method
408
+ * @name Konva.Node#getDepth
409
+ * @returns {Integer}
410
+ * @example
411
+ * var depth = shape.getDepth(); // 2 for a shape right inside of a layer
412
+ */
155
413
  getDepth(): number;
156
414
  _batchTransformChanges(func: any): void;
157
415
  setPosition(pos: Vector2d): this;
@@ -159,10 +417,37 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
159
417
  x: number;
160
418
  y: number;
161
419
  };
420
+ /**
421
+ * get position of first pointer (like mouse or first touch) relative to local coordinates of current node
422
+ * @method
423
+ * @name Konva.Node#getRelativePointerPosition
424
+ * @returns {Konva.Node}
425
+ * @example
426
+ *
427
+ * // let's think we have a rectangle at position x = 10, y = 10
428
+ * // now we clicked at x = 15, y = 15 of the stage
429
+ * // if you want to know position of the click, related to the rectangle you can use
430
+ * rect.getRelativePointerPosition();
431
+ */
162
432
  getRelativePointerPosition(): {
163
433
  x: number;
164
434
  y: number;
165
435
  } | null;
436
+ /**
437
+ * get absolute position of a node. That function can be used to calculate absolute position, but relative to any ancestor
438
+ * @method
439
+ * @name Konva.Node#getAbsolutePosition
440
+ * @param {Object} [top] optional ancestor node
441
+ * @returns {Konva.Node}
442
+ * @example
443
+ *
444
+ * // returns absolute position relative to top-left corner of canvas
445
+ * node.getAbsolutePosition();
446
+ *
447
+ * // calculate absolute position of node, inside stage
448
+ * // so stage transforms are ignored
449
+ * node.getAbsolutePosition(stage)
450
+ */
166
451
  getAbsolutePosition(top?: Node): {
167
452
  x: number;
168
453
  y: number;
@@ -180,84 +465,461 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
180
465
  skewX: number;
181
466
  skewY: number;
182
467
  };
468
+ /**
469
+ * move node by an amount relative to its current position
470
+ * @method
471
+ * @name Konva.Node#move
472
+ * @param {Object} change
473
+ * @param {Number} change.x
474
+ * @param {Number} change.y
475
+ * @returns {Konva.Node}
476
+ * @example
477
+ * // move node in x direction by 1px and y direction by 2px
478
+ * node.move({
479
+ * x: 1,
480
+ * y: 2
481
+ * });
482
+ */
183
483
  move(change: Vector2d): this;
184
- _eachAncestorReverse(func: any, top: any): void;
484
+ /**
485
+ * rotate node by an amount in degrees relative to its current rotation
486
+ * @method
487
+ * @name Konva.Node#rotate
488
+ * @param {Number} theta
489
+ * @returns {Konva.Node}
490
+ */
185
491
  rotate(theta: number): this;
492
+ /**
493
+ * move node to the top of its siblings
494
+ * @method
495
+ * @name Konva.Node#moveToTop
496
+ * @returns {Boolean}
497
+ */
186
498
  moveToTop(): boolean;
499
+ /**
500
+ * move node up
501
+ * @method
502
+ * @name Konva.Node#moveUp
503
+ * @returns {Boolean} flag is moved or not
504
+ */
187
505
  moveUp(): boolean;
506
+ /**
507
+ * move node down
508
+ * @method
509
+ * @name Konva.Node#moveDown
510
+ * @returns {Boolean}
511
+ */
188
512
  moveDown(): boolean;
513
+ /**
514
+ * move node to the bottom of its siblings
515
+ * @method
516
+ * @name Konva.Node#moveToBottom
517
+ * @returns {Boolean}
518
+ */
189
519
  moveToBottom(): boolean;
190
520
  setZIndex(zIndex: any): this;
521
+ /**
522
+ * get absolute opacity
523
+ * @method
524
+ * @name Konva.Node#getAbsoluteOpacity
525
+ * @returns {Number}
526
+ */
191
527
  getAbsoluteOpacity(): any;
192
528
  _getAbsoluteOpacity(): number;
529
+ /**
530
+ * move node to another container
531
+ * @method
532
+ * @name Konva.Node#moveTo
533
+ * @param {Container} newContainer
534
+ * @returns {Konva.Node}
535
+ * @example
536
+ * // move node from current layer into layer2
537
+ * node.moveTo(layer2);
538
+ */
193
539
  moveTo(newContainer: any): this;
540
+ /**
541
+ * convert Node into an object for serialization. Returns an object.
542
+ * @method
543
+ * @name Konva.Node#toObject
544
+ * @returns {Object}
545
+ */
194
546
  toObject(): {
195
547
  attrs: Config & Record<string, any>;
196
548
  className: string;
197
549
  children?: Array<any>;
198
550
  };
551
+ /**
552
+ * convert Node into a JSON string. Returns a JSON string.
553
+ * @method
554
+ * @name Konva.Node#toJSON
555
+ * @returns {String}
556
+ */
199
557
  toJSON(): string;
558
+ /**
559
+ * get parent container
560
+ * @method
561
+ * @name Konva.Node#getParent
562
+ * @returns {Konva.Node}
563
+ */
200
564
  getParent(): Container<Node<NodeConfig>, import("./Container.ts").ContainerConfig> | null;
565
+ /**
566
+ * get all ancestors (parent then parent of the parent, etc) of the node
567
+ * @method
568
+ * @name Konva.Node#findAncestors
569
+ * @param {String} selector selector for search
570
+ * @param {Boolean} [includeSelf] show we think that node is ancestro itself?
571
+ * @param {Konva.Node} [stopNode] optional node where we need to stop searching (one of ancestors)
572
+ * @returns {Array} [ancestors]
573
+ * @example
574
+ * // get one of the parent group
575
+ * var parentGroups = node.findAncestors('Group');
576
+ */
201
577
  findAncestors(selector: string | Function, includeSelf?: boolean, stopNode?: Node): Node<NodeConfig>[];
202
578
  isAncestorOf(node: Node): boolean;
579
+ /**
580
+ * get ancestor (parent or parent of the parent, etc) of the node that match passed selector
581
+ * @method
582
+ * @name Konva.Node#findAncestor
583
+ * @param {String} selector selector for search
584
+ * @param {Boolean} [includeSelf] show we think that node is ancestro itself?
585
+ * @param {Konva.Node} [stopNode] optional node where we need to stop searching (one of ancestors)
586
+ * @returns {Konva.Node} ancestor
587
+ * @example
588
+ * // get one of the parent group
589
+ * var group = node.findAncestors('.mygroup');
590
+ */
203
591
  findAncestor(selector: string | Function, includeSelf?: boolean, stopNode?: Container): Node<NodeConfig>;
204
592
  _isMatch(selector: string | Function): any;
593
+ /**
594
+ * get layer ancestor
595
+ * @method
596
+ * @name Konva.Node#getLayer
597
+ * @returns {Konva.Layer}
598
+ */
205
599
  getLayer(): Layer | null;
600
+ /**
601
+ * get stage ancestor
602
+ * @method
603
+ * @name Konva.Node#getStage
604
+ * @returns {Konva.Stage}
605
+ */
206
606
  getStage(): Stage | null;
207
607
  _getStage(): Stage | null;
608
+ /**
609
+ * fire event
610
+ * @method
611
+ * @name Konva.Node#fire
612
+ * @param {String} eventType event type. can be a regular event, like click, mouseover, or mouseout, or it can be a custom event, like myCustomEvent
613
+ * @param {Event} [evt] event object
614
+ * @param {Boolean} [bubble] setting the value to false, or leaving it undefined, will result in the event
615
+ * not bubbling. Setting the value to true will result in the event bubbling.
616
+ * @returns {Konva.Node}
617
+ * @example
618
+ * // manually fire click event
619
+ * node.fire('click');
620
+ *
621
+ * // fire custom event
622
+ * node.fire('foo');
623
+ *
624
+ * // fire custom event with custom event object
625
+ * node.fire('foo', {
626
+ * bar: 10
627
+ * });
628
+ *
629
+ * // fire click event that bubbles
630
+ * node.fire('click', null, true);
631
+ */
208
632
  fire(eventType: string, evt?: any, bubble?: boolean): this;
633
+ /**
634
+ * get absolute transform of the node which takes into
635
+ * account its ancestor transforms
636
+ * @method
637
+ * @name Konva.Node#getAbsoluteTransform
638
+ * @returns {Konva.Transform}
639
+ */
209
640
  getAbsoluteTransform(top?: Node | null): Transform;
210
- _getAbsoluteTransform(top?: Node): Transform;
641
+ _hasCachedAncestor(): boolean;
642
+ _getAbsoluteTransform(top?: Node | null): Transform;
643
+ _getCachedAbsoluteTransform(): Transform;
644
+ _multiplyOwnTransform(at: Transform): Transform;
645
+ /**
646
+ * get absolute scale of the node which takes into
647
+ * account its ancestor scales
648
+ * @method
649
+ * @name Konva.Node#getAbsoluteScale
650
+ * @returns {Object}
651
+ * @example
652
+ * // get absolute scale x
653
+ * var scaleX = node.getAbsoluteScale().x;
654
+ */
211
655
  getAbsoluteScale(top?: Node): {
212
656
  x: number;
213
657
  y: number;
214
658
  };
659
+ /**
660
+ * get absolute rotation of the node which takes into
661
+ * account its ancestor rotations
662
+ * @method
663
+ * @name Konva.Node#getAbsoluteRotation
664
+ * @returns {Number}
665
+ * @example
666
+ * // get absolute rotation
667
+ * var rotation = node.getAbsoluteRotation();
668
+ */
215
669
  getAbsoluteRotation(): number;
670
+ /**
671
+ * get transform of the node
672
+ * @method
673
+ * @name Konva.Node#getTransform
674
+ * @returns {Konva.Transform}
675
+ */
216
676
  getTransform(): Transform;
217
677
  _getTransform(): Transform;
678
+ /**
679
+ * clone node. Returns a new Node instance with identical attributes. You can also override
680
+ * the node properties with an object literal, enabling you to use an existing node as a template
681
+ * for another node
682
+ * @method
683
+ * @name Konva.Node#clone
684
+ * @param {Object} obj override attrs
685
+ * @returns {Konva.Node}
686
+ * @example
687
+ * // simple clone
688
+ * var clone = node.clone();
689
+ *
690
+ * // clone a node and override the x position
691
+ * var clone = rect.clone({
692
+ * x: 5
693
+ * });
694
+ */
218
695
  clone(obj?: any): any;
219
696
  _toKonvaCanvas(config: any): SceneCanvas;
697
+ /**
698
+ * converts node into an canvas element.
699
+ * @method
700
+ * @name Konva.Node#toCanvas
701
+ * @param {Object} config
702
+ * @param {Function} config.callback function executed when the composite has completed
703
+ * @param {Number} [config.x] x position of canvas section
704
+ * @param {Number} [config.y] y position of canvas section
705
+ * @param {Number} [config.width] width of canvas section
706
+ * @param {Number} [config.height] height of canvas section
707
+ * @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
+ * If you export to 500x500 size with pixelRatio = 2, then produced image will have size 1000x1000.
711
+ * @param {Boolean} [config.imageSmoothingEnabled] set this to false if you want to disable imageSmoothing
712
+ * @example
713
+ * var canvas = node.toCanvas();
714
+ */
220
715
  toCanvas(config?: CanvasConfig): HTMLCanvasElement;
716
+ /**
717
+ * Creates a composite data URL (base64 string). If MIME type is not
718
+ * specified, then "image/png" will result. For "image/jpeg", specify a quality
719
+ * level as quality (range 0.0 - 1.0)
720
+ * @method
721
+ * @name Konva.Node#toDataURL
722
+ * @param {Object} config
723
+ * @param {String} [config.mimeType] can be "image/png" or "image/jpeg".
724
+ * "image/png" is the default
725
+ * @param {Number} [config.x] x position of canvas section
726
+ * @param {Number} [config.y] y position of canvas section
727
+ * @param {Number} [config.width] width of canvas section
728
+ * @param {Number} [config.height] height of canvas section
729
+ * @param {Number} [config.quality] jpeg quality. If using an "image/jpeg" mimeType,
730
+ * you can specify the quality from 0 to 1, where 0 is very poor quality and 1
731
+ * is very high quality
732
+ * @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.
735
+ * If you export to 500x500 size with pixelRatio = 2, then produced image will have size 1000x1000.
736
+ * @param {Boolean} [config.imageSmoothingEnabled] set this to false if you want to disable imageSmoothing
737
+ * @returns {String}
738
+ */
221
739
  toDataURL(config?: ImageConfig & {
222
740
  callback?: (url: string) => void;
223
741
  }): string;
742
+ /**
743
+ * converts node into an image. Since the toImage
744
+ * method is asynchronous, the resulting image can only be retrieved from the config callback
745
+ * or the returned Promise. toImage is most commonly used
746
+ * to cache complex drawings as an image so that they don't have to constantly be redrawn
747
+ * @method
748
+ * @name Konva.Node#toImage
749
+ * @param {Object} config
750
+ * @param {Function} [config.callback] function executed when the composite has completed
751
+ * @param {String} [config.mimeType] can be "image/png" or "image/jpeg".
752
+ * "image/png" is the default
753
+ * @param {Number} [config.x] x position of canvas section
754
+ * @param {Number} [config.y] y position of canvas section
755
+ * @param {Number} [config.width] width of canvas section
756
+ * @param {Number} [config.height] height of canvas section
757
+ * @param {Number} [config.quality] jpeg quality. If using an "image/jpeg" mimeType,
758
+ * you can specify the quality from 0 to 1, where 0 is very poor quality and 1
759
+ * is very high quality
760
+ * @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.
763
+ * If you export to 500x500 size with pixelRatio = 2, then produced image will have size 1000x1000.
764
+ * @param {Boolean} [config.imageSmoothingEnabled] set this to false if you want to disable imageSmoothing
765
+ * @return {Promise<Image>}
766
+ * @example
767
+ * var image = node.toImage({
768
+ * callback(img) {
769
+ * // do stuff with img
770
+ * }
771
+ * });
772
+ */
224
773
  toImage(config?: ImageConfig & {
225
774
  callback?: (img: HTMLImageElement) => void;
226
775
  }): Promise<HTMLImageElement>;
776
+ /**
777
+ * Converts node into a blob. Since the toBlob method is asynchronous,
778
+ * the resulting blob can only be retrieved from the config callback
779
+ * or the returned Promise.
780
+ * @method
781
+ * @name Konva.Node#toBlob
782
+ * @param {Object} config
783
+ * @param {Function} [config.callback] function executed when the composite has completed
784
+ * @param {Number} [config.x] x position of canvas section
785
+ * @param {Number} [config.y] y position of canvas section
786
+ * @param {Number} [config.width] width of canvas section
787
+ * @param {Number} [config.height] height of canvas section
788
+ * @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.
791
+ * If you export to 500x500 size with pixelRatio = 2, then produced image will have size 1000x1000.
792
+ * @param {Boolean} [config.imageSmoothingEnabled] set this to false if you want to disable imageSmoothing
793
+ * @example
794
+ * var blob = await node.toBlob({});
795
+ * @returns {Promise<Blob>} rejects if the canvas can not be encoded
796
+ */
227
797
  toBlob(config?: ImageConfig & {
228
- callback?: (blob: Blob | null) => void;
229
- }): Promise<unknown>;
798
+ callback?: (blob: Blob) => void;
799
+ }): Promise<Blob>;
230
800
  setSize(size: any): this;
231
801
  getSize(): {
232
802
  width: number;
233
803
  height: number;
234
804
  };
805
+ /**
806
+ * get class name, which may return Stage, Layer, Group, or shape class names like Rect, Circle, Text, etc.
807
+ * @method
808
+ * @name Konva.Node#getClassName
809
+ * @returns {String}
810
+ */
235
811
  getClassName(): string;
812
+ /**
813
+ * get the node type, which may return Stage, Layer, Group, or Shape
814
+ * @method
815
+ * @name Konva.Node#getType
816
+ * @returns {String}
817
+ */
236
818
  getType(): string;
237
819
  getDragDistance(): number;
238
820
  _off(type: any, name?: any, callback?: any): void;
239
821
  _fireChangeEvent(attr: any, oldVal: any, newVal: any): void;
822
+ /**
823
+ * add name to node
824
+ * @method
825
+ * @name Konva.Node#addName
826
+ * @param {String} name
827
+ * @returns {Konva.Node}
828
+ * @example
829
+ * node.name('red');
830
+ * node.addName('selected');
831
+ * node.name(); // return 'red selected'
832
+ */
240
833
  addName(name: string): this;
834
+ /**
835
+ * check is node has name
836
+ * @method
837
+ * @name Konva.Node#hasName
838
+ * @param {String} name
839
+ * @returns {Boolean}
840
+ * @example
841
+ * node.name('red');
842
+ * node.hasName('red'); // return true
843
+ * node.hasName('selected'); // return false
844
+ * node.hasName(''); // return false
845
+ */
241
846
  hasName(name: any): boolean;
847
+ /**
848
+ * remove name from node
849
+ * @method
850
+ * @name Konva.Node#removeName
851
+ * @param {String} name
852
+ * @returns {Konva.Node}
853
+ * @example
854
+ * node.name('red selected');
855
+ * node.removeName('selected');
856
+ * node.hasName('selected'); // return false
857
+ * node.name(); // return 'red'
858
+ */
242
859
  removeName(name: any): this;
860
+ /**
861
+ * set attr
862
+ * @method
863
+ * @name Konva.Node#setAttr
864
+ * @param {String} attr
865
+ * @param {*} val
866
+ * @returns {Konva.Node}
867
+ * @example
868
+ * node.setAttr('x', 5);
869
+ */
243
870
  setAttr<AttrConfig extends Config, K extends AnyString<keyof Config>>(attr: K, val: K extends keyof AttrConfig ? AttrConfig[K] : any): this;
244
871
  _requestDraw(): void;
245
872
  _setAttr<AttrConfig extends Config, K extends AnyString<keyof Config>>(key: K, val: K extends keyof AttrConfig ? AttrConfig[K] : any): void;
246
- _setComponentAttr(key: any, component: any, val: any): void;
247
873
  _fireAndBubble(eventType: any, evt: any, compareShape?: any): void;
248
- static protoListenerMap: Map<string, any>;
874
+ static protoListenerMap: WeakMap<object, any>;
249
875
  _getProtoListeners(eventType: any): any;
250
876
  _fire(eventType: any, evt: any): void;
877
+ /**
878
+ * draw both scene and hit graphs. If the node being drawn is the stage, all of the layers will be cleared and redrawn
879
+ * @method
880
+ * @name Konva.Node#draw
881
+ * @returns {Konva.Node}
882
+ */
251
883
  draw(): this;
252
884
  _createDragElement(evt: any): void;
885
+ /**
886
+ * initiate drag and drop.
887
+ * @method
888
+ * @name Konva.Node#startDrag
889
+ */
253
890
  startDrag(evt?: any, bubbleEvent?: boolean): void;
254
891
  _setDragPosition(evt: any, elem: any): void;
892
+ /**
893
+ * stop drag and drop
894
+ * @method
895
+ * @name Konva.Node#stopDrag
896
+ */
255
897
  stopDrag(evt?: any): void;
256
898
  setDraggable(draggable: any): void;
899
+ /**
900
+ * determine if node is currently in drag and drop mode
901
+ * @method
902
+ * @name Konva.Node#isDragging
903
+ */
257
904
  isDragging(): boolean;
258
905
  _listenDrag(): void;
259
906
  _dragChange(): void;
260
907
  _dragCleanup(): void;
908
+ /**
909
+ * determine if node (at least partially) is currently in user-visible area
910
+ * @method
911
+ * @param {(Number | Object)} margin optional margin in pixels
912
+ * @param {Number} margin.x
913
+ * @param {Number} margin.y
914
+ * @returns {Boolean}
915
+ * @name Konva.Node#isClientRectOnScreen
916
+ * @example
917
+ * // get index
918
+ * // default calculations
919
+ * var isOnScreen = node.isClientRectOnScreen()
920
+ * // increase object size (or screen size) for cases when objects close to the screen still need to be marked as "visible"
921
+ * var isOnScreen = node.isClientRectOnScreen({ x: stage.width(), y: stage.height() })
922
+ */
261
923
  isClientRectOnScreen(margin?: {
262
924
  x: number;
263
925
  y: number;
@@ -310,7 +972,7 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
310
972
  skew: GetSet<Vector2d, this>;
311
973
  skewX: GetSet<number, this>;
312
974
  skewY: GetSet<number, this>;
313
- to: (params: AnimTo) => void;
975
+ to: (params: Omit<TweenConfig, 'node'>) => Tween;
314
976
  transformsEnabled: GetSet<string, this>;
315
977
  visible: GetSet<boolean, this>;
316
978
  width: GetSet<number, this>;
@@ -318,12 +980,21 @@ export declare abstract class Node<Config extends NodeConfig = NodeConfig> {
318
980
  x: GetSet<number, this>;
319
981
  y: GetSet<number, this>;
320
982
  globalCompositeOperation: GetSet<globalCompositeOperationType, this>;
983
+ /**
984
+ * create node with JSON string or an Object. Deserialization restores attributes only,
985
+ * not functions, images or event handlers (that would make the serialized object huge):
986
+ * `sceneFunc`/`hitFunc` of custom shapes, images and `fillPatternImage`, `clipFunc`,
987
+ * `dragBoundFunc`, filter functions, the `nodes`, `boundBoxFunc` and `anchorStyleFunc`
988
+ * of a Transformer, typed arrays and the `cache()` state. If your app uses them (it probably
989
+ * does), select the nodes after loading the stage and set these properties again with
990
+ * `on()`, `sceneFunc()`, `image()` and so on
991
+ * @method
992
+ * @memberof Konva.Node
993
+ * @param {String|Object} data string or object
994
+ * @param {Element} [container] optional container dom element used only if you're
995
+ * creating a stage node
996
+ */
321
997
  static create(data: any, container?: any): any;
322
998
  static _createNode(obj: any, container?: any): any;
323
999
  }
324
- interface AnimTo extends NodeConfig {
325
- onFinish?: Function;
326
- onUpdate?: Function;
327
- duration?: number;
328
- }
329
1000
  export {};