Tags: %TAGME{ tpaction="" web="Main" tag="" }% view all tags

Introduction to Animation with HTML5 and JavaScript

Moodle Link

  • HTML5 is the latest language for building web pages.
  • HTML5 has a feature called the <canvas> element that allows graphics and animation.
  • Using a programming language called JavaScript, web developers can create graphics, static and moving.
  • Here is an example of a simple animation using HTML5: Bouncing Ball Example
  • Here is a tutorial on using HTML5 with Canvas: Canvas Tutorial
  • HTML5 Canvas Reference

Step 1 - Draw a ball

  • First we need to create a canvas to draw on.
  • The following code creates a area to "draw" on that is 300 pixels wide, and 300 pixels high:
<!DOCTYPE html>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>Kenyon Canvas Example</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <canvas id="myCanvas" width="300" height="300" >
    <script src="script.js"></script>

  • Now we can write code to draw something on this canvas:
<!DOCTYPE html>

var context;
function init()
    context= myCanvas.getContext('2d');
    // fill objects with blue color
    // Draws a circle of radius 20 at the coordinates 100,100 on the canvas
    context.arc(100,100,20,0,Math.PI*2,true); context.closePath();


Step 2 - Move the ball

  • Now that we have the circle, let’s try to move it. We’ll replace the hardcoded values of the coordinates in the .arc method (100, 100 — the first two arguments) with variables xand y, which we will then increment by an amount of dx and dy.
  • Also since we need to redraw the circle at the new positions, we’ll move the code into a function called draw() and call it every 10ms using JavaScript’s setInterval()function.
  • Try the code below
var context;
var x=50; 
var y=50; 
var dx=5; 
var dy=5;

function init()
  context= myCanvas.getContext('2d');
function draw()
  // Draws a circle of radius 20 at the coordinates 100,100 on the canvas


We have a problem! The circle is actually forming a line! Why? How do we fix this?

Step 3 - Erase the old circles!

  • The balls merged into a line because each time the draw() function is called, it draws a circle at the new coordinates without removing the old ones.
  • To erase the old circles, we’ll need to call the clearRect method right at the start of our draw()function so that it clears out the previous circle before it draws the new one. Add the line below at the beginning of the draw function:
context.clearRect(0,0, 300,300);
  • Try it out!
  • We stil have a problem, what is it?
  • What soluton can we try?

Step 4 - bounce the ball inside imaginary walls

  • All you need to do is check if the values of x and y are beyond the canvas dimensions, and if so, we need to reverse the direction by setting values of dx and dy to the negative values.
  • Add the following code to bounce the ball:
// Boundary Logic
  if( x<0 || x>300) dx=-dx;
  if( y<0 || y>300) dy=-dy;

  • Notice how the balls goes corner to corner. How can we fix that?
  • Let's give the ball a random start point. Let's start by giving the height and width a name for a better program. Then use it to compute a random location at the beginning of the code:
var height=300, width=300; var x=Math.random() * 300; var y=Math.random() * 300;

  • Let's also now use the height and width in the clearRect function:
context.clearRect(0,0, height, width);

  • Next note the ball is going off the edge. How do we fix this? Let's start by making a variable for the radius, and then using that.
var radius=20;

Step 5 - Have fun! Add some features!

Can you try to add some more features?

  • Make the box bigger.
  • Change the color of the ball whenever it hits the wall.
  • More than one ball.
  • Simulate the effects of gravity and friction (the ball's height decreases with each bounce, and the side to side motion slows down)
  • Whatever you can think of!
Random color function:
function getRandomColor() {
  var letters = '0123456789ABCDEF';
  var color = '#';
  for (var i = 0; i < 6; i++) {
    color += letters[Math.floor(Math.random() * 16)];
  return color;
Edit | Attach | Watch | Print version | History: r5 < r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r5 - 2020-04-28 - JimSkon
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback