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';


All Canvas Animations