跳到主要内容

拖放多个带边框突出显示的图像

此示例展示如何为图像实现突出显示效果。将鼠标悬停在图像上时,边框会消失。将鼠标移开后,边框会再次出现。

**操作说明:**将鼠标悬停在图像上以隐藏边框,并在舞台上拖动图像。

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);
};
imageObj1.src = 'https://konvajs.org/assets/darth-vader.jpg';

// Load Yoda image
const imageObj2 = new Image();
imageObj2.onload = function () {
  yodaImg.image(imageObj2);
};
imageObj2.src = 'https://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.on('mouseout', function (evt) {
  const shape = evt.target;
  document.body.style.cursor = 'default';
  shape.strokeEnabled(true);
});