鼠标悬停时扩展图像
本演示展示了如何创建一个效果,当鼠标悬停在图像上时图像会扩展。这些图像也是可拖动的。
说明: 将鼠标悬停在图像上以查看它们的扩展效果。
- Vanilla
- React
- Vue
import Konva from 'konva'; const width = window.innerWidth; const height = window.innerHeight; const stage = new Konva.Stage({ container: 'container', width: width, height: height, }); const layer = new Konva.Layer(); stage.add(layer); // 创建达斯·维德图像 const darthVaderImg = new Konva.Image({ x: 110, y: 88, width: 200, height: 137, offset: { x: 100, y: 68, }, draggable: true, }); layer.add(darthVaderImg); // 创建尤达图像 const yodaImg = new Konva.Image({ x: 290, y: 70, width: 93, height: 104, offset: { x: 46, y: 52, }, draggable: true, }); layer.add(yodaImg); // 加载达斯·维德图像 const imageObj1 = new Image(); imageObj1.onload = function () { darthVaderImg.image(imageObj1); }; imageObj1.src = 'https://konvajs.org/assets/darth-vader.jpg'; // 加载尤达图像 const imageObj2 = new Image(); imageObj2.onload = function () { yodaImg.image(imageObj2); }; imageObj2.src = 'https://konvajs.org/assets/yoda.jpg'; // 使用事件委托更新指针样式并应用缩放 layer.on('mouseover', function (evt) { const shape = evt.target; document.body.style.cursor = 'pointer'; // 在悬停时缩放图像 shape.to({ scaleX: 1.2, scaleY: 1.2, duration: 0.2, }); }); layer.on('mouseout', function (evt) { const shape = evt.target; document.body.style.cursor = 'default'; // 鼠标离开时缩放回正常 shape.to({ scaleX: 1, scaleY: 1, duration: 0.2, }); });
import { useState, useEffect } from 'react'; import { Stage, Layer, Image } from 'react-konva'; import { useImage } from 'react-konva-utils'; const ImageWithHover = ({ src, x, y, width, height, offsetX, offsetY }) => { const [image] = useImage(src); const [isHovered, setIsHovered] = useState(false); const scale = isHovered ? 1.2 : 1; return ( <Image image={image} x={x} y={y} width={width} height={height} offsetX={offsetX} offsetY={offsetY} scaleX={scale} scaleY={scale} draggable onMouseEnter={() => setIsHovered(true)} onMouseLeave={() => setIsHovered(false)} /> ); }; const App = () => { return ( <Stage width={window.innerWidth} height={window.innerHeight}> <Layer> <ImageWithHover src="https://konvajs.org/assets/darth-vader.jpg" x={110} y={88} width={200} height={137} offsetX={100} offsetY={68} /> <ImageWithHover src="https://konvajs.org/assets/yoda.jpg" x={290} y={70} width={93} height={104} offsetX={46} offsetY={52} /> </Layer> </Stage> ); }; export default App;
<template> <v-stage :config="stageConfig"> <v-layer ref="layerRef"> <v-image v-for="(img, index) in images" :key="index" :config="getImageConfig(img, index)" @mouseenter="handleMouseEnter(index)" @mouseleave="handleMouseLeave(index)" /> </v-layer> </v-stage> </template> <script setup> import { ref, onMounted, computed } from 'vue'; const stageConfig = { width: window.innerWidth, height: window.innerHeight }; const layerRef = ref(null); const hoveredIndex = ref(null); const loadedImages = ref({}); // 定义图像数据 const images = ref([ { src: 'https://konvajs.org/assets/darth-vader.jpg', x: 110, y: 88, width: 200, height: 137, offsetX: 100, offsetY: 68, loaded: false }, { src: 'https://konvajs.org/assets/yoda.jpg', x: 290, y: 70, width: 93, height: 104, offsetX: 46, offsetY: 52, loaded: false } ]); // 加载图像 onMounted(() => { images.value.forEach((img, index) => { const imageObj = new Image(); imageObj.onload = () => { loadedImages.value = { ...loadedImages.value, [index]: imageObj }; }; imageObj.src = img.src; }); }); // 获取每个图像的配置 const getImageConfig = (img, index) => { const isHovered = hoveredIndex.value === index; const scale = isHovered ? 1.2 : 1; return { image: loadedImages.value[index], x: img.x, y: img.y, width: img.width, height: img.height, offsetX: img.offsetX, offsetY: img.offsetY, scaleX: scale, scaleY: scale, draggable: true }; }; // 鼠标事件处理程序 const handleMouseEnter = (index) => { hoveredIndex.value = index; document.body.style.cursor = 'pointer'; }; const handleMouseLeave = () => { hoveredIndex.value = null; document.body.style.cursor = 'default'; }; </script>