Canvas distorts drawing. How do I get the scale factor between the set size and the styled size?

北慕城南 提交于 2019-11-30 20:43:48

This is the same problem that the Mozilla Bespin team ran into. (back when they were using Canvas, before it merged with Ace)

Don't give the Canvas any CSS width/height rules. Doing so usually ends up as a pain. Put the Canvas in a Div that only has a single thing in it (the canvas itself)

As the canvas-parent div changes size, change the size of the canvas (canvas.width and canvas.height) to match the size of the div.

Since most browsers do not fire an event when a div changes size, you'll simply have to check, say, every half second with a timer to see if the div has changed size. If it has, then you resize the canvas accordingly.

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