Layer
new Konva.Layer(config)
Layer constructor. Layers are tied to their own canvas element and are used to contain groups or shapes.
Parameters
| Name | Type | Description |
|---|---|---|
| config | Object | |
| clearBeforeDraw (optional) | Boolean | set this property to false if you don't want to clear the canvas before each layer draw. The default value is true. |
| 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
Own Methods
getCanvas()
get layer canvas wrapper
getNativeCanvasElement()
get native canvas element
getHitCanvas()
get layer hit canvas
getContext()
get layer canvas context
width()
get/set width of layer. getter return width of stage. setter doing nothing. if you want change width use stage.width(value);
Returns: Number
Example:
var width = layer.width();
height()
get/set height of layer.getter return height of stage. setter doing nothing. if you want change height use stage.height(value);
Returns: Number
Example:
var height = layer.height();
batchDraw()
batch draw. this function will not do immediate draw but it will schedule drawing to next tick (requestAnimFrame)
Returns: Konva.Layer this
getIntersection(pos)
get visible intersection shape. This is the preferred method for determining if a point intersects a shape or not also you may pass optional selector parameter to return ancestor of intersected shape nodes with listening set to false will not be detected
Parameters:
pos(Object)pos.x(Number)pos.y(Number)
Returns: Konva.Node
Example:
var shape = layer.getIntersection({x: 50, y: 50});
enableHitGraph()
enable hit graph. DEPRECATED! Use layer.listening(true) instead.
Returns: Layer
disableHitGraph()
disable hit graph. DEPRECATED! Use layer.listening(false) instead.
Returns: Layer
toggleHitCanvas()
Show or hide hit canvas over the stage. May be useful for debugging custom hitFunc
imageSmoothingEnabled(imageSmoothingEnabled)
get/set imageSmoothingEnabled flag For more info see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/imageSmoothingEnabled
Parameters:
imageSmoothingEnabled(Boolean)
Returns: Boolean
Example:
// get imageSmoothingEnabled flag
var imageSmoothingEnabled = layer.imageSmoothingEnabled();
layer.imageSmoothingEnabled(false);
layer.imageSmoothingEnabled(true);
clearBeforeDraw(clearBeforeDraw)
get/set clearBeforeDraw flag which determines if the layer is cleared or not before drawing
Parameters:
clearBeforeDraw(Boolean)
Returns: Boolean
Example:
// get clearBeforeDraw flag
var clearBeforeDraw = layer.clearBeforeDraw();
// disable clear before draw
layer.clearBeforeDraw(false);
// enable clear before draw
layer.clearBeforeDraw(true);
hitGraphEnabled(enabled)
get/set hitGraphEnabled flag. DEPRECATED! Use layer.listening(false) instead. Disabling the hit graph will greatly increase draw performance because the hit graph will not be redrawn each time the layer is drawn. This, however, also disables mouse/touch event detection
Parameters:
enabled(Boolean)
Returns: Boolean
Example:
// get hitGraphEnabled flag
var hitGraphEnabled = layer.hitGraphEnabled();
// disable hit graph
layer.hitGraphEnabled(false);
// enable hit graph
layer.hitGraphEnabled(true);
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 byoffsetpixel 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}