Advanced Image Manipulations with the Canvas Element

Written by:  • Edited by: Michele McDonough
Updated May 22, 2010
• Related Guides: HTML | Javascript | Canvas Element

Learn how to manipulate images in ways that were not previously possible using JavaScript and the canvas element.

The last article demonstrated a basic implementation of the canvas element where an image was moved across the screen. While it was a simple example, the same effect could just as easily been achieved by modifying the properties of a handful of standard HTML elements like an IMG or DIV. Here we will tap into some of the more advanced functions of the canvas element to demonstrate some of its real power.

jsplatformer2.html

The only change made to the HTML file is the addition of a few buttons. When clicked these button set the currentFunction variable (described later), changing which function is run during the render loop.

jsplatformer2.js

As before, a number of global variables have been defined in the JavaScript file. [code]

FPS The target frames per second

SECONDSBETWEENFRAMES The number of seconds between each frame (which is the inverse of the FPS value)

HALFIMAGEDIMENSION Half of the width / height of the image we are drawing, which is used to position the image in the center of the canvas

HALFCANVASWIDTH Half of the width of the canvas, used in conjunction with HALFIMAGEDIMENSION to center the image in the canvas

HALFCANVASHEIGHT Half of the height of the canvas, used in conjunction with HALFIMAGEDIMENSION to center the image in the canvas

currentFunction The function to be run during the render loop

currentTime The time in seconds that the application has been running

sineWave A value between 0 and 1, used to animate the image

image The image to be drawn to the canvas

canvas A reference to the canvas element

context2D A reference to the 2D context of the canvas element

And, just like before, the init function is set to run once the window is loaded (see the last article for a description of the init function). [code]

Showing page 1 of 3

Comments

Showing all 3 comments
 
Anonymous Feb 3, 2011 5:17 AM
can't get it to work!
has anyone got the demos to work
Ark Aug 29, 2010 5:45 PM
Good Tutorial...
But I don't understand one thing. Why do you have to call context2D.restore()? Why would you want to discard whatever changes were made instantly after performing those changes? In which case, why does the image actually successfully distort itself then if this is being called at the end of draw() every frame?
Kunjan Aug 8, 2010 2:53 PM
Amazing
Thank you for these amazing tips. I have never done any forms of animation or game design and I learned a lot form this tutorial.

I want to share the biggest lessons that I learned from this post:

1. Using Standard functions like Sine for generating numbers

2. Animation Abstraction. e.g transforming an image could yield pendulum behavior

3. Cumulative vs Context Saving in Animation

4. JavaScript's first class function's usage eg. currentfunction = scale and applying them

5 Using Frame Per Seconds and keeping track of the state using this parameter.

6 Importance of translating before scaling

Thank you very much.
 
blog comments powered by Disqus
Email to a friend