HTML5 canvas Custom Shape Tutorial
To create a custom shape with Konva
, you can use the Konva.Shape()
object and define a custom drawing function.
When creating a custom shape, you need to define a drawing function that is passed a Konva.Context renderer and a shape instance. Here's a simple rectangle example:
const rect = new Konva.Shape({
x: 10,
y: 20,
fill: '#00D2FF',
width: 100,
height: 50,
sceneFunc: function (context, shape) {
context.beginPath();
// don't need to set position of rect, Konva will handle it
context.rect(0, 0, shape.getAttr('width'), shape.getAttr('height'));
// (!) Konva specific method, it is very important
// it will apply all required styles
context.fillStrokeShape(shape);
}
});
Konva.Context
is a wrapper around native 2d canvas context that has the same properties and methods with some additional API.
There are two properties that can be used for drawing custom shapes:
sceneFunc
- defines visual appearance of a shapehitFunc
- optional function to define custom hit region for events (see Custom Hit Region demo)
Best practices for writing sceneFunc
and hitFunc
:
- Optimize the function as it can be called many times per second. Avoid creating images or large objects.
- The function should not have side effects like moving shapes, attaching events or changing app state.
- Define custom
hitFunc
when applying complex styles or drawing images. - Don't manually apply position and scaling in
sceneFunc
. Let Konva handle it through shape properties. - Avoid manual styling in
sceneFunc
. Usecontext.fillStrokeShape(shape)
for styling. - Reference Konva core shapes implementations for more examples.
For full list of properties and methods, see the Shape API Reference.
- Vanilla
- React
- Vue