Parallax scrolling with the canvas element

Article by Matthew Casperson (4,911 pts )
Edited & published by Lucinda Watrous (19,577 pts ) on Dec 15, 2009

Parallax scrolling is a technique that gives the illusion of depth to a 2D application. See how easily it can be implemented building off the framework that we have just created.

Now that we have the beginnings of a game framework, lets do something fun with the canvas element.

Paralax scrolling is a term used to describe a technique where several layers are transposed on top of each other, with those in the background scrolling more slowly then those in front. This creates the illusion of depth, and is used extensivly in 2D games.

RepeatingGameObject.js

The RepeatingGameObject class allows an image to be repeated and scrolled inside a defined area. Up until now we have been drawing an image in it's entirety. RepeatingGameObjectis different in that it takes an image and draws it such that it fills up an area that has been defined (the dimensions of which are independent of that image that is being drawn). We will use this to take a large image (like a panoramic mountain view) and display just a small section of it at a time to create a background image.

You may have noticed the xScroll and yScroll properties of the GameObjectManager, which are passed to the draw and update functions on the GameObjects. These values define how much the camera has been moved along the x and y axes. The RepeatingGameObject class uses these values to scroll the texture they display, creating the illusion of movement.

First we need to define the area that the RepeatingGameObject will draw to. The x and y properties of the underlying GameObject class define the top left position, while the new width and height properties define the size of the drawing area. [code]

The scrollFactor property is used to change the amount of scrolling that is reflected in the RepeatingGameObject object in regards to the xScroll and yScroll values passed to the draw function. By setting scrollFactor to a value less than one we get slower movement, and thus the illusion of a more distant object. [code]

The draw and drawRepeat functions do the hard work of actually rendering the tiled and offset texture. [code]

ApplicationManager.js

Here we use the ApplicationManager to create three new instances of the RepeatingGameObject class. Each is used to display a new parallax layer, using the z (depth) and scrollFactor values to create progressivly deeper and slower moving RepeatingGameObject instances.

The end result is quite convincing. The parallax scrolling gives a nice sense of depth, and the whole effect has been achieved with one additional class.

Check out the demo here, download the source code here, and browse the code documentation here.

Read more in the Game Development with JavaScript and the Canvas element series.

Images

Demo Screenshot

Related Articles

SDLSDL Game Programming Tutorials

This tutorial series shows you how to make an old school shoot'em'up with C++ and SDL.

Comments

Sep 10, 2009 12:35 PM
Chris
Thanks
I rewrote my c++ code of a game to javascript so I could put it online. This newly discovered 'canvas' was exactly what I needed to replace my feeble idea of using table cells for a grid.

Thanks for the very easy to follow tutorial!
Jul 10, 2009 1:29 AM
Roger Purves
parallax runs hot
to Matthew:

Thank you for the very helpful tutorial.

In Safari 3.1 on my 1 1/2 year old MacBook
(2.4 GHz Intel Core 2 Duo with 2GB ram)
the parallax scrolling demo really turns the
fans on. The activity monitor shows that
Safari is using a lot of the CPU-- much
much more than normal. Is this to be expected?

Roger Purves

 
Subscribe to Web Development
RSS
Get free weekly updates, directly to your inbox.
Subscribe
Browse Web Development