1 回答

TA貢獻(xiàn)1851條經(jīng)驗 獲得超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} />;
};
或者您可以重置狀態(tài)中的位置(并且保存位置更改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} />;
};
添加回答
舉報