Programming Simple Animations Using HTML, Canvas, and JavaScript


Mario Drawn Using HTML and JavaScript

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!

Check out some awesome visualizations and gameful experiences using HTML and JavaScript.

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.

  • Code Academy – I would focus your attention on learning JavaScript and HTML.
  • Khan Academy for Computer Science – –  They have created an amazing collection of learning tutorials.
  • – This is a great FREE book helping to introduce JavaScript.   While it does not cover any “canvas” concepts, it provides a good reference for the basic and advanced features of the language.

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.

[cc lang=”html”]

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
in JavaScript later.

[cc lang=”html”]

Your browser does not support the HTML5 canvas tag.


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.

[cc lang=”html”]
function bodyOnLoad(){

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.

[cc lang=”js”]
function drawClock()
// Access the drawing context for the canvas tag.
var c=document.getElementById(“myCanvas”);
var g=c.getContext(“2d”);

// Clear the screen

// Draw stuff

// Do this process again in 1 second
setTimeout(drawClock, 1000);

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. 🙂

[cc lang=”js”]

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.

[cc lang=”js”]
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.

[cc lang=”js”]
g.strokeStyle = ‘#ff0000’;
g.moveTo(clockCenterX, clockCenterY);
g.lineTo(hourX, hourY);

To learn more about the canvas tag and discover more tutorials on this
browser feature, check out the following resource from

Photo by Randi Boice.


Related Posts:

This entry was posted in Uncategorized. Bookmark the permalink.