Parallax Scrolling with the Canvas Element

Written by:  • Edited by: Michele McDonough
Updated Nov 9, 2010
• Related Guides: Javascript | Canvas Element

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.

Parallax Scrolling

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

Parallax 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 extensively 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 progressively 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.

Demo Screenshot

Additional Resources

SDL SDL Game Programming Tutorials

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


Comments

Showing all 13 comments
 
Kyle Richards Sep 23, 2011 1:45 PM
Sooner scroll view
Can anyone tell me how to edit the code so it scrolls sooner? So you can see whats in front of you.

Thanks everyone!

ps. Great tut!!
irfi Sep 20, 2011 10:31 AM
GameObject
What is GameObject.I mean can you upload gameobject code also. and can you tell me if there is IDE for developing html5. I mean where can I start the html5 coding
Anonymous Sep 8, 2011 2:19 AM
thanks for tutorial
but from 3 to 4 , the change is huge
Would you please explain more about this section?
Lark Jun 14, 2011 3:08 PM
Update function
Why is the update function suddenly in the ApplicationManager and not in the RepeatingGameObject?

Thanks.
RiPr Jan 21, 2011 2:16 AM
INDEX_SIZE_ERR: DOM Exception 1
Nice tut, but there seems to appear an error (also in your demo).
When the background image should repeat the first time, there appears an "INDEX_SIZE_ERR: DOM Exception 1".
Seems like some var having a negative value instead of positive one.
Adam Carlson Jan 3, 2011 10:25 PM
Amazing tutorial.
You have done great with explaining good ways to use JavaScript and canvas together. I enjoyed the simplicity and the structure of your guide and I hope you will pull out some more in the future. Thank you very much. Why yes the parallax was a bit hard on older machines it still looked very nice and well done.
Markus Aug 24, 2010 7:59 PM
80% processor usage on a 2GHz dual core
While I like most of the tutorial, the scrolling example takes an insane amount of processor power. I can't use that for a real game, as nice as it looks.
BK Jul 20, 2010 8:01 PM
Re: parallax runs hot
When I load the page in Firefox 3.5.1 it takes about 60% usage, and my desktop is 8yrs old. I loaded it in Safari 4.0.5 and it was bouncing from 45 to 85% usage and kept lagging, I do not think Safari handles the script as cleanly as Firefox.
Ritin Pali Jul 20, 2010 7:07 AM
Nice Tutorial Though
:-)
Ritin Pali Jul 20, 2010 7:05 AM
CPU usage 100%
on an old computer, AMD64-3000+, XP64, Firefox 3.5.7
1GB Ram

CPU usage 100%
it hangs Firefox
Tyler Feb 20, 2010 7:15 PM
"Javascript Feelings"
I'm working on a series of "Javascript Feelings" which attempt to explain complex human emotions using Javascript. I've used the parallax effect in my first piece entitled "Intimacy With Strangers."

http://verytogether.com/lifestyle/play/javascript-feelings-intimacy-with-strangers.html
Chris Sep 10, 2009 12:35 PM
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!
Roger Purves Jul 10, 2009 1:29 AM
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

 
blog comments powered by Disqus
Email to a friend