konva 10.0.10 → 10.0.11

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 (127) hide show
  1. package/konva.js +2 -2
  2. package/konva.min.js +2 -2
  3. package/lib/Animation.d.ts +24 -0
  4. package/lib/Animation.js +142 -0
  5. package/lib/BezierFunctions.d.ts +6 -0
  6. package/lib/BezierFunctions.js +784 -0
  7. package/lib/Canvas.d.ts +33 -0
  8. package/lib/Canvas.js +107 -0
  9. package/lib/Container.d.ts +54 -0
  10. package/lib/Container.js +316 -0
  11. package/lib/Context.d.ts +95 -0
  12. package/lib/Context.js +561 -0
  13. package/lib/Core.d.ts +3 -0
  14. package/lib/Core.js +3 -0
  15. package/lib/DragAndDrop.d.ts +17 -0
  16. package/lib/DragAndDrop.js +110 -0
  17. package/lib/Factory.d.ts +22 -0
  18. package/lib/Factory.js +123 -0
  19. package/lib/FastLayer.d.ts +4 -0
  20. package/lib/FastLayer.js +12 -0
  21. package/lib/Global.d.ts +37 -0
  22. package/lib/Global.js +66 -0
  23. package/lib/Group.d.ts +9 -0
  24. package/lib/Group.js +13 -0
  25. package/lib/Layer.d.ts +62 -0
  26. package/lib/Layer.js +305 -0
  27. package/lib/Node.d.ts +323 -0
  28. package/lib/Node.js +1532 -0
  29. package/lib/PointerEvents.d.ts +11 -0
  30. package/lib/PointerEvents.js +37 -0
  31. package/lib/Shape.d.ts +196 -0
  32. package/lib/Shape.js +555 -0
  33. package/lib/Stage.d.ts +86 -0
  34. package/lib/Stage.js +715 -0
  35. package/lib/Tween.d.ts +90 -0
  36. package/lib/Tween.js +531 -0
  37. package/lib/Util.d.ts +137 -0
  38. package/lib/Util.js +909 -0
  39. package/lib/Validators.d.ts +11 -0
  40. package/lib/Validators.js +178 -0
  41. package/lib/_CoreInternals.d.ts +224 -0
  42. package/lib/_CoreInternals.js +34 -0
  43. package/lib/_FullInternals.d.ts +306 -0
  44. package/lib/_FullInternals.js +80 -0
  45. package/lib/canvas-backend.d.ts +2 -0
  46. package/lib/canvas-backend.js +27 -0
  47. package/lib/filters/Blur.d.ts +2 -0
  48. package/lib/filters/Blur.js +235 -0
  49. package/lib/filters/Brighten.d.ts +2 -0
  50. package/lib/filters/Brighten.js +12 -0
  51. package/lib/filters/Brightness.d.ts +2 -0
  52. package/lib/filters/Brightness.js +8 -0
  53. package/lib/filters/Contrast.d.ts +2 -0
  54. package/lib/filters/Contrast.js +35 -0
  55. package/lib/filters/Emboss.d.ts +2 -0
  56. package/lib/filters/Emboss.js +101 -0
  57. package/lib/filters/Enhance.d.ts +2 -0
  58. package/lib/filters/Enhance.js +84 -0
  59. package/lib/filters/Grayscale.d.ts +2 -0
  60. package/lib/filters/Grayscale.js +9 -0
  61. package/lib/filters/HSL.d.ts +2 -0
  62. package/lib/filters/HSL.js +24 -0
  63. package/lib/filters/HSV.d.ts +2 -0
  64. package/lib/filters/HSV.js +23 -0
  65. package/lib/filters/Invert.d.ts +2 -0
  66. package/lib/filters/Invert.js +8 -0
  67. package/lib/filters/Kaleidoscope.d.ts +2 -0
  68. package/lib/filters/Kaleidoscope.js +133 -0
  69. package/lib/filters/Mask.d.ts +2 -0
  70. package/lib/filters/Mask.js +151 -0
  71. package/lib/filters/Noise.d.ts +2 -0
  72. package/lib/filters/Noise.js +12 -0
  73. package/lib/filters/Pixelate.d.ts +2 -0
  74. package/lib/filters/Pixelate.js +60 -0
  75. package/lib/filters/Posterize.d.ts +2 -0
  76. package/lib/filters/Posterize.js +10 -0
  77. package/lib/filters/RGB.d.ts +2 -0
  78. package/lib/filters/RGB.js +38 -0
  79. package/lib/filters/RGBA.d.ts +2 -0
  80. package/lib/filters/RGBA.js +49 -0
  81. package/lib/filters/Sepia.d.ts +2 -0
  82. package/lib/filters/Sepia.js +11 -0
  83. package/lib/filters/Solarize.d.ts +2 -0
  84. package/lib/filters/Solarize.js +14 -0
  85. package/lib/filters/Threshold.d.ts +2 -0
  86. package/lib/filters/Threshold.js +10 -0
  87. package/lib/index.d.ts +2 -0
  88. package/lib/index.js +2 -0
  89. package/lib/shapes/Arc.d.ts +27 -0
  90. package/lib/shapes/Arc.js +60 -0
  91. package/lib/shapes/Arrow.d.ts +27 -0
  92. package/lib/shapes/Arrow.js +99 -0
  93. package/lib/shapes/Circle.d.ts +15 -0
  94. package/lib/shapes/Circle.js +33 -0
  95. package/lib/shapes/Ellipse.d.ts +18 -0
  96. package/lib/shapes/Ellipse.js +37 -0
  97. package/lib/shapes/Image.d.ts +29 -0
  98. package/lib/shapes/Image.js +131 -0
  99. package/lib/shapes/Label.d.ts +39 -0
  100. package/lib/shapes/Label.js +173 -0
  101. package/lib/shapes/Line.d.ts +29 -0
  102. package/lib/shapes/Line.js +156 -0
  103. package/lib/shapes/Path.d.ts +50 -0
  104. package/lib/shapes/Path.js +670 -0
  105. package/lib/shapes/Rect.d.ts +11 -0
  106. package/lib/shapes/Rect.js +22 -0
  107. package/lib/shapes/RegularPolygon.d.ts +26 -0
  108. package/lib/shapes/RegularPolygon.js +72 -0
  109. package/lib/shapes/Ring.d.ts +17 -0
  110. package/lib/shapes/Ring.js +33 -0
  111. package/lib/shapes/Sprite.d.ts +32 -0
  112. package/lib/shapes/Sprite.js +108 -0
  113. package/lib/shapes/Star.d.ts +19 -0
  114. package/lib/shapes/Star.js +38 -0
  115. package/lib/shapes/Text.d.ts +90 -0
  116. package/lib/shapes/Text.js +461 -0
  117. package/lib/shapes/TextPath.d.ts +72 -0
  118. package/lib/shapes/TextPath.js +268 -0
  119. package/lib/shapes/Transformer.d.ts +130 -0
  120. package/lib/shapes/Transformer.js +1009 -0
  121. package/lib/shapes/Wedge.d.ts +19 -0
  122. package/lib/shapes/Wedge.js +38 -0
  123. package/lib/skia-backend.d.ts +2 -0
  124. package/lib/skia-backend.js +24 -0
  125. package/lib/types.d.ts +56 -0
  126. package/lib/types.js +21 -0
  127. package/package.json +1 -1
@@ -0,0 +1,22 @@
1
+ import type { Node } from './Node.ts';
2
+ import type { GetSet } from './types.ts';
3
+ type EnforceString<T> = T extends string ? T : never;
4
+ type Constructor = abstract new (...args: any) => any;
5
+ type Attr<T extends Constructor> = EnforceString<keyof InstanceType<T>>;
6
+ type AfterFunc<T extends Constructor> = (this: InstanceType<T>) => void;
7
+ type ExtractGetSet<T> = T extends GetSet<infer U, any> ? U : never;
8
+ type Value<T extends Constructor, U extends Attr<T>> = ExtractGetSet<InstanceType<T>[U]>;
9
+ type ValidatorFunc<T> = (val: ExtractGetSet<T>, attr: string) => T;
10
+ type ExtractComponents<T extends Constructor, U extends Attr<T>> = Value<T, U> extends Record<string, any> ? EnforceString<keyof Value<T, U>>[] : never;
11
+ export declare const Factory: {
12
+ addGetterSetter<T extends Constructor, U extends Attr<T>>(constructor: T, attr: U, def?: Value<T, U>, validator?: ValidatorFunc<Value<T, U>>, after?: AfterFunc<T>): void;
13
+ addGetter<T extends Constructor, U extends Attr<T>>(constructor: T, attr: U, def?: Value<T, U>): void;
14
+ addSetter<T extends Constructor, U extends Attr<T>>(constructor: T, attr: U, validator?: ValidatorFunc<Value<T, U>>, after?: AfterFunc<T>): void;
15
+ overWriteSetter<T extends Constructor, U extends Attr<T>>(constructor: T, attr: U, validator?: ValidatorFunc<Value<T, U>>, after?: AfterFunc<T>): void;
16
+ addComponentsGetterSetter<T extends Constructor, U extends Attr<T>>(constructor: T, attr: U, components: ExtractComponents<T, U>, validator?: ValidatorFunc<Value<T, U>>, after?: AfterFunc<T>): void;
17
+ addOverloadedGetterSetter<T extends Constructor, U extends Attr<T>>(constructor: T, attr: U): void;
18
+ addDeprecatedGetterSetter<T extends Constructor, U extends Attr<T>>(constructor: T, attr: U, def: Value<T, U>, validator: ValidatorFunc<Value<T, U>>): void;
19
+ backCompat<T extends Constructor>(constructor: T, methods: Record<string, string>): void;
20
+ afterSetFilter(this: Node): void;
21
+ };
22
+ export {};
package/lib/Factory.js ADDED
@@ -0,0 +1,123 @@
1
+ import { Util } from "./Util.js";
2
+ import { getComponentValidator } from "./Validators.js";
3
+ const GET = 'get';
4
+ const SET = 'set';
5
+ export const Factory = {
6
+ addGetterSetter(constructor, attr, def, validator, after) {
7
+ Factory.addGetter(constructor, attr, def);
8
+ Factory.addSetter(constructor, attr, validator, after);
9
+ Factory.addOverloadedGetterSetter(constructor, attr);
10
+ },
11
+ addGetter(constructor, attr, def) {
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
+ };
19
+ },
20
+ addSetter(constructor, attr, validator, after) {
21
+ const method = SET + Util._capitalize(attr);
22
+ if (!constructor.prototype[method]) {
23
+ Factory.overWriteSetter(constructor, attr, validator, after);
24
+ }
25
+ },
26
+ overWriteSetter(constructor, attr, validator, after) {
27
+ const method = SET + Util._capitalize(attr);
28
+ constructor.prototype[method] = function (val) {
29
+ if (validator && val !== undefined && val !== null) {
30
+ val = validator.call(this, val, attr);
31
+ }
32
+ this._setAttr(attr, val);
33
+ if (after) {
34
+ after.call(this);
35
+ }
36
+ return this;
37
+ };
38
+ },
39
+ addComponentsGetterSetter(constructor, attr, components, validator, after) {
40
+ const len = components.length, capitalize = Util._capitalize, getter = GET + capitalize(attr), setter = SET + capitalize(attr);
41
+ constructor.prototype[getter] = function () {
42
+ const ret = {};
43
+ for (let n = 0; n < len; n++) {
44
+ const component = components[n];
45
+ ret[component] = this.getAttr(attr + capitalize(component));
46
+ }
47
+ return ret;
48
+ };
49
+ const basicValidator = getComponentValidator(components);
50
+ constructor.prototype[setter] = function (val) {
51
+ const oldVal = this.attrs[attr];
52
+ if (validator) {
53
+ val = validator.call(this, val, attr);
54
+ }
55
+ if (basicValidator) {
56
+ basicValidator.call(this, val, attr);
57
+ }
58
+ for (const key in val) {
59
+ if (!val.hasOwnProperty(key)) {
60
+ continue;
61
+ }
62
+ this._setAttr(attr + capitalize(key), val[key]);
63
+ }
64
+ if (!val) {
65
+ components.forEach((component) => {
66
+ this._setAttr(attr + capitalize(component), undefined);
67
+ });
68
+ }
69
+ this._fireChangeEvent(attr, oldVal, val);
70
+ if (after) {
71
+ after.call(this);
72
+ }
73
+ return this;
74
+ };
75
+ Factory.addOverloadedGetterSetter(constructor, attr);
76
+ },
77
+ addOverloadedGetterSetter(constructor, attr) {
78
+ const capitalizedAttr = Util._capitalize(attr), setter = SET + capitalizedAttr, getter = GET + capitalizedAttr;
79
+ constructor.prototype[attr] = function () {
80
+ if (arguments.length) {
81
+ this[setter](arguments[0]);
82
+ return this;
83
+ }
84
+ return this[getter]();
85
+ };
86
+ },
87
+ addDeprecatedGetterSetter(constructor, attr, def, validator) {
88
+ Util.error('Adding deprecated ' + attr);
89
+ const method = GET + Util._capitalize(attr);
90
+ const message = attr +
91
+ ' property is deprecated and will be removed soon. Look at Konva change log for more information.';
92
+ constructor.prototype[method] = function () {
93
+ Util.error(message);
94
+ const val = this.attrs[attr];
95
+ return val === undefined ? def : val;
96
+ };
97
+ Factory.addSetter(constructor, attr, validator, function () {
98
+ Util.error(message);
99
+ });
100
+ Factory.addOverloadedGetterSetter(constructor, attr);
101
+ },
102
+ backCompat(constructor, methods) {
103
+ Util.each(methods, function (oldMethodName, newMethodName) {
104
+ const method = constructor.prototype[newMethodName];
105
+ const oldGetter = GET + Util._capitalize(oldMethodName);
106
+ const oldSetter = SET + Util._capitalize(oldMethodName);
107
+ function deprecated() {
108
+ method.apply(this, arguments);
109
+ Util.error('"' +
110
+ oldMethodName +
111
+ '" method is deprecated and will be removed soon. Use ""' +
112
+ newMethodName +
113
+ '" instead.');
114
+ }
115
+ constructor.prototype[oldMethodName] = deprecated;
116
+ constructor.prototype[oldGetter] = deprecated;
117
+ constructor.prototype[oldSetter] = deprecated;
118
+ });
119
+ },
120
+ afterSetFilter() {
121
+ this._filterUpToDate = false;
122
+ },
123
+ };
@@ -0,0 +1,4 @@
1
+ import { Layer } from './Layer.ts';
2
+ export declare class FastLayer extends Layer {
3
+ constructor(attrs: any);
4
+ }
@@ -0,0 +1,12 @@
1
+ import { Util } from "./Util.js";
2
+ import { Layer } from "./Layer.js";
3
+ import { _registerNode } from "./Global.js";
4
+ export class FastLayer extends Layer {
5
+ constructor(attrs) {
6
+ super(attrs);
7
+ this.listening(false);
8
+ Util.warn('Konva.Fast layer is deprecated. Please use "new Konva.Layer({ listening: false })" instead.');
9
+ }
10
+ }
11
+ FastLayer.prototype.nodeType = 'FastLayer';
12
+ _registerNode(FastLayer);
@@ -0,0 +1,37 @@
1
+ export declare const glob: any;
2
+ export declare const Konva: {
3
+ _global: any;
4
+ version: string;
5
+ isBrowser: boolean;
6
+ isUnminified: boolean;
7
+ dblClickWindow: number;
8
+ getAngle(angle: number): number;
9
+ enableTrace: boolean;
10
+ pointerEventsEnabled: boolean;
11
+ autoDrawEnabled: boolean;
12
+ hitOnDragEnabled: boolean;
13
+ capturePointerEventsEnabled: boolean;
14
+ _mouseListenClick: boolean;
15
+ _touchListenClick: boolean;
16
+ _pointerListenClick: boolean;
17
+ _mouseInDblClickWindow: boolean;
18
+ _touchInDblClickWindow: boolean;
19
+ _pointerInDblClickWindow: boolean;
20
+ _mouseDblClickPointerId: null;
21
+ _touchDblClickPointerId: null;
22
+ _pointerDblClickPointerId: null;
23
+ _renderBackend: string;
24
+ legacyTextRendering: boolean;
25
+ pixelRatio: number;
26
+ dragDistance: number;
27
+ angleDeg: boolean;
28
+ showWarnings: boolean;
29
+ dragButtons: number[];
30
+ isDragging(): boolean;
31
+ isTransforming(): boolean;
32
+ isDragReady(): boolean;
33
+ releaseCanvasOnDestroy: boolean;
34
+ document: any;
35
+ _injectGlobal(Konva: any): void;
36
+ };
37
+ export declare const _registerNode: (NodeClass: any) => void;
package/lib/Global.js ADDED
@@ -0,0 +1,66 @@
1
+ const PI_OVER_180 = Math.PI / 180;
2
+ function detectBrowser() {
3
+ return (typeof window !== 'undefined' &&
4
+ ({}.toString.call(window) === '[object Window]' ||
5
+ {}.toString.call(window) === '[object global]'));
6
+ }
7
+ export const glob = typeof global !== 'undefined'
8
+ ? global
9
+ : typeof window !== 'undefined'
10
+ ? window
11
+ : typeof WorkerGlobalScope !== 'undefined'
12
+ ? self
13
+ : {};
14
+ export const Konva = {
15
+ _global: glob,
16
+ version: '10.0.11',
17
+ isBrowser: detectBrowser(),
18
+ isUnminified: /param/.test(function (param) { }.toString()),
19
+ dblClickWindow: 400,
20
+ getAngle(angle) {
21
+ return Konva.angleDeg ? angle * PI_OVER_180 : angle;
22
+ },
23
+ enableTrace: false,
24
+ pointerEventsEnabled: true,
25
+ autoDrawEnabled: true,
26
+ hitOnDragEnabled: false,
27
+ capturePointerEventsEnabled: false,
28
+ _mouseListenClick: false,
29
+ _touchListenClick: false,
30
+ _pointerListenClick: false,
31
+ _mouseInDblClickWindow: false,
32
+ _touchInDblClickWindow: false,
33
+ _pointerInDblClickWindow: false,
34
+ _mouseDblClickPointerId: null,
35
+ _touchDblClickPointerId: null,
36
+ _pointerDblClickPointerId: null,
37
+ _renderBackend: 'web',
38
+ legacyTextRendering: false,
39
+ pixelRatio: (typeof window !== 'undefined' && window.devicePixelRatio) || 1,
40
+ dragDistance: 3,
41
+ angleDeg: true,
42
+ showWarnings: true,
43
+ dragButtons: [0, 1],
44
+ isDragging() {
45
+ return Konva['DD'].isDragging;
46
+ },
47
+ isTransforming() {
48
+ var _a, _b;
49
+ return (_b = (_a = Konva['Transformer']) === null || _a === void 0 ? void 0 : _a.isTransforming()) !== null && _b !== void 0 ? _b : false;
50
+ },
51
+ isDragReady() {
52
+ return !!Konva['DD'].node;
53
+ },
54
+ releaseCanvasOnDestroy: true,
55
+ document: glob.document,
56
+ _injectGlobal(Konva) {
57
+ if (typeof glob.Konva !== 'undefined') {
58
+ console.error('Several Konva instances detected. It is not recommended to use multiple Konva instances in the same environment.');
59
+ }
60
+ glob.Konva = Konva;
61
+ },
62
+ };
63
+ export const _registerNode = (NodeClass) => {
64
+ Konva[NodeClass.prototype.getClassName()] = NodeClass;
65
+ };
66
+ Konva._injectGlobal(Konva);
package/lib/Group.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ import type { ContainerConfig } from './Container.ts';
2
+ import { Container } from './Container.ts';
3
+ import type { Node } from './Node.ts';
4
+ import type { Shape } from './Shape.ts';
5
+ export interface GroupConfig extends ContainerConfig {
6
+ }
7
+ export declare class Group extends Container<Group | Shape> {
8
+ _validateAdd(child: Node): void;
9
+ }
package/lib/Group.js ADDED
@@ -0,0 +1,13 @@
1
+ import { Util } from "./Util.js";
2
+ import { Container } from "./Container.js";
3
+ import { _registerNode } from "./Global.js";
4
+ export class Group extends Container {
5
+ _validateAdd(child) {
6
+ const type = child.getType();
7
+ if (type !== 'Group' && type !== 'Shape') {
8
+ Util.throw('You may only add groups and shapes to groups.');
9
+ }
10
+ }
11
+ }
12
+ Group.prototype.nodeType = 'Group';
13
+ _registerNode(Group);
package/lib/Layer.d.ts ADDED
@@ -0,0 +1,62 @@
1
+ import type { ContainerConfig } from './Container.ts';
2
+ import { Container } from './Container.ts';
3
+ import { Node } from './Node.ts';
4
+ import { SceneCanvas, HitCanvas } from './Canvas.ts';
5
+ import type { Stage } from './Stage.ts';
6
+ import type { GetSet, Vector2d } from './types.ts';
7
+ import type { Group } from './Group.ts';
8
+ import type { Shape } from './Shape.ts';
9
+ export interface LayerConfig extends ContainerConfig {
10
+ clearBeforeDraw?: boolean;
11
+ hitGraphEnabled?: boolean;
12
+ imageSmoothingEnabled?: boolean;
13
+ }
14
+ export declare class Layer extends Container<Group | Shape> {
15
+ canvas: SceneCanvas;
16
+ hitCanvas: HitCanvas;
17
+ _waitingForDraw: boolean;
18
+ constructor(config?: LayerConfig);
19
+ createPNGStream(): any;
20
+ getCanvas(): SceneCanvas;
21
+ getNativeCanvasElement(): HTMLCanvasElement;
22
+ getHitCanvas(): HitCanvas;
23
+ getContext(): import("./Context.ts").Context;
24
+ clear(bounds?: any): this;
25
+ setZIndex(index: number): this;
26
+ moveToTop(): boolean;
27
+ moveUp(): boolean;
28
+ moveDown(): boolean;
29
+ moveToBottom(): boolean;
30
+ getLayer(): this;
31
+ remove(): this;
32
+ getStage(): Stage;
33
+ setSize({ width, height }: {
34
+ width: any;
35
+ height: any;
36
+ }): this;
37
+ _validateAdd(child: any): void;
38
+ _toKonvaCanvas(config: any): SceneCanvas;
39
+ _checkVisibility(): void;
40
+ _setSmoothEnabled(): void;
41
+ getWidth(): number | undefined;
42
+ setWidth(): void;
43
+ getHeight(): number | undefined;
44
+ setHeight(): void;
45
+ batchDraw(): this;
46
+ getIntersection(pos: Vector2d): Shape<import("./Shape.ts").ShapeConfig> | null;
47
+ _getIntersection(pos: Vector2d): {
48
+ shape?: Shape;
49
+ antialiased?: boolean;
50
+ };
51
+ drawScene(can?: SceneCanvas, top?: Node, bufferCanvas?: SceneCanvas): this;
52
+ drawHit(can?: HitCanvas, top?: Node): this;
53
+ enableHitGraph(): this;
54
+ disableHitGraph(): this;
55
+ setHitGraphEnabled(val: any): void;
56
+ getHitGraphEnabled(val: any): boolean;
57
+ toggleHitCanvas(): void;
58
+ destroy(): this;
59
+ hitGraphEnabled: GetSet<boolean, this>;
60
+ clearBeforeDraw: GetSet<boolean, this>;
61
+ imageSmoothingEnabled: GetSet<boolean, this>;
62
+ }
package/lib/Layer.js ADDED
@@ -0,0 +1,305 @@
1
+ import { Util } from "./Util.js";
2
+ import { Container } from "./Container.js";
3
+ import { Node } from "./Node.js";
4
+ import { Factory } from "./Factory.js";
5
+ import { SceneCanvas, HitCanvas } from "./Canvas.js";
6
+ import { getBooleanValidator } from "./Validators.js";
7
+ import { shapes } from "./Shape.js";
8
+ import { _registerNode } from "./Global.js";
9
+ const HASH = '#', BEFORE_DRAW = 'beforeDraw', DRAW = 'draw', INTERSECTION_OFFSETS = [
10
+ { x: 0, y: 0 },
11
+ { x: -1, y: -1 },
12
+ { x: 1, y: -1 },
13
+ { x: 1, y: 1 },
14
+ { x: -1, y: 1 },
15
+ ], INTERSECTION_OFFSETS_LEN = INTERSECTION_OFFSETS.length;
16
+ export class Layer extends Container {
17
+ constructor(config) {
18
+ super(config);
19
+ this.canvas = new SceneCanvas();
20
+ this.hitCanvas = new HitCanvas({
21
+ pixelRatio: 1,
22
+ });
23
+ this._waitingForDraw = false;
24
+ this.on('visibleChange.konva', this._checkVisibility);
25
+ this._checkVisibility();
26
+ this.on('imageSmoothingEnabledChange.konva', this._setSmoothEnabled);
27
+ this._setSmoothEnabled();
28
+ }
29
+ createPNGStream() {
30
+ const c = this.canvas._canvas;
31
+ return c.createPNGStream();
32
+ }
33
+ getCanvas() {
34
+ return this.canvas;
35
+ }
36
+ getNativeCanvasElement() {
37
+ return this.canvas._canvas;
38
+ }
39
+ getHitCanvas() {
40
+ return this.hitCanvas;
41
+ }
42
+ getContext() {
43
+ return this.getCanvas().getContext();
44
+ }
45
+ clear(bounds) {
46
+ this.getContext().clear(bounds);
47
+ this.getHitCanvas().getContext().clear(bounds);
48
+ return this;
49
+ }
50
+ setZIndex(index) {
51
+ super.setZIndex(index);
52
+ const stage = this.getStage();
53
+ if (stage && stage.content) {
54
+ stage.content.removeChild(this.getNativeCanvasElement());
55
+ if (index < stage.children.length - 1) {
56
+ stage.content.insertBefore(this.getNativeCanvasElement(), stage.children[index + 1].getCanvas()._canvas);
57
+ }
58
+ else {
59
+ stage.content.appendChild(this.getNativeCanvasElement());
60
+ }
61
+ }
62
+ return this;
63
+ }
64
+ moveToTop() {
65
+ Node.prototype.moveToTop.call(this);
66
+ const stage = this.getStage();
67
+ if (stage && stage.content) {
68
+ stage.content.removeChild(this.getNativeCanvasElement());
69
+ stage.content.appendChild(this.getNativeCanvasElement());
70
+ }
71
+ return true;
72
+ }
73
+ moveUp() {
74
+ const moved = Node.prototype.moveUp.call(this);
75
+ if (!moved) {
76
+ return false;
77
+ }
78
+ const stage = this.getStage();
79
+ if (!stage || !stage.content) {
80
+ return false;
81
+ }
82
+ stage.content.removeChild(this.getNativeCanvasElement());
83
+ if (this.index < stage.children.length - 1) {
84
+ stage.content.insertBefore(this.getNativeCanvasElement(), stage.children[this.index + 1].getCanvas()._canvas);
85
+ }
86
+ else {
87
+ stage.content.appendChild(this.getNativeCanvasElement());
88
+ }
89
+ return true;
90
+ }
91
+ moveDown() {
92
+ if (Node.prototype.moveDown.call(this)) {
93
+ const stage = this.getStage();
94
+ if (stage) {
95
+ const children = stage.children;
96
+ if (stage.content) {
97
+ stage.content.removeChild(this.getNativeCanvasElement());
98
+ stage.content.insertBefore(this.getNativeCanvasElement(), children[this.index + 1].getCanvas()._canvas);
99
+ }
100
+ }
101
+ return true;
102
+ }
103
+ return false;
104
+ }
105
+ moveToBottom() {
106
+ if (Node.prototype.moveToBottom.call(this)) {
107
+ const stage = this.getStage();
108
+ if (stage) {
109
+ const children = stage.children;
110
+ if (stage.content) {
111
+ stage.content.removeChild(this.getNativeCanvasElement());
112
+ stage.content.insertBefore(this.getNativeCanvasElement(), children[1].getCanvas()._canvas);
113
+ }
114
+ }
115
+ return true;
116
+ }
117
+ return false;
118
+ }
119
+ getLayer() {
120
+ return this;
121
+ }
122
+ remove() {
123
+ const _canvas = this.getNativeCanvasElement();
124
+ Node.prototype.remove.call(this);
125
+ if (_canvas && _canvas.parentNode && Util._isInDocument(_canvas)) {
126
+ _canvas.parentNode.removeChild(_canvas);
127
+ }
128
+ return this;
129
+ }
130
+ getStage() {
131
+ return this.parent;
132
+ }
133
+ setSize({ width, height }) {
134
+ this.canvas.setSize(width, height);
135
+ this.hitCanvas.setSize(width, height);
136
+ this._setSmoothEnabled();
137
+ return this;
138
+ }
139
+ _validateAdd(child) {
140
+ const type = child.getType();
141
+ if (type !== 'Group' && type !== 'Shape') {
142
+ Util.throw('You may only add groups and shapes to a layer.');
143
+ }
144
+ }
145
+ _toKonvaCanvas(config) {
146
+ config = { ...config };
147
+ config.width = config.width || this.getWidth();
148
+ config.height = config.height || this.getHeight();
149
+ config.x = config.x !== undefined ? config.x : this.x();
150
+ config.y = config.y !== undefined ? config.y : this.y();
151
+ return Node.prototype._toKonvaCanvas.call(this, config);
152
+ }
153
+ _checkVisibility() {
154
+ const visible = this.visible();
155
+ if (visible) {
156
+ this.canvas._canvas.style.display = 'block';
157
+ }
158
+ else {
159
+ this.canvas._canvas.style.display = 'none';
160
+ }
161
+ }
162
+ _setSmoothEnabled() {
163
+ this.getContext()._context.imageSmoothingEnabled =
164
+ this.imageSmoothingEnabled();
165
+ }
166
+ getWidth() {
167
+ if (this.parent) {
168
+ return this.parent.width();
169
+ }
170
+ }
171
+ setWidth() {
172
+ Util.warn('Can not change width of layer. Use "stage.width(value)" function instead.');
173
+ }
174
+ getHeight() {
175
+ if (this.parent) {
176
+ return this.parent.height();
177
+ }
178
+ }
179
+ setHeight() {
180
+ Util.warn('Can not change height of layer. Use "stage.height(value)" function instead.');
181
+ }
182
+ batchDraw() {
183
+ if (!this._waitingForDraw) {
184
+ this._waitingForDraw = true;
185
+ Util.requestAnimFrame(() => {
186
+ this.draw();
187
+ this._waitingForDraw = false;
188
+ });
189
+ }
190
+ return this;
191
+ }
192
+ getIntersection(pos) {
193
+ if (!this.isListening() || !this.isVisible()) {
194
+ return null;
195
+ }
196
+ let spiralSearchDistance = 1;
197
+ let continueSearch = false;
198
+ while (true) {
199
+ for (let i = 0; i < INTERSECTION_OFFSETS_LEN; i++) {
200
+ const intersectionOffset = INTERSECTION_OFFSETS[i];
201
+ const obj = this._getIntersection({
202
+ x: pos.x + intersectionOffset.x * spiralSearchDistance,
203
+ y: pos.y + intersectionOffset.y * spiralSearchDistance,
204
+ });
205
+ const shape = obj.shape;
206
+ if (shape) {
207
+ return shape;
208
+ }
209
+ continueSearch = !!obj.antialiased;
210
+ if (!obj.antialiased) {
211
+ break;
212
+ }
213
+ }
214
+ if (continueSearch) {
215
+ spiralSearchDistance += 1;
216
+ }
217
+ else {
218
+ return null;
219
+ }
220
+ }
221
+ }
222
+ _getIntersection(pos) {
223
+ const ratio = this.hitCanvas.pixelRatio;
224
+ const p = this.hitCanvas.context.getImageData(Math.round(pos.x * ratio), Math.round(pos.y * ratio), 1, 1).data;
225
+ const p3 = p[3];
226
+ if (p3 === 255) {
227
+ const colorKey = Util.getHitColorKey(p[0], p[1], p[2]);
228
+ const shape = shapes[colorKey];
229
+ if (shape) {
230
+ return {
231
+ shape: shape,
232
+ };
233
+ }
234
+ return {
235
+ antialiased: true,
236
+ };
237
+ }
238
+ else if (p3 > 0) {
239
+ return {
240
+ antialiased: true,
241
+ };
242
+ }
243
+ return {};
244
+ }
245
+ drawScene(can, top, bufferCanvas) {
246
+ const layer = this.getLayer(), canvas = can || (layer && layer.getCanvas());
247
+ this._fire(BEFORE_DRAW, {
248
+ node: this,
249
+ });
250
+ if (this.clearBeforeDraw()) {
251
+ canvas.getContext().clear();
252
+ }
253
+ Container.prototype.drawScene.call(this, canvas, top, bufferCanvas);
254
+ this._fire(DRAW, {
255
+ node: this,
256
+ });
257
+ return this;
258
+ }
259
+ drawHit(can, top) {
260
+ const layer = this.getLayer(), canvas = can || (layer && layer.hitCanvas);
261
+ if (layer && layer.clearBeforeDraw()) {
262
+ layer.getHitCanvas().getContext().clear();
263
+ }
264
+ Container.prototype.drawHit.call(this, canvas, top);
265
+ return this;
266
+ }
267
+ enableHitGraph() {
268
+ this.hitGraphEnabled(true);
269
+ return this;
270
+ }
271
+ disableHitGraph() {
272
+ this.hitGraphEnabled(false);
273
+ return this;
274
+ }
275
+ setHitGraphEnabled(val) {
276
+ Util.warn('hitGraphEnabled method is deprecated. Please use layer.listening() instead.');
277
+ this.listening(val);
278
+ }
279
+ getHitGraphEnabled(val) {
280
+ Util.warn('hitGraphEnabled method is deprecated. Please use layer.listening() instead.');
281
+ return this.listening();
282
+ }
283
+ toggleHitCanvas() {
284
+ if (!this.parent || !this.parent['content']) {
285
+ return;
286
+ }
287
+ const parent = this.parent;
288
+ const added = !!this.hitCanvas._canvas.parentNode;
289
+ if (added) {
290
+ parent.content.removeChild(this.hitCanvas._canvas);
291
+ }
292
+ else {
293
+ parent.content.appendChild(this.hitCanvas._canvas);
294
+ }
295
+ }
296
+ destroy() {
297
+ Util.releaseCanvas(this.getNativeCanvasElement(), this.getHitCanvas()._canvas);
298
+ return super.destroy();
299
+ }
300
+ }
301
+ Layer.prototype.nodeType = 'Layer';
302
+ _registerNode(Layer);
303
+ Factory.addGetterSetter(Layer, 'imageSmoothingEnabled', true);
304
+ Factory.addGetterSetter(Layer, 'clearBeforeDraw', true);
305
+ Factory.addGetterSetter(Layer, 'hitGraphEnabled', true, getBooleanValidator());