2 Replies - 258 Views - Last Post: 01 February 2012 - 10:15 PM

Topic Sponsor:

#1 Sho Ke  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 97
  • Joined: 13-October 11

HTML5 Canvas / stroke() Acting Up

Posted 01 February 2012 - 10:15 AM

For a few days now I've been fiddling around with the new HTML5 elements and have been focusing on the canvas tag. I've been messing around drawing randomobjects and have run into some trouble.

I tried drawing a horizontal and vertical line both intersecting in the middle of the canvas(imitating a coordinate plane's x and y axis), though I didn't get the expected results until some guesswork with the numbers, namely the y values.

What I used to get it to work:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
	<title> </title>
</head>
<body>
	<canvas id="canv" style="height:300px;width:300px;"> </canvas>

	<script>
	
var c = document.getElementById("canv");
var ctx = c.getContext("2d");



//y-axis
ctx.moveTo(150,0);
ctx.lineTo(150,300);
ctx.stroke();

//x-axis
ctx.moveTo(0,75);
ctx.lineTo(300,75);
ctx.stroke();

ctx.moveTo(0,0);
ctx.lineTo(100,100);
ctx.stroke();



	</script>
</body>



sans the last 3 lines(move to 0,0 ; line to 100,100 ; and the stroke), The result is a canvas looking similar to an x-y graph.

Take note of the y values for the second(horizontal) line. In order to (correctly) graph the line, the y values had to be cut in half, otherwise the line would be sitting on the bottom of the canvas. Also, the horizontal line's stroke appears a few pixels thicker than the vertical.

Is there any weird x:y ratio that I'm not aware of? Any help would be appreciated. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: HTML5 Canvas / stroke() Acting Up

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3239
  • View blog
  • Posts: 10,644
  • Joined: 18-April 07

Re: HTML5 Canvas / stroke() Acting Up

Posted 01 February 2012 - 05:55 PM

It is because you are using styling to set the width and height. This does not reset the coordinates in the context. So what is happening is that you are defining the width and height coordinate system with one set of dimensions, then adjusting the viewable area with the styling.

The canvas tag has "width" and "height" attributes and if you set those to 300 each, you will see that your Y values will go back to being 150 instead of 75.

Nothing wrong with using the attributes here. If you need to change the width and height afterwards, you will have to reinitialize the coordinates system. Take a look at setting the width and height of the item using the setAttribute() method of the canvas' context object.

:)
Was This Post Helpful? 2
  • +
  • -

#3 Sho Ke  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 97
  • Joined: 13-October 11

Re: HTML5 Canvas / stroke() Acting Up

Posted 01 February 2012 - 10:15 PM

I took out the styling and used width="300" and height="300", changed the coordinates used back to 150 from 75 and it worked! I was under the impression that using the width and height attributes was always bad practice and that changing the appearance(style="...") would change the x- and y-max in the canvas.

Thanks for the help!

This post has been edited by Sho Ke: 01 February 2012 - 10:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1