A number of people in our community have started asking the “how do I get started as a games programmer?” I believe we live in an exciting time to learn the craft of computer programming. All the tools you need to start a simple game, exist in your web browser today.
In this post, I will introduce a very simple programmable browser feature that enables you to start learning computer graphics. Using the Canvas tag, browser based users can view games and visualizations that you create without installing additional software like Silverlight or Flash. Since the technology is just part of a HTML web page, you can publish and share your work with the world!
The “canvas” tag gives programmers a rectangular 2D “drawing pad” that can be placed in your browser. In this tutorial, I want to introduce you to drawing simple animations using the Canvas tag. If you are new to browser based programming, I would highly recommend that you check out the following free websites and tutorials.
- Khan Academy for Computer Science – https://www.khanacademy.org/cs – They have created an amazing collection of learning tutorials.
As I work with the canvas tag, I appreciate that the coding experience feels rapid. I can make small code changes and refresh the browser to see the impact quickly. It’s fun working with a programming technology that does not require compiling. Thanks to Google, Apple, Microsoft, and Firefox, the browsers are getting faster and more capable. I think programming using the Canvas tag can be a great technology option for students and teachers who have Chromebooks.
To demonstrate this pattern for simple animation, let’s create a small clock using the Canvas tag. I have included all the code below. You can see the clock below.
// Calculate position of clock objects...
var clockWidth = myCanvas.width;
var clockCenterX = clockWidth / 2;
var clockCenterY = clockWidth / 2;
var hourLen = clockWidth / 2 * 0.7;
var minLen = clockWidth / 2 * 0.6;
var secLen = clockWidth / 2 * 0.5;
var K = -1*(Math.PI * 2.0) / 4;
// Draw background ...
// Draw circle for clock ...
g.strokeStyle = 'yellow';
g.arc(clockCenterX,clockCenterY,clockWidth/2 * 0.9,0,2*Math.PI);
// Get time ...
var date = new Date;
var seconds = date.getSeconds();
var minutes = date.getMinutes();
var hour = date.getHours() ;
var divTime = document.getElementById("divTime");
divTime.innerText = "The time is " + hour+":" +minutes+ ":"+seconds;
// Draw Hour Glass ...
// Calculate Hour X, Y
var hourAngleUnit = (Math.PI * 2.0) / 12.0;
var hourX = hourLen * Math.cos(hour * hourAngleUnit + K) + clockCenterX;
var hourY = hourLen * Math.sin(hour * hourAngleUnit + K) + clockCenterY;
g.strokeStyle = '#ff0000';
// Draw Minutes ...
var minuteAngleUnit = (Math.PI * 2.0) / 60.0;
var minX = minLen * Math.cos(minutes * minuteAngleUnit + K) + clockCenterX;
var minY = minLen * Math.sin(minutes * minuteAngleUnit + K) + clockCenterY;
g.strokeStyle = '#00ff00';
// Draw Seconds ...
var secondAngleUnit = (Math.PI*2.0) / 60.0;
var secX = secLen * Math.cos(seconds * secondAngleUnit + K) + clockCenterX;
var secY = secLen * Math.sin(seconds * secondAngleUnit + K) + clockCenterY;
g.strokeStyle = '#0000ff';
<canvas id="myCanvas" width="400" height="400" style="border:1px solid black">
Your browser does not support the HTML5 canvas tag.
When you start coding with the canvas tag, you need to include the tag in the
body of the document. In the example below, the canvas tag is 400 x 400
pixels. We have assigned a name of “myCanvas” to the element.
The name of the canvas is important so that we can reference this element
On the body tag, we are using the “onload” event to trigger a function
we created called “bodyOnLoad.” This function is responsible for
starting our clock and drawing it.
In the following code sample, focus your attention on the 4 elements
required to perform simple animation. You need to get access to the drawing
context of the canvas. (i.e variable g ) . You need to clear to the screen.
In the final code sample, you can see that I used a rectangle. After clearing
the screen, you are free to draw anything else that you like. The final line
of code in “drawClock” schedules the function to be called again in one second.
// Access the drawing context for the canvas tag.
// Clear the screen
// Draw stuff
// Do this process again in 1 second
The following code draws a black rectangle over the canvas.
The rectangle is positioned on the upper left corner of the canvas.
(i.e. Point 0,0) In the canvas tag, the X-axis increases from left to right.
The Y-axis increases from top to bottom. This is backwards from standard
cartesian coordinate systems. 🙂
Code to draw the circle of the clock using the “arc” function. We had to
specify the center of the circle, radius, a start angle in radians, and
end angle in radians.
g.strokeStyle = 'yellow';
g.arc(clockCenterX,clockCenterY,clockWidth/2 * 0.9, 0, 2*Math.PI);
The following shows how we draw one hand of the clock. The line is colored
red. We move to the center of the clock. We draw a line outward to point
(hourX, hourY) To learn more about the polar coordinate math,
please check out the following link.
g.strokeStyle = '#ff0000';
To learn more about the canvas tag and discover more tutorials on this
browser feature, check out the following resource from W3Schools.com
Photo by Randi Boice.