Creating 2-D Animations on Canvas

In this animation, you will:

  • Move a balloon and its reflection across a background of floating hot air balloons.
  • Make the movement smooth by using the timer functions.
  • Make them move forward and backward seamlessly.
Finding a good book to master HTML5 can be very challenging: there are so many around - most with eye-catching titles and very complex substance.
Therefore, Vivax Solutions strongly recommends Core HTML5 Canvas for those who really want to delve into HTML5.
Please click the image to access Amazon:

The flight of hot air balloon

In this example, the background is an image made by using CSS in canvas element.

The balloon and its reflection are produced by two images.

The images are moved across the background by using JavaScript.

The Code for the Flight of Hot Air Balloon is as follows:

<script type="text/javascript">
var canvas = document.getElementById('Canvas_T');
var context = canvas.getContext('2d');
// image of the top balloon
var imgObj1 = new Image();
// image of the second balloon
var imgObj2 = new Image();
var i = 0; var j = 1; var x; var y;
// function for the movement
function circ() {
i = i + j;
if (i == 400) { j = -1; }
if (i == 0) { j = 1; }
context.clearRect(0, 0, 500, 400);
x = i;
y = Math.round(100 + 15 * Math.sin(i * Math.PI / 180));
context.drawImage(imgObj1, x, y);
imgObj1.src = '../images/hot.png';
context.drawImage(imgObj2, x, y + 180);
imgObj2.src = '../images/hot1.png';







Resources at Fingertips

There is a significant selection of tutorials here, covering ASP.Net, HTML5, CSS3 and JavaScript. They are categorized clearly for you to access them easily on any device - desktops/laptops, smartphones and laptops.
There are quite a few of them on HTML5 Canvas and CSS3. In addition, there are tutorials on JavaScript and ASP.Net too.


Stand Out - from the crowd


"There's no such thing as a free lunch."

The best things in nature are free with no strings attached - fresh air, breathtakingly warm sunshine, scene of meadow on the horizon...

Vivax Solutions, while mimicking nature, offers a huge set of tutorials along with interactive tools for free.

Please use them and excel in the sphere of science education.

Everything is free; not even registration is required.



Recommended Reading


The best book to master HTML5 canvas animations. The author tells you how to manipulate the canvas element with JavaScript in a progressive way - starting from the easier to harder worked examples. It is ideal for anyone who wants to be a game developer in HTML5.