Container
new Konva.Container(config)
Container constructor. Containers are used to contain nodes or other containers
Parameters
| Name | Type | Description |
|---|---|---|
| config | Object | |
| x (optional) | Number | |
| y (optional) | Number | |
| width (optional) | Number | |
| height (optional) | Number | |
| visible (optional) | Boolean | |
| listening (optional) | Boolean | whether or not the node is listening for events |
| id (optional) | String | unique id |
| name (optional) | String | non-unique name |
| opacity (optional) | Number | determines node opacity. Can be any number between 0 and 1 |
| scale (optional) | Object | set scale |
| scaleX (optional) | Number | set scale x |
| scaleY (optional) | Number | set scale y |
| rotation (optional) | Number | rotation in degrees |
| offset (optional) | Object | offset from center point and rotation point |
| offsetX (optional) | Number | set offset x |
| offsetY (optional) | Number | set offset y |
| draggable (optional) | Boolean | makes the node draggable. When stages are draggable, you can drag and drop the entire stage by dragging any portion of the stage |
| dragDistance (optional) | Number | |
| dragBoundFunc (optional) | function | * @param {Object} [config.clip] set clip |
| clipX (optional) | Number | set clip x |
| clipY (optional) | Number | set clip y |
| clipWidth (optional) | Number | set clip width |
| clipHeight (optional) | Number | set clip height |
| clipFunc (optional) | function | set clip func |
Inherited from: Konva.Node
Own Methods
getChildren(filterFunc)
returns an array of direct descendant nodes
Parameters:
filterFunc(function) (optional): filter function
Returns: Array
Example:
// get all children
var children = layer.getChildren();
// get only circles
var circles = layer.getChildren(function(node){
return node.getClassName() === 'Circle';
});
hasChildren()
determine if node has children
Returns: Boolean
removeChildren()
remove all children. Children will be still in memory. If you want to completely destroy all children please use "destroyChildren" method instead
destroyChildren()
destroy all children nodes.
add(children)
add a child and children into container
Parameters:
children(Konva.Node)
Returns: Container
Example:
layer.add(rect);
layer.add(shape1, shape2, shape3);
// empty arrays are accepted, though each individual child must be defined
layer.add(...shapes);
find(selector)
return an array of nodes that match the selector. You can provide a string with '#' for id selections and '.' for name selections. Or a function that will return true/false when a node is passed through. See example below. With strings you can also select by type or class name. Pass multiple selectors separated by a comma.
Parameters:
selector(String|function)
Returns: Array
Example:
Passing a string as a selector
// select node with id foo
var node = stage.find('#foo');
// select nodes with name bar inside layer
var nodes = layer.find('.bar');
// select all groups inside layer
var nodes = layer.find('Group');
// select all rectangles inside layer
var nodes = layer.find('Rect');
// select node with an id of foo or a name of bar inside layer
var nodes = layer.find('#foo, .bar');
Passing a function as a selector
// get all groups with a function
var groups = stage.find(node => {
return node.getType() === 'Group';
});
// get only Nodes with partial opacity
var alphaNodes = layer.find(node => {
return node.getType() === 'Node' && node.getAbsoluteOpacity() < 1;
});
findOne(selector)
return a first node from find method
Parameters:
selector(String|function)
Returns: Konva.Node|Undefined
Example:
// select node with id foo
var node = stage.findOne('#foo');
// select node with name bar inside layer
var nodes = layer.findOne('.bar');
// select the first node to return true in a function
var node = stage.findOne(node => {
return node.getType() === 'Shape'
})
isAncestorOf(node)
determine if node is an ancestor of descendant
Parameters:
node(Konva.Node)
getAllIntersections(pos)
get all shapes that intersect a point. Note: because this method must clear a temporary canvas and redraw every shape inside the container, it should only be used for special situations because it performs very poorly. Please use the Konva.Stage#getIntersection method if at all possible because it performs much better nodes with listening set to false will not be detected
Parameters:
pos(Object)pos.x(Number)pos.y(Number)
Returns: Array array of shapes
clip(clip)
get/set clip
Parameters:
clip(Object)clip.x(Number)clip.y(Number)clip.width(Number)clip.height(Number)
Returns: Object
Example:
// get clip
var clip = container.clip();
// set clip
container.clip({
x: 20,
y: 20,
width: 20,
height: 20
});
clipX(x)
get/set clip x
Parameters:
x(Number)
Returns: Number
Example:
// get clip x
var clipX = container.clipX();
// set clip x
container.clipX(10);
clipY(y)
get/set clip y
Parameters:
y(Number)
Returns: Number
Example:
// get clip y
var clipY = container.clipY();
// set clip y
container.clipY(10);
clipWidth(width)
get/set clip width
Parameters:
width(Number)
Returns: Number
Example:
// get clip width
var clipWidth = container.clipWidth();
// set clip width
container.clipWidth(100);
clipHeight(height)
get/set clip height
Parameters:
height(Number)
Returns: Number
Example:
// get clip height
var clipHeight = container.clipHeight();
// set clip height
container.clipHeight(100);
clipFunc(function)
get/set clip function
Parameters:
function(function)
Returns: function
Example:
// get clip function
var clipFunction = container.clipFunc();
// set clip function
container.clipFunc(function(ctx) {
ctx.rect(0, 0, 100, 100);
});
container.clipFunc(function(ctx) {
// optionally return a clip Path2D and clip-rule or just the clip-rule
return [new Path2D('M0 0v50h50Z'), 'evenodd']
});
Inherited Methods
Konva.Container also has all methods of its parent classes. Each link below opens the full documentation of the method on the class that defines it.
From Konva.Node
- clearCache()
- cache(config)
- isCached()
- getClientRect(config)
- on(evtStr, handler)
- off(evtStr)
- remove()
- destroy()
- getAttr(attr)
- getAncestors()
- getAttrs()
- setAttrs(config)
- isListening()
- isVisible()
- show()
- hide()
- getAbsoluteZIndex()
- getDepth()
- getRelativePointerPosition()
- getAbsolutePosition(top)
- move(change)
- rotate(theta)
- moveToTop()
- moveUp()
- moveDown()
- moveToBottom()
- getAbsoluteOpacity()
- moveTo(newContainer)
- toObject()
- toJSON()
- getParent()
- findAncestors(selector, includeSelf, stopNode)
- findAncestor(selector, includeSelf, stopNode)
- getLayer()
- getStage()
- fire(eventType, evt, bubble)
- getAbsoluteTransform()
- getAbsoluteScale()
- getAbsoluteRotation()
- getTransform()
- clone(obj)
- toCanvas(config)
- toDataURL(config)
- toImage(config)
- toBlob(config)
- getClassName()
- getType()
- addName(name)
- hasName(name)
- removeName(name)
- setAttr(attr, val)
- draw()
- startDrag()
- stopDrag()
- isDragging()
- isClientRectOnScreen(margin)
- zIndex(index)
- absolutePosition(pos)
- position(pos)
- x(x)
- y(y)
- globalCompositeOperation(type)
- opacity(opacity)
- name(name)
- id(id)
- rotation(rotation)
- scale(scale)
- scaleX(x)
- scaleY(y)
- skew(skew)
- skewX(x)
- skewY(y)
- offsetX(x)
- offsetY(y)
- dragDistance(distance)
- width(width)
- height(height)
- listening(listening)
- preventDefault(preventDefault)
- filters(filters)
- visible(visible)
- transformsEnabled(enabled)
- size(size)
- dragBoundFunc(dragBoundFunc)
- draggable(draggable)
- to(params)
- blurRadius(radius)
- brightness(brightness)
- contrast(contrast)
- embossStrength(level)
- embossWhiteLevel(embossWhiteLevel)
- embossDirection(embossDirection)
- embossBlend(embossBlend)
- enhance(amount)
- hue(hue)
- saturation(saturation)
- luminance(value)
- value(value)
- kaleidoscopePower(power)
- kaleidoscopeAngle(degrees)
- noise(noise)
- pixelSize(pixelSize)
- levels(level)
- red(red)
- green(green)
- blue(blue)
- alpha(alpha)
- threshold(threshold)