Creating 2-D Animations on HTML5 Canvas - rolling panorama

In the code that is given below, the following points are highlighted:

  • Image declaration
  • Declaration of left-most value of the moving image on the canvas
  • The function that draws the image on the canvas
  • The Windows function that calls the drawing function every 60 milliseconds
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:

 

Rolling Panorama - the Knuckles Mountain Range, Sri Lanka - a UNESCO World Heritage Site

In order to make the image move, the width of the canvas element must always be kept below that of the image. Moreover, the two ends of the image, have to be almost identical in order to be appeared seamless throughout the animation.

The Code for the animation is as follows:

 

<script type = 'text/javascript' >
// declare the leftmost coordinate of the image position on canvas
var x = -1;
//width of the image
var imgWidth = 800;
var canvas = document.getElementById('Canvas_Six');
var context = canvas.getContext('2d');
//declare the image object
var imageObj = new Image();
//the function that draws the image on the canvas
function draw() {
x = x + 1;
if (x == imgWidth) { x = -1; };
context.drawImage(imageObj, x, 0);
context.drawImage(imageObj, x - 800, 0);
imageObj.src = '../images/knuckles.jpg';
};
//call setInterval function every 60 millisecond
window.setInterval('draw()', 60);
</script>

 

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.