konva 10.3.2 → 10.4.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.
- package/README.md +5 -1
- package/konva.js +1327 -1572
- package/konva.min.js +4 -4
- package/lib/Animation.d.ts +63 -3
- package/lib/Animation.js +86 -65
- package/lib/BezierFunctions.d.ts +10 -0
- package/lib/BezierFunctions.js +32 -0
- package/lib/Canvas.d.ts +53 -0
- package/lib/Canvas.js +93 -34
- package/lib/Container.d.ts +156 -9
- package/lib/Container.js +260 -25
- package/lib/Context.d.ts +247 -0
- package/lib/Context.js +312 -74
- package/lib/Core.js +1 -0
- package/lib/DragAndDrop.d.ts +1 -0
- package/lib/DragAndDrop.js +66 -17
- package/lib/Factory.d.ts +24 -1
- package/lib/Factory.js +14 -20
- package/lib/FastLayer.d.ts +14 -1
- package/lib/FastLayer.js +14 -1
- package/lib/Global.d.ts +114 -3
- package/lib/Global.js +134 -5
- package/lib/Group.d.ts +11 -0
- package/lib/Group.js +11 -0
- package/lib/Layer.d.ts +128 -3
- package/lib/Layer.js +260 -49
- package/lib/Node.d.ts +692 -30
- package/lib/Node.js +1534 -202
- package/lib/PointerEvents.d.ts +2 -1
- package/lib/PointerEvents.js +19 -1
- package/lib/Shape.d.ts +99 -14
- package/lib/Shape.js +1101 -67
- package/lib/Stage.d.ts +81 -1
- package/lib/Stage.js +166 -7
- package/lib/Tween.d.ts +159 -4
- package/lib/Tween.js +243 -24
- package/lib/Util.d.ts +147 -4
- package/lib/Util.js +332 -82
- package/lib/Validators.d.ts +0 -2
- package/lib/Validators.js +3 -25
- package/lib/_CoreInternals.d.ts +10 -6
- package/lib/_CoreInternals.js +1 -0
- package/lib/_FullInternals.d.ts +15 -6
- package/lib/_FullInternals.js +7 -0
- package/lib/canvas-backend.js +6 -0
- package/lib/filters/Blur.d.ts +18 -0
- package/lib/filters/Blur.js +97 -24
- package/lib/filters/Brighten.d.ts +21 -0
- package/lib/filters/Brighten.js +24 -0
- package/lib/filters/Brightness.d.ts +11 -0
- package/lib/filters/Brightness.js +16 -0
- package/lib/filters/Contrast.d.ts +10 -0
- package/lib/filters/Contrast.js +21 -0
- package/lib/filters/Emboss.d.ts +20 -0
- package/lib/filters/Emboss.js +68 -8
- package/lib/filters/Enhance.d.ts +14 -0
- package/lib/filters/Enhance.js +34 -0
- package/lib/filters/Grayscale.d.ts +9 -0
- package/lib/filters/Grayscale.js +12 -0
- package/lib/filters/HSL.d.ts +17 -0
- package/lib/filters/HSL.js +48 -5
- package/lib/filters/HSV.d.ts +18 -0
- package/lib/filters/HSV.js +49 -5
- package/lib/filters/Invert.d.ts +9 -0
- package/lib/filters/Invert.js +12 -0
- package/lib/filters/Kaleidoscope.d.ts +12 -0
- package/lib/filters/Kaleidoscope.js +83 -0
- package/lib/filters/Mask.d.ts +11 -0
- package/lib/filters/Mask.js +18 -0
- package/lib/filters/Noise.d.ts +19 -0
- package/lib/filters/Noise.js +19 -0
- package/lib/filters/Pixelate.d.ts +20 -0
- package/lib/filters/Pixelate.js +28 -1
- package/lib/filters/Posterize.d.ts +21 -0
- package/lib/filters/Posterize.js +22 -0
- package/lib/filters/RGB.d.ts +21 -0
- package/lib/filters/RGB.js +43 -28
- package/lib/filters/RGBA.d.ts +23 -0
- package/lib/filters/RGBA.js +28 -41
- package/lib/filters/Sepia.d.ts +9 -0
- package/lib/filters/Sepia.js +10 -0
- package/lib/filters/Solarize.d.ts +10 -0
- package/lib/filters/Solarize.js +11 -0
- package/lib/filters/Threshold.d.ts +22 -0
- package/lib/filters/Threshold.js +22 -0
- package/lib/shapes/Arc.d.ts +40 -0
- package/lib/shapes/Arc.js +89 -6
- package/lib/shapes/Arrow.d.ts +24 -1
- package/lib/shapes/Arrow.js +83 -0
- package/lib/shapes/Circle.d.ts +18 -0
- package/lib/shapes/Circle.js +36 -9
- package/lib/shapes/Ellipse.d.ts +34 -2
- package/lib/shapes/Ellipse.js +67 -9
- package/lib/shapes/Image.d.ts +49 -0
- package/lib/shapes/Image.js +151 -22
- package/lib/shapes/Label.d.ts +65 -1
- package/lib/shapes/Label.js +113 -20
- package/lib/shapes/Line.d.ts +50 -8
- package/lib/shapes/Line.js +116 -20
- package/lib/shapes/Path.d.ts +40 -6
- package/lib/shapes/Path.js +187 -128
- package/lib/shapes/Rect.d.ts +18 -0
- package/lib/shapes/Rect.js +36 -0
- package/lib/shapes/RegularPolygon.d.ts +22 -0
- package/lib/shapes/RegularPolygon.js +70 -0
- package/lib/shapes/Ring.d.ts +19 -0
- package/lib/shapes/Ring.js +51 -5
- package/lib/shapes/Sprite.d.ts +78 -0
- package/lib/shapes/Sprite.js +238 -25
- package/lib/shapes/Star.d.ts +23 -0
- package/lib/shapes/Star.js +62 -0
- package/lib/shapes/Text.d.ts +59 -1
- package/lib/shapes/Text.js +481 -97
- package/lib/shapes/TextPath.d.ts +49 -3
- package/lib/shapes/TextPath.js +278 -34
- package/lib/shapes/Transformer.d.ts +108 -11
- package/lib/shapes/Transformer.js +744 -102
- package/lib/shapes/Wedge.d.ts +22 -0
- package/lib/shapes/Wedge.js +67 -3
- package/lib/skia-backend.js +9 -0
- package/lib/types.d.ts +2 -23
- package/lib/types.js +1 -21
- package/package.json +24 -21
package/lib/Container.d.ts
CHANGED
|
@@ -1,28 +1,140 @@
|
|
|
1
1
|
import type { HitCanvas, SceneCanvas } from './Canvas.ts';
|
|
2
2
|
import type { SceneContext } from './Context.ts';
|
|
3
|
-
import type { NodeConfig } from './Node.ts';
|
|
3
|
+
import type { GetClientRectConfig, NodeConfig } from './Node.ts';
|
|
4
4
|
import { Node } from './Node.ts';
|
|
5
5
|
import type { Shape } from './Shape.ts';
|
|
6
6
|
import type { GetSet, IRect } from './types.ts';
|
|
7
7
|
export type ClipFuncOutput = void | [Path2D | CanvasFillRule] | [Path2D, CanvasFillRule];
|
|
8
8
|
export interface ContainerConfig extends NodeConfig {
|
|
9
9
|
clearBeforeDraw?: boolean;
|
|
10
|
-
clipFunc?: (ctx: SceneContext) => ClipFuncOutput;
|
|
10
|
+
clipFunc?: (ctx: SceneContext, container: Container) => ClipFuncOutput;
|
|
11
11
|
clipX?: number;
|
|
12
12
|
clipY?: number;
|
|
13
13
|
clipWidth?: number;
|
|
14
14
|
clipHeight?: number;
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Container constructor. Containers are used to contain nodes or other containers
|
|
18
|
+
* @constructor
|
|
19
|
+
* @memberof Konva
|
|
20
|
+
* @augments Konva.Node
|
|
21
|
+
* @abstract
|
|
22
|
+
* @param {Object} config
|
|
23
|
+
* @@nodeParams
|
|
24
|
+
* @@containerParams
|
|
25
|
+
*/
|
|
16
26
|
export declare abstract class Container<ChildType extends Node = Node, Config extends ContainerConfig = ContainerConfig> extends Node<Config> {
|
|
17
27
|
children: Array<ChildType>;
|
|
28
|
+
/**
|
|
29
|
+
* returns an array of direct descendant nodes
|
|
30
|
+
* @method
|
|
31
|
+
* @name Konva.Container#getChildren
|
|
32
|
+
* @param {Function} [filterFunc] filter function
|
|
33
|
+
* @returns {Array}
|
|
34
|
+
* @example
|
|
35
|
+
* // get all children
|
|
36
|
+
* var children = layer.getChildren();
|
|
37
|
+
*
|
|
38
|
+
* // get only circles
|
|
39
|
+
* var circles = layer.getChildren(function(node){
|
|
40
|
+
* return node.getClassName() === 'Circle';
|
|
41
|
+
* });
|
|
42
|
+
*/
|
|
18
43
|
getChildren(filterFunc?: (item: Node) => boolean): ChildType[];
|
|
44
|
+
/**
|
|
45
|
+
* determine if node has children
|
|
46
|
+
* @method
|
|
47
|
+
* @name Konva.Container#hasChildren
|
|
48
|
+
* @returns {Boolean}
|
|
49
|
+
*/
|
|
19
50
|
hasChildren(): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* remove all children. Children will be still in memory.
|
|
53
|
+
* If you want to completely destroy all children please use "destroyChildren" method instead
|
|
54
|
+
* @method
|
|
55
|
+
* @name Konva.Container#removeChildren
|
|
56
|
+
*/
|
|
20
57
|
removeChildren(): this;
|
|
58
|
+
/**
|
|
59
|
+
* destroy all children nodes.
|
|
60
|
+
* @method
|
|
61
|
+
* @name Konva.Container#destroyChildren
|
|
62
|
+
*/
|
|
21
63
|
destroyChildren(): this;
|
|
22
64
|
abstract _validateAdd(node: Node): void;
|
|
65
|
+
/**
|
|
66
|
+
* add a child and children into container
|
|
67
|
+
* @name Konva.Container#add
|
|
68
|
+
* @method
|
|
69
|
+
* @param {...Konva.Node} children
|
|
70
|
+
* @returns {Container}
|
|
71
|
+
* @example
|
|
72
|
+
* layer.add(rect);
|
|
73
|
+
* layer.add(shape1, shape2, shape3);
|
|
74
|
+
* // empty arrays are accepted, though each individual child must be defined
|
|
75
|
+
* layer.add(...shapes);
|
|
76
|
+
*/
|
|
23
77
|
add(...children: ChildType[]): this;
|
|
24
78
|
destroy(): this;
|
|
79
|
+
/**
|
|
80
|
+
* return an array of nodes that match the selector.
|
|
81
|
+
* You can provide a string with '#' for id selections and '.' for name selections.
|
|
82
|
+
* Or a function that will return true/false when a node is passed through. See example below.
|
|
83
|
+
* With strings you can also select by type or class name. Pass multiple selectors
|
|
84
|
+
* separated by a comma.
|
|
85
|
+
* @method
|
|
86
|
+
* @name Konva.Container#find
|
|
87
|
+
* @param {String | Function} selector
|
|
88
|
+
* @returns {Array}
|
|
89
|
+
* @example
|
|
90
|
+
*
|
|
91
|
+
* Passing a string as a selector
|
|
92
|
+
* // select node with id foo
|
|
93
|
+
* var node = stage.find('#foo');
|
|
94
|
+
*
|
|
95
|
+
* // select nodes with name bar inside layer
|
|
96
|
+
* var nodes = layer.find('.bar');
|
|
97
|
+
*
|
|
98
|
+
* // select all groups inside layer
|
|
99
|
+
* var nodes = layer.find('Group');
|
|
100
|
+
*
|
|
101
|
+
* // select all rectangles inside layer
|
|
102
|
+
* var nodes = layer.find('Rect');
|
|
103
|
+
*
|
|
104
|
+
* // select node with an id of foo or a name of bar inside layer
|
|
105
|
+
* var nodes = layer.find('#foo, .bar');
|
|
106
|
+
*
|
|
107
|
+
* Passing a function as a selector
|
|
108
|
+
*
|
|
109
|
+
* // get all groups with a function
|
|
110
|
+
* var groups = stage.find(node => {
|
|
111
|
+
* return node.getType() === 'Group';
|
|
112
|
+
* });
|
|
113
|
+
*
|
|
114
|
+
* // get only Nodes with partial opacity
|
|
115
|
+
* var alphaNodes = layer.find(node => {
|
|
116
|
+
* return node.getType() === 'Node' && node.getAbsoluteOpacity() < 1;
|
|
117
|
+
* });
|
|
118
|
+
*/
|
|
25
119
|
find<ChildNode extends Node>(selector: any): Array<ChildNode>;
|
|
120
|
+
/**
|
|
121
|
+
* return a first node from `find` method
|
|
122
|
+
* @method
|
|
123
|
+
* @name Konva.Container#findOne
|
|
124
|
+
* @param {String | Function} selector
|
|
125
|
+
* @returns {Konva.Node | Undefined}
|
|
126
|
+
* @example
|
|
127
|
+
* // select node with id foo
|
|
128
|
+
* var node = stage.findOne('#foo');
|
|
129
|
+
*
|
|
130
|
+
* // select node with name bar inside layer
|
|
131
|
+
* var nodes = layer.findOne('.bar');
|
|
132
|
+
*
|
|
133
|
+
* // select the first node to return true in a function
|
|
134
|
+
* var node = stage.findOne(node => {
|
|
135
|
+
* return node.getType() === 'Shape'
|
|
136
|
+
* })
|
|
137
|
+
*/
|
|
26
138
|
findOne<ChildNode extends Node = Node>(selector: string | Function): ChildNode | undefined;
|
|
27
139
|
_generalFind<ChildNode extends Node>(selector: string | Function, findOne: boolean): ChildNode[];
|
|
28
140
|
private _descendants;
|
|
@@ -31,24 +143,59 @@ export declare abstract class Container<ChildType extends Node = Node, Config ex
|
|
|
31
143
|
className: string;
|
|
32
144
|
children?: Array<any>;
|
|
33
145
|
};
|
|
146
|
+
/**
|
|
147
|
+
* determine if node is an ancestor
|
|
148
|
+
* of descendant
|
|
149
|
+
* @method
|
|
150
|
+
* @name Konva.Container#isAncestorOf
|
|
151
|
+
* @param {Konva.Node} node
|
|
152
|
+
*/
|
|
34
153
|
isAncestorOf(node: Node): boolean;
|
|
35
154
|
clone(obj?: any): this;
|
|
155
|
+
/**
|
|
156
|
+
* get all shapes that intersect a point. Note: because this method must clear a temporary
|
|
157
|
+
* canvas and redraw every shape inside the container, it should only be used for special situations
|
|
158
|
+
* because it performs very poorly. Please use the {@link Konva.Stage#getIntersection} method if at all possible
|
|
159
|
+
* because it performs much better.
|
|
160
|
+
* Nodes with listening set to false or invisible nodes are not detected
|
|
161
|
+
* @method
|
|
162
|
+
* @name Konva.Container#getAllIntersections
|
|
163
|
+
* @param {Object} pos
|
|
164
|
+
* @param {Number} pos.x
|
|
165
|
+
* @param {Number} pos.y
|
|
166
|
+
* @returns {Array} array of shapes
|
|
167
|
+
*/
|
|
36
168
|
getAllIntersections(pos: any): Shape<import("./Shape.ts").ShapeConfig>[];
|
|
37
169
|
_clearSelfAndDescendantCache(attr?: string): void;
|
|
38
170
|
_setChildrenIndices(): void;
|
|
39
171
|
drawScene(can?: SceneCanvas, top?: Node, bufferCanvas?: SceneCanvas): this;
|
|
40
172
|
drawHit(can?: HitCanvas, top?: Node): this;
|
|
41
173
|
_drawChildren(drawMethod: any, canvas: any, top: any, bufferCanvas?: any): void;
|
|
42
|
-
getClientRect(config?:
|
|
43
|
-
skipTransform?: boolean;
|
|
44
|
-
skipShadow?: boolean;
|
|
45
|
-
skipStroke?: boolean;
|
|
46
|
-
relativeTo?: Container<Node>;
|
|
47
|
-
}): IRect;
|
|
174
|
+
getClientRect(config?: GetClientRectConfig): IRect;
|
|
48
175
|
clip: GetSet<IRect, this>;
|
|
49
176
|
clipX: GetSet<number, this>;
|
|
50
177
|
clipY: GetSet<number, this>;
|
|
51
178
|
clipWidth: GetSet<number, this>;
|
|
52
179
|
clipHeight: GetSet<number, this>;
|
|
53
|
-
clipFunc: GetSet<(ctx:
|
|
180
|
+
clipFunc: GetSet<(ctx: SceneContext, container: Container) => ClipFuncOutput, this>;
|
|
54
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* get/set clip function
|
|
184
|
+
* @name Konva.Container#clipFunc
|
|
185
|
+
* @method
|
|
186
|
+
* @param {Function} function
|
|
187
|
+
* @returns {Function}
|
|
188
|
+
* @example
|
|
189
|
+
* // get clip function
|
|
190
|
+
* var clipFunction = container.clipFunc();
|
|
191
|
+
*
|
|
192
|
+
* // set clip function
|
|
193
|
+
* container.clipFunc(function(ctx) {
|
|
194
|
+
* ctx.rect(0, 0, 100, 100);
|
|
195
|
+
* });
|
|
196
|
+
*
|
|
197
|
+
* container.clipFunc(function(ctx) {
|
|
198
|
+
* // optionally return a clip Path2D and clip-rule or just the clip-rule
|
|
199
|
+
* return [new Path2D('M0 0v50h50Z'), 'evenodd']
|
|
200
|
+
* });
|
|
201
|
+
*/
|
package/lib/Container.js
CHANGED
|
@@ -1,41 +1,97 @@
|
|
|
1
1
|
import { Factory } from "./Factory.js";
|
|
2
2
|
import { Node } from "./Node.js";
|
|
3
3
|
import { getNumberValidator } from "./Validators.js";
|
|
4
|
+
/**
|
|
5
|
+
* Container constructor. Containers are used to contain nodes or other containers
|
|
6
|
+
* @constructor
|
|
7
|
+
* @memberof Konva
|
|
8
|
+
* @augments Konva.Node
|
|
9
|
+
* @abstract
|
|
10
|
+
* @param {Object} config
|
|
11
|
+
* @@nodeParams
|
|
12
|
+
* @@containerParams
|
|
13
|
+
*/
|
|
4
14
|
export class Container extends Node {
|
|
5
15
|
constructor() {
|
|
6
16
|
super(...arguments);
|
|
7
17
|
this.children = [];
|
|
8
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* returns an array of direct descendant nodes
|
|
21
|
+
* @method
|
|
22
|
+
* @name Konva.Container#getChildren
|
|
23
|
+
* @param {Function} [filterFunc] filter function
|
|
24
|
+
* @returns {Array}
|
|
25
|
+
* @example
|
|
26
|
+
* // get all children
|
|
27
|
+
* var children = layer.getChildren();
|
|
28
|
+
*
|
|
29
|
+
* // get only circles
|
|
30
|
+
* var circles = layer.getChildren(function(node){
|
|
31
|
+
* return node.getClassName() === 'Circle';
|
|
32
|
+
* });
|
|
33
|
+
*/
|
|
9
34
|
getChildren(filterFunc) {
|
|
35
|
+
// a copy, so that removing children while iterating over it is safe
|
|
10
36
|
const children = this.children || [];
|
|
11
|
-
|
|
12
|
-
return children.filter(filterFunc);
|
|
13
|
-
}
|
|
14
|
-
return children;
|
|
37
|
+
return filterFunc ? children.filter(filterFunc) : children.slice();
|
|
15
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* determine if node has children
|
|
41
|
+
* @method
|
|
42
|
+
* @name Konva.Container#hasChildren
|
|
43
|
+
* @returns {Boolean}
|
|
44
|
+
*/
|
|
16
45
|
hasChildren() {
|
|
17
|
-
return this.
|
|
46
|
+
return this.children.length > 0;
|
|
18
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* remove all children. Children will be still in memory.
|
|
50
|
+
* If you want to completely destroy all children please use "destroyChildren" method instead
|
|
51
|
+
* @method
|
|
52
|
+
* @name Konva.Container#removeChildren
|
|
53
|
+
*/
|
|
19
54
|
removeChildren() {
|
|
20
|
-
this.
|
|
55
|
+
this.children.forEach((child) => {
|
|
56
|
+
// reset parent to prevent many _setChildrenIndices calls
|
|
21
57
|
child.parent = null;
|
|
22
58
|
child.index = 0;
|
|
23
59
|
child.remove();
|
|
24
60
|
});
|
|
25
61
|
this.children = [];
|
|
62
|
+
// because all children were detached from parent, request draw via container
|
|
26
63
|
this._requestDraw();
|
|
27
64
|
return this;
|
|
28
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* destroy all children nodes.
|
|
68
|
+
* @method
|
|
69
|
+
* @name Konva.Container#destroyChildren
|
|
70
|
+
*/
|
|
29
71
|
destroyChildren() {
|
|
30
|
-
this.
|
|
72
|
+
this.children.forEach((child) => {
|
|
73
|
+
// reset parent to prevent many _setChildrenIndices calls
|
|
31
74
|
child.parent = null;
|
|
32
75
|
child.index = 0;
|
|
33
76
|
child.destroy();
|
|
34
77
|
});
|
|
35
78
|
this.children = [];
|
|
79
|
+
// because all children were detached from parent, request draw via container
|
|
36
80
|
this._requestDraw();
|
|
37
81
|
return this;
|
|
38
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* add a child and children into container
|
|
85
|
+
* @name Konva.Container#add
|
|
86
|
+
* @method
|
|
87
|
+
* @param {...Konva.Node} children
|
|
88
|
+
* @returns {Container}
|
|
89
|
+
* @example
|
|
90
|
+
* layer.add(rect);
|
|
91
|
+
* layer.add(shape1, shape2, shape3);
|
|
92
|
+
* // empty arrays are accepted, though each individual child must be defined
|
|
93
|
+
* layer.add(...shapes);
|
|
94
|
+
*/
|
|
39
95
|
add(...children) {
|
|
40
96
|
if (children.length === 0) {
|
|
41
97
|
return this;
|
|
@@ -52,14 +108,15 @@ export class Container extends Node {
|
|
|
52
108
|
return this;
|
|
53
109
|
}
|
|
54
110
|
this._validateAdd(child);
|
|
55
|
-
child.index = this.
|
|
111
|
+
child.index = this.children.length;
|
|
56
112
|
child.parent = this;
|
|
57
113
|
child._clearCaches();
|
|
58
|
-
this.
|
|
114
|
+
this.children.push(child);
|
|
59
115
|
this._fire('add', {
|
|
60
116
|
child: child,
|
|
61
117
|
});
|
|
62
118
|
this._requestDraw();
|
|
119
|
+
// chainable
|
|
63
120
|
return this;
|
|
64
121
|
}
|
|
65
122
|
destroy() {
|
|
@@ -69,9 +126,69 @@ export class Container extends Node {
|
|
|
69
126
|
super.destroy();
|
|
70
127
|
return this;
|
|
71
128
|
}
|
|
129
|
+
/**
|
|
130
|
+
* return an array of nodes that match the selector.
|
|
131
|
+
* You can provide a string with '#' for id selections and '.' for name selections.
|
|
132
|
+
* Or a function that will return true/false when a node is passed through. See example below.
|
|
133
|
+
* With strings you can also select by type or class name. Pass multiple selectors
|
|
134
|
+
* separated by a comma.
|
|
135
|
+
* @method
|
|
136
|
+
* @name Konva.Container#find
|
|
137
|
+
* @param {String | Function} selector
|
|
138
|
+
* @returns {Array}
|
|
139
|
+
* @example
|
|
140
|
+
*
|
|
141
|
+
* Passing a string as a selector
|
|
142
|
+
* // select node with id foo
|
|
143
|
+
* var node = stage.find('#foo');
|
|
144
|
+
*
|
|
145
|
+
* // select nodes with name bar inside layer
|
|
146
|
+
* var nodes = layer.find('.bar');
|
|
147
|
+
*
|
|
148
|
+
* // select all groups inside layer
|
|
149
|
+
* var nodes = layer.find('Group');
|
|
150
|
+
*
|
|
151
|
+
* // select all rectangles inside layer
|
|
152
|
+
* var nodes = layer.find('Rect');
|
|
153
|
+
*
|
|
154
|
+
* // select node with an id of foo or a name of bar inside layer
|
|
155
|
+
* var nodes = layer.find('#foo, .bar');
|
|
156
|
+
*
|
|
157
|
+
* Passing a function as a selector
|
|
158
|
+
*
|
|
159
|
+
* // get all groups with a function
|
|
160
|
+
* var groups = stage.find(node => {
|
|
161
|
+
* return node.getType() === 'Group';
|
|
162
|
+
* });
|
|
163
|
+
*
|
|
164
|
+
* // get only Nodes with partial opacity
|
|
165
|
+
* var alphaNodes = layer.find(node => {
|
|
166
|
+
* return node.getType() === 'Node' && node.getAbsoluteOpacity() < 1;
|
|
167
|
+
* });
|
|
168
|
+
*/
|
|
72
169
|
find(selector) {
|
|
170
|
+
// protecting _generalFind to prevent user from accidentally adding
|
|
171
|
+
// second argument and getting unexpected `findOne` result
|
|
73
172
|
return this._generalFind(selector, false);
|
|
74
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* return a first node from `find` method
|
|
176
|
+
* @method
|
|
177
|
+
* @name Konva.Container#findOne
|
|
178
|
+
* @param {String | Function} selector
|
|
179
|
+
* @returns {Konva.Node | Undefined}
|
|
180
|
+
* @example
|
|
181
|
+
* // select node with id foo
|
|
182
|
+
* var node = stage.findOne('#foo');
|
|
183
|
+
*
|
|
184
|
+
* // select node with name bar inside layer
|
|
185
|
+
* var nodes = layer.findOne('.bar');
|
|
186
|
+
*
|
|
187
|
+
* // select the first node to return true in a function
|
|
188
|
+
* var node = stage.findOne(node => {
|
|
189
|
+
* return node.getType() === 'Shape'
|
|
190
|
+
* })
|
|
191
|
+
*/
|
|
75
192
|
findOne(selector) {
|
|
76
193
|
const result = this._generalFind(selector, true);
|
|
77
194
|
return result.length > 0 ? result[0] : undefined;
|
|
@@ -92,8 +209,7 @@ export class Container extends Node {
|
|
|
92
209
|
}
|
|
93
210
|
_descendants(fn) {
|
|
94
211
|
let shouldStop = false;
|
|
95
|
-
const
|
|
96
|
-
for (const child of children) {
|
|
212
|
+
for (const child of this.children) {
|
|
97
213
|
shouldStop = fn(child);
|
|
98
214
|
if (shouldStop) {
|
|
99
215
|
return true;
|
|
@@ -108,14 +224,22 @@ export class Container extends Node {
|
|
|
108
224
|
}
|
|
109
225
|
return false;
|
|
110
226
|
}
|
|
227
|
+
// extenders
|
|
111
228
|
toObject() {
|
|
112
229
|
const obj = Node.prototype.toObject.call(this);
|
|
113
230
|
obj.children = [];
|
|
114
|
-
this.
|
|
231
|
+
this.children.forEach((child) => {
|
|
115
232
|
obj.children.push(child.toObject());
|
|
116
233
|
});
|
|
117
234
|
return obj;
|
|
118
235
|
}
|
|
236
|
+
/**
|
|
237
|
+
* determine if node is an ancestor
|
|
238
|
+
* of descendant
|
|
239
|
+
* @method
|
|
240
|
+
* @name Konva.Container#isAncestorOf
|
|
241
|
+
* @param {Konva.Node} node
|
|
242
|
+
*/
|
|
119
243
|
isAncestorOf(node) {
|
|
120
244
|
let parent = node.getParent();
|
|
121
245
|
while (parent) {
|
|
@@ -127,12 +251,26 @@ export class Container extends Node {
|
|
|
127
251
|
return false;
|
|
128
252
|
}
|
|
129
253
|
clone(obj) {
|
|
254
|
+
// call super method
|
|
130
255
|
const node = Node.prototype.clone.call(this, obj);
|
|
131
|
-
this.
|
|
256
|
+
this.children.forEach(function (no) {
|
|
132
257
|
node.add(no.clone());
|
|
133
258
|
});
|
|
134
259
|
return node;
|
|
135
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* get all shapes that intersect a point. Note: because this method must clear a temporary
|
|
263
|
+
* canvas and redraw every shape inside the container, it should only be used for special situations
|
|
264
|
+
* because it performs very poorly. Please use the {@link Konva.Stage#getIntersection} method if at all possible
|
|
265
|
+
* because it performs much better.
|
|
266
|
+
* Nodes with listening set to false or invisible nodes are not detected
|
|
267
|
+
* @method
|
|
268
|
+
* @name Konva.Container#getAllIntersections
|
|
269
|
+
* @param {Object} pos
|
|
270
|
+
* @param {Number} pos.x
|
|
271
|
+
* @param {Number} pos.y
|
|
272
|
+
* @returns {Array} array of shapes
|
|
273
|
+
*/
|
|
136
274
|
getAllIntersections(pos) {
|
|
137
275
|
const arr = [];
|
|
138
276
|
this.find('Shape').forEach((shape) => {
|
|
@@ -144,12 +282,18 @@ export class Container extends Node {
|
|
|
144
282
|
}
|
|
145
283
|
_clearSelfAndDescendantCache(attr) {
|
|
146
284
|
var _a;
|
|
285
|
+
// Perf: bracket absoluteTransform cascades with a depth counter so
|
|
286
|
+
// Node._runAfterAbsTransformCascade can defer work until the outermost
|
|
287
|
+
// walk returns. try/finally so depth/flush survive listener exceptions.
|
|
147
288
|
const isAbsTransform = attr === 'absoluteTransform';
|
|
148
289
|
if (isAbsTransform)
|
|
149
290
|
Node._absTransformCascadeDepth++;
|
|
150
291
|
try {
|
|
151
292
|
super._clearSelfAndDescendantCache(attr);
|
|
152
|
-
|
|
293
|
+
// a cached container is drawn from its bitmap, so its descendants keep
|
|
294
|
+
// their transform caches while it moves (perf); Node.getAbsoluteTransform
|
|
295
|
+
// skips those caches under a cached ancestor
|
|
296
|
+
if (isAbsTransform && this.isCached())
|
|
153
297
|
return;
|
|
154
298
|
(_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (node) {
|
|
155
299
|
node._clearSelfAndDescendantCache(attr);
|
|
@@ -262,6 +406,7 @@ export class Container extends Node {
|
|
|
262
406
|
};
|
|
263
407
|
const that = this;
|
|
264
408
|
(_a = this.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) {
|
|
409
|
+
// skip invisible children
|
|
265
410
|
if (!child.visible()) {
|
|
266
411
|
return;
|
|
267
412
|
}
|
|
@@ -270,10 +415,14 @@ export class Container extends Node {
|
|
|
270
415
|
skipShadow: config.skipShadow,
|
|
271
416
|
skipStroke: config.skipStroke,
|
|
272
417
|
});
|
|
273
|
-
|
|
418
|
+
// skip invisible children (like empty groups) and a child whose rect
|
|
419
|
+
// would poison the whole box
|
|
420
|
+
if ((rect.width === 0 && rect.height === 0) ||
|
|
421
|
+
!isFinite(rect.x + rect.y + rect.width + rect.height)) {
|
|
274
422
|
return;
|
|
275
423
|
}
|
|
276
424
|
if (minX === undefined) {
|
|
425
|
+
// initial value for first child
|
|
277
426
|
minX = rect.x;
|
|
278
427
|
minY = rect.y;
|
|
279
428
|
maxX = rect.x + rect.width;
|
|
@@ -286,16 +435,7 @@ export class Container extends Node {
|
|
|
286
435
|
maxY = Math.max(maxY, rect.y + rect.height);
|
|
287
436
|
}
|
|
288
437
|
});
|
|
289
|
-
|
|
290
|
-
let hasVisible = false;
|
|
291
|
-
for (let i = 0; i < shapes.length; i++) {
|
|
292
|
-
const shape = shapes[i];
|
|
293
|
-
if (shape._isVisible(this)) {
|
|
294
|
-
hasVisible = true;
|
|
295
|
-
break;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
if (hasVisible && minX !== undefined) {
|
|
438
|
+
if (minX !== undefined) {
|
|
299
439
|
selfRect = {
|
|
300
440
|
x: minX,
|
|
301
441
|
y: minY,
|
|
@@ -317,14 +457,109 @@ export class Container extends Node {
|
|
|
317
457
|
return selfRect;
|
|
318
458
|
}
|
|
319
459
|
}
|
|
460
|
+
// add getters setters
|
|
320
461
|
Factory.addComponentsGetterSetter(Container, 'clip', [
|
|
321
462
|
'x',
|
|
322
463
|
'y',
|
|
323
464
|
'width',
|
|
324
465
|
'height',
|
|
325
466
|
]);
|
|
467
|
+
/**
|
|
468
|
+
* get/set clip
|
|
469
|
+
* @method
|
|
470
|
+
* @name Konva.Container#clip
|
|
471
|
+
* @param {Object} clip
|
|
472
|
+
* @param {Number} clip.x
|
|
473
|
+
* @param {Number} clip.y
|
|
474
|
+
* @param {Number} clip.width
|
|
475
|
+
* @param {Number} clip.height
|
|
476
|
+
* @returns {Object}
|
|
477
|
+
* @example
|
|
478
|
+
* // get clip
|
|
479
|
+
* var clip = container.clip();
|
|
480
|
+
*
|
|
481
|
+
* // set clip
|
|
482
|
+
* container.clip({
|
|
483
|
+
* x: 20,
|
|
484
|
+
* y: 20,
|
|
485
|
+
* width: 20,
|
|
486
|
+
* height: 20
|
|
487
|
+
* });
|
|
488
|
+
*/
|
|
326
489
|
Factory.addGetterSetter(Container, 'clipX', undefined, getNumberValidator());
|
|
490
|
+
/**
|
|
491
|
+
* get/set clip x
|
|
492
|
+
* @name Konva.Container#clipX
|
|
493
|
+
* @method
|
|
494
|
+
* @param {Number} x
|
|
495
|
+
* @returns {Number}
|
|
496
|
+
* @example
|
|
497
|
+
* // get clip x
|
|
498
|
+
* var clipX = container.clipX();
|
|
499
|
+
*
|
|
500
|
+
* // set clip x
|
|
501
|
+
* container.clipX(10);
|
|
502
|
+
*/
|
|
327
503
|
Factory.addGetterSetter(Container, 'clipY', undefined, getNumberValidator());
|
|
504
|
+
/**
|
|
505
|
+
* get/set clip y
|
|
506
|
+
* @name Konva.Container#clipY
|
|
507
|
+
* @method
|
|
508
|
+
* @param {Number} y
|
|
509
|
+
* @returns {Number}
|
|
510
|
+
* @example
|
|
511
|
+
* // get clip y
|
|
512
|
+
* var clipY = container.clipY();
|
|
513
|
+
*
|
|
514
|
+
* // set clip y
|
|
515
|
+
* container.clipY(10);
|
|
516
|
+
*/
|
|
328
517
|
Factory.addGetterSetter(Container, 'clipWidth', undefined, getNumberValidator());
|
|
518
|
+
/**
|
|
519
|
+
* get/set clip width
|
|
520
|
+
* @name Konva.Container#clipWidth
|
|
521
|
+
* @method
|
|
522
|
+
* @param {Number} width
|
|
523
|
+
* @returns {Number}
|
|
524
|
+
* @example
|
|
525
|
+
* // get clip width
|
|
526
|
+
* var clipWidth = container.clipWidth();
|
|
527
|
+
*
|
|
528
|
+
* // set clip width
|
|
529
|
+
* container.clipWidth(100);
|
|
530
|
+
*/
|
|
329
531
|
Factory.addGetterSetter(Container, 'clipHeight', undefined, getNumberValidator());
|
|
532
|
+
/**
|
|
533
|
+
* get/set clip height
|
|
534
|
+
* @name Konva.Container#clipHeight
|
|
535
|
+
* @method
|
|
536
|
+
* @param {Number} height
|
|
537
|
+
* @returns {Number}
|
|
538
|
+
* @example
|
|
539
|
+
* // get clip height
|
|
540
|
+
* var clipHeight = container.clipHeight();
|
|
541
|
+
*
|
|
542
|
+
* // set clip height
|
|
543
|
+
* container.clipHeight(100);
|
|
544
|
+
*/
|
|
330
545
|
Factory.addGetterSetter(Container, 'clipFunc');
|
|
546
|
+
/**
|
|
547
|
+
* get/set clip function
|
|
548
|
+
* @name Konva.Container#clipFunc
|
|
549
|
+
* @method
|
|
550
|
+
* @param {Function} function
|
|
551
|
+
* @returns {Function}
|
|
552
|
+
* @example
|
|
553
|
+
* // get clip function
|
|
554
|
+
* var clipFunction = container.clipFunc();
|
|
555
|
+
*
|
|
556
|
+
* // set clip function
|
|
557
|
+
* container.clipFunc(function(ctx) {
|
|
558
|
+
* ctx.rect(0, 0, 100, 100);
|
|
559
|
+
* });
|
|
560
|
+
*
|
|
561
|
+
* container.clipFunc(function(ctx) {
|
|
562
|
+
* // optionally return a clip Path2D and clip-rule or just the clip-rule
|
|
563
|
+
* return [new Path2D('M0 0v50h50Z'), 'evenodd']
|
|
564
|
+
* });
|
|
565
|
+
*/
|