Group
new Konva.Group(config)
Group constructor. Groups are used to contain shapes or other groups.
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.Container
Inherited Methods
getChildren(filterFunc)
returns an array of direct descendant nodes
Parameters:
filterFunc
(function) (optional): filter function
Returns: Array
Inherited from: Konva.Container#getChildren
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
Inherited from: Konva.Container#hasChildren
removeChildren()
remove all children. Children will be still in memory. If you want to completely destroy all children please use "destroyChildren" method instead
Inherited from: Konva.Container#removeChildren
destroyChildren()
destroy all children nodes.
Inherited from: Konva.Container#destroyChildren
add(children)
add a child and children into container
Parameters:
children
(Konva.Node)
Returns: Container
Inherited from: Konva.Container#add
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
Inherited from: Konva.Container#find
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
Inherited from: Konva.Container#findOne
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)
Inherited from: Konva.Container#isAncestorOf
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
Inherited from: Konva.Container#getAllIntersections
clip(clip)
get/set clip
Parameters:
clip
(Object)clip.x
(Number)clip.y
(Number)clip.width
(Number)clip.height
(Number)
Returns: Object
Inherited from: Konva.Container#clip
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
Inherited from: Konva.Container#clipX
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
Inherited from: Konva.Container#clipY
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
Inherited from: Konva.Container#clipWidth
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
Inherited from: Konva.Container#clipHeight
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
Inherited from: Konva.Container#clipFunc
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']
});
clearCache()
clear cached canvas
Returns: Konva.Node
Inherited from: Konva.Node#clearCache
Example:
node.clearCache();
cache(config)
cache node to improve drawing performance, apply filters, or create more accurate hit regions. For all basic shapes size of cache canvas will be automatically detected. If you need to cache your custom Konva.Shape
instance you have to pass shape's bounding box properties. Look at https://konvajs.org/docs/performance/Shape_Caching.html for more information.
Parameters:
config
(Object) (optional)config.x
(Number) (optional)config.y
(Number) (optional)config.width
(Number) (optional)config.height
(Number) (optional)config.offset
(Number) (optional): increase canvas size byoffset
pixel in all directions.config.drawBorder
(Boolean) (optional): when set to true, a red border will be drawn around the cached region for debugging purposesconfig.pixelRatio
(Number) (optional): change quality (or pixel ratio) of cached image. pixelRatio = 2 will produce 2x sized cache.config.imageSmoothingEnabled
(Boolean) (optional): control imageSmoothingEnabled property of created canvas for cacheconfig.hitCanvasPixelRatio
(Number) (optional): change quality (or pixel ratio) of cached hit canvas.
Returns: Konva.Node
Inherited from: Konva.Node#cache
Example:
// cache a shape with the x,y position of the bounding box at the center and
// the width and height of the bounding box equal to the width and height of
// the shape obtained from shape.width() and shape.height()
image.cache();
// cache a node and define the bounding box position and size
node.cache({
x: -30,
y: -30,
width: 100,
height: 200
});
// cache a node and draw a red border around the bounding box
// for debugging purposes
node.cache({
x: -30,
y: -30,
width: 100,
height: 200,
offset : 10,
drawBorder: true
});
isCached()
determine if node is currently cached
Returns: Boolean
Inherited from: Konva.Node#isCached
getClientRect(config)
Return client rectangle {x, y, width, height} of node. This rectangle also include all styling (strokes, shadows, etc). The purpose of the method is similar to getBoundingClientRect API of the DOM.
Parameters:
config
(Object)config.skipTransform
(Boolean) (optional): should we apply transform to node for calculating rect?config.skipShadow
(Boolean) (optional): should we apply shadow to the node for calculating bound box?config.skipStroke
(Boolean) (optional): should we apply stroke to the node for calculating bound box?config.relativeTo
(Object) (optional): calculate client rect relative to one of the parents
Returns: Object
rect with {x, y, width, height} properties
Inherited from: Konva.Node#getClientRect
Example:
var rect = new Konva.Rect({
width : 100,
height : 100,
x : 50,
y : 50,
strokeWidth : 4,
stroke : 'black',
offsetX : 50,
scaleY : 2
});
// get client rect without think off transformations (position, rotation, scale, offset, etc)
rect.getClientRect({ skipTransform: true});
// returns {
// x : -2, // two pixels for stroke / 2
// y : -2,
// width : 104, // increased by 4 for stroke
// height : 104
//}
// get client rect with transformation applied
rect.getClientRect();
// returns Object {x: -2, y: 46, width: 104, height: 208}
on(evtStr, handler)
bind events to the node. KonvaJS supports mouseover, mousemove, mouseout, mouseenter, mouseleave, mousedown, mouseup, wheel, contextmenu, click, dblclick, touchstart, touchmove, touchend, tap, dbltap, dragstart, dragmove, and dragend events. Pass in a string of events delimited by a space to bind multiple events at once such as 'mousedown mouseup mousemove'. Include a namespace to bind an event by name such as 'click.foobar'.
Parameters:
evtStr
(String): e.g. 'click', 'mousedown touchstart', 'mousedown.foo touchstart.foo'handler
(function): The handler function. The first argument of that function is event object. Event object hastarget
as main target of the event,currentTarget
as current node listener andevt
as native browser event.
Returns: Konva.Node
Inherited from: Konva.Node#on
Example:
// add click listener
node.on('click', function() {
console.log('you clicked me!');
});
// get the target node
node.on('click', function(evt) {
console.log(evt.target);
});
// stop event propagation
node.on('click', function(evt) {
evt.cancelBubble = true;
});
// bind multiple listeners
node.on('click touchstart', function() {
console.log('you clicked/touched me!');
});
// namespace listener
node.on('click.foo', function() {
console.log('you clicked/touched me!');
});
// get the event type
node.on('click tap', function(evt) {
var eventType = evt.type;
});
// get native event object
node.on('click tap', function(evt) {
var nativeEvent = evt.evt;
});
// for change events, get the old and new val
node.on('xChange', function(evt) {
var oldVal = evt.oldVal;
var newVal = evt.newVal;
});
// get event targets
// with event delegations
layer.on('click', 'Group', function(evt) {
var shape = evt.target;
var group = evt.currentTarget;
});
off(evtStr)
remove event bindings from the node. Pass in a string of event types delimmited by a space to remove multiple event bindings at once such as 'mousedown mouseup mousemove'. include a namespace to remove an event binding by name such as 'click.foobar'. If you only give a name like '.foobar', all events in that namespace will be removed.
Parameters:
evtStr
(String): e.g. 'click', 'mousedown touchstart', '.foobar'
Returns: Konva.Node
Inherited from: Konva.Node#off
Example:
// remove listener
node.off('click');
// remove multiple listeners
node.off('click touchstart');
// remove listener by name
node.off('click.foo');
remove()
remove a node from parent, but don't destroy. You can reuse the node later.
Returns: Konva.Node
Inherited from: Konva.Node#remove
Example:
node.remove();
destroy()
remove and destroy a node. Kill it and delete forever! You should not reuse node after destroy(). If the node is a container (Group, Stage or Layer) it will destroy all children too.
Inherited from: Konva.Node#destroy
Example:
node.destroy();
getAttr(attr)
get attr
Parameters:
attr
(String)
Returns: Integer|String|Object|Array
Inherited from: Konva.Node#getAttr
Example:
var x = node.getAttr('x');
getAncestors()
get ancestors
Returns: Array
Inherited from: Konva.Node#getAncestors
Example:
shape.getAncestors().forEach(function(node) {
console.log(node.getId());
})
getAttrs()
get attrs object literal
Returns: Object
Inherited from: Konva.Node#getAttrs
setAttrs(config)
set multiple attrs at once using an object literal
Parameters:
config
(Object): object containing key value pairs
Returns: Konva.Node
Inherited from: Konva.Node#setAttrs
Example:
node.setAttrs({
x: 5,
fill: 'red'
});
isListening()
determine if node is listening for events by taking into account ancestors. Parent | Self | isListening listening | listening | ----------+-----------+------------ T | T | T T | F | F F | T | F F | F | F
Returns: Boolean
Inherited from: Konva.Node#isListening
isVisible()
determine if node is visible by taking into account ancestors. Parent | Self | isVisible visible | visible | ----------+-----------+------------ T | T | T T | F | F F | T | F F | F | F
Returns: Boolean
Inherited from: Konva.Node#isVisible
show()
show node. set visible = true
Returns: Konva.Node
Inherited from: Konva.Node#show
hide()
hide node. Hidden nodes are no longer detectable
Returns: Konva.Node
Inherited from: Konva.Node#hide