Book Image

Canvas Cookbook

Book Image

Canvas Cookbook

Overview of this book

With the growing popularity of HTML5 Canvas, this book offers tailored recipes to help you develop portable applications, presentations, and games. The recipes are simple yet creative and build on each other. At every step, the book inspires the reader to develop his/her own recipe. From basic to advanced, every aspect of Canvas API has been covered to guide readers to develop their own application, presentation, or game.
Table of Contents (16 chapters)
Canvas Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Creating an animation class


As per Wikipedia, animation is the process of creating the illusion of motion and shape change by means of the rapid display of a sequence of static images that minimally differ from each other. So we draw a canvas, clear it, then draw it again with minimal changes, again clear it, again draw it with some more changes, again clear it, and this cycle continues.

So the steps for animation can be given as:

  1. Initialize.

  2. Clear the canvas.

  3. Draw on the canvas.

  4. Update the drawing with minimum changes.

  5. Perform the previous three steps (2, 3 4) in a loop.

These steps need to be called rapidly, and for this we use a function named requireanimationframe (callback).

This method tells the browser that you wish to perform an animation and requests that the browser calls a specified function to update an animation before the next repaint (redraw). The method takes as an argument a callback method to be invoked before the repaint (redraw).

As browsers and computer hardware are not created...