Relatively sizing HTML Canvas

后端 未结 3 1195
花落未央
花落未央 2020-12-14 21:09

The HTML5 element does not accept relative sizes (percent) for its width and height properties.

What I\'m trying to accompl

3条回答
  •  时光取名叫无心
    2020-12-14 21:47

    Like said by sethobrien a canvas element has TWO pairs width/height of attributes.

    1. canvas.width / canvas.height are about the size in pixel of the buffer that will contains the result of drawing commands.

    2. canvas.style.width / canvas.style.height are about the size used to show the canvas object in the browser window and they can be in any of the units supported by css.

    You can indeed set canvas.width and canvas.height just once, do the drawing in the canvas, setting the style size parameters in percentage and then forget about redrawing the canvas content. Of course this means that the browser will just do the scaling itself like for a regular image loaded from the network so the visible result will show pixel scaling artifacts.

    You need to redraw the canvas content after the resize of the canvas element only if you want pixel-perfect results.

提交回复
热议问题