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>

 

 

 

 

 

 

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.

Email: 

Stand Out - from the crowd

students

"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.

Advertisements

Advertisement