Skip to main content

Drag and Drop Multiple Images with Border Highlighting

This demo shows how to implement highlighting effects with images. When hovering over an image, the border disappears, and it reappears when you move the mouse away.

Instructions: Hover over the images to hide their borders and drag them around the stage.

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);

// Create Darth Vader image
const darthVaderImg = new Konva.Image({
  x: 20,
  y: 20,
  width: 200,
  height: 137,
  stroke: 'red',
  strokeWidth: 10,
  draggable: true,
});
layer.add(darthVaderImg);

// Create Yoda image
const yodaImg = new Konva.Image({
  x: 240,
  y: 20,
  width: 93,
  height: 104,
  draggable: true,
  stroke: 'red',
  strokeWidth: 10,
});
layer.add(yodaImg);

// Load Darth Vader image
const imageObj1 = new Image();
imageObj1.onload = function () {
  darthVaderImg.image(imageObj1);
  layer.draw();
};
imageObj1.src = 'https://new.konvajs.org/assets/darth-vader.jpg';

// Load Yoda image
const imageObj2 = new Image();
imageObj2.onload = function () {
  yodaImg.image(imageObj2);
  layer.draw();
};
imageObj2.src = 'https://new.konvajs.org/assets/yoda.jpg';

// Use event delegation to update pointer style and borders
layer.on('mouseover', function (evt) {
  const shape = evt.target;
  document.body.style.cursor = 'pointer';
  shape.strokeEnabled(false);
  layer.draw();
});

layer.on('mouseout', function (evt) {
  const shape = evt.target;
  document.body.style.cursor = 'default';
  shape.strokeEnabled(true);
  layer.draw();
});