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




Recommended Reading

Amazon Best Seller


Everything is evolving; so is the layout of a text book. By uniquely presenting the rich contents of the book, the author has elevated positive user experience of reading a text book to a new level: the examples are easy to follow and rich in standard. Highly recommended for those who want to master JavaScript and JQuery.

Progressive Web Apps(PWA)


The significance of app stores is over; progressive web apps is the next big thing. They are just websites that makes the need of going through app stores and need of storing redundant. They work offline too. If you have a reasonable understanding of HTML, CSS and JavaScript, this is the book for you to learn in no time.

HTML5 Canvas Animations


David Geary, in this book, shows how to combine JavaScript and HTML5 Canvas to produce amazing animations; he has set aside a whole chapter to teach you the role of physics in animations. If you want an in-depth understanding about HTML5 Canvas animations, this is a must read.