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