konva 10.5.0 → 10.7.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 (79) hide show
  1. package/README.md +16 -12
  2. package/konva.js +3623 -3819
  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 +10 -3
  9. package/lib/Canvas.js +91 -10
  10. package/lib/Container.d.ts +3 -2
  11. package/lib/Container.js +31 -17
  12. package/lib/Context.d.ts +9 -7
  13. package/lib/Context.js +99 -73
  14. package/lib/DragAndDrop.d.ts +4 -3
  15. package/lib/DragAndDrop.js +39 -25
  16. package/lib/Factory.d.ts +3 -2
  17. package/lib/Factory.js +10 -9
  18. package/lib/Global.d.ts +2 -7
  19. package/lib/Global.js +8 -9
  20. package/lib/Group.d.ts +7 -1
  21. package/lib/Group.js +61 -0
  22. package/lib/Layer.d.ts +1 -1
  23. package/lib/Layer.js +13 -8
  24. package/lib/Node.d.ts +25 -19
  25. package/lib/Node.js +178 -169
  26. package/lib/PointerEvents.d.ts +1 -2
  27. package/lib/PointerEvents.js +20 -18
  28. package/lib/Shape.d.ts +9 -15
  29. package/lib/Shape.js +190 -86
  30. package/lib/Stage.d.ts +19 -15
  31. package/lib/Stage.js +184 -99
  32. package/lib/Tween.js +59 -66
  33. package/lib/Util.d.ts +3 -2
  34. package/lib/Util.js +46 -16
  35. package/lib/_CoreInternals.d.ts +23 -11
  36. package/lib/_FullInternals.d.ts +23 -11
  37. package/lib/filters/Blur.js +16 -12
  38. package/lib/filters/Brighten.d.ts +1 -0
  39. package/lib/filters/Brighten.js +1 -0
  40. package/lib/filters/Brightness.d.ts +2 -1
  41. package/lib/filters/Brightness.js +4 -4
  42. package/lib/filters/Emboss.js +16 -58
  43. package/lib/filters/Kaleidoscope.js +8 -15
  44. package/lib/filters/Mask.js +8 -8
  45. package/lib/filters/Pixelate.js +16 -29
  46. package/lib/filters/Threshold.d.ts +1 -1
  47. package/lib/filters/Threshold.js +1 -1
  48. package/lib/shapes/Arc.js +8 -6
  49. package/lib/shapes/Arrow.d.ts +3 -6
  50. package/lib/shapes/Arrow.js +75 -47
  51. package/lib/shapes/Circle.d.ts +1 -0
  52. package/lib/shapes/Circle.js +4 -1
  53. package/lib/shapes/Ellipse.d.ts +1 -0
  54. package/lib/shapes/Ellipse.js +4 -1
  55. package/lib/shapes/Image.d.ts +2 -1
  56. package/lib/shapes/Image.js +22 -22
  57. package/lib/shapes/Label.d.ts +2 -3
  58. package/lib/shapes/Label.js +25 -41
  59. package/lib/shapes/Line.d.ts +2 -1
  60. package/lib/shapes/Line.js +5 -2
  61. package/lib/shapes/Path.d.ts +11 -2
  62. package/lib/shapes/Path.js +133 -143
  63. package/lib/shapes/Rect.d.ts +1 -0
  64. package/lib/shapes/Rect.js +4 -1
  65. package/lib/shapes/RegularPolygon.js +2 -2
  66. package/lib/shapes/Ring.d.ts +7 -0
  67. package/lib/shapes/Ring.js +8 -1
  68. package/lib/shapes/Sprite.d.ts +2 -1
  69. package/lib/shapes/Sprite.js +8 -9
  70. package/lib/shapes/Star.d.ts +6 -0
  71. package/lib/shapes/Star.js +5 -1
  72. package/lib/shapes/Text.d.ts +16 -5
  73. package/lib/shapes/Text.js +134 -65
  74. package/lib/shapes/TextPath.d.ts +11 -3
  75. package/lib/shapes/TextPath.js +56 -27
  76. package/lib/shapes/Transformer.js +95 -96
  77. package/lib/shapes/Wedge.js +1 -1
  78. package/lib/types.d.ts +2 -2
  79. package/package.json +5 -20
@@ -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
@@ -1,7 +1,7 @@
1
1
  import type { GetClientRectConfig, NodeConfig } from './Node.ts';
2
2
  import { Node } from './Node.ts';
3
3
  import type { Context } from './Context.ts';
4
- import type { GetSet, Vector2d } from './types.ts';
4
+ import type { GetSet, Vector2d, IRect } from './types.ts';
5
5
  import type { HitCanvas, SceneCanvas } from './Canvas.ts';
6
6
  export type ShapeConfigHandler<TTarget> = {
7
7
  bivarianceHack(ctx: Context, shape: TTarget): void;
@@ -59,6 +59,7 @@ export type ShapeConfig = NodeConfig & {
59
59
  miterLimit?: number;
60
60
  sceneFunc?: (con: Context, shape: Shape) => void;
61
61
  hitFunc?: (con: Context, shape: Shape) => void;
62
+ selfRectFunc?: (shape: Shape) => IRect;
62
63
  shadowColor?: string;
63
64
  shadowBlur?: number;
64
65
  shadowOffset?: Vector2d;
@@ -169,7 +170,7 @@ export declare class Shape<Config extends ShapeConfig = ShapeConfig> extends Nod
169
170
  */
170
171
  intersects(point: Vector2d): boolean;
171
172
  destroy(): this;
172
- _useBufferCanvas(forceFill?: boolean): boolean;
173
+ _useBufferCanvas(forceFill?: boolean, opacity?: any): boolean;
173
174
  setStrokeHitEnabled(val: number): void;
174
175
  getStrokeHitEnabled(): boolean;
175
176
  /**
@@ -184,19 +185,11 @@ export declare class Shape<Config extends ShapeConfig = ShapeConfig> extends Nod
184
185
  * circle.getSelfRect(); // return {x: - circle.width() / 2, y: - circle.height() / 2, width:circle.width(), height:circle.height()}
185
186
  *
186
187
  */
187
- getSelfRect(): {
188
- x: number;
189
- y: number;
190
- width: number;
191
- height: number;
192
- };
193
- getClientRect(config?: ShapeGetClientRectConfig): {
194
- width: number;
195
- height: number;
196
- x: number;
197
- y: number;
198
- };
199
- drawScene(can?: SceneCanvas, top?: Node, bufferCanvas?: SceneCanvas): this;
188
+ getSelfRect(): IRect;
189
+ _getSelfRectForDrawing(): IRect;
190
+ getClientRect(config?: ShapeGetClientRectConfig): IRect;
191
+ _getStrokePadding(miterLimit?: number): number;
192
+ drawScene(can?: SceneCanvas, top?: Node): this;
200
193
  drawHit(can?: HitCanvas, top?: Node): this;
201
194
  /**
202
195
  * draw hit graph using the cached scene canvas
@@ -255,6 +248,7 @@ export declare class Shape<Config extends ShapeConfig = ShapeConfig> extends Nod
255
248
  miterLimit: GetSet<number, this>;
256
249
  perfectDrawEnabled: GetSet<boolean, this>;
257
250
  sceneFunc: GetSet<ShapeConfigHandler<this>, this>;
251
+ selfRectFunc: GetSet<((shape: Shape) => IRect) | undefined, this>;
258
252
  shadowColor: GetSet<string, this>;
259
253
  shadowEnabled: GetSet<boolean, this>;
260
254
  shadowForStrokeEnabled: GetSet<boolean, this>;
package/lib/Shape.js CHANGED
@@ -3,7 +3,7 @@ import { Transform, Util } from "./Util.js";
3
3
  import { Factory } from "./Factory.js";
4
4
  import { Node } from "./Node.js";
5
5
  import { getNumberValidator, getNumberOrAutoValidator, getStringValidator, getBooleanValidator, getStringOrGradientValidator, } from "./Validators.js";
6
- import { _registerNode } from "./Global.js";
6
+ import { _boundedShapes, _registerNode } from "./Global.js";
7
7
  import * as PointerEvents from "./PointerEvents.js";
8
8
  const HAS_SHADOW = 'hasShadow';
9
9
  const HAS_FILL = 'hasFill';
@@ -279,7 +279,9 @@ export class Shape extends Node {
279
279
  if (!stage) {
280
280
  return false;
281
281
  }
282
- const bufferHitCanvas = stage._syncBufferSize(stage.bufferHitCanvas);
282
+ const bufferHitCanvas = stage.bufferHitCanvas;
283
+ // lazy: allocated at stage size on first use
284
+ bufferHitCanvas.setSizeIfChanged(stage.width(), stage.height());
283
285
  bufferHitCanvas.getContext().clear();
284
286
  this.drawHit(bufferHitCanvas);
285
287
  const p = bufferHitCanvas.context.getImageData(Math.floor(point.x), Math.floor(point.y), 1, 1).data;
@@ -294,7 +296,7 @@ export class Shape extends Node {
294
296
  // why do we need buffer canvas?
295
297
  // it give better result when a shape has
296
298
  // stroke with fill and with some opacity
297
- _useBufferCanvas(forceFill) {
299
+ _useBufferCanvas(forceFill, opacity = this.getAbsoluteOpacity()) {
298
300
  // image and sprite still has "fill" as image
299
301
  // so they use that method with forced fill
300
302
  // it probably will be simpler, then copy/paste the code
@@ -306,7 +308,7 @@ export class Shape extends Node {
306
308
  }
307
309
  const hasFill = forceFill || this.hasFill();
308
310
  const hasStroke = this.hasStroke();
309
- const isTransparent = this.getAbsoluteOpacity() !== 1;
311
+ const isTransparent = !this._isUnderCache && opacity !== 1;
310
312
  if (hasFill && hasStroke && isTransparent) {
311
313
  return true;
312
314
  }
@@ -347,6 +349,9 @@ export class Shape extends Node {
347
349
  *
348
350
  */
349
351
  getSelfRect() {
352
+ const selfRectFunc = this.attrs.selfRectFunc;
353
+ if (selfRectFunc)
354
+ return { ...selfRectFunc.call(this, this) };
350
355
  const size = this.size();
351
356
  return {
352
357
  x: this._centroid ? -size.width / 2 : 0,
@@ -355,47 +360,118 @@ export class Shape extends Node {
355
360
  height: size.height,
356
361
  };
357
362
  }
363
+ _getSelfRectForDrawing() {
364
+ return this.getSelfRect();
365
+ }
358
366
  getClientRect(config = {}) {
367
+ const cachedRect = this._getCachedSceneRect(config);
368
+ if (cachedRect)
369
+ return cachedRect;
359
370
  const skipTransform = config.skipTransform;
360
371
  const relativeTo = config.relativeTo;
361
- const fillRect = this.getSelfRect();
372
+ const forDrawing = config._forDrawing;
373
+ // Painted bounds are measured in the canvas that draws the shape
374
+ // (relativeTo): non-scaling strokes and shadows use that canvas's axes.
375
+ // The identity transform normalizes negative sizes.
376
+ const fillRect = !forDrawing
377
+ ? this.getSelfRect()
378
+ : this.strokeScaleEnabled()
379
+ ? new Transform()._getTransformedRect(this._getSelfRectForDrawing())
380
+ : this._transformedRect(this._getSelfRectForDrawing(), relativeTo);
362
381
  const applyStroke = !config.skipStroke && this.hasStroke();
363
- const strokeWidth = (applyStroke && this.strokeWidth()) || 0;
364
- const fillAndStrokeWidth = fillRect.width + strokeWidth;
365
- const fillAndStrokeHeight = fillRect.height + strokeWidth;
366
- const applyShadow = !config.skipShadow && this.hasShadow();
367
- const shadowOffsetX = applyShadow ? this.shadowOffsetX() : 0;
368
- const shadowOffsetY = applyShadow ? this.shadowOffsetY() : 0;
369
- const preWidth = fillAndStrokeWidth + Math.abs(shadowOffsetX);
370
- const preHeight = fillAndStrokeHeight + Math.abs(shadowOffsetY);
371
- const blurRadius = (applyShadow && this.shadowBlur()) || 0;
372
- const width = preWidth + blurRadius * 2;
373
- const height = preHeight + blurRadius * 2;
374
- const rect = {
375
- width: width,
376
- height: height,
377
- x: -(strokeWidth / 2 + blurRadius) +
378
- Math.min(shadowOffsetX, 0) +
379
- fillRect.x,
380
- y: -(strokeWidth / 2 + blurRadius) +
381
- Math.min(shadowOffsetY, 0) +
382
- fillRect.y,
382
+ const strokeWidth = applyStroke
383
+ ? forDrawing
384
+ ? this._getStrokePadding() * 2
385
+ : this.strokeWidth()
386
+ : 0;
387
+ let strokeWidthX = strokeWidth;
388
+ let strokeWidthY = strokeWidth;
389
+ let collapsedStroke;
390
+ if (!forDrawing && strokeWidth && !this.strokeScaleEnabled()) {
391
+ // Strokes are constant in the canvas they are drawn into. A cached
392
+ // ancestor supplies that coordinate space until its bitmap is rebuilt.
393
+ let top = this;
394
+ while (top && !top.isCached() && !top._isUnderCache)
395
+ top = top.parent;
396
+ const [a, b, c, d] = this.getAbsoluteTransform(top).getMatrix();
397
+ const determinant = Math.abs(a * d - b * c);
398
+ if (determinant) {
399
+ strokeWidthX *= Math.hypot(c, d) / determinant;
400
+ strokeWidthY *= Math.hypot(a, b) / determinant;
401
+ }
402
+ else if (!skipTransform) {
403
+ // A collapsed path can still draw a stroke after the context resets
404
+ // its transform. Expand in the destination space in this case.
405
+ strokeWidthX = strokeWidthY = 0;
406
+ collapsedStroke = top
407
+ ? top.getAbsoluteTransform().copy()
408
+ : new Transform();
409
+ if (relativeTo) {
410
+ const relativeTransform = relativeTo.getAbsoluteTransform();
411
+ const [a, b, c, d] = relativeTransform.getMatrix();
412
+ // A collapsed ancestor has no inverse. Its local bounds can only
413
+ // describe the uncollapsed stroke; world bounds are measured directly.
414
+ collapsedStroke =
415
+ a * d - b * c
416
+ ? relativeTransform.copy().invert().multiply(collapsedStroke)
417
+ : new Transform();
418
+ }
419
+ }
420
+ }
421
+ let rect = {
422
+ x: fillRect.x - strokeWidthX / 2,
423
+ y: fillRect.y - strokeWidthY / 2,
424
+ width: fillRect.width + strokeWidthX,
425
+ height: fillRect.height + strokeWidthY,
383
426
  };
427
+ if (forDrawing && this.strokeScaleEnabled()) {
428
+ rect = this._transformedRect(rect, relativeTo);
429
+ }
430
+ if (!config.skipShadow && this.hasShadow()) {
431
+ const scale = forDrawing ? this.getAbsoluteScale() : { x: 1, y: 1 };
432
+ const dx = this.shadowOffsetX() * scale.x;
433
+ const dy = this.shadowOffsetY() * scale.y;
434
+ const blur = this.shadowBlur() *
435
+ (forDrawing ? 2 * Math.min(Math.abs(scale.x), Math.abs(scale.y)) : 1);
436
+ rect.x += Math.min(0, dx) - blur;
437
+ rect.y += Math.min(0, dy) - blur;
438
+ rect.width += Math.abs(dx) + blur * 2;
439
+ rect.height += Math.abs(dy) + blur * 2;
440
+ }
441
+ if (forDrawing) {
442
+ return rect;
443
+ }
384
444
  if (!skipTransform) {
385
- return this._transformedRect(rect, relativeTo);
445
+ const transformed = this._transformedRect(rect, relativeTo);
446
+ if (collapsedStroke) {
447
+ const [a, b, c, d] = collapsedStroke.getMatrix();
448
+ const x = (strokeWidth * Math.hypot(a, c)) / 2;
449
+ const y = (strokeWidth * Math.hypot(b, d)) / 2;
450
+ transformed.x -= x;
451
+ transformed.y -= y;
452
+ transformed.width += x * 2;
453
+ transformed.height += y * 2;
454
+ }
455
+ return transformed;
386
456
  }
387
457
  return rect;
388
458
  }
389
- drawScene(can, top, bufferCanvas) {
459
+ _getStrokePadding(miterLimit = this.miterLimit() || 10) {
460
+ // Custom paths can have sharper joins than the built-in geometry.
461
+ if (this.attrs.sceneFunc)
462
+ miterLimit = this.miterLimit() || 10;
463
+ return ((Math.abs(this.strokeWidth()) / 2) *
464
+ Math.max(this.lineCap() === 'square' ? Math.SQRT2 : 1, (this.lineJoin() || 'miter') === 'miter' ? miterLimit : 1));
465
+ }
466
+ drawScene(can, top) {
390
467
  // basically there are 3 drawing modes
391
468
  // 1 - simple drawing when nothing is cached.
392
469
  // 2 - when we are caching current
393
470
  // 3 - when node is cached and we need to draw it into layer
394
471
  const layer = this.getLayer();
395
472
  const canvas = can || layer.getCanvas(), context = canvas.getContext(), cachedCanvas = this._getCanvasCache(), drawFunc = this.getSceneFunc(), hasShadow = this.hasShadow();
396
- let stage;
397
473
  const cachingSelf = top === this;
398
- if (!this.isVisible() && !cachingSelf) {
474
+ if (!(top ? this._isVisible(top) : this.isVisible()) && !cachingSelf) {
399
475
  return this;
400
476
  }
401
477
  // if node is cached we just need to draw from cache
@@ -411,66 +487,72 @@ export class Shape extends Node {
411
487
  return this;
412
488
  }
413
489
  context.save();
414
- // if buffer canvas is needed
415
- if (this._useBufferCanvas()) {
416
- stage = this.getStage();
417
- const bc = bufferCanvas || stage._syncBufferSize(stage.bufferCanvas);
418
- const bufferContext = bc.getContext();
419
- if (!bufferCanvas) {
420
- bufferContext.clear();
421
- }
422
- else if (!bc.width) {
423
- // cache() and toCanvas() hand over an empty buffer: size it to the
424
- // destination on first use and shift it to the buffer origin
425
- bc.setSize(canvas._logicalWidth, canvas._logicalHeight);
426
- bufferContext.translate(-bc.x, -bc.y);
427
- }
428
- else {
429
- // the buffer is translated, so reset the transform before clearing
490
+ try {
491
+ // if buffer canvas is needed; an empty canvas has nothing to draw into
492
+ if (canvas.width &&
493
+ canvas.height &&
494
+ this._useBufferCanvas(undefined, context._getOpacity(this))) {
495
+ // Built-in shapes, custom shapes with a selfRectFunc and isolated
496
+ // groups use a buffer the size of the painted bounds. Other custom
497
+ // drawing (sceneFunc, charRenderFunc, own classes) gets the whole
498
+ // canvas, since it can paint outside its bounds. The shadow is
499
+ // applied when the buffer is drawn, not inside it.
500
+ const bounded = !!context._opacityRoot ||
501
+ (!!this.attrs.selfRectFunc &&
502
+ this.getSelfRect === Shape.prototype.getSelfRect) ||
503
+ (!this.attrs.sceneFunc &&
504
+ !this.attrs.charRenderFunc &&
505
+ _boundedShapes.has(this.constructor));
506
+ const bc = canvas._prepareIsolationCanvas(bounded
507
+ ? this.getClientRect({
508
+ _forDrawing: true,
509
+ relativeTo: top,
510
+ skipShadow: true,
511
+ })
512
+ : undefined);
513
+ const bufferContext = bc.getContext();
430
514
  bufferContext.save();
431
- bufferContext.setTransform(1, 0, 0, 1, 0, 0);
432
- bufferContext.clearRect(0, 0, bc.width, bc.height);
515
+ bufferContext._applyLineJoin(this);
516
+ bufferContext._applyMiterLimit(this);
517
+ // layer might be undefined if we are using cache before adding to layer
518
+ const o = this.getAbsoluteTransform(top).getMatrix();
519
+ bufferContext.transform(o[0], o[1], o[2], o[3], o[4], o[5]);
520
+ drawFunc.call(this, bufferContext, this);
433
521
  bufferContext.restore();
522
+ if (hasShadow) {
523
+ context._applyShadow(this);
524
+ }
525
+ // if we are caching self, we don't need to apply opacity and global composite operation
526
+ // because it will be applied in the cache
527
+ if (!cachingSelf) {
528
+ context._applyOpacity(this);
529
+ context._applyGlobalCompositeOperation(this);
530
+ }
531
+ context._drawDeviceBuffer(bc);
434
532
  }
435
- bufferContext.save();
436
- // the buffer canvas is a separate context that does not inherit the
437
- // destination `imageSmoothingEnabled` flag, so propagate it to keep
438
- // pattern/image smoothing consistent with the target context.
439
- bufferContext.imageSmoothingEnabled = context.imageSmoothingEnabled;
440
- bufferContext._applyLineJoin(this);
441
- bufferContext._applyMiterLimit(this);
442
- // layer might be undefined if we are using cache before adding to layer
443
- const o = this.getAbsoluteTransform(top).getMatrix();
444
- bufferContext.transform(o[0], o[1], o[2], o[3], o[4], o[5]);
445
- drawFunc.call(this, bufferContext, this);
446
- bufferContext.restore();
447
- const ratio = bc.pixelRatio;
448
- if (hasShadow) {
449
- context._applyShadow(this);
450
- }
451
- // if we are caching self, we don't need to apply opacity and global composite operation
452
- // because it will be applied in the cache
453
- if (!cachingSelf) {
454
- context._applyOpacity(this);
455
- context._applyGlobalCompositeOperation(this);
533
+ else {
534
+ context._applyLineJoin(this);
535
+ context._applyMiterLimit(this);
536
+ if (!cachingSelf) {
537
+ const o = this.getAbsoluteTransform(top).getMatrix();
538
+ context.transform(o[0], o[1], o[2], o[3], o[4], o[5]);
539
+ context._applyOpacity(this);
540
+ context._applyGlobalCompositeOperation(this);
541
+ }
542
+ if (hasShadow) {
543
+ context._applyShadow(this);
544
+ }
545
+ drawFunc.call(this, context, this);
456
546
  }
457
- context.drawImage(bc._canvas, bc.x, bc.y, bc.width / ratio, bc.height / ratio);
458
547
  }
459
- else {
460
- context._applyLineJoin(this);
461
- context._applyMiterLimit(this);
462
- if (!cachingSelf) {
463
- const o = this.getAbsoluteTransform(top).getMatrix();
464
- context.transform(o[0], o[1], o[2], o[3], o[4], o[5]);
465
- context._applyOpacity(this);
466
- context._applyGlobalCompositeOperation(this);
467
- }
468
- if (hasShadow) {
469
- context._applyShadow(this);
470
- }
471
- drawFunc.call(this, context, this);
548
+ catch (error) {
549
+ // A throwing sceneFunc can leave a clip on the shared buffer.
550
+ canvas._releaseIsolationCanvas();
551
+ throw error;
552
+ }
553
+ finally {
554
+ context.restore();
472
555
  }
473
- context.restore();
474
556
  return this;
475
557
  }
476
558
  drawHit(can, top) {
@@ -500,8 +582,12 @@ export class Shape extends Node {
500
582
  const o = this.getAbsoluteTransform(top).getMatrix();
501
583
  context.transform(o[0], o[1], o[2], o[3], o[4], o[5]);
502
584
  }
503
- drawFunc.call(this, context, this);
504
- context.restore();
585
+ try {
586
+ drawFunc.call(this, context, this);
587
+ }
588
+ finally {
589
+ context.restore();
590
+ }
505
591
  return this;
506
592
  }
507
593
  /**
@@ -778,6 +864,24 @@ Factory.addGetterSetter(Shape, 'hitFunc');
778
864
  * context.fillStrokeShape(shape);
779
865
  * });
780
866
  */
867
+ Factory.addGetterSetter(Shape, 'selfRectFunc');
868
+ /**
869
+ * get/set the function that returns the self rectangle of a custom shape: the
870
+ * local, untransformed area its sceneFunc paints, before the stroke and shadow
871
+ * Konva adds. Use it when drawing goes outside the width and height box, so
872
+ * bounds, cache(), isolated groups and perfect drawing include that paint.
873
+ * Paint outside the returned rectangle can be clipped. Meant for custom
874
+ * shapes, and for Text drawn with charRenderFunc; built-in shapes that compute
875
+ * their own bounds may ignore it.
876
+ * @name Konva.Shape#selfRectFunc
877
+ * @method
878
+ * @param {Function} selfRectFunc function returning {x, y, width, height}
879
+ * @returns {Function}
880
+ * @example
881
+ * shape.selfRectFunc(() => ({ x: -10, y: -10, width: 120, height: 120 }));
882
+ * // back to the automatic width and height box
883
+ * shape.selfRectFunc(undefined);
884
+ */
781
885
  Factory.addGetterSetter(Shape, 'dash');
782
886
  /**
783
887
  * get/set dash array for stroke.
@@ -1133,7 +1237,7 @@ Factory.addGetterSetter(Shape, 'dashEnabled', true);
1133
1237
  */
1134
1238
  Factory.addGetterSetter(Shape, 'strokeScaleEnabled', true);
1135
1239
  /**
1136
- * get/set strokeScale enabled flag
1240
+ * get/set strokeScale enabled flag. Text and TextPath always scale their stroke.
1137
1241
  * @name Konva.Shape#strokeScaleEnabled
1138
1242
  * @method
1139
1243
  * @param {Boolean} enabled
package/lib/Stage.d.ts CHANGED
@@ -8,6 +8,19 @@ export interface StageConfig extends ContainerConfig {
8
8
  container?: HTMLDivElement | string;
9
9
  eventBatchFunc?: (callback: () => void) => void;
10
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
+ };
11
24
  export declare const stages: Stage[];
12
25
  /**
13
26
  * Stage constructor. A stage is used to contain multiple layers
@@ -33,20 +46,10 @@ export declare class Stage extends Container<Layer, StageConfig> {
33
46
  _changedPointerPositions: (Vector2d & {
34
47
  id: number;
35
48
  })[];
36
- bufferCanvas: SceneCanvas;
49
+ _pointerEventType?: EventType;
50
+ _pointerStates: Map<string, PointerState>;
51
+ _lastClicks: Partial<Record<EventType, Click>>;
37
52
  bufferHitCanvas: HitCanvas;
38
- _mouseTargetShape: Shape;
39
- _touchTargetShape: Shape;
40
- _pointerTargetShape: Shape;
41
- _mouseClickStartShape: Shape;
42
- _touchClickStartShape: Shape;
43
- _pointerClickStartShape: Shape;
44
- _mouseClickEndShape: Shape;
45
- _touchClickEndShape: Shape;
46
- _pointerClickEndShape: Shape;
47
- _mouseDblTimeout: any;
48
- _touchDblTimeout: any;
49
- _pointerDblTimeout: any;
50
53
  constructor(config: StageConfig);
51
54
  _validateAdd(child: any): void;
52
55
  _checkVisibility(): void;
@@ -108,7 +111,6 @@ export declare class Stage extends Container<Layer, StageConfig> {
108
111
  */
109
112
  getIntersection(pos: Vector2d): Shape<import("./Shape.ts").ShapeConfig> | null;
110
113
  _resizeDOM(): void;
111
- _syncBufferSize<T extends SceneCanvas | HitCanvas>(canvas: T): T;
112
114
  add(layer: Layer, ...rest: any[]): this;
113
115
  getParent(): null;
114
116
  getLayer(): null;
@@ -125,7 +127,8 @@ export declare class Stage extends Container<Layer, StageConfig> {
125
127
  _batchEvents(callback: () => void): void;
126
128
  _pointerenter(evt: PointerEvent): void;
127
129
  _pointerover(evt: any): void;
128
- _getTargetShape(evenType: any): Shape<import("./Shape.ts").ShapeConfig> | null;
130
+ _pointerEventsEnabled(): boolean;
131
+ _cancelClick(pointerId: number | undefined, eventType?: EventType): void;
129
132
  _pointerleave(evt: any): void;
130
133
  _pointerdown(evt: TouchEvent | MouseEvent | PointerEvent): void;
131
134
  _pointermove(evt: TouchEvent | MouseEvent | PointerEvent): void;
@@ -169,3 +172,4 @@ export declare class Stage extends Container<Layer, StageConfig> {
169
172
  container: GetSet<HTMLDivElement, this, HTMLDivElement | string>;
170
173
  eventBatchFunc: GetSet<((callback: () => void) => void) | undefined, this>;
171
174
  }
175
+ export {};