HTML5 Canvas 拖放图像
如需使用 Konva 拖放图像,可以在创建图形时将 draggable 属性
设置为 true,也可以使用 draggable() 方法。
draggable() 方法会自动为桌面应用和移动应用启用拖放功能。
- Vanilla
- React
- Vue
import Konva from 'konva';
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight,
});
const layer = new Konva.Layer();
const imageObj = new Image();
imageObj.onload = () => {
const yoda = new Konva.Image({
x: 50,
y: 50,
image: imageObj,
width: 106,
height: 118,
draggable: true,
});
// add cursor styling
yoda.on('mouseover', function () {
document.body.style.cursor = 'pointer';
});
yoda.on('mouseout', function () {
document.body.style.cursor = 'default';
});
layer.add(yoda);
};
imageObj.src = 'https://konvajs.org/assets/yoda.jpg';
stage.add(layer);
import { Stage, Layer, Image } from 'react-konva';
import { useState } from 'react';
import useImage from 'use-image';
const App = () => {
const [position, setPosition] = useState({ x: 50, y: 50 });
const [yodaImage] = useImage('https://konvajs.org/assets/yoda.jpg');
return (
<Stage width={window.innerWidth} height={window.innerHeight}>
<Layer>
<Image
x={position.x}
y={position.y}
image={yodaImage}
width={106}
height={118}
draggable
onDragEnd={(e) => {
setPosition({ x: e.target.x(), y: e.target.y() });
}}
onMouseEnter={(e) => {
document.body.style.cursor = 'pointer';
}}
onMouseLeave={(e) => {
document.body.style.cursor = 'default';
}}
/>
</Layer>
</Stage>
);
};
export default App;
<template>
<v-stage :config="stageSize">
<v-layer>
<v-image
v-if="yodaImage"
:config="imageConfig"
@mouseenter="handleMouseEnter"
@mouseleave="handleMouseLeave"
/>
</v-layer>
</v-stage>
</template>
<script setup>
import { ref } from 'vue';
import { useImage } from 'vue-konva';
const stageSize = {
width: window.innerWidth,
height: window.innerHeight
};
const [yodaImage] = useImage('https://konvajs.org/assets/yoda.jpg');
const imageConfig = ref({
x: 50,
y: 50,
image: yodaImage,
width: 106,
height: 118,
draggable: true
});
const handleMouseEnter = () => {
document.body.style.cursor = 'pointer';
};
const handleMouseLeave = () => {
document.body.style.cursor = 'default';
};
</script>