1 回答
TA贡献1851条经验 获得超3个赞
如果您想在加载新图像时重置舞台位置,您可以:
ref如果访问,请手动重置舞台的位置
const App = () => {
const stageRef = React.useRef();
const [image] useImage(url);
// every time an images is changed we should reset position of the stage
React.useEffect(() => {
stageRef.current.position({ x: 0, y: 0});
}, [image])
return <Stage ref={stageRef} draggable width={canvasWidth} height={canvasHeight} />;
};
或者您可以重置状态中的位置(并且保存位置更改dragmove或dragend事件很重要):
const App = () => {
const [pos, setPos] = React.useState({x : 0, y: 0 });
const handleDragEnd = (e) => {
setPos({
x: e.target.x(),
y: e.target.y(),
});
};
// every time an images is changed we should reset position of the stage
React.useEffect(() => {
setPos({ x: 0, y: 0});
}, [image])
return <Stage onDragEnd={handleDragEnd} draggable width={canvasWidth} height={canvasHeight} />;
};
添加回答
举报