How to Create a Visual Library of Images in HTML5 Canvas

by David Catuhe

As a user interface fan, I could not miss the opportunity to develop with HTML5 Canvas.  It unlocks a whole new set of ways to visualize images and data on the web.  In this tutorial, I’ll walk you through how to create one for your site.

Application overview

We will produce an application that will let us display a Magic the Gathering © (courtesy of www.wizards.com/Magic) cards collection. Users will be able to scroll and zoom using the mouse (like Bing Maps, for example).

Note: Image and data visualization is hardware intensive.  Learn about HTML5 hardware acceleration and why it’s important here.

You can see the final result here: https://bolaslenses.catuhe.com

The project source files can be downloaded here: https://www.catuhe.com/msdn/bolaslenses.zip

Cards are stored on Windows Azure Storage and use the Azure Content Distribution Network (CDN : a service that deploys data near the final users) in order to achieve maximum performances. An ASP.NET service is used to return cards list (using JSON format).

Tools

To write our application, we will use Visual Studio 2010 SP1 with Web Standards Update. This extension adds IntelliSense support in HTML5 page (which is a really important thing Description: Sourire).

So, our solution will contain an HTML5 page side by side with .js files (these files will contain JavaScript scripts). About debug, it is possible to set a breakpoint directly in the .js files under Visual Studio. Try using the F12 Developer tools in Internet Explorer 9.

Debug with Visual Studio 2010

Debug with Internet Explorer 9 (F12/Developer bar)

So, we have a modern developer environment with IntelliSense and debug support. Therefore, we are ready to start and first of all, we will write the HTML5 page.

The HTML5 page

Our page will be built around an HTML5 canvas which will be used to draw the cards:

If we dissect this page, we can note that it is divided into two parts:

The header part with the title, the logo and the special mentions

The main part (section) holds the canvas and the tooltips that will display the status of the application. There is also a hidden image (backImage) used as source for not yet loaded cards.

To build the layout of the page, a style sheet (full.css) is applied. Style sheets are a mechanism used to change the tags styles (in HTML, a style defines the entire display options for a tag):

Thus, this sheet is responsible for setting up the following display:

Style sheets are powerful tools that allow an infinite number of displays. However, they are sometimes complicated to setup (for example if a tag is affected by a class, an identifier and its container). To simplify this setup, the development bar of Internet Explorer 9 is particularly useful because we can use it to see styles hierarchy that is applied to a tag.
For example let’s take a look at the waitText tooltip with the development bar. To do this, you must press F12 in Internet Explorer 9 and use the selector to choose the tooltip:

Once the selection is done, we can see the styles hierarchy:

Thus, we can see that our div received its styles from the body tag and the .tooltip entry of the style sheet.

With this tool, it becomes possible to see the effect of each style (which can be disabled). It is also possible to add new style on the fly.

Another important point of this window is the ability to change the rendering mode of Internet Explorer 9. Indeed, we can test how, for example, Internet Explorer 8 will handle the same page. To do this, go to the [Browser mode] menu and select the engine of Internet Explorer 8. This change will especially impact our tooltip as it uses border-radius (rounded edge) and box-shadow that are features of CSS 3:

Internet Explorer 9 Internet Explorer 8

Our page provides a graceful degradation as it still works (with no annoying visual difference) when the browser does not support all the required technologies.

Now that our interface is ready, we will take a look at the data source to retrieve the cards to display.

Data gathering

The server provides the cards list using JSON format on this URL:

https://bolaslenses.catuhe.com/Home/ListOfCards/?colorString=0

It takes one parameter (colorString) to select a specific color (0 = all).

When developing with JavaScript, there is a good reflex to have (reflex also good in other languages too, but really important in JavaScript): one must ask whether what we want to develop has not been already done in an existing framework.

Indeed, there is a multitude of open source projects around JavaScript. One of them is jQuery which provides a plethora of convenient services.

Thus, in our case to connect to the URL of our server and get the cards list, we could go through a XmlHttpRequest and have fun to parse the returned JSON. Or we can use jQuery Description: Sourire.

So we will use the getJSON function which will take care of everything for us:

As we can see, our function stores the cards list in the listOfCards variable and calls two jQuery functions:

  • text that change the text of a tag

slideToggle that hides (or shows) a tag by animating its height

The listOfCards list contains objects whose format is:

  • ID: unique identifier of the card

Path: relative path of the card (without the extension)

It should be noted that the URL of the server is called with the “?jsoncallback=?” suffix. Indeed, Ajax calls are constrained in terms of security to connect only to the same address as the calling script. However, there is a solution called JSONP that will allow us to make a concerted call to the server (which of course must be aware of the operation). And fortunately, jQuery can handle it all alone by just adding the right suffix.

Once we have our cards list, we can set up the pictures loading and caching.

Cards loading & cache handling

The main trick of our application is to draw only the cards effectively visible on the screen. The display window is defined by a zoom level and an offset (x, y) in the overall system.

The overall system is defined by 14819 cards that are spread over 200 columns and 75 rows.

Also, we must be aware that each card is available in three versions:

High definition: 480x680 without compression (.jpg suffix)

Medium definition: 240x340 with standard compression (.50.jpg suffix)

Low definition: 120x170 with strong compression (.25.jpg suffix)

Thus, depending on the zoom level, we will load the correct version to optimize networks transfer.

To do this we will develop a function that will give an image for a given card. This function will be configured to download a certain level of quality. In addition it will be linked with lower quality level to return it if the card for the current level is not yet uploaded:

An ImageCache is built by giving the associated suffix and the underlying cache.

Here you can see two important functions:

  • load: this function will load the right picture and will store it in a cache (the msecnd.net url is the Azure CDN address of the cards)
  • getImageForCard: this function returns the card picture from the cache if already loaded. Otherwise it requests the underlying cache to return its version (and so on)

So to handle our 3 levels of caches, we have to declare three variables:

Selecting the right cover is only depending on zoom:

To give a feedback to the user, we will add a timer that will manage a tooltip that indicates the number of images currently loaded:

Again we note the use of jQuery to simplify animations.

We will now discuss the display of cards.

Cards display

To draw our cards, we need to actually fill the canvas using its 2D context (which exists only if the browser supports HTML5 canvas):

The drawing will be made by processListOfCards function (called 60 times per second):

This function is built around many key points:

If the cards list is not yet loaded, we display a tooltip indicating that download is in progress:

Subsequently, we define the position of the display window (in terms of cards and coordinates), then we proceed to clean the canvas:

Then we browse the cards list and call the drawImage function of the canvas context. The current image is provided by the active cache (depending on the zoom):

  • We also have to draw the scroll bar with the RoundedRectangle function that uses quadratic curves:

And finally, we need to compute the number of frames per second:

Drawing cards relies heavily on the browser's ability to speed up canvas rendering. For the record, here are the performances on my machine with the minimum zoom level (0.05):

Browser FPS
Internet Explorer 9 30
Firefox 5 30
Chrome 12 17
iPad (with a zoom level of 0.8) 7
Windows Phone Mango (with a zoom level of 0.8) 20 (!!)

The site even works on mobile phones and tablets as long as they support HTML5.

Here we can see the inner power of HTML5 browsers that can handle a full screen of cards more than 30 times per second!  This is possible through hardware acceleration.

Mouse management

To browse our cards collection, we have to manage the mouse (including its wheel).

For the scrolling, we'll just handle the onmouvemove, onmouseup and onmousedown events.

Onmouseup and onmousedown events will be used to detect if the mouse is clicked or not:

The onmousemove event is connected to the canvas and used to move the view:

This function (onMouseMove) calculates the current position and provides also the previous value in order to move the offset of the display window:

Note that jQuery also provides tools to manage mouse events.

For the management of the wheel, we will have to adapt to different browsers that do not behave the same way on this point:

We can see that everyone does what he wants.

The function to register with this event is:

And we will use this function to change the zoom with the wheel:

Finally we will add a bit of inertia when moving the mouse (and the zoom) to give some kind of smoothness:

This kind of small function does not cost a lot to implement, but adds a lot to the quality of user experience.

State storage

Also to provide a better user experience, we will save the display window’s position and zoom. To do this, we will use the service of localStorage (which saves pairs of keys / values ​​for the long term (the data is retained after the browser is closed) and only accessible by the current window object):

Animations

To add even more dynamism to our application we will allow our users to double-click on a card to zoom and focus on it.

Our system should animate three values: the two offsets (X, Y) and the zoom. To do this, we will use a function that will be responsible of animating a variable from a source value to a destination value with a given duration:

The use of this function is:

The advantage of the AnimationHelper function is that it is able to animate as many parameters as you wish (and that only with the setTimer function!)

Handling multi-devices

Finally we will ensure that our page can also be seen on tablets PC and even on phones.

To do this, we will use a feature of CSS 3: The media-queries. With this technology, we can apply style sheets according to some queries such as a specific display size:

Here we see that if the screen width is less than 480 pixels, the following style sheet will be added:

This sheet will reduce the size of the header to keep the site viewable even when the browser width is less than 480 pixels (for example, on a Windows Phone):

Conclusion

HTML5 / CSS 3 / JavaScript and Visual Studio 2010 allow to develop portable and efficient solutions (within the limits of browsers that support HTML5 of course) with some great features such as hardware accelerated rendering.

This kind of development is also simplified by the use of frameworks like jQuery.

Also, I am especially fan of JavaScript that turns out to be a very powerful dynamic language. Of course, C# or VB.NET developers have to change theirs reflexes but for the development of web pages it's worth.

In conclusion, I think that the best to be convinced is to try!

To go further

About the Author

David Catuhe is a developer evangelist for Microsoft France in charge of user experience development tools (from XAML to DirectX/XNA and HTML5). He defines himself as a geek and likes coding all that refer to graphics. Before working for Microsoft, he founded a company that developed a realtime 3D engine written with DirectX (www.vertice.fr)."