如何使用 React 将图像元素拖放到 Canvas 中?
可以使用原生 HTML5 拖放功能,将页面中的图像或其他元素放入 Canvas 区域。
import React from 'react'; import { Stage, Layer, Image } from 'react-konva'; import useImage from 'use-image'; const URLImage = ({ image }) => { const [img] = useImage(image.src); return ( <Image image={img} x={image.x} y={image.y} // I will use offset to set origin to the center of the image offsetX={img ? img.width / 2 : 0} offsetY={img ? img.height / 2 : 0} draggable /> ); }; const App = () => { const dragUrl = React.useRef(); const stageRef = React.useRef(); const [images, setImages] = React.useState([]); return ( <div> Try to drag and drop the image into the stage: <br /> <img alt="lion" src="https://konvajs.org/assets/lion.png" draggable="true" onDragStart={(e) => { dragUrl.current = e.target.src; }} /> <div onDrop={(e) => { e.preventDefault(); // register event position stageRef.current.setPointersPositions(e); // add image setImages( images.concat([ { ...stageRef.current.getPointerPosition(), src: dragUrl.current, }, ]) ); }} onDragOver={(e) => e.preventDefault()} > <Stage width={window.innerWidth} height={window.innerHeight} style={{ border: '1px solid grey' }} ref={stageRef} > <Layer> {images.map((image) => { return <URLImage image={image} />; })} </Layer> </Stage> </div> </div> ); }; export default App;