Current location: Hot Scripts Forums » General Web Coding » HTML5 » Strange line with context.stroke()


Strange line with context.stroke()

Reply
  #1 (permalink)  
Old 12-14-11, 08:43 AM
mdagis mdagis is offline
New Member
 
Join Date: Dec 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Strange line with context.stroke()

Hello, I am quite a newby on HTML 5 and I tried to paly around with the graphics.

I have made a method drawTree that creates trees and it consists from 2 other methods : the drawBranches for the upper part and drawHead for the lower part. The problem is that when I call both the methods drawBranches & drawHead, a strange line at the upper left part is created. When I call each of these methods separetly the strange line does not apear.

Here is the code:

Code:
<!DOCTYPE HTML>
<html>
<head>
<script>

function drawHead(context, startX, startY){

		context.lineCap = "round";
		context.lineWidth = 20 + 10;

		context.strokeStyle = "saddlebrown";

		context.moveTo(startX, startY);

		context.lineTo(startX, startY + 70);

		//context.fillStyle   = '#00f';
		//alert("before")
		context.stroke();
		//alert("after")

}

function drawBranches(context, startX, startY, trunkWidth, level){
if (level < 12) {
	
	var changeX = 100 / (level + 1);
	var changeY = 200 / (level + 1);
	
	var topRightX = startX + Math.random() * changeX;
	var topRightY = startY - Math.random() * changeY;
	
	var topLeftX = startX - Math.random() * changeX;
	var topLeftY = startY - Math.random() * changeY;
	
	
	// draw right branch
	context.beginPath();
	context.moveTo(startX + trunkWidth / 4, startY);
	context.quadraticCurveTo(startX + trunkWidth / 4, startY - trunkWidth, topRightX, topRightY);
	context.lineWidth = trunkWidth;
	context.lineCap = "round";
	if (level < 5)
  {
		//context.strokeStyle = "rgb(128,0,128)";
		context.strokeStyle = "saddlebrown";
  }
  else
  {
	  context.strokeStyle = "olive";
  }
	//context.strokeStyle = "green";
	context.stroke();
	
	
	// draw left branch
	context.beginPath();
	context.moveTo(startX - trunkWidth / 4, startY);
	context.quadraticCurveTo(startX - trunkWidth / 4, startY - trunkWidth, topLeftX, topLeftY);
	context.lineWidth = trunkWidth;
	context.lineCap = "round";
	context.stroke();
	
	
	drawBranches(context, topRightX, topRightY, trunkWidth * 0.7, level + 1);
	drawBranches(context, topLeftX, topLeftY, trunkWidth * 0.7, level + 1);


	}

	
}

window.onload = function(){
canvas = document.getElementById("myCanvas");
context = canvas.getContext("2d");

//drawHead(context, 200, canvas.height - 100)
//drawBranches(context, 200, canvas.height - 100, 20, 0);

drawTree(context, 200, canvas.height - 100, 20, 0);
drawTree(context, 600, canvas.height - 100, 20, 0);
drawTree(context, 900, canvas.height - 100, 20, 0);


//drawBranches(context, 600, canvas.height - 100, 20, 0);

};

function drawTree(context, startX, startY, trunkWidth, level){
	//drawHead(context, 200, canvas.height - 100)
	//drawBranches(context, 200, canvas.height - 100, 20, 0);

	drawBranches(context, startX, startY, 20, 0);
	
	drawHead(context, startX, startY);

};

</script>
</head>
<body>
<canvas id="myCanvas" width="1100" height="800" style="border:0px solid black;">
</canvas>
</body>
</html>
Reply With Quote
  #2 (permalink)  
Old 12-16-11, 07:39 AM
alxkls alxkls is offline
Newbie Coder
 
Join Date: Nov 2011
Posts: 98
Thanks: 0
Thanked 9 Times in 9 Posts
by the looks of it you've used the same name for separate variables somewhere cause if you do this:
Code:
window.onload = function(){
canvas = document.getElementById("myCanvas");
context = canvas.getContext("2d");

drawHead(context, 200, canvas.height - 100)
drawHead(context, 600, canvas.height - 100)
drawHead(context, 900, canvas.height - 100)
//drawBranches(context, 200, canvas.height - 100, 20, 0);

drawTree(context, 200, canvas.height - 100, 20, 0);
drawTree(context, 600, canvas.height - 100, 20, 0);
drawTree(context, 900, canvas.height - 100, 20, 0);


//drawBranches(context, 600, canvas.height - 100, 20, 0);

};

function drawTree(context, startX, startY, trunkWidth, level){
	//drawHead(context, 200, canvas.height - 100)
	//drawBranches(context, 200, canvas.height - 100, 20, 0);

	drawBranches(context, startX, startY, 20, 0);
	
	//drawHead(context, startX, startY);

};
it works fine. also if you do this:

Code:
function drawTree(context, startX, startY, trunkWidth, level){
	//drawHead(context, 200, canvas.height - 100)
	//drawBranches(context, 200, canvas.height - 100, 20, 0);
	drawHead(context, startX, startY);
	drawBranches(context, startX, startY, 20, 0);
	
	//

};
.....
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Moving division chiragkhunti CSS 5 03-19-09 12:47 PM
No error message... stormshadow PHP 3 12-11-06 06:31 PM
Redirection back to a page from form submit DAL Perl 11 03-21-05 02:45 PM
I most definately suggest DevelopingCentral.com For Any Website Design/Development! Salty777 General Advertisements 2 10-01-04 04:27 AM
asp-iis-Server error nsuresh_rasr ASP.NET 3 02-08-04 12:47 AM


All times are GMT -5. The time now is 10:20 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.