An Introduction to the Canvas Element

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

The canvas element, along with some advancements in JavaScript engines, gives web developers the opportunity to create detailed and interactive 2D HTML web pages without any 3rd party plugins. This article introduces you to the canvas element, and describes some of its potential.

JavaScript and the Canvas Element

HTML started life as a way to format a static page. Animated GIF images and flashing text was about as dynamic as HTML could be. JavaScript changed all that by allowing web pages to be dynamically modified, and many web servies now make use of AJAX to create web pages that give the user a more seamless experience than the standard "click, reload page, click" procedure that is so often involved with standard HTML pages.

Unfortunately JavaScript is somewhat limited by the functionality of the web browser that hosts it. While you can create and modify any element in a web page, JavaScript doesn't (easily) allow the host web browser to display a new type of object. JavaScript can modify some text, insert an image or resize a table with ease because these element are already supported in HTML. But what if you want to do something fancy, like create a game? At best you'd be looking at a frustrating time spent trying to manipulate standard HTML elements in ways that they were never meant to be used. Or, you would use a plugin like Flash or Silverlight.

Enter the canvas element. This new HTML element provides a way for JavaScript developers to paint directly onto a web page, without the need for an additional plugin. It was first introduced by Apple in its WebKit framework, and used by the Safari web browser and Dashboard widgets. The canvas element is currently being proposed as part of HTML 5, and is supported now by most web browsers like Chrome, Firefox, Opera and Konqueror. Internet Explorer (at least up to version 8) is the notable exception, although projects like ExplorerCanvas do provide some of the functionality of the canvas element for IE.

Anyone who has done 2D graphics programming will find the canvas element quite easy to use. You can draw lines, shapes, gradients and even modify individual pixels using functions that are very similar to most other 2D API's. And thanks to the performance of the latest JavaScript engines like Chrome's V8, Firefox's SpiderMonkey and Safari's Nitro creating detailed and interactive web applications is quite feasible.

This series will show you how to create a simple platformer game using JavaScript and the canvas element. It will cover topics like animation, resource loading, layered rendering, scrolling and interaction. With step by step examples with code and live demos for you to play with you'll be able to master the power of the canvas element for yourself.

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

Next Article in Series

JavaScript Canvas Demo
click to enlarge
Read on to Drawing an Image to the Canvas.

See how to draw to and animate the canvas element, with step by step example code and live demo application.

Additional Resource

Widgetpad Logo
click to enlarge

You might also be interested in WidgetPad - Mobile development for web developers.

WidgetPad aims to give web developers a platform on which to build cross platform mobile apps. Read on to see how it stacks up.


Comments

Showing all 11 comments
 
Steve Jun 24, 2011 11:42 AM
JavaScript Game Tutorials
Thanks very much for sharing your knowledge.
Pratixha Dec 27, 2010 1:33 AM
HTML 5 opportunities
Hey this is really great.

We are currently looking for HTML 5 developers or people with JS background interested in learning HTML 5.
The opening is with a gaming studio in Mumbai, India.

If anyone is interested, please revert with your CVs/Contact details on pratixha@bluesky-hr.com
eric blair Aug 1, 2010 9:22 AM
eric
Thanks for the tutorial, but as you well know, his is far from a finished game. Where is your events manager? Your sound framework? etc... I think you should be more honest about the limitations of js...
Karl May 2, 2010 9:33 PM
Flash is Dead and IPads, Ipods etc.
I develop websites for small businesses with fairly basic functionality. Many clients request flash that we use intermittently and my technical knowledge is incredibly limited.
With incredibly increased usage of Iphones etc where do I find a developer that can assist with my canvas development requirements?
Yair Jan 13, 2010 12:35 PM
Open Source Canvas for Creating Collages
Hi,

I am looking for an open source Javascript canvas allowing users to create collages from images they either upload or get online.
The canvas should allow resizing, flipping, rotating images and then save whatever is on the canvas (and also re-edit later)
I am still learning the <canvas> tag so not sure this will work. I need it to work on FF, IE, Safari, and Chrome
Can someone recommend a good starting point?
Rob Nov 10, 2009 3:28 PM
Origin of Canvas
@Shig
WebKit was a KHTML derivative which was indeed created by Apple. KDE and other open source groups have since made contributions to WebKit, but it was originally released by Apple as an Open Source renderer for Safari. Canvas was also created by Apple for use in OSX Dashboard and and has since been adopted by other open source HTML engines.

While WebKit was based on an existing open source project it was a substantial enough change that it created a whole separate fork of KHTML, so to say they "didn't write most of it" is very misleading.

Also, to claim the same for OSX is just plain disingenuous. The OSX kernal is based on OpenBSD but there is so much to a modern OS that claiming they're just rebranding OpenBSD is outright wrong - the Aqua windowing environment alone is significant enough to discredit such a statement.

Back to the point, this is a great tutorial! Thanks!
Shig Oct 19, 2009 11:11 AM
WebKit
Nice tutorial.
Webkit started as part of KDE. Like OS X, Apple didn't write most of it, instead opting to take open-source code and rebrand it as their own.
Matthew Casperson Sep 23, 2009 7:45 PM
Animations
Drawing animations from a single sprite sheet is covered in this article: http://www.brighthub.com/internet/web-development/articles/40515.aspx
Leo Aug 20, 2009 12:36 PM
regard
Is it possible select a specific area of pixels from a source image so that you could, for example, create an animation using a single sprite sheet image, or is it necessary that each frame be divided into separate images?
vbccx Jul 22, 2009 7:19 PM
Game
Impossible to win!
Thom Parkin Jul 2, 2009 10:14 PM
Javascript for 3D games (on the web and off)
Extremely interesting and well presented details of the Canvas object!
You can apply your Javascript skills to fully-interactive 3D games with a terrific product called DXStudio.
There is a link on www.DXCreations.com along with some examples - all completely in Javascript.
 
blog comments powered by Disqus
Email to a friend