<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>User Agent Man &#187; HTML</title>
	<atom:link href="http://www.useragentman.com/blog/category/technologies/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.useragentman.com/blog</link>
	<description>A Blog about Client Side Web Technology</description>
	<lastBuildDate>Thu, 26 Jan 2012 03:14:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Cross Browser HTML5 Progress Bars In Depth</title>
		<link>http://www.useragentman.com/blog/2012/01/03/cross-browser-html5-progress-bars-in-depth/</link>
		<comments>http://www.useragentman.com/blog/2012/01/03/cross-browser-html5-progress-bars-in-depth/#comments</comments>
		<pubDate>Tue, 03 Jan 2012 07:08:54 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[CSS3]]></category>
		<category><![CDATA[Forms]]></category>
		<category><![CDATA[gradients]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Polyfills]]></category>
		<category><![CDATA[progress]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=4016</guid>
		<description><![CDATA[<img src="http://www.useragentman.com/blog/wp-content/uploads/2012/01/intro.png" />  As a web application developer,<strong> progress bars are great when you want to show the user that some action is happening,</strong> especially when it can take a long time.  Creating them is easy with the HTML5 <code>&#60;progress&#62;</code> tag.  This article will discuss <strong>how this tag is rendered by default</strong> in all operating systems and browsers and how to <strong>style the <code>progress</code> tag with CSS</strong>, even in browsers that don't officially support the it.  It will also show some <strong>interesting examples using advanced CSS3 techniques.</strong>]]></description>
			<content:encoded><![CDATA[<div class="importantNotes">
<h3>Note:</h3>
<p>The stylesheet for the polyfill was not included correctly when this post was first published, and as a result, the examples below were not showing up correctly on IE7-8 and Safari 5.1+.  The problem has been corrected.</p>
</div>
<p><div id="attachment_4224" class="wp-caption alignleft" style="width: 260px"><img src="http://www.useragentman.com/blog/wp-content/uploads/2012/01/intro.png" alt="" title="intro" width="250" height="180" class="size-full wp-image-4224" /><p class="wp-caption-text">Screenshots of HTML5 progress bars with different styles applied.  Details given below.</p></div> As a web application developer,<strong> progress bars are great when you want to show the user that some action is happening,</strong> especially when it can take a long time.  They can be <strong>animated</strong> (like the one in Gmail does when it shows the user how long it is going to take for it to load and initialize), <strong>or static</strong> (like some shopping cart applications have to show the user how many pages it will take to check out an order).  I used to <a href="http://css-tricks.com/css3-progress-bars/">create progress bars using <code>&lt;div&gt;</code> tags, CSS and a litle bit of math</a>, but now I like to do it the HTML5 way using the <code>&lt;progress&gt;</code> tag.  This article will discuss <strong>how this tag is rendered by default</strong> in all operating systems and browsers and how to <strong>style the <code>progress</code> tag with CSS</strong>, even in browsers that don&#8217;t officially support the it.  It will also discuss some interesting <strong>limitations of all the browser implementations</strong> amd show some <strong>interesting examples using advanced CSS3 techniques.</strong></p>
<h2>The HTML: Simple</h2>
<p>The HTML for a Progress bar is dead simple:</p>
<blockquote class="code">
<pre>
&lt;progress max="100" value="60"&gt;
  &lt;strong&gt;Progress: 60% done.&lt;/strong&gt;
&lt;/progress&gt;
</pre>
</blockquote>
<div class="example">
<progress max="100" value="60"><strong>Progress: 60% done.</strong></progress>
</div>
<p><strong>Note that the HTML inside the <code>&lt;progress&gt;</code> tag is the fallback for browsers that do not support it.</strong>  That, unfortunately, includes all versions of IE and Safari so far, as well older versions of Firefox (5.x and lower) and Opera (10.x and lower).  Although the fallback is acceptable, we can go a step further and use <strong><a href="http://lea.verou.me/">Lea Verou&#8217;s</a> excellent <a href="http://lea.verou.me/2011/07/a-polyfill-for-html5-progress-element-the-obsessive-perfectionist-way/"><code>&lt;progress&gt;</code> tag polyfill</a></strong>, which adds pretty much full-support for all of these browsers <strong>except for Safari 5 and lower</strong> (so you should <em><strong>always put in the fallback HTML just to be on the safe side</strong></em>).  Let&#8217;s take a look at these screenshots to see what how <code>&lt;progress&gt;</code> looks across the browserverse:</p>
<table class="dataTable">
<thead>
<tr>
<th></th>
<th>Windows 7</th>
<th>Windows XP</th>
<th>Mac OS X</th>
<th>Ubuntu Linux</th>
</thead>
<tbody>
<tr>
<th>Firefox</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/defaultFirefoxWin.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/defaultFirefoxWinXP.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/defaultFirefoxMac.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/defaultFirefoxLinux.png" alt=""  /></td>
</tr>
<tr>
<th>Chrome</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/defaultChromeWin.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/defaultChromeWinXP.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/defaultChromeMac.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/defaultChromeLinux.png" alt=""  /></td>
</tr>
<tr>
<th>IE7+ (polyfill)</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/defaultSafariWin_poly.png" alt=""  /></td>
<td colspan="3" class="na">N/A</td>
</tr>
<tr>
<th>Safari 5.1+ (polyfill)</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/defaultSafariWin_poly.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/defaultSafariWin_poly.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/defaultSafariWin_poly.png" alt=""  /></td>
<td class="na">N/A</td>
</tr>
<tr>
<th>Opera</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/defaultOperaWin.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/defaultOperaWin.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/defaultOperaMac.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/defaultOperaLinux.png" alt=""  /></td>
</tr>
</tbody>
</table>
<p>Note that:</p>
<ul>
<li>Firefox and Chrome will render the progress bar the same way that the host operating system would &#8230; except for Chrome for Linux, which uses it&#8217;s own custom style (thanks to <a href="http://blog.oldworld.fr/">Mounir Lamouri</a> for correcting me on this exception).</li>
<li>The color of the Opera progress value is always green (more on this later).</li>
<li>The browsers that use the polyfill all render the progress bar with a nice bluish gradient effect</li>
</ul>
<p>A progress bar <strong>can also have an <em>&#8220;indeterminate&#8221;</em> state</strong>, which happens when there is <strong>no value attribute</strong>. </p>
<blockquote class="code">
<pre>
&lt;progress max="100"&gt;
  &lt;strong&gt;Progress: 60% done.&lt;/strong&gt;
&lt;/progress&gt;
</pre>
</blockquote>
<div class="example">
<progress max="100" ><strong>Progress: 60% done.</strong></progress>
</div>
<p>This effect is used to show that the state of progress is currently unknown (e.g. how long it will take for a web server to initiate the download of a file if is generating it on the fly).  How this looks varies from browser to browser as well.</p>
<table class="dataTable">
<thead>
<tr>
<th></th>
<th>Windows 7</th>
<th>Windows XP</th>
<th>Mac OS X</th>
<th>Ubuntu Linux</th>
</thead>
<tbody>
<tr>
<th>Firefox</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/indeterFirefoxWin.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/indeterFirefoxWinXP.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/indeterFirefoxMac.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/indeterFirefoxLinux.png" alt=""  /></td>
</tr>
<tr>
<th>Chrome</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/indeterChromeWin.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/indeterChromeWinXP.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/indeterChromeMac.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/indeterChromeLinux.png" alt=""  /></td>
</tr>
<tr>
<th>IE7+ (polyfill)</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/indeterSafariWin_poly.png" alt=""  /></td>
<td colspan="3" class="na">N/A</td>
</tr>
<tr>
<th>Safari 5.1+ (polyfill)</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/indeterSafariWin_poly.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/indeterSafariWin_poly.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/indeterSafariWin_poly.png" alt=""  /></td>
<td class="na">N/A</td>
</tr>
<tr>
<th>Opera</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/indeterOperaWin.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/indeterOperaWin.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/indeterOperaMac.png" alt=""  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/12/indeterOperaLinux.png" alt=""  /></td>
</tr>
</tbody>
</table>
<p>Note that:</p>
<ul>
<li>Opera is the only browser that doesn&#8217;t distinguish between a progress bar with an indeterminate state and one with a value of zero.</li>
<li>All of the other browsers (including the ones that use the polyfill) animate the indeterminate states.  (I have opted to not show all the animations here to avoid readers getting seizures &#8230; I hear that would be a bad thing).</li>
</ul>
<h2>But I Want To Style Them My Way!</h2>
<p>If you are particular in how you want your <code>&lt;progress&gt;</code> tags to look, the good news is that <strong>you can pretty much style them any way you want</strong>.  You must, however, <strong>be aware about the browser quirks</strong> that can trip you up &#8230; and it isn&#8217;t all IE&#8217;s fault this time!  Follow this three-to-four-step process, and you&#8217;ll be styling progress bars in your sleep in no time:</p>
<h3>Step 1: Turn off default styling</h3>
<p>The first step is to turn off the default styling in all browsers:</p>
<blockquote class="code">
<pre>
progress,          /* All HTML5 progress enabled browsers */
progress[role]     /* polyfill */
{

	/* Turns off styling - not usually needed, but good to know. */
	appearance: none;
	-moz-appearance: none;
	-webkit-appearance: none;

	/* gets rid of default border in Firefox and Opera. */
	border: none;

	/* Needs to be in here for Safari polyfill so background images work as expected. */
	background-size: auto;

	/* Dimensions */
	width: 400px;
	height: 60px;

}

/* Polyfill */
progress[role]:after {
	background-image: none; /* removes default background from polyfill */
}

/* Ensure fallback text doesn't appear in polyfill */
progress[role] strong {
	display: none;
}
</pre>
</blockquote>
<div class="example">
<progress class="example1 a" min="0" max="100" value="50">
<strong>Progress: 60% done.</strong>
</progress>
</div>
<p>Simple stuff: remove the border and add a specific width and height.  I added the second rule to remove the background image inserted by the polyfill&#8217;s stylesheet, but if you wanted, you can  modify the polyfill&#8217;s stylesheet directly (or leave it out completely if you elect not to use the polyfill).   The final rule is to ensure the polyfill doesn&#8217;t display the fallback content &mdash; it assumes that is always wrapped in a <code>&lt;strong&gt;</code> tag, so this may be something you should keep in mind when setting the default content (if you don&#8217;t like using a <code>&lt;strong&gt;</code> tag as a wrapper for your fallback content, use whatever tag you like).</p>
<p>Note that the <code>appearance</code> property (and its vendor-specific brethren) are there to turn off the default operating-system styling on the progress bar &mdash; it doesn&#8217;t seem like it is really necessary, but I put it here for reference in case it becomes mandatory in the future.</p>
<h3>Step 2: The Progress Bar Background.</h3>
<p>Now let&#8217;s change the background color of the progress bar to a light red.  </p>
<blockquote class="code">
<pre>
progress,                          /* Firefox  */
progress[role][aria-valuenow] {    /* Polyfill */
   background: #ffeeee !important; /* !important is needed by the polyfill */
}

/* Chrome */
progress::-webkit-progress-bar {
    background: #ffeeee;
}
</pre>
</blockquote>
<div class="example">
<progress class="example1 a b" min="0" max="100" value="50"></progress>
</div>
<p>Notice that with Firefox and the polyfilled browsers, all you need to do is change the background of just the <code>progress</code> tag itself, while in Chrome (and I assume future versions of Safari) it is necessary to use the <code>-webkit-progress-bar</code> pseudo-element. Note that even though the code inside these rules are the same,<strong> you cannot put all of these selectors in one rule</strong>: doing so breaks Firefox and Opera (so much for degrading gracefully).</p>
<h3>Step 3: The Progress Bar Value</h3>
<p>Now let&#8217;s change the color of progress bar value to black.  The CSS is a <em>wee bit longer than it really should be</em>:</p>
<blockquote class="code">
<pre>
/* Firefox */
progress::-moz-progress-bar {
    background: black;
}

/* Chrome */
progress::-webkit-progress-value {
    background: black;
}

/* Polyfill */
progress[aria-valuenow]:before  {
    background: black;
}
</pre>
</blockquote>
<div class="example">
<progress class="example1 a b c" min="0" max="100" value="50"></progress>
</div>
<p>Yes, three rules to rule them all!  Again, putting them all together in one selector breaks every browser on the planet (including the polyfilled ones), so we have to write three separate rules with the same CSS properties in them! &lt;sarcasm&gt;Yaaayyy!!!!&lt;/sarcasm&gt;</p>
<p><strong>Note that there is no way that I know of to style the progress bar value in Opera 11.52 and lower.</strong>  It just stays the same green no matter what you do. :-( If anyone is reading this and knows otherwise, I would be indebted to you to let me know how.</p>
<h3>Step 4: The Indeterminate Value</h3>
<p>This part is optional, and I would only put these rules in if I know I&#8217;ll need a style for the indeterminate value (not all applications need it):</p>
<blockquote class="code">
<pre>
/* Firefox */
progress:not([value])::-moz-progress-bar {
  background-image:  url(../images/indeter.gif);
}

/* Chrome */
progress:not([value])::-webkit-progress-bar {
  background-image:  url(../images/indeter.gif);
}

/* Polyfill - IE */
progress[role]{
	background-image: url(../images/indeter.gif) !important;
}

/* Polyfill - Safari */
progress:not([value])  {
	background-image: url(../images/indeter.gif) !important;
	background-size: auto;  /* Needs to be in here for Safari */
}
</pre>
</blockquote>
<div class="example">
<progress class="example1 a b c d" min="0" max="100"></progress>
</div>
<p>It even works in Opera! Note that the <code>background-size</code> must be set to <code>auto</code> in order to override the default style in the polyfill.  :-)</p>
<p><a href="http://www.useragentman.com/examples/progress/" class="exampleLink">See the above CSS in action in a &#8220;clean room&#8221; page</a></p>
<h2>That&#8217;s Too Basic! I Want Fancy-Pants&trade; Progress Bars</h2>
<p>So, now that you know the basics, let&#8217;s take a look at some more complicated and interesting progress bars:</p>
<h3>Two Image Effect</h3>
<p>This progress bar uses two versions of the same image (one grey-scale, one color) to differentiate between the progress bar background and value: </p>
<div class="example">
<progress class="example2" min="0" max="100" value="60"></progress>
</div>
<p><a href="http://www.useragentman.com/examples/progress/uam.html" class="exampleLink">See the above example in action in a &#8220;clean room&#8221; page</a></p>
<h3>Gradients</h3>
<p>Note that  not only can you use <code>background-color</code>s and <code>-image</code>s, but also use the variety of gradients that are available for developers today in supported browsers (this does <strong>not</strong> include IE9 and lower).  To prove this, I took Chris Croyer beautiful <code>&lt;div&gt;</code> style progress bars from his <a href="http://css-tricks.com/css3-progress-bars/">blog post on CSS3 based progress bars</a> and HTML5-ified&trade; them here:</p>
<div class="example">
<progress class="example3" min="0" max="100" value="60"></progress>
</div>
<div class="example">
<progress class="example4" min="0" max="100" value="60"></progress>
</div>
<p>Unfortunately, due to CSS gradients being implemented by the browser vendors using vendor-specific prefixes, the CSS tends to be a wee long.  Here is the CSS of the first example (scroll the code to see the whole lot of it):</p>
<blockquote class="code shortened">
<pre>
/*
 * Gradient Shadow
 */

/* All HTML5 progress enabled browsers */
progress.example3 {

	/* Turns off styling - not usually needed, but good to know. */
	appearance: none;
	-moz-appearance: none;
	-webkit-appearance: none;
	/* gets rid of default border in Firefox and Opera. */
	border: solid #cccccc 5px;
	border-radius: 10px;
	/* Dimensions */
	width: 238px;
	height: 45px;
}

/* Polyfill */
progress.example3[role]:after {
	background-image: none; /* removes default background from polyfill */
}

/*
 * Background of the progress bar background
 */

/* Firefox and Polyfill */
progress.example3 {
	background: #cccccc !important; /* !important only needed in polyfill */
}

/* Chrome */
progress.example3::-webkit-progress-bar {
	background: #cccccc;
}

/*
 * Background of the progress bar value
 */

/* Firefox */
progress.example3::-moz-progress-bar {
	border-radius: 5px;
	background-image: -moz-linear-gradient(
		center bottom,
		rgb(43,194,83) 37%,
		rgb(84,240,84) 69%
	);
}

/* Chrome */
progress.example3::-webkit-progress-value {
	border-radius: 5px;
	background-image: -webkit-gradient(
		linear,
		left bottom,
		left top,
		color-stop(0, rgb(43,194,83)),
		color-stop(1, rgb(84,240,84))
	);
	background-image: -webkit-linear-gradient(
		center bottom,
		rgb(43,194,83) 37%,
		rgb(84,240,84) 69%
	);
}

/* Polyfill */
progress.example3[aria-valuenow]:before {
	border-radius: 5px;
	background-image: -moz-linear-gradient(
		center bottom,
		rgb(43,194,83) 37%,
		rgb(84,240,84) 69%
	);
	background-image: -ms-linear-gradient(
		center bottom,
		rgb(43,194,83) 37%,
		rgb(84,240,84) 69%
	);
	background-image: -o-linear-gradient(
		center bottom,
		rgb(43,194,83) 37%,
		rgb(84,240,84) 69%
	);

}
</pre>
</blockquote>
<p>Note that I did not use the <a href="http://msdn.microsoft.com/en-us/library/ms532997%28v=vs.85%29.aspx"><code>Gradient</code> filter</a> to polyfill CSS3 gradients in IE6-9.  This is because Visual Filters don&#8217;t work in CSS3 pseudo-elements like <code>:before</code> or <code>:after</code>.  Oh well.</p>
<p><a href="http://www.useragentman.com/examples/progress/striping.html" class="exampleLink">See the above CSS in action in a “clean room” page</a></p>
<h3>Monkeys!</h3>
<p>What blog post about HTML5 progress tags would be complete without monkeys?  These simians appear in all browser except for IE8 and lower (as explained below).  </p>

<div class="example">
<progress class="monkey" min="0" max="100" value="60"></progress><div class="after"></div>
</div>
<p>Apologies to the great <a href="http://banksy.co.uk/">Banksy</a> &mdash; it&#8217;s not <a href="http://www.guyhepner.com/index.php?url=images/get/509x397/3087/banksy-paintings-laugh-now-but-one-day-we-ll-be-in-charge.jpg">his monkey</a> but one grabbed from <a href="http://www.animatedgif.net/clockscounters/clockscounters3.shtml">this Animated GIF site</a> and converted into a <a href="http://www.alistapart.com/articles/sprites">CSS sprite</a> using <a href="http://blog.somepixels.net/">André Gil&#8217;s</a> super-cool <a href="http://labs.andregil.net/Gif2TileSet/Gif2TileSet.html">Gif2TileSet tool</a>.  I took this sprite and animated the monkey using blitting whenever the progress bar changes values (thanks to my friend and colleague <a href="http://noel.tibbles.ca/">Noel Tibbles</a> for turning me on to this technique).</p>
<p>Note the HTML has an extra <code>&lt;div&gt;</code> tag so the monkey has a place to live:</p>
<blockquote class="code">
<pre>
&lt;progress class="monkey" min="0" max="100" value="60"&gt;&lt;/progress&gt;
&lt;div class="after"&gt;&lt;/div&gt;
</pre>
</blockquote>
<p><strong>I wish that I could have used <code>:after</code> (or <code>::after</code>) rules</strong> instead, but these pseudo-elements <strong>don&#8217;t work with the <code>progress</code> tags in <em>any</em> browser that doesn&#8217;t use the polyfill</strong>.  And no, <code>:before</code> doesn&#8217;t work either.  I have no idea why it doesn&#8217;t work, but it&#8217;s a shame &mdash; using them would be perfect to get rid of the extra markup.</p>
<p>The CSS that makes the monkey animate when the progress bar changes values is here:</p>
<blockquote class="code">
<pre>
progress.monkey[value="0"] +  .after{
	background: url('/blog/wp-content/uploads/2011/12/monkeyBlit.gif');
} 

progress.monkey[value^="1"] +  .after {
	background: url('/blog/wp-content/uploads/2011/12/monkeyBlit.gif') 0  -77px ;
}

progress.monkey[value^="2"] +  .after {
	background: url('/blog/wp-content/uploads/2011/12/monkeyBlit.gif') 0  -154px ;
}

progress.monkey[value^="3"] +  .after,
progress.monkey[value="100"] +  .after  {
	background: url('/blog/wp-content/uploads/2011/12/monkeyBlit.gif') 0  -231px !important ;
}

progress.monkey[value^="4"] +  .after {
	background: url('/blog/wp-content/uploads/2011/12/monkeyBlit.gif') 0  -308px ;
}

progress.monkey[value^="5"] +  .after {
	background: url('/blog/wp-content/uploads/2011/12/monkeyBlit.gif') 0  -385px ;
}

progress.monkey[value^="6"] +  .after {
	background: url('/blog/wp-content/uploads/2011/12/monkeyBlit.gif') 0  -462px ;
}

progress.monkey[value^="7"] +  .after {
	background: url('/blog/wp-content/uploads/2011/12/monkeyBlit.gif') 0  -539px ;
}

progress.monkey[value^="8"] +  .after {
	background: url('/blog/wp-content/uploads/2011/12/monkeyBlit.gif') 0  -616px ;
}

progress.monkey[value^="9"] +  .after {
	background: url('/blog/wp-content/uploads/2011/12/monkeyBlit.gif') 0  -693px ;
}
</pre>
</blockquote>
<p>Each of these rules are applied when the first digit changes in the progress bar, which is done using the ^= attribute rules. This works because the progress bar goes from 0 &#8211; 100 and the progress bar increments by 10.  They monkeys don&#8217;t appear in IE8 and under because of it&#8217;s lack of support of the ^= attribute selector (but it works well in IE9).</p>
<p>Note that <strong>this would be much easier if CSS allowed us to combine the CSS3 <code>calc()</code> and <code>attr()</code> attributes together</strong> like <a href="http://lea.verou.me/2010/09/on-attr-and-calc/">Lea Verou dreams about in one of her blog posts</a>:</p>
<blockquote class="code">
<pre>
/*
 * Don't try this - it doesn't work in any browser, but it would be nice if it did.
 */

progress.monkey::after {
   background-image: url('/blog/wp-content/uploads/2011/12/monkeyBlit.gif');
   background-position-x: 0;
   background-position-y: calc(-77 * attr(value), 'px');
}
</pre>
</blockquote>
<p>Hopefully this will come to pass in the future.</p>
<p><a href="http://www.useragentman.com/examples/progress/blit.html" class="exampleLink">See the above CSS in action in a “clean room” page</a></p>
<h3>Speedometer</h3>
<p>My last example is radically different than the others.  How about we apply the speedometer metephor to the progress bar with some fancy-pants CSS3?   This example in all browsers except IE8 and lower (due to lack of support for native CSS3 <code>transform</code>) </p>

<div class="example speedometer">
<div id="progressContainer">
  <progress id="rot" class="example_r" min="0" max="100" value="60"></progress>
  <div data-arrow-for="rot" class="arrow"><img src="/blog/wp-content/uploads/2012/01/hand.png" /></div>
</div>
</div>
<p>The CSS is similar to the monkey example in that I add an extra <code>&lt;div&gt;</code> after the progress tag, except that it has an image of a pointer inside of it.</p>
<blockquote class="code">
<pre>
&lt;div id="progressContainer"&gt;
  &lt;progress id="rot" class="example_r" min="0" max="100" value="60"&gt;&lt;/progress&gt;
  &lt;div data-arrow-for="rot" class="arrow"&gt;&lt;img src="/blog/wp-content/uploads/2012/01/hand.png" /&gt;&lt;/div&gt;
&lt;/div&gt;
</pre>
</blockquote>
<p>I wrapped both these tags inside a relatively positioned container <code>&lt;div&gt;</code>.  This is so I could absolutely position both the progress bar and the arrow in order for them to line up properly.  I make the progress bar into a semi-circle by using <code>border-radius</code> and part of a <a href="http://www.flickr.com/photos/listener42/4991330345/">photo from Flickr user &#8216;listener42&#8242;</a> &#8230;</p>
<blockquote class="code">
<pre>
progress.example_r {

	/* gets rid of default border in Firefox and Opera. */
	border: solid 1px black;
	display: inline-block;

	/* Produces the semi-circle */
	<span class="hilite">border-radius: 238px 238px 0 0;</span>

	/* Dimensions */
	width: 238px;
	height: 126px;
	padding: 0;
}
</pre>
</blockquote>
<p>&#8230; and make the arrow rotate as the progress bar increments by using CSS transforms: </p>
<blockquote class="code shortened">
<pre>
progress.example_r[value="0"] + .arrow {
	-moz-transform: rotate(270deg);
	-webkit-transform: rotate(270deg);
	-o-transform: rotate(270deg);
	-ms-transform: rotate(270deg);
}

progress.example_r[value^="1"]:not([value="1"]):not([value="100"]) + .arrow {
	-moz-transform: rotate(288deg);
	-webkit-transform: rotate(288deg);
	-o-transform: rotate(288deg);
	-ms-transform: rotate(288deg);
}

progress.example_r[value^="2"]:not([value="2"]) + .arrow {
	-moz-transform: rotate(306deg);
	-webkit-transform: rotate(306deg);
	-o-transform: rotate(306deg);
	-ms-transform: rotate(306deg);
}

progress.example_r[value^="3"]:not([value="3"]) + .arrow {
	-moz-transform: rotate(324deg);
	-webkit-transform: rotate(324deg);
	-o-transform: rotate(324deg);
	-ms-transform: rotate(324deg);
}

progress.example_r[value^="4"]:not([value="4"]) + .arrow {
	-moz-transform: rotate(342deg);
	-webkit-transform: rotate(342deg);
	-o-transform: rotate(342deg);
	-ms-transform: rotate(342deg);
}

progress.example_r[value^="5"]:not([value="5"]) + .arrow {
	-moz-transform: rotate(360deg);
	-webkit-transform: rotate(360deg);
	-o-transform: rotate(360deg);
	-ms-transform: rotate(360deg);
}

progress.example_r[value^="6"]:not([value="6"]) + .arrow {
	-moz-transform: rotate(378deg);
	-webkit-transform: rotate(378deg);
	-o-transform: rotate(378deg);
	-ms-transform: rotate(378deg);
}

progress.example_r[value^="7"]:not([value="7"]) + .arrow {
	-moz-transform: rotate(396deg);
	-webkit-transform: rotate(396deg);
	-o-transform: rotate(396deg);
	-ms-transform: rotate(396deg);
}

progress.example_r[value^="8"]:not([value="8"]) + .arrow {
	-moz-transform: rotate(414deg);
	-webkit-transform: rotate(414deg);
	-o-transform: rotate(414deg);
	-ms-transform: rotate(414deg);
}

progress.example_r[value^="9"]:not([value="9"]) + .arrow {
	-moz-transform: rotate(432deg);
	-webkit-transform: rotate(432deg);
	-o-transform: rotate(432deg);
	-ms-transform: rotate(432deg);
}

progress.example_r[value="100"] + .arrow {
	-moz-transform: rotate(450deg);
	-webkit-transform: rotate(450deg);
	-o-transform: rotate(450deg);
	-ms-transform: rotate(450deg);
}
</pre>
</blockquote>
<p>Again, the CSS would be much smaller in size if I could use <code>calc()</code> and <code>attr()</code> together, but again, oh well.   Also note that this example doesn&#8217;t work in Opera very well because we cannot turn of the green in the progress bar value.  :-(</p>
<p><a href="http://www.useragentman.com/examples/progress/rotate.html" class="exampleLink">See the above CSS in action in a “clean room” page</a></p>
<h2>Summary of Gotchas</h2>
<p>As mentioned before, there are a few annoyances I have found with they way the browsers have implemented HTML5 progress bars:</p>
<ol>
<li>You cannot use <code>:before</code>/<code>::before</code> or <code>:after</code>/<code>::after</code> pseudo-elements on the <code>progress</code> element.  Why this is not allowed is unclear to me, and I hope this is allowed in the future.</li>
<li>Safari 5.0 and lower cannot use the polyfill, so you should always use the fallback HTML inside the <code>&lt;progress&gt;</code> tag.</li>
<li>It seems like it is impossible to change Opera&#8217;s progress bar style to anything besides green.</li>
<li>There is a <strong>small bug in the polyfill</strong> when applying borders to the progress bar.  I have <a href="https://github.com/zoltan-dulac/HTML5-Progress-polyfill">a fix</a> that has been <a href="https://github.com/LeaVerou/HTML5-Progress-polyfill/pull/6">submitted as a pull request</a> and I assume being reviewed, but in the meantime, you can <a href="https://github.com/zoltan-dulac/HTML5-Progress-polyfill">get my forked version of the polyfill (with the bug fix) on GitHub</a>.</li>
</ol>
<h2>Help Me Keep This Page Up-To-Date!</h2>
<p>If you find out more information about the HTML5 <code>progress</code> tag, I&#8217;d love to hear from you!  Please let me know in the comments below.  I will add relevant information to the above article and credit you fully. :-)</p>
<p><script src="/shared/js/lea.verou.me/progressPolyfill/progress-polyfill.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2012/01/03/cross-browser-html5-progress-bars-in-depth/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>Clipping JPEG Images Into Non-Rectangular Polygons Using polyClip.js</title>
		<link>http://www.useragentman.com/blog/2011/10/29/clipping-jpeg-images-into-non-rectangular-polygons-using-polyclip-js/</link>
		<comments>http://www.useragentman.com/blog/2011/10/29/clipping-jpeg-images-into-non-rectangular-polygons-using-polyclip-js/#comments</comments>
		<pubDate>Sat, 29 Oct 2011 15:20:29 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[canvas]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Images]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[VML]]></category>
		<category><![CDATA[arbitrary clipping]]></category>
		<category><![CDATA[clipping]]></category>
		<category><![CDATA[non-rectangular clipping. polygon clipping]]></category>
		<category><![CDATA[polyClip]]></category>
		<category><![CDATA[polygon]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=3526</guid>
		<description><![CDATA[<img src="http://www.useragentman.com/blog/wp-content/uploads/2011/10/scissorsTeaser.jpg" /> Up until now, if a developer needed to clip an image in a non-rectangular shape, it was necessary to save the image as a PNG with an alpha channel. If the image is a photograph, the file-size balloons up to unacceptable levels. My new library, polyClip.js, allows developers to clip these images using photograph friendly JPEGs instead. This article guides you step by step on how to use it yourself.]]></description>
			<content:encoded><![CDATA[<div class="box">
<div id="explanation1">This photo is not a PNG image with an alpha channel.</div>
<div id="explanation2"><a href="http://www.useragentman.com/blog/wp-content/uploads/2011/10/scissors.jpg">It is a JPEG</a> that has been clipped with polyClip.js</div>
<div id="explanation3">
<p>The text was rotated using CSS3 Transforms, with alternate CSS for older IE using the <a href="http://www.useragentman.com/IETransformTranslator">IE Transform Translator</a>.  Original photo by <a href="http://www.flickr.com/photos/free-stock/4816852597/">Emilian Robert Vicol</a> clipped with polyClip.js</p>
</p></div>
<div class="clipParent"><img data-polyclip=" 0,0,45,3,55,8,64,18,74,24,80,32,86,45,82,32,83,38,86,46,88,52,90,62,90,68,91,76,91,82,91,83,103,87,112,91,121,97,127,102,133,106,136,112,138,116,139,121,139,122,147,125,154,127,163,129,178,131,190,132,204,132,210,131,225,124,286,107,332,96,370,88,401,83,425,79,450,76,457,77,456,80,446,85,430,91,406,99,383,108,365,114,352,119,384,117,416,116,453,114,472,113,474,113,472,118,446,126,419,132,390,138,362,144,343,148,307,153,282,157,266,159,245,160,234,160,219,160,209,163,198,167,182,175,170,183,158,194,156,195,157,201,157,204,157,204,157,210,153,219,148,229,143,236,137,244,132,250,124,258,118,263,113,267,98,276,87,281,77,283,0,284,1,191,10,190,16,191,20,191,21,192,28,191,37,190,39,189,38,190,40,190,48,190,53,191,57,191,69,188,77,187,91,183,109,179,127,173,146,166,162,158,173,153,158,151,144,150,127,151,105,152,86,154,66,157,53,160,49,161,46,165,44,167,41,168,33,168,28,167,28,166,21,170,16,171,11,171,4,170,0,168" src="http://www.useragentman.com/blog/wp-content/uploads/2011/10/scissors.jpg" alt=""  width="480" height="360" class="alignleft size-full wp-image-3771" /></div>
</div>
<p>There have been many times I have come across the need to <strong>take an image and cut an irregular shape out of it.</strong>  Normally, when a developer comes across this requirement, the only thing to do is to open the image up with your favorite graphics editor, use the select tool to cut out the shape you want, and then save the result as a <a href="http://en.wikipedia.org/wiki/Portable_Network_Graphics">PNG</a>, since it is the only image format used by all web browsers that support alpha channels.  The problem is that <strong>PNG images, while compressed, are not as small as <a href="http://en.wikipedia.org/wiki/Jpeg">JPEGs</a> if the source image is a photograph</strong>, and the download time of a page can balloon to unacceptable levels if there are many of these types of image on a page.  When I first came across this problem, I didn&#8217;t think there was an obvious solution, but after a lot of thought, I created <strong>a library that uses <a href="http://en.wikipedia.org/wiki/HTML_canvas">HTML5 <code>canvas</code></a> to clip a JPEG </strong> (or any other image for that matter).  <strong>The library also supports older versions of IE</strong> (7-8) using the <a href="http://excanvas.sourceforge.net/">Excanvas JavaScript library</a> which polyfills <code>canvas</code> using <a href="http://en.wikipedia.org/wiki/Vector_Markup_Language">VML</a>.</p>
<h2>Okay, How Can I Do The Same Thing?</h2>
<ul>
<li><a href="https://github.com/zoltan-dulac/polyClip">Download my script from github</a> (It includes jQuery 1.6.4 and excanvas release 3.  It should work with later versions &mdash; if it doesn&#8217;t, please let me know and I&#8217;ll fix it ;-) ) .</li>
<li>Include them into the head of your document.<br />
<blockquote class="code">
<pre>
  &lt;script src="/path/to/js/jquery-1.6.2.min.js"&gt;&lt;/script&gt;

  &lt;!--[if lt IE 9 ]&gt;
   &lt;script src="/path/to/js/excanvas/excanvas.compiled.js"&gt;&lt;/script&gt;
  &lt;![endif]--&gt;

  &lt;script src="/path/to/js/polyClip-p.js"&gt;&lt;/script&gt;
</pre>
</blockquote>
</li>
<li>Layout your page as normal, placing <code>img</code> tags where you want the clipped images to go:<br />
<blockquote class="code">
<pre>
&lt;div class="clipParent"&gt;
  &lt;img src="images/image.jpg" /&gt;
&lt;/div&gt;
</pre>
</blockquote>
<p><strong>Note the <code>div</code> tag with the class of <code>clipParent</code></strong>.  You must surround all the image tags with <code>data-polyclip</code> attributes set one.  This is so you can style the image correctly, since the library will remove the image tag and replace it with a <code>canvas</code> element.
</p></blockquote>
</li>
<li>For every image on your page that you want to clip, calculate the points of the shape you want to cut out of the image and place them in the <code>data-polyclip</code> attribute.  For example, let&#8217;s say you have the following shape you want to cut out of a photograph:
<div class="centered ghost">
  <img src="/tests/polyClip/images/crop.jpg"   />
</div>
<div class="clipParent centered inFront">
  <img src="/tests/polyClip/images/crop.jpg" data-polyclip="487, 4, 500, 239, 19, 239, 43, 195" data-stroke="rgb(0, 0, 0)" /></p>
<div class="point" id="pt1" style="left: 487px; top: 4px; "><span>(487, 4)</span></div>
<div class="point" id="pt2" style="left: 500px; top: 239px"><span>(500, 239)</span></div>
<div class="point" id="pt3" style="left: 19px; top: 239px"><span>(19, 239)</span></div>
<div class="point" id="pt4" style="left: 43px; top: 195px; "><span>(43, 195)</span></div>
</div>
<p>You would then just set the <code>data-polyclip</code> attribute to those points:</p>
<blockquote class="code">
<pre>
&lt;img src="image.jpg" <span class="hilite">data-polyclip="487, 4, 500, 239, 19, 239, 43, 195"</span> /&gt;
</pre>
</blockquote>
<p>Note that the point co-ordinates are comma delimited.  If you don&#8217;t want to calculate these numbers by hand, you can use your favorite imagemap generation tool to do generate the list of coordinates.  Just remember to take the coordinates out of the imagemap code and stick it in the <code>data-polyclip</code> attribute (for the examples on this page, I used the on-line tool available at <a href="http://www.image-maps.com">image-maps.com</a>).</li>
</ul>
<h2>Why Should I Do This Instead of Using a PNG?</h2>
<p>Compare the image above with a PNG clipped with the GIMP.   Even when I compressed the PNG with <a href="http://www.useragentman.com/blog/2009/09/16/optimizing-png-files-for-both-web-and-print/">my PNG optimization script</a>, the 191K PNG file is huge compared to it&#8217;s tiny 18K polyclipped JPEG analogue (<strong>Note:</strong> file sizes refer to the size of the full image as displayed at the top of this article):</p>
<table class="dataTable">
<thead>
<tr>
<th>JPG using polyClip.js (18K)</th>
<th>PNG (191K)</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="clipParent"><img data-polyclip="0,0,55,0,64,4,70,12,80,22,82,32,86,45,82,32,83,38,86,46,88,52,90,62,90,68,91,76,91,82,91,83,103,87,112,91,121,97,127,102,133,106,136,112,138,116,139,121,139,122,147,125,154,127,163,129,178,131,190,132,204,132,210,131,225,124,286,107,332,96,370,88,401,83,425,79,450,76,457,77,456,80,446,85,430,91,406,99,383,108,365,114,352,119,384,117,416,116,453,114,472,113,474,113,472,118,446,126,419,132,390,138,362,144,343,148,307,153,282,157,266,159,245,160,234,160,219,160,209,163,198,167,182,175,170,183,158,194,156,195,157,201,157,204,157,204,157,210,153,219,148,229,143,236,137,244,132,250,124,258,118,263,113,267,98,276,87,281,77,283,0,284,1,191,10,190,16,191,20,191,21,192,28,191,37,190,39,189,38,190,40,190,48,190,53,191,57,191,69,188,77,187,91,183,109,179,127,173,146,166,162,158,173,153,158,151,144,150,127,151,105,152,86,154,66,157,53,160,49,161,46,165,44,167,41,168,33,168,28,167,28,166,21,170,16,171,11,171,4,170,0,168" src="http://www.useragentman.com/blog/wp-content/uploads/2011/10/scissors.jpg" alt=""  width="480" height="360" class="alignleft size-full wp-image-3771" /></div>
</td>
<td>
<div class="pngImage">
  <img src="http://www.useragentman.com/blog/wp-content/uploads/2011/10/scissors.png" />
  </div>
</td>
</tr>
</tbody>
</table>
<p>You may ask whether the amount of JavaScript used in this solution is greater than the bandwidth saved by using JPEG compression.  <strong>Not including jQuery, the amount of JavaScript clocks in at 2K or 13K</strong>, depending on the browser used (2K for the compressed polyClip script and 11K for excanvas, which will only be loaded by IE7-8). Although jQuery adds 91K to this equation, it doesn&#8217;t matter that much to me personally since I am probably using it for other parts of my page anyways.  <strong>Even with jQuery, the amount of JavaScript downloaded outweighs using a PNG,</strong> especially if your are clipping a larger image or a huge amount of smaller images.</p>
<p><a class="exampleLink" href="https://github.com/zoltan-dulac/polyClip">Download the latest version of polyClip.js from github.</a></p>
<h2>A Few Caveats</h2>
<ul>
<li>In IE7 and 8, the image may appear briefly as a black outline before the image appears. Other than that, it looks about the same as the other browsers.</li>
<li>The image does not show up correctly in Opera Mini.  Then again, a lot of things (e.g. CSS3 text-shadows, CSS3 Transforms, etc.) don&#8217;t show up correctly in Opera Mini. :-)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2011/10/29/clipping-jpeg-images-into-non-rectangular-polygons-using-polyclip-js/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Fixing oninput in IE Using html5Widgets</title>
		<link>http://www.useragentman.com/blog/2011/05/12/fixing-oninput-in-ie9-using-html5widgets/</link>
		<comments>http://www.useragentman.com/blog/2011/05/12/fixing-oninput-in-ie9-using-html5widgets/#comments</comments>
		<pubDate>Thu, 12 May 2011 18:43:43 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[Forms]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Polyfills]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[html5Widgets]]></category>
		<category><![CDATA[IE9]]></category>
		<category><![CDATA[Internet Explorer 9]]></category>
		<category><![CDATA[javascript events]]></category>
		<category><![CDATA[onforminput]]></category>
		<category><![CDATA[oninput]]></category>
		<category><![CDATA[oninput backspace]]></category>
		<category><![CDATA[oninput cut]]></category>
		<category><![CDATA[oninput delete]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=3174</guid>
		<description><![CDATA[<img src="http://www.useragentman.com/blog/wp-content/uploads/2011/05/oninput.png" alt="" title="oninput" width="250" height="140" class="alignleft size-full wp-image-3197" /> Even though <code>onforminput</code> is deprecated, <code>oninput</code> will allow developers to do dynamic calculations on forms with little JavaScript knowledge.  Unfortunately, it doesn't work in IE8 and lower, and there are some small bugs in IE9's implementation when trying to delete characters inside a form field. So I implemented a fix for these issues with an update to my cross-browser HTML5 Forms polyfill, html5Widgets.]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/05/oninput.png" alt="" title="oninput" width="250" height="140" class="alignleft size-full wp-image-3197" /> In my <a href="/blog/2011/05/10/is-onforminput-deprecated-in-html5-forms-and-why-should-i-care-anyways/#comment-12167">previous blog post</a>, I wished that the <code>onforminput</code> event was not deprecated in the HTML5 specification.  I have used this attribute in the past to show values in range elements and show calculations of values inside a form, and thought it was perfect for those who know little JavaScript to implement features like this very easily. <a href="http://www.boogdesign.com/b2evo/">Rob Crowther</a> made <a href="/blog/2011/05/10/is-onforminput-deprecated-in-html5-forms-and-why-should-i-care-anyways/#comment-12167">pointed out</a> that the <code>oninput</code> event, which has a similar function as <code>onforminput</code> the main argument behind dropping <code>onforminput</code> event.  Makes sense, and I was happy &mdash; it works in all modern web browsers, including IE.  But as you would guess, there has to be at least one problem with a cool bit of new web technology when it comes to cross-browser implementation.</p>
<h2>What are the issues?</h2>
<p>IE has three issues related to <code>oninput</code>:</p>
<ol>
<li><code>oninput</code> is not supported at all in IE8 and lower.</li>
<li>IE9 does not allow referring to form fields directly in the <code>oninput</code> expression.  For example, this code works in all browsers except IE9:<br />
<blockquote class="code">
<pre>
&lt;form name="f1" <span class="hilite">oninput="output.value = parseFloat(darkness.value);"</span>&gt;
    Darkness Level:
    &lt;input type="range" name="darkness" value="0" min="0" max="255"&gt;
    &lt;output name="output"&gt;
&lt;/form&gt;
</pre>
</blockquote>
<p>This is not so bad, since one could refactor the event code like this:</p>
<blockquote class="code">
<pre>
&lt;form name="f1"
  <span class="hilite">oninput="document.getElementById('output').innerHTML = parseFloat(darkness.value);"</span>&gt;
    Darkness Level:
    &lt;input type="range" name="darkness" value="0" min="0" max="255"&gt;
    &lt;output id="output"&gt;
&lt;/form&gt;
</pre>
</blockquote>
</li>
<li>If you use the backspace key while entering in a value, IE9 doesn&#8217;t fire the <code>oninput</code> event.  This, in my opinion, is really bad, because it does affect how the form behaves.  For example, take the following form in IE9:<br />
<form oninput="document.getElementById('tax').innerHTML = (parseFloat(amount.value) * parseFloat(price.value) * 0.15).toFixed(2); document.getElementById('total').innerHTML = (parseFloat(amount.value) * parseFloat(price.value) * 1.15).toFixed(2)">
<table class="formTable">
<tbody>
<tr>
<th>
Amount:
</th>
<td>
<input name="amount" value="1" min="1" max="100" type="number">
</td>
</tr>
<tr>
<th>
Item:
</th>
<td>
<input name="price" value="299.99" min="1" step="0.01" type="number">
</td>
</tr>
<tr>
<th>Tax:</th>
<td><output id="tax">45.00</output></td>
</tr>
<tr>
<th>Total:</th>
<td><output id="total">344.99</output>
</td>
</tr>
</tbody>
</table>
</form>
<p>As the user types in values for the amount and price into the form, the <code>oninput</code> event calculates the tax and total.  <strong>However, if the user is entering a value into, say, the price and make a mistake and press the backspace, IE9 will not update the tax and total.</strong>  Your application will give erroneous information, the user will be surprised at the total being larger than he expected, says &#8220;screw this&#8221; and leaves the application.  Your company loses the big sale, your boss fires you, and <strong>next thing you know you are on a street, or worse, coding <a href="http://drdobbs.com/architecture-and-design/210602491">COBOL</a> on a 30 year old IBM PC running MS-DOS 1.0 in some fishing tackle store.</strong>  Not pretty.</li>
</ol>
<h2>Oh The Humanity! How Can I Prevent Such a Fate?</h2>
<p>Since I hate COBOL, I decided to update my html5Widgets library to:</p>
<ol>
<li>Add support for <code>oninput</code> for browsers that don&#8217;t support it (e.g. IE7 and 8)</li>
<li>Force IE9 to fire a form&#8217;s <code>oninput</code> when the backspace and delete keys are pressed inside any of the <code>input</code> nodes.</li>
<li>Force IE9 to fire a form&#8217;s <code>oninput</code> when the <code>cut</code> event is fired on any of the <code>input</code> nodes.</li>
</ol>
<p>Using the link below, you can see an example similar to the one above using html5Widgets to fix these issues.</p>
<p><a class="exampleLink" href="/examples/html5Widgets/tests/html5Widgets/outputWithOninput.html">Let&#8217;s see html5Widgets give IE some <code>oninput</code> goodness</a></p>
<p>In order to do this, I used ideas from <a href="http://blog.danielfriesen.name">Daniel Friesen&#8217;s</a> blog post, <em><a href="http://blog.danielfriesen.name/2010/02/16/html5-browser-maze-oninput-support/">A HTML5 Browser Maze, Oninput Support</a></em> (thanks to <a href="www.paulirish.com">Paul Irish</a> for pointing me into that direction).  The result works rather well.  Note that I did not fix the IE9 &#8220;cannot use <code>.value</code>, must use <code>.innerHTML</code>&#8221; bug described earlier. It&#8217;s a little bit more verbose, but for now it&#8217;s what a developer needs to do for true cross-browser <code>oninput</code> support. I have also kept <code>onforminput</code> support in the code, just in case anyone has used it in the past (e.g. me).  To ensure the best experience, I wouldn&#8217;t use both events on the same page.  </p>
<p>Note also I did not add support for <code>oninput</code> in any element other than <code>form</code>.  I may do this after further research in the way it supported across browsers (for example, <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=195696">Firefox has an issue with <code>oninput</code> being used on <code>textarea</code> elements</a>).</p>
<h2>Download</h2>
<p><a class="exampleLink" href="https://github.com/zoltan-dulac/html5Widgets">Download the latest version of html5Widgets, which includes <code>oninput</code> support, from github.</a></p>
<h2>Further Reading</h2>
<ul>
<li><a href="http://blog.danielfriesen.name">Daniel Friesen&#8217;s</a> blog post, <em><a href="http://blog.danielfriesen.name/2010/02/16/html5-browser-maze-oninput-support/">A HTML5 Browser Maze, Oninput Support</a></em>.</li>
<li><a href="http://help.dottoro.com/ljhxklln.php">oninput event | input event</a> from the <a href="http://help.dottoro.com/">Dottoro Web Reference</a></li>
<li><a href="http://mathiasbynens.be/notes/oninput">Using the oninput event with onkeydown as its fallback</a> by the ever talented <a href="http://mathiasbynens.be/">Mathias Bynens</a>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2011/05/12/fixing-oninput-in-ie9-using-html5widgets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is onforminput Deprecated in HTML5 Forms? (And Why Should I Care Anyways?)</title>
		<link>http://www.useragentman.com/blog/2011/05/10/is-onforminput-deprecated-in-html5-forms-and-why-should-i-care-anyways/</link>
		<comments>http://www.useragentman.com/blog/2011/05/10/is-onforminput-deprecated-in-html5-forms-and-why-should-i-care-anyways/#comments</comments>
		<pubDate>Tue, 10 May 2011 19:16:01 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[Events]]></category>
		<category><![CDATA[Forms]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Polyfills]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=3065</guid>
		<description><![CDATA[<img src="http://www.useragentman.com/blog/wp-content/uploads/2011/05/deprecated.png" alt="" title="deprecated" width="250" height="140" class="alignleft size-full wp-image-3112" /> The <code>onforminput</code> event has been stripped out of HTML5, and it makes me sad.  This article will show examples of how it works, how you can use it today in browsers that don't support it, and hopefully will also present a good case as to why it should be put back into the HTML5 specification.  Should something this useful and easy-to-use be abandoned?]]></description>
			<content:encoded><![CDATA[<div class="importantNotes">
<h3>Update:</h3>
<p><strong>Newer versions of all browsers support the <code>form</code> element&#8217;s <code>oninput</code> attribute, which replaces the functionality of <code>onforminput</code>, and as a result I wrote a related article <a href="http://www.useragentman.com/blog/2011/05/12/fixing-oninput-in-ie9-using-html5widgets/">Fixing <code>oninput</code> in IE Using html5Widgets</a>.</strong>  Thanks to <a href="http://www.boogdesign.com/b2evo/">Rob Crowther</a> for setting me straight and providing <a href="http://jsfiddle.net/robertc/YCEKQ/">a great example</a>, which <a href="http://jsfiddle.net/dYSS6/">I have forked</a> to work for IE9.  I will refactor html5Widgets to support <code>oninput</code> in IE very soon.  I will leave this article as is for the benefit of anyone who is Googling for <code>onforminput</code> and why it was deprecated.
</p>
</div>
<p><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/05/deprecated.png" alt="" title="deprecated" width="250" height="140" class="alignleft size-full wp-image-3112" /></p>
<p>I was just doing some regression testing on <a href="http://www.useragentman.com/blog/2010/07/27/cross-browser-html5-forms-using-modernizr-webforms2-and-html5widgets/">html5Widgets</a>, and noticed that the <code>onforminput</code> event wasn&#8217;t working in Firefox 4.  This didn&#8217;t make sense to me: I designed html5Widgets to detect if the <code>output</code> tag is supported, and if not, add support for it.  I believed that if a browser supported the <code>output</code> tag, it also had to support the <code>onforminput</code> event, since I assumed that this was one of the main reasons of having the <code>output</code> tag in the first place.  However, <strong>Firefox 4 supports the <code>output</code> tag, but doesn&#8217;t support <code>onforminput</code>.</strong>  After doing a bit of light research, I came across this <a href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=11129">this bug report</a> that seems to indicate that <code>onforminput</code> is deprecated.  This article will discuss<strong> what <code>onforminput</code> does</strong>, why it is very <strong>useful to designers are not so strong in JavaScript</strong>, how one can <strong>add support for <code>onforminput</code> to browsers that don&#8217;t</strong>, and why I really <strong>hope someone in power adds it back to the HTML5 Forms specification,</strong> or at least something very similar.</p>
<h2>Why Do I Care</h2>
<p>For those that don&#8217;t know what this is, <strong><code>onforminput</code> is an attribute</strong> that a web author can use on the <code>output</code> tag <strong>to do some calculations with the values in a form&#8217;s input fields</strong> and show that calculation to a user.  Right now, Opera is the only browser that natively supports it, but <a href="http://www.useragentman.com/blog/2010/07/27/cross-browser-html5-forms-using-modernizr-webforms2-and-html5widgets/">in an earlier post</a> I showed how one could use it with my html5Widgets polyfill library in all the other browsers.  How does it work? To illustrate a very simple use case, let&#8217;s say I have a <code>range</code> input field:</p>

<table class="dataTable">
<thead>
<tr>
<th>HTML</th>
<th>Output</th>
</tr>
</thead>
<tbody>
<tr>
<td class="withCode">
<blockquote class="code">
<pre>
&lt;form&gt;
  Darkness Level:
  &lt;input type="range" name="darkness"
         value="0" min="0" max="255" /&gt;
&lt;/form&gt;
</pre>
</blockquote>
</td>
<td>
<form>
Darkness Level: 
<input type="range" 
       name="darkness"
       value="0"
       min="0"
       max="255" />
</form>
</td>
</tr>
</tbody>
</table>
<p>(Note the <code>range</code> element is <a href="http://remysharp.com/2010/10/08/what-is-a-polyfill/">polyfilled</a> with html5Widgets if your browser doesn&#8217;t support it).</p>
<p>The <code>range</code> input type is a slider widget, and we use it here so the user can choose a number between 0 and 255.  The slider looks cool, but from a usability standpoint, there is no feedback of what value the user is sliding to.  It would make more sense to show a number next to the slider. which gives feedback to the user as to what the slider&#8217;s value is.  This is where <code>onforminput</code> comes in:</p>

<table class="dataTable">
<thead>
<tr>
<th>HTML</th>
<th>Output</th>
</tr>
</thead>
<tbody>
<tr>
<td class="withCode">
<blockquote class="code">
<pre>
&lt;form&gt;
  Darkness Level:
  &lt;input type="range"
     name="darkness"
     value="0" min="0" max="255" /&gt;
  <span class="hilite">&lt;output 
     onforminput="this.value = darkness.value"
  &gt;0&lt;/output&gt; / 255</span>
&lt;/form&gt;

</pre>
</blockquote>
</td>
<td>
<form>
  Darkness Level:
  <input type="range" name="darkness"
         value="0" min="0" max="255" />
  <output 
         onforminput="this.value = darkness.value"
  >0</output> / 255
</form>
</td>
</tr>
</tbody>
</table>
<p><strong>Immediate user feedback!  Great user experience! <em>And simple syntax!</em></strong>  Even a person who is not familiar with JavaScript can figure it out.  But it&#8217;s deprecated!  So, even though the <code>output</code> tag is supported by Firefox 4, <code>onforminput</code> is not.  Oh-noes!!!!</p>
<p>Luckily, my html5Widgets library adds support for it in browsers that don&#8217;t, including Firefox 4.  I just made a small change that worked around an issue preventing Firefox from supporting it, and BAM!  Instant support.</p>
<p>Another good (and common) example for using <code>output</code> would be a form for an online tool that calculates a total automatically.  Enter in values into the following form and see the calculations magically appear:</p>

<form>
<table class="formTable">
<tbody>
<tr>
<th>
Amount:
</th>
<td>
<input type="number" name="amount" value="1" min="1" max="100" />
</td>
</tr>
<tr>
<th>
Item:
</th>
<td>
<input type="number" name="price" value="299.99" min="1" step="0.01" />
</td>
</tr>
<tr>
<th>Tax:</th>
<td><output onforminput="this.value = (parseFloat(amount.value) * parseFloat(price.value) * 0.15).toFixed(2)"
>0</output></td>
</tr>
<tr>
<th>Total:</th>
<td><output onforminput="this.value = (parseFloat(amount.value) * parseFloat(price.value) * 1.15).toFixed(2)"
>0</output>
</tr>
</tbody>
</table>
</form>
<h2>But, why is it depreciated in the first place?</h2>
<p>It seems the decision has been made to remove <code>onforminput</code> from the HTML5 specification since it&#8217;s purpose can be easily done with capturing events (at least, that&#8217;s what I understood from <a href="http://lists.w3.org/Archives/Public/public-webapps/2010OctDec/0177.html">this thread about <code>onforminput</code> and <code>onformchange</code> on the public-webapps mailing list</a>).  </p>
<p>While this is true, I think keeping it in would be a huge help to designers and developers.  I even think that having it declared inline like I do in the code examples above is useful.  Even though it breaks the separation of document structure and behavior, it allows a developer with practically no JavaScript knowledge to implement this simple feature in a form.  According to the <a href="http://www.w3.org/Bugs/Public/show_bug.cgi?id=11129">Bug Report</a>, designers shouldn&#8217;t have to use <code>onforminput</code> since they can do this instead: </p>
<blockquote class="code">
<pre>
&lt;form name="f1"&gt;
Darkness Level:
<div class="hilite">    &lt;script type="text/javascript"&gt;
      document.addEventListener("input",
        function(e) {
          if (document.forms.f1 == e.target.form)
            e.target.form.output.value =
              parseFloat(e.target.form.darkness.value);
        }, true);
    &lt;/script&gt;</div>

    &lt;input type="range" name="darkness"
         value="0" min="0" max="255" /&gt;
    &lt;output name="output"&gt;
  &lt;/form&gt;
</pre>
</blockquote>
<p><strong>This is uglier than your Mom&#8217;s mood when you don&#8217;t call her on Mother&#8217;s Day</strong> (not that I&#8217;m speaking from any recent experience). Even though it is dead easy for a JavaScript programmer to implement this without <code>onforminput</code>, some web designers wouldn&#8217;t have the knowledge or the patience to do so.  This is not a slight to those designers &mdash; after all, they can probably make better looking web pages than the average programmer. So I am keeping <code>onforminput</code> in html5Widgets.  It is really convenient and easy to understand.</p>
<h2>A Note To Those In Power</h2>
<p><strike>To anyone at the W3C who has the power to do so, I am politely asking to reconsider and <strong>please put <code>onforminput</code> back into the specification</strong> or <strong>at least replace it with something just as simple</strong> (the latter being attractive to those who want to continue separating the structure of the document (i.e. HTML) from the behavior (i.e. JavaScript)).</strike> As mention in the update on the top of the page, <code>oninput</code> is a great replacement for <code>onforminput</code> and it works in all modern browsers.  </p>
<p>In my humble opinion, creating calculations the old-fashioned way with events and lines and lines of JavaScript is painful for designers who aren&#8217;t scripters.  After all, isn&#8217;t this the reason why <code>required</code> and <code>pattern</code> were created?</p>
<p><a class="exampleLink" href="https://github.com/zoltan-dulac/html5Widgets">Download the latest version of html5Widgets, which includes <code>onforminput</code> support, from github</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2011/05/10/is-onforminput-deprecated-in-html5-forms-and-why-should-i-care-anyways/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>The CSS3 matrix() Transform for the Mathematically Challenged</title>
		<link>http://www.useragentman.com/blog/2011/01/07/css3-matrix-transform-for-the-mathematically-challenged/</link>
		<comments>http://www.useragentman.com/blog/2011/01/07/css3-matrix-transform-for-the-mathematically-challenged/#comments</comments>
		<pubDate>Fri, 07 Jan 2011 05:49:57 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[Drag and Drop]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[transform]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=2159</guid>
		<description><![CDATA[<img src="/blog/wp-content/uploads/2011/01/xx2.png" /> The CSS3 <code>transform</code> property can do some really cool things - with it, web designers can rotate, scale, skew and flip objects quite easily.  However, in order for designers to have <strong>fine-grained, pixel level control over their transforms</strong>, the <strong><code>matrix()</code> function</strong> is hard to beat.  This post explains what it does and the math behind the code.  It also includes a tool to help you create <code>matrix()</code> transforms easily using <a href="/blog/2010/01/10/cross-browser-html5-drag-and-drop/">HTML5 Drag and Drop</a> for the user interface and the <a href="http://sylvester.jcoglan.com/">Sylvester JavaScript library</a> to do the mathematics needed to calculate the matrix values so you don't have to (in case you have difficulties with math, or if you just are lazy and don't want to be bothered). 
<br /><br />]]></description>
			<content:encoded><![CDATA[


<div class="importantNotes">
<h3>Notes:</h3>
<ul>
<li><strong>This article was not rendering correctly in Firefox 4.x due to a change in how that browser handles MathML.</strong>  This issue has now been fixed as of May 1, 2011.</li>
<li>If you are not familar with 2D-Transforms, take a look at my other article <cite><a href="http://www.useragentman.com/blog/2010/03/09/cross-browser-css-transforms-even-in-ie/">Cross Browser CSS Transforms – even in IE</a></cite></li>
</ul>
</div>


<div  class="wp-caption alignright" style="width: 419px;"><a href="/matrix"><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/01/matrixConstructionSetScreenshot.png" alt="Screenshot of the Matrix Construction Set" title="matrixConstructionSetScreenshot"  class="size-full wp-image-2055" /></a>
<p class="wp-caption-text"><a href="/matrix">The Matrix Construction Set</a> allows you to make a pixel perfect transformation by just dragging and dropping objects around.</p>
</div>


<p>The CSS3 <code>transform</code> property can do some really cool things - with it, web designers can rotate, scale, skew and flip objects quite easily.  However, in order for deisgners to have <strong>fine-grained, pixel level control over their transforms</strong>, it would be really helpful to understand how the <strong><code>matrix()</code> function</strong> works.  With the <code>matrix()</code> function, designers can position and shape their transformations exactly where they want to.</p>

<p>The problem is, <strong>not many people actually understand what the numbers in the matrix filter actually mean</strong>.  To solve this issue, I have created a tool, which I call the <a href="/matrix/">CSS3 Matrix Construction Set</a> that can take any block-element, positioned anywhere on the page, transform it anywhere else on the page using the <code>matrix()</code> filter, and reveal the correct CSS code for this operation.  <a href="/matrix/">Look at the tool now</a> and if that is all you are interested in, you can stop there.  However, <strong>if you what to know what the numbers in the <code>matrix()</code> function actually mean</strong>, and why you want such knowledge in the first place, read on.  I'll try to explain this as simply as possible (and please feel free to use the feedback form if you have any opinions on how to make this article better).</p>

<a class="exampleLink" href="/matrix/">Take a Look at the CSS3 Matrix Construction Set</a>

<div class="importantNotes">
<p><strong>Note:</strong> This tool works in all modern versions of Firefox, Sarari and Chrome.  Although it is not usable in Opera (due to its lack of support for <a href="http://www.useragentman.com/blog/2010/01/10/cross-browser-html5-drag-and-drop/">HTML5 drag and drop</a>) the CSS code it produces in other browsers <strong>is</strong> usable in that browser.  The tool also does not work in IE since neither that browser, nor my CSS3 polyfill library, <a href="http://www.useragentman.com/blog/csssandpaper-a-css3-javascript-library/">cssSandpaper</a>, support the <code>transform-origin</code> property.  I hope to fix cssSandpaper to implement this in a future release, but in the meantime, please read the note at the end of this article for a workaround for this issue.</p>
</div>

<h2>Matrices: Why Should I Care?</h2>

<p>Matrices are to transforms like RGB hex codes are to colors: they are representations that are easy for computers to understand, but not human beings.  Sure, web designers can use the <code>rotate()</code>, <code>skew()</code>, <code>scale()</code> and <code>translate()</code> functions to fulfill their transformation needs ... why should we bother with <code>matrix()</code> at all?  There are a few cases when you may want to:</p>

<dl>
     <dt>Brevity:</dt>
     <dd>Using matrices, it is possible to represent a complex string of 2D transforms like this:

<blockquote class="code">
<pre>
#object {
    transform-origin: 0 0;
    transform: rotate(15deg) translateX(230px)  scale(1.5, 2.6) skew(220deg, -150deg) translateX(230px)
}
</pre>
</blockquote>

using one <code>matrix()</code> rule like this:

<blockquote class="code">
<pre>
#object {
    transform-origin: 0 0;
    transform: matrix(1.06, 1.84, 0.54, 2.8, 466px, 482px)
}
</pre>
</blockquote>

<p>(<strong>Note:</strong> I have ignored the vendor-specific variants (e.g. <code>moz-transform</code>, etc.) for the sake of brevity).</p>

</dd>
     <dt>Pixel-Perfection:</dt>
     <dd>If you know exactly how you want your transformation to look, it'll take a bit of fiddling around to get it to look how you want using the other transform functions, and it may not be pixel perfect if you lack patience (like I do).  However, using a tool like the Matrix Construction Set, you can place the transform exactly where you want (This is analogous to using a color wheel to choose RGB colors for you, instead of using keywords like <code>red</code>, <code>green</code> or <code><a href="http://www.colordic.org/colorsample/1019.html">peachpuff</a></code>).</dd>

     <dt>JavaScript:</dt>
     <dd>Revealing an object's transform information using JavaScript's <code>getComputedStyle()</code> function will yield a <code>matrix()</code> function in all current web browsers that support CSS3 transforms, <strong>even if it was rendered using other transform functions like <code>rotate()</code></strong>.  For example, the object below has been rotated 45&deg; using <code>transform: rotate(45deg)</code>:


<div class="exampleBackground">
<div id="exampleRotation">
This object has been rotated 45&deg;
</div>
</div>

But <a href="#" onclick="
  if (!window.getComputedStyle) {
     alert('This browser does not support getComputedStyle()');
     return false;
  }
  var props = ['MozTransform', 'WebkitTransform', 'OTransform', 'MSTransform', 'transform'];
  var compStyle = window.getComputedStyle(document.getElementById('exampleRotation'), null);
  for (var i=0; i<props.length; i++) {

    var style = compStyle[props[i]];
    if (style != null) {
       alert('Calling getComputedStyle() on the rotated element gives: ' + style);
       return false;
    }
  }
  alert('This browser doesn\'t support CSS3 Transforms');
    return false;">this link, which shows the object's computed style</a> will reveal that internally it stores a <code>matrix()</code> function.</dd>
</dl>


<h2>Ok, What Does The Markup Look Like?</h2>

<p>The <code>matrix()</code> function takes 6 parameters in order for it to work:</p>
 
<blockquote class="code">
<pre>
#transformedObject {
     -moz-transform:    matrix(1.4488, -0.3882, 0.3882, 1.4489, 400px, -100px);
     -webkit-transform: matrix(1.4488, -0.3882, 0.3882, 1.4489, 400, -100);
     -o-transform:      matrix(1.4488, -0.3882, 0.3882, 1.4489, 400, -100);
     transform:         matrix(1.4488, -0.3882, 0.3882, 1.4489, 400, -100);
}
</pre>
</blockquote>

<p><strong>Note the difference with the Firefox implementation of <code>matrix()</code></strong> &mdash; the last two elements need the <code>px</code> units after it.  For now, think of it is a difference in notation, but we'll explain why this is later.</p>


<h2>But What Do The Numbers Mean?</h2>
 
<p>In order to explain what they mean, I will have to define a few math-concepts here.  <strong>Don't panic if you are mathematically challenged</strong>.  These concepts are not that hard to understand, but there is a bit of explanation needed.  Why should you torture yourself if you hate math?</p>

<dl>
<dt>If you are a designer:</dt>
<dd>Think of the stuff below as informational.  If you use the Matrix Construction Set <strong>you won't need to calculate anything by hand</strong> (but wouldn't you want to have an idea what the numbers mean anyway?)</dd>
<dt>If you are a JavaScript developer:</dt>
<dd><em>This information will be invaluable when optimizing scripts that rely on transformation effects.</em>  Just try to understand the basic concepts here, and if you still need help, use the <a href="http://sylvester.jcoglan.com/">Sylvester JavaScript library</a> to do the heavy lifting for you.</dd>
<dt>If you want to be an &uuml;ber-geek:</dt>
<dd>All those matrix jokes you hear at parties will suddenly start to make sense!</dd>
</dl>

<div class="wp-caption aligncenter" >
<a href="http://xkcd.com/184/"><img src="http://www.useragentman.com/blog/wp-content/uploads/2011/01/matrix_transform.png" alt="In fact, draw all your rotational matrices sideways. Your professors will love it! And then they&#039;ll go home and shrink." title="In fact, draw all your rotational matrices sideways. Your professors will love it! And then they&#039;ll go home and shrink." width="400" height="152" class="aligncenter size-full wp-image-2186" /></a>
<p class="wp-caption-text">Finally you'll be able to understand why all the geeks at work find <a href="http://xkcd.com/184/">this XKCD comic</a> so funny.</p>
</div>


<h2>Terminology</h2>

<h3>Matrix</h3>

<p>The easiest way to think of a matrix is as a group of numbers written in a rectangle or square.  For our purposes, we will be dealing with 3x3 matrices, such as this one below:</p>

<div class="equation">
	<math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mn>3</mn> </mtd>
		      <mtd> <mn>5</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>2</mn> </mtd>
		      <mtd> <mn>4</mn> </mtd>
		      <mtd> <mn>6</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
	</math>
</div>

<p>That's nine numbers!  So how come the CSS3 <code>matrix()</code> function only has six?</p>

<blockquote class="code">
<pre>
#transformedObject {
     transform:  matrix(1, 2, 3, 4, 5, 6);
}
</pre>
</blockquote>

For CSS3 2D transforms, we only deal with 3x3 matrices that have the <strong>two bottom-left numbers that are equal to zero, and the bottom-right value equal to 1</strong>. As a result, these two notations are equal:

<div class="equation">
<code>matrix(1, 2, 3, 4, 5, 6) = </code>
	<math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mn>3</mn> </mtd>
		      <mtd> <mn>5</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>2</mn> </mtd>
		      <mtd> <mn>4</mn> </mtd>
		      <mtd> <mn>6</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
	</math>
</div>


<h3>Dot Product</h3>

<p>Next we are going to explain what a <strong>dot product</strong> is.  At first, this may not seem like this is related to matrices at all, but I promise it does ... just read on and you'll understand why in no time.  Trust me. :-)</p>

<p>Let's say you have two (x, y) points,


	<math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mn>2</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
	  </mrow>
	</math>
	and
	<math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>4</mn> </mtd>
		      <mtd> <mn>5</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
	  </mrow>
	</math>.  The dot product of these two points (written <math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mn>2</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
	  </mrow>
	</math>
	&middot;
	<math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>4</mn> </mtd>
		      <mtd> <mn>5</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
	  </mrow>
	</math>) is what you get when you multiply the two x-coordinates, multiply the two y-coordinates, and then add them together:</p>

<div class="equation">
    <math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn >1</mn> </mtd>
		      <mtd> <mn>2</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
	          <mo>&middot;</mo>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>4</mn> </mtd>
		      <mtd> <mn>5</mn> </mtd>
		    </mtr>
		  </mtable>
                  <mo> ) </mo>
          </mrow> </math>
	         = 1x4 + 2x5 = 4 + 10 = 14
</div>

This doesn't only work for 2-dimensional coordinates, but also in 3-dimensions and higher:

<div class="equation">
    <math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mn>2</mn> </mtd>
                      <mtd> <mn>3</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
	  </mrow>
	</math>
	&middot;
	<math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>4</mn> </mtd>
		      <mtd> <mn>5</mn> </mtd>
                      <mtd> <mn>6</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
	  </mrow>
	</math> = 1x4 + 2x5  + 3x6 = 4 + 10 + 18  = 32
<br /><br />
    <math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mn>2</mn> </mtd>
                      <mtd> <mn>3</mn> </mtd>
                      <mtd> <mn>10</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
	  </mrow>
	</math>
	&middot;
	<math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>4</mn> </mtd>
		      <mtd> <mn>5</mn> </mtd>
                      <mtd> <mn>6</mn> </mtd>
                      <mtd> <mn>20</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
	  </mrow>
	</math> = 1x4 + 2x5  + 3x6 + 10x20 = 4 + 10 + 18 + 200 = 232
</div>

<p>Got it?  It's pretty simple, right?  Note that when we write (x, y) co-ordinates like <math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>x</mn> </mtd>
		      <mtd> <mn>y</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
	  </mrow>
	</math>, we call <math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>x</mn> </mtd>
		      <mtd> <mn>y</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
	  </mrow>
	</math> a <strong>vector</strong>.  Vector notation can be written horizontally, (e.g. <math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>x</mn> </mtd>
		      <mtd> <mn>y</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
	  </mrow>
	</math>) or vertically (e.g. <math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>x</mn> </mtd>
		      
		    </mtr>
                    <mtr>
                       <mtd> <mn>y</mn> </mtd>
                    </mtr>
		  </mtable>
		  <mo> ) </mo>
	  </mrow>
	</math>).  <strong>When using them in 2D transforms, we always add an extra co-ordinate with a number 1 at the end.</strong>  So (20, 90) would be written 
<math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>20</mn> </mtd>
		      <mtd> <mn>90</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
	  </mrow>
	</math> or like <math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>20</mn> </mtd>
		      
		    </mtr>
                    <mtr>
                       <mtd> <mn>90</mn> </mtd>
                    </mtr>
                    <mtr>
                       <mtd> <mn>1</mn> </mtd>
                    </mtr>
		  </mtable>
		  <mo> ) </mo>
	  </mrow>
	</math> in vector notation.

<h3>Multiplicatying a Matrix with a Vector</h3>

<p>So, what does the dot product have to do with matrices?  Well the idea of the dot product can also be extended to matrices.  Let's say you need to multiply the following together: </p>

<div class="equation">
	<math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr color="#ff0000" fontweight="bold">
		      <mtd mathbackground="#ffcccc"> <mn>1</mn> </mtd>
		      <mtd mathbackground="#ffcccc"> <mn>3</mn> </mtd>
		      <mtd mathbackground="#ffcccc"> <mn>4</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>2</mn> </mtd>
		      <mtd> <mn>4</mn> </mtd>
		      <mtd> <mn>5</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
	</math>
	&middot;
	<math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>20</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>30</mn> </mtd>  
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> 
</div>

To do this, you need to produce the dot product of each of the matrix's rows with the vector like this:

<div class="equation">
	<math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr color="#ff0000" fontweight="bold">
		      <mtd mathbackground="#ffcccc"> <mn>1</mn> </mtd>
		      <mtd mathbackground="#ffcccc"> <mn>3</mn> </mtd>
		      <mtd mathbackground="#ffcccc"> <mn>4</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>2</mn> </mtd>
		      <mtd> <mn>4</mn> </mtd>
		      <mtd> <mn>5</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
	</math>
	&middot;
	<math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>20</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>30</mn> </mtd>  
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> =  <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>1</mn><mo>&#215;</mo><mn>20</mn> <mo>+</mo> <mn>3</mn><mo>&#215;</mo><mn>30</mn> <mo>+</mo> <mn>4</mn><mo>&#215;</mo><mn>1</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		      
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		      
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> 
        = 
 <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>20</mn><mo>+</mo><mn>90</mn><mo>+</mo><mn>4</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		      
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		      
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> = <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn> 114</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		      
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		      
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math>

</div>

<div class="equation">
    <math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mn>3</mn> </mtd>
		      <mtd> <mn>4</mn> </mtd>
		    </mtr>
		    <mtr  color="#ff0000" fontweight="bold">
		      <mtd  mathbackground="#ffcccc"> <mn>2</mn> </mtd>
		      <mtd  mathbackground="#ffcccc"> <mn>4</mn> </mtd>
		      <mtd  mathbackground="#ffcccc"> <mn>5</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
	</math>
	&middot;
	<math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>20</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>30</mn> </mtd>  
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> =  <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd > <mn>115</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>2</mn><mo>&#215;</mo><mn>20</mn> <mo>+</mo> <mn>4</mn><mo>&#215;</mo><mn>30</mn> <mo>+</mo> <mn>5</mn><mo>&#215;</mo><mn>1</mn> </mtd>
		      
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		      
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> = <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd > <mn>115</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>40</mn> <mo>+</mo> <mn>120</mn> <mo>+</mo> <mn>5</mn> </mtd>
		      
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		      
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> = <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd > <mn>115</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>165</mn> </mtd>
		      
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		      
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> 
</div>


<div class="equation">
    <math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mn>3</mn> </mtd>
		      <mtd> <mn>4</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>2</mn> </mtd>
		      <mtd> <mn>4</mn> </mtd>
		      <mtd> <mn>5</mn> </mtd>
		    </mtr>
		    <mtr   color="#ff0000" fontweight="bold">
		      <mtd  mathbackground="#ffcccc"> <mn>0</mn> </mtd>
		      <mtd  mathbackground="#ffcccc"> <mn>0</mn> </mtd>
		      <mtd  mathbackground="#ffcccc"> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
	</math>
	&middot;
	<math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>20</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>30</mn> </mtd>  
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> =  <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd > <mn>115</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd> <mn>165</mn> </mtd>
		      
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>0</mn><mo>&#215;</mo><mn>20</mn> <mo>+</mo> <mn>0</mn><mo>&#215;</mo><mn>30</mn> <mo>+</mo> <mn>1</mn><mo>&#215;</mo><mn>1</mn> </mtd>
		      
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> = <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd > <mn>115</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd> <mn>165</mn> </mtd>
		      
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>0 + 0 + 1</mn> </mtd>
		      
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> = <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd > <mn>115</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd> <mn>165</mn> </mtd>
		      
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"><mn>1</mn> </mtd>
		      
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> 
</div>

A little bit more complicated that the dot product, but not too bad.

<h2>So, How Does This Relate To CSS3 Transforms?</h2>

A transformation of an block using the <code>matrix()</code> function is done by <strong>multiplying the matrix with each of the corner-coordinates of the block</strong> which will give the corners of the new object <strong>when the <code>transform-origin</code> is set to <code>0 0</code></strong>. For example let's say you have an HTML element styled like this:</p>

<blockquote class="code">
<pre>
#transformedObject {
     position: absolute;
     left: 0px;
     top: 0px;
     width: 200px;
     height: 80px;
     transform:  matrix(0.9, -0.05, -0.375, 1.375, 220, 20);
     transform-origin: 0 0;
}
</pre>
</blockquote>

<p>What is the end result?  Well, first let's take a look at the object <strong>without</strong> the transform CSS:</p>


<div id="originalExample" class="exampleBackground">
    <div class="point top-left-pt"></div>
    <div class="point top-right-pt"></div>
    <div class="point bottom-left-pt"></div>
    <div class="point bottom-right-pt"></div>
    <div class="coord top-left">(0, 0)</div>
      <div class="coord top-right">(200, 0)</div>
      <div class="coord bottom-left">(0, 80)</div>
      <div class="coord bottom-right">(200, 80)</div>
   <div id="exampleObject1">
      
   </div>
</div>

<p>When the browser applies the transform to this block, it takes the matrix and multiplies it to each of the corner coordinates.  For example, taking the bottom-right corner, (200, 80) or 
<math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>200</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>80</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
                  </mtable>
                <mo> ) </mo>
          </mrow>
</math>, we get:
</p>

<div class="equation">
    <p>
    <math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd   mathbackground="#ffcccc"> <mn>0.9</mn> </mtd>
		      <mtd   mathbackground="#ffcccc"> <mn>-0.375</mn> </mtd>
		      <mtd   mathbackground="#ffcccc"> <mn>220</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>-0.05</mn> </mtd>
		      <mtd> <mn>1.375</mn> </mtd>
		      <mtd> <mn>20</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
	</math>
	&middot;
	<math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>200</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>80</mn> </mtd>  
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> =  <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd  color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>0.9</mn><mo>&#215;</mo><mn>200</mn> <mo>+</mo> <mn>-0.375</mn><mo>&#215;</mo><mn>80</mn> <mo>+</mo> <mn>220</mn><mo>&#215;</mo><mn>1</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		      
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		      
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math>  = <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd  color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>180</mn> <mo>-</mo><mn>30</mn> <mo>+</mo> <mn>220</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		      
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		      
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> = <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd  color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>370</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		      
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		      
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> 
        </p>
        <p>
              <math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>0.9</mn> </mtd>
		      <mtd> <mn>-0.375</mn> </mtd>
		      <mtd> <mn>220</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd    mathbackground="#ffcccc"> <mn>-0.05</mn> </mtd>
		      <mtd    mathbackground="#ffcccc"> <mn>1.375</mn> </mtd>
		      <mtd    mathbackground="#ffcccc"> <mn>20</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
	</math>
	&middot;
	<math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>200</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>80</mn> </mtd>  
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> = <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd> <mn>370</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>-0.05</mn><mo>&#215;</mo><mn>200</mn> <mo>+</mo> <mn>1.375</mn><mo>&#215;</mo><mn>80</mn> <mo>+</mo> <mn>20</mn><mo>&#215;</mo><mn>1</mn> </mtd>  
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
       </math> = <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd> <mn>370</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>-10</mn> <mo>+</mo> <mn>110</mn> <mo>+</mo> <mn>20</mn> </mtd>  
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> = <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd> <mn>370</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd  color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>120</mn> </mtd>  
		    </mtr>
		    <mtr>
		      <mtd> <mn>.</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math>
        </p>

          <p>
              <math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>0.9</mn> </mtd>
		      <mtd> <mn>-0.375</mn> </mtd>
		      <mtd> <mn>220</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd > <mn>-0.05</mn> </mtd>
		      <mtd > <mn>1.375</mn> </mtd>
		      <mtd  > <mn>20</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd  mathbackground="#ffcccc"> <mn>0</mn> </mtd>
		      <mtd   mathbackground="#ffcccc"> <mn>0</mn> </mtd>
		      <mtd   mathbackground="#ffcccc"> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
	</math>
	&middot;
	<math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>200</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>80</mn> </mtd>  
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> = <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd> <mn>370</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>120</mn> </mtd>  
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>0&#215;200 + 0&#215;80 + 1&#215;1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
       </math> = <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd> <mn>370</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>120</mn> </mtd>  
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>0</mn> <mo>+</mo> <mn>0</mn> <mo>+</mo> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> = <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd> <mn>370</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>120</mn> </mtd>  
		    </mtr>
		    <mtr>
		      <mtd color="#0000ff" fontweight="bold" mathbackground="#ffcccc"> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math>
        </p>
</div>

<p>Let's look at the results for the other three coordinates.  For (200, 0):</p>

<div class="equation">
    <p>
    <math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>0.9</mn> </mtd>
		      <mtd> <mn>-0.375</mn> </mtd>
		      <mtd> <mn>220</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>-0.05</mn> </mtd>
		      <mtd> <mn>1.375</mn> </mtd>
		      <mtd> <mn>20</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
	</math>
	&middot;
	<math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd> <mn>200</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>  
		    </mtr>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> =  <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd> <mn>0.9</mn><mo>&#215;</mo><mn>200</mn> <mo>+</mo> <mn>-0.375</mn><mo>&#215;</mo><mn>0</mn> <mo>+</mo> <mn>220</mn><mo>&#215;</mo><mn>1</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd> <mn>-0.05</mn><mo>&#215;</mo><mn>200</mn> <mo>+</mo> <mn>1.375</mn><mo>&#215;</mo><mn>0</mn> <mo>+</mo> <mn>20</mn><mo>&#215;</mo><mn>1</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn><mo>&#215;</mo><mn>200</mn> <mo>+</mo> <mn>0</mn><mo>&#215;</mo><mn>0</mn> <mo>+</mo> <mn>20</mn><mo>&#215;</mo><mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math>  = <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd> <mn>400</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd> <mn>10</mn> </mtd>
		      
		    </mtr>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		      
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> 
        </p>
</div>


<p>For (0, 80) test:</p>

<div class="equation">
    <p>
    <math display="inline">
	    <mrow>
    <mo>(</mo>
    <mtable>
      <mtr>
        <mtd>
          <mn>0.9</mn>
        </mtd>
        <mtd>
          <mn>-0.375</mn>
        </mtd>
        <mtd>
          <mn>220</mn>
        </mtd>
      </mtr>
      <mtr>
        <mtd>
          <mn>-0.05</mn>
        </mtd>
        <mtd>
          <mn>1.375</mn>
        </mtd>
        <mtd>
          <mn>20</mn>
        </mtd>
      </mtr>
      <mtr>
        <mtd>
          <mn>0</mn>
        </mtd>
        <mtd>
          <mn>0</mn>
        </mtd>
        <mtd>
          <mn>1</mn>
        </mtd>
      </mtr>
    </mtable>
    <mo>)</mo>
  </mrow>
  <mo>&#xB7;</mo>
  <mrow>
    <mo>(</mo>
    <mtable>
      <mtr>
        <mtd>
          <mn>0</mn>
        </mtd>
      </mtr>
      <mtr>
        <mtd>
          <mn>80</mn>
        </mtd>
      </mtr>
      <mtr>
        <mtd>
          <mn>1</mn>
        </mtd>
      </mtr>
    </mtable>
    <mo>)</mo>
  </mrow>

  <mo>=</mo>
  <mrow>
    <mo>(</mo>
    <mtable>
      <mtr>
        <mtd>
          <mn>0.9</mn>
          <mo>&#xD7;</mo>
          <mn>0</mn>
          <mo>+</mo>
          <mn>-0.375</mn>
          <mo>&#xD7;</mo>
          <mn>80</mn>
          <mo>+</mo>
          <mn>220<mn><mo>&#xD7;</mo><mn>1</mn></mn></mn>
        </mtd>
      </mtr>
      <mtr>
        <mtd>
          <mn>-0.05</mn>
          <mo>&#xD7;</mo>
          <mn>0</mn>
          <mo>+</mo>
          <mn>1.375</mn>
          <mo>&#xD7;</mo>
          <mn>80</mn>
          <mo>+</mo>
          <mn>20</mn>
          <mo>&#xD7;</mo>
          <mn>1</mn>
        </mtd>
      </mtr>
      <mtr>
        <mtd>
          <mn>0</mn>
          <mo>&#xD7;</mo>
          <mn>0</mn>
          <mo>+</mo>
          <mn>0</mn>
          <mo>&#xD7;</mo>
          <mn>80</mn>
          <mo>+</mo>
          <mn>20</mn>
          <mo>&#xD7;</mo>
          <mn>1</mn>
        </mtd>
      </mtr>
    </mtable>
    <mo>)</mo>
  </mrow>
<mo>=</mo>	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd> <mn>190</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd> <mn>130</mn> </mtd>
		      
		    </mtr>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		      
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> 
        </p>
</div>



<p>And finally, for (0, 0):</p>

<div class="equation">
    <p>
    <math display="inline">
	  <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>0.9</mn> </mtd>
		      <mtd> <mn>-0.375</mn> </mtd>
		      <mtd> <mn>220</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>-0.05</mn> </mtd>
		      <mtd> <mn>1.375</mn> </mtd>
		      <mtd> <mn>20</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
	</math>
	&middot;
	<math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>  
		    </mtr>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> =  <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd> <mn>0.9</mn><mo>&#215;</mo><mn>0</mn> <mo>+</mo> <mn>-0.375</mn><mo>&#215;</mo><mn>0</mn> <mo>+</mo> <mn>220</mn><mo>&#215;</mo><mn>1</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd> <mn>-0.05</mn><mo>&#215;</mo><mn>0</mn> <mo>+</mo> <mn>1.375</mn><mo>&#215;</mo><mn>0</mn> <mo>+</mo> <mn>20</mn><mo>&#215;</mo><mn>1</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn><mo>&#215;</mo><mn>0</mn> <mo>+</mo> <mn>0</mn><mo>&#215;</mo><mn>0</mn> <mo>+</mo> <mn>20</mn><mo>&#215;</mo><mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math>  = <math display="inline">	
		<mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr >
		      <mtd> <mn>220</mn> </mtd>
		     
		    </mtr>
		    <mtr>
		      <mtd> <mn>20</mn> </mtd>
		      
		    </mtr>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		      
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow> 
	</math> 
        </p>
</div>

<p>Here is the result:</p>

<div id="transformedExample" class="exampleBackground">
       <div class="point top-left-pt"></div>
      <div class="point top-right-pt"></div>
      <div class="point bottom-left-pt"></div>
      <div class="point bottom-right-pt"></div>
      <div class="coord top-left">(220, 20)</div>
      <div class="coord top-right">(400, 10)</div>
      <div class="coord bottom-left">(190, 130)</div>
      <div class="coord bottom-right">(370, 120)</div>
   <div id="exampleObject2">
      
   </div>
</div>

<p>I don't expect anyone to calculate these by hand on a regular basis (I personally don't).  But now you know what these numbers mean.  :-)</p>

<h2>What About Internet Explorer?</h2>
<p>Although <code>transform-origin</code> doesn't work with IE, it is possible to generate the matrix in another browser and use <a href="http://www.useragentman.com/blog/csssandpaper-a-css3-javascript-library/">cssSandpaper</a> to generate the shape of the transform in that browser.  In order to fix the positioning, one can use <a href="http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/">Paul Irish's conditional stylesheet fix</a> to position that block manually in IE only (it is what I used on this page so that the matrix transform in my example above appeared correctly in IE).  I hope to have transform-origin working in cssSandpaper soon so that it will be unnecessary to do this.</p>

<h2>Other Interesting Facts About Matrices</h2>

<ol>
<li>All the other CSS3 transform functions have equivalent matrix notation:

<table class="dataTable">
<thead class="centered">
<tr>
<th>
scale(a)
</th>
<th>
scaleX(x)
</th>
<th>
scaleY(y)
</th>
<th>translateX(x)</th>
<th>translateY(y)</th>
<th>translate(x,y)</th>




</tr>
</thead>

<tbody class="centered">
<tr>
<td>
<math display="inline">
     <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>a</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>a</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
</math>
</td>
<td>
<math display="inline">
     <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>x</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
</math>
</td>
<td>
<math display="inline">
     <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>y</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
</math>
</td>
<td>
<math display="inline">
     <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>x</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
</math>
</td>
<td>
<math display="inline">
     <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>y</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
</math>
</td>
<td>
<math display="inline">
     <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>x</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>y</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
</math>
</td>


</tr>
</table>

<table class="dataTable">
<thead class="centered">
<tr>
<th>skewX(x)</th>
<th>skewY(y)</th>
<th>skew(x,y)</th>
<th>rotate(θ)</th>

</tr>
</thead>
<tbody class="centered">
<tr>

<td>
<math display="inline">
     <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mo>tan</mo><mi>x</mi> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
</math>
</td>
<td>
<math display="inline">
     <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mo>tan</mo><mi>y</mi> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
</math>
</td>
<td>
<math display="inline">
     <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mo>tan</mo><mi>x</mi> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mo>tan</mo><mi>y</mi> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
</math>
</td>
<td>
<math display="inline">
     <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mo>cos</mo><mi>θ</mi> </mtd>
		      <mtd> <mo>-sin</mo><mi>θ</mi> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mo>sin</mo><mi>θ</mi> </mtd>
		      <mtd> <mo>cos</mo><mi>θ</mi> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
</math>
</td>
</tr>
</table>


</li>

<li>A list of transforms in CSS3 like this:

<blockquote class="code">
<pre>
#o1 {
   transform-origin: 0px 0px;
   transform: rotate(15deg) translateX(230px) scale(1.5);
}
</pre>
</blockquote>

is the same as multiplying the equivalent matrices together:

<div class="equation">
<math display="inline">
     <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mo>cos</mo><mi>15&#176;</mi> </mtd>
		      <mtd> <mo>-sin</mo><mi>15&#176;</mi> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mo>sin</mo><mi>15&#176;</mi> </mtd>
		      <mtd> <mo>cos</mo><mi>15&#176;</mi> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
</math> &middot;
<math display="inline">
     <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>230</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
</math> &middot; <math display="inline">
     <mrow>
		  <mo> ( </mo>
		  <mtable>
		    <mtr>
		      <mtd> <mn>1.5</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		      <mtd> <mn>0</mn> </mtd>
		    </mtr>
		    <mtr>
		      <mtd> <mn>0</mn> </mtd>
		      <mtd > <mn>0</mn> </mtd>
		      <mtd> <mn>1</mn> </mtd>
		    </mtr>
		  </mtable>
		  <mo> ) </mo>
		</mrow>
</math>
</div>

(I know, I didn't tell you how to multiply matrices together. 
<a href="http://people.hofstra.edu/stefan_waner/RealWorld/index.html">Stephan Waner</a> has written <a href="http://people.hofstra.edu/stefan_waner/realworld/tutorialsf1/frames3_2.html">a good tutorial for matrix multiplication</a> if you are interested.)</li>


<li><strong>CSS3 2D-Transforms can only transform blocks into parallelograms</strong>.  For example, it is impossible to transform a block into this shape:


<div id="transformedExample" class="exampleBackground">
      <img src="http://www.useragentman.com/blog/wp-content/uploads/2011/01/irregular.png" alt="[Irregular Shape]" title="irregular"  />
</div>

In order to do this, one must use <a href="http://www.w3.org/TR/css3-3d-transforms/">CSS3 3D Transforms</a>.  This is why the Matrix Construction Set only has three control points to drag around, not four.  If you would like to see a version of the Matrix Construction Set to use <code>matrix3d()</code>, please let me know by leaving a comment below.</li>




</ol>

<h2>In Conclusion</h2>

<p>I don't expect anyone to be an expert in matrix artithmetic after reading this article.  But at least you know what those pesky numbers mean, and this information may become useful in the future.</p>

<h2>Acknowledgments</h2>

<ul>
<li>The MathML markup used to display the mathematical equations in this article were rendered by <a href="http://www.mathjax.org/">MathJax</a>, an excellent open-source JavaScript library</li>
<li>The equations on  <a href="http://www.physicsforums.com/showthread.php?t=360963">this Physics Forum post</a> was quite helpful in the creation of the Matrix Construction Set.</li>
<li>The Matrix Construction Set uses <a href="http://sylvester.jcoglan.com/">the Sylvester JavaScript library</a> written by <a href="http://jcoglan.com/">James Coglan</a> for performing matrix calculations.  It also used my own <a href="http://www.useragentman.com/blog/2010/01/10/cross-browser-html5-drag-and-drop/">DragDropHelpers</a> library to smooth out the different browser implementations of HTML5 Drag and Drop.</li>
<li>A blog post that has a bunch of math equations on it may be scary for web designers.  My apologies.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2011/01/07/css3-matrix-transform-for-the-mathematically-challenged/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Cross Browser HTML5 Ruby Annotations Using CSS</title>
		<link>http://www.useragentman.com/blog/2010/10/29/cross-browser-html5-ruby-annotations-using-css/</link>
		<comments>http://www.useragentman.com/blog/2010/10/29/cross-browser-html5-ruby-annotations-using-css/#comments</comments>
		<pubDate>Fri, 29 Oct 2010 04:17:18 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS3]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Internationalization]]></category>
		<category><![CDATA[Polyfills]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[bopomofo]]></category>
		<category><![CDATA[chinese]]></category>
		<category><![CDATA[furigana]]></category>
		<category><![CDATA[hangul]]></category>
		<category><![CDATA[hanja]]></category>
		<category><![CDATA[hiragana]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[internationalization]]></category>
		<category><![CDATA[japanese]]></category>
		<category><![CDATA[kanji]]></category>
		<category><![CDATA[katakana]]></category>
		<category><![CDATA[korean]]></category>
		<category><![CDATA[multilanguage]]></category>
		<category><![CDATA[multilingual]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[xhtml]]></category>
		<category><![CDATA[xhtml 2.0]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=1658</guid>
		<description><![CDATA[<img src="/blog/wp-content/uploads/2010/10/containerFirefox2.png"  /> Ruby Characters, although used originally to help people read complicated Chinese and Japanese characters, can also be used to annotate all types of information to written text.  This article shows how you can use it in browsers that support it, but also in ones that don't using a simple stylesheet.]]></description>
			<content:encoded><![CDATA[<div id="attachment_1735" class="wp-caption alignright" style="width: 419px"><a href="http://www.flickr.com/photos/eliazar/3193967863/"><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/10/rubyExample.jpg" alt="An example of ruby text, from a photo by Flickr user eliazar" title="screenshot" width="409" height="261" class="size-full wp-image-1619" /></a>
<p class="wp-caption-text">An example of ruby text, from <a href="http://www.flickr.com/photos/eliazar/3193967863/">a photo</a> by Flickr user <a href="http://www.flickr.com/photos/eliazar/">eliazar</a>.&#8221;</p>
</div>
<p>When talking about <strong>Ruby Annotations</strong>, I don&#8217;t mean Ruby on Rails, or anything related to the Ruby programming language.  I am talking about <a href="http://en.wikipedia.org/wiki/Ruby_character"><strong>ruby characters</strong></a> which <strong>are used to annotate text with supplementary information.</strong>  For example if some readers wouldn&#8217;t know what the term &#8220;WWW&#8221; means, we can use ruby annotations to help them out like this:</p>
<div class="rubyExample">
<p>I love the <ruby><rb>WWW</rb><rp> （</rp><rt class="small" style="font-size: 0.37em">World Wide Web</rt><rp>） </rp></ruby>.</p>
</div>
<p>The <strong>ruby base</strong> (in this case &#8220;WWW&#8221;) is annotated by <strong>ruby text</strong> (&#8220;World Wide Web&#8221;).  In this case, the ruby text appears in a small font-size above the base.  Traditionally, ruby annotations have been used in Chinese, Korean and Japanese text-books to help students of those languages sound out more complicated characters:</p>
<div class="rubyExample japanese">
  <ruby><rb>東京</rb><rp> （</rp><rt>とうきょう</rt><rp>） </rp></ruby>に<ruby>行<rp> （</rp><rt>い</rt><rp>） </rp></ruby>きたい。
</div>
<p>In the text above, the <strong>ruby base is written in Kanji</strong>, many of which are only readable by Japanese with a high-school education. The <strong>ruby text is written in Hiragana</strong>, which all Japanese grade-schoolers can understand. </p>
<p>I can hear you saying to yourself <strong>&#8220;Who cares?  I&#8217;m not Japanese!  I can&#8217;t read any of it!&#8221;</strong>. Ruby annotations can also be used to help those who don&#8217;t speak the language at all.  An author can use ruby to help English speakers  pronounce Japanese &#8230; </p>
<div class="rubyExample japanese">
  <ruby><rb>東京</rb><rp> (</rp><rt><span class="romaji">tō kyō</span></rt><rp>) </rp></ruby> <ruby>に<rp> (</rp><rt><span class="romaji">ni</span></rt><rp>) </rp></ruby><ruby>行き<rp> (</rp><rt><span class="romaji">iki</span></rt><rp>) </rp></ruby><ruby>たい<rp> (</rp><rt><span class="romaji">tai</span></rt><rp>) </rp></ruby>。
</div>
<p>.. or, for that matter, any other language, like Ukrainian.</p>
<div class="rubyExample">
<ruby><rb>Золтан </rb><rp> (</rp><rt>zol-tan</rt><rp>)</rp></ruby> <ruby><rb>Євгенович </rb><rp> (</rp><rt>yew-hen-o-vich</rt><rp>)</rp></ruby> <ruby><rb>Гаврилюк </rb><rp> (</rp><rt>how-real-luke</rt><rp>)</rp></ruby>.
</div>
<p>It can also allow an author to give translations on top of foreign words.</p>
<div class="rubyExample japanese">
  <ruby><rb>東京 に 行き たい</rb><rp> (</rp><rt><span class="romaji">I want to go to Tokyo.</span></rt><rp>) </rp></ruby>。
</div>
<h2>But I Have a Hard Enough Time With English!</h2>
<p>Ruby text can also be used for help foreign students pronounce English words that may difficult to read:</p>
<div class="rubyExample">
His <ruby><rb>communication </rb><rp> (</rp><rt class="phonetic">k&#601;-my<span class="long">oo</span>-n&#301;-<strong>k&#257;&#180;</strong>-sh&#601;n</rt><rp>) </rp></ruby> skills are quite bad.
</div>
<p>It can be also be used to help English speakers read intimidating Eastern European surnames transliterated into English:</p>
<div class="rubyExample">
My name is Zoltan <ruby><rb>Hawryluk</rb><rp> (pronounced: </rp><rt>how-real-luke</rt><rp>)</rp></ruby>.
</div>
<p>(<strong>An aside note to telemarketers:</strong> if you call my home and ask my to speak to &#8220;Mister Have &#8212;- um &#8212; uk&#8221; , &#8220;Mister Hawww &#8212; ahh &#8212; uhhh&#8221; <strong><em>or especially &#8220;Mr. Zoltar&#8221;</em></strong>, I will hang up immediately.  If you actually pronounce my name correctly, I&#8217;ll give you at least five minutes of my time.  I mean it.)</p>
<h2>Okay, how is this coded?</h2>
<p>Let&#8217;s take a look at the markup of the above example:</p>
<blockquote class="code">
<pre>
My name is Zoltan
&lt;ruby&gt;
   &lt;rb&gt;Hawryluk&lt;/rb&gt;
   &lt;rp&gt; (&lt;/rp&gt;
   &lt;rt&gt;how-real-luke&lt;/rt&gt;
   &lt;rp&gt;) &lt;/rp&gt;
&lt;/ruby&gt;
</pre>
</blockquote>
<p>Let&#8217;s break down what each of these tags do:</p>
<table class="dataTable">
<thead>
<tr>
<th>Tag</th>
<th>Name</th>
<th>Function</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>&lt;ruby&gt;</code>
</td>
<td>Ruby Tag</td>
<td>Used to specify a Ruby Annotation.</td>
</tr>
<tr>
<td>
<code>&lt;rb&gt;</code>
</td>
<td>Ruby Base Tag</td>
<td>Used to specify the text that is to be annotated.</td>
</tr>
<tr>
<td><code>&lt;rt&gt;</code></td>
<td>Ruby Text</td>
<td>The actual ruby text, which contains the actual annotation.  By default, it appears above the actual text.</td>
</tr>
<tr>
<td><code>&lt;rp&gt;</code></td>
<td>Ruby Parenthesis</td>
<td>These are displayed to browsers that cannot display ruby text, and permits graceful degradation.  It also allows the text to be copied and pasted into another document without the formatting.</td>
</tr>
</tbody>
</table>
<p>To illustrate the last point about the <code>&lt;rp&gt;</code> tag, copy and paste the following ruby text into notepad, vi or any other plain text editor:</p>
<div class="rubyExample">
My name is Zoltan <ruby><rb>Hawryluk</rb><rp> (pronounced: </rp><rt>how-real-luke</rt><rp>)</rp></ruby>.
</div>
<p>You will see the following in your text editor:</p>
<blockquote class="code">
<pre>
My name is Zoltan Hawryluk (pronounced: how-real-luke).
</pre>
</blockquote>
<p>This is because the of how the <code>&lt;rp&gt;</code> tags are defined:</p>
<blockquote class="code">
<pre>
My name is Zoltan &lt;ruby&gt;&lt;rb&gt;Hawryluk&lt;/rb&gt;<span class="hilite">&lt;rp&gt; (pronounced: &lt;/rp&gt;</span>&lt;rt&gt;how-real-luke&lt;/rt&gt;<span class="hilite">&lt;rp&gt;)&lt;/rp&gt;</span>
</pre>
</blockquote>
<h2>Browser Support</h2>
<p>So what browsers support these tags?  Originally, the <code>&lt;ruby&gt;</code> tag was a proprietary Microsoft tag that worked in IE 5+.  Since then it has become a part of HTML5 and WebKit based browsers like Safari and Chrome now support it as well.  In order for Firefox and Opera to join the party, I <a href="http://www.useragentman.com/shared/css/ruby/screen.css">remixed a stylesheet</a> developed for <a href="http://www.princexml.com/bb/viewtopic.php?t=81">Prince XML</a> by <a href="http://www.chavchanidze.com/">George Chavchanidze</a> of Opera Software.  Below are some screenshots of how the ruby text looks in the various browsers with or without the stylesheet.</p>
<table class="dataTable">
<thead>
<tr>
<th></th>
<th>With stylesheet set</th>
<th>Without stylesheet set</th>
</tr>
</thead>
<tbody>
<tr>
<th>IE 6.0+</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/10/example-IE-with.png" alt="example screenshot with IE with stylesheet set"  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/10/example-IE-without.png" alt="example screenshot with IE with stylesheet set"   /></td>
</tr>
<tr>
<th>Firefox 3.6+</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/10/example-Firefox-with.png" alt="example screenshot with Firefox with stylesheet set" title="example-IE-without"  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/10/example-Firefox-without.png" alt="example screenshot with Firefox with stylesheet set"   /></td>
</tr>
<tr>
<th>Safari 5.0+</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/10/example-safari-with.png" alt="example screenshot with Safari with stylesheet set"   /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/10/example-safari-without.png" alt="example screenshot with Safari with stylesheet set"  /></td>
</tr>
<tr>
<th>Chrome 7.0+</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/10/example-chrome-with.png" alt="example screenshot with Chrome with stylesheet set" title="example-IE-without"  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/10/example-chrome-without.png" alt="example screenshot with Chrome with stylesheet set" title="example-IE-without"  /></td>
</tr>
<tr>
<th>Opera 10.63+</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/10/example-opera-with.png" alt="example screenshot with Opera with stylesheet set"  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/10/example-opera-without.png" alt="example screenshot with Opera with stylesheet set"  /></td>
</tr>
</tbody>
</table>
<p><a class="exampleLink" href="http://www.useragentman.com/shared/css/ruby/screen.css">Download the stylesheet used in the examples on this page</a></p>
<p>I took George&#8217;s <strong>excellent</strong> work and added a few additional features:</p>
<ul>
<li>It tells IE to ignore the Firefox and Opera styles (since they don&#8217;t look so good in IE)</li>
<li>It makes the font size smaller in Chrome and Safari (a personal preference).</li>
<li>It sets the <code>&lt;ruby&gt;</code> tag&#8217;s <code>ruby-align</code> CSS property to <code>center</code>, so that IE mimics the way the WebKit browser display ruby text by default (IE&#8217;s default seems to be <code>distribute-letter</code>, which looks horrible in a lot of situations).</li>
</ul>
<p>Also note that without the stylesheet, the ruby text gracefully degrades in Firefox and Opera, so it is still readable, and illustrates the usefulness of the <code>&lt;rp&gt;</code> tags.  This stylesheet occasionally cause some unusual rendering in some older browsers (such as Safari 4.0 for Mac), but on the whole it does a pretty good job.</p>
<p>Note that in order to make the stylesheet set to work correctly, you must replace the <code>&lt;body&gt;</code> tag with this variation of <a href="http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/">Paul Irish&#8217;s Conditional CSS design pattern</a>:</p>
<blockquote class="code">
<pre>
&lt;!--[if lt IE 7 ]&gt; &lt;body class="ie6"&gt; &lt;![endif]--&gt;
&lt;!--[if IE 7 ]&gt;    &lt;body class="ie7"&gt; &lt;![endif]--&gt;
&lt;!--[if IE 8 ]&gt;    &lt;body class="ie8"&gt; &lt;![endif]--&gt;
&lt;!--[if IE 9 ]&gt;    &lt;body class="ie9"&gt; &lt;![endif]--&gt;
&lt;!--[if (gt IE 9)]&gt;&lt;body class="modern"&gt; &lt;![endif]--&gt;
&lt;!--[!(IE)]&gt;&lt;!--&gt;  &lt;body class="notIE modern"&gt; &lt;!--&lt;![endif]--&gt;
</pre>
</blockquote>
<p>Using this declaration of the <code>&lt;body&gt;</code> blocks the Firefox and Opera styles in IE.</p>
<h2>Advanced Ruby Annotations</h2>
<p>Using the stylesheet, it is possible to also <strong>break ruby text into parts (e.g. syllables) without using a separate <code>&lt;ruby&gt;</code> tag for each part</strong>.  This is done with the <code>&lt;rbc&gt;</code> (ruby base container) and <code>&lt;rtc&gt;</code> (ruby text container).  Here is an example:</p>
<div class="rubyExample">
His <ruby class="syllables unicodeRubyText"><rbc><rb>com</rb><rb>mu</rb><rb>ni</rb><rb>ca</rb><rb>tion </rb></rbc><rp> (</rp><rtc><rt >k&#601;m</rt><rt>myoo</rt><rt>n&#301;</rt><rt><strong>k&#257;&#180;</strong></rt><rt  >sh&#601;n</rt></rtc><rp>) </rp></ruby> skills are quite bad.
</div>
<p>Although Internet Explorer does not understand these new tags, it does degrade gracefully.  Here is a comparison of how IE renders the above and how Firefox does.</p>
<table class="dataTable">
<thead>
<tr>
<th>Firefox 3.6</th>
<th>Internet Explorer 6+</th>
</tr>
</thead>
<tbody>
<tr>
<td style="width: 50%"><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/10/containerFirefox.png" alt="[Screenshot of rbc/rtc tag example in Firefox]"  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/10/containerExplorer.png" alt="[Screenshot of rbc/rtc tag example in IE]"  /></td>
</tr>
</tbody>
</table>
<p>Here is the code that generated the example above.  </p>
<blockquote class="code">
<pre>
&lt;ruby class="syllables unicodeRubyText"&gt;
<span class="hilite">  &lt;rbc&gt;
    &lt;rb&gt;com&lt;/rb&gt;&lt;rb&gt;mu&lt;/rb&gt;&lt;rb&gt;ni&lt;/rb&gt;&lt;rb&gt;ca&lt;/rb&gt;&lt;rb&gt;tion &lt;/rb&gt;
  &lt;/rbc&gt;</span>
  &lt;rp&gt; (&lt;/rp&gt;
<span class="hilite">  &lt;rtc&gt;
    &lt;rt&gt;k&#601;m&lt;/rt&gt;&lt;rt&gt;myoo&lt;/rt&gt;&lt;rt&gt;n&#301;&lt;/rt&gt;&lt;rt&gt;&lt;strong&gt;k&#257;&lt;/strong&gt;&lt;/rt&gt;&lt;rt&gt;sh&#601;n&lt;/rt&gt;
  &lt;/rtc&gt;</span>
  &lt;rp&gt;) &lt;/rp&gt;
&lt;/ruby&gt;
</pre>
</blockquote>
<p>Note that the <code>&lt;ruby&gt;</code> tag is a member of the classes <code>syllables</code> and <code>unicodeRubyText</code>, which are defined in the stylesheet.  The <code>syllables</code> class tells the browser to separate the ruby base text into syllables, separated by the <a href="http://en.wikipedia.org/wiki/Middot">&#8220;Interpunct&#8221;</a> symbol, while the <code>unicodeRubyText</code> tells the browser to render the ruby text with a Unicode font, so that the pronunciation symbols are printed correctly (it seems that this is only a requirement in Explorer, since the other browsers handle this automatically).</p>
<p>Here is a rundown of these advanced tags:</p>
<table class="dataTable">
<thead>
<tr>
<th>Tag</th>
<th>Name</th>
<th>Function</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>&lt;rbc&gt;</code>
</td>
<td>Ruby Base Container</td>
<td>Contains a list of <code>&lt;rb&gt;</code> tags.  This list is usually separated into syllables or some other logical order to make the text easier to read.</td>
</tr>
<tr>
<td>
<code>&lt;rtc&gt;</code>
</td>
<td>Ruby Text Container</td>
<td>Contains a list of <code>&lt;rt&gt;</code> tags.  Each <code>&lt;rt&gt;</code> tag in this list corresponds to the respective <code>&lt;rb&gt;</code> tag in the <code>&lt;rtc&gt;</code> list.</td>
</tr>
</tbody>
</table>
</blockquote>
<p>Unfortunately, there is a bug in older WebKit browsers that gives very strange results. Here are screen shots of the above code as viewed in Chrome 7.0 for Windows, with and without my stylesheet:</p>
<table class="dataTable">
<thead>
<tr>
<th>Chrome 7.0 Windows (with stylesheet) </th>
<th>Chrome 7.0 Windows (without stylesheet) </th>
</tr>
</thead>
<tbody>
<tr>
<td style="width: 50%"><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/10/containerChromeWithStylesheet.png" alt="[Screenshot of rbc/rtc tag example in Chrome 7.0 With My Stylesheet]"  /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/10/containerChromeWithoutStylesheet.png" alt="[Screenshot of rbc/rtc tag example in Chrome 7.0 Without My Stylesheet]"  /></td>
</tr>
</tbody>
</table>
<p>This bug appears in Safari 4.x and lower and the current version of Chrome (7.0), for both Windows and Mac.  Since this has been fixed in Safari 5.0, I am assuming that this will be fixed in the next version of Chrome as well.</p>
<h2>Ruby CSS3 Properties</h2>
<p>There are three main CSS3 properties that can be used today &#8230; but in Internet Explorer only.  Here are a list of them, along with additional properties that the <a href="http://www.w3.org/TR/2001/WD-css3-ruby-20010216/">W3C put in their recommendation back in 2001</a>.</p>
<table class="dataTable">
<thead>
<tr>
<th>Property Name</th>
<th>Description</th>
<th>Support</th>
<th>More Info</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>ruby-align</code></td>
<td>Affects the horizontal alignment of the Ruby Text with respect to the ruby base.   Default value is <code>auto</code> (which is the same as <code>center</code>).  Other values are <code>left</code>, <code>right</code>, as well as <code>distribute-letter</code>, <code>distribute-space</code>, <code>line-edge</code>, <code>start</code> and <code>end</code>.</td>
<td><code>left</code>, <code>right</code>, as well as <code>distribute-letter</code>, <code>distribute-space</code>, <code>line-edge</code> work in IE5+.</td>
<td style="white-space: nowrap">
<ul>
<li><a href="http://www.w3.org/International/articles/ruby/#Slide0210">W3C Reference</a>
<li><a href="http://msdn.microsoft.com/en-us/library/ms531150%28VS.85%29.aspx">MSDN</a></li>
<li><a href="http://www.blooberry.com/indexdot/css/properties/intl/ralign.htm">Index  DOT  Css</a></li>
</ul>
</td>
</tr>
<tr>
<td><code>ruby-overhang</code></td>
<td>
Affects how the ruby text will &#8220;hang&#8221; over the ruby base if the ruby-text is wider than the ruby-base.  Valid values are <code>auto</code>, <code>none</code>, <code>whitespace</code>, <code>start</code> and <code>end</code>.</td>
<td><code>auto</code>, <code>none</code> and <code>whitespace</code> work in IE 5+</td>
<td>
<ul>
<li><a href="http://www.w3.org/International/articles/ruby/#Slide0260">W3C Reference</a>
<li><a href="http://msdn.microsoft.com/en-us/library/ms531151%28VS.85%29.aspx">MSDN</a></li>
<li><a href="http://www.blooberry.com/indexdot/css/properties/intl/roverhang.htm">Index  DOT  Css</a></li>
</ul>
</tr>
<tr>
<td><code>ruby-position</code></td>
<td>Indicates where ruby text should appear relative to the base text.  Default is <code>before</code>. Other values are <code>after</code> (which makes it appear underneath the text), <code>inline</code>, <code>left</code> and <code>right</code>.</td</p>
<td>Only <code>above</code> and <code>inline</code> work in IE5+</td>
<td>
<ul>
<li><a href="http://www.w3.org/International/articles/ruby/#Slide0180">W3C Reference</a>
<li><a href="http://msdn.microsoft.com/en-us/library/ms531152(VS.85).aspx">MSDN</a></li>
<li><a href="http://www.blooberry.com/indexdot/css/properties/intl/rposition.htm">Index  DOT  Css</a></li>
</ul>
</tr>
</tbody>
</table>
<p>Hopefully the other browsers will support these eventually.</p>
<h2>And So, In Conclusion &#8230;</h2>
<p>Here is a quick recap of the various things you should keep in mind when using Ruby Text:</p>
<ul>
<li>Firefox 3.6 and Opera 10.x need the stylesheet in order to show ruby text correctly.</li>
<li>Recent versions of Safari and Chrome don&#8217;t need it, but using the stylesheet won&#8217;t harm the display of simple ruby text.</li>
<li>IE doesn&#8217;t need the styleheet, but if you use the stylesheet to ensure cross-browser display of ruby text, you must use the special body tag declaration above.</li>
<li><code>&lt;rbc&gt;</code> and <code>&lt;rtc&gt;</code> are not recognized in any version of IE, but they will degrade gracefully.</li>
<li>Ruby text inside of <code>&lt;rtc&gt;</code> tags doesn&#8217;t look right in Chrome 7 or Safari 4.  Since they do show up right in Safari 5, I assume/hope this will be fixed in the next version of Chrome.</li>
<li>The advanced CSS3 <code>ruby-</code> properties, such as <code>ruby-align</code>, <code>ruby-overhang</code> and <code>ruby-position</code> are only supported by IE5+.</li>
</ul>
<h2>Additional Reading</h2>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/ff460533%28v=VS.85%29.aspx">[MS-RUBY]: Internet Explorer Ruby Annotation Standards Support Document</a> from the <a href="http://msdn.microsoft.com/">MSDN Web Site</a>.
<li><a href="http://webkit.org/blog/948/ruby-rendering-in-webkit/">Ruby Rendering in WebKit</a> from the <a href="http://webkit.org/blog/">Surfin’ Safari</a> Blog archive
<li><a href="http://www.w3.org/TR/css3-ruby/">The W3C Recommendation document for Ruby Annotations</a>, dated 31 May 2001 with corrections made on 25 June 2008</a></li>
<li><a href="http://www.w3.org/TR/2001/WD-css3-ruby-20010216/">The W3C CSS3 Rub Module Working Draft</a> dated 14 May 2003.</a>.  There is also <a href="http://dev.w3.org/csswg/css3-ruby/">an editor&#8217;s copy</a> dated 04 March 2010.</li>
<li><a href="http://princecss.com/bb/viewtopic.php?f=5&#038;t=81">XHTML Ruby in Prince</a>, the forum thread where the original stylesheet was developerd.  Great to see as a reference.</li>
</ul>
<p><a class="exampleLink" href="http://www.useragentman.com/shared/css/ruby/screen.css">Download the stylesheet used in the examples on this page</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2010/10/29/cross-browser-html5-ruby-annotations-using-css/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Creating Cross Browser HTML5 Forms Now, Using modernizr, webforms2 and html5Widgets</title>
		<link>http://www.useragentman.com/blog/2010/07/27/cross-browser-html5-forms-using-modernizr-webforms2-and-html5widgets/</link>
		<comments>http://www.useragentman.com/blog/2010/07/27/cross-browser-html5-forms-using-modernizr-webforms2-and-html5widgets/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 03:50:06 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[Forms]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Polyfills]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=1110</guid>
		<description><![CDATA[<img src="/blog/wp-content/uploads/2010/07/html5Calendar.png" />Next generation web forms using HTML5 is hard to do today due to spotty browser support.  I demonstrate how a suite of JavaScript libraries can be used to help us use HTML5 Forms <strong>today</strong>.]]></description>
			<content:encoded><![CDATA[<div class="importantNotes">
<h3>Updates:</h3>
<ul>
<li><strong>Sept. 10, 2010:</strong> This webforms2 script in the link below has been modified to fix validation issues with the newer versions of Safari and Chrome.</li>
<li><strong>Aug. 6, 2010:</strong> This article has been <a href="http://nz.pe.kr/wordpress/?p=547">translated into Korean</a>.</li>
<li><strong>May 12, 2011:</strong> html5Widgets has been <a href="http://www.useragentman.com/blog/2011/05/12/fixing-oninput-in-ie9-using-html5widgets/">updated to support the <code>oninput</code> event</a>. </li>
</ul>
</div>
<div class="wp-caption alignright" style="width: 298px; border: solid 3px black;">
<div class="size-full wp-image-1387" style="width: 288px">
<form action="javascript:void(0)">
<table class="html5ExampleTable">
<tr>
<th>Range:</th>
<td>
<input type="range" /></td>
</tr>
<tr>
<th>Placeholder:</th>
<td>
<input type="text" placeholder="This is a test" /></td>
</tr>
<tr>
<th>Color:</th>
<td>
<input type="color" /></td>
</tr>
</table>
</form>
</div>
<p class="wp-caption-text" style="width: 288px;">Working example HTML5 Form using <a href="http://www.modernizr.com">Modernizr</a>, <a href="https://github.com/westonruter/webforms2">webforms2</a> and my new script, html5Widgets.  Go ahead &#8230; try them out.  You know you wanna!</p>
</div>
<p>Calendars, colour swatches, sliding widgets, client side validation: this is the nirvana that the HTML5 forms module promises. Some would say &#8220;So what? I&#8217;ve seen this on the web for years!&#8221;, and they&#8217;d be right. There have been some really brilliant people coding some really interesting widget and validation frameworks, so why should we change?</p>
<ul>
<li><strong>Ease the markup learning curve:</strong> HTML5 form widgets and validation have been built to be as dead simple to markup as a select box with no JavaScript knowledge required</li>
<li><strong>It&#8217;s a W3C standard:</strong> so you know that it&#8217;ll work for years to come and if you have problems, you could always ask almost anyone in the web development community for help.</li>
<li><strong>Cellular phone support:</strong> HTML5 form fields will have optimized user interfaces appropriate for the type of device.  <a href="http://devblog.blackberry.com/2010/03/use-html5-in-your-blackberry-web-content/">Blackberry already has optimized versions of the date/time and color widgets</a> and, according to <a href="http://diveintohtml5.org/">Mark Pilgrim&#8217;s</a> article <a href="http://diveintohtml5.org/forms.html">A Form of Madness</a>, the virtual keyboards that appear when using HTML5 form fields are optimized for the data being input.</li>
<li><strong>Web development tools will have to support it:</strong> It&#8217;s a safe bet that Aptana, Dreamweaver, and all the other IDEs out there will have HTML5 support.</li>
<li><strong>It&#8217;s HTML5:</strong> when you tell your non-techie co-workers that you use it, you will be the envy of all &mdash; after all, it <strong>must</strong> be five times better than regular HTML, right?  Your boss will be so impressed that you are now a guru in this futuristic technology with a cool numbered acronym that he or she will give you a big fat raise!!!
<p>(Okay, okay.  Don&#8217;t try to laugh too hard &#8230; your co-workers will start to worry).</li>
</ul>
<h2>The Support Dilemma</h2>
<p>Unfortunately, today&#8217;s support for the HTML5 Form Module is spotty with each browser supporting different parts of the specification.  Take a look at Wikipedia&#8217;s<a href="http://en.wikipedia.org/wiki/Comparison_of_layout_engines_%28HTML5%29"> HTML5 Forms comparison chart</a>.  You&#8217;ll see each browser supporting a different set of features, but the lowest common denominator they all support is rather small.</p>
<h2>But I Want To Use It <strong>Now!!!</strong></h2>
<p>HTML5 Forms started off as <a href="http://www.whatwg.org/specs/web-forms/2004-06-27-call-for-comments/">WebForms 2.0 back in 2004</a>, so I wasn&#8217;t surprised to see that a few developers had already coded some JavaScript implementations.  Weston Ruter&#8217;s cross browser library, <code>webforms2.js</code>, implements a huge chunk of it, including parts that didn&#8217;t make the transition from WebForms 2.0 to HTML5 forms (e.g. <a href="http://www.whatwg.org/specs/web-forms/current-work/#repeatingFormControls">Webform 2.0&#8242;s Repetition Model</a>).</p>
<p>Since widgets weren&#8217;t implemented in <code>webforms2</code>, I created <code>html5Widgets.js</code>.  This interface library is independent of <code>webforms2</code> &#8211; if all you want is a cross-browser HTML5 form with validation, then all you need is the original <code>webforms2.js</code>.  If you want special widgets that are not available for all browsers, include <code>html5Widgets.js</code>.  Since I did not want to re-invent the wheel by creating widgets from scratch, <code>html5Widgets.js</code> uses some really nice third party JavaScript libraries to create them (more on that below).  It also uses <code><a href="http://www.modernizr.com">Modernizr</a></code> to detect if there is native support for each of the HTML5 form widgets &#8211; if not, <code>html5Widgets</code> steps in to put the right widget in place.  To save bandwidth, developers only need to include the third party libraries for the widgets they need.</p>
<p>In the rest of this article, I will go over different parts of the HTML5 specification and show how you can use them in your applications today, step-by-step.  Each section will state which browser natively supports that part of the specification, and what is needed for browsers that don&#8217;t.  Eventually, when the browser manufacturers catch up with the standard, you won&#8217;t need to use any JavaScript at all.</p>
<h2>Form Validation Using the <code>required</code> and <code>pattern</code> Attributes</h2>
<p>The <code>required</code> attribute makes an input field mandatory and forces the user to enter in a value in order to submit the form data.  The markup is simple</p>
<blockquote class="code">
<pre>&lt;input type="text" name="firstName" value="" required="required" /&gt;
</pre>
</blockquote>
<p>(<strong>Note:</strong>, you can also just use <code>required</code> on its own if you aren&#8217;t trying to be XHTML compliant.)</p>
<p>The <code>pattern</code> attribute forces the user to enter in a value using a specified format.  It uses <a href="http://en.wikipedia.org/wiki/Regular_expressions">regular expressions</a> to define this format. For example, if you want to force the user to input a U.S. Zip Code inside a form field, you would use the following markup.</p>
<blockquote class="code">
<pre>&lt;--
     zip code regular expression from
     <a href="http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256F6B005294C2">http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256F6B005294C2</a>
--&gt;
&lt;input type="text" name="zipCode" value=""
   pattern="\d{5}([\-]\d{4})?" required="required"  /&gt;
</pre>
</blockquote>
<p>Note that <code>required</code> and <code>pattern</code> are independent from each other.  You can have a <code>pattern</code> set on a form field without it being <code>mandatory</code>  (i.e. the pattern would only be checked if the user enters in data into the field).</p>
<p>Opera 10+ (mobile and desktop editions) is the only browser that supports the validation routines natively.  To use this in all other browsers, all you need is to include the following script tags in the <code>head</code> of your document.</p>
<blockquote class="code">
<pre>&lt;script type="text/javascript" src="/path/to/archive/js/weston.ruter.net/webforms2/webforms2_src.js"&gt;
&lt;/script&gt;
</pre>
</blockquote>
<p>Let&#8217;s take a look at how this looks visually:</p>
<table class="screenshots">
<thead>
<tr>
<th>Opera 10+ Windows</th>
<th>Firefox 3.6 with webforms2.js</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<a href="/tests/html5Widgets/patternRequired.html"><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/07/validityOpera.png" alt="[Screenshot of HTML5 Validation using Opera]" title="Screenshot of HTML5 Validation using Opera"  class="alignnone size-full wp-image-1282" /></a>
</td>
<td>
<a href="/tests/html5Widgets/patternRequired.html"><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/07/validityFirefoxWithHTML5Widgets.png" alt="[Screenshot of HTML5 Validation using Firefox and webforms2.js]" title="Screenshot of HTML5 Validation using Firefox and webforms2.js"  class="alignnone size-full wp-image-1282" /></a>
</td>
</tr>
</tbody>
</table>
<p>Note the &#8220;starred&#8221; style of the form field &mdash; this is not the default look and feel of the <code>required</code> fields, but something I added with a simple amount of CSS:</p>
<blockquote class="code">
<pre>input[required], select[required] {
	background: #ffffee url("../images/asterix.gif")  no-repeat left 2px;
	padding-left: 1.5em;
	width: 13.5em !important;
}
</pre>
</blockquote>
<p>(<strong>This CSS does <em>not</em> work in Internet Explorer 6.</strong>  Given that <strong>everything else in this article does</strong>, I hope the reader will overlook this one oversight considering that this browser should have entered retirement years ago (and have its driving license taken away, and shipped off to some really horrible nursing home watching really crappy talk shows all day and eating bad food &#8230;)</p>
<p><a class="exampleLink" href="/tests/html5Widgets/patternRequired.html">See an example of <code>pattern</code> and <code>required</code> in action.</a></p>
<p>Note that the same validation framework checks the values of inputs of type <code>email</code>, <code>url</code> and <code>number</code> to ensure that the values are in their respective valid formats.  As an added bonus, if you are using the iPhone or iPad version of Safari, the virtual keyboard that appears will be optimized for these type of form fields (e.g. when editing a <code>number</code> field, the keyboard that appears contains only digits and the &#8220;+&#8221;, &#8220;-&#8221;, and &#8220;.&#8221; keys.  This is native behaviour for iOS, and I hope other mobile browsers, such the Android&#8217;s, follow suit.</p>
<h2>The <code>autofocus</code> Attribute</h2>
<p>The <code>autofocus</code> attribute allows developers to choose which element has focus when the page is loaded.  The Google front page has done this via JavaScript, and now, 12 years later, there is finally an HTML attribute to easily handle this.</p>
<blockquote class="code">
<pre>&lt;input type="text" name="fullName" value=""
  required="required" autofocus="autofocus" /&gt;
</pre>
</blockquote>
<p>Safari, Chrome and Opera support it natively.  To make it work in other browsers, include the <code>webforms2.js</code> library, as we did in the <code>required</code> and  <code>pattern</code> examples.</p>
<p><a class="exampleLink" href="/tests/html5Widgets/autofocus.html">See an example of <code>autofocus</code> in action</a></p>
<h2>The <code>placeholder</code> Attribute</h2>
<p>A placeholder is a great visual cue to communicate any special information about a field (e.g. a description<br />
of the data to be input, if the field is required, etc). </p>
<p><div id="attachment_1317" class="wp-caption aligncenter" style="width: 397px"><a href="/tests/html5Widgets/placeholder.html"><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/07/placeholder.png" alt="An example of placeholder text.  The text disappears when the field has focus or if the user types information into the field." title="placeholder" width="387" height="94" class="size-full wp-image-1317" /></a><p class="wp-caption-text">An example of placeholder text.  The text disappears when the field has focus or if the user types information into the field.</p></div></p>
<p>Syntax is simple:</p>
<blockquote class="code">
<pre>&lt;input type="text" name="fullName" value="" required="required"
   placeholder="Required information" /&gt;
</pre>
</blockquote>
<p>Safari, Chrome and Firefox support this attribute natively.  To make it work in other browsers, it is necessary to load the html5Widgets library, with the necessary supporting libraries:</p>
<blockquote class="code">
<pre>&lt;script type="text/javascript"
   src="/path/to/shared/js/modernizr.com/modernizr-1.5.min.js"&gt;
&lt;/script&gt;

&lt;script type="text/javascript" src="/path/to/shared/js/EventHelpers.js"&gt;
&lt;/script&gt;

&lt;script type="text/javascript" src="/path/to/shared/js/html5Widgets.js"&gt;
&lt;/script&gt;
</pre>
</blockquote>
<p>What do these libraries do?</p>
<ul>
<li><a href="http://modernizr.com"><code>modernizr</code></a> is used to detect which HTML5 attributes and tags are supported by the users&#8217; browser</li>
<li><code>html5Widgets</code> is what actually does the placeholder logic, with <code>EventHelpers.js</code> providing cross-browser event-handling routines.</li>
</ul>
<p><a class="exampleLink" href="/tests/html5Widgets/placeholder.html">See an example of the HTML5 <code>placeholder</code><br />
tag in action.</a></p>
<h2>The <code>range</code> Input Type and <code>output</code> Tag</h2>
<p>Easily my favourite of the HTML5 widgets, <code>range</code> gives developers a sliding control to put inside their forms.  </p>
<p>The syntax is simple:</p>
<blockquote class="code">
<pre>&lt;input type="range"  name="rangeEl" value="" min="0" max="150" step="1" /&gt;
</pre>
</blockquote>
<p>The <code>min</code> and <code>max</code> attributes contain the minimum and maximum values, and <code>step</code> denotes by what increments the range slider increments by when moved.  Note that you can use these attributes with the <code>number</code> input type as well, but instead of having the fancy interface, it will use the validation engine to ensure the value follows what these attributes dictate.</p>
<p>At the time of this writing, Opera and WebKit based browsers (like Safari and Chrome), support it natively, and html5Widgets uses <a href="http://www.frequency-decoder.com/demo/slider-revisited/">the Frequency Decoder Slider Widget</a> to implement it in unsupported browsers.  To ensure cross-browser HTML5 range element goodness, place the following script tags in your document:</p>
<blockquote class="code">
<pre>&lt;!-- Needed for Range Element --&gt;
&lt;link type="text/css" rel="stylesheet" href="/path/to/shared/css/slider.css"&gt;

&lt;!--[if lte IE 6]&gt;
&lt;link type="text/css" rel="stylesheet" href="/path/to/shared/css/slider_ie.css" /&gt;
&lt;![endif]--&gt;

&lt;script type="text/javascript" src="/path/to/shared/js/frequency-decoder.com/slider.js"&gt;
&lt;/script&gt;

&lt;!-- Needed for Validation --&gt;
&lt;script type="text/javascript" src="/path/to/shared/js/weston.ruter.net/webforms2/webforms2_src.js"&gt;
&lt;/script&gt;

&lt;!-- Need for HTML5Widgets --&gt;
&lt;script type="text/javascript"
  src="/path/to/shared/js/modernizr.com/modernizr-1.5.min.js"&gt;
&lt;/script&gt;

&lt;script type="text/javascript" src="/path/to/shared/js/html5.js"&gt;
&lt;/script&gt;
&lt;script type="text/javascript" src="/path/to/shared/js/EventHelpers.js"&gt;
&lt;/script&gt;

&lt;script type="text/javascript" src="/path/to/shared/js/html5Widgets.js"&gt;
&lt;/script&gt;</pre>
</blockquote>
<p>Take a look at the screenshots below.  You will see that the way a <code>range</code> field varies among the browsers that natively support it, and even in some of the browsers that use html5Widgets:</p>
<table class="screenshots">
<thead>
<tr>
<th></th>
<th>Explorer 6.x+<br />
(html5Widgets support)</th>
<th>Firefox 3.5+<br />
(html5Widgets support)</th>
<th>Safari 4.0+<br />
(native support)</th>
<th>Chrome 3.0+<br />
(native support)</th>
<th>Opera 10.0+<br />
(native support)</th>
</tr>
</thead>
<tbody>
<tr>
<th>Windows</th>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/05/rangeExplorerWindows.png"><img title="Screenshot of range field for Internet Explorer 6" src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/rangeExplorerWindows.png" alt="Screenshot of range field for Internet Explorer 6" width="120" height="68" /></a></td>
<td><img title="Screenshot of range field for Windows Firefox" src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/rangeFirefoxWindows.png" alt="Screenshot of range field for Windows Firefox" width="120" height="68" /></td>
<td><img title="Screenshot of range field for Windows Safari" src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/rangeSafariWindows.png" alt="Screenshot of range field for Windows Safari " width="120" height="68" /></td>
<td><img title="Screenshot of range field for Chrome Windows" src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/rangeChromeWindows.png" alt="Screenshot of range field for Chrome Windows" width="120" height="68" /></td>
<td><img title="Screenshot of range field for Windows Opera" src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/rangeOperaWindows.png" alt="Screenshot of range field for Windows Opera" width="120" height="68" /></td>
</tr>
<tr>
<th>Mac</th>
<td>Not Applicable</td>
<td><img title="Screenshot of range field for Mac Firefox" src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/rangeFirefoxMac.png" alt="Screenshot of range field for Mac Firefox" width="120" height="68" /></td>
<td><img title="Screenshot of range field for Mac Safari" src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/rangeSafariMac.png" alt="Screenshot of range field for Mac Safari " width="120" height="68" /></td>
<td><img title="Screenshot of range field for Chrome Mac" src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/rangeChromeMac.png" alt="Screenshot of range field for Chrome Mac" width="120" height="68" /></td>
<td><img title="Screenshot of range field for Mac Opera" src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/rangeOperaMac.png" alt="Screenshot of range field for Mac Opera" width="120" height="68" /></td>
</tr>
<tr>
<th>Linux</th>
<td>Not Applicable</td>
<td><img title="Screenshot of range field for Linux Firefox" src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/rangeFirefoxUbuntu.png" alt="Screenshot of range field for Linux Firefox" width="120" height="68" /></td>
<td>Not Applicable</td>
<td><img title="Screenshot of range field for Linux Chrome" src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/rangeChromeUbuntu.png" alt="Screenshot of range field for Linux Chrome" width="120" height="68" /></td>
<td><img title="Screenshot of range field for Linux Opera" src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/rangeOperaUbuntu.png" alt="Screenshot of range field for Linux Opera" width="120" height="68" /></td>
</tr>
</tbody>
</table>
<p>The <code>output</code> tag can be used to show the value of a field, or the result of an operation performed on a number of fields, using JavaScript expressions.  Although the <code>output</code> tag may calculate fomulas referencing <em>any</em> form fields, it is useful especially for the <code>range</code> input type so users can see what value the range element is pointing to:</p>
<blockquote class="code">
<pre><span class="hilite">&lt;output onforminput="this.value = rangeEl.value"&gt;-&lt;/output&gt;</span>
&lt;input type="range"  name="rangeEl" value="" min="0" max="150" step="1" /&gt;
</pre>
</blockquote>
<p>The contents of the <code>output</code> tag is the default value.  Note the <code>this.value</code> syntax &#8211; I am not sure why the W3C HTML5 working group decided it was needed (why not just have the formula?), but it is.  If there are other types of expressions supported in the final specification, they are not supported by <code>html5Widgets</code> at this time.  Note that in order to apply CSS to the <code>output</code> tag in IE, it is necessary to use <a href="http://remysharp.com/2009/01/07/html5-enabling-script/">Remy Sharp&#8217;s HTML5 Enabling Script</a>.</p>
<p><a class="exampleLink" href="/tests/html5Widgets/range.html">See an example of the <code>range</code> input being used with the <code>output</code> tag.</a></p>
<p><a class="exampleLink" href="/tests/html5Widgets/output.html">See an example of the <code>output</code> tag being used <em>without</em> a <code>range</code> field.</a></p>
<div class="importantNotes">
<h3>Update (May 12. 2011):</h3>
<p>Since this article was written, the <code>onforminput</code> event has been deprecated in favor of the <code>oninput</code>.  I have updated html5Widgets to support <code>oninput</code>, and <a href="http://www.useragentman.com/blog/2011/05/12/fixing-oninput-in-ie9-using-html5widgets/">have written article about its <code>oninput</code> support</a>.  I encourage you to see the different between the above to examples and the <code>oninput</code> method of <a href="http://www.useragentman.com/examples/html5Widgets/tests/html5Widgets/range.html">using the <code>output</code> tag with <code>range</code></a> and <a href="http://www.useragentman.com/examples/html5Widgets/tests/html5Widgets/output.html">without <code>range</code></a>
</div>
<h2>The <code>datetime</code>, <code>datetime-local</code>, <code>date</code>, <code>week</code> and <code>week</code> Input Types</h2>
<p>At the time of this writing, Opera is the only desktop browser that supports HTML5 date fields.  To support the other browsers, html5Widgets uses a slightly modified version of <a href="http://www.dynarch.com/">DynArch.com</a>&#8216;s <a href="http://www.dynarch.com/projects/calendar/old/">Ex-&#8221;Coolest&#8221; DHTML Calendar</a> (I decided not to use <a href="http://www.dynarch.com/projects/calendar/">the coolest one</a> because the Ex-Coolest has a more permissive license and it works really well). Now all browsers can support the <code>datetime</code>, <code>datetime-local</code>, <code>date</code>, <code>month</code> and <code>week</code> input fields, and submit these values in a consistent format.</p>
<p>Here are the scripts needed for the browsers that need it:</p>
<blockquote class="code">
<pre>
&lt;!-- Needed for Date/Time Elements --&gt;
&lt;link type="text/css" rel="stylesheet" href="/path/to/shared/js/jscalendar-1.0/calendar-win2k-1.css" /&gt;
&lt;script type="text/javascript" src="/path/to/shared/js/jscalendar-1.0/calendar.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="/path/to/shared/js/jscalendar-1.0/lang/calendar-en.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="/path/to/shared/js/jscalendar-1.0/calendar-setup.js"&gt;&lt;/script&gt;

&lt;!-- Needed for Validation --&gt;
&lt;script type="text/javascript" src="/path/to/shared/js/weston.ruter.net/webforms2/webforms2_src.js"&gt;
&lt;/script&gt;

&lt;!-- What glues all the above together --&gt;

&lt;script type="text/javascript" src="/path/to/shared/js/modernizr.com/modernizr-1.5.min.js"&gt;
&lt;/script&gt;
&lt;script type="text/javascript" src="/path/to/shared/js/EventHelpers.js"&gt;
&lt;/script&gt;
&lt;script type="text/javascript" src="/path/to/shared/js/html5Widgets.js"&gt;
&lt;/script&gt;
</pre>
</blockquote>
<p>Note that the calendar widget uses language packs and that I am using the English one (<code>calendar-en.js</code>.  Other language packs are included.</p>
<p>Below is a comparison between Opera&#8217;s native date widget vs. the one provided by the DynArch/HTML5Widget combo:</p>
<table class="screenshots">
<thead>
<tr>
<th></th>
<th>DateTime Widget</th>
<th>Month Widget</th>
</tr>
<tr>
<th>Opera Windows</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/dateTimeOperaWindows.png" /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/monthOperaWindows.png" /></td>
</tr>
<tr>
<th>Opera Mac</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/dateTimeOperaMac.png" /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/monthOperaMac.png" /></td>
</tr>
<tr>
<th>Firefox 3.5+ Windows<br />
(html5Widgets support)</th>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/datetimeFirefoxWithHTML5Widgets.png" /></td>
<td><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/05/monthFirefoxWithHTML5Widgets.png" /></td>
</tr>
</table>
<p>It looks like the native calendar for Opera for Mac is a smaller than the Windows version &#8211; hopefully this is just on my copy of the browser.</p>
<p>The display formats, and they values that they submit to the server, are pretty much the same</p>
<table class="screenshots">
<thead>
<tr>
<th>Input type</th>
<th>Format displayed on page</th>
<th>Format sent to server</th>
</tr>
</thead>
<tbody>
<tr>
<td>datetime</td>
<td><em>yyyy-mm-dd HH:MM</em></td>
<td><em>yyyy-mm-dd</em>T<em>HH:MM</em>Z</td>
</tr>
<tr>
<td>datetime-local</td>
<td><em>yyyy-mm-dd HH:MM</em></td>
<td><em>yyyy-mm-dd</em>T<em>HH:MM</em></td>
</tr>
<tr>
<td>date</td>
<td><em>yyyy-mm-dd</em></td>
<td><em>yyyy-mm-dd</em></td>
</tr>
<tr>
<td>month</td>
<td><em>yyyy-mm</em></td>
<td><em>yyyy-mm</em></td>
</tr>
<tr>
<td>week</td>
<td><em>yyyy-mm</em>W</td>
<td><em>yyyy-mm</em>W</td>
</tr>
</tbody>
</table>
<p><a class="exampleLink" href="/tests/html5Widgets/dateTime.html">See the HTML5 <code>date</code> widgets in action.</a></p>
<p><strong>Note:</strong> In this example, The Opera Mobile Windows Emulator incorrectly displays the <code>datetime</code> and <code>datetime-local</code> calendars in the upper left-hand corner of the screen, but <strong>not</strong> the other ones.  Since this is Opera&#8217;s own calendar widget, and not html5Widgets&#8217;, this bug will have to fixed by Opera.  </p>
<p><div id="attachment_1269" class="wp-caption aligncenter" style="width: 501px"><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/07/operaMobileDateTimeBug.png"><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/07/operaMobileDateTimeBug.png" alt="Screenshot of Opera Mobile bug" title="operaMobileDateTimeBug" width="491" height="768" class="size-full wp-image-1269" /></a><p class="wp-caption-text">Screenshot of Opera Mobile bug</p></div></p>
<h2>The <code>color</code> Input Type</h2>
<p>Currently <strong>none</strong> of the desktop browser manufacturers support the <code>color</code> input type (although the new WebKit based <a href="http://devblog.blackberry.com/2010/03/use-html5-in-your-blackberry-web-content">Blackberry browser seems to have a neat implementation</a>). While we wait to see how the major browser manufacturers decide to implement <code>color</code>, you can use html5Widgets&#8217; implementation which uses <a href="http://www.softwareishard.com">Jan Odvarko</a>&#8216;s <a href="http://jscolor.com">jscolor</a>.  The script has been configured to allow only allow lower case hexadecimal rgb values to be entered, and that a blank value not be allowed, as per the W3C spec.</p>
<p>Here are the script tags needed to implement this in all browsers:</p>
<blockquote class="code"><pre>&lt;!-- Needed for Color Element --&gt;
&lt;script type="text/javascript" src="../../shared/js/jscolor/jscolor.js"&gt;
&lt;/script&gt;

&lt;!-- What glues all the above together --&gt;
&lt;script type="text/javascript" src="../../shared/js/modernizr.com/modernizr-1.5.min.js"&gt;
&lt;/script&gt;

&lt;script type="text/javascript" src="../../shared/js/EventHelpers.js"&gt;
&lt;/script&gt;

&lt;script type="text/javascript" src="../../shared/js/html5Widgets.js"&gt;
&lt;/script&gt;
</pre>
</blockquote>
<p>Below is a comparison between the Blackberry&#8217;s implementation (grabbed from <a href="http://devblog.blackberry.com/">the Blackberry Development Blog</a>) and HTML5Widget/jscolor&#8217;s. </p>
<table class="screenshot">
<thead>
<tr>
<th>Blackberry Web Browser</th>
<th>Firefox 3.5 with html5Widgets.js</th>
</thead>
<tbody>
<tr>
<td>
<a href="http://www.useragentman.com/blog/wp-content/uploads/2010/07/html5-color-input.jpg"><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/07/html5-color-input.jpg" alt="HTML5 Color form field, Blackberry" title="html5-color-input"  class="alignnone size-full wp-image-1292" /></a>
</td>
<td><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/07/jscolor.png"><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/07/jscolor.png" alt="HTML5 color input field with html5Widgets and jscolor." title="jscolor"  class="alignnone size-full wp-image-1291" /></a>
</td>
</tr>
</tbody>
</table>
<p><a class="exampleLink" href="/tests/html5Widgets/color.html">See HTML5Widget&#8217;s implementation of the<br />
<code>color</code> input type.</a></p>
<p><strong>Note:</strong> Like other HTML syntax, <code>color</code> uses the unfortunate American spelling instead of the Queen&#8217;s Proper English (i.e. colour). Ensure you spell it incorrectly in order to make this work.  ;-)</p>
<h2>How Well Do These Libraries Handle the Official Specification?</h2>
<p>I have done some testing on some existing examples that use HTML5 forms to ensure that it works the way developers expect.  Here are a few examples that I have taken, with the necessary JavaScript added:</p>
<ul>
<li><a href="/tests/html5Widgets/24ways.org/index.html"><strong>A cool looking payment form</strong></a> by <a href="http://yaili.com/">Inayaili de León</a> (originally from<a href="http://24ways.org/2009/have-a-field-day-with-html5-forms"> her HTML5 forms tutorial</a>)</li>
<li><a href="/tests/html5Widgets/bradshawenterprises.com/index.html"><strong>An HTML5 Form Demo</strong></a> by <a href="http://www.bradshawenterprises.com/">Richard Bradshaw</a> (originally from his <a href="http://www.bradshawenterprises.com/blog/2010/fancy-forms-html5-css3-js/"><em>Fancy Forms: HTML5 + CSS3 – JS</em></a> article)</li>
<li><a href="/tests/html5Widgets/dev.opera.com"><strong>Another cool Example</strong></a> by <a href="http://annevankesteren.nl/">Anne van Kesteren</a> (originally from his article <a href="http://dev.opera.com/articles/view/improve-your-forms-using-html5/"><em>Improve your forms using HTML5!</em></a> on the <a href="http://dev.opera.com">The Opera Developer Community Website</a>).</li>
</ul>
<p>However, the HTML5 Forms specification is large, and unfortunately, the <code>webforms2</code> and <code>html5Widgets</code> libraries don&#8217;t implement everything &#8230; <strong>yet!!!!</strong>  Since I am really motivated with what HTML5 forms can do, I am committed to completing support to most, if not all, of the HTML5 Forms specification.  The version of <code>webforms2</code> in this blog post has been modified from Weston Ruter&#8217;s original verison, but it will be merged with the official Google Code version in the next week or so and will include compressed versions of all the library. The <code>html5Widgets</code> library will also have a permanent home there.</p>
<p>Things that I will be working on in the near future are:</p>
<ul>
<li><strong>Support for other HTML5 form elements</strong>, among other things, <code>datalist</code>, <code>number</code>, <code>keygen</code>, <code>progress</code>, <code>time</code> and <code>meter</code></li>
<li><strong>Support for CSS styling of HTML5 form widgets</strong> as well as the ability to style form fields according to their validation state (e.g. <code>:valid</code> and <code>:invalid</code> psudo-classes).  Opera seems to be the only browser currently supporting this.</li>
<li><strong>Default styling for some of the new input types</strong>, like <code>tel</code>, <code>email</code>, <code>url</code>.  Opera for Mac and Opera Mobile are the only browsers I know of that support this for <code>email</code> and <code>url</code>.
<p><div id="attachment_1298" class="wp-caption aligncenter" style="width: 340px"><a href="http://www.useragentman.com/blog/wp-content/uploads/2010/07/operaMacURLandEmail.png"><img src="http://www.useragentman.com/blog/wp-content/uploads/2010/07/operaMacURLandEmail.png" alt="email and url form fields in Opera Mac" title="" width="330" height="156" class="size-full wp-image-1298" /></a><p class="wp-caption-text"><code>email</code> and <code>url</code> form fields in Opera Mac</p></div></li>
<li><strong>Support for customizing the validation look and feel.</strong>  This is one I would love to do, since I&#8217;m sure a lot of developers would want to change how validation errors appear on the screen.  Unfortunately, the HTML5 specification doesn&#8217;t describe a standard way of doing this.  It will be interesting to see how the browser manufacturers deal with this issue (<em><strong>To any browser developer reading this is working on the display form validation errors:</strong> I would love to know how you are doing this if you are willing to share.  <strong>Hint, HINT! ;-)</strong></em>)</li>
<li><strong>In Internet Explorer 7 and lower, the ability to style input types it doesn&#8217;t support natively</strong> with CSS code like <code>input[type="range"]</code></li>
<li><strong>Enabling HTML5 forms validation on the server side</strong> to ensure data integrity for browser that don&#8217;t support HTML5 forms that have JavaScript disabled.</li>
<li><strong>Support for internationalization of the error messages used in <code>webforms2</code>.</strong>  If anyone wants to help in the translation of these error messages, please drop me a line &mdash; I need help!</li>
</ul>
<p>Below is a modified version of the Wikipedia&#8217;s HTML5 forms compatibility table and see what how things the support situation stands today.</p>
<table class="screenshots">
<thead>
<tr>
<th></th>
<th>Trident</th>
<th>Gecko</th>
<th>WebKit</th>
<th>Presto</th>
</tr>
</thead>
<tbody>
<tr>
<th colspan="5">Attributes</th>
</tr>
<tr>
<th><code>autocomplete</code></th>
<td class="table-yes">Yes</td>
<td class="table-yes">Yes</td>
<td class="table-yes">Yes</td>
<td class="table-yes">2.0</td>
</tr>
<tr>
<th><code>list</code></th>
<td class="table-no">No</td>
<td class="table-no">No</td>
<td class="table-no">No</td>
<td class="table-yes">2.0</td>
</tr>
<tr>
<th><code>required</code></th>
<td class="table-yesWithWebforms2">Yes (with webforms2)</td>
<td class="table-yesWithWebforms2">Yes (with webforms2)</td>
<td class="table-yesWithWebforms2">Yes (with webforms2)</td>
<td class="table-yes">2.0</td>
</tr>
<tr>
<th><code>multiple</code></th>
<td class="table-no">No</td>
<td class="table-yes">1.9.2</td>
<td class="table-yes">526</td>
<td class="table-no">No</td>
</tr>
<tr>
<th><code>pattern</code></th>
<td class="table-yesWithWebforms2">Yes (with webforms2)</td>
<td class="table-yesWithWebforms2">Yes (with webforms2)</td>
<td class="nightly table-yesWithWebforms2 table-nightly">Yes (with webforms2, and natively in Nightly build)</td>
<td class="table-yes">2.0</td>
</tr>
<tr>
<th><code>min</code>, <code>max</code></th>
<td class="table-yesWithWebforms2">Yes (with webforms2)</td>
<td class="table-yesWithWebforms2">Yes (with webforms2)</td>
<td class="table-yesWithWebforms2">Yes (with webforms2)</td>
<td class="table-yes">2.0</td>
</tr>
<tr>
<th><code>step</code></th>
<td class="table-yesWithWebforms2">Yes (with webforms2)</td>
<td class="table-yesWithWebforms2">Yes (with webforms2)</td>
<td class="nightly table-nightly table-yesWithWebforms2">Yes (with webforms2 and Nightly build natively)</td>
<td class="table-yes">2.0</td>
</tr>
<tr>
<th><code>placeholder</code></th>
<td class="table-yesWithWebforms2">Yes (with webforms2)</td>
<td class="nightly table-nightly">1.9.3</td>
<td class="table-yes">Yes</td>
<td class="table-yesWithWebforms2">Yes (with webforms2)</td>
</tr>
<tr>
<th><code>form</code></th>
<td class="table-no">No</td>
<td class="nightly table-nightly">1.9.3</td>
<td class="table-no">No</td>
<td class="table-yes">2.0</td>
</tr>
<tr>
<th><code>autofocus</code></th>
<td class="table-yesWithhtml5Widgets">Yes (with html5Widgets)</td>
<td class="nightly table-nightly">1.9.3</td>
<td class="nightly table-nightly table-yesWithhtml5Widgets">Yes (with html5Widgets and Nightly build natively)</td>
<td class="table-yes">2.0</td>
</tr>
<tr>
<th><code>maxlength</code></th>
<td class="table-no">No</td>
<td class="nightly table-nightly">1.9.3</td>
<td class="nightly table-nightly">Nightly build</td>
<td class="table-yes">2.0</td>
</tr>
<tr>
<th><code>novalidate</code></th>
<td class="table-no">No</td>
<td class="table-no">No</td>
<td class="nightly table-nightly">Nightly build</td>
<td class="table-no">No</td>
</tr>
<tr>
<th><code>control</code></th>
<td class="table-no">No</td>
<td class="nightly table-nightly">1.9.3</td>
<td class="table-no">No</td>
<td class="table-no">No</td>
</tr>
<tr>
<th><code>accept</code></th>
<td class="table-no">No</td>
<td class="nightly table-nightly">1.9.3</td>
<td class="table-no">No</td>
<td class="table-no">No</td>
</tr>
<tr>
<th><code>formtarget</code></th>
<td class="table-no">No</td>
<td class="table-no">No</td>
<td class="table-no">No</td>
<td class="table-no">No</td>
</tr>
<tr>
<th><code>formaction</code></th>
<td class="table-no">No</td>
<td class="table-no">No</td>
<td class="table-no">No</td>
<td class="table-no">No</td>
</tr>
<tr>
<th colspan="5">Elements</th>
</tr>
<tr>
<th><code>datalist</code></th>
<td class="table-no">No</td>
<td class="table-no">No</td>
<td class="table-no">No</td>
<td class="table-yes">2.0</td>
</tr>
<tr>
<th><code>keygen</code></th>
<td class="table-no">No</td>
<td class="incorrect">1.0</td>
<td class="table-yes">125</td>
<td class="table-yes">1.0</td>
</tr>
<tr>
<th><code>output</code></th>
<td class="table-no">No</td>
<td class="nightly table-nightly">1.9.3</td>
<td class="table-no">No</td>
<td class="table-yes">2.0</td>
</tr>
<tr>
<th><code>progress</code></th>
<td class="table-no">No</td>
<td class="table-no">No</td>
<td class="nightly table-nightly">Nightly build</td>
<td class="table-no">No</td>
</tr>
<tr>
<th><code>meter</code></th>
<td class="table-no">No</td>
<td class="table-no">No</td>
<td class="table-no">No</td>
<td class="table-no">No</td>
</tr>
<tr>
<th colspan="5">Input types</th>
</tr>
<tr>
<th><code>search</code></th>
<td class="table-no">No</td>
<td class="nightly table-nightly">1.9.3</td>
<td class="table-yes">312</td>
<td class="table-no">No</td>
</tr>
<tr>
<th><code>tel</code></th>
<td class="table-no">No</td>
<td class="nightly table-nightly">1.9.3</td>
<td class="nightly table-nightly">Nightly build</td>
<td class="table-no">No</td>
</tr>
<tr>
<th><code>url</code></th>
<td class="table-no">No</td>
<td class="table-no">No</td>
<td class="nightly table-nightly">Nightly build</td>
<td class="table-yes">2.0</td>
</tr>
<tr>
<th><code>email</code></th>
<td class="table-no">No</td>
<td class="table-no">No</td>
<td class="nightly table-nightly">Nightly build</td>
<td class="table-yes">2.0</td>
</tr>
<tr>
<th><code>datetime</code></th>
<td class="table-yesWithhtml5Widgets" rowspan="6">Yes (with html5Widgets)</td>
<td class="table-yesWithhtml5Widgets" rowspan="6">Yes (with html5Widgets)</td>
<td class="nightly table-nightly table-yesWithhtml5Widgets" rowspan="6">Yes (with html5Widgets and Nightly build natively)</td>
<td class="table-yes" rowspan="6">2.0</td>
</tr>
<tr>
<th><code>date</code></th>
</tr>
<tr>
<th><code>month</code></th>
</tr>
<tr>
<th><code>week</code></th>
</tr>
<tr>
<th><code>time</code></th>
</tr>
<tr>
<th><code>datetime-local</code></th>
</tr>
<tr>
<th><code>number</code></th>
<td class="table-no">No</td>
<td class="table-no">No</td>
<td class="nightly table-nightly">Nightly build</td>
<td class="table-yes">2.0</td>
</tr>
<tr>
<th><code>range</code></th>
<td class="table-yesWithhtml5Widgets">Yes (with html5Widgets)</td>
<td class="table-yesWithhtml5Widgets">Yes (with html5Widgets)</td>
<td class="table-yes">Yes</td>
<td class="table-yes">2.0</td>
</tr>
<tr>
<th><code>color</code></th>
<td class="table-yesWithhtml5Widgets">Yes (with html5Widgets)</td>
<td class="table-yesWithhtml5Widgets">Yes (with html5Widgets)</td>
<td class="nightly table-nightly table-yesWithhtml5Widgets">Yes (with html5Widgets and Nightly build natively)</td>
<td class="table-yesWithhtml5Widgets">Yes (with html5Widgets)</td>
</tr>
</tbody>
</table>
<h2>Integrating With <code>visibleIf</code> To Make Even <strong>Cooler</strong> Looking Forms</h2>
<p>The <code>webforms2</code> and <code>html5Widgets</code> libraries are designed to co-exist well with <code><a href="http://www.useragentman.com/blog/2010/06/20/visibleif-html5-custom-data-attributes-with-javascript-make-dynamic-interactive-forms/">visibleIf</a></code> to create interactive forms with fields that only validate the ones that visibleIf is displaying.</p>
<p><a class="exampleLink" href="/tests/html5Widgets/patternRequiredWithVisibleIf.html">See an example of HTML5 validation working with the <code>visibleIf</code> JavaScript library</a></p>
<h2>Acknowledgments, Shout-outs and Kudos</h2>
<ul>
<li><strong><a href="http://farukat.es/">Faruk Ates</a> and <a href="http://paulirish.com/">Paul Irish</a></strong> for <em>the</em> definitive HTML5 and CSS3 feature detector, <a href="http://www.modernizr.com/">modernizr</a>.</li>
<li><strong><a href="http://weston.ruter.net/">Weston Ruter</a></strong> for doing the insane amount of work on <a href="https://github.com/westonruter/webforms2">webforms2</a> <em>years ago</em>.</li>
<li><strong><a href="http://www.frequency-decoder.com/">Brian McAllister:</a></strong> for making the excellent <a href="http://www.frequency-decoder.com/2008/08/01/unobtrusive-slider-control-v2">Unobtrusive slider control</a> which I used for the range element.</li>
<li><strong><a href="http://www.dynarch.com/">Mihai Bazon</a></strong> for keeping the <a href="http://www.dynarch.com/projects/calendar/old/">The Ex-“Coolest” DHTML Calendar</a> around even though he has a paid product.</li>
<li><strong><a href="http://www.softwareishard.com">Jan Odvárko</a></strong> for his rocking <a href="http://jscolor.com/">jscolor</a> widget.</li>
<li><strong><a href="http://remysharp.com/">Remy Sharp</a></strong> for his fine <a href="<a href="http://remysharp.com/2009/01/07/html5-enabling-script/">HTML5 Enabling Script</a> so we can force IE to style things like the <code>output</code> tag.</li>
<li><strong><a href="http://yaili.com/">Inayaili de Leon</a>, <a href="http://annevankesteren.nl/">Anne van Kesteren</a> and <a href="http://www.bradshawenterprises.com/">Richard Bradshaw</a></strong> for allowing me to show how easy it is to make their original HTML5 Form demos work with html5Widgets.</li>
</ul>
<h2>Further Reading</h2>
<ul>
<li><strong><a href="http://www.w3.org/TR/html5/forms.htm">The W3C HTML5 Forms Specification Working Draft:</a></strong> The Working Draft is in &#8220;Last call for comments&#8221; status, so there may be some last minute changes.</li>
<li><strong><a href="http://diveintohtml5.org/forms.html">A Form of Madness:</a></strong> Chapter 10 of Mark Pilgrim&#8217;s great reference on HTML5.</li>
<li><strong><a href="http://www.whatwg.org/specs/web-forms/current-work/">Web Forms 2.0 Working Draft:</a></strong> The WHATWG spec that the W3C spec is based on.  It includes the repetition model which didn&#8217;t make it into the W3C spec</li>
</ul>
<h2>Download</h2>
<p><del datetime="2011-05-12T23:52:56+00:00">This archive is the temporary home for <code>html5Widgets</code>.  It will soon be hosted at the <a href="http://code.google.com/p/webforms2/"><code>webforms2</code> Google Code page</a>.</del>  Both <a href="https://github.com/zoltan-dulac/html5Widgets">html5Widgets</a> and <a href="https://github.com/westonruter/webforms2">webforms2</a> both have permanent homes at github (however, the html5Widgets github page contains a copy of webforms2 in it for your convenience).  Weston Ruter has been kind to give me deciding rights over the destiny of this code.  I <em>will</em> be updating webforms2 in the near future.</p>
<p><a class="exampleLink" href="https://github.com/zoltan-dulac/html5Widgets">Download the latest version of html5Widgets, which includes webforms2, from github.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2010/07/27/cross-browser-html5-forms-using-modernizr-webforms2-and-html5widgets/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>visibleIf – Dynamic and Complex Interactive Forms Using HTML5 Custom Data Attributes</title>
		<link>http://www.useragentman.com/blog/2010/06/20/visibleif-html5-custom-data-attributes-with-javascript-make-dynamic-interactive-forms/</link>
		<comments>http://www.useragentman.com/blog/2010/06/20/visibleif-html5-custom-data-attributes-with-javascript-make-dynamic-interactive-forms/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 00:49:36 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[Custom Data Attributes]]></category>
		<category><![CDATA[Forms]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=1251</guid>
		<description><![CDATA[One thing HTML5 forms can't do is dynamically show and hide form elements according to the data that the user has already entered.  My new library, <code>visibleIf</code> gives you a very easy way to do this using the HTML5 <code>data-</code> attributes.]]></description>
			<content:encoded><![CDATA[<div class="importantNotes">
<h3>Update (July 28, 2010):</h3>
<p>I have fixed a minor bug in this script to ensure compatibility with webforms2, part of <a href="http://www.useragentman.com/blog/2010/07/27/cross-browser-html5-forms-using-modernizr-webforms2-and-html5widgets/">a suite of scripts that implement cross-browser compatibility with the HTML5 forms specification</a>.</p>
</div>
<p>I have been following the progression of the HTML5 Forms standard with great interest.  It promises a whole slew of new visual and usability goodness to existing HTML forms (e.g. slider and calendar widgets), a client-side validation framework, and so much more.  One of the things I was disappointed with, though, was that <strong>HTML5 cannot hide and show form elements depending on what the user has input into the form so far.</strong>  To give a very simple example of what I am talking about, select a country of origin in the form below:</p>
<form>
<table class="formTable">
<tbody>
<tr>
<th>Country of Origin: </th>
<td>
<select name="country">
<option> Select One&#8230; </option>
<option value="Canada"> Canada </option>
<option value="United States"> United States </option>
</select>
</td>
</tr>
<tr class="visibleIf" data-visibleif-rule=" country == 'Canada'">
<th>Postal Code: </th>
<td>
<input name="postalCode" type="text" /></td>
</tr>
<tr class="visibleIf" data-visibleif-rule=" country == 'United States'">
<th>Zip Code: </th>
<td>
<input name="zipCode" type="text" /></td>
</tr>
</tbody>
</table>
</form>
<p>In HTML5, there is no way for a developer to tell the browser <em>&#8220;only show me this block of HTML if </em><code>country == 'Canada'</code><em>&#8220;</em>.  This is not a criticism of HTML5 or its authors &mdash; HTML5 forms has a lot of awesomeness in it (some of which I will blog about in my next article) and we can&#8217;t expect it to do everything. However, I have been using custom JavaScript to do this kind of stuff all the time, and wouldn&#8217;t it be great if the JavaScript challenged could do so?  In the ideal world graphic designers, J2EE programmers with minimal front-end experience, and even my 75-year-old mom should be able to do this.</p>
<p>As a result, I present a JavaScript library, <code>visibleIf</code>, which allows web developers to use <a href="http://ejohn.org/blog/html-5-data-attributes/">HTML5 custom data attributes</a> to define when blocks of HTML should be visible or invisible.  <strong>Even if you are not a JavaScript genius, you will be able to make cool looking dynamic forms with very simple markup.</strong>  For example, let&#8217;s take a look at the HTML of the example above:</p>
<blockquote class="code">
<pre>&lt;form&gt;
    &lt;table class="formTable"&gt;
        &lt;tr&gt;
            &lt;th&gt;Country of Origin&lt;/th&gt;

            &lt;td&gt;&lt;select name="country"&gt;
                &lt;option value=""&gt;
                    Select One...
                &lt;/option&gt;

                &lt;option value="Canada"&gt;
                    Canada
                &lt;/option&gt;

                &lt;option value="United States"&gt;
                    United States
                &lt;/option&gt;
            &lt;/select&gt; &lt;/td&gt;
        &lt;/tr&gt;

        &lt;tr <span class="hilite">class="visibleIf" data-visibleif-rule="country == 'Canada'"</span>&gt;
            &lt;th&gt;Postal Code&lt;/th&gt;

            &lt;td&gt;&lt;input type="text" name="postalCode"
            value="" /&gt; &lt;/td&gt;
        &lt;/tr&gt;

        &lt;tr <span class="hilite">class="visibleIf" data-visibleif-rule="country == 'United States'"</span>&gt;
            &lt;th&gt;Zip Code&lt;/th&gt;

            &lt;td&gt;&lt;input type="text" name="zipCode"
            value="" /&gt; &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/table&gt;
&lt;/form&gt;
</pre>
</blockquote>
<p>This example is rather simple: the Postal Code form field is shown when <code>country == 'Canada'</code> and Zip Code is shown when <code>country == 'United States'</code>.  Note that in order to hide and show a block of HTML, you must set two attributes inside the containing tag:</p>
<ul>
<li><strong><code>class="visibleIf"</code>:</strong> which lets the visibleIf.js script know that this block of HTML is to be hidden or shown according to the <code>data-visibleif-rule</code></li>
<li><strong><code>data-visibleif-rule='<em>rule expression'</em></code>:</strong> which contains the visibility rule.  The rule variables are form element names, like <code>country</code> is in the above example. You can do simple equality rules like this, or complex ones like:<br />
<blockquote class="code"><pre>
&lt;div class="visibleIf"
   data-visibleif-rule="country == 'Canada' &#038;&#038; (city == 'Toronto' || city == 'Vancouver')"&gt;
  ...
&lt;/div&gt;
</pre>
</blockquote>
<p>If you know a little bit of JavaScript, you will note that you can use JavaScript functions to do interesting rules like:</p>
<blockquote class="code"><pre>
&lt;div class="visibleIf" data-visibleif-rule="parseInt(total) &gt; 100"&gt;
  ...
&lt;/div&gt;
</pre>
</blockquote>
</li>
</ul>
<p>Note that the above examples have visibleIf rules inside a <code>tr</code> tag, <strong>they can be used in any tag inside an HTML form</strong>. </p>
<p>Although the above example is quite simple, you can use visibleIf to make really complex forms &mdash; you can even put visibleIf blocks inside of visibleIf blocks.  To illustrate this I have created <a href="/tests/visibleIf/example1.html">a more interesting form</a> that shows what can be done using visibleIf.</p>
<p><a href="/tests/visibleIf/example1.html" class="exampleLink">See a more complex visibleIf form example</a></p>
<h2>Hidden Fields &#8211; To Clear Or Not To Clear</h2>
<p>If the user does something that hides some form fields (e.g. clicks on a checkbox) and then reverses that action (in this case clicking on the same checkbox), the fields will reappear with any previously filled-in data erased. Developers can override this default behavior by setting the class of the fields that shouldn&#8217;t be erased to <code>visibleIf-doNotReset</code>.  To illustrate this, I have created a page with two identical forms on it &mdash; one that erases the values inside hidden fields, and one that doesn&#8217;t.  Play with the example and you&#8217;ll see what I mean.</p>
<p><a class="exampleLink" href="/tests/visibleIf/example2-dotNotReset.html">See an example of a form with and without <code>visibleIf-doNotReset</code> fields.</a></p>
<h2>Hidden Fields &#8211; Submit To The Server, Or Not?</h2>
<p>Also note that by default, only the visible form elements are submitted to the server (this is because when a form element is hidden by <code>visibleIf</code>, its <code>disabled</code> attribute is set to <code>true</code> when the form is submitted).  If developers wants to override this behavior, they can simply set the form&#8217;s <code>class</code> to <code>visibleIf-submitInvisibleData</code>.</p>
<p><a  class="exampleLink"  href="/tests/visibleIf/example2-submitInvisibleData.html">See an example of a form with and without <code>visibleIf-submitInvisibleData</code> class set.</a></p>
<h2>The Quick &#8220;How-To&#8221; Guide&#8221;</h2>
<p>Setting up your existing forms to use visibleIf is a quick, three-step process:</p>
<ol>
<li>Download the source from the visibleIf Project Page</li>
<li>Include the visibleIf.css stylesheet, EventHelpers.js and visibleIf.js JavaScript files into your web page:<br />
<blockquote class="code">
<pre>&lt;link rel="stylesheet" href="/path/to/visibleIf.css" type="text/css" /&gt;

&lt;script type="text/javascript" src="/path/to/EventHelpers.js"&gt;
&lt;/script&gt;
&lt;script type="text/javascript" src="/path/to/visibleIf.js"&gt;
&lt;/script&gt;
</pre>
</blockquote>
</li>
<li>Use the visibleIf rules as seen in the example above.  Note that <code>&amp;&amp;</code>, <code>||</code> and other logical operators are allowed in the expressions, as are JavaScript functions (e.g. parseInt, parseFloat, etc).</li>
</ol>
<h2>A Note On The Use Of Custom Data Attributes</h2>
<p>Why did I bother with custom data attributes when making <code>visibleIf</code>?  Why did I just use the attribute <code>data-visibleif-rule</code> instead of just <code>visibleif-rule</code> or just <code>rule</code> to place the visibility rule? Wouldn&#8217;t that be simpler?</p>
<ul>
<li><strong><code>data-</code> attributes are HTML compliant: </strong>.  Their use ensure that <code>visibleIf</code> will be compatible with future versions of HTML since <strong><code>data-</code> attributes are reserved to be used for JavaScript libraries</strong>.  As long as you use the <a href="http://dev.w3.org/html5/spec/syntax.html#the-doctype">HTML5 DOCTYPE</a>, your forms should also validate.</li>
<li><strong>The <code>data-visibleif-</code> prefix will not collide with attributes used in other JavaScript libraries. </strong> There is a high chance that <code>data-rule</code> could be used by another library.  Using the name of the library in the attribute significantly reduces that possibility.
</ul>
<h2>Download</h2>
<p><a href="/downloads/visibleIf-2.1.zip" class="exampleLink"><code>visibleIf.js</code> v. 2.1 and example code (zip format)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2010/06/20/visibleif-html5-custom-data-attributes-with-javascript-make-dynamic-interactive-forms/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Cross Browser HTML5 Drag and Drop</title>
		<link>http://www.useragentman.com/blog/2010/01/10/cross-browser-html5-drag-and-drop/</link>
		<comments>http://www.useragentman.com/blog/2010/01/10/cross-browser-html5-drag-and-drop/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 03:52:17 +0000</pubDate>
		<dc:creator>zoltan</dc:creator>
				<category><![CDATA[Drag and Drop]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[drag]]></category>
		<category><![CDATA[dragend]]></category>
		<category><![CDATA[dragenter]]></category>
		<category><![CDATA[draggable]]></category>
		<category><![CDATA[dragleave]]></category>
		<category><![CDATA[dragover]]></category>
		<category><![CDATA[dragstart]]></category>
		<category><![CDATA[drop]]></category>
		<category><![CDATA[dropEffect]]></category>
		<category><![CDATA[effectAllowed]]></category>
		<category><![CDATA[getData]]></category>
		<category><![CDATA[html5 drag and drop]]></category>
		<category><![CDATA[on]]></category>
		<category><![CDATA[ondragstart]]></category>
		<category><![CDATA[setData]]></category>

		<guid isPermaLink="false">http://www.useragentman.com/blog/?p=734</guid>
		<description><![CDATA[HTML5 Drag and Drop is one of the least understood modules of the HTML5 specification and it can be a pain to implement in a cross browser fashion.  This article is for those who want to use it today in their web applications and goes into a lot of detail so you can "get the job done"]]></description>
			<content:encoded><![CDATA[<div class="importantNotes">
<h3>Update (Feb 3, 2009):</h3>
<ul>
<li>A <a href="http://code.google.com/p/chromium/issues/detail?id=14654">bug in Webkit</a> seems to be the culprit in the permissions form example below not working correctly in Safari 4.  The code has been updated to work around this bug and the article below has been updated.  Thanks to <code>russbuelt</code> for pointing this out.</li>
<li>Apparently, an example made by Apple which I reported not working in Safari <em>does</em> work in Safari 4.0.4 for Mac OS X.  Thanks for Scott Straker for this information.</li>
</div>
<p><div id="attachment_538" class="wp-caption alignleft" style="width: 267px"><a href="http://flickr.com/photos/svartling/3822068125/"><img class="size-full wp-image-538   " title="HTML5 Drag and Drop" src="http://www.useragentman.com/blog/wp-content/uploads/2010/12/html5DragAndDrop.png" alt="Remixed version of image by Svartling." width="257" height="174" /></a><p class="wp-caption-text">Image Credit: Flickr user svartling</p></div></p>
<p><a href="http://dev.w3.org/html5/spec/editing.html#dnd">HTML5 Drag and Drop</a> has been talked about a lot lately, but it&#8217;s hard to find really useful information about implementing it across multiple browsers.   <a href="http://hacks.mozilla.org/2009/07/html5-drag-and-drop/">Mozilla</a>, <a href="http://developer.apple.com/Mac/library/documentation/AppleApplications/Conceptual/SafariJSProgTopics/Tasks/DragAndDrop.html">Apple</a> and <a href="http://msdn.microsoft.com/en-us/library/ms537658%28VS.85%29.aspx">Microsoft</a> all have pages describing how to use it, but their examples seem to work only in their particular browser (<strike><em><a href="http://developer.apple.com/Mac/library/documentation/AppleApplications/Conceptual/SafariJSProgTopics/Tasks/DragAndDrop.html">Apple&#8217;s example</a> doesn&#8217;t even work in their own!</em></strike> <strong>Updated, Jan. 11, 2009:</strong> Although I have not been able to get this example working on Safari 2.0.4 and 3.1.2 for OS X and 4.0.4 for Windows, I have received word that it works on Safari 4.0.4 on OS X).  Remy Sharp&#8217;s great article <a href="http://html5doctor.com/native-drag-and-drop/">Native Drag and Drop</a> was a good place for me to start — however, the examples didn&#8217;t work in Internet Explorer.  I also thoroughly enjoyed JavaScript guru <a href="http://www.quirksmode.org/">Peter-Paul Koch&#8217;s</a> <a href="http://www.quirksmode.org/blog/archives/2009/09/the_html5_drag.html">humorous and lengthy rant about cross-browser drag and drop headaches</a> where he uses creative and colourful language to describe what he thought of the standard, the browser manufacturers, and the WHAT-WG.</p>
<p>When normal people see the author of the <a href="http://www.quirksmode.org/compatibility.html">Compatibility Master Tables</a> respond negatively to a web technology, they would probably assume it would be a good sign to stay away from it.</p>
<p>However, I am not normal.  <strong>With a name like Zoltan, how could I <em>possibly</em> be normal?</strong> (and yes, <a href="http://www.useragentman.com/blog/the-infamous-zoltan-faq/">it <em>is</em> my real name</a>).</p>
<p>So, I decided to find out for myself how bad HTML5 Drag and Drop really is. Almost immediately, I understood Koch&#8217;s reaction &#8211; the browser vendors have not implemented all the same features, and there are even a few quirks in how the features that <em>are </em>implemented work.  However, after doing <em>a lot</em> of research, <strong>I found a common denominator that works well</strong>, with the help of a small bit of JavaScript that smooths out the edges.  Despite the implementation flaws, <strong>Future-proof HTML5 Drag and Drop is not too hard for developers to use in their own applications.</strong> This article will explain how to do this step by step with many examples along the way.  By the time you are done, you will be able to write useful  drag and drop scripts of your own like in this example:</p>
<p><a class="exampleLink" href="/tests/dragAndDrop/permissionForm.html#">See an example of HTML5 Drag and Drop in action.</a></p>
<h2>Advantages Over Existing Drag and Drop Implementations</h2>
<p>Koch mentioned in his blog post that &#8220;Web developers MUST NOT (in the sense of RFC 2119) use HTML 5 drag and drop. They should use <a href="http://www.quirksmode.org/js/dragdrop.html">old-school scripts</a> instead&#8221;.  I would argue that developers <em>should</em> use HTML5 drag and drop for the following reasons:</p>
<ul>
<li><strong>JavaScript Framework Independent: </strong>Most other (but not all) drag and drop implementations are tied into 3rd party frameworks like <a href="http://www.dojotoolkit.org/">Dojo</a>, <a href="http://www.prototypejs.org/">Prototype</a> or <a href="http://jquery.com/">jQuery</a>.</li>
<li><strong>Built-in Browser Support: </strong>HTML5 Drag and Drop is supported in Firefox 3.5+, Chrome 3.0+, Safari 3.0+ and Internet Explorer 5.0 (<strong>Note:</strong> that is not a typo &mdash; HTML5 Drag and Drop is based on work done by Microsoft <strong>in 1999</strong>).  Because it is part of HTML5, I assume Opera support should be inevitable.</li>
<li><strong>Integration With Other Web Applications:</strong> the HTML5 specification will allow developers to <em><strong>produce  drag and drop scripts that work across frames, and across browser windows.</strong></em></li>
<li><strong>Integration With Non-Web Applications:</strong> the HTML5 specification also allows users to <em><strong>drag and drop data to and from non-web applications</strong></em></li>
</ul>
<h2>The Basics of Drag and Drop, Step by Step</h2>
<p>In order to save other developers from the headaches I got deciphering cross browser drag and drop, I present the following guide that shows how to do in five easy steps.   Every step will describe a set of related concepts, and will show examples, most of which are built on code from previous steps.  At the end of each step, I will discuss any issues and interesting bits I came across.</p>
<div class="steps">
<h3>Step 1: Defining a Draggable Object</h3>
<p>First you need to define the HTML nodes that you want to drag.  Firefox requires that these nodes have their <code>draggable</code> attribute set to <code>"true"</code>, Internet Explorer requires that the node must be an <code>&lt;a&gt;</code> tag with (the <code>href</code> attribute set) or an <code>&lt;img&gt;</code> tag.</p>
<blockquote class="code">
<pre>&lt;a href="#" id="toDrag" draggable="true"&gt;This is a draggable item&lt;/a&gt;</pre>
</blockquote>
<p>To show how this works, I have written a very simple page with a the above markup in the <code>&lt;body&gt;</code>:</p>
<blockquote class="code">
<pre>&lt;!DOCTYPE html&gt;

&lt;html lang="en"&gt;
    &lt;head&gt;
        &lt;meta name="generator" content="HTML Tidy, see www.w3.org"&gt;

        &lt;title&gt;Test #1: A Simple Draggable Object&lt;/title&gt;

      &lt;script type="text/javascript" src="../../shared/js/EventHelpers.js"&gt;
      &lt;/script&gt;
      &lt;script type="text/javascript" src="../../shared/js/DragDropHelpers.js"&gt;
      &lt;/script&gt;

        &lt;link rel="stylesheet" type="text/css" media="screen" href=
        "css/test1.css"&gt;
    &lt;/head&gt;

    &lt;body&gt;
        &lt;h1&gt;Test #1: A Simple Draggable Object&lt;/h1&gt;

<span class="hilite">        &lt;a href="#" id="toDrag" draggable="true"&gt;This is a
        draggable item&lt;/a&gt;</span>

        &lt;p&gt;Try to drag the red box around. You will see the
        draggable object cloned in every browser except Explorer
        and Chrome.&lt;/p&gt;

        &lt;a href=
        "http://www.useragentman.com/blog/2010/01/10/cross-browser-html5-drag-and-drop/"&gt;
        Go back to the User Agent Man HTML5 Drag and Drop
        article&lt;/a&gt;
    &lt;/body&gt;
&lt;/html&gt;
</pre>
</blockquote>
<p>Note that this HTML page has two scripts in the <code>&lt;head&gt;</code>:</p>
<ul>
<li><strong><code>EventHelpers.js</code>:</strong> this script implements cross-browser event handling routines without the need for a JavaScript framework.  All the examples in this article use it, but feel free to refactor all the code in this article to use a third party framework like Dojo, jQuery or whatever framework you like.</li>
<li><strong><code>DragDropHelpers.js</code>:</strong> among other things that I will get to later, will activate dragging on <code>draggable="true"</code> objects in Safari and Chrome using a built-in copy of Dean Edwards&#8217; <a href="http://dean.edwards.name/my/cssQuery/">cssQuery</a>.  It does this by inserting the following CSS code into the HTML document:<br />
<blockquote class="code">
<pre>[draggable=true] {
  -khtml-user-drag: element;
  -webkit-user-drag: element;
  -khtml-user-select: none;
  -webkit-user-select: none;
}</pre>
</blockquote>
<p>The first two rules allow the dragging of the <code>draggable="true"</code> nodes, while the last two prevent the user from selecting the text within the node (which can cause some unexpected behavior in Webkit browsers).  The <code>-khtml </code>properties work in older versions of Safari, while <code>-webkit</code> ones work in newer versions.  <strong>Without these CSS properties, these nodes will not be draggable when using the Safari web browser</strong></li>
</ul>
<p><a class="exampleLink" href="/tests/dragAndDrop/01-dragObject.html">See Example  #1: a draggable object</a></p>
<p>A couple of notes:</p>
<ol>
<li> You&#8217;ll notice that when you hover the mouse pointer over the draggable item, it will change to <img class="alignnone size-full wp-image-599" title="The  &quot;Move&quot; cursor" src="http://www.useragentman.com/blog/wp-content/uploads/2010/01/cursor-move.gif" alt="The  &quot;Move&quot; cursor" width="24" height="28" />(i.e. I the &#8220;move&#8221; cursor).  This is not the default behavior of the web browser &#8211; it is something I added in <code>DragDropHelpers</code>, since I believe that it&#8217;s a nice visual cue to show users that an object is draggable (if you don&#8217;t want this behavior, it is possible to turn it off in your own scripts by setting <code>DragDropHelpers.showMouseoverCue</code> to false</li>
<li>How draggable objects look in the various browsers varies.  Here are screenshots of how it looks in Windows web browsers (Note: although all screenshots were taken using Windows XP, similar results occur when viewing under Windows Vista or Windows 7):<br />
<table class="screenshots" border="0">
<thead>
<tr>
<th>Firefox (Windows XP)</th>
<th>Safari (Windows XP)</th>
<th>Explorer &amp; Chrome (Windows XP)</th>
</tr>
</thead>
<tbody>
<tr>
<td><img class="alignnone size-full wp-image-514" title="Dragging of a link element, Firefox Windows" src="http://www.useragentman.com/blog/wp-content/uploads/2010/12/dragLink-firefoxWindows.png" alt="[Dragging of a link element, Firefox Windows]" width="180" height="190" /></td>
<td><img class="alignnone size-full wp-image-514" title="Dragging of a link element, Safari Windows" src="http://www.useragentman.com/blog/wp-content/uploads/2010/12/dragLink-safariWindows.png" alt="[Dragging of a link element, Safari Windows]" width="180" height="190" /></td>
<td><img class="alignnone size-full wp-image-514" title="Dragging of a link element, Explorer Windows" src="http://www.useragentman.com/blog/wp-content/uploads/2010/12/dragLink-explorerWindows.png" alt="[Dragging of a link element, Explorer Windows]" width="180" height="190" /></td>
</tr>
</tbody>
</table>
<p>Note the differences between these three browsers:</p>
<ul>
<li>Firefox for Windows shows a cloned version of the draggable object underneath the mouse pointer when the object is being dragged, while Safari displays a grey box with the text &#8220;This is a draggable item&#8221; written inside.  Explorer and Chrome don&#8217;t show any effect at all (We will address this issue later).</li>
<li>Since we have not established <em>where</em> we are going to drag our object, all Windows browsers will change the mouse pointer cursor to <img class="alignnone size-full wp-image-567" title="&quot;Not-Allowed&quot; cursor." src="http://www.useragentman.com/blog/wp-content/uploads/2010/12/cursor_not-allowed.gif" alt="&quot;Not-Allowed&quot; cursor." width="22" height="22" /> (a.k.a. the &#8220;not-allowed&#8221; mouse pointer).  This is the expected behavior, and you will see later that it will help the user know where to drop the object.</li>
</ul>
<p>On OS X, Firefox and Safari look similar to Firefox for Windows, except that the &#8220;not-allowed&#8221; mouse-pointer doesn&#8217;t show up:</p>
<table class="screenshots" border="0">
<thead>
<tr>
<th>Firefox (OS X)</th>
<th>Safari (OS X)</th>
<th>Chrome (OS X)</th>
</tr>
</thead>
<tbody>
<tr>
<td><img class="alignnone size-full wp-image-513" title="Dragging of a link element, Firefox OS X" src="http://www.useragentman.com/blog/wp-content/uploads/2010/12/dragLink-firefoxMac.png" alt="[Dragging of a link element, Firefox Windows]" width="180" height="190" /></td>
<td><img class="alignnone size-full wp-image-514" title="Dragging of a link element, Safari OS X" src="http://www.useragentman.com/blog/wp-content/uploads/2010/12/dragLink-safariMac.png" alt="[Dragging of a link element, Safari OS X]" width="180" height="190" /></td>
<td><img class="alignnone size-full wp-image-653" title="Chrome for Mac" src="http://www.useragentman.com/blog/wp-content/uploads/2010/01/dragLink-chromeMac.png" alt="Chrome for Mac" width="180" height="190" /></td>
</tr>
</tbody>
</table>
<p>The Linux browsers don&#8217;t have the &#8220;not-allowed&#8221; mouse cursor either.  Also, the cloned version of the draggable object isn&#8217;t transparent using the Linux version of Firefox, and Chrome under Linux behaves  pretty much the  same as on the other platforms (Note: all Linux testing was done using Ubuntu 9.10 using the standard Gnome window manager).</p>
<table class="screenshots" border="0">
<thead>
<tr>
<th>Firefox (Ubuntu)</th>
<th>Chrome (Ubuntu)</th>
</tr>
</thead>
<tbody>
<tr>
<td><img class="size-full wp-image-698 alignnone" title="dragLink-firefoxUbuntu" src="http://www.useragentman.com/blog/wp-content/uploads/2010/01/dragLink-firefoxUbuntu.png" alt="Firefox Linux" width="180" height="190" /></td>
<td><img class="size-full wp-image-697 alignnone" title="dragLink-chromeUbuntu" src="http://www.useragentman.com/blog/wp-content/uploads/2010/01/dragLink-chromeUbuntu.png" alt="Chrome Linux" width="180" height="190" /></td>
</tr>
</tbody>
</table>
<p>The fact that Explorer and Chrome don&#8217;t have any visual dragging cues bugged me, so I put a &#8220;fix&#8221; inside <code>DragDropHelpers.js</code> which can be turned on by setting <code>DragDropHelpers.fixVisualCues</code> to true.  Let&#8217;s look at a slightly refactored version of Example #1 where we insert the following code in the <code>&lt;head&gt;:</code></p>
<blockquote class="code">
<pre>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
   &lt;head&gt;
      &lt;title&gt;Test #1: A Simple Draggable Object (with visual cues added to Explorer and Chrome)&lt;/title&gt;
      &lt;script type="text/javascript" src="../../shared/js/EventHelpers.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="../../shared/js/DragDropHelpers.js"&gt;&lt;/script&gt;
<span class="hilite">      &lt;script type="text/javascript"&gt;
      &lt;!--
      DragDropHelpers.fixVisualCues=true;
      --&gt;
      &lt;/script&gt;</span>

      &lt;link rel="stylesheet" type="text/css" media="screen" href="css/test1.css" /&gt;

   &lt;/head&gt;
   &lt;body&gt;

      &lt;h1&gt;Test #1: A Simple Draggable Object (with visual cues added to Explorer and Chrome)&lt;/h1&gt;

      &lt;a href="#" id="toDrag" draggable="true"&gt;This is a draggable item&lt;/a&gt;

      &lt;p&gt;Try to drag the red box around.  You will see the draggable object cloned in all browsers.&lt;/p&gt;

      &lt;a href="http://www.useragentman.com/blog/2010/01/10/cross-browser-html5-drag-and-drop/"&gt;Go back to the User Agent Man HTML5 Drag and Drop article&lt;/a&gt;

   &lt;/body&gt;
&lt;/html&gt;
</pre>
</blockquote>
</li>
</ol>
<p><a class="exampleLink" href="/tests/dragAndDrop/01a-dragObject.html">See Example #1a, a draggable object with visual cue fix for Explorer and Chrome</a></p>
<p>Voila!  The visual cue now shows up.  I&#8217;ll explain how this works at the end of the article.</p>
<h3>Step 2: Setting Events on the Draggable Object</h3>
<p>Now that you have defined an object as draggable, you should attach some events to it so the browser knows what to do while it is being dragged.  There are three events that a draggable object can fire:</p>
<table class="dataTable" border="0">
<thead>
<tr>
<th>Event name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>dragstart</code></td>
<td>Fires when the user starts dragging of the object.</td>
</tr>
<tr>
<td><code>drag</code></td>
<td>Fires every time the mouse is moved while the object is being dragged.</td>
</tr>
<tr>
<td><code>dragend</code></td>
<td>Fires when the user releases the mouse button while dragging an object.</td>
</tr>
</tbody>
</table>
<p>A developer would attach these events the way they would any other JavaScript event &#8211; I use <code>EventHelpers.addEvent</code>, but one could use jQuery&#8217;s <code>bind()</code> method, prototype&#8217;s <code>Event.observe()</code> or whatever you favourite framework provides for event handling.  Developers can also use the old school inline events (i.e. <code>&lt;a href="#" id="toDrag" draggable="true" <span class="hilite">ondragstart="somefunction();"</span>&gt;This is a draggable item&lt;/a&gt;</code>), but I try to stay away from using those to seperate document structure from behavior.</p>
<p>Let&#8217;s take the example in step 1 and change it so we can log when these events happen to the <code>toDrag</code> node:</p>
<blockquote class="code">
<pre>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
   &lt;head&gt;
      &lt;title&gt;Example #2: a draggable object with events attached &lt;/title&gt;
<span class="hilite">      &lt;script type="text/javascript" src="../../shared/js/sprintf.js"&gt;&lt;/script&gt;</span>
      &lt;script type="text/javascript" src="../../shared/js/EventHelpers.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="../../shared/js/DragDropHelpers.js"&gt;&lt;/script&gt;

<span class="hilite">      &lt;script type="text/javascript" src="js/02-dragObjectWithEvent.js"&gt;&lt;/script&gt;</span>

      &lt;link rel="stylesheet" type="text/css" media="screen" href="css/test1.css" /&gt;

   &lt;/head&gt;
   &lt;body&gt;

      &lt;h1&gt;Example #2: a draggable object with events attached&lt;/h1&gt;

      &lt;p&gt;Try to drag the red box around.  This page will tell you if the
      object is currently dragging, and will log all &lt;code&gt;dragstart&lt;/code&gt;
      and &lt;code&gt;dragstop&lt;/code&gt; events&lt;/p&gt;

<span class="hilite">      &lt;p&gt;&lt;strong&gt;Is dragging:&lt;/strong&gt; &lt;span id="dragEventNotice"&gt;no&lt;/span&gt;

      &lt;p id="eventLog"&gt;&lt;strong&gt;Event now firing:&lt;/strong&gt;&lt;br /&gt;&lt;span id="eventNotice"&gt;&lt;/span&gt;</span>

      &lt;a class="goBack" href="http://www.useragentman.com/blog/2010/01/10/cross-browser-html5-drag-and-drop/"&gt;Go back to the User Agent Man HTML5 Drag and Drop article&lt;/a&gt;&lt;/p&gt;

      &lt;a href="#" id="toDrag" draggable="true"&gt;This is a draggable item&lt;/a&gt;

   &lt;/body&gt;
&lt;/html&gt;
</pre>
</blockquote>
<p>In the <code>02-dragObjectWithEvent.js</code> script, the <code>dragEventNotice</code> node will be used to report whether the user is dragging the object around, and the <code>eventNotice</code> will be used to log what events the user has fired:</p>
<blockquote class="code">
<pre>var dragObject = new function () {
   var me = this;

   var dragNode;
   var eventNoticeNode, dragEventNoticeNode;

   /* runs when the page is loaded */
   me.init = function () {

      if (EventHelpers.hasPageLoadHappened(arguments)) {
         return;
      }   

      /* The node being dragged */
      dragNode=document.getElementById('toDrag');

      /* The nodes that report to the user what is happening to that node*/
      eventNoticeNode = document.getElementById('eventNotice');
      dragEventNoticeNode = document.getElementById('dragEventNotice');

      /* The drag event handlers */
<span class="hilite">      EventHelpers.addEvent(dragNode, 'dragstart', dragStartEvent);
      EventHelpers.addEvent(dragNode, 'drag', dragEvent);
      EventHelpers.addEvent(dragNode, 'dragend', dragEndEvent);</span>
   }

   /*
    * The dragstart event handler logs to the user when the event started.
    */
<span class="hilite">   function dragStartEvent(e) {
      eventNoticeNode.innerHTML =
         sprintf("&lt;strong&gt;%s&lt;/strong&gt;: Drag Event started.&lt;br /&gt;%s",
            new Date(),  eventNoticeNode.innerHTML);</span>
   }

   /*
    * The drag event reports to the user that dragging is on.
    */
<span class="hilite">   function dragEvent(e) {
      dragEventNoticeNode.innerHTML = "Currently dragging.";
   }</span>

   /*
    * The dragend event logs to the user when the event had finished *and*
    * also reports that dragging has now stopped.
    */
<span class="hilite">   function dragEndEvent(e) {
      eventNoticeNode.innerHTML =
         sprintf("&lt;strong&gt;%s&lt;/strong&gt;: Drag Event stopped.&lt;br /&gt;%s",
            new Date(), eventNoticeNode.innerHTML);
      dragEventNoticeNode.innerHTML = "Dragging stopped."
   }
}</span>

// fixes visual cues in IE and Chrome.
DragDropHelpers.fixVisualCues=true;

EventHelpers.addPageLoadEvent('dragObject.init');</pre>
</blockquote>
<p>Before we get to the drag events, a word about the coding conventions I use for this and all the other code examples in this article:</p>
<ul>
<li>the <code>var me = this </code>line ensures that the script doesn&#8217;t confuse the <code>this </code>keyword of the object with the <code>this </code>keyword inside an event handler.</li>
<li><code>EventHelpers.addPageLoadEvent()</code> (which is part of <code>EventHelpers.js</code>) will execute <code>dragObject.init</code> when the HTML has loaded.  <code>addPageLoadEvent()</code> is based on code from Dean Edwards&#8217; article <a href="http://dean.edwards.name/weblog/2005/09/busted/">The window.onload Problem &#8211; Solved!</a>.  It is similar to jQuery&#8217;s<code> $(document).ready()</code>method and prototype&#8217;s <code>dom:loaded</code> event.</li>
<li>This code uses a JavaScript version of <code>sprintf()</code> that <a href="/blog/2009/12/22/my-favourite-third-party-javascript-libraries/">I had mentioned in a previous blog post</a>.  I have used it in all the remaining examples because I think it makes the code a lot easier to read (however, it is not necessary in order to make HTML5 drag and drop work).</li>
</ul>
<p>Let&#8217;s get back to the drag events — you&#8217;ll notice that I have set the <code>dragstart</code>, <code>drag</code> and <code>dragend</code> events.  When this page is loaded, it will tell you if the draggable object is currently being dragged, and will log all <code>dragstart</code> and <code>dragstop</code> events on-screen. Click on the example link below and see for yourself:</p>
<p><a class="exampleLink" href="/tests/dragAndDrop/02-dragObjectWithEvents.html">See Example #2, a draggable object with events attached</a></p>
<h3>Step 3: Setting Events on the Target Object</h3>
<p>Step 2 showed us how we can drag an object around the screen and know when each drag starts and ends.  Now we need to drop the object. Let&#8217;s define a <strong>drop target</strong> to be <strong>an object where draggable objects can be dropped</strong>.  A drop target can have the following event handlers attached to it:</p>
<table class="dataTable" border="0">
<thead>
<tr>
<th>Event name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>dragenter</code></td>
<td>Fires when a draggable object is <strong>first dragged inside</strong> an object.</td>
</tr>
<tr>
<td><code>dragover</code></td>
<td>Fires everytime a draggable object is <strong>moved inside</strong> an object.  <strong>Note: if you want to allow the draggable object to be <em>dropped </em>inside this object, <em>you must cancel the default behaviour of this event handler.</em></strong></td>
</tr>
<tr>
<td><code>dragleave</code></td>
<td>Fires when a draggable object is <strong>dragged out</strong> of an object.</td>
</tr>
<tr>
<td><code>drop</code></td>
<td>Fired when a draggable object is <strong>dropped into</strong> an object.</td>
</tr>
</tbody>
</table>
<p><strong>If you want an object to become a drop target, you must attach both the <code>dragover</code> and <code>drop</code> events to it.</strong> You may ask &#8220;I understand why I need to implement <code>drop</code> (after all, it is one half of the term &#8220;drag and drop&#8221;) but why do I need <code>dragover</code>?&#8221;  <strong>As stated in the table above, the <code>drop</code> event will not fire unless you cancel the default behaviour of the event target&#8217;s <code>dragover</code> event</strong>.  This point is really important and <strong>your scripts will not work correctly unless you do this</strong>.</p>
<p>You may ask &#8220;Why is there even a <code>dragover</code> event in the first place?  Isn&#8217;t <code>drag</code> sufficient?&#8221; (In his article, Peter-Paul Koch <a href="http://www.quirksmode.org/blog/archives/2009/09/the_html5_drag.html#link8"> asked this question a bit more emphatically that I did</a>).  The <code>dragover</code> event object does contain some useful information, such as <strong>the coordinates of the  mouse pointer inside the event target</strong>, (When I created <code>DragDropHelpers</code>, this information allowed me to fix Explorer and Chrome&#8217;s visual cue issue that I mentioned in step #1).  These coordinates are stored in the properties <code>offsetX</code> and <code>offsetY</code>, <strong>except in Firefox 3.5</strong> which keeps these values inside of the event properties <code>layerX</code> and <code>layerY</code>.  For convenience, <code>DragDropHelpers</code> includes a method called <code>getEventCoords()</code> that will use the appropriate property to get these values (<strong>Note:</strong> <code>layerX</code> and <code>layerY</code> are only accurate when the drop target&#8217;s CSS <code>position</code> propety is set to <code>relative</code>,<code> absolute</code> or <code>fixed</code>).</p>
<p>To show how to add drop functionality to your code, lets take the code from example #2 and add a drop target:</p>
<blockquote class="code">
<pre>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
   &lt;head&gt;
      &lt;title&gt;Example #3: a dragable object with a drop target&lt;/title&gt;
      &lt;script type="text/javascript" src="../../shared/js/sprintf.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="../../shared/js/EventHelpers.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="../../shared/js/DragDropHelpers.js"&gt;&lt;/script&gt;

<span class="hilite">      &lt;script type="text/javascript" src="js/03-dragObjectWithTargetObject.js"&gt;&lt;/script&gt;</span>

      &lt;link rel="stylesheet" type="text/css" media="screen" href="css/test1.css" /&gt;

   &lt;/head&gt;
   &lt;body&gt;

      &lt;h1&gt;Example #3: a dragable object with a drop target&lt;/h1&gt;

      &lt;p&gt;Try to drag the red box around and dropping it in the target object.&lt;/p&gt;

      &lt;p&gt;&lt;strong&gt;Is dragging:&lt;/strong&gt; &lt;span id="dragEventNotice"&gt;no&lt;/span&gt;

      &lt;p id="eventLog"&gt;&lt;strong&gt;Event now firing:&lt;/strong&gt;&lt;br /&gt;&lt;span id="eventNotice"&gt;&lt;/span&gt;
      &lt;a class="goBack" href="http://www.useragentman.com/blog/2010/01/10/cross-browser-html5-drag-and-drop/"&gt;Go back to the User Agent Man HTML5 Drag and Drop article&lt;/a&gt;&lt;/p&gt;

      &lt;a href="#" id="toDrag" draggable="true"&gt;This is a draggable item&lt;/a&gt;
<span class="hilite">      &lt;div id="dropTarget"&gt;This is a "target" object&lt;/div&gt;</span>

   &lt;/body&gt;
&lt;/html&gt;
</pre>
</blockquote>
<p>The drop target will be the <code>&lt;div&gt;</code> with an <code>id</code> of <code>dropTarget</code>.   The <code>03-dragObjectWithTargetObject.js</code> script is the same code in example #2 except we add two event handlers:</p>
<ul>
<li>a <code>dragover</code> event that reports where the mouse is inside <code>dropTarget</code></li>
<li>a <code>drop</code> event that reports when the drop happened.</li>
</ul>
<blockquote class="code">
<pre>var dragObject = new function () {
   var me = this;

   var dragNode, targetNode;
   var eventNoticeNode, dragEventNoticeNode;
   me.init = function () {

   	if (EventHelpers.hasPageLoadHappened(arguments)) {
   		return;
   	}	

   	dragNode=document.getElementById('toDrag');
   	targetNode=document.getElementById('dropTarget');
   	eventNoticeNode = document.getElementById('eventNotice');
   	dragEventNoticeNode = document.getElementById('dragEventNotice');

   	/* These are events for the draggable object */
   	EventHelpers.addEvent(dragNode, 'dragstart', dragStartEvent);
   	EventHelpers.addEvent(dragNode, 'drag', dragEvent);
   	EventHelpers.addEvent(dragNode, 'dragend', dragEndEvent);

   	/* These are events for the object to be dropped */
   	<span class="hilite">EventHelpers.addEvent(targetNode, 'dragover', dragOverEvent);</span>
   	<span class="hilite">EventHelpers.addEvent(targetNode, 'drop', dropEvent);</span>

   }

   function dragStartEvent(e) {
   	showMessage("Drag Event started");
   }

   function dragEvent(e) {
   	dragEventNoticeNode.innerHTML = "Currently dragging.&lt;br /&gt;";
   }

   function dragEndEvent(e) {
   	showMessage("Drag Event stopped");
   	dragEventNoticeNode.innerHTML = "Dragging stopped."
   }

<span class="hilite">   function dragOverEvent(e) {
   	var coords = DragDropHelpers.getEventCoords(e);
   	showMessage(sprintf(
   	   "Drag over event happened on node with id %s at coordinate (%d, %d)",
   	   this.id, coords.x, coords.y));
   	EventHelpers.preventDefault(e);
   }</span>

<span class="hilite">   function dropEvent(e) {
   	showMessage("Drop event happened on node with id " + this.id);
   	EventHelpers.preventDefault(e);
   }</span>

   function showMessage(message) {
   	eventNoticeNode.innerHTML =
   		sprintf("&lt;strong&gt;%s&lt;/strong&gt;: %s&lt;br /&gt;%s",
   			new Date(), message, eventNoticeNode.innerHTML);
   }

}

// fixes visual cues in IE and Chrome.
DragDropHelpers.fixVisualCues=true;

EventHelpers.addPageLoadEvent('dragObject.init');</pre>
</blockquote>
<p>(<strong>Note:</strong> <code>EventHelpers.preventDefault(e)</code> does what you&#8217;d expect: it prevents the default behavior of the event handler, similar to prototype&#8217;s <code>Event.stop()</code> or jQuery&#8217;s <code>event.preventDefault()</code>)</p>
<p><a class="exampleLink" href="/tests/dragAndDrop/03-dragObjectAndTargetObject.html">See Example #3: a dragable object with a drop target</a></p>
<p>Note that in the above example, the time that the draggable object&#8217;s <code>dragend</code> event fires and the time that the drag target&#8217;s <code>drop</code> event fires is not consistant among browsers.  Safari and Chrome fire <code>dragend</code> first, while Firefox and Internet Explorer fire <code>drop</code> first.</p>
<p>The two other drop target events, <code>dragenter</code> and <code>dragleave</code> are not absolutely necessary for every script, but let&#8217;s add these events to example #3 anyway to see how they work:</p>
<blockquote class="code">
<pre>var dragObject = new function () {
   var me = this;

   var dragNode, targetNode;
   var eventNoticeNode, dragEventNoticeNode;
   me.init = function () {

      if (EventHelpers.hasPageLoadHappened(arguments)) {
         return;
      }   

      dragNode=document.getElementById('toDrag');
      targetNode=document.getElementById('dropTarget');
      eventNoticeNode = document.getElementById('eventNotice');
      dragEventNoticeNode = document.getElementById('dragEventNotice');

      /* These are events for the draggable object */
      EventHelpers.addEvent(dragNode, 'dragstart', dragStartEvent);
      EventHelpers.addEvent(dragNode, 'drag', dragEvent);
      EventHelpers.addEvent(dragNode, 'dragend', dragEndEvent);

      /* These are events for the object to be dropped */
      EventHelpers.addEvent(targetNode, 'dragover', dragOverEvent);
      EventHelpers.addEvent(targetNode, 'drop', dropEvent);
<span class="hilite">      EventHelpers.addEvent(targetNode, 'dragenter', dragEnterEvent);
      EventHelpers.addEvent(targetNode, 'dragleave', dragLeaveEvent);</span>
   }

   function dragStartEvent(e) {
      showMessage("Drag Event started");
   }

   function dragEvent(e) {
      dragEventNoticeNode.innerHTML = "Currently dragging.&lt;br /&gt;";
   }

   function dragEndEvent(e) {
      showMessage("Drag Event stopped");
      dragEventNoticeNode.innerHTML = "Dragging stopped.";
   }

   function dragOverEvent(e) {
      var coords = DragDropHelpers.getEventCoords(e);

      showMessage(sprintf(
         "Drag over event happened on node with id %s at coordinate (%d, %d)",
         this.id, coords.x, coords.y));

      EventHelpers.preventDefault(e);
   }

   function dropEvent(e) {
      showMessage("Drop event happened on node with id " + this.id);
      EventHelpers.preventDefault(e);

   }

<span class="hilite">   function dragEnterEvent(e) {
      showMessage("Drag Enter event happened on node with id " + this.id);
   }

   function dragLeaveEvent(e) {
      showMessage("Drag Leave event happened on node with id " + this.id);
   }</span>

   function showMessage(message) {
      eventNoticeNode.innerHTML =
         sprintf("&lt;strong&gt;%s&lt;/strong&gt;: %s&lt;br /&gt;%s",
            new Date(), message, eventNoticeNode.innerHTML);
   }

}

// fixes visual cues in IE and Chrome.
DragDropHelpers.fixVisualCues=true;

EventHelpers.addPageLoadEvent('dragObject.init');</pre>
</blockquote>
<p><a class="exampleLink" href="/tests/dragAndDrop/03a-dragEnterDragLeave.html">See Example #3a: a drop target with dragenter and dragleave event handlers</a></p>
<p>(Note that in Firefox 3.5, a <code>dragleave</code> event will fire just before a <code>drop</code> event, which the other browsers don&#8217;t fire <code>dragleave</code> when dropping).</p>
<h3>Step 4: Passing Data Between the Draggable and Target Objects</h3>
<p>Once a draggable object is dropped into a drop target, the two objects can pass information between them using the drop event&#8217;s <code>dataTransfer</code> property.  This property has two methods, <code>setData()</code> and <code>getData()</code>.  In order to pass data between the two, a developer must use <code>setData()</code> during one of the draggable node&#8217;s drag events (e.g. <code>dragstart</code>).  The drop target can then receive this data during one of it&#8217;s events using the <code>getData()</code> method.</p>
<table class="dataTable" border="0">
<thead>
<tr>
<th>Method</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="methodName"><code>setData(dataType, data)</code></td>
<td>
<p>Sets the data that can be shared between the draggable node and the drag target.</p>
<p><strong>Parameters</strong></p>
<ul>
<li><code>dataType</code> &#8211; The type of data that is to be set.  So far, the only cross-browser dataTypes that can be set are &#8216;Text&#8217; and &#8216;Url&#8217;.</li>
<li><code>data</code> &#8211; The data that is going to be set.</li>
</ul>
</td>
</tr>
<tr>
<td class="methodName"><code>getData(dataType)</code></td>
<td>
<p>Gets the data that was previously set by <code>dataTransfer.setData()</code>.  <strong>It can also get data that was set by a <code>dataTransfer.setData()</code> call from another page, another browser window, and <em>another vendor&#8217;s web browser on the same machine</em>.</strong> It also can get data that was set by a drop event of another desktop application (for example, Windows WordPad).</p>
<p><strong>Parameters</strong></p>
<ul>
<li><code>dataType</code> &#8211; The type of data that is to be set.  So far, the only cross-browser dataTypes that can be set are &#8216;Text&#8217; and &#8216;Url&#8217;.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p><code>setData()</code> takes one string parameter, which is the data you want to share between the draggable object and a drop target.</p>
<p>Let&#8217;s take the code from step #3 and mix it up a bit: instead of having one draggable object, let&#8217;s make four.  I have taken <a href="http://commons.wikimedia.org/wiki/File:The_Fabs.JPG">four photos of the Beatles from the Wikmedia Commons</a> and made them draggable.</p>
<blockquote class="code">
<pre>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
   &lt;head&gt;
      &lt;title&gt;Test 4: setData() and getData()&lt;/title&gt;
      &lt;script type="text/javascript" src="../../shared/js/sprintf.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="../../shared/js/helpers.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="../../shared/js/DragDropHelpers.js"&gt;&lt;/script&gt;

<span class="hilite">      &lt;script type="text/javascript" src="js/04-setDataGetData.js"&gt;&lt;/script&gt;</span>

      &lt;link rel="stylesheet" type="text/css" media="screen" href="css/test1.css" /&gt;

   &lt;/head&gt;
   &lt;body&gt;

      &lt;h1&gt;Test 4: &lt;code&gt;setData()&lt;/code&gt; and &lt;code&gt;getData()&lt;/code&gt;&lt;/h1&gt;

<span class="hilite">      &lt;img draggable="true" src="images/george.png" alt="George Harrison" /&gt;
      &lt;img draggable="true" src="images/john.png" alt="John Lennon" /&gt;
      &lt;img draggable="true" src="images/paul.png" alt="Paul McCartney" /&gt;
      &lt;img draggable="true" src="images/ringo.png" alt="Ringo Starr" /&gt;</span>

      &lt;div id="dropTarget"&gt;&lt;span&gt;Drop an image here to find out more information about it.&lt;/span&gt;&lt;/div&gt;

   &lt;/body&gt;
&lt;/html&gt;</pre>
</blockquote>
<p>The script <code>04-setDataGetData.js</code> uses <code>dataTransfer.setData()</code> and <code>.getData()</code> to copy the <code>&lt;img&gt;</code> of the Beatle being dragged, as well as placing the <code>alt</code> attribute&#8217;s contents underneath the image.</p>
<blockquote class="code">
<pre>var dragObject = new function () {
   var me = this;

   var targetNode;
   var eventNoticeNode, dragEventNoticeNode;

   var dataTransferCommentString;

   me.init = function () {

      if (EventHelpers.hasPageLoadHappened(arguments)) {
         return;
      }   

      targetNode=document.getElementById('dropTarget');
      eventNoticeNode = document.getElementById('eventNotice');
      dragEventNoticeNode = document.getElementById('dragEventNotice');

      /* These are events for the draggable objects */
      var dragNodes = cssQuery('[draggable=true]');
      for (var i = 0; i &lt; dragNodes.length; i++) {
         var  dragNode=dragNodes[i]
         EventHelpers.addEvent(dragNode, 'dragstart', dragStartEvent);
      }

      /* These are events for the object to be dropped */
      EventHelpers.addEvent(targetNode, 'dragover', dragOverEvent);
      EventHelpers.addEvent(targetNode, 'drop', dropEvent);
   }

   function dragStartEvent(e) {
<span class="hilite">      e.dataTransfer.setData('Text',
         sprintf('&lt;img src="%s" alt="%s" /&gt;&lt;br /&gt;&lt;p class="caption"&gt;%s&lt;/p&gt;',
            this.src, this.alt, this.alt
         )
      );</span>
   }

   function dragOverEvent(e) {
      EventHelpers.preventDefault(e);
   }

   function dropEvent(e) {
<span class="hilite">      this.innerHTML = e.dataTransfer.getData('Text');</span>
      EventHelpers.preventDefault(e);
   }

}

// fixes visual cues in IE and Chrome.
DragDropHelpers.fixVisualCues=true;

EventHelpers.addPageLoadEvent('dragObject.init');</pre>
</blockquote>
<p><a class="exampleLink" href="/tests/dragAndDrop/04-setDataGetData.html">See Example #4: using setData() and getData()</a></p>
<p>As previously mentioned the data that is set in setData() can be read in other applications.  If you are in Windows, open up WordPad and drag one of the images into it.  You&#8217;ll see the data that was given to setData() during the dragstart event.</p>
<p>This interoperability with the operating system will allow developers to do some very interesting things which are out of the scope of this article.  I will, however, want to explore this in future postings.</p>
<h3>Step 5: Drag  and  Drop Effects</h3>
<p>In a traditional desktop application, you can use drag and drop to copy objects, move objects, and create links to things.  You can use the HTML5 drag and drop events to do this as well, but it would be nice to give the user visual cues to show what kind of action a draggable object can do, and what kind of action a drop target can accept.  It would also be nice to only allow &#8220;copy objects&#8221; to only drop on drop targets that are programmed to accept them.</p>
<p>These features can be implemented using <code>e.dataTransfer.effectAllowed</code> on the draggable object and <code>e.dataTransfer.dropEffect()</code>on the drop target:</p>
<table class="dataTable" border="0">
<thead>
<tr>
<th>Method</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>e.dataTransfer.effectAllowed</code></td>
<td>Sets the type of effect a draggable object is allowed to make.  Valid values are  <code>copy</code>, <code>move</code>, <code>link</code>, <code>copyMove</code>, <code>copyLink</code>, <code>linkMove</code>, <code>all</code>, and <code>none</code>.</td>
</tr>
<tr>
<td><code>e.dataTransfer.dropEffect</code></td>
<td>Sets the type of effect a drop target is allowed to accept.  Valid effects include <code>copy</code>, <code>move</code>, and <code>link</code>.</td>
</tr>
</tbody>
</table>
<p>When these are set correctly, drag and drop will only work when the draggable object&#8217;s <code>effectAllowed</code> and the drop target&#8217;s <code>dropEffect</code> are compatible.  To illustrate this, let&#8217;s first change the <code>dragstart</code> and <code>dragover</code> events in example #4 to do the <code>copy</code> effect:</p>
<blockquote class="code">
<pre>   function dragStartEvent(e) {
<span class="hilite">      e.dataTransfer.effectAllowed="copy";</span> 

      e.dataTransfer.setData('Text',
         sprintf('&lt;img src="%s" alt="%s" /&gt;&lt;br /&gt;&lt;p class="caption"&gt;%s&lt;/p&gt;',
            this.src, this.alt, this.alt
         )
      );
   }

   function dragOverEvent(e) {
<span class="hilite">      e.dataTransfer.dropEffect = "copy";</span>
      EventHelpers.preventDefault(e);
   }</pre>
</blockquote>
<p><a class="exampleLink" href="/tests/dragAndDrop/05-dataTransfer.html">Example #5: matching drag and drop effects</a></p>
<p>Note that unlike <a href="/tests/dragAndDrop/04-setDataGetData.html">example #4</a>, the mouse pointer has changed from the copy icon (in Windows, <img class="alignnone size-full wp-image-616" title="Windows Copy Icon" src="http://www.useragentman.com/blog/wp-content/uploads/2010/01/copyWindows.gif" alt="[Windows Copy Icon]" width="23" height="32" />) to the move icon (<img class="alignnone size-full wp-image-615" title="Windows Move Icon" src="http://www.useragentman.com/blog/wp-content/uploads/2010/01/moveWindows.gif" alt="[Windows Move Icon]" width="13" height="27" />).</p>
<p>Now let&#8217;s change the code so that the draggable object and the drop target don&#8217;t have matching effects:</p>
<blockquote class="code">
<pre>   function dragStartEvent(e) {
<span class="hilite">      e.dataTransfer.effectAllowed="copy";</span> 

      e.dataTransfer.setData('Text',
         sprintf('&lt;img src="%s" alt="%s" /&gt;&lt;br /&gt;&lt;p class="caption"&gt;%s&lt;/p&gt;',
            this.src, this.alt, this.alt
         )
      );
   }

   function dragOverEvent(e) {
<span class="hilite">      e.dataTransfer.dropEffect = "move";</span>
      EventHelpers.preventDefault(e);
   }</pre>
</blockquote>
<p><a class="exampleLink" href="/tests/dragAndDrop/05a-dataTransferNoMatch.html">See Example #5a: unmatching drag and drop effects</a></p>
<p>You&#8217;ll see that the user is not able to drop the image on the drop target.</p>
<p><strong>Update (Jan 28, 2010):</strong>  There is a documented bug in Safari and Chrome for Windows where <a href="http://code.google.com/p/chromium/issues/detail?id=14654">drag and drop will not occur if <code>effectAllowed</code> and <code>dropEffect</code> are set to <code>move</code></a>.  This bug does not occur in the Mac editions of these browsers.  </p>
</div>
<h2>A More Complex Example</h2>
<p>Let&#8217;s take all the information we have gathered and make a &#8220;real-world&#8221; script. Let&#8217;s say you were asked to create a &#8220;user entitlement&#8221; administration widget for a website.  The widget will have three entitlement catagories: &#8220;Unassigned Users&#8221;, &#8220;Restricted Users&#8221; (e.g. users who have restricted access to the website) and &#8220;Power Users&#8221; (e.g. users who would have administration access to a website):</p>
<p><div id="attachment_684" class="wp-caption aligncenter" style="width: 403px"><img class="size-full wp-image-684" title="permissionFormScreenshot" src="http://www.useragentman.com/blog/wp-content/uploads/2010/01/permissionFormScreenshot1.png" alt="Screenshot of the user entitlement screen we want to implement" width="393" height="185" /><p class="wp-caption-text">Screenshot of the user entitlement screen we want to implement</p></div></p>
<p>It should be rather easy for an administrator to drag and drop users back and forth between entitlement &#8220;buckets&#8221; in order to elevate or lower a users credentials.</p>
<p>Let&#8217;s go through the steps we established above to implement this script.</p>
<div class="steps">
<h3>Step 1: Defining a Draggable Objects</h3>
<p>First let&#8217;s take a look at the table in the HTML:</p>
<blockquote class="code">
<pre>&lt;table&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th&gt;Unassigned Users&lt;/th&gt;
            &lt;th&gt;Restricted Users&lt;/th&gt;
            &lt;th&gt;Power Users&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;

    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td id="unassignedUsers"&gt;
            	&lt;a href="#" draggable="true"&gt;Moe Howard&lt;/a&gt;
		&lt;a href="#" draggable="true"&gt;Curly Howard&lt;/a&gt;
		&lt;a href="#" draggable="true"&gt;Shemp Howard&lt;/a&gt;
            	&lt;a href="#" draggable="true"&gt;Larry Fine&lt;/a&gt;
	    &lt;/td&gt;

            &lt;td id="restrictedUsers"&gt;
            &lt;/td&gt;

            &lt;td id="powerUsers"&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
    &lt;tfoot&gt;
	&lt;td id="unassignedUsersHelp"&gt;
	  Drag a user from this list to another list to change the
	  user's permissions.
	&lt;/td&gt;
	&lt;td id="restrictedUsersHelp"&gt;
	  Dragging user here will give this user restricted
	  permissions.
	&lt;/td&gt;
	&lt;td id="powerUsersHelp" &gt;
	  Dragging a user here will give this user power user access.
	&lt;/td&gt;
    &lt;/tfoot&gt;
&lt;/table&gt;</pre>
</blockquote>
<p>(Note that the contents in the <code>tfoot</code> node are hidden by CSS).</p>
<h3>Step 2: Setting Events on the Draggable Objects</h3>
<p>The &#8220;users&#8221; are draggable objects.  In the script, we find all these nodes using Dean Edwards&#8217; <a href="http://dean.edwards.name/my/cssQuery/">cssQuery</a> and  set <code>dragstart</code> and <code>dragend</code> event handlers on each of them (Note: cssQuery allows us to select nodes by CSS selector, similar to the built-in functionality inside jQuery).</p>
<blockquote class="code">
<pre>userNodes = cssQuery('[draggable=true]');
for (var i=0; i&lt;userNodes.length; i++) {
   EventHelpers.addEvent(userNodes[i], 'dragstart', userDragStartEvent);
   EventHelpers.addEvent(userNodes[i], 'dragend', userDragEndEvent);
}</pre>
</blockquote>
<p>It then keeps track of the current node being dragged in the object variable <code>currentlyDraggedNode</code> and makes that node transparent by making it a member of the <code>draggedUser</code> class, which the page&#8217;s stylesheet defines as transparent.</p>
<blockquote class="code">
<pre>function userDragStartEvent(e) {
   currentlyDraggedNode = this;
   currentlyDraggedNode.className = 'draggedUser';
}</pre>
</blockquote>
<p>The <code>dragend</code> event removes the transparency of the <code>currentlyDraggedNode</code>:</p>
<blockquote class="code">
<pre>function userDragEndEvent(e) {
   currentlyDraggedNode.className = '';
}</pre>
</blockquote>
<h3>Step 3: Setting Events on the Target Objects</h3>
<p>All the table cells in the <code>&lt;tbody&gt;</code> are drop targets, so we use cssQuery again to grab all those nodes and set the appropriate events.</p>
<blockquote class="code">
<pre>userListNodes = cssQuery('.userList');

for (var i=0; i&lt;userListNodes.length; i++) {
   var userListNode = userListNodes[i];
   EventHelpers.addEvent(userListNode, 'dragover', cancel);
   EventHelpers.addEvent(userListNode, 'dragleave', userDragLeaveListEvent);
   EventHelpers.addEvent(userListNode, 'drop', userDropListEvent);
   EventHelpers.addEvent(userListNode, 'dragenter', userDragOverListEvent);
}</pre>
</blockquote>
<p>The most important event is the <code>drop</code> event handler.  This event handler takes the <code>currentlyDraggedNode</code>, removes it from its current table cell (using <code>removeChild()</code>) and  drops it into the drop target (using <code>appendChild()</code>).</p>
<blockquote class="code">
<pre>function setHelpVisibility(node, isVisible) {
   var helpNodeId = node.id + "Help";
   var helpNode = document.getElementById(helpNodeId);

   if (isVisible) {
      helpNode.className =  'showHelp';
   } else {
      helpNode.className =  '';
   }
}

function userDropListEvent(e) {
   currentlyDraggedNode.parentNode.removeChild(currentlyDraggedNode);
   this.appendChild(currentlyDraggedNode);
   setHelpVisibility(this, false);
   userDragEndEvent(e);
}</pre>
</blockquote>
<p>They show the contents of the table cell below it by making it a member of the class <code>showHelp</code>.  They also</p>
<p>prevent the <code>dragover</code> default behaviour so that <code>drop</code> events will fire in the drop target.</p>
<blockquote class="code">
<pre>function userDragOverListEvent(e) {
   setHelpVisibility(this, true);
   EventHelpers.preventDefault(e);
}</pre>
</blockquote>
<p>The <code>dragleave</code> re-hides the contents of the table cell below it.</p>
<blockquote class="code">
<pre>function userDragLeaveListEvent(e) {
  setHelpVisibility(this, false);
}</pre>
</blockquote>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 4921px; width: 1px; height: 1px;">
<pre>function userDragEndEvent(e) {
   currentlyDraggedNode.className = '';
}</pre>
</div>
<h3>Steps 4 and 5</h3>
<p>We do not actually pass any data between the draggable object and the drag target — when the <code>drop</code> event occurs, the draggable object is moved from one column to another without the need of any <code>dataTransfer</code> data.  The drag and drop effects were already handled in step 3, so we are all done.</p>
<p><a class="exampleLink" href="/tests/dragAndDrop/permissionForm.html">See the above example in action</a></p>
</div>
<h2>Drag and Drop Between Frames</h2>
<p>Using the above information, it is also possible to drag and drop objects between frames (this is something that I don&#8217;t think any old school drag and drop script can do).  I made an example below, and I leave it as an exercise to the reader to go through the code and figure out how it works.</p>
<p><a class="exampleLink" href="/tests/dragAndDrop/05a-crossFrameSetData/">See the inter-frame drag and drop example in action</a></p>
<h2>Cross-Browser Issues I Have Seen</h2>
<p>These are the cross-browser issues I know of so far.  I will update this list with others I find in the future.</p>
<ul>
<li>in Firefox 3.5, a <code>dragleave</code> event will fire just before a <code>drop</code> event, which the other browsers don’t fire <code>dragleave</code> when dropping.</li>
<li>the time that the draggable object’s <code>dragend</code> event fires and the time that the drag target’s <code>drop</code> event fires is not consistant among browsers. Safari and Chrome fire <code>dragend</code> first, while Firefox and Internet Explorer fire <code>drop</code> first.</li>
<li>According to <a href="http://www.alertdebugging.com/drag-and-drop-bugs/">Francisco Tolmasky</a>, <a href="http://www.alertdebugging.com/drag-and-drop-bugs/"><code>setData()</code> cannot be called during drag events in Firefox</a>.  Hopefully this will be fixed in a future release of the browser.</li>
<li><code>DragDropHelpers</code> implements a workaround for Explorer so it can drag <em>any</em> object around. However, I did not use this functionality in any of the examples because it exposes a problem with <a href="http://lists.apple.com/archives/web-dev/2009/Jul/msg00042.html">Safari not being able to drag an object correctly when the user initiates the drag on the text inside it</a>.  This problem doesn&#8217;t happen with text inside of links, but with text inside other nodes (like a <code>&lt;div&gt;</code> tag).  I understand this problem has been fixed in the Safari nightly builds, so I left the functionality in the DragDropHelpers library for future use.</li>
<li>DragDropHelpers.js &#8220;fixes&#8221; the dragging visual cues in Explorer and Chrome by:
<ul>
<li>making a transparent and absolutely positioned clone of a draggable object when it&#8217;s <code>dragstart</code> event is fired</li>
<li>moving the cloned object near the mouse when the <code>&lt;body&gt;</code>&#8216;s <code>drag</code> event is fired.</li>
<li>destroying the clone when draggable object&#8217;s <code>dragend</code> event is fired.</li>
</ul>
<p>I didn&#8217;t turn it on by default because I am not sure when (or if) this problem will be fixed in Internet Explorer or Chrome.  I also doesn&#8217;t work well when dragging objects from one window or frame to another, since the cloned visual cue can&#8217;t jump from page to page.  Feel free to use it (or not) as you see fit.</li>
<li>As mentioned previously, because of a <a href="http://code.google.com/p/chromium/issues/detail?id=14654">bug in Webkit</a>, drag and drop will not occur if <code>effectAllowed</code> and <code>dropEffect</code>  are set to <code>move</code> in Safari 4.0.4.  Hopefully this will be fixed in a future version of Webkit.</li>
</ul>
<h2>Conclusion</h2>
<p>HTML5 drag and drop will be an important part of our front-end toolkit.  Even though the flavors that browser-vendors offer today are incomplete, it is possible to smooth out these flaws to produce useful web applications.  What I have covered here only scratches the surface &#8211; there are more properties and methods that some of browsers offer that can be used to further enhance the drag and drop experience.   But I have been working on this blog entry way to long, and my wife is giving me that &#8220;what the hell are you doing up at this hour&#8221; look, so I think I&#8217;ll stop here for now.</p>
<h2>Download</h2>
<p><code>DragDropHelpers</code>, and all code used in this article can be downloaded below.</p>
<p><a class="exampleLink" href="/downloads/DragDropHelpers-1.0a.zip">DragDropHelpers.js v.1.0a and sample code.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.useragentman.com/blog/2010/01/10/cross-browser-html5-drag-and-drop/feed/</wfw:commentRss>
		<slash:comments>58</slash:comments>
		</item>
	</channel>
</rss>

