Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 679
chris Avatar asked Aug 02 '11 06:08

chris


2 Answers

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.

like image 102
Jonas Avatar answered Oct 15 '22 07:10

Jonas


The default size of the canvas is 300 x 150.

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

like image 1
Ray Toal Avatar answered Oct 15 '22 08:10

Ray Toal