MeltingDog
MeltingDog

Reputation: 15414

HTML 5 canvas lines appear distorted

I am trying to make a grid on my 500px x 500px canvas:

<canvas id="area" style="width: 500px; height: 500px;"></canvas>


 var canvas = document.getElementById('area');
     var context = canvas.getContext('2d');

for (var x = 0.5; x < 500; x += 10) {
  context.moveTo(x, 0);
  context.lineTo(x, 500);
}

for (var y = 0.5; y < 500; y += 10) {
  context.moveTo(0, y);
  context.lineTo(500, y);
}

context.strokeStyle = "#eee";
context.stroke();

The code looks correct to me but for some reason its coming out elongated and pixelated:

http://jsfiddle.net/DK4m7/1/

Would anyone know why this occurring?

Upvotes: 1

Views: 147

Answers (1)

user1693593
user1693593

Reputation:

Avoid using CSS to set the canvas size, do instead:

<canvas id="area" width=500 height=500></canvas>

Using CSS will just stretch the current size of the canvas' bitmap which defaults to 350 x 150 pixels. You need to specifically define the bitmap size using the width and height attributes.

Modified fiddle

Upvotes: 5

Related Questions