HTML5 Canvas with Dave Shea
HTML5 Canvas with Dave Shea
One of the great things about living in Vancouver is the abundance of tech talent. Last night, for instance, CSS Zen Garden creator/author, Dave Shea, shared his thoughts and experiences with HTML5 Canvas, at an HTML5 Meetup. Dave also introduced us to his own canvas project, Paintbrush JS. Modernizr creator, Faruk Ates, also gave a brief intro to his project, which I first discussed here.
[Above: Photo by Boris Mann. More pics here.]
Dave pointed out that there are a lot of similarities between Canvas and Processing, a Java-based, open-source language, originally developed to teach the basics of programming.
The five demos he showed us:
- the basic balls chasing the cursor.
- Paintbrush JS – a JS library that allows you to do photoshop-like effects, real-time in the browser.
- interacting with video.
- grabbing data from Google street maps, then applying real-time, 3D stereographic effects to images of buildings in the area. *if anyone has the link to this, please share it in the comments.
- Using Microsoft Kinect with HTML5 Canvas to animate a human model. *if anyone has the link to this, please share it in the comments.
Canvas is not CSS3, but it is a general purpose drawing region in an HTML5 element. It allows pixel oriented, bitmap drawing. It is, pretty much, all JS. Canvas was introduced by Apple in 2005, but is now part of HTML5.
Within an HTML5 web page, the following code will give you a canvas area. The fallback content within the paragraph tags is not strictly necessary – it's just a way of providing an explanation for people using browsers that do not support canvas.
<canvas id=”Name-your-canvas” width=”500” height=”200”>
<p> fallback content </p>
- basically, 2D.
“Well,” says Dave, “We don't really know yet!”
It's good for:
- replacing flash content - but, it's not vectors. Once a Canvas object is plotted, it cannot be manipulated in the same way vectors can.
- charts, graphs and live data,
- generated graphics,
- pixel manipulation,
- basic animation - but, not for games.
Canvas is widely implemented, and has more visibility than SVG. Additionally, there are workarounds for using Canvas with older browsers. While SVG and Canvas do overlap, they are not complementary. You will be using either one or the other. The main technical difference between them is that SVG is DOM-based while Canvas is procedural. The chart above is from http://sitepoint.com/how-to-choose-between-canvas-and-svg. Visit the web page for more information.
Canvas and Internet Explorer
Because of Canvas's frame by frame drawing, it is a CPU hog. It can be fast on hardware accelerated desktops, but is slow on mobile. There are a few basic API's for creating interaction with Canvas: PaperJS is one; MooTools has a library - but essentially, you need to write your own event handlers.
Getting Vectors into Canvas
Is tricky! Opacity, a sort of “Illustrator light” program, exports to native Canvas, while Adobe CS5 and above provide some support.
The best solution remains exporting your vector art to SVG, then from SVG to Canvas. Another tool for getting vector art into Canvas is SVGCanvas. Lastly, here's Dave's blog post on moving artwork from Adobe Illustrator to Canvas.
Canvas is more pixel friendly than SVG. It gives you the ability to adjust hue, colour, saturation and other Photoshop-like settings to images right in the browser. Pixastic.com, like Dave's Paintbrush JS is a JS library that utilizes Canvas to apply a multitude of Photoshop-like filters and effects to browser images, in realtime.
Here are a few terms from Canvas' code base:
- “Radians,” not “degrees” to rotate elements.
- “Functions” group code for specific elements.
- “Compositing” modes are like Photoshop's “screen, lighten, multiply, etc.” blending options for layers.
- “Saving” and “restoring” contexts are used for animation.