Click here to Skip to main content
15,897,187 members
Articles / Web Development / HTML5

My First Steps Towards HTML5 – Trying out Canvas

19 Mar 2012CPOL7 min read 35.2K   291   16  
In this article, Paul Farquhar, recounts his first experience with HTML5 and how he was inspired to create a spirograph app using Canvas. This article shows the initial spirograph design that Paul created, as well as sample codes and additional resources.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

<!DOCTYPE html>
<html>
 <head>
 <title>Spiro-01-2</title>
  <script type="application/javascript">
    function DrawSpiro() {
		var objCanvas = document.getElementById("canvas");
		var ctx = objCanvas.getContext("2d");
		ctx.save();

		var size = 0;
		if(objCanvas.width<objCanvas.height)
			size = objCanvas.width;
		else
			size = objCanvas.height;
		ctx.translate(size/2,size/2);
		
		var OuterRadius = document.SpiroInput.OuterRadius.value;
		var InnerRadius = document.SpiroInput.InnerRadius.value;
		// calc some helper variables
		var OmI = OuterRadius-InnerRadius;	
		var IdO = InnerRadius / OuterRadius;
		var IdO1 = 1 - IdO;
		
		var PenOffset = document.SpiroInput.PenOffset.value*InnerRadius/100;
		var PenColour;		// for later extension
		var PenThickness;	// for later extension
		var StartX;			// we start drawing here
		var StartY;			// we start drawing here
		var x,y;
		var Step = Math.PI/180*10;
		var Angle = 0;
		
		StartX = OmI*Math.cos(IdO*Angle)+PenOffset*Math.cos(IdO1*Angle);
		StartY = OmI*Math.sin(IdO*Angle)-PenOffset*Math.sin(IdO1*Angle);
		
		ctx.beginPath();
		ctx.moveTo(StartX,StartY);
		do
		{
			Angle += Step;
			x = OmI*Math.cos(IdO*Angle)+PenOffset*Math.cos(IdO1*Angle);
			y = OmI*Math.sin(IdO*Angle)-PenOffset*Math.sin(IdO1*Angle);
			ctx.lineTo(x,y);
		} while ((x != StartX) && (y != StartY));
		ctx.stroke();
		ctx.restore();
    }
	
	function clearCanvas(){
		var canvas = document.getElementById("canvas");
		var context = canvas.getContext("2d");
		context.clearRect(0, 0, canvas.width, canvas.height);
	}

	function OuterP10(){
		document.SpiroInput.OuterRadius.value = String(Number(document.SpiroInput.OuterRadius.value)+10);
	}
	function OuterP1(){
		document.SpiroInput.OuterRadius.value = String(Number(document.SpiroInput.OuterRadius.value)+1);
	}	
	function OuterM10(){
		document.SpiroInput.OuterRadius.value = String(Number(document.SpiroInput.OuterRadius.value)-10);
	}
	function OuterM1(){
		document.SpiroInput.OuterRadius.value = String(Number(document.SpiroInput.OuterRadius.value)-1);
	}
	function InnerP10(){
		document.SpiroInput.InnerRadius.value = String(Number(document.SpiroInput.InnerRadius.value)+10);
	}
	function InnerP1(){
		document.SpiroInput.InnerRadius.value = String(Number(document.SpiroInput.InnerRadius.value)+1);
	}	
	function InnerM10(){
		document.SpiroInput.InnerRadius.value = String(Number(document.SpiroInput.InnerRadius.value)-10);
	}
	function InnerM1(){
		document.SpiroInput.InnerRadius.value = String(Number(document.SpiroInput.InnerRadius.value)-1);
	}
	function PenOffsetP10(){
		document.SpiroInput.PenOffset.value = String(Number(document.SpiroInput.PenOffset.value)+10);
	}
	function PenOffsetP1(){
		document.SpiroInput.PenOffset.value = String(Number(document.SpiroInput.PenOffset.value)+1);
	}	
	function PenOffsetM10(){
		document.SpiroInput.PenOffset.value = String(Number(document.SpiroInput.PenOffset.value)-10);
	}
	function PenOffsetM1(){
		document.SpiroInput.PenOffset.value = String(Number(document.SpiroInput.PenOffset.value)-1);	
	}
  </script>
 </head>
 <body>
   <canvas id="canvas" width="400" height="400"></canvas>
   <br/>

	<button onclick="OuterP10();">+10</button>
	<button onclick="OuterP1();">+1</button>
	<button onclick="InnerP10();">+10</button>
	<button onclick="InnerP1();">+1</button>
	<button onclick="PenOffsetP10();">+10</button>
	<button onclick="PenOffsetP1();">+1</button>
   <form name="SpiroInput">
		<input name="OuterRadius" type="text" size="7" value="181">
		<input name="InnerRadius" type="text" size="7" value="47">
		<input name="PenOffset" type="text" size="5" value="130">
	</form>
	<button onclick="OuterM10();">-10 </button>
	<button onclick="OuterM1();">-1</button>
	<button onclick="InnerM10();">-10</button>
	<button onclick="InnerM1();">-1</button>
	<button onclick="PenOffsetM10();">-10</button>
	<button onclick="PenOffsetM1();">-1</button>
	</br>
	<button onclick="DrawSpiro();">Draw</button>
	<button onclick="clearCanvas();">Clear</button>
  
 </body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Intel
Germany Germany
Paul Farquhar leads the Engineering & Validation team for the Intel AppUpSM developer program in Europe. When he is not talking to the community about developing for AppUp at conferences and other events he and his team help developers in all aspects of the developer program. Prior to joining Intel Paul was a software developer, IT sales and marketing manager and project manager.

Comments and Discussions