Skip to main content

Canvas Cursor Style — Change Mouse Cursor on HTML5 Canvas Shapes

Change the mouse cursor when users hover over shapes on an HTML5 Canvas. Konva lets you listen for mouse events on individual shapes and apply any CSS cursor style to the stage container — pointer, crosshair, grab, move, or any custom cursor.

Instructions: Hover over each pentagon to see the cursor change.

import Konva from 'konva';

const stage = new Konva.Stage({
  container: 'container',
  width: window.innerWidth,
  height: window.innerHeight,
});

const layer = new Konva.Layer();
stage.add(layer);

const pentagon1 = new Konva.RegularPolygon({
  x: 80,
  y: stage.height() / 2,
  sides: 5,
  radius: 30,
  fill: 'red',
  stroke: 'black',
  strokeWidth: 4,
});

pentagon1.on('mouseover', function (e) {
  e.target.getStage().container().style.cursor = 'pointer';
});
pentagon1.on('mouseout', function (e) {
  e.target.getStage().container().style.cursor = 'default';
});

const pentagon2 = new Konva.RegularPolygon({
  x: 180,
  y: stage.height() / 2,
  sides: 5,
  radius: 30,
  fill: 'green',
  stroke: 'black',
  strokeWidth: 4,
});

pentagon2.on('mouseover', function (e) {
  e.target.getStage().container().style.cursor = 'crosshair';
});
pentagon2.on('mouseout', function (e) {
  e.target.getStage().container().style.cursor = 'default';
});

const pentagon3 = new Konva.RegularPolygon({
  x: 280,
  y: stage.height() / 2,
  sides: 5,
  radius: 30,
  fill: 'blue',
  stroke: 'black',
  strokeWidth: 4,
});

pentagon3.on('mouseover', function (e) {
  e.target.getStage().container().style.cursor = 'move';
});
pentagon3.on('mouseout', function (e) {
  e.target.getStage().container().style.cursor = 'default';
});

layer.add(pentagon1);
layer.add(pentagon2);
layer.add(pentagon3);