经过一番研究,我发现了问题出在哪里.
Why I couldn't manipulate deserialized object?
Because there were 2 canvas html elements. As we can see from screenshot, there are 2 canvases. First one (green border) with image and the second (red border) is empty. And all my interaction was with empty (red) canvas. If I delete second canvas I can interact with image object.
useEffect
的StrictMode
个回调中有Why did it happen?个调用两次,这就是为什么我得到嵌套的画布html元素的原因.
How to fix it?有几种变通方法可以摆脱它.
- go 掉
StrictMode
.显而易见,简单,但不想要的解决方案.
- 在组件外部使用标志变量.
let wasCalled = false;
export default function App() {
// ...
useEffect(() => {
if (wasCalled) {
return;
}
wasCalled = true;
// ...
},[])
也许还有更好的解决方案,比如以其他方式初始化画布实例,但目前对我来说还可以.