I can set the width and height of my canvas by setting the width and height attributes like so:
<canvas id="canvas" width="510" height="510"></canvas>
But I don’t want to hard-code all my drawing code to depend on these values. I’d like set the “internal” width/height to say, 100×100 and then draw everything in those coordinates, and have the canvas scale it out (the same way I do in OpenGL). That way if I ever resize my canvas, it still renders nicely.
I figured out that if I set the CSS width and height on the canvas I can make it larger, but keep the same units, but I found that it actually stretches the result and it blurs and looks terrible (I thought it still render like a vector, nice and crisp).
Is there a way to do this?
You could draw everything in “local” coordinates and then as the first thing you do when you draw, scale the context by the relative size of the actual canvas.
E.g.
HTML
JS
Running example