konva 10.4.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 (63) hide show
  1. package/README.md +4 -4
  2. package/konva.js +3432 -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 +5 -3
  14. package/lib/DragAndDrop.js +65 -29
  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 +33 -15
  21. package/lib/Node.js +179 -135
  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 +23 -14
  27. package/lib/Stage.js +221 -93
  28. package/lib/Tween.js +59 -66
  29. package/lib/Util.d.ts +3 -2
  30. package/lib/Util.js +43 -16
  31. package/lib/_CoreInternals.d.ts +25 -11
  32. package/lib/_FullInternals.d.ts +25 -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 +27 -2
  53. package/lib/shapes/Path.js +148 -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.d.ts +16 -1
  61. package/lib/shapes/Transformer.js +209 -130
  62. package/lib/types.d.ts +2 -2
  63. package/package.json +4 -3
@@ -3,17 +3,18 @@ const Captures = new Map();
3
3
  // we may use this module for capturing touch events too
4
4
  // so make sure we don't do something super specific to pointer
5
5
  const SUPPORT_POINTER_EVENTS = Konva._global['PointerEvent'] !== undefined;
6
- export function getCapturedShape(pointerId) {
7
- return Captures.get(pointerId);
6
+ export function getCapturedShape(pointerId, stage) {
7
+ const capture = Captures.get(pointerId);
8
+ return capture && (!stage || capture.stage === stage)
9
+ ? capture.shape
10
+ : undefined;
8
11
  }
9
- export function createEvent(evt) {
10
- return {
11
- evt,
12
- pointerId: evt.pointerId,
13
- };
12
+ function fireCapture(shape, type, pointerId) {
13
+ shape._fire(type, { evt: new PointerEvent(type, { pointerId }), pointerId });
14
14
  }
15
15
  export function hasPointerCapture(pointerId, shape) {
16
- return Captures.get(pointerId) === shape;
16
+ var _a;
17
+ return ((_a = Captures.get(pointerId)) === null || _a === void 0 ? void 0 : _a.shape) === shape;
17
18
  }
18
19
  export function setPointerCapture(pointerId, shape) {
19
20
  var _a;
@@ -21,7 +22,8 @@ export function setPointerCapture(pointerId, shape) {
21
22
  const stage = shape.getStage();
22
23
  if (!stage)
23
24
  return;
24
- Captures.set(pointerId, shape);
25
+ // Native capture belongs to this stage even if the shape is reparented.
26
+ Captures.set(pointerId, { shape, stage });
25
27
  if (SUPPORT_POINTER_EVENTS) {
26
28
  // capture on the DOM level too, so the stage keeps receiving the events
27
29
  // of that pointer even when it moves outside of the stage container
@@ -34,32 +36,32 @@ export function setPointerCapture(pointerId, shape) {
34
36
  // ids of mouse and touch events (999 and touch identifiers) and
35
37
  // programmatic calls outside of a pointer event land here
36
38
  }
37
- shape._fire('gotpointercapture', createEvent(new PointerEvent('gotpointercapture')));
39
+ fireCapture(shape, 'gotpointercapture', pointerId);
38
40
  }
39
41
  }
40
- // a destroyed node must not keep receiving the events of its pointer
42
+ // Destroying either the captured node or its native stage ends capture.
41
43
  export function releaseCapturesOf(node) {
42
- Captures.forEach((shape, pointerId) => {
43
- if (shape === node) {
44
+ Captures.forEach(({ shape, stage }, pointerId) => {
45
+ if (shape === node || stage === node) {
44
46
  releaseCapture(pointerId);
45
47
  }
46
48
  });
47
49
  }
48
50
  export function releaseCapture(pointerId, target) {
49
51
  var _a;
50
- const shape = Captures.get(pointerId);
52
+ const capture = Captures.get(pointerId);
51
53
  // a node can only release its own capture
52
- if (!shape || (target && shape !== target))
54
+ if (!capture || (target && capture.shape !== target))
53
55
  return;
54
- const stage = shape.getStage();
56
+ const { shape, stage } = capture;
55
57
  Captures.delete(pointerId);
56
58
  if (SUPPORT_POINTER_EVENTS) {
57
59
  try {
58
- (_a = stage === null || stage === void 0 ? void 0 : stage.content) === null || _a === void 0 ? void 0 : _a.releasePointerCapture(pointerId);
60
+ (_a = stage.content) === null || _a === void 0 ? void 0 : _a.releasePointerCapture(pointerId);
59
61
  }
60
62
  catch (e) {
61
63
  // same as in setPointerCapture: the pointer may not be active
62
64
  }
63
- shape._fire('lostpointercapture', createEvent(new PointerEvent('lostpointercapture')));
65
+ fireCapture(shape, 'lostpointercapture', pointerId);
64
66
  }
65
67
  }
package/lib/Shape.d.ts CHANGED
@@ -191,10 +191,10 @@ export declare class Shape<Config extends ShapeConfig = ShapeConfig> extends Nod
191
191
  height: number;
192
192
  };
193
193
  getClientRect(config?: ShapeGetClientRectConfig): {
194
- width: number;
195
- height: number;
196
194
  x: number;
197
195
  y: number;
196
+ width: number;
197
+ height: number;
198
198
  };
199
199
  drawScene(can?: SceneCanvas, top?: Node, bufferCanvas?: SceneCanvas): this;
200
200
  drawHit(can?: HitCanvas, top?: Node): this;
package/lib/Shape.js CHANGED
@@ -306,7 +306,7 @@ export class Shape extends Node {
306
306
  }
307
307
  const hasFill = forceFill || this.hasFill();
308
308
  const hasStroke = this.hasStroke();
309
- const isTransparent = this.getAbsoluteOpacity() !== 1;
309
+ const isTransparent = !this._isUnderCache && this.getAbsoluteOpacity() !== 1;
310
310
  if (hasFill && hasStroke && isTransparent) {
311
311
  return true;
312
312
  }
@@ -361,8 +361,42 @@ export class Shape extends Node {
361
361
  const fillRect = this.getSelfRect();
362
362
  const applyStroke = !config.skipStroke && this.hasStroke();
363
363
  const strokeWidth = (applyStroke && this.strokeWidth()) || 0;
364
- const fillAndStrokeWidth = fillRect.width + strokeWidth;
365
- const fillAndStrokeHeight = fillRect.height + strokeWidth;
364
+ let strokeWidthX = strokeWidth;
365
+ let strokeWidthY = strokeWidth;
366
+ let collapsedStroke;
367
+ if (strokeWidth && !this.strokeScaleEnabled()) {
368
+ // Strokes are constant in the canvas they are drawn into. A cached
369
+ // ancestor supplies that coordinate space until its bitmap is rebuilt.
370
+ let top = this;
371
+ while (top && !top.isCached() && !top._isUnderCache)
372
+ top = top.parent;
373
+ const [a, b, c, d] = this.getAbsoluteTransform(top).getMatrix();
374
+ const determinant = Math.abs(a * d - b * c);
375
+ if (determinant) {
376
+ strokeWidthX *= Math.hypot(c, d) / determinant;
377
+ strokeWidthY *= Math.hypot(a, b) / determinant;
378
+ }
379
+ else if (!skipTransform) {
380
+ // A collapsed path can still draw a stroke after the context resets
381
+ // its transform. Expand in the destination space in this case.
382
+ strokeWidthX = strokeWidthY = 0;
383
+ collapsedStroke = top
384
+ ? top.getAbsoluteTransform().copy()
385
+ : new Transform();
386
+ if (relativeTo) {
387
+ const relativeTransform = relativeTo.getAbsoluteTransform();
388
+ const [a, b, c, d] = relativeTransform.getMatrix();
389
+ // A collapsed ancestor has no inverse. Its local bounds can only
390
+ // describe the uncollapsed stroke; world bounds are measured directly.
391
+ collapsedStroke =
392
+ a * d - b * c
393
+ ? relativeTransform.copy().invert().multiply(collapsedStroke)
394
+ : new Transform();
395
+ }
396
+ }
397
+ }
398
+ const fillAndStrokeWidth = fillRect.width + strokeWidthX;
399
+ const fillAndStrokeHeight = fillRect.height + strokeWidthY;
366
400
  const applyShadow = !config.skipShadow && this.hasShadow();
367
401
  const shadowOffsetX = applyShadow ? this.shadowOffsetX() : 0;
368
402
  const shadowOffsetY = applyShadow ? this.shadowOffsetY() : 0;
@@ -374,15 +408,25 @@ export class Shape extends Node {
374
408
  const rect = {
375
409
  width: width,
376
410
  height: height,
377
- x: -(strokeWidth / 2 + blurRadius) +
411
+ x: -(strokeWidthX / 2 + blurRadius) +
378
412
  Math.min(shadowOffsetX, 0) +
379
413
  fillRect.x,
380
- y: -(strokeWidth / 2 + blurRadius) +
414
+ y: -(strokeWidthY / 2 + blurRadius) +
381
415
  Math.min(shadowOffsetY, 0) +
382
416
  fillRect.y,
383
417
  };
384
418
  if (!skipTransform) {
385
- return this._transformedRect(rect, relativeTo);
419
+ const transformed = this._transformedRect(rect, relativeTo);
420
+ if (collapsedStroke) {
421
+ const [a, b, c, d] = collapsedStroke.getMatrix();
422
+ const x = (strokeWidth * Math.hypot(a, c)) / 2;
423
+ const y = (strokeWidth * Math.hypot(b, d)) / 2;
424
+ transformed.x -= x;
425
+ transformed.y -= y;
426
+ transformed.width += x * 2;
427
+ transformed.height += y * 2;
428
+ }
429
+ return transformed;
386
430
  }
387
431
  return rect;
388
432
  }
@@ -395,7 +439,7 @@ export class Shape extends Node {
395
439
  const canvas = can || layer.getCanvas(), context = canvas.getContext(), cachedCanvas = this._getCanvasCache(), drawFunc = this.getSceneFunc(), hasShadow = this.hasShadow();
396
440
  let stage;
397
441
  const cachingSelf = top === this;
398
- if (!this.isVisible() && !cachingSelf) {
442
+ if (!(top ? this._isVisible(top) : this.isVisible()) && !cachingSelf) {
399
443
  return this;
400
444
  }
401
445
  // if node is cached we just need to draw from cache
package/lib/Stage.d.ts CHANGED
@@ -6,7 +6,21 @@ import type { Shape } from './Shape.ts';
6
6
  import type { Layer } from './Layer.ts';
7
7
  export interface StageConfig extends ContainerConfig {
8
8
  container?: HTMLDivElement | string;
9
+ eventBatchFunc?: (callback: () => void) => void;
9
10
  }
11
+ type Click = {
12
+ shape: Shape | null;
13
+ time: number;
14
+ };
15
+ type EventType = 'mouse' | 'touch' | 'pointer';
16
+ type PointerState = {
17
+ targetShape?: Shape;
18
+ downPosition?: Vector2d;
19
+ pointerType?: string;
20
+ relatedPointer?: PointerState;
21
+ clickStartShape?: Shape | null;
22
+ lastClick?: Click;
23
+ };
10
24
  export declare const stages: Stage[];
11
25
  /**
12
26
  * Stage constructor. A stage is used to contain multiple layers
@@ -32,20 +46,11 @@ export declare class Stage extends Container<Layer, StageConfig> {
32
46
  _changedPointerPositions: (Vector2d & {
33
47
  id: number;
34
48
  })[];
49
+ _pointerEventType?: EventType;
50
+ _pointerStates: Map<string, PointerState>;
51
+ _lastClicks: Partial<Record<EventType, Click>>;
35
52
  bufferCanvas: SceneCanvas;
36
53
  bufferHitCanvas: HitCanvas;
37
- _mouseTargetShape: Shape;
38
- _touchTargetShape: Shape;
39
- _pointerTargetShape: Shape;
40
- _mouseClickStartShape: Shape;
41
- _touchClickStartShape: Shape;
42
- _pointerClickStartShape: Shape;
43
- _mouseClickEndShape: Shape;
44
- _touchClickEndShape: Shape;
45
- _pointerClickEndShape: Shape;
46
- _mouseDblTimeout: any;
47
- _touchDblTimeout: any;
48
- _pointerDblTimeout: any;
49
54
  constructor(config: StageConfig);
50
55
  _validateAdd(child: any): void;
51
56
  _checkVisibility(): void;
@@ -121,16 +126,18 @@ export declare class Stage extends Container<Layer, StageConfig> {
121
126
  */
122
127
  getLayers(): Layer[];
123
128
  _bindContentEvents(): void;
129
+ _batchEvents(callback: () => void): void;
124
130
  _pointerenter(evt: PointerEvent): void;
125
131
  _pointerover(evt: any): void;
126
- _getTargetShape(evenType: any): Shape<import("./Shape.ts").ShapeConfig> | null;
132
+ _pointerEventsEnabled(): boolean;
133
+ _cancelClick(pointerId: number | undefined, eventType?: EventType): void;
127
134
  _pointerleave(evt: any): void;
128
135
  _pointerdown(evt: TouchEvent | MouseEvent | PointerEvent): void;
129
136
  _pointermove(evt: TouchEvent | MouseEvent | PointerEvent): void;
130
137
  _pointerup(evt: any): void;
131
138
  _contextmenu(evt: any): void;
132
139
  _wheel(evt: any): void;
133
- _pointercancel(evt: PointerEvent): void;
140
+ _pointercancel(evt: PointerEvent | TouchEvent): void;
134
141
  _lostpointercapture(evt: PointerEvent): void;
135
142
  /**
136
143
  * manually register pointers positions (mouse/touch) in the stage.
@@ -165,4 +172,6 @@ export declare class Stage extends Container<Layer, StageConfig> {
165
172
  */
166
173
  batchDraw(): this;
167
174
  container: GetSet<HTMLDivElement, this, HTMLDivElement | string>;
175
+ eventBatchFunc: GetSet<((callback: () => void) => void) | undefined, this>;
168
176
  }
177
+ export {};