简体   繁体   中英

Rectangle on HTML5 Canvas is stretched

I am working on a website and I try to get rectangles on a HTML5 canvas with javascript. Normally this is no problem but now when I make a rectangle with the width and height of 10. It seems that it makes a rectangle with the width of 10 and the height of 20.

I'm making the Rectangle like this:

var canvas = $("#canvas");
var context = canvas.get(0).getContext("2d");

context.fillRect(0, 0, 10, 10);

The div canvas is set with a width of 100% but i tried to give it a fixed width and that didn't help either.

You need to set a width and height on your canvas element, otherwise this can be the result in some browsers.

<canvas id="canvas" width="400" height="300" />

And you can not set the canvas size with CSS, this will stretch the canvas.

The default size of the canvas is 300 x 150.

You are probably using these defaults, which will make the rectangle look not square....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM