FabricJS canvas is “stuck” after adding elements

被刻印的时光 ゝ 提交于 2019-11-30 14:18:48

It's possible that canvas internal offset is not updated properly. Something on the page could change dimensions/position, making canvas positioned at a different location than the one it was during initialization. Try calling canvas.calcOffset() after renderAll() call, or after adding those objects.

The reason calcOffset is not called in renderAll is for performance reasons. renderAll is a redraw of an entire canvas. It happens every time something changes on canvas and canvas needs to be redrawn. As you can imagine, it gets called quite often (sometimes dozens of times per second), and it would be pretty wasteful to calculate new offset every time redraw happens.

Consider also that in most cases canvas position does not change throughout page life. It probably happens pretty rarely. Mainly during page load.

In highly-dynamic environment — where canvas changes position often — you can solve offset "problem" by explicitly calling calcOffset().

I had the same problem, in adition to kangax solution, in a VERY extreme environment, you can force it to recalculate the offset anytime the render occurs, with an event.

canvas.on('after:render', function(){ this.calcOffset(); });

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!