written by David Catuhe
Last week in our tutorial on How To Create a Visual Library of Images in HTML5 Canvas, we provided you with an overview of an application that will allow us to display a Magic the Gathering cards collection, and discussed the tools we will use. This week we will show you the HTML5 code that is used, and we will delve into data gathering.
The HTML 5 page
Our page will be built around an HTML 5 canvas which will be used to draw the cards:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset=”utf-8″ />
- <title>Bolas Lenses</title>
- <link href=”Content/full.css” rel=”stylesheet” type=”text/css” />
- <link href=”Content/mobile.css” rel=”stylesheet” type=”text/css” media=”screen and (max-width: 480px)” />
- <link href=”Content/mobile.css” rel=”stylesheet” type=”text/css” media=”screen and (max-device-width: 480px)” />
- <script src=”Scripts/jquery-1.5.1.min.js” type=”text/javascript”></script>
- </head>
- <body>
- <header>
- <div id=”legal”>
- Cards scanned by <a href=”http://www.slightlymagic.net/”>MWSHQ Team</a><br />
- Magic the Gathering official site : <a href=”http://www.wizards.com/Magic/TCG/Article.aspx?x=mtg/tcg/products/allproducts”>
- http://www.wizards.com/Magic</a>
- <div id=”cardsCount”>
- </div>
- </div>
- <div id=”leftHeader”>
- <img id=”pictureCell” src=”/Content/MTG Black.png” alt=”Bolas logo” id=”bolasLogo” />
- <div id=”title”>
- Bolas Lenses
- </div>
- </div>
- </header>
- <section>
- <img src=”Content/Back.jpg” style=”display: none” id=”backImage” alt=”backImage”
- width=”128″ height=”128″ />
- <canvas id=”mainCanvas”>
- Your browser does not support HTML5 canvas.
- </canvas>
- <div id=”stats” class=”tooltip”>
- </div>
- <div id=”waitText” class=”tooltip”>
- Loading data…
- </div>
- </section>
- <!–Scripts–>
- <script src=”Bolas/bolasLenses.animations.js” type=”text/javascript”></script>
- <script src=”Bolas/bolasLenses.mouse.js” type=”text/javascript”></script>
- <script src=”Bolas/bolasLenses.cache.js” type=”text/javascript”></script>
- <script src=”Bolas/bolasLenses.js” type=”text/javascript”></script>
- </body>
- </html>
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):
- html, body
- {
- height: 100%;
- }
- body
- {
- background-color: #888888;
- font-size: .85em;
- font-family: “Segoe UI, Trebuchet MS” , Verdana, Helvetica, Sans-Serif;
- margin: 0;
- padding: 0;
- color: #696969;
- }
- a:link
- {
- color: #034af3;
- text-decoration: underline;
- }
- a:visited
- {
- color: #505abc;
- }
- a:hover
- {
- color: #1d60ff;
- text-decoration: none;
- }
- a:active
- {
- color: #12eb87;
- }
- header, footer, nav, section
- {
- display: block;
- }
- table
- {
- width: 100%;
- }
- header, #header
- {
- position: relative;
- margin-bottom: 0px;
- color: #000;
- padding: 0;
- }
- #title
- {
- font-weight: bold;
- color: #fff;
- border: none;
- font-size: 60px !important;
- vertical-align: middle;
- margin-left: 70px
- }
- #legal
- {
- text-align: right;
- color: white;
- font-size: 14px;
- width: 50%;
- position: absolute;
- top: 15px;
- right: 10px
- }
- #leftHeader
- {
- width: 50%;
- vertical-align: middle;
- }
- section
- {
- margin: 20px 20px 20px 20px;
- }
- #mainCanvas{
- border: 4px solid #000000;
- }
- #cardsCount
- {
- font-weight: bolder;
- font-size: 1.1em;
- }
- .tooltip
- {
- position: absolute;
- bottom: 5px;
- color: black;
- background-color: white;
- margin-right: auto;
- margin-left: auto;
- left: 35%;
- right: 35%;
- padding: 5px;
- width: 30%;
- text-align: center;
- border-radius: 10px;
- -webkit-border-radius: 10px;
- -moz-border-radius: 10px;
- box-shadow: 2px 2px 2px #333333;
- }
- #bolasLogo
- {
- width: 64px;
- height: 64px;
- }
- #pictureCell
- {
- float: left;
- width: 64px;
- margin: 5px 5px 5px 5px;
- vertical-align: middle;
- }
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 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:
.
Once we have our cards list, we can set up the pictures loading and caching.
This article was reprinted with permission from Microsoft Corporation. The original is available here. This site does business with Microsoft Corporation.