CHAPTER 9

image

The Truth About HTML5’s Canvas, Gaming, and Flash

Canvas lets us draw programmatically on a specific area of a web page. And it can do some pretty cool things: design enhancements, visualizations, drawing/painting applications, image manipulation, and games (the visual side, at least).

Later we’ll get into the nuts and bolts of Canvas. But first let’s look at some big-picture stuff and the inevitable comparisons to Flash.

Canvas is not Flash, and comparing a single piece of web technology for (primarily) 2D graphics to a feature-rich, broadly supported client environment and a mature ecosystem of development tools is a bit apples and oranges. Canvas alongside HTML5 (and related) technologies, however, is being touted as a serious Flash competitor. But exploring Canvas (and HTML5 on the whole) made me wish I could do some of the amazing things being done with Flash. This isn’t a popular opinion—I’m writing about web standards, so shouldn’t I hate Flash? It’s also not one I thought I’d have; I haven’t used Flash for a decade, and as a stereotypical designer/Apple fan (tautology?) I regularly experience the memory-hogging, crash-tastic, resource-consuming, pain in the ass that Flash is on the Mac (and apply Flashblock liberally).

And while HTML5 has come a long way, Canvas and WebGL still haven’t replaced Flash. Support for <video> is letting many sites integrate interesting animated backgrounds and the popularity of scroll effects is making them feel more interactive, but for something truly immersive Flash still feels like the only game in town. Don’t believe me? Go to any modern video game or movie site. Nine of out ten of them are still built in, you guessed it, Adobe Flash. That’s not to say things aren’t changing, though, and Adobe themselves have started to build Canvas export features into Flash Professional. We’ll consider some examples and pit these technologies head to head in the rest of the chapter.

Flash Is Dying, and HTML5 Is All We’ve Got

Nevertheless, it’s hard to escape the fact that Flash is dying.

As you know, there’s no Flash on iOS devices, as has been widely discussed. (See the April 2010 piece “Thoughts on Flash” by the late Steve Jobs for why Apple chose not to support Flash: www.apple.com/hotnews/thoughts-on-flash/.)

But there won’t be any Flash plug-in on future Android devices, either. In November 2011, Adobe announced it was discontinuing support for the Flash plug-in on mobile devices altogether (see  http://blogs.adobe.com/conversations/2011/11/flash-focus.html), shifting its focus to Flash-driven native apps through Adobe AIR and, ultimately, HTML5.

Windows Phone has also never supported the Flash plug-in.

The absence of Flash on iOS devices (at a minimum) made offering a mobile, HTML-only version of a site a necessity for most sites. And if you are going to build an HTML version of your site anyway, you need a pretty good reason to build an additional Flash-based desktop site. But the justification could still be made in some cases—a simple HTML site for mobile and a rich Flash site for desktop, perhaps.

Then Microsoft dropped a bombshell.

In September 2011, Microsoft announced that for Windows 8, the nonlegacy version of IE10 would not support any plug-ins (http://blogs.msdn.com/b/b8/archive/2011/09/14/metro-style-browsing-and-plug-in-free-html5.aspx). No Flash, no Silverlight, nothing. Flash for mobile browsers was a nonstarter, but this announcement from Microsoft signaled the beginning of the end of Flash on the desktop, too.

The writing for Flash was on the wall. The default experience of the flagship operating system that will run on hundreds of millions of the world’s desktops wouldn’t support Flash (or any other plug-in technology).

Shortly after making the announcement, Microsoft reversed its decision and decided to keep on supporting Flash, but the damage was done. Like many “dying” and “dead” technologies before it, Flash continues to exist comfortably long after passing from vogue, and a company as large and widespread as Microsoft can’t stop supporting it so quickly. At the same time, Adobe has drastically reduced the size of the Flash development team, moved it all off-shore, and doesn’t seem to be investing resources in extending the platform. Flash, the plug-in, is still dying, though it may be a while before it’s finally dead.

Can Canvas and HTML5 Fill the Gap?

At some point in the future, advanced graphics and audio won’t be delivered through a plug-in like Flash. Already each new year reveals more and more people who connect to the Web primarily or exclusively through post-PC devices such as phones and tablets, where Flash doesn’t exist. Consider the implications:

  • Advertising: Site owners and advertisers can’t sit idly by as their ad revenue and click-through rates fall, and they’re not going to go back to animated GIFs. There’s going to be a huge shift to HTML5-powered (and mobile-viewable) banner ads, and Canvas will have a big role to play there.
  • Media delivery: Audio and video content will need to be delivered using HTML5, but as we’ll see in the next chapter, that’s not as straightforward as it sounds.
  • Games: HTML5 games will also rely heavily on Canvas, as we’ll explore later in this chapter.
  • Sites: Finally, there’s going to be a massive number of legacy Flash sites that simply can’t continue to work in their current Flash-powered form. Restaurant sites, I’m looking at you. (There’s a niche in converting Flash-based restaurant sites waiting there for someone!) Sadly, this also means great interactive sites like the Lego Star Wars III site will need to be rebuilt, but ideally this encourages enterprising designers and developers to push web standards to their absolute limits to enable the kind of incredible experiences Flash has been delivering for years to a post-Flash world.

Flash Authoring for HTML5?

For a while, Adobe played with the idea of using its Flash authoring platform as a way to generate HTML5 content. Adobe introduced this idea in 2009, when it demoed such functionality at the MAX conference (see the demo at http://youtu.be/v69S22ZBBqA), and again in early 2012 with an announcement of an extension Adobe was working on called “The Adobe Flash Professional Toolkit for CreateJS” (see the announcement and a video of it in action here: http://blogs.adobe.com/creativelayer/html5-flash-professional/). (Erroneous reports made the rounds in mid-2010 that Flash CS5 would export to Canvas, based on the MAX 2009 video resurfacing, but that was not the case.) The latest demo shows a simple animation making the jump from Flash to Canvas and playing smoothly on a PC and iPad, but whether this tool will be sophisticated enough for most web professionals (when it’s eventually released) remains to be seen.

Just a year later, however, Flash as an HTML5 IDE started looking less and less like a real part of Adobe’s plans. In 2010 Adobe also released an experimental FLA-to-HTML tool called Wallaby [http://labs.adobe.com/technologies/wallaby/] that relies heavily on SVG and WebKit-only CSS3. Wallaby was then rolled into a new JavaScript creation platform called “CreateJS”, sold as a “suite of JavaScript libraries and tools for building rich, interactive experiences in HTML5”, which Adobe still supports and promotes at the time of this writing (http://www.createjs.com/).

Then in 2011 Adobe released a preview of new product they were calling “Edge” (http://labs.adobe.com/technologies/edge/), a simple HTML animation tool that relied mostly on JavaScript, despite “HTML5” claims. It allowed users to make complex animations and tweens, similar to those from Flash, but using 100% “open web” technology. At the time of this writing, Edge is now a full product line from lAdobe, and includes things such as an animation application (the original “Edge”, now Edge Animate, and still no Canvas support), a coding environment,  a design tool (Reflow), typography tools, and PhoneGap, which Adobe acquired in October 2011.

At the time of this writing, Adobe have just announced an update to their Flash Professional CC line, which adds support for exporting animations to Canvas. This is a big step, and something that has stirred up quite a bit of interest. Could this be the Canvas animation tool that designers and developers have been clamoring for? At this point, we’ll just have to wait and see.

And Then Apps Happened

Because of the immaturity of some important HTML5 features (especially audio and video, as we’ll see in the next chapter) and the lack of mature design tools for things such as Canvas, some site owners may have no choice but to stick with Flash over the next few years, especially as a legacy technology. Or, they’ll start pushing apps.

We’re entering a strange period for the Web.

On the one hand, as far as technology goes, web standards have won, and won handsomely. They have persevered in the face of numerous challenges from proprietary technology (Flash, Silverlight, and many others that have fallen by the wayside), implementer apathy (for example, Microsoft letting IE stagnate in the ’00s), and specification dead ends (the W3C’s XHTML 2 debacle). For web purists, this is an incredible victory that seemed anything but certain several years ago. After all, Microsoft—Microsoft!— tried releasing a desktop version of its browser that only supported web standards and has not only joined the party in implementing cutting-edge web technology but is doing it particularly well.

On the other hand, the web standards we’ve got with HTML5, and the associated development tools, are still not very good. Canvas, as we’ll see, has its uses, but Canvas, and HTML5 in the broadest sense, still has a long way to go in development, adoption, and tool set before it can rival what can be done with Flash today. So, what will happen when developers want to do something cool but they can’t do it in the browser?

They’ll create apps.

iOS apps. Android apps. Windows apps. Mac apps. Platform-specific apps that, ironically, take us away from the true promise of the Web—that it’s available to all, from any platform. We saw an explosion in platform-specific software in the ’90s as desktop PCs became common, and then web apps started to emerge in the ’00s—software that was liberated from any specific platform vendor. Now we have a fierce competitive environment where major platform vendors (that is, Apple, Google, and Microsoft) see having the best apps as a competitive advantage, and two of those vendors are doing their bit to “sunset” Flash as a relevant web technology platform.

This leaves us in the curious situation where there is an exploding market for apps and a rapidly maturing web standards environment but some gaping holes in the web technology platform that gets broadly labeled as “HTML5.”

It’s an interesting time for the Web—web standards won but so far without the technology for the Web to go all the way as a ubiquitous platform for all. We’ve instead seen a ’90s-esque explosion of platform-specific apps and the threat that vendor-specific “walled gardens” will make the Web a second-class citizen.

All is not lost, though. For one, many of those “native” apps are powered in large part by web standards under the surface. And Adobe, for its part, is also investing in HTML5 development tools and getting involved in the web standards process. The browser makers are improving their browsers at a breakneck speed, and new specifications to bridge the gulf between web standards and native app development are appearing all the time. We’ll explore this more in Chapter 12 when we look at HTML5 and web apps.

Let’s Bury Flash-isms with Flash

If Flash is dying on the Web, we should aim to bury some Flash-isms with it. Let’s make sure we remember the lessons of the Flash era, especially when it comes to splash pages, loading screens, pointless animation, annoying widgets, and over-engineered, over-designed “experiences.” They were mostly terrible ideas. Some things simply don’t bear repeating, whether it’s done in Flash, mountains of JavaScript, advanced CSS3, Canvas, or some ungodly combination of these technologies.

Let’s also be careful of judging a technology too quickly. Canvas is going through its awkward adolescence at the moment—full of potential, embarrassing mistakes, experimentation, monosyllabic grunts, and finally (ideally!) maturity. Whenever new technology enters the web scene, it’s generally shown off in a bunch of experimental or inappropriate ways as a gimmick before settling into its groove and being used moderately and appropriately. I hope Canvas finds its groove quickly.

We’re Not in Canvas Anymore

That’s the background. Now let’s look at what the Canvas element actually does.

The <canvas> element defines a bitmap area—or “canvas,” if you will—that you can program and draw on with Canvas’ JavaScript API. The Canvas element has been kicking around since 2004 and was rolled into HTML5. All modern browsers support it natively (including IE9, although IE6–8 need to use emulation), as do modern mobile browsers.

The actual element looks like this:

<canvas id="mycanvas" width="500" height="200">
   Sorry, your browser doesn’t support canvas.
</canvas>

Like other things in HTML5, browsers that don’t understand <canvas> tags just see them as a generic element (like <mymadeupelement>) and ignore them, exposing the text between them.

That’s it as far as the HTML goes. Everything else happens through the JavaScript API, which lets us do the following:

  • Draw shapes, gradients, and shadows
  • Manipulate images and text
  • Create animations (by redrawing the canvas enough times per second)

Working with Canvas is like drawing on a single layer in Photoshop. You have only one layer of pixels to work with, and once you draw over them, they’re gone. So, to do animation (in games, for instance), we need to redraw the canvas for every frame. Canvas has no sense of managing and manipulating objects (that’s more SVG’s thing, which we’ll look at in Chapter 11), but a variety of libraries (for visualizations and games, in particular) have sprung up to help deal with this.

Given Canvas is manipulated through its JavaScript API, the extent you’ll want to get your hands dirty will depend on your interest in JavaScript and drawing graphics programmatically. Here’s an example of how we would draw a basic square (using the earlier <canvas> element and <body onload="draw();">):

function draw() {
   var canvas = document.getElementById('mycanvas'),
   var context = canvas.getContext('2d'),
   context.fillStyle = "rgb(200,0,0)";
   context.fillRect (10, 10, 100, 100);
}

This function gets our mycanvas Canvas element (using the 500 x 200px example we looked at earlier), sets the fill color to red, and then draws a solid red rectangle using the fillRect(x, y, width, height) function. As you can see in Figure 9-1, we’ve drawn a red square that is offset 10px on the x-axis, is offset 10px on the y-axis, and is 100px wide and 100px high. (I’ve added a 1px border around our <canvas> with CSS so you can see the size of the element itself.)

9781430264156_Fig09-01.jpg

Figure 9-1. Our simple Canvas example

We won’t delve into the workings of the Canvas API because there’s plenty of solid resources online, including the following:

Canvas has great potential for gaming and visualizations, along with more mundane uses such as creating charts and even tooltips. But perhaps the most exciting use for Canvas is bringing 3D to the web in a roundabout way with WebGL.

We’ll look at that in a moment. First, let’s look at some examples of Canvas in action.

Cool Things with Canvas

You can do a bunch of cool things with Canvas, from animations to full-blown games. Let’s start with something a bit more modest, though: tooltips.

Tooltips

Can the humble tooltip demonstrate that Canvas can be a better supported option than cutting-edge CSS3? I think so. Tipped (Figure 9-2, http://projects.nickstakenburg.com/tipped) is a great example of using Canvas to enhance a page. By drawing tooltips programmatically through the JavaScript API, there are no images to worry about. Check it out:

9781430264156_Fig09-02.jpg

Figure 9-2. Tipped examples

Tipped

Tupped makes it easy to create new skins and themes, as well as effects such as rounded corners, shadows, and gradients, all on the fly with Canvas and its JavaScript API. Plus, with IE6–8 compatibility provided through ExCanvas (which we’ll look at shortly), we get all the CSS3-style effects with full IE support.

Charts

Later we’ll touch on some SVG-based charting tools (including gRaphaël and the excellent Highcharts). But there’s no shortage of Canvas-based charting options either. Here’s a small selection.

RGraph

Figure 9-3 shows a Canvas chart built with the powerful-if-not-all-that-pretty RGraph (www.rgraph.net/). The beauty of Canvas-based charts is the solid support in iOS (and Android), where Flash isn’t an option. (The paid www.zingchart.com/ does Canvas, Flash, and SVG if you need better cross-platform support. If you’re after something simpler, Flot is a popular, free, jQuery- and Canvas-based charting tool: http://www.flotcharts.org/

9781430264156_Fig09-03.jpg

Figure 9-3. An RGraph chart

Visualize

Filament Group also has an accessible Canvas charting solution with its Visualize plug-in, which gives results like those shown in Figure 9-4. Visualize is discussed in detail at www.filamentgroup.com/lab/update_to_jquery_visualize_accessible_charts_with_html5_from_designing_with/.

9781430264156_Fig09-04.jpg

Figure 9-4. Visualize in action

HumbleFinance

HumbleFinance (www.humblesoftware.com/finance/index) is an HTML5-driven demonstration of Google Finance–style charting. Figure 9-5 shows an example of how clean the interface is. Because Canvas is just another HTML element, you can easily position other <div>s (or any other DOM object) on top of it, which HumbleFinance has done in Figure 9-5 for the chart labels and other text.

9781430264156_Fig09-05.jpg

Figure 9-5. HumbleFinance’s sophisticated Canvas-powered chart

Peity

Peity (http://benpickles.github.com/peity/) is a jQuery plug-in that turns an element’s content into a mini pie, bar, or line chart. It takes the values in an element like <span class="line">5,3,9,6,5,9,7,3,5,2</span> and converts it into a <canvas> element that renders the appropriate chart (see Figure 9-6).

9781430264156_Fig09-06.jpg

Figure 9-6. Peity examples

Similar to Peity, jQuery Sparklines (http://omnipotent.net/jquery.sparkline/) takes a similar Canvas-based approach and has even more options.

Visualizations

Canvas is a great tool for creating dynamic visualizations of data, and there are many great implementations of it. We’ll highlight a few here.

Processing.js

Some of the best Canvas examples use Processing.js (http://processingjs.org/), the JavaScript port of the Processing visual programming language. Examples range from simple games to abstract digital art to visualizations (as shown in Figure 9-7).

9781430264156_Fig09-07.jpg

Figure 9-7. Processing.js example Fizz

“Evolution of Privacy on Facebook”

One of the most practical examples of a Canvas-based visualization using Processing.js is the interactive “Evolution of Privacy on Facebook” visualization (Figure 9-8; see http://mattmckeon.com/facebook-privacy/). Because it’s implemented in Canvas, it will work on iOS devices, but we still have to worry about compatibility with the (currently) larger IE6–8 group.

9781430264156_Fig09-08.jpg

Figure 9-8. The “Evolution of Privacy on Facebook” visualization

Canvas, Twitter, and Audio Mashup

From the practical to the ... well, pretty. The HTML5 Canvas experiment shown in Figure 9-9 uses Processing.js for particle rendering and uses the <audio> element to play music (but it’s not an audio visualizer). See it for yourself: http://9elements.com/io/projects/html5/canvas/. The particles are actually 100 HTML5-related tweets, with their contents rendered as normal HTML in the document. (See the write-up here: http://9elements.com/io/?p=153.)

9781430264156_Fig09-09.jpg

Figure 9-9. This mashup pulls in HTML5-related tweets, some of which are quite appropriate

Paper.js

Processing.js isn’t the only game in town. Paper.js (Figure 9-10; see http://paperjs.org) dubs itself as “The Swiss Army Knife of Vector Graphics Scripting” and demonstrates how the bitmap-based Canvas element can be used for advanced vector graphics scripting, complete with a “Document Object Model for vector graphics” and keyboard and mouse interaction. See their examples for more: http://paperjs.org/examples/.

9781430264156_Fig09-10.jpg

Figure 9-10. Paper.js looks great in motion—be sure to check out the examples on the site

(Smashing Magazine also published an extensive comparison of Processing.js, Paper.js, and the SVG-based Raphaël: http://coding.smashingmagazine.com/2012/02/22/web-drawing-throwdown-paper -processing-raphael/.)

Games

A variety of (mostly retro) games have been built with Canvas. We’ll take a look at a handful here and then look at some amazing WebGL-powered games.

Google Chrome Racer

Speaking of Paper.js, Google Chrome added a game called “Racer” (see Figure 9-11) to their impressive HTML5 experiments that uses Paper.js, Canvas, and websockets to create a multiplayer game that runs in Chrome on iOS or Android. Racer shows off a major advantage of HTML5 games: they can run in on platform without having to be ported across devices. It relies on cutting edge HTML5 featurs and only claims to support Chrome, but works well in other modern mobile browsers as well. See more about it on the Racer website: http://www.chrome.com/racer.

9781430264156_Fig09-11.jpg

Figure 9-11. Google Chrome Racer shows off the power of Canvas on mobile devices

Biolab Disaster

Biolab Disaster (Figure 9-12; see http://playbiolab.com) by Dominic Szablewski is a neat example of retro gaming using <canvas> for the visuals. It’s a fun little platform game where you run, jump, and shoot your way through the levels.

9781430264156_Fig09-12.jpg

Figure 9-12. Biolab Disaster is a fun little platformer

Canvas Rider

Canvas Rider (Figure 9-13; see http://canvasrider.com/) is another example of the fun (and hard!) browser games that can be built with Canvas. (Warning: it’s highly addictive.)

9781430264156_Fig09-13.jpg

Figure 9-13. Canvas Rider is addictive

Cut the Rope

From small gaming experiments to huge international hits. The extraordinarily popular mobile game Cut the Rope (see Figure 9-14) was ported from the original iOS code to HTML5 and released in early 2012. The project was sponsored by Microsoft to show off the HTML5 capabilities of IE9, including its hardware-accelerated Canvas implementation. You can play it in your browser right now: www.cuttherope.ie/.

9781430264156_Fig09-14.jpg

Figure 9-14. Cut the Rope takes Canvas-based graphics to a new level

This project demonstrates the potential of web standards for gaming: develop with tools like Canvas and then easily implement your game in modern browsers and bundle it up as a native app for any platform where HTML5 is well supported. We’ll take a closer look at gaming and Canvas later in this chapter.

Image Manipulation

Online image manipulation is another category of web page that used to be possible only through Adobe Flash. Since it requires loading a file’s raw bytes into memory, rendering the resulting image, and allowing the user to manipulate that image, you can imagine how difficult this was without a full blown image editing engine like Flash. However, recent advances in the ability to store and manipulate memory through JavaScript in Canvas have allowed similar sites to appear in HTML5 as well. Let’s look at a few.

PaintbrushJS

With Canvas we can do some pretty impressive image manipulation, as Dave Shea’s PaintbrushJS library ably demonstrates (http://mezzoblue.github.com/PaintbrushJS/demo/). PaintbrushJS lets us apply Gaussian blurs (see Figure 9-15), add noise, fade to grayscale (or sepia), and more. And it’s all done on the client side with Canvas and JavaScript.

9781430264156_Fig09-15.jpg

Figure 9-15. PaintbrushJS can perform some impressive Photoshop-like effects

Canvas-Driven Web Apps

Some real, honest-to-goodness web apps use Canvas, which show what’s possible in the browser right now. They’re mostly painting- or drawing-related (this is Canvas, after all), and none better illustrates this than deviantART’s Muro—a free, HTML5-powered drawing/painting app (see Figure 9-16). Try it at http://muro.deviantart.com/ or read more about it at http://news.deviantart.com/article/125373/.

9781430264156_Fig09-16.jpg

Figure 9-16. Muro is a powerful drawing program, right in your browser

Sketchpad

Sketchpad, as shown in Figure 9-17, is another great HTML5-powered painting app that you can play with here: http://mugtug.com/sketchpad/.

9781430264156_Fig09-17.jpg

Figure 9-17. Sketchpad shows the possibilities of a Canvas-powered painting program

Endless Mural

The Endless Mural (Figure 9-18; see www.endlessmural.com/) is “an interactive, collaborative art web site,” powered by Canvas and sponsored by Microsoft for its IE9 launch. The project is the work of Automata Studios and Joshua Davis Studios (Joshua Davis was an early Flash and digital art pioneer). The code has been released as Okapi, an “open-source framework for building digital, generative art in HTML5,” and is available here: http://www.okapijs.org/.

9781430264156_Fig09-18.jpg

Figure 9-18. The Endless Mural site lets you create variations of this artwork

LucidChart

It’s not all arty painting, though. Some fully fledged diagramming (paid) web apps also use Canvas, such as LucidChart (shown in Figure 9-19), which you can test here: www.lucidchart.com/.

9781430264156_Fig09-19.jpg

Figure 9-19. LucidChart lets you dive right in with a couple of clicks, so give it a go

Drawing Interface Elements

The prior examples all show Canvas being used to do some pretty wild stuff. What about employing it in a more normal context? In this section we’ll look at ways Canvas is being used to augment regular interface elements to provide a more interesting experience.

Flash-Style Interface Effects

Rally Interactive pulled off an impressive animated triangle-to-circle effect to show off its work (see Figure 9-20), with screenshots and statistics pulled from Dribbble. I initially assumed it was a bunch of fancy CSS3 (which would be cool in its own right), but Rally Interactive is in fact using Canvas. See it in action: http://beta.rallyinteractive.com/ (and view source to see how they did it).

9781430264156_Fig09-20.jpg

Figure 9-20. Rally Interactive demonstrates that a clever use of multiple Canvas instances can produce impressive effects

Background Animations

Google Music’s tour page (see Figure 9-21) uses Canvas to render the background animations, which has the thick colored lines scribbling and scrawling as you move from section to section. See it in action: http://music.google.com/about/tour/.

9781430264156_Fig09-21.jpg

Figure 9-21. The Canvas-drawn background elements on the Google Music site take you on quite a journey as you browse the site

Interface Backgrounds with Liquid Canvas

Here’s an idea for us web designers: how about using Canvas to draw the backgrounds of interface elements? We could position <canvas> elements wherever we like in our markup, even layering them on top of each other as appropriate (using position and the z-index), and then have a small amount of JavaScript render all the graphics—no images required.

This approach could speed up development considerably. No more exporting finicky images from Photoshop to tweak a color scheme for a client. Just change some JavaScript variables and we’re done. And with ExplorerCanvas for IE, we might even have better browser compatibility than the current state of CSS3. Plus, hardware acceleration is only making Canvas faster on the mobile and the desktop.

The best (and perhaps only) example of this is the Liquid Canvas JavaScript library (see Figure 9-22) from 2008. (You can read about it at www.ruzee.com/content/liquid-canvas and see it in action at www.ruzee.com/files/liquid-canvas/demo.html.) The demos aren’t the prettiest, but the possibilities are certainly intriguing. For example, with Liquid Canvas you can use Canvas to draw backgrounds behind your <div>s with shadows, rounded corners, gradients, and strokes. (There’s a tutorial available here: www.caffeinedi.com/2009/11/02/using-jquery-and-liquidcanvas-to-add-drop-shadows-borders-rounded-corners-and-other-effects-to-your-website-even-in-ie6-and-ie7/.)

9781430264156_Fig09-22.jpg

Figure 9-22. The examples may not be pretty, but Liquid Canvas demonstrates some intriguing possibilities

With a bit more design love, this could be a great way to A/B test certain aesthetic treatments of a layout, all using JavaScript alone—again, no images to export and maintain in CSS. Of course, there will always be limitations with this approach, and presentation should obviously be CSS’s domain long term. (And, realistically, most people are going to stick with CSS3.) But Liquid Canvas certainly gives us a very novel approach to drawing interface elements.

We don’t necessarily need to use Liquid Canvas in some (non-IE) situations either. It’s been possible to use Canvas elements as CSS backgrounds in WebKit since 2008 (!), as described here: www.webkit.org/blog/176/css-canvas-drawing/. Firefox 4 added similar support in 2011 (http://hacks.mozilla.org/2010/08/mozelement/), and there are workarounds for other browsers (for static Canvas’s), as described in the answers here: http://stackoverflow.com/questions/3397334/use-canvas-as-a-css-background. With hardware acceleration arriving and the ability to use Canvas as a CSS background, we have some intriguing options for fast, programmatically generated interface elements.

Consider the possibilities for responsive web design too—we can programmatically redraw CSS backgrounds (in iOS at least) based on the device’s resolution. There’s no need to download a desktop-sized image and shrink it down or maintain different sets of artwork for different devices; just let a script do the heavy lifting for us. (Let me know if you create such a script!)

While we’re floating ideas, I’d love to see something like the Photoshop-esque layer-style panel for CSS3 effects (which is very cool in its own right—see http://layerstyles.org/builder.html) built for Canvas. It could generate the Liquid Canvas–style JavaScript, which you could then drop into your page and have the effects rendered on just about all browsers. Heck, it would be cool if Photoshop itself could export to Canvas.

Canvas may not be a Flash replacement, but these examples show it certainly opens up some interesting doors.

The Sometimes Good and Sometimes Bad Canvas Emulation for IE6–8

While IE6–8 don’t support Canvas, all is not lost. Several emulation options can give these browsers at least some support using IE’s native, legacy Vector Markup Language (VML); Microsoft’s Silverlight plug-in; or Flash.

Each approach has its pros and cons. VML is slow and loads elements into the DOM. (Canvas elements get re-created as vector elements, so the more that get loaded, the slower it gets.) But animation is smooth, and the Canvas on the whole remains in the DOM.

While IE6–8 don’t support Canvas, all is not lost. Several emulation options can give these browsers at least some support using IE’s native, legacy Vector Markup Language (VML); Microsoft’s Silverlight plug-in; or Flash.

Each approach has its pros and cons. VML is slow and loads elements into the DOM. (Canvas elements get re-created as vector elements, so the more that get loaded, the slower it gets.) But animation is smooth, and the Canvas on the whole remains in the DOM.

Throw in issues with interactivity, mixed support for features in the Canvas API, and performance issues for processor-intensive applications, and you have quite a mixed bag. You can still render some impressive animated effects (albeit slowly) or render static images and have them available in the DOM (which is great for charts and the like). But it may be a case of “so close, so far” if performance isn’t up to scratch or a crucial feature isn’t supported.

Here are the tools that do the emulation. Check out their demos in IE 6, 7, or 8 to get a feel for how they perform (for example, try these in IE: http://code.google.com/p/flashcanvas/wiki/Examples). In some cases (such as static Canvas renderings), you may never know it’s being emulated, while in other cases you may get away with acceptable-if-not-perfect emulation. Just remember that Canvas emulation can be a pretty murky area; it’s rarely a get-perfect-support-for-free-IE6–8 card.

Here are some Canvas emulation utilities:

9781430264156_Fig09-23.jpg

Figure 9-23. This kaleidoscope FlashCanvas example demonstrates that impressive Canvas effects are possible even in IE7; they’re just slow

The Haphazard World of Web Standards (Or: How Did We End Up with Canvas?)

Let’s touch on the history of Canvas because it illustrates how haphazardly these features can be developed (as was the case with much of HTML5, which is in some ways just a grab-bag of tech that’s been around for years).

“HTML5” is just a buzzword for 7 years worth of neat stuff.

—Dave Balmer, www.slideshare.net/dbalmer/rockstar-graphics-with-html5-media-uk

Do you use OS X’s Dashboard feature? That’s where Canvas originated, back in 2004. Apple wanted Dashboard widgets to be easy to write, so it based them on the good ol’ web stack of HTML, CSS, and JavaScript (and native code if you wanted) and used WebKit to render them. (WebKit is the rendering engine behind Safari and Google’s Chrome.)

But Apple thought the web stack for rendering Dashboard widgets had limitations, so it added a few features to WebKit, the major one being Canvas. Safari uses WebKit, and so Safari now supported Canvas. And lo, Canvas on the Web was born.

This, in the tradition of new web technology, caused considerable concern.

Vendors adding browser-specific features to HTML, without any standards process, is what the web standards movement was trying to get away from. Had Microsoft and Mozilla responded with their own, incompatible attempts at something like Canvas, we would have a great mess on our hands.

So, Ian Hickson reverse engineered Apple’s Canvas implementation and put it in the WHATWG Web Applications 1.0 spec. Canvas gained support in Firefox 1.5 in 2005, Opera 9 in 2006, and finally IE9 in 2011. The WHATWG Web Apps 1.0 spec became HTML5 as we saw in Chapter 1, and Canvas is now an official part of HTML5. Hats off to Hickson for all his work bringing it into the spec.

(For more on Canvas’ history, see http://peter.sh/2010/06/thank-you-microsoft-html5-canvas-is-a-go/.)

The Canvas Element and Accessibility

In terms of accessibility, Canvas can be a bit of a nightmare. There’s nothing for a screen reader to read—just a black hole and whatever text (if any) the designer has put between the <canvas> tags.

Vendors are trying to address this. For example, IE9+ exposes the fallback content between the <canvas> tags to assistive technology. The idea here is that when the browser does support Canvas but the vision-impaired user can’t see it, they can still get something useful in the form of alternate text via their screen reader, for example. (Well, that’s the theory. At the moment they’ll get a lot of erroneous “Your browser doesn’t support canvas” messages, because that’s how the fallback content has been used to date.) For more, see accessibility guru Steve Faulkner’s discussion of this feature at www.paciellogroup.com/blog/2010/09/html5-canvas-accessibility-in-internet-explorer-9/ and the right way to make content inside of a canvas accessible at http://blog.paciellogroup.com/2012/06/html5-canvas-accessibility-in-firefox-13/.

As for the accessibility of the Canvas element itself, it’s a thorny issue that has been discussed for years, and it still hasn’t been resolved. There’s a summary of the last few years of discussions at www.paciellogroup.com/blog/2011/12/html5-canvas-accessibility-discussions-2009-2011/, but that’s as far as we’ve come.

Let’s not repeat the accessibility mistakes of decades past with <canvas>. We could in theory render fancy text, an entire web page, or a web application (which has been done!), with JavaScript in a <canvas> area. But it’s a terrible idea and about as useful (from an accessibility perspective) as having our design as one giant image.

(You can do some crazy things with text in Canvas, though, as this tutorial illustrates: www.html5rocks.com/tutorials/canvas/texteffects/. So, we may still do text replacement with such techniques.)

The Current State of Canvas

Technologies (web or otherwise) don’t exist in a vacuum. Their success often depends on having a conducive environment around them. There’s certainly plenty of enthusiasm for Canvas, but what about the environment around it?

Primitive Development Environment

It’s worth remembering the environment around Canvas is still quite primitive, so there are few (if any) development tools. (Flash has been successful not just because of Flash Player’s ubiquity but also the mature tools available to developers.)

The situation is changing. HTML5 game engines (for example, http://impactjs.com/, which was used for the game Biolab Disaster, featured earlier) usually have some support for Canvas, but they’re pretty niche- and developer-oriented, not quite what we need for general web design.

In terms of tools for designers, there’s an Adobe Illustrator to Canvas plug-in from Microsoft’s Mike Swanson (http://visitmix.com/labs/ai2canvas/). And Adobe has been working on Canvas animations for some time, as we looked at earlier, first through Flash Authoring and now through Adobe Edge. (Adobe has also been experimenting with more generic “HTML5” export, but it uses the term HTML5 quite loosely. We’ll look more at Adobe’s position when we discuss SVG in Chapter 11.)

Performance

One of the exciting things about Canvas is that you can view it on everything from iOS, where Flash isn’t an option, to the desktop. In simple, static cases, such as graphs and charts, this is a reality now. But for anything more processor-intensive (such as animation and gaming), recent mobile devices simply haven’t been powerful enough to do anything other than simple tasks.

This is changing, though, particularly as mobile devices get faster and Canvas gets hardware acceleration (as Microsoft promised and demonstrated for IE9 mobile: www.gsmarena.com/internet_explorer_9_on_wp7_aces_html5_drawing_test-news-2524.php). Apple has also dramatically improved Canvas performance in iOS Safari starting in iOS5.  See jsPerf.com for up to date performance statistics across all of the prominent browser, platforms, and devices (http://jsperf.com/canvas-draw-tests.)

Sencha also reported dramatic Canvas iOS5 speed improvements: www.sencha.com/blog/apple-ios-5-html5-developer-scorecard/. (Sencha’s HTML5 blog posts are excellent for keeping up on feature support, by the way.)

Hardware acceleration is making a big difference. But at the moment anything Canvas-intensive can quickly turn into a relative chug-fest on your not-cutting-edge phone or tablet (as the iOS4 and Android device results earlier demonstrated).

Limited IE Compatibility

As we saw, IE6–8 can support Canvas to varying degrees and in several ways (VML, Flash, Silverlight). If you’re set on using Canvas, this may be a godsend. But compared to Flash, it may be a total headache and limit Canvas’s uptake until IE9 becomes the new baseline for design and development. (Remember, IE8 is the end of the line for Windows XP users, so we’ll need to wait until Windows XP finally disappears before we can assume native Canvas support.)

While simple or static Canvas implementations may be perfectly acceptable with emulation, complex animation and games are probably out of the question. This creates an odd situation when Flash-style interactivity is a necessity—we can either support IE8 and older with a Flash-based experience that won’t work on mobile or create something for modern browsers that won’t work on IE8 and older. Some poor designers and developers may end up doing both and may ironically end up creating a more advanced Flash experience for the legacy desktop browsers and a simpler Canvas-based version for the modern browsers. Here’s to the speedy demise of IE8 and the rapid development, adoption, and maturation of new web standards.

Again with the Glass Metaphor

This is another one of HTML5’s “glass half-full, glass half-empty” situations. It’s amazing what people are doing with what was once an OS X Dashboard feature from 2004—from nifty design features (such as tooltips) and interactive experiments to games and full-blown web apps. Canvas wasn’t designed with these things in mind; it just turned out to be useful in those situations. That’s the “glass half-full” perspective. If you’re waiting for a mature, “write-once, run-anywhere on the desktop” environment such as Flash, however, it probably looks like the glass is half empty. We’re going to be waiting a good while yet.

HTML5 Gaming: Canvas or Not?

Canvas often gets mentioned when discussing HTML5 and games, so let’s take a brief look at the state of HTML5 gaming. Could you use your existing web skills to write games using HTML and JavaScript that run in any modern browser? Sure, if you’re comfortable developing in JavaScript. Will the game be any good? Well, that depends....

One of the biggest trends in recent years has been casual gaming, both in browsers and on mobile devices such as the iPhone. From FarmVille to AngryBirds to CandyCrush, social games on the web (primarily inside of other services like Facebook) or mobile devices have been a dominant force in the gaming world for recent years and keeps growing as smart phone penetration grows.

So, an open, cross-device platform for creating casual, social games is appealing, from both a hard-nosed business perspective and an idealistic “open platform” perspective, and HTML5 fits the bill quite nicely.

The key is to understand what type of “gaming” we’re talking about here. Graphically these are often simple 2D games, similar to what was around in the early ’90s. In this sense, it’s very much “back to the future”—we’re using the latest web tech to create games that look 20 years old in the very modern, mobile world of social networking.

Is It Even Canvas?

For all the HTML5 hype, some of these HTML games and game engines have explicitly avoided features such as Canvas for performance reasons, relying on DOM scripting and CSS3 (which is partly hardware-accelerated on iOS devices) to get the job done. Here’s what one group of developers found from a quick tech demo that followed an HTML gaming engine approach (http://seb.ly/2011/04/html5javascript-platform-game-optimised-for-ipad/):

So what’s the answer to getting the performance on iOS? Forget HTML5 canvas, all the moving objects in this game are HTML div elements, we’re just moving them around by controlling CSS properties with JavaScript.

When discussing HTML5, we need to look closely at the technology and techniques people are actually using. What you think is Canvas may well not be. Canvas may be used more widely in web-based gaming as performance improves (and hardware acceleration becomes the norm), but it’s worth keeping in mind just how liberally the term HTML5 gets thrown around.

Don’t get me wrong: the demos and ports have shown us the scope of what’s possible for HTML5 games on the desktop now and in mobile browsers in the not-too-distant future. Just keep in mind the phenomena of casual gaming in the browser is less about the latest technology and more about big ideas such as social networking, which the traditional web stack can exploit in pretty interesting ways. That said, hardware-accelerated, 3D gaming is also coming to the browser through Canvas thanks to WebGL, which we’ll look at shortly.

Getting Started with Canvas Game Development

Nevertheless, if you want to get hands-on with Canvas for gaming, check out the tutorial and overview (just ignore the hype in the article) at www.html5rocks.com/tutorials/casestudies/onslaught.html or the absolute beginners tutorial at www.lostdecadegames.com/how-to-make-a-simple-html5-canvas-game/. And don’t forget the game examples we looked at earlier, including Cut the Rope (www.cuttherope.ie).

For a detailed rundown of all the different technologies available for “HTML5” games (in the broader sense) and their delivery and monetization options, check out this excellent article from January 2012: “The Reality of HTML5 Game Development and making money from it” (www.photonstorm.com/archives/2759/the-reality-of-html5-game-development-and-making-money-from-it). For a more up-to-date listing of popular HTML5 games, check out html5games.com.

HTML Gaming: Beyond HTML5

There is also a lot of developer interest in taking the web platform beyond HTML5 and including things like the Joystick API, surround-sound support, and extensions to CSS. It’s a bit dated now, but the W3C held a workshop all about html.next and games – see their detailed write-up for more: “Report on the Workshop on HTML.next for Games” (www.w3.org/2011/09/games/). We’ll touch on the post-HTML5 web platform in Chapter 12.

Canvas: What’s in It for Me?

Now that we’ve looked at some specific use cases for Canvas on the web, let’s consider canvas from a different perspective: what type of people is it relevant to?

Canvas for Web Designers

How much Canvas will matter to you depends on where you work and the projects you do. If you work in a big-budget agency, where Facebook components are mandatory for massive, national, or worldwide marketing campaigns, you may find new gaming capabilities very interesting.

If you’re a freelancer doing client work on tight budgets, the off-the-shelf charting tools like we saw may not be quite as sexy but extremely useful nevertheless. Canvas emulation for IE6–8 may prove handy as a cross-device solution (as opposed to Flash-based tools) that covers IE6+ and iOS devices.

Canvas-based image editing tools for content management systems could start springing up too, and if you like to tinker, there is enormous room for experimentation. You may want to experiment with rendering interface elements with Canvas (as the Liquid Canvas and Tipped libraries demonstrate) or see how far you can push Canvas in the way studios like Rally Interactive have demonstrated.

Canvas for Students and Hobbyists

A free, open, and relatively simple platform such as HTML, JavaScript, and CSS could create a fertile environment for kids who want to cut their teeth on simple game design. With tutorials and development libraries springing up all over the place, they have enough information to start making simple (and not so simple) games. It would be great to see this happen in schools, and it wouldn’t need much resourcing—just a half-decent PC (or a $35 Raspberry Pi: www.raspberrypi.org/) and a modern-ish browser.

Canvas for Flash Designers

I was all ready to beat down on Flash again here, but then Adobe dropped a bombshell. As of December 2013, Flash Professional CC now includes full native support of Canvas. This is great news for pretty much everyone involved. Canvas has been screaming out for a good authoring tool, and there are an awful lot of designers and developers out there who are familiar with Flash Professional. You now get to design and animate within Flash Professional’s mature toolset, and then push it straight out to Canvas. At the time of this writing this is still very fresh news, so nobody’s really had much of a chance to put it through its paces, but if it works well then we should see a lot more people switching over to Canvas, and a lot of stunning work appearing. Flash is dead, long live Flash!

Suck It and See

We have enormous scope for weaving the <canvas> element into our web pages in subtle (or not-so-subtle) ways. But whether Canvas becomes a staple web design tool or simply the Java applet of our time is up to us. Let’s give it a go and see what we can come up with.

2D Canvas’s 3D Future: WebGL

I’ve left the best to last—one of the most exciting developments related to Canvas is WebGL (Web-based Graphics Library). Despite Canvas’s ostensibly 2D origins, the new WebGL standard gives Canvas a hardware-accelerated 3D context powered by OpenGL—if the browser (and underlying hardware) supports it. This opens the door to modern, 3D gaming in your browser.

(The WebGL spec wasn’t developed by the W3C or the WHATWG but instead by nonprofit technology consortium Khronos Group, which sprang from Mozilla and shepards OpenGL as well. So, it’s not HTML5 per se, but it’s still cool.)

The WebGL working group includes Apple, Google, Mozilla, and Opera. (See the Wikipedia entry for more: http://en.wikipedia.org/wiki/WebGL.) Microsoft was a late adopter of WebGL, citing some legitimate concerns around security that were echoed by others (such as John Carmack, founder of id Software and a highly respected gaming developer: https://twitter.com/#!/ID_AA_Carmack/status/81732190949486592). But with Microsoft on board in IE11, it’s looking very likely that WebGL will be the de facto hardware-accelerated 3D content technology provider for the modern Web.

(Here’s a techcrunch blog post on Microsoft’s adoption of WebGL: http://techcrunch.com/2013/10/21/with-internet-explorer-on-board-webgl-is-ready-for-prime-time/.)

3D on the Web: WebGL Alternatives

WebGL isn’t the only game in town, either.

The major players in browser technology were eyeing 3D development years ago. Adobe Flash had a thriving 3D development community years before it introduced hardware accelerated 3D in 2010. Around the same time, Microsoft introduced hardware accelerated 3D in its Silverlight plugin. But with Silverlight all but gone as a browser plugin and Flash quickly disappearing, what options do 3D developers still have?

Interestingly, Unity Technologies, which makes the popular cross-platform (and very mobile-friendly) 3D engine Unity, has been distributing its own browser plug-in, which it claims has seen 225 million downloads. It is also rolling out an export-to-Flash option so users don’t need to download the Unity browser plug-in. Microsoft also recently announced a deal with Unitey for creating content for Windows 8 devices, dedicating “porting labs” to help developers, and claims at least 1000 game ports to date (see their announcement here: http://blogs.windows.com/windows_phone/b/wpdev/archive/2013/11/18/1000-unity-apps-across-windows-store-and-windows-phone.aspx).

It’s worth noting that Unity’s made a lot of headway into all of the areas Flash was tryin to get to extend it’s life: games, 3D, and exporting directly to mobile devices. More than Canvas, WebGL, or any other HTML5 technology, Unity may be the player that actually replaces Flash in those contexts.

WebGL also has old-school predecessors. Displaying 3D on the Web isn’t new—Virtual Reality Modeling Language (VRML), for example, dates back to 1994 (see http://en.wikipedia.org/wiki/VRML). But with hardware-accelerated 3D now pretty much a given on any platform (including smartphones), the potential for 3D on the Web for gaming and beyond (for example, 3D-modeled product previews, medical models, maps, and more) is infinitely greater.

Who knows? Maybe we’ll finally have the technology for that 3D “virtual” shopping mall experience we’ve all been, uh, craving...

So WebGL isn’t the only game in town, but support and popularity are growing fast. Let’s look at a few demos and you can make up your own mind about whether you think plugins are a part of the 3D-and-gaming web’s future or not.

Show Me the Demos!

When it comes to WebGL, seeing is believing. So, fire up the latest version of your favorite browser and check out these cool demos. And remember, this is all happening in <canvas>—just another element in the DOM (which we can push around with CSS3).

HelloRun

This addictive and immersive game by HelloEnjoy shows off just how far rendering in a browser has come in recent years. The game (Figure 9-24) renders an immersive 3D environment complete with reflections, fog, and a slew of lighting effects, all rather smoothly and without relying on a plugin. You can play it here: http://hellorun.helloenjoy.com/.

9781430264156_Fig09-24.jpg

Figure 9-24. HelloRun shows the full power of WebGL in a simple and addictive browser game

Epic Citadel

In 2013 Epic Games released a demo of the Unreal Engine 3 running in WebGL (Figure 9-25). The company released a small level called the “Epic Citadel” that allowed a player to navigate an immersive, if small, 3D world. It demonstrated that WebGL could be used to deliver console-quality experiences in the browser. Check it out here: http://www.unrealengine.com/html5/.

9781430264156_Fig09-25.jpg

Figure 9-25. Epic’s Unreal Engine 3 running in a browser

Angry Birds

Yep, Angry Birds in the browser (Figure 9-26, http://chrome.angrybirds.com). WebGL delivers hardware-accelerated 2D graphics as well. (Take a moment to consider the implications of 2D, WebGL-powered interfaces for web sites, not just games.) Try Angry Birds on the Web for yourself at http://chrome.angrybirds.com. Interestingly, when WebGL isn’t available, this falls back to DOM animation (including moving 2D Canvas elements around), so you can compare the performance and how much a 2D game like Angry Birds benefits from hardware acceleration. (You still need Flash for sound because of the poor state of audio for games in browsers.)

9781430264156_Fig09-26.jpg

Figure 9-26. The Angry Birds juggernaut hits the Web thanks to WebGL

Rome “3 Dreams in Black” Interactive Music Video

This incredible music video for Danger Mouse’s Rome project (Figure 9-27) is a great example of how interactive WebGL can be. Check it out in Chrome at www.ro.me, though it should be viewable in the latest Firefox versions as well. It’s an amazing experience, and there are even user-submitted 3D models in the desert at the end.

9781430264156_Fig09-27.jpg

Figure 9-27. The Rome experience is an absolute must-see

You can watch a video of the team behind the film (though “film” doesn’t do it justice) with clips of the experience here: www.youtube.com/watch?v=ReH7zzj5GPc.

It will be exciting to see what designers, artists, and engineers can produce with this technology in the future. (I just hope it doesn’t turn into too much of a marketing tool. Imagine reading the news on a mainstream news site when suddenly a big Canvas element is overlaid and you’re thrust into a 3D “brand experience.”)

glfx.js Image Manipulation

Earlier we saw how Canvas can manipulate images in 2D, but WebGL unleashes even more power thanks to its hardware acceleration: http://evanw.github.com/glfx.js/. The WebGL-powered glfx.js image effects library lets us apply hardware-accelerated, Photoshop-like filters such as brightness/contrast, curves, denoise, hue/saturation, unsharp mask, lens blur, tilt shift, triangle blur, zoom blur, color halftone, perspective transformations, swirls, and more (Figure 9-28). Check out the demo to see it in action: http://evanw.github.com/glfx.js/demo/.

9781430264156_Fig09-28.jpg

Figure 9-28. The tilt shift effect in glfx.js is one of many Photoshop-esque effects

Quake II

Quake II (figure 9-29) has also been ported to WebGL, using “WebGL, the Canvas API, HTML 5 <audio> elements, the local storage API, and WebSockets to demonstrate the possibilities of pure web applications in modern browsers such as Safari and Chrome.” For more, see http://code.google.com/p/quake2-gwt-port/. (You need to download and compile the code to actually play it, but there’s a video of it in action here: www.youtube.com/watch?v=fyfu4OwjUEI. There’s also a Quake 3 demo level in WebGL here: http://media.tojicode.com/q3bsp/.)

9781430264156_Fig09-29.jpg

Figure 9-29. Quake II running in the browser using only web technologies

In a response to a tweet that said this:

Not sure if the best endorsement of JS engine speed in 2010 is ports of games from 1997...

Joel Webber, one of the engineers behind the port, wrote the following (http://blog.j15r.com/2010/04/quake-ii-in-html5-what-does-this-really.html):

What's the point? What this code currently proves is that it's feasible to build a "real" game on WebGL, including complex game logic, collision detection, and so forth. It also did an excellent job exposing weak spots in the current specs and implementations, information we can and will use to go improve them. [...]
[O]ne can envision a world where game developers can deploy their games as easily as we currently deploy web pages, and users can play them just as easily. And that's a pretty damned compelling world to imagine.

Send a link. Play the game. That’s what WebGL (and the other emerging 3D technologies) will enable. Or, in fact, is enabling.

GT Racing: Motor Academy

In December 2011, Gameloft debuted a WebGL-powered version of its game GT Racing: Motor Academy on Google+ (Figure 9-30). This is an interesting look at not just the future of gaming technology in the browser but distribution through social networks too. (Gameloft’s Baudouin Corman discussed these issues with Gamasutra here: www.gamasutra.com/view/news/39273/Gameloft_Embraces_HTML5_With_3D_Game_GT_Racing.php.)

9781430264156_Fig09-30.jpg

Figure 9-30. Playing GT Racing live in the browser on Google+

Skid Racer

GT Racing isn’t the only WebGL-powered racing game. Skid Racer (Figure 9-31) is an original, WebGL-powered “kart racer” that’s available online here: https://skid.gamagio.com/play/. (The irony of a web-based, Chrome-only game should be noted, but we’ll give the developer the benefit of the doubt and put it down to distribution issues.)

9781430264156_Fig09-31.jpg

Figure 9-31. The console-like Skid Racer may represent one of the first steps in modern, 3D, web-distributed games

More WebGL Demos

Here are some more WebGL demos and examples:

For more reading, check out HTML5 Games Most Wanted, which covers CycleBlog in depth, and Beginning WebGL for HTML5, both published by APress and available at après.com.  For more WebGL demos, see the experiments at www.chromeexperiments.com/webgl or this list of traditional Canvas as well as WebGL games http://www.creativebloq.com/html5/top-20-html5-games-1122788.

Still Early Days for WebGL

The WebGL games and demos show incredible promise, but it’s not all beer and Skittles for WebGL development at present. One developer on Hacker News commented on the hardware incompatibilities, software bugs, and inconsistencies that can make it a difficult development environment for small teams, saying this (https://news.ycombinator.com/item?id=3253016):

[W]e found so many inconsistencies across different hardware and different browsers that it made it not worth it to work on a WebGL project for the time being, especially for a small team. We wrote a number of runtime checks, but we still could not account for all the bugs, or find ways around every one of them.

Nevertheless, as the earlier demos show, amazing things are possible. And if you’re interested in the raw performance details of WebGL, check out the extensive post “HTML5 2D gaming performance analysis” (www.scirra.com/blog/58/html5-2d-gaming-performance-analysis) from Scirra, developers of HTML5 game–making tool Construct 2, which concludes the following:

Hardware accelerated 2D canvases are fast, but WebGL is still way faster. It is in the realm of native engines, which is frankly amazing considering how inefficiently designed JavaScript is, and is a testament to the amazing job browser makers have done in writing JIT compilers for JavaScript.

WebGL’s also made inroads in mobile browsers, gaining support in the mobile versions of Firefox, Opera, Chrome (if enabled), and IE (with a polyfill.) At the time of this writing mobile Safari is the only holdout and Apple hasn’t announced any plans to make it available.

That wraps up our discussion of Canvas, Gaming, and the future of immersive experiences without Flash. Next we’ll dive into the exciting and misleading world of HTML5 Audio and Video support.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.123.189