How to listen to an event on a canvas shape with Svelte and Konva?

With svelte-konva you can easily listen to user input events (click, dblclick, mouseover, tap, dbltap, touchstart, etc…) and drag&drop events (dragstart, dragmove, dragend).

import { Stage, Layer, Rect } from 'svelte-konva';

function handleClick(e) {
const konvaEvent = e.detail;
window.alert(`Clicked on rectangle: ${konvaEvent.type}`);

<Stage config={{ width: window.innerWidth, height: window.innerHeight }}>
config={{ x: 100, y: 100, width: 400, height: 200, fill: 'blue' }}

For the full list of events take a look into on() method documentation.


Konva events bubble up by default. To prevent this you can call preventDefault() on the event object or set the cancelBubble property of the Konva event to false:

function handleClick(e) {
const konvaEvent = e.detail;

// Cancel bubbling

// or alternatively
konvaEvent.cancelBubble = true;
Enjoying Konva? Please consider to support the project.