Search code examples
htmlgraphicscanvasimage-scaling

Why do my HTML5 rectangles appear zoomed in?


Following http://www.html5canvastutorials.com/tutorials/html5-canvas-rectangles/, I have drawn some rectangles side-by-side on a canvas. The problem is that they appear greatly zoomed in; at a zoom of 1.0 they appear approximately five times their original size; they appear correctly sized (if fuzzy around the borders) at a zoom of around 0.16.

I expect I could get a workaround by making the pixel dimensions of the canvas much greater and zooming out, but what is the proper way to get a 1:1 scaling on a canvas? The canvas is styled to width and height of 100%, and the body has a margin of 0. Manually setting the canvas's width and height to the height and width of the window does not alter this behavior.

TIA,


Solution

  • the problem is, you set the width and height of the style for the canvas. You need to set the width and height attributes, not the css style. so something like:

    <canvas id='mycanvas' width='800' height='600'></canvas>
    

    More info in a similar question: Canvas is stretched when using CSS but normal with "width" / "height" properties