konva 8.4.3 → 9.0.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 (190) hide show
  1. package/README.md +1 -9
  2. package/konva.js +17669 -16989
  3. package/konva.min.js +3 -3
  4. package/lib/Animation.js +11 -7
  5. package/lib/BezierFunctions.d.ts +6 -0
  6. package/lib/BezierFunctions.js +791 -0
  7. package/lib/Canvas.d.ts +1 -0
  8. package/lib/Canvas.js +26 -18
  9. package/lib/Container.js +16 -12
  10. package/lib/Context.d.ts +3 -1
  11. package/lib/Context.js +18 -10
  12. package/lib/Core.js +7 -3
  13. package/lib/DragAndDrop.js +25 -22
  14. package/lib/Factory.js +27 -24
  15. package/lib/FastLayer.js +10 -6
  16. package/lib/Global.js +16 -12
  17. package/lib/Group.js +10 -6
  18. package/lib/Layer.js +37 -33
  19. package/lib/Node.js +95 -90
  20. package/lib/PointerEvents.js +15 -7
  21. package/lib/Shape.js +90 -86
  22. package/lib/Stage.js +51 -47
  23. package/lib/Tween.js +26 -22
  24. package/lib/Util.js +29 -25
  25. package/lib/Validators.js +58 -44
  26. package/lib/_CoreInternals.js +36 -33
  27. package/lib/_FullInternals.js +78 -75
  28. package/lib/filters/Blur.js +9 -5
  29. package/lib/filters/Brighten.js +9 -5
  30. package/lib/filters/Contrast.js +9 -5
  31. package/lib/filters/Emboss.js +14 -10
  32. package/lib/filters/Enhance.js +9 -5
  33. package/lib/filters/Grayscale.js +5 -1
  34. package/lib/filters/HSL.js +11 -7
  35. package/lib/filters/HSV.js +11 -7
  36. package/lib/filters/Invert.js +5 -1
  37. package/lib/filters/Kaleidoscope.js +13 -9
  38. package/lib/filters/Mask.js +9 -5
  39. package/lib/filters/Noise.js +9 -5
  40. package/lib/filters/Pixelate.js +11 -7
  41. package/lib/filters/Posterize.js +9 -5
  42. package/lib/filters/RGB.js +11 -7
  43. package/lib/filters/RGBA.js +12 -8
  44. package/lib/filters/Sepia.js +5 -1
  45. package/lib/filters/Solarize.js +5 -1
  46. package/lib/filters/Threshold.js +9 -5
  47. package/lib/index-node.js +7 -5
  48. package/lib/index.js +4 -2
  49. package/lib/shapes/Arc.js +17 -13
  50. package/lib/shapes/Arrow.js +17 -13
  51. package/lib/shapes/Circle.js +11 -7
  52. package/lib/shapes/Ellipse.js +13 -9
  53. package/lib/shapes/Image.js +21 -17
  54. package/lib/shapes/Label.js +18 -13
  55. package/lib/shapes/Line.js +14 -10
  56. package/lib/shapes/Path.d.ts +5 -2
  57. package/lib/shapes/Path.js +44 -43
  58. package/lib/shapes/Rect.js +13 -9
  59. package/lib/shapes/RegularPolygon.js +12 -8
  60. package/lib/shapes/Ring.js +12 -8
  61. package/lib/shapes/Sprite.js +19 -15
  62. package/lib/shapes/Star.js +13 -9
  63. package/lib/shapes/Text.js +31 -26
  64. package/lib/shapes/TextPath.d.ts +5 -1
  65. package/lib/shapes/TextPath.js +82 -188
  66. package/lib/shapes/Transformer.js +85 -81
  67. package/lib/shapes/Wedge.js +16 -12
  68. package/lib/types.d.ts +6 -0
  69. package/lib/types.js +5 -2
  70. package/package.json +13 -42
  71. package/cmj/Animation.d.ts +0 -24
  72. package/cmj/Animation.js +0 -145
  73. package/cmj/Canvas.d.ts +0 -32
  74. package/cmj/Canvas.js +0 -114
  75. package/cmj/Container.d.ts +0 -47
  76. package/cmj/Container.js +0 -325
  77. package/cmj/Context.d.ts +0 -85
  78. package/cmj/Context.js +0 -538
  79. package/cmj/Core.d.ts +0 -3
  80. package/cmj/Core.js +0 -7
  81. package/cmj/DragAndDrop.d.ts +0 -17
  82. package/cmj/DragAndDrop.js +0 -111
  83. package/cmj/Factory.d.ts +0 -11
  84. package/cmj/Factory.js +0 -125
  85. package/cmj/FastLayer.d.ts +0 -4
  86. package/cmj/FastLayer.js +0 -16
  87. package/cmj/Global.d.ts +0 -34
  88. package/cmj/Global.js +0 -61
  89. package/cmj/Group.d.ts +0 -8
  90. package/cmj/Group.js +0 -17
  91. package/cmj/Layer.d.ts +0 -61
  92. package/cmj/Layer.js +0 -309
  93. package/cmj/Node.d.ts +0 -328
  94. package/cmj/Node.js +0 -1420
  95. package/cmj/PointerEvents.d.ts +0 -11
  96. package/cmj/PointerEvents.js +0 -45
  97. package/cmj/Shape.d.ts +0 -186
  98. package/cmj/Shape.js +0 -521
  99. package/cmj/Stage.d.ts +0 -85
  100. package/cmj/Stage.js +0 -692
  101. package/cmj/Tween.d.ts +0 -86
  102. package/cmj/Tween.js +0 -520
  103. package/cmj/Util.d.ts +0 -91
  104. package/cmj/Util.js +0 -801
  105. package/cmj/Validators.d.ts +0 -11
  106. package/cmj/Validators.js +0 -192
  107. package/cmj/_CoreInternals.d.ts +0 -152
  108. package/cmj/_CoreInternals.js +0 -37
  109. package/cmj/_FullInternals.d.ts +0 -196
  110. package/cmj/_FullInternals.js +0 -81
  111. package/cmj/filters/Blur.d.ts +0 -2
  112. package/cmj/filters/Blur.js +0 -701
  113. package/cmj/filters/Brighten.d.ts +0 -2
  114. package/cmj/filters/Brighten.js +0 -16
  115. package/cmj/filters/Contrast.d.ts +0 -2
  116. package/cmj/filters/Contrast.js +0 -38
  117. package/cmj/filters/Emboss.d.ts +0 -2
  118. package/cmj/filters/Emboss.js +0 -106
  119. package/cmj/filters/Enhance.d.ts +0 -2
  120. package/cmj/filters/Enhance.js +0 -87
  121. package/cmj/filters/Grayscale.d.ts +0 -2
  122. package/cmj/filters/Grayscale.js +0 -13
  123. package/cmj/filters/HSL.d.ts +0 -2
  124. package/cmj/filters/HSL.js +0 -28
  125. package/cmj/filters/HSV.d.ts +0 -2
  126. package/cmj/filters/HSV.js +0 -28
  127. package/cmj/filters/Invert.d.ts +0 -2
  128. package/cmj/filters/Invert.js +0 -12
  129. package/cmj/filters/Kaleidoscope.d.ts +0 -2
  130. package/cmj/filters/Kaleidoscope.js +0 -137
  131. package/cmj/filters/Mask.d.ts +0 -2
  132. package/cmj/filters/Mask.js +0 -144
  133. package/cmj/filters/Noise.d.ts +0 -2
  134. package/cmj/filters/Noise.js +0 -16
  135. package/cmj/filters/Pixelate.d.ts +0 -2
  136. package/cmj/filters/Pixelate.js +0 -64
  137. package/cmj/filters/Posterize.d.ts +0 -2
  138. package/cmj/filters/Posterize.js +0 -14
  139. package/cmj/filters/RGB.d.ts +0 -2
  140. package/cmj/filters/RGB.js +0 -43
  141. package/cmj/filters/RGBA.d.ts +0 -2
  142. package/cmj/filters/RGBA.js +0 -53
  143. package/cmj/filters/Sepia.d.ts +0 -2
  144. package/cmj/filters/Sepia.js +0 -15
  145. package/cmj/filters/Solarize.d.ts +0 -2
  146. package/cmj/filters/Solarize.js +0 -29
  147. package/cmj/filters/Threshold.d.ts +0 -2
  148. package/cmj/filters/Threshold.js +0 -14
  149. package/cmj/index-node.d.ts +0 -2
  150. package/cmj/index-node.js +0 -21
  151. package/cmj/index-types.d.ts +0 -181
  152. package/cmj/index.d.ts +0 -2
  153. package/cmj/index.js +0 -4
  154. package/cmj/package.json +0 -6
  155. package/cmj/shapes/Arc.d.ts +0 -26
  156. package/cmj/shapes/Arc.js +0 -59
  157. package/cmj/shapes/Arrow.d.ts +0 -26
  158. package/cmj/shapes/Arrow.js +0 -103
  159. package/cmj/shapes/Circle.d.ts +0 -14
  160. package/cmj/shapes/Circle.js +0 -37
  161. package/cmj/shapes/Ellipse.d.ts +0 -17
  162. package/cmj/shapes/Ellipse.js +0 -41
  163. package/cmj/shapes/Image.d.ts +0 -25
  164. package/cmj/shapes/Image.js +0 -117
  165. package/cmj/shapes/Label.d.ts +0 -36
  166. package/cmj/shapes/Label.js +0 -178
  167. package/cmj/shapes/Line.d.ts +0 -28
  168. package/cmj/shapes/Line.js +0 -159
  169. package/cmj/shapes/Path.d.ts +0 -37
  170. package/cmj/shapes/Path.js +0 -631
  171. package/cmj/shapes/Rect.d.ts +0 -10
  172. package/cmj/shapes/Rect.js +0 -26
  173. package/cmj/shapes/RegularPolygon.d.ts +0 -23
  174. package/cmj/shapes/RegularPolygon.js +0 -69
  175. package/cmj/shapes/Ring.d.ts +0 -16
  176. package/cmj/shapes/Ring.js +0 -37
  177. package/cmj/shapes/Sprite.d.ts +0 -31
  178. package/cmj/shapes/Sprite.js +0 -112
  179. package/cmj/shapes/Star.d.ts +0 -18
  180. package/cmj/shapes/Star.js +0 -42
  181. package/cmj/shapes/Text.d.ts +0 -63
  182. package/cmj/shapes/Text.js +0 -373
  183. package/cmj/shapes/TextPath.d.ts +0 -56
  184. package/cmj/shapes/TextPath.js +0 -351
  185. package/cmj/shapes/Transformer.d.ts +0 -116
  186. package/cmj/shapes/Transformer.js +0 -971
  187. package/cmj/shapes/Wedge.d.ts +0 -18
  188. package/cmj/shapes/Wedge.js +0 -42
  189. package/cmj/types.d.ts +0 -50
  190. package/cmj/types.js +0 -25
package/cmj/Animation.js DELETED
@@ -1,145 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Animation = void 0;
4
- const Global_1 = require("./Global");
5
- const Util_1 = require("./Util");
6
- var now = (function () {
7
- if (Global_1.glob.performance && Global_1.glob.performance.now) {
8
- return function () {
9
- return Global_1.glob.performance.now();
10
- };
11
- }
12
- return function () {
13
- return new Date().getTime();
14
- };
15
- })();
16
- class Animation {
17
- constructor(func, layers) {
18
- this.id = Animation.animIdCounter++;
19
- this.frame = {
20
- time: 0,
21
- timeDiff: 0,
22
- lastTime: now(),
23
- frameRate: 0,
24
- };
25
- this.func = func;
26
- this.setLayers(layers);
27
- }
28
- setLayers(layers) {
29
- var lays = [];
30
- if (!layers) {
31
- lays = [];
32
- }
33
- else if (layers.length > 0) {
34
- lays = layers;
35
- }
36
- else {
37
- lays = [layers];
38
- }
39
- this.layers = lays;
40
- return this;
41
- }
42
- getLayers() {
43
- return this.layers;
44
- }
45
- addLayer(layer) {
46
- var layers = this.layers, len = layers.length, n;
47
- for (n = 0; n < len; n++) {
48
- if (layers[n]._id === layer._id) {
49
- return false;
50
- }
51
- }
52
- this.layers.push(layer);
53
- return true;
54
- }
55
- isRunning() {
56
- var a = Animation, animations = a.animations, len = animations.length, n;
57
- for (n = 0; n < len; n++) {
58
- if (animations[n].id === this.id) {
59
- return true;
60
- }
61
- }
62
- return false;
63
- }
64
- start() {
65
- this.stop();
66
- this.frame.timeDiff = 0;
67
- this.frame.lastTime = now();
68
- Animation._addAnimation(this);
69
- return this;
70
- }
71
- stop() {
72
- Animation._removeAnimation(this);
73
- return this;
74
- }
75
- _updateFrameObject(time) {
76
- this.frame.timeDiff = time - this.frame.lastTime;
77
- this.frame.lastTime = time;
78
- this.frame.time += this.frame.timeDiff;
79
- this.frame.frameRate = 1000 / this.frame.timeDiff;
80
- }
81
- static _addAnimation(anim) {
82
- this.animations.push(anim);
83
- this._handleAnimation();
84
- }
85
- static _removeAnimation(anim) {
86
- var id = anim.id, animations = this.animations, len = animations.length, n;
87
- for (n = 0; n < len; n++) {
88
- if (animations[n].id === id) {
89
- this.animations.splice(n, 1);
90
- break;
91
- }
92
- }
93
- }
94
- static _runFrames() {
95
- var layerHash = {}, animations = this.animations, anim, layers, func, n, i, layersLen, layer, key, needRedraw;
96
- for (n = 0; n < animations.length; n++) {
97
- anim = animations[n];
98
- layers = anim.layers;
99
- func = anim.func;
100
- anim._updateFrameObject(now());
101
- layersLen = layers.length;
102
- if (func) {
103
- needRedraw = func.call(anim, anim.frame) !== false;
104
- }
105
- else {
106
- needRedraw = true;
107
- }
108
- if (!needRedraw) {
109
- continue;
110
- }
111
- for (i = 0; i < layersLen; i++) {
112
- layer = layers[i];
113
- if (layer._id !== undefined) {
114
- layerHash[layer._id] = layer;
115
- }
116
- }
117
- }
118
- for (key in layerHash) {
119
- if (!layerHash.hasOwnProperty(key)) {
120
- continue;
121
- }
122
- layerHash[key].batchDraw();
123
- }
124
- }
125
- static _animationLoop() {
126
- var Anim = Animation;
127
- if (Anim.animations.length) {
128
- Anim._runFrames();
129
- Util_1.Util.requestAnimFrame(Anim._animationLoop);
130
- }
131
- else {
132
- Anim.animRunning = false;
133
- }
134
- }
135
- static _handleAnimation() {
136
- if (!this.animRunning) {
137
- this.animRunning = true;
138
- Util_1.Util.requestAnimFrame(this._animationLoop);
139
- }
140
- }
141
- }
142
- exports.Animation = Animation;
143
- Animation.animations = [];
144
- Animation.animIdCounter = 0;
145
- Animation.animRunning = false;
package/cmj/Canvas.d.ts DELETED
@@ -1,32 +0,0 @@
1
- import { Context } from './Context';
2
- interface ICanvasConfig {
3
- width?: number;
4
- height?: number;
5
- pixelRatio?: number;
6
- }
7
- export declare class Canvas {
8
- pixelRatio: number;
9
- _canvas: HTMLCanvasElement;
10
- context: Context;
11
- width: number;
12
- height: number;
13
- isCache: boolean;
14
- constructor(config: ICanvasConfig);
15
- getContext(): Context;
16
- getPixelRatio(): number;
17
- setPixelRatio(pixelRatio: any): void;
18
- setWidth(width: any): void;
19
- setHeight(height: any): void;
20
- getWidth(): number;
21
- getHeight(): number;
22
- setSize(width: any, height: any): void;
23
- toDataURL(mimeType: any, quality: any): string;
24
- }
25
- export declare class SceneCanvas extends Canvas {
26
- constructor(config?: ICanvasConfig);
27
- }
28
- export declare class HitCanvas extends Canvas {
29
- hitCanvas: boolean;
30
- constructor(config?: ICanvasConfig);
31
- }
32
- export {};
package/cmj/Canvas.js DELETED
@@ -1,114 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HitCanvas = exports.SceneCanvas = exports.Canvas = void 0;
4
- const Util_1 = require("./Util");
5
- const Context_1 = require("./Context");
6
- const Global_1 = require("./Global");
7
- const Factory_1 = require("./Factory");
8
- const Validators_1 = require("./Validators");
9
- var _pixelRatio;
10
- function getDevicePixelRatio() {
11
- if (_pixelRatio) {
12
- return _pixelRatio;
13
- }
14
- var canvas = Util_1.Util.createCanvasElement();
15
- var context = canvas.getContext('2d');
16
- _pixelRatio = (function () {
17
- var devicePixelRatio = Global_1.Konva._global.devicePixelRatio || 1, backingStoreRatio = context.webkitBackingStorePixelRatio ||
18
- context.mozBackingStorePixelRatio ||
19
- context.msBackingStorePixelRatio ||
20
- context.oBackingStorePixelRatio ||
21
- context.backingStorePixelRatio ||
22
- 1;
23
- return devicePixelRatio / backingStoreRatio;
24
- })();
25
- Util_1.Util.releaseCanvas(canvas);
26
- return _pixelRatio;
27
- }
28
- class Canvas {
29
- constructor(config) {
30
- this.pixelRatio = 1;
31
- this.width = 0;
32
- this.height = 0;
33
- this.isCache = false;
34
- var conf = config || {};
35
- var pixelRatio = conf.pixelRatio || Global_1.Konva.pixelRatio || getDevicePixelRatio();
36
- this.pixelRatio = pixelRatio;
37
- this._canvas = Util_1.Util.createCanvasElement();
38
- this._canvas.style.padding = '0';
39
- this._canvas.style.margin = '0';
40
- this._canvas.style.border = '0';
41
- this._canvas.style.background = 'transparent';
42
- this._canvas.style.position = 'absolute';
43
- this._canvas.style.top = '0';
44
- this._canvas.style.left = '0';
45
- }
46
- getContext() {
47
- return this.context;
48
- }
49
- getPixelRatio() {
50
- return this.pixelRatio;
51
- }
52
- setPixelRatio(pixelRatio) {
53
- var previousRatio = this.pixelRatio;
54
- this.pixelRatio = pixelRatio;
55
- this.setSize(this.getWidth() / previousRatio, this.getHeight() / previousRatio);
56
- }
57
- setWidth(width) {
58
- this.width = this._canvas.width = width * this.pixelRatio;
59
- this._canvas.style.width = width + 'px';
60
- var pixelRatio = this.pixelRatio, _context = this.getContext()._context;
61
- _context.scale(pixelRatio, pixelRatio);
62
- }
63
- setHeight(height) {
64
- this.height = this._canvas.height = height * this.pixelRatio;
65
- this._canvas.style.height = height + 'px';
66
- var pixelRatio = this.pixelRatio, _context = this.getContext()._context;
67
- _context.scale(pixelRatio, pixelRatio);
68
- }
69
- getWidth() {
70
- return this.width;
71
- }
72
- getHeight() {
73
- return this.height;
74
- }
75
- setSize(width, height) {
76
- this.setWidth(width || 0);
77
- this.setHeight(height || 0);
78
- }
79
- toDataURL(mimeType, quality) {
80
- try {
81
- return this._canvas.toDataURL(mimeType, quality);
82
- }
83
- catch (e) {
84
- try {
85
- return this._canvas.toDataURL();
86
- }
87
- catch (err) {
88
- Util_1.Util.error('Unable to get data URL. ' +
89
- err.message +
90
- ' For more info read https://konvajs.org/docs/posts/Tainted_Canvas.html.');
91
- return '';
92
- }
93
- }
94
- }
95
- }
96
- exports.Canvas = Canvas;
97
- Factory_1.Factory.addGetterSetter(Canvas, 'pixelRatio', undefined, (0, Validators_1.getNumberValidator)());
98
- class SceneCanvas extends Canvas {
99
- constructor(config = { width: 0, height: 0 }) {
100
- super(config);
101
- this.context = new Context_1.SceneContext(this);
102
- this.setSize(config.width, config.height);
103
- }
104
- }
105
- exports.SceneCanvas = SceneCanvas;
106
- class HitCanvas extends Canvas {
107
- constructor(config = { width: 0, height: 0 }) {
108
- super(config);
109
- this.hitCanvas = true;
110
- this.context = new Context_1.HitContext(this);
111
- this.setSize(config.width, config.height);
112
- }
113
- }
114
- exports.HitCanvas = HitCanvas;
@@ -1,47 +0,0 @@
1
- import { Node, NodeConfig } from './Node';
2
- import { GetSet, IRect } from './types';
3
- import { HitCanvas, SceneCanvas } from './Canvas';
4
- import { SceneContext } from './Context';
5
- export interface ContainerConfig extends NodeConfig {
6
- clearBeforeDraw?: boolean;
7
- clipFunc?: (ctx: SceneContext) => void;
8
- clipX?: number;
9
- clipY?: number;
10
- clipWidth?: number;
11
- clipHeight?: number;
12
- }
13
- export declare abstract class Container<ChildType extends Node = Node> extends Node<ContainerConfig> {
14
- children: Array<ChildType> | undefined;
15
- getChildren(filterFunc?: (item: Node) => boolean): ChildType[];
16
- hasChildren(): boolean;
17
- removeChildren(): this;
18
- destroyChildren(): this;
19
- abstract _validateAdd(node: Node): void;
20
- add(...children: ChildType[]): this;
21
- destroy(): this;
22
- find<ChildNode extends Node = Node>(selector: any): Array<ChildNode>;
23
- findOne<ChildNode extends Node = Node>(selector: string | Function): ChildNode;
24
- _generalFind<ChildNode extends Node = Node>(selector: string | Function, findOne: boolean): ChildNode[];
25
- private _descendants;
26
- toObject(): any;
27
- isAncestorOf(node: Node): boolean;
28
- clone(obj?: any): this;
29
- getAllIntersections(pos: any): any[];
30
- _clearSelfAndDescendantCache(attr?: string): void;
31
- _setChildrenIndices(): void;
32
- drawScene(can?: SceneCanvas, top?: Node): this;
33
- drawHit(can?: HitCanvas, top?: Node): this;
34
- _drawChildren(drawMethod: any, canvas: any, top: any): void;
35
- getClientRect(config?: {
36
- skipTransform?: boolean;
37
- skipShadow?: boolean;
38
- skipStroke?: boolean;
39
- relativeTo?: Container<Node>;
40
- }): IRect;
41
- clip: GetSet<IRect, this>;
42
- clipX: GetSet<number, this>;
43
- clipY: GetSet<number, this>;
44
- clipWidth: GetSet<number, this>;
45
- clipHeight: GetSet<number, this>;
46
- clipFunc: GetSet<(ctx: CanvasRenderingContext2D, shape: Container<ChildType>) => void, this>;
47
- }
package/cmj/Container.js DELETED
@@ -1,325 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Container = void 0;
4
- const Factory_1 = require("./Factory");
5
- const Node_1 = require("./Node");
6
- const Validators_1 = require("./Validators");
7
- class Container extends Node_1.Node {
8
- constructor() {
9
- super(...arguments);
10
- this.children = [];
11
- }
12
- getChildren(filterFunc) {
13
- if (!filterFunc) {
14
- return this.children || [];
15
- }
16
- const children = this.children || [];
17
- var results = [];
18
- children.forEach(function (child) {
19
- if (filterFunc(child)) {
20
- results.push(child);
21
- }
22
- });
23
- return results;
24
- }
25
- hasChildren() {
26
- return this.getChildren().length > 0;
27
- }
28
- removeChildren() {
29
- this.getChildren().forEach((child) => {
30
- child.parent = null;
31
- child.index = 0;
32
- child.remove();
33
- });
34
- this.children = [];
35
- this._requestDraw();
36
- return this;
37
- }
38
- destroyChildren() {
39
- this.getChildren().forEach((child) => {
40
- child.parent = null;
41
- child.index = 0;
42
- child.destroy();
43
- });
44
- this.children = [];
45
- this._requestDraw();
46
- return this;
47
- }
48
- add(...children) {
49
- if (children.length === 0) {
50
- return this;
51
- }
52
- if (children.length > 1) {
53
- for (var i = 0; i < children.length; i++) {
54
- this.add(children[i]);
55
- }
56
- return this;
57
- }
58
- const child = children[0];
59
- if (child.getParent()) {
60
- child.moveTo(this);
61
- return this;
62
- }
63
- this._validateAdd(child);
64
- child.index = this.getChildren().length;
65
- child.parent = this;
66
- child._clearCaches();
67
- this.getChildren().push(child);
68
- this._fire('add', {
69
- child: child,
70
- });
71
- this._requestDraw();
72
- return this;
73
- }
74
- destroy() {
75
- if (this.hasChildren()) {
76
- this.destroyChildren();
77
- }
78
- super.destroy();
79
- return this;
80
- }
81
- find(selector) {
82
- return this._generalFind(selector, false);
83
- }
84
- findOne(selector) {
85
- var result = this._generalFind(selector, true);
86
- return result.length > 0 ? result[0] : undefined;
87
- }
88
- _generalFind(selector, findOne) {
89
- var retArr = [];
90
- this._descendants((node) => {
91
- const valid = node._isMatch(selector);
92
- if (valid) {
93
- retArr.push(node);
94
- }
95
- if (valid && findOne) {
96
- return true;
97
- }
98
- return false;
99
- });
100
- return retArr;
101
- }
102
- _descendants(fn) {
103
- let shouldStop = false;
104
- const children = this.getChildren();
105
- for (const child of children) {
106
- shouldStop = fn(child);
107
- if (shouldStop) {
108
- return true;
109
- }
110
- if (!child.hasChildren()) {
111
- continue;
112
- }
113
- shouldStop = child._descendants(fn);
114
- if (shouldStop) {
115
- return true;
116
- }
117
- }
118
- return false;
119
- }
120
- toObject() {
121
- var obj = Node_1.Node.prototype.toObject.call(this);
122
- obj.children = [];
123
- this.getChildren().forEach((child) => {
124
- obj.children.push(child.toObject());
125
- });
126
- return obj;
127
- }
128
- isAncestorOf(node) {
129
- var parent = node.getParent();
130
- while (parent) {
131
- if (parent._id === this._id) {
132
- return true;
133
- }
134
- parent = parent.getParent();
135
- }
136
- return false;
137
- }
138
- clone(obj) {
139
- var node = Node_1.Node.prototype.clone.call(this, obj);
140
- this.getChildren().forEach(function (no) {
141
- node.add(no.clone());
142
- });
143
- return node;
144
- }
145
- getAllIntersections(pos) {
146
- var arr = [];
147
- this.find('Shape').forEach(function (shape) {
148
- if (shape.isVisible() && shape.intersects(pos)) {
149
- arr.push(shape);
150
- }
151
- });
152
- return arr;
153
- }
154
- _clearSelfAndDescendantCache(attr) {
155
- var _a;
156
- super._clearSelfAndDescendantCache(attr);
157
- if (this.isCached()) {
158
- return;
159
- }
160
- (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (node) {
161
- node._clearSelfAndDescendantCache(attr);
162
- });
163
- }
164
- _setChildrenIndices() {
165
- var _a;
166
- (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child, n) {
167
- child.index = n;
168
- });
169
- this._requestDraw();
170
- }
171
- drawScene(can, top) {
172
- var layer = this.getLayer(), canvas = can || (layer && layer.getCanvas()), context = canvas && canvas.getContext(), cachedCanvas = this._getCanvasCache(), cachedSceneCanvas = cachedCanvas && cachedCanvas.scene;
173
- var caching = canvas && canvas.isCache;
174
- if (!this.isVisible() && !caching) {
175
- return this;
176
- }
177
- if (cachedSceneCanvas) {
178
- context.save();
179
- var m = this.getAbsoluteTransform(top).getMatrix();
180
- context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
181
- this._drawCachedSceneCanvas(context);
182
- context.restore();
183
- }
184
- else {
185
- this._drawChildren('drawScene', canvas, top);
186
- }
187
- return this;
188
- }
189
- drawHit(can, top) {
190
- if (!this.shouldDrawHit(top)) {
191
- return this;
192
- }
193
- var layer = this.getLayer(), canvas = can || (layer && layer.hitCanvas), context = canvas && canvas.getContext(), cachedCanvas = this._getCanvasCache(), cachedHitCanvas = cachedCanvas && cachedCanvas.hit;
194
- if (cachedHitCanvas) {
195
- context.save();
196
- var m = this.getAbsoluteTransform(top).getMatrix();
197
- context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
198
- this._drawCachedHitCanvas(context);
199
- context.restore();
200
- }
201
- else {
202
- this._drawChildren('drawHit', canvas, top);
203
- }
204
- return this;
205
- }
206
- _drawChildren(drawMethod, canvas, top) {
207
- var _a;
208
- var context = canvas && canvas.getContext(), clipWidth = this.clipWidth(), clipHeight = this.clipHeight(), clipFunc = this.clipFunc(), hasClip = (clipWidth && clipHeight) || clipFunc;
209
- const selfCache = top === this;
210
- if (hasClip) {
211
- context.save();
212
- var transform = this.getAbsoluteTransform(top);
213
- var m = transform.getMatrix();
214
- context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
215
- context.beginPath();
216
- if (clipFunc) {
217
- clipFunc.call(this, context, this);
218
- }
219
- else {
220
- var clipX = this.clipX();
221
- var clipY = this.clipY();
222
- context.rect(clipX, clipY, clipWidth, clipHeight);
223
- }
224
- context.clip();
225
- m = transform.copy().invert().getMatrix();
226
- context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
227
- }
228
- var hasComposition = !selfCache &&
229
- this.globalCompositeOperation() !== 'source-over' &&
230
- drawMethod === 'drawScene';
231
- if (hasComposition) {
232
- context.save();
233
- context._applyGlobalCompositeOperation(this);
234
- }
235
- (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) {
236
- child[drawMethod](canvas, top);
237
- });
238
- if (hasComposition) {
239
- context.restore();
240
- }
241
- if (hasClip) {
242
- context.restore();
243
- }
244
- }
245
- getClientRect(config) {
246
- var _a;
247
- config = config || {};
248
- var skipTransform = config.skipTransform;
249
- var relativeTo = config.relativeTo;
250
- var minX, minY, maxX, maxY;
251
- var selfRect = {
252
- x: Infinity,
253
- y: Infinity,
254
- width: 0,
255
- height: 0,
256
- };
257
- var that = this;
258
- (_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) {
259
- if (!child.visible()) {
260
- return;
261
- }
262
- var rect = child.getClientRect({
263
- relativeTo: that,
264
- skipShadow: config.skipShadow,
265
- skipStroke: config.skipStroke,
266
- });
267
- if (rect.width === 0 && rect.height === 0) {
268
- return;
269
- }
270
- if (minX === undefined) {
271
- minX = rect.x;
272
- minY = rect.y;
273
- maxX = rect.x + rect.width;
274
- maxY = rect.y + rect.height;
275
- }
276
- else {
277
- minX = Math.min(minX, rect.x);
278
- minY = Math.min(minY, rect.y);
279
- maxX = Math.max(maxX, rect.x + rect.width);
280
- maxY = Math.max(maxY, rect.y + rect.height);
281
- }
282
- });
283
- var shapes = this.find('Shape');
284
- var hasVisible = false;
285
- for (var i = 0; i < shapes.length; i++) {
286
- var shape = shapes[i];
287
- if (shape._isVisible(this)) {
288
- hasVisible = true;
289
- break;
290
- }
291
- }
292
- if (hasVisible && minX !== undefined) {
293
- selfRect = {
294
- x: minX,
295
- y: minY,
296
- width: maxX - minX,
297
- height: maxY - minY,
298
- };
299
- }
300
- else {
301
- selfRect = {
302
- x: 0,
303
- y: 0,
304
- width: 0,
305
- height: 0,
306
- };
307
- }
308
- if (!skipTransform) {
309
- return this._transformedRect(selfRect, relativeTo);
310
- }
311
- return selfRect;
312
- }
313
- }
314
- exports.Container = Container;
315
- Factory_1.Factory.addComponentsGetterSetter(Container, 'clip', [
316
- 'x',
317
- 'y',
318
- 'width',
319
- 'height',
320
- ]);
321
- Factory_1.Factory.addGetterSetter(Container, 'clipX', undefined, (0, Validators_1.getNumberValidator)());
322
- Factory_1.Factory.addGetterSetter(Container, 'clipY', undefined, (0, Validators_1.getNumberValidator)());
323
- Factory_1.Factory.addGetterSetter(Container, 'clipWidth', undefined, (0, Validators_1.getNumberValidator)());
324
- Factory_1.Factory.addGetterSetter(Container, 'clipHeight', undefined, (0, Validators_1.getNumberValidator)());
325
- Factory_1.Factory.addGetterSetter(Container, 'clipFunc');