Why HTML5 Canvas with a larger size stretch a draw

2019-07-20 13:42发布

I am using a HTML5 <canvas> to draw lines on.

When I change the dimensions from the default of 300x150 to match the dimensions of another <div> my lines appear stretched and are thicker than before. Why is this happening?

I want to draw the lines on the canvas to match the position of elements on the <div> that is in front of it (with a higher z-index) in the same position on the page. How can I stop this stretching from happening?

EDIT: This appears only to be in Firefox.

1条回答
Emotional °昔
2楼-- · 2019-07-20 14:46

You can't change canvas size with CSS. If you do it will behave like an image and it will stretch You have to do it in javascript with canvas properties canvas.width, canvas.height

查看更多
登录 后发表回答