Interact Seattle presents an HTML5 Workshop: April 20, 2011

HTML5

HTML5

An introduction

HTML5 is about as new to me as it is to you.

I've studied everything that I will be presenting over the past three months.

What we’ll cover tonight

  • The Doctype
  • Modernizer
  • Link relations
  • New markup
  • Canvas
  • Video
  • Geolocation
  • HTML5 storage
  • Offline web application
  • HTML5 forms
  • Microdata

The Doctype

There are 15 doctypes

The Doctype

Standards Doctype looks like this

<!DOCTYPE html PUBLIC "-//W3C//DTD XHMTL 1.0 Strict//EN" "http://www.w3.org/TR/xhtml/DTD/xhtml-strict.dtd">

The Doctype

HTML5 Doctype looks like this

<!DOCTYPE html>

Modernizer

Modernizer is an HTML5 detection JavaScript library.

You need only to download the library from www.modernizr.com and refer to it in a <script> element in the <head> of you page, like so:

<script src="modernizer.min.js"> </script>

It will run automatically, creating a global object with a set of Boolean properties for each HTML5 feature it can detect.

Link relations

Link relations tell a browser why you need to reference another page.

Link relations

Link relations tell a browser why you need to reference another page.

  • rel="archives", for referencing records, documents, etc. of historical interest.
  • rel="author", for referencing the author of the site's content.
  • rel="external", to reference a document not part of the whole website
  • rel="start", rel="prev", rel="next" for referencing parts of a site which are assembled like a book or chronicle.
  • rel="icon", to reference a favicon
  • rel="license", to reference a copyright license.
  • rel="nofollow", to suggest that the referenced link is not endorsed by the original author of the page. It usually indicates a commercial relationship, such as advertising.
  • rel="noreferrer", instructs the browser not to leak referrer information when following the link.
  • rel="pingback" for blogs or sites to be automatically notified when other sites link to it.
  • rel="prefetch" can be used to indicate that preemptively caching the resource is encouraged.
  • rel="search", to indicate that the referenced document is configured for search.
  • rel="sidebar", for referencing a document to populate a sidebar, in a secondary browsing context.
  • rel="tag" to indicate that the referenced document is represented by a tag that applies to the current document.

New semantics

There are nine new markup elements defined in the HTML5 specification

New semantics

There are nine new markup elements defined in the HTML5 specification

  • <section> represents a generic section. It is useful for thematic grouping of content.
  • <nav> can create a section of a page that contains links to other pages or parts within the page.
  • <article> can describe a self-contained composition, with the intention of standing independently of the site.
  • <aside> can be used to wrap content that is related to the main content of a page, but is less important or is a separate piece of information quasi-related.
  • <hgroup> can be used to contain elements with multiple levels.
  • <header> contains introductory content, and/or navigational aids. It can also wrap a table of contents.
  • <time> is used to contain information of a chronological nature.
  • <mark> is used for “highlighting” information on a page.

The Canvas

New for HTML5, the <canvas> element can render resolution-dependent bitmaps of graphs, game graphics, etc., on-demand.

The Canvas

What if you want to use canvas for IE8?

Download the script from http://code.google.com/p/explorercanvas/

Then, you must place this in your <head> element:

<!--[if IE]> <script type='text/javascript' src='excanvas.js'></script> <![endif]-->

Limitations

  1. Gradients can only be linear.
  2. Patterns must repeat in both directions.
  3. Clipping regions are not supported.
  4. Strokes are not correctly scaled with nonuniform scaling.
  5. It is slow.

The Canvas

A few canvas demos

You can use fillStyle and strokeStyle to set colors for drawn objects. You can determine colors with hex, rgba and even hsla, with browser support.

Most popular browsers are supporting canvas as a 2D element. Some browsers, such as Firefox and Opera, are experimenting with a 3D canvas.

The Canvas

Example #1

This example demonstrates drawing a basic triangle, using fillStyle and strokeStyle, as well as x and y values for each of the points of the triangle.

The text in canvas example.

The Canvas

Example #2

To set up a canvas, you need to define an element ID so that it can be found with JavaScript. Width and height of the canvas also needs to be determined with inline code, not CSS.

The text in canvas example.

The Canvas

Example #3

The ImageData object is achieved using a <getImageData> method. You can also do image filtering, as I've done here with a color inversion filter applied.

The text in canvas example.

// Loop over each pixel and invert the color. for (var i = 0, n = pix.length; i < n; i += 4) { pix[i ] = 255 - pix[i ]; // red pix[i+1] = 255 - pix[i+1]; // green pix[i+2] = 255 - pix[i+2]; // blue // i+3 is alpha (the fourth element) }

The Canvas

Example #4

The text in canvas example.

You can create text in <canvas>. It gives you some nice design options, and the same fillStyle and strokeStyle properties that are applied to basic shapes can work here.

The Canvas

Example #5

The text in canvas example.

You can define shadows with the <canvas> API. Determine values with context.shadowOffsetX, context.shadowOffsetY, shadowBlur and shadowColor.

The Canvas

Example #6

Gradients can be created by providing source and destination coordinates for start and stop points. You can designate colors with hex values. You can also designate linear and radial gradients. These methods are createLinearGradient and createRadialGradient.

The text in canvas example.

The Canvas

Example #7: the Halma game

The game is demonstrated in Mark Pilgrim's Dive into HTML 5 and the O'Reilly book HTML5: Up and Running.

Video

There are five video container formats.

  1. MPEG-4
  2. Flash Video
  3. Ogg
  4. WebM
  5. Audio Video Interleave (AVI)

Video

Video markup is very similar to the <img src="#"> tag. It's just <video src="##"><video>.

Player controls are not presented by default. You can build your own interface with HTML, CSS and JavaScript.

You can also have a standard control interface load by including <control> in your <video> tag.

Use the <preload> attribute if you want a user's browser to start downloading the video as soon as the page loads. The <autoplay> attribute tells the user's browser to start downloading the video file as soon as the page loads and to play the video as soon as possible.

Usually you have to tell the browser what type of video file and codecs you used. Add this information with the "type" attribute in the source element.

Important: video files must be served with the proper MIME types.

For older browsers, you can fall back to the Flash player by coding your videos in H.264 and ACC audio in an MPEG-4 file.

You can create an object tag within a video tag and fallback to older video players gracefully because HTML5 will ignore all child elements of a video element except source.

Video

Video demo

This video was created with FireOgg, wrapped in an Ogg container and using the Theora coda for video and Vorbis codec for audio.

Geolocation

This API offers new property on the global navigator object, titled navigator.geolocation.

Geo location is opt-in only. Your browser can only reveal your location with your permission.

Getting the current location from a mobile user uses several different functions depending on the intended use of the geo location data.

Use watchPosition() function will callback everytime the user's location is changed. Their mobile device determines the optimum polling intervals, and send that information to your application. This can be used for updating a visible marker on a page.

Geolocation

The Geolocation API is very simple.

function get_location() {
navigator.geolocation.getCurrentPosition(show_map);
}

Geolocation

In case of IE, you can use Gears, an open source browser plug-in configured for Mac, Windows, Linux, Windows Mobile and Android. It is useful as a fallback for browsers which do not support geolocation.

HTML5 storage

This feature enables websites to store named key/value pairs locally, within the client web browser.

The data is stored locally and never transmitted to the remote server the way cookies are.

Web storage is supported by all major browsers, including IE 8+.

HTML5 storage

HTML5 web storage is beyond the scope of tonight's presentation. It can be returned in a future session if demand for it is evident.

HTML5 storage

The Halma game in the Canvas demo uses HTML5 storage. If you make a few moves, close the browser, and then return to it later, it will remember your last play.

See an example.

Offline web application

This feature enables websites to store named key/value pairs locally, within the client web browser.

The data is stored locally and never transmitted to the remote server the way cookies are.

Web storage is supported by all major browsers, including IE 8+.

The Halma game in the Canvas demo uses HTML5 storage. If you make a few moves and then close the browser, and return to it later, it will remember your last play.

HTML5 web storage is beyond the scope of tonight's presentation. It can be returned in a future session if demand for it is evident.

HTML5 forms

Microdata

One of the great promises of "Web 3.0" is semantics. Microdata lets you create your own attributes. But there is a method to it, otherwise your home-spun attributes won't validate.

The object is to extend web pages with additional semantics outside of the standard HTML elements.

Wrapping up

Resources for this presentation

Wrapping up

Questions

This presentation