<?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>Mozilla Hacks - the Web developer blog &#187; Demo</title>
	<atom:link href="http://hacks.mozilla.org/category/demo/feed/" rel="self" type="application/rss+xml" />
	<link>http://hacks.mozilla.org</link>
	<description>hacks.mozilla.org</description>
	<lastBuildDate>Wed, 08 Feb 2012 22:52:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Creating thumbnails with drag and drop and HTML5 canvas</title>
		<link>http://hacks.mozilla.org/2012/02/creating-thumbnails-with-drag-and-drop-and-html5-canvas/</link>
		<comments>http://hacks.mozilla.org/2012/02/creating-thumbnails-with-drag-and-drop-and-html5-canvas/#comments</comments>
		<pubDate>Wed, 01 Feb 2012 14:00:28 +0000</pubDate>
		<dc:creator>Janet Swisher</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[Featured Demo]]></category>
		<category><![CDATA[FileAPI]]></category>
		<category><![CDATA[HTML5]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=11119</guid>
		<description><![CDATA[HTML5 Canvas is a very cool feature. Seemingly just an opportunity to paint inside the browser with a very low-level API you can use it to heavily convert and change image and video content in the document. Today, let&#8217;s take a quick look at how you can use Canvas and the FileReader API to create [...]]]></description>
			<content:encoded><![CDATA[<p>HTML5 Canvas is a very cool feature. Seemingly just an opportunity to paint inside the browser with a very low-level API you can use it to heavily convert and change image and video content in the document. Today, let&#8217;s take a quick look at how you can use Canvas and the FileReader API to create thumbnails from images dropped into a browser document.</p>
<p>The <a href="https://github.com/codepo8/canvasthumber">final code is available on GitHub</a> and you can see an <a href="http://thewebrocks.com/demos/canvasthumber/">online demo here</a>. There is also a screencast available on YouTube:</p>
<p><iframe width="500" height="284" src="http://www.youtube.com/embed/cYN45FixIgI?rel=0" frameborder="0" allowfullscreen></iframe></p>
<h2>Step 1: Getting the files into the browser</h2>
<p>The first step to resize images in the browser is to somehow get them. For this, we can just add an element in the page and assign drag and drop event handlers to it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">s.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'dragover'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span> evt <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  evt.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
s.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'drop'</span><span style="color: #339933;">,</span> getfiles<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Notice that we only prevent the default behaviour when we drag things over the element. This is to prevent the browser from just showing the images when we drag them in. </p>
<p>The <code>getfiles()</code> function then does the hard work of reading all the files in and sending them on to the functions that do the resizing and image generation:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getfiles<span style="color: #009900;">&#40;</span> ev <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> files <span style="color: #339933;">=</span> ev.<span style="color: #660066;">dataTransfer</span>.<span style="color: #660066;">files</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> files.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> files.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span> i<span style="color: #339933;">--</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> file <span style="color: #339933;">=</span> files<span style="color: #009900;">&#91;</span> i <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> file.<span style="color: #660066;">type</span>.<span style="color: #660066;">indexOf</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'image'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">continue</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
      <span style="color: #003366; font-weight: bold;">var</span> reader <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> FileReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      reader.<span style="color: #660066;">readAsDataURL</span><span style="color: #009900;">&#40;</span> file <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      reader.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span> ev <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> img <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Image<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> ev.<span style="color: #660066;">target</span>.<span style="color: #660066;">result</span><span style="color: #339933;">;</span>
        img.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        imagetocanvas<span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> thumbwidth<span style="color: #339933;">,</span> thumbheight<span style="color: #339933;">,</span> crop<span style="color: #339933;">,</span> background <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  ev.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The <code>drop</code> event gives us a property called <code>dataTransfer</code> which contains a list of all the files that have been dropped. We make sure that there was at least one file in the drop and then iterate over them.</p>
<p>If the file type was not an image (or in other words the type property of the file does not contain the string &#8220;image&#8221;) we don&#8217;t do anything with the file and continue the loop.</p>
<p>If the file is an image we instantiate a new <code>FileReader</code> and tell it to read the file as a Data URL. When the reader successfully loaded the file it fires its <code>onload</code> handler.</p>
<p>In this handler we create a new image and set its <code>src</code> attribute to the <code>result</code> of the file transfer. We then send this image to the <code>imagetocanvas()</code> function with the parameters to resize (in the demo these come from the form):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> imagetocanvas<span style="color: #009900;">&#40;</span> img<span style="color: #339933;">,</span> thumbwidth<span style="color: #339933;">,</span> thumbheight<span style="color: #339933;">,</span> crop<span style="color: #339933;">,</span> background <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  c.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> thumbwidth<span style="color: #339933;">;</span>
  c.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> thumbheight<span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> dimensions <span style="color: #339933;">=</span> resize<span style="color: #009900;">&#40;</span> img.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> img.<span style="color: #660066;">height</span><span style="color: #339933;">,</span> thumbwidth<span style="color: #339933;">,</span> thumbheight <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> crop <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    c.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> dimensions.<span style="color: #660066;">w</span><span style="color: #339933;">;</span>
    c.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> dimensions.<span style="color: #660066;">h</span><span style="color: #339933;">;</span>
    dimensions.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    dimensions.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> background <span style="color: #339933;">!==</span> <span style="color: #3366CC;">'transparent'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    cx.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> background<span style="color: #339933;">;</span>
    cx.<span style="color: #660066;">fillRect</span> <span style="color: #009900;">&#40;</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> thumbwidth<span style="color: #339933;">,</span> thumbheight <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  cx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span> 
    img<span style="color: #339933;">,</span> dimensions.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> dimensions.<span style="color: #660066;">y</span><span style="color: #339933;">,</span> dimensions.<span style="color: #660066;">w</span><span style="color: #339933;">,</span> dimensions.<span style="color: #660066;">h</span> 
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  addtothumbslist<span style="color: #009900;">&#40;</span> jpeg<span style="color: #339933;">,</span> quality <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This function gets the desired thumbnail size and resizes the canvas to these dimensions. This has the added benefit of wiping the canvas so that no old image data would be added to our thumbnail. We then resize the image to fit into the thumbnail using a <code>resize()</code> function. You can see for yourself what this one does in the source code, it just means the image gets resized to fit. The function returns an object with the width and the height of the new image and the x and y position where it should be positioned onto the canvas. </p>
<p>If we don&#8217;t want the full-size thumbnail but instead crop it we resize the canvas accordingly and reset x and y to 0.</p>
<p>If the user requested a background we fill the canvas with the colour. After that we put the image on the canvas with the x and y coordinates and the new width and height. </p>
<p>This takes care of creating a new thumbnail on the canvas, but we haven&#8217;t got it as an image in the document yet. To this end, we call  <code>addtothumbslist()</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> addtothumbslist<span style="color: #009900;">&#40;</span> jpeg<span style="color: #339933;">,</span> quality <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> thumb <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Image<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      url <span style="color: #339933;">=</span> jpeg <span style="color: #339933;">?</span> c.<span style="color: #660066;">toDataURL</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'image/jpeg'</span> <span style="color: #339933;">,</span> quality <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> c.<span style="color: #660066;">toDataURL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  thumb.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> url<span style="color: #339933;">;</span>
  thumb.<span style="color: #660066;">title</span> <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span> url.<span style="color: #660066;">length</span> <span style="color: #009966; font-style: italic;">/ 1000 * 100 ) /</span> <span style="color: #CC0000;">100</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' KB'</span><span style="color: #339933;">;</span>
  o.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span> thumb <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This one creates a new image and checks if the users wanted a JPG or PNG image to be created. PNG images tend to be better quality but also bigger in file size. If a JPG was requested we call the canvas&#8217; <code>toDataURL()</code> method with two parameters: the requested JPEG mime type and the quality of the image (ranging between 0 and 1 with 1 being best quality). If a PNG is wanted, we can just call <code>toDataURL()</code> without any parameters as this is the default.</p>
<p>We set the <code>src</code> of the image to the  generated url string and add a title showing the size of the image in KB (rounded to two decimals). All that is left then is to add the thumb to the output element on the page.</p>
<p>That&#8217;s it, you can now drag and drop images into the browser to generate thumbnails. Right now, we can only save them one at a time (or if you have some download add-ons all at once). Would be fun to add <a href="gildas-lormeau.github.com/zip.js/">Zip.js</a> to the mix to offer them as a zip. I dare you! :)</p>
<p>More reading:</p>
<ul>
<li><a href="https://developer.mozilla.org/en/DragDrop/Drag_and_Drop">Drag and Drop</a> on MDN</a></li>
<li><a href="https://developer.mozilla.org/en/DOM/FileReader">FileReader</a> on MDN</a></li>
<li><a href="https://developer.mozilla.org/en/HTML/Canvas">Canvas</a> on MDN</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2012/02/creating-thumbnails-with-drag-and-drop-and-html5-canvas/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Screencast: 3D CSS rollovers and 3D CSS tester</title>
		<link>http://hacks.mozilla.org/2011/12/screencast-3d-css-rollovers-and-3d-css-tester/</link>
		<comments>http://hacks.mozilla.org/2011/12/screencast-3d-css-rollovers-and-3d-css-tester/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 01:06:55 +0000</pubDate>
		<dc:creator>Chris Heilmann</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[transforms]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=10453</guid>
		<description><![CDATA[CSS 3D transforms as supported in the latest Aurora allow us to do some nice effects that in the past were only possible in Flash or with a lot of trickery using skewing and filters. I was asked to show a small demo the other day and thought it would be fun to spice up [...]]]></description>
			<content:encoded><![CDATA[<p>CSS 3D transforms as supported in the latest Aurora allow us to do some nice effects that in the past were only possible in Flash or with a lot of trickery using skewing and filters. I was asked to show a small demo the other day and thought it would be fun to spice up the classic image rollover:</p>
<p><iframe src="http://www.screenr.com/embed/sObs" width="500" height="305" frameborder="0"></iframe></p>
<p>You can <a href="http://thewebrocks.com/demos/cuberollovers/">see this in action</a> using Chrome, Safari or Firefox Aurora/Nightly. Older browsers should just show a normal roll-over (and yes, the first example looks weird due to the logo transparency but it makes the effect much cooler in supporting browsers). </p>
<p>As people asked how this is done I thought it easiest to create <a href="http://thewebrocks.com/demos/3D-css-tester/">a testing tool where you can play with 3D CSS yourself</a>. Here&#8217;s a screencast where I explain how to use it and how the roll-over effects were done.</p>
<p><iframe width="500" height="254" src="http://www.youtube.com/embed/yP5eFRZ_X54?rel=0&amp;hd=1" frameborder="0" allowfullscreen></iframe></p>
<p>I found a few interesting bugs while creating this and will file them now. This is the benefit of playing with new tech. Enjoy!</p>
<p>More reading on this: </p>
<ul>
<li><a href="http://24ways.org/2010/intro-to-css-3d-transforms">An introduction to CSS 3D transforms</a></li>
<li><a href="http://www.webkit.org/blog/386/3d-transforms/">3D transforms at surfin safari</a></li>
<li><a href="http://dev.w3.org/csswg/css3-3d-transforms/">The W3C draft spec</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/12/screencast-3d-css-rollovers-and-3d-css-tester/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Making the wait for the holidays easier &#8211; the MDN advent calendar</title>
		<link>http://hacks.mozilla.org/2011/11/making-the-wait-for-the-holidays-easier-the-mdn-advent-calendar/</link>
		<comments>http://hacks.mozilla.org/2011/11/making-the-wait-for-the-holidays-easier-the-mdn-advent-calendar/#comments</comments>
		<pubDate>Wed, 30 Nov 2011 12:36:55 +0000</pubDate>
		<dc:creator>Janet Swisher</dc:creator>
				<category><![CDATA[Demo]]></category>
		<category><![CDATA[MDN]]></category>
		<category><![CDATA[advent]]></category>
		<category><![CDATA[calendar]]></category>
		<category><![CDATA[mdn]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=10386</guid>
		<description><![CDATA[Tomorrow we will release the MDN advent calendar at http://thewebrocks.com/calendar with a daily link on a web technology product, a MDN wiki page or a great demo collected by us over the last few days. You can get a preview of how the calendar will look and work here: As an extra bonus, we thought [...]]]></description>
			<content:encoded><![CDATA[<p>Tomorrow we will release the MDN advent calendar at <a href="http://thewebrocks.com/calendar">http://thewebrocks.com/calendar</a> with a daily link on a web technology product, a MDN wiki page or a great demo collected by us over the last few days. You can get a preview of how the calendar will look and work here: </p>
<p><iframe src="http://www.screenr.com/embed/dbts" width="500" height="305" frameborder="0"></iframe> </p>
<p>As an extra bonus, we thought it would be fun to document the step-by-step development of the calendar and release it for you to re-use or get inspired by.</p>
<p>You can get  <a href="https://github.com/codepo8/calendar-tutorial">a white-labeled version of the calendar on GitHub</a> and there is a two-part step-by-step instruction how it was built available: <a href="http://christianheilmann.com/2011/11/29/building-an-advent-calendar-for-mozilla-in-phpjscss-part-1/">Part 1</a> &#8211; <a href="http://christianheilmann.com/2011/11/29/building-an-advent-calendar-for-mozilla-in-phpjscss-part-2/">Part 2</a>.</p>
<p>It is an example of how to build something server-side (so you can&#8217;t cheat by setting your operating system calendar ahead), enhance it with JavaScript and make it smooth by using CSS transitions. This should work for everybody and by playing each technology to its strengths, the code is very small indeed. </p>
<p>Enjoy, and see you tomorrow and the day after, and the day after that and&hellip;</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/11/making-the-wait-for-the-holidays-easier-the-mdn-advent-calendar/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML5 context menus in Firefox (Screencast and Code)</title>
		<link>http://hacks.mozilla.org/2011/11/html5-context-menus-in-firefox-screencast-and-code/</link>
		<comments>http://hacks.mozilla.org/2011/11/html5-context-menus-in-firefox-screencast-and-code/#comments</comments>
		<pubDate>Thu, 24 Nov 2011 09:28:04 +0000</pubDate>
		<dc:creator>Chris Heilmann</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[Firefox 8]]></category>
		<category><![CDATA[Firefox 9]]></category>
		<category><![CDATA[HTML5]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=10264</guid>
		<description><![CDATA[You may not know it, but the HTML5 specifications go beyond what we put in the pages and also define how parts of the browser should become available to developers with HTML, CSS and JavaScript. One of these parts of the specs are context menus, or &#8220;right click menus&#8221;. Using HTML5 and a menu element [...]]]></description>
			<content:encoded><![CDATA[<p>You may not know it, but the HTML5 specifications go beyond what we put in the pages and also define how parts of the browser should become available to developers with HTML, CSS and JavaScript. One of these parts of the specs are <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/interactive-elements.html#context-menus">context menus</a>, or &#8220;right click menus&#8221;. Using HTML5 and a menu element you can add new options to these without having to write a browser add-on. In Firefox 8 (the current one) we have support for those. See the following screencast for a <a href="http://thewebrocks.com/demos/context-menu">context menu demo</a>.</p>
<p><video controls width="100%" preload="none" poster="http://cf.cdn.vid.ly/8e1y1v/poster.jpg"><source src="http://cf.cdn.vid.ly/8e1y1v/mp4.mp4" type="video/mp4"><source src="http://cf.cdn.vid.ly/8e1y1v/webm.webm" type="video/webm"><source src="http://cf.cdn.vid.ly/8e1y1v/ogv.ogv" type="video/ogg"><a target='_blank' href='http://vid.ly/8e1y1v'><img src='http://cf.cdn.vid.ly/8e1y1v/poster.jpg' width="500"></a></video></p>
<p>The image example is pretty simple and was actually written by Paul Rouget as a demo <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=617528">in the original Firefox bug request</a>. The main core is the HTML of it:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;section</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;noninteractive&quot;</span> <span style="color: #000066;">contextmenu</span>=<span style="color: #ff0000;">&quot;imagemenu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;html5.png&quot;</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;HTML5&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;menudemo&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menu</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;context&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;imagemenu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menuitem</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;rotate&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;rotate()&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">icon</span>=<span style="color: #ff0000;">&quot;arrow_rotate_clockwise.png&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/menuitem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menuitem</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;resize&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;resize()&quot;</span></span>
<span style="color: #009900;">                <span style="color: #000066;">icon</span>=<span style="color: #ff0000;">&quot;image-resize.png&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/menuitem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menu</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;share&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menuitem</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;twitter&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;alert('not yet')&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/menuitem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menuitem</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;facebook&quot;</span> <span style="color: #000066;">onclick</span>=<span style="color: #ff0000;">&quot;alert('not yet')&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/menuitem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/menu<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/menu<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/section<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>As you can see you link the <code>menu</code> element to an element via its ID. The <code>contextmenu</code> attribute then points to this one. Each menu can have several <code>menuitems</code>. Each of those gets a textual label and a possible icon. You can also nest <code>menu</code> elements to create multiple layer menus. Here, we add inline <clode>onclick</clode> handlers to point to different JavaScript functions to call when the menu item gets activated. The resulting context menu looks like this:</p>
<p><img src="http://hacks.mozilla.org/wp-content/uploads/2011/11/contextmenu.png" alt="image with a context menu" title="a context menu" width="484" height="307" /></p>
<p>The functionality is simple, all the <code>rotate()</code> and <code>resize()</code> functions do is add class names to the image using <code>querySelector</code> and <code>classList</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> rotate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#menudemo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">classList</span>.<span style="color: #660066;">toggle</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'rotate'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> resize<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>   
  document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#menudemo'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">classList</span>.<span style="color: #660066;">toggle</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'resize'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The real effect is in CSS transforms and transitions. As the image has an ID of <code>menudemo</code> here is what is needed in CSS to rotate and resize:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#menudemo</span> <span style="color: #00AA00;">&#123;</span> 
  -moz-transition<span style="color: #00AA00;">:</span> 0.2s<span style="color: #00AA00;">;</span> 
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#menudemo</span><span style="color: #6666ff;">.rotate</span> <span style="color: #00AA00;">&#123;</span> 
  -moz-transform<span style="color: #00AA00;">:</span> rotate<span style="color: #00AA00;">&#40;</span>90deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#menudemo</span><span style="color: #6666ff;">.resize</span> <span style="color: #00AA00;">&#123;</span> 
  -moz-transform<span style="color: #00AA00;">:</span> scale<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0.7</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#menudemo</span><span style="color: #6666ff;">.resize</span><span style="color: #6666ff;">.rotate</span> <span style="color: #00AA00;">&#123;</span> 
  -moz-transform<span style="color: #00AA00;">:</span> scale<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0.7</span><span style="color: #00AA00;">&#41;</span> rotate<span style="color: #00AA00;">&#40;</span>90deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Notice that in a real product we should of course add the other browser prefixes and go prefix-less but as the functionality now only works in Firefox, this is enough for this demo.</p>
<h2>Detecting support and visual hinting</h2>
<p>Now, as this is extending the normal user offerings in the browser we need to make it obvious that there is a right-click menu available. In CSS3, there is a <code>context-menu</code> cursor available to us. When context menus are available, this should be shown:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.contextmenu</span> <span style="color: #cc00cc;">#menudemo</span><span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.contextmenu</span> <span style="color: #6666ff;">.demo</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">cursor</span><span style="color: #00AA00;">:</span> context-menu<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>We test the browser for support by checking for contextmenu on the body element and for <code>HTMLMenuItemElement</code> in the window (this has been added as a pull request to <a href="https://github.com/Modernizr/Modernizr">Modernizr</a>, too).</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'contextMenu'</span> <span style="color: #000066; font-weight: bold;">in</span> document.<span style="color: #660066;">body</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #3366CC;">'HTMLMenuItemElement'</span> <span style="color: #000066; font-weight: bold;">in</span> window<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  document.<span style="color: #660066;">documentElement</span>.<span style="color: #660066;">classList</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'contextmenu'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Wouldn&#8217;t <code>HTMLMenuItemElement</code> be enough? Yes, but a real context menu should only offer functionality when it is sensible, and that is where <code>contextMenu</code> comes in.</p>
<h2>Turning menuitems on and off depending on functionality</h2>
<p>As a slightly more complex example, let&#8217;s add a &#8220;count words&#8221; functionality to the document. For this, we generate a counter element that will become a tooltip when the words were counted:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> counter <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
counter.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'counter'</span><span style="color: #339933;">;</span>
counter.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'hide'</span><span style="color: #339933;">;</span>
document.<span style="color: #660066;">body</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>counter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
counter.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ev<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'hide'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This one is hidden by default and becomes visible when the <code>hide</code> class is removed. To make it smooth, we use a transition:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#counter</span><span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">0.7</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span><span style="color: #933;">.5em</span> <span style="color: #933;">1em</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span><span style="color: #993333;">bold</span><span style="color: #00AA00;">;</span>
  border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
  -moz-transition<span style="color: #00AA00;">:</span> opacity 0.4s<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #cc00cc;">#counter</span>.<span style="color: #993333;">hide</span><span style="color: #00AA00;">&#123;</span>
  opacity<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>We start with two sections with context menus:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;section</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;noninteractive&quot;</span> <span style="color: #000066;">contextmenu</span>=<span style="color: #ff0000;">&quot;countmenu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menu</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;context&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;countmenu&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menuitem</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;wordcount&quot;</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;count words&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/menuitem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/menu<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/section<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;section</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;interactive&quot;</span> <span style="color: #000066;">contextmenu</span>=<span style="color: #ff0000;">&quot;countmenuinteractive&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menu</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;context&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;countmenuinteractive&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;menuitem</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;wordcount&quot;</span> <span style="color: #000066;">label</span>=<span style="color: #ff0000;">&quot;count words&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/menuitem<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/menu<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/section<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>We then loop through all the <code>menuitems</code> with the class <code>wordcount</code> and apply the functionality.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> wordcountmenus <span style="color: #339933;">=</span> document.<span style="color: #660066;">querySelectorAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.wordcount'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    i <span style="color: #339933;">=</span> wordcountmenus.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  wordcountmenus<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ev<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// add functionality </span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We need to find out what has been selected in the page. We do this by using <code>getSelection()</code> and splitting its string version at whitespace. We then show the counter by removing the <code>hide</code> class name.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> wordcountmenus <span style="color: #339933;">=</span> document.<span style="color: #660066;">querySelectorAll</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.wordcount'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    i <span style="color: #339933;">=</span> wordcountmenus.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  wordcountmenus<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ev<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> document.<span style="color: #660066;">getSelection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        count <span style="color: #339933;">=</span> text.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\s/</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
    counter.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> count <span style="color: #339933;">+</span> <span style="color: #3366CC;">' words'</span><span style="color: #339933;">;</span>
    counter.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can see this in action in the second <a href="http://thewebrocks.com/demos/context-menu">context menu demo</a>. Now, the issue with this (as explained in the screencast) is that it always counts the words, regardless of the user having selected some text. What we want is the menu only to be active when there is text selected.</p>
<p><img src="http://hacks.mozilla.org/wp-content/uploads/2011/11/activeinactive.png" alt="context menu item available or not available depending on selection" title="interactive menu item" style="width:100%" /> </p>
<p>So in order to make our menu only become available when it makes sense we check if there is a selection in the document. Every context menu fires an event called <code>contextmenu</code> when it opens. So all we need to do is to subscribe to this event. </p>
<p>When something is selected in the document <code>document.getSelection().isCollapsed</code> is true. Otherwise it is false, so all we need to do is to enable or disable the menu item accordingly:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#interactive'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span>
  <span style="color: #3366CC;">'contextmenu'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ev<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.wordcount'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">disabled</span> <span style="color: #339933;">=</span>
    document.<span style="color: #660066;">getSelection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">isCollapsed</span><span style="color: #339933;">;</span>  
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The last thing to solve is the position of the mouse to position the counter element. As the menu selection event doesn&#8217;t give us the mouse position we need to add a <code>contextmenu</code> handler to the whole document that positions the counter invisibly behind the menu when it is opened:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">body</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span>
  <span style="color: #3366CC;">'contextmenu'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ev<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    counter.<span style="color: #660066;">style</span>.<span style="color: #660066;">left</span> <span style="color: #339933;">=</span> ev.<span style="color: #660066;">pageX</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #339933;">;</span>
    counter.<span style="color: #660066;">style</span>.<span style="color: #660066;">top</span> <span style="color: #339933;">=</span> ev.<span style="color: #660066;">pageY</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #339933;">;</span>
    counter.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'hide'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
<span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Further reading and resources</h2>
<ul>
<li><a href="http://thewebrocks.com/demos/context-menu">The full code demo</a></li>
<li><a href="https://bugzilla.mozilla.org/show_bug.cgi?id=617528">The Firefox bug request with lots of discussion</a></li>
<li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/interactive-elements.html#context-menus">Context Menus on the WHATWG</a></li>
<li><a href="https://plus.google.com/115133653231679625609/posts/CJMyExJTbug">A longer discussion on Google+ about context menus</a></li>
<li><a href="http://medialize.github.com/jQuery-contextMenu/demo/html5-polyfill-firefox8.html">A jQuery Polyfill for contextmenu</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/11/html5-context-menus-in-firefox-screencast-and-code/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
<enclosure url="http://cf.cdn.vid.ly/8e1y1v/mp4.mp4" length="9242123" type="video/mp4" />
<enclosure url="http://cf.cdn.vid.ly/8e1y1v/webm.webm" length="10110466" type="video/webm" />
<enclosure url="http://cf.cdn.vid.ly/8e1y1v/ogv.ogv" length="9682321" type="video/ogg" />
		</item>
		<item>
		<title>Screencast: CSS 3D rollover with fallback for older browsers</title>
		<link>http://hacks.mozilla.org/2011/11/screencast-css-3d-rollover-with-fallback-for-older-browsers/</link>
		<comments>http://hacks.mozilla.org/2011/11/screencast-css-3d-rollover-with-fallback-for-older-browsers/#comments</comments>
		<pubDate>Tue, 15 Nov 2011 09:06:10 +0000</pubDate>
		<dc:creator>Chris Heilmann</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[Screencast]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[a11y]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[effect]]></category>
		<category><![CDATA[rollover]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=10194</guid>
		<description><![CDATA[Here&#8217;s a quick screencast how to create a 3D image rollover and still give a useful interface to browsers that do not support 3D transforms. If you want to see the effect in Firefox get the latest Aurora or Nightly. Check the following video to see what it looks like (first with a browser without [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick screencast how to create a 3D image rollover and still give a useful interface to browsers that do not support 3D transforms. If you want to see the effect in Firefox get the latest Aurora or Nightly. Check the following video to see what it looks like (first with a browser without CSS 3D transport, then with a newer one):</p>
<p><video controls><source src="http://thewebrocks.com/demos/3D-photo-rollover/3dkitty.mp4" type="video/mp4"><source src="http://thewebrocks.com/demos/3D-photo-rollover/3dkitty.webm" type="video/webm"><a href="http://thewebrocks.com/demos/3D-photo-rollover.mp4">Download video, seems your browser doesn&#8217;t support <span class="caps">HTML5</span> video</a></video></p>
<p>The screencast is on YouTube:</p>
<p><iframe width="560" height="315" src="http://www.youtube.com/embed/c3wHB6mOMBs" frameborder="0" allowfullscreen></iframe></p>
<p>The main procedure to achieve the effect is simple. First we need a semantically valuable way to show these images, in our case a HTML list with figures and figcaptions. Notice that images still need an alternative text as the figcaption can apply to several images:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;li</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;image3d&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;figure<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;mittens.jpg&quot;</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;Mittens the cat&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;figcaption<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;strong<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Mittens<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/strong<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> loves to play with yarn and stuff.
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/figcaption<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/figure<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/li<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- repeated --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ul<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>We then position the caption absolutely inside the list item, give the list item dimensions and an overflow of hidden and move the caption outside of the visible space. When the user hovers over the list item (or focuses it with a keyboard) we move the caption to the right place and lower the opacity of the image.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.positioned</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">list-style</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">300px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">relative</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.positioned</span> figcaption <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> rgba<span style="color: #00AA00;">&#40;</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0.7</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span>
  border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">65px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">bottom</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">230px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.positioned</span> img <span style="color: #00AA00;">&#123;</span>
  opacity<span style="color: #00AA00;">:</span><span style="color: #cc66cc;">0.4</span> <span style="color: #00AA00;">;</span>    
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.positioned</span> figcaption strong <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">lime</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">normal</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span><span style="color: #933;">22px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.positioned</span> figcaption p <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">white</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.interactive</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.interactive</span> img <span style="color: #00AA00;">&#123;</span>
  opacity<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.interactive</span> figcaption <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">300px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>  
<span style="color: #6666ff;">.interactive</span><span style="color: #3333ff;">:hover </span>img<span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.interactive</span><span style="color: #3333ff;">:focus </span>img <span style="color: #00AA00;">&#123;</span>
  opacity<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0.4</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.interactive</span><span style="color: #3333ff;">:hover </span>figcaption<span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.interactive</span><span style="color: #3333ff;">:focus </span>figcaption <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">75px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>We make the effect smooth by adding transitions. Notice that it makes sense to list all the browser prefixes and set a prefix-less fallback. That way we don&#8217;t need to re-write code when a new browser supports them.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.smooth</span> img <span style="color: #00AA00;">&#123;</span>
  -webkit-transition<span style="color: #00AA00;">:</span> all 1s<span style="color: #00AA00;">;</span>
  -moz-transition<span style="color: #00AA00;">:</span>    all 1s<span style="color: #00AA00;">;</span>
  -o-transition<span style="color: #00AA00;">:</span>      all 1s<span style="color: #00AA00;">;</span>
  -ms-transition<span style="color: #00AA00;">:</span>     all 1s<span style="color: #00AA00;">;</span>
  transition<span style="color: #00AA00;">:</span>         all 1s<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.smooth</span> figcaption <span style="color: #00AA00;">&#123;</span>
  -webkit-transition<span style="color: #00AA00;">:</span> all 1s<span style="color: #00AA00;">;</span>
  -moz-transition<span style="color: #00AA00;">:</span>    all 1s<span style="color: #00AA00;">;</span>
  -ms-transition<span style="color: #00AA00;">:</span>     all 1s<span style="color: #00AA00;">;</span>
  -o-transition<span style="color: #00AA00;">:</span>      all 1s<span style="color: #00AA00;">;</span>
  transition<span style="color: #00AA00;">:</span>         all 1s<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>In order to rotate the kitty in 3D space, we need to give the list item a perspective and rotate the image and shift it in 3D space with translate3D to avoid clipping:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.threed</span> <span style="color: #00AA00;">&#123;</span>
  -webkit-perspective<span style="color: #00AA00;">:</span> <span style="color: #933;">800px</span><span style="color: #00AA00;">;</span>
  -moz-perspective<span style="color: #00AA00;">:</span>    <span style="color: #933;">800px</span><span style="color: #00AA00;">;</span>
  -ms-perspective<span style="color: #00AA00;">:</span>     <span style="color: #933;">800px</span><span style="color: #00AA00;">;</span>
  -o-perspective<span style="color: #00AA00;">:</span>      <span style="color: #933;">800px</span><span style="color: #00AA00;">;</span>
  perspective<span style="color: #00AA00;">:</span>         <span style="color: #933;">800px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.threed</span><span style="color: #3333ff;">:hover </span>img<span style="color: #00AA00;">,</span> <span style="color: #6666ff;">.threed</span><span style="color: #3333ff;">:focus </span>img <span style="color: #00AA00;">&#123;</span>
  -webkit-transform<span style="color: #00AA00;">:</span> rotateY<span style="color: #00AA00;">&#40;</span> 50deg <span style="color: #00AA00;">&#41;</span> rotateX<span style="color: #00AA00;">&#40;</span> 10deg <span style="color: #00AA00;">&#41;</span>
                     translate3d<span style="color: #00AA00;">&#40;</span> <span style="color: #933;">80px</span><span style="color: #00AA00;">,</span> <span style="color: #933;">-20px</span><span style="color: #00AA00;">,</span> <span style="color: #933;">-100px</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  -moz-transform<span style="color: #00AA00;">:</span>    rotateY<span style="color: #00AA00;">&#40;</span> 50deg <span style="color: #00AA00;">&#41;</span> rotateX<span style="color: #00AA00;">&#40;</span> 10deg <span style="color: #00AA00;">&#41;</span>
                     translate3d<span style="color: #00AA00;">&#40;</span> <span style="color: #933;">80px</span><span style="color: #00AA00;">,</span> <span style="color: #933;">-20px</span><span style="color: #00AA00;">,</span> <span style="color: #933;">-100px</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  -o-transform<span style="color: #00AA00;">:</span>      rotateY<span style="color: #00AA00;">&#40;</span> 50deg <span style="color: #00AA00;">&#41;</span> rotateX<span style="color: #00AA00;">&#40;</span> 10deg <span style="color: #00AA00;">&#41;</span>
                     translate3d<span style="color: #00AA00;">&#40;</span> <span style="color: #933;">80px</span><span style="color: #00AA00;">,</span> <span style="color: #933;">-20px</span><span style="color: #00AA00;">,</span> <span style="color: #933;">-100px</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  -ms-transform<span style="color: #00AA00;">:</span>     rotateY<span style="color: #00AA00;">&#40;</span> 50deg <span style="color: #00AA00;">&#41;</span> rotateX<span style="color: #00AA00;">&#40;</span> 10deg <span style="color: #00AA00;">&#41;</span>
                     translate3d<span style="color: #00AA00;">&#40;</span> <span style="color: #933;">80px</span><span style="color: #00AA00;">,</span> <span style="color: #933;">-20px</span><span style="color: #00AA00;">,</span> <span style="color: #933;">-100px</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  transform<span style="color: #00AA00;">:</span>         rotateY<span style="color: #00AA00;">&#40;</span> 50deg <span style="color: #00AA00;">&#41;</span> rotateX<span style="color: #00AA00;">&#40;</span> 10deg <span style="color: #00AA00;">&#41;</span>
                     translate3d<span style="color: #00AA00;">&#40;</span> <span style="color: #933;">80px</span><span style="color: #00AA00;">,</span> <span style="color: #933;">-20px</span><span style="color: #00AA00;">,</span> <span style="color: #933;">-100px</span> <span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>That&#8217;s it. By adding all the browser prefixes, falling back to a simple rollover and by making sure you do the effect on hover and on focus you support all the browsers and bring the cool to the newest ones. </p>
<p>More reading and resources:</p>
<ul>
<li><a href="http://thewebrocks.com/demos/3D-photo-rollover/">Standalone example</a></li>
<li><a href="http://thewebrocks.com/demos/3D-photo-rollover/steps.html">Step-by-step example</a></li>
<li><a href="https://github.com/codepo8/3D-photo-rollover">GitHub source</a></li>
<li><a href="http://christianheilmann.com/2011/11/14/fun-with-3d-transforms-and-rollovers-kittens-in-space/">Detailed blog post</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/11/screencast-css-3d-rollover-with-fallback-for-older-browsers/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
<enclosure url="http://thewebrocks.com/demos/3D-photo-rollover/3dkitty.mp4" length="491259" type="video/mp4" />
<enclosure url="http://thewebrocks.com/demos/3D-photo-rollover/3dkitty.webm" length="404464" type="video/webm" />
<enclosure url="http://thewebrocks.com/demos/3D-photo-rollover.mp4" length="0" type="video/mp4" />
		</item>
		<item>
		<title>Screencast: BrowserID login flow on OpenPhoto.me</title>
		<link>http://hacks.mozilla.org/2011/11/screencast-browserid-login-flow-on-openphoto-me/</link>
		<comments>http://hacks.mozilla.org/2011/11/screencast-browserid-login-flow-on-openphoto-me/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 01:03:49 +0000</pubDate>
		<dc:creator>Austin King</dc:creator>
				<category><![CDATA[BrowserID]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[Featured Demo]]></category>
		<category><![CDATA[Screencast]]></category>
		<category><![CDATA[BrowseriD]]></category>
		<category><![CDATA[OpenPhoto]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=10105</guid>
		<description><![CDATA[BrowserID is an initiative to provide the web with a better way to sign in. The web is a connected collection of resources and you should not have to have a user name and password for each of them when you could use the web instead. Today we show you a screencast of how easy [...]]]></description>
			<content:encoded><![CDATA[<p><a href="https://browserid.org/">BrowserID</a> is an initiative to provide the web with a better way to sign in. The web is a connected collection of resources and you should not have to have a user name and password for each of them when you could use the web instead. </p>
<p>Today we show you a screencast of how easy BrowserID makes it to login to a web site. For this, we&#8217;ll look at how the login flow for an existing BrowserID user works the first time they log in on a new website. Our example website is <a href="http://current.openphoto.me/">OpenPhoto</a>, a hot new photo sharing app that keeps users in control of their data.</p>
<h3>Screencast of logging into OpenPhoto with BrowserID</h3>
<p><video controls preload="none" style="width:100%;height:300px;" poster="http://cf.cdn.vid.ly/1l5i5m/poster.jpg"><source src="http://cf.cdn.vid.ly/1l5i5m/mp4.mp4" type="video/mp4"><source src="http://cf.cdn.vid.ly/1l5i5m/webm.webm" type="video/webm"><source src="http://cf.cdn.vid.ly/1l5i5m/ogv.ogv" type="video/ogg"><a target='_blank' href='http://vid.ly/1l5i5m'><img   src='http://cf.cdn.vid.ly/1l5i5m/poster.jpg' width="500"></a></video></p>
<h3>Get involved:</h3>
<p>BrowserID needs your help to grow and become a weapon of choice in the fight against insecure and annoying login systems. The great thing is that now is the time where you can be part of this.</p>
<ul>
<li><a href="https://github.com/mozilla/browserid/wiki/How-to-Use-BrowserID-on-Your-Site">Implement BrowserID</a></li>
<li><a href="http://theopenphotoproject.org/">Install OpenPhoto</a></li>
<li><a href="http://mozilla.github.com/browserid-field-guide/toc.html">Contribute to the Field Guide</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/11/screencast-browserid-login-flow-on-openphoto-me/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
<enclosure url="http://cf.cdn.vid.ly/1l5i5m/mp4.mp4" length="16057575" type="video/mp4" />
<enclosure url="http://cf.cdn.vid.ly/1l5i5m/webm.webm" length="16954384" type="video/webm" />
<enclosure url="http://cf.cdn.vid.ly/1l5i5m/ogv.ogv" length="15170781" type="video/ogg" />
		</item>
		<item>
		<title>CSS 3D transformations in Firefox Nightly</title>
		<link>http://hacks.mozilla.org/2011/10/css-3d-transformations-in-firefox-nightly/</link>
		<comments>http://hacks.mozilla.org/2011/10/css-3d-transformations-in-firefox-nightly/#comments</comments>
		<pubDate>Wed, 26 Oct 2011 20:18:42 +0000</pubDate>
		<dc:creator>Chris Heilmann</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[Featured Demo]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Standards]]></category>
		<category><![CDATA[Web Developers]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=10017</guid>
		<description><![CDATA[When the first 3D transformations in CSS got support on Webkit browsers people got incredibly excited about them. Now that they have matured we also support 3D CSS in Firefox. To see it for yourself, check out one of the latest nightly builds. You can see them in action in this demo of a rotating [...]]]></description>
			<content:encoded><![CDATA[<p>When the first 3D transformations in CSS got support on Webkit browsers people got incredibly excited about them. Now <a href="http://dev.w3.org/csswg/css3-3d-transforms/">that they have matured</a> we also support 3D CSS in Firefox. To see it for yourself, check out <a href="http://nightly.mozilla.org">one of the latest nightly builds</a>. </p>
<p>You can see them in action in <a href="http://thewebrocks.com/demos/html5-3d-css">this demo of a rotating HTML5 logo</a> and the screencast below:</p>
<p><video controls preload="none" style="width:100%;height:300px;" poster="http://cf.cdn.vid.ly/5l3j6a/poster.jpg"><source src="http://cf.cdn.vid.ly/5l3j6a/mp4.mp4" type="video/mp4"><source src="http://cf.cdn.vid.ly/5l3j6a/webm.webm" type="video/webm"><source src="http://cf.cdn.vid.ly/5l3j6a/ogv.ogv" type="video/ogg"><a target='_blank' href='http://vid.ly/5l3j6a'><img   src='http://cf.cdn.vid.ly/5l3j6a/poster.jpg' width="500"></a></video></p>
<p>This means now that we need your support in trying out CSS 3D examples in Firefox and add other extensions than <code>-webkit-</code> to your CSS 3D products and demos. To show that this is possible, we took the well-known <a href="http://www.webkit.org/blog-files/3d-transforms/poster-circle.html">webkit-only &#8220;poster circle&#8221; demo</a> and made it work with Firefox nightly by adding the -moz- (and of course the other prefixes and one set of instructions without browser prefixes). Here is a slight excerpt:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">-webkit-transform-style<span style="color: #00AA00;">:</span> preserve-3d<span style="color: #00AA00;">;</span>
-moz-transform-style<span style="color: #00AA00;">:</span> preserve-3d<span style="color: #00AA00;">;</span>
-o-transform-style<span style="color: #00AA00;">:</span> preserve-3d<span style="color: #00AA00;">;</span>
-ms-transform-style<span style="color: #00AA00;">:</span> preserve-3d<span style="color: #00AA00;">;</span>
transform-style<span style="color: #00AA00;">:</span> preserve-3d<span style="color: #00AA00;">;</span></pre></div></div>

<p>You can see this in action in the screencast below alongside Chrome and you <a href="http://thewebrocks.com/demos/postercircle/">try the demo out yourself</a>. The slight jerkiness is actually my MacBook Air impersonating a starting jet every time I use ScreenFlow and not the browser.</p>
<p><video controls preload="none" style="width:100%;height:300px;" poster="http://cf.cdn.vid.ly/4m8d9f/poster.jpg"><source src="http://cf.cdn.vid.ly/4m8d9f/mp4.mp4" type="video/mp4"><source src="http://cf.cdn.vid.ly/4m8d9f/webm.webm" type="video/webm"><source src="http://cf.cdn.vid.ly/4m8d9f/ogv.ogv" type="video/ogg"><a target='_blank' href='http://vid.ly/4m8d9f'><img   src='http://cf.cdn.vid.ly/4m8d9f/poster.jpg' width="500"></a></video></p>
<p>To celebrate the release and to show how CSS 3D can be applied as subtle effect, have a <a href="http://thewebrocks.com/demos/browsermemory/">game of pairs using your favourite browsers (and a cat)</a> : </p>
<p><a href="http://thewebrocks.com/demos/browsermemory/"><img src="http://farm7.static.flickr.com/6112/6283741113_b3604d4ee5_o.jpg" alt="browser pairs"></a></p>
<p>Oleg Romashin also spent some time to <a href="http://romaxa.bolshe.net/css3d/">convert a few CSS 3D demos to work with Mozilla</a> and you can <a href="http://m8y.org/tmp/cubiq.org/dropbox/3dcity/index2.html">check the 3D city</a> for more &#8220;wow&#8221;.</p>
<p>If you are new to CSS 3D transformations <a href="http://24ways.org/2010/intro-to-css-3d-transforms">here&#8217;s a good beginner course</a> and <a href="http://www.westciv.com/tools/3Dtransforms/index.html">a tool to create them</a>. </p>
<p>The rotating HTML5 logo demo also shows how you can check if the currently used browser supports 3D transforms. Instead of repeating the animation frames for all the prefixes we test in JavaScript and create the CSS on the fly:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> checksupport<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> props <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'perspectiveProperty'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'WebkitPerspective'</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">'MozPerspective'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'OPerspective'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'msPerspective'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
      i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
      support <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>props<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>props<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #000066; font-weight: bold;">in</span> form.<span style="color: #660066;">style</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      support <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
      pfx <span style="color: #339933;">=</span> props<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Perspective'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      pfx <span style="color: #339933;">=</span> pfx.<span style="color: #660066;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    i<span style="color: #339933;">++;</span>
  <span style="color: #009900;">&#125;</span> 
  <span style="color: #000066; font-weight: bold;">return</span> support<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>checksupport<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
  styles <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'style'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  s <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'#stage{-'</span><span style="color: #339933;">+</span> pfx <span style="color: #339933;">+</span><span style="color: #3366CC;">'-perspective: 300px;}'</span><span style="color: #339933;">+</span>
       <span style="color: #3366CC;">'#logo{-'</span><span style="color: #339933;">+</span> pfx <span style="color: #339933;">+</span><span style="color: #3366CC;">'-transform-style: preserve-3d;position:relative;}'</span><span style="color: #339933;">+</span>  
       <span style="color: #3366CC;">'#logo.spin{-'</span><span style="color: #339933;">+</span> pfx <span style="color: #339933;">+</span><span style="color: #3366CC;">'-animation: spin 3s infinite linear;}'</span><span style="color: #339933;">+</span>
       <span style="color: #3366CC;">'@-'</span><span style="color: #339933;">+</span> pfx <span style="color: #339933;">+</span><span style="color: #3366CC;">'-keyframes spin {'</span><span style="color: #339933;">+</span>  
       <span style="color: #3366CC;">'0% {'</span><span style="color: #339933;">+</span> 
       <span style="color: #3366CC;">'-'</span><span style="color: #339933;">+</span> pfx <span style="color: #339933;">+</span><span style="color: #3366CC;">'-transform: rotateX(0deg) rotateY(0deg) rotateZ(0deg);'</span><span style="color: #339933;">+</span>   
       <span style="color: #3366CC;">'}'</span><span style="color: #339933;">+</span>
       <span style="color: #3366CC;">'100% {'</span><span style="color: #339933;">+</span>   
       <span style="color: #3366CC;">'-'</span><span style="color: #339933;">+</span> pfx <span style="color: #339933;">+</span><span style="color: #3366CC;">'-transform: rotateX(0deg) rotateY(360deg)'</span><span style="color: #339933;">+</span>
       <span style="color: #3366CC;">' rotateZ(360deg);'</span><span style="color: #339933;">+</span> 
       <span style="color: #3366CC;">'}}'</span><span style="color: #339933;">;</span>
  styles.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> s<span style="color: #339933;">;</span>
  document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'head'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>styles<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For more information on creating your own pages that use 3D transformations, take a look at the <a href="http://dev.w3.org/csswg/css3-3d-transforms/">draft specification</a></p>
<p>As always, If you find any bugs, please report them at <a href="https://bugzilla.mozilla.org">bugzilla.mozilla.org</a>!</p>
<p>So please reward our hard work bringing the third dimension to Firefox&#8217;s CSS engine by supporting and testing. Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/10/css-3d-transformations-in-firefox-nightly/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
<enclosure url="http://cf.cdn.vid.ly/5l3j6a/mp4.mp4" length="6843288" type="video/mp4" />
<enclosure url="http://cf.cdn.vid.ly/5l3j6a/webm.webm" length="6684672" type="video/webm" />
<enclosure url="http://cf.cdn.vid.ly/5l3j6a/ogv.ogv" length="6626659" type="video/ogg" />
<enclosure url="http://cf.cdn.vid.ly/4m8d9f/mp4.mp4" length="6138097" type="video/mp4" />
<enclosure url="http://cf.cdn.vid.ly/4m8d9f/webm.webm" length="6887958" type="video/webm" />
<enclosure url="http://cf.cdn.vid.ly/4m8d9f/ogv.ogv" length="13626193" type="video/ogg" />
		</item>
		<item>
		<title>Debugging and editing webpages in 3D</title>
		<link>http://hacks.mozilla.org/2011/10/debugging-and-editing-webpages-in-3d/</link>
		<comments>http://hacks.mozilla.org/2011/10/debugging-and-editing-webpages-in-3d/#comments</comments>
		<pubDate>Wed, 26 Oct 2011 14:47:09 +0000</pubDate>
		<dc:creator>victor</dc:creator>
				<category><![CDATA[Add-ons]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[Developer Tools]]></category>
		<category><![CDATA[WebGL]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=9896</guid>
		<description><![CDATA[Tilt is a Firefox addon that lets you visualize any web page in 3D. A new update is available, coming with more developer-oriented features. Try the addon. Since the first alpha version of Tilt was announced (a Firefox extension focused on creating a 3D visualization of a webpage), a lot of work has been done [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Tilt</strong> is a Firefox addon that lets you visualize any web page in 3D. A new update is available, coming with more developer-oriented features. Try the <a href="#availableasanaddon">addon</a>.</em></p>
<p><object width="500" height="375"><param name="movie" value="http://www.youtube.com/v/_7eG_PONHRw?version=3&#038;feature=oembed"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/_7eG_PONHRw?version=3&#038;feature=oembed" type="application/x-shockwave-flash" width="500" height="375" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>Since the <a href="http://hacks.mozilla.org/2011/07/tilt-visualize-your-web-page-in-3d/">first alpha version of Tilt was announced</a> (a Firefox extension focused on creating a 3D visualization of a webpage), a lot of work has been done to add a great number of developer-oriented features. These focus on debugging the structure of a webpage, inspecting styling and attributes for each node and seamlessly refreshing the visualization when the DOM structure changes or after contents of document are repainted.</p>
<h2>Solve nesting problems</h2>
<p>Tilt is useful when searching problems in the HTML structure (like finding unclosed DIV elements for example) by providing the extra third dimension, layering each node based on nesting in the DOM tree. Stacks of elements visually represent branches in the DOM, and each node can be inspected for the inner HTML contents, its computed CSS style and the attributes.</p>
<p>Clicking anywhere on the visualization highlights a color-coded rectangle surrounding the corresponding node. Double click shows up the source preview for that node. Tilt also tries to show the most relevant information when needed (one is most likely to inspect the attributes of an input, button or image element, for example, but can easily switch between HTML, CSS and attributes view at any time).</p>
<p><a href="http://hacks.mozilla.org/wp-content/uploads/2011/10/tilt3d2.png"><img class="aligncenter size-full wp-image-9911" src="http://hacks.mozilla.org/wp-content/uploads/2011/10/tilt3d2.png" alt="" width="500" height="312" /></a></p>
<h2>Minidom map</h2>
<p>The “minidom” is a tree view representation showing a minimalistic snapshot of the document object model. Each node is assigned a color associated by tag name (blue for div, green for span etc.) and represented as a strip, along with visual markers for the id and/or class if available. Each one of these strips also has a width relative to the type, id and class name length for the respective element, and the corresponding 3D stack in the visualization has color-coded margins. The coloring for individual elements is easily changeable using the color picker near to the minidom legend.</p>
<p>Clicking a strip in the tree view (or directly a stack on the 3D document visualization mesh) also highlights the node with a colored quad. This behavior is a good way to relate with the Style Inspector, and a more unified interaction between Tilt and other Developer Tools is planned in the future. All of these additions make it easier to analyze the bounds of each node, along with the HTML, computed CSS and attributes.</p>
<h2>Realtime editing</h2>
<p>Because Tilt is able to detect when a webpage’s DOM structure changes or when a repaint is necessary, integration is seamless with existing Developer Tools. Using Tilt and Firebug or Style Editor at the same time is easy. One can enable or disable CSS properties, changing the style of a node, and the visualization changes accordingly.</p>
<p><object width="500" height="375"><param name="movie" value="http://www.youtube.com/v/ae1p5W20Ug8?version=3&#038;feature=oembed"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/ae1p5W20Ug8?version=3&#038;feature=oembed" type="application/x-shockwave-flash" width="500" height="375" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><em>To enable realtime updates for the 3D webpage, go to the Options menu and check “Refresh visualization”.</em></p>
<h2>Useful for learning</h2>
<p>Developer tools such as &#8220;view source&#8221; have always been used to help people learn about web development. The 3D view highlights the structure of a page better than a flat view, thus anyone can immediately understand the parent-child relationship between nodes in a webpage, their positioning and how the layout is influenced.</p>
<p>One use case for this is the Hackasaurus mashup. The <a href="http://hackasaurus.org/goggles/" target="_blank">X-Ray Goggles</a> is a nice and fun tool designed to make it easier to learn about the different document node types, the “building blocks” which create a webpage.</p>
<h2>Export</h2>
<p>A requested feature was the ability to export the visualization as a 3D mesh, to be used in games or other 3D editors. Tilt adds the ability to export to <em>.obj</em>, along with a material <em>.mtl</em> file and a <em>.png</em> texture (a screenshot of the entire webpage). The open <em>.obj</em> format ensures the fact that the mesh can be opened with almost any editor. Here’s a ray-traced rendering of <a href="http://hacks.mozilla.org/2011/07/tilt-visualize-your-web-page-in-3d/" target="_blank">hacks.mozilla.org</a> in <a href="http://www.blender.org/" target="_blank">Blender</a>:</p>
<p style="text-align: center"><a href="http://hacks.mozilla.org/wp-content/uploads/2011/10/tilt3d3.png"><img class="aligncenter size-full wp-image-9927" src="http://hacks.mozilla.org/wp-content/uploads/2011/10/tilt3d3.png" alt="" width="500" height="305" /></a></p>
<h2>Fun with experiments</h2>
<p>As soon as it was released, many people found clever and interesting alternative ways to interact with Tilt. One experiment was creating a 3D visualization of an image, by exporting chunks of pixels to a HTML representation. The result was a voxel-like representation, with node blocks and stacks instead of pixels. A simple <a href="http://tinyurl.com/Img2Tilt" target="_blank">Image2Tilt converter</a> was written in JavaScript, and you can try it directly in the browser.</p>
<p><object width="500" height="375"><param name="movie" value="http://www.youtube.com/v/7YXq4gylERE?version=3&#038;feature=oembed"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/7YXq4gylERE?version=3&#038;feature=oembed" type="application/x-shockwave-flash" width="500" height="375" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>Accelerometer support was another addition based on community request. This shows how easy it is to add functionality that wasn’t originally planned.</p>
<p><object width="500" height="281"><param name="movie" value="http://www.youtube.com/v/rbTLwVEfPn0?version=3&#038;feature=oembed"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/rbTLwVEfPn0?version=3&#038;feature=oembed" type="application/x-shockwave-flash" width="500" height="281" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>You can view the source code, fork it and also contribute to the addon with ideas or feature requests on Github, at <a href="https://github.com/victorporof/Tilt" target="_blank">github.com/victorporof/Tilt</a>.</p>
<h2><a name="availableasanaddon"></a>Available as an addon</h2>
<p>The latest version of <a href="https://github.com/victorporof/Tilt/raw/master/bin/Tilt.xpi" target="_blank">Tilt</a> can be found on <a href="https://github.com/victorporof/Tilt/raw/master/bin/Tilt.xpi" target="_blank">Github</a>, but you can also download Tilt as an <a href="https://addons.mozilla.org/en-US/firefox/addon/tilt/" target="_blank">addon from addons.mozilla.org</a>.</p>
<p>For compatibility, Tilt requires WebGL capabilities. Go to <a href="http://get.webgl.org/" target="_blank">get.webgl.org</a> to check availability and troubleshoot any issues. The current version works with Firefox 6.0 to latest <a href="http://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/latest-trunk/" target="_blank">10.0 Nightly releases</a> (latest Nightly builds now also support WebGL anti-aliasing, working great with Tilt).</p>
<p>To start Tilt, hit <em>Control+Shift+M</em> (or <em>Command+Shift+M</em> if you&#8217;re on Mac OS), or go to <strong>Web Developer -&gt; Tilt</strong>, available in the Firefox application menu (or the Tools menu on Mac OS). You can modify this hotkey (and other properties) from the Options menu after starting Tilt.</p>
<p>More information about Tilt, the development process and milestone updates can be found on <a href="http://blog.mozilla.com/tilt">blog.mozilla.com/tilt</a>.</p>
<h2>Future</h2>
<p>Tilt has become an active Developer Tools project, and an ongoing effort is made to integrate it with other existing tools like Style Inspector and Style Editor (<a href="https://github.com/neonux/StyleEditor" target="_blank">source code</a> and <a href="http://neonux.com/StyleEditor/builds/" target="_blank">latest builds</a>). As the 3D view of a webpage has proven to be useful for debugging, this main functionality will gradually become part of Firefox in future releases.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/10/debugging-and-editing-webpages-in-3d/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Geolocation explained &#8211; a quick screencast</title>
		<link>http://hacks.mozilla.org/2011/09/geolocation-explained-a-quick-screencast/</link>
		<comments>http://hacks.mozilla.org/2011/09/geolocation-explained-a-quick-screencast/#comments</comments>
		<pubDate>Tue, 13 Sep 2011 14:54:37 +0000</pubDate>
		<dc:creator>Chris Heilmann</dc:creator>
				<category><![CDATA[Demo]]></category>
		<category><![CDATA[Dev Derby]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=9531</guid>
		<description><![CDATA[If you&#8217;ve been here last week, you might have seen the webinar and geolocation Q&#038;A with Remy Sharp. Sadly enough, we had a problem recording the screen so we recorded this replacement screencast yesterday night to give you a quick introduction to the Geolocation API. Once you are up to speed (or refreshed your memory) [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve been here last week, you might have seen the <a href="http://hacks.mozilla.org/2011/09/webinar-geolocation-with-remy-sharp/">webinar and geolocation Q&#038;A with Remy Sharp</a>. Sadly enough, we had a problem recording the screen so we recorded this replacement screencast yesterday night to give you a quick introduction to the Geolocation API.</p>
<p>Once you are up to speed (or refreshed your memory) why not go and <a href="https://developer.mozilla.org/en-US/demos/devderby">build something for this month&#8217;s Developer Derby</a>?</p>
<p>You can <a href="http://vid.ly/6b3p7k">see the video on any HTML5 enabled device here</a> (courtesy of vid.ly).</p>
<p><video controls style="width:100%;margin-bottom:40px;"><source src="http://cf.cdn.vid.ly/6b3p7k/mp4.mp4" type="video/mp4"><source src="http://cf.cdn.vid.ly/6b3p7k/webm.webm" type="video/webm"><source src="http://cf.cdn.vid.ly/6b3p7k/ogv.ogv" type="video/ogg"><a target='_blank' href='http://vid.ly/6b3p7k'><img src='http://cf.cdn.vid.ly/6b3p7k/poster.jpg' width="500"></a></video></p>
<ul>
<li><a href="http://isithackday.com/hacks/geo/check/">The simple geolocation example shown in the screencast</a></li>
<li><a href="http://dev.w3.org/geo/api/spec-source.html">The W3C geolocation specs</li>
<li><a href="https://gist.github.com/366184">A polyfill to add support to older browsers</a></li>
<li><a href="http://isithackday.com/hacks/geo/distance.php">The distance between your IP location and the browser geolocation</a></li>
<li><a href="https://developer.mozilla.org/en-US/demos/detail/things-around-you">The &#8220;things around you&#8221; demo shown in the screencast</a></li>
</ul>
<p><script type="text/javascript" src="http://s3.www.universalsubtitles.org/js/mirosubs-widgetizer.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/09/geolocation-explained-a-quick-screencast/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
<enclosure url="http://cf.cdn.vid.ly/6b3p7k/webm.webm" length="56202099" type="video/webm" />
<enclosure url="http://cf.cdn.vid.ly/6b3p7k/mp4.mp4" length="48373237" type="video/mp4" />
<enclosure url="http://cf.cdn.vid.ly/6b3p7k/ogv.ogv" length="51449032" type="video/ogg" />
		</item>
		<item>
		<title>Mozilla demoparty winners announced</title>
		<link>http://hacks.mozilla.org/2011/09/mozilla-demoparty-winners-announced/</link>
		<comments>http://hacks.mozilla.org/2011/09/mozilla-demoparty-winners-announced/#comments</comments>
		<pubDate>Sat, 03 Sep 2011 16:42:19 +0000</pubDate>
		<dc:creator>Chris Heilmann</dc:creator>
				<category><![CDATA[Demo]]></category>
		<category><![CDATA[Mozilla]]></category>
		<category><![CDATA[WebGL]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=9448</guid>
		<description><![CDATA[The Demoparty Online Competition 2011 is part of the Mozilla Labs Demoparty Project, an initiative to foster artful exploration of open web technologies. We asked people from the demo scene to have a go at web technologies and (with WebGL being the absolute winner of course) managed to collect over 100 submissions. Now the judges [...]]]></description>
			<content:encoded><![CDATA[<p>The Demoparty Online Competition 2011 is part of the <a href="https://demoparty.mozillalabs.com/">Mozilla Labs Demoparty Project</a>, an initiative to foster artful exploration of open web technologies.</p>
<p>We asked people from the demo scene to have a go at web technologies and (with WebGL being the absolute winner of course) managed to <a href="https://demoparty.mozillalabs.com/gallery/">collect over 100 submissions</a>. Now the judges have spoken and we <a href="http://mozillalabs.com/blog/2011/09/we-got-the-winners">picked the winners</a> in the categories of Main Demo, Single Effect, Audio Demo, Animated GIF and pure CSS demo.</p>
<p>Amongst other great examples of using technology in a purely creative way unhindered by real life application needs here are the winners of Demo and Single Effect:</p>
<p>Main Demo: Akemi</p>
<p><a href="https://demoparty.mozillalabs.com/gallery/51/akemi"><img class="alignnone size-full wp-image-7220" style="margin-left: 0px!important" src="http://mozillalabs.com/files/2011/09/akemi_main_demo_winner.jpg" alt="" width="602" height="360" /></a></p>
<p>Single Effect: WebGL Water Simulation </p>
<p><a href="https://demoparty.mozillalabs.com/gallery/81/webgl-water">Demo Link</a> or screen capture:<br />
<iframe width="480" height="390" src="http://www.youtube.com/embed/R0O_9bp3EKQ?rel=0" frameborder="0" allowfullscreen></iframe></p>
<p>We congratulate all the winners and thank all those who contributed. Playing with technology is a big part of making it interesting for everyone to use.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/09/mozilla-demoparty-winners-announced/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Calculated drop shadows in HTML5 canvas</title>
		<link>http://hacks.mozilla.org/2011/08/calculated-drop-shadows-in-html5-canvas/</link>
		<comments>http://hacks.mozilla.org/2011/08/calculated-drop-shadows-in-html5-canvas/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 10:09:48 +0000</pubDate>
		<dc:creator>Chris Heilmann</dc:creator>
				<category><![CDATA[Animations]]></category>
		<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[Demo Studio]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[MDN]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[dropshadow]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=9381</guid>
		<description><![CDATA[One of the best new features of HTML5 when it comes to visual effects is the canvas element and its API. On the surface, it doesn&#8217;t look like much &#8211; just a rectangle in the page you can paint on and wipe. Much like an etch-a-sketch. However, the ability to transform, rotate and scale its [...]]]></description>
			<content:encoded><![CDATA[<p>One of the best new features of HTML5 when it comes to visual effects is the canvas element and its API. On the surface, it doesn&#8217;t look like much &#8211; just a rectangle in the page you can paint on and wipe. Much like an etch-a-sketch. However, the ability to transform, rotate and scale its coordinate system is very powerful indeed once you master it. </p>
<p>Today I want to quickly show how you can do (well simulate) something rather complex with it, like a calculated drop shadow on an element. To see what I mean with this, check out the following demo which is <a href="https://developer.mozilla.org/en-US/demos/detail/calculated-shadow/launch">also available on the Demo Studio</a>:     </p>
<p><iframe style="width: 100%; height: 350px" src="http://jsfiddle.net/codepo8/wNrfU/embedded/result,js,html,css/"></iframe><br />
<a href="http://jsfiddle.net/codepo8/wNrfU/">See animated version on JSFiddle</a></p>
<p>(This is using JSFiddle to show you the demos, so you can click the different tabs to see the JavaScript and CSS needed for the effects. All of the demos are also <a href="https://github.com/codepo8/calculated-shadow">available on GitHub</a>.)    </p>
<p>As you can see, the shadow becomes more blurred and less pronounced the further away the &#8220;sun&#8221; is from it. You can use the mouse to see the effect in the following demo:</p>
<p><iframe style="width: 100%; height: 350px" src="http://jsfiddle.net/codepo8/XJcpg/1/embedded/result,js,html,css/"></iframe><br />
<a href="http://jsfiddle.net/codepo8/XJcpg/1">See mouse enabled demo on JSFiddle</a></p>
<p>Let&#8217;s take a look how that is done. The first step is to have a canvas we can paint on &#8211; you do this simply by having a mouse detection script (which we used for years and years) and a canvas with access to its API: </p>
<p><iframe style="width: 100%; height: 350px" src="http://jsfiddle.net/codepo8/deePU/1/embedded/js,result,html,css/"></iframe><br />
<a href="http://jsfiddle.net/codepo8/deePU/1">See step one on JSFiddle</a></p>
<p>Click the play button of the above example and you see that you can paint on the canvas. However, the issue is that you keep painting on the canvas instead of only having the orb follow the cursor. To do this, we need to wipe the canvas every time the mouse moves. You do this with <code>clearRect()</code></p>
<p><iframe style="width: 100%; height: 350px" src="http://jsfiddle.net/codepo8/z9Z8T/embedded/js,result,html,css/"></iframe><br />
<a href="http://jsfiddle.net/codepo8/z9Z8T">See step two on JSFiddle</a></p>
<p>Running the above example now shows that the orb moves with the mouse. Cool, so this is going to be our &#8220;sun&#8221;. Now we need to place an object on the canvas to cast a shadow. We could just plot it somewhere, but what we really want is it to be in the middle of the canvas and the shadow to go left and right from that. You can move the origin of the canvas&#8217; coordinate system using <code>translate()</code>. Which means though that our orb is now offset from the mouse:</p>
<p><iframe style="width: 100%; height: 350px" src="http://jsfiddle.net/codepo8/z9Z8T/2/embedded/result,js,html,css/"></iframe><br />
<a href="http://jsfiddle.net/codepo8/z9Z8T/2">See step three on JSFiddle</a></p>
<p>If you check the &#8220;fix mouse position&#8221; checkbox you see that this is fixed. As we move the coordinate system to the half of the width of the canvas and half of its height, we also need to substract these values from the mouse x and y position. </p>
<p>Now we can draw a line from the centre of the canvas to the mouse position to see the distance using <code>c.moveTo( 0, 0 );c.lineTo( distx, disty );</code> where <code>distx</code> and <code>disty</code> are the mouse position values after the shifting:</p>
<p><iframe style="width: 100%; height: 350px" src="http://jsfiddle.net/codepo8/z9Z8T/4/embedded/result,js,html,css/"></iframe><br />
<a href="http://jsfiddle.net/codepo8/z9Z8T/4">See step four on JSFiddle</a></p>
<p>In order to find out the distance of the shadow, all we need to do is multiply the mouse coordinates with -1 &#8211; in this demo shown as a red line:</p>
<p><iframe style="width: 100%; height: 350px" src="http://jsfiddle.net/codepo8/z9Z8T/5/embedded/result,js,html,css/"></iframe><br />
<a href="http://jsfiddle.net/codepo8/z9Z8T/5">See step five on JSFiddle</a></p>
<p>This gives us a shadow distance from the centre opposite of the mouse position, but we don&#8217;t want the full length. Therefore we can apply a factor to the length, in our case 0.6 or 60%:</p>
<p><iframe style="width: 100%; height: 320px" src="http://jsfiddle.net/codepo8/z9Z8T/6/embedded/result,js,html,css/"></iframe><br />
<a href="http://jsfiddle.net/codepo8/z9Z8T/6">See step six on JSFiddle</a></p>
<p>Now we are ready for some drop shadow action. You can apply shadows to canvas objects using <code>shadowColor</code> and its distance is <code>shadowOffsetX</code> and <code>shadowOffsetY</code>. In our case, this is the end of the red line, the inversed and factored distance from the mouse position to the centre of the canvas:</p>
<p><iframe style="width: 100%; height: 350px" src="http://jsfiddle.net/codepo8/z9Z8T/7/embedded/result,js,html,css/"></iframe><br />
<a href="http://jsfiddle.net/codepo8/z9Z8T/7">See step seven on JSFiddle</a></p>
<p>Now, let&#8217;s blur the shadow. Blurring is done using the <code>shadowBlur</code> property and it is a number starting from 0 to the strength of the blur. We now need to find a way to calculate the blur strength from the distance of the mouse to the centre of the canvas. Luckily enough, <a href="http://en.wikipedia.org/wiki/Pythagorean_theorem">Pythagoras</a> found out for us years ago how to do it. As the x and y coordinate of the mouse are the catheti of a right-angled triangle, we can <a href="http://en.wikipedia.org/wiki/Hypotenuse">calculate the length of the hypothenuse</a> (the distance of the point from the centre of the canvas) using the Square root of the squares of the coordinates or <code>Math.sqrt( ( distx * distx ) + ( disty * disty ) )</code>.</p>
<p>This gives us the distance in pixels, but what the really want is a number much lower. Therefore we can again calculate a factor for the blur strength &#8211; here we use an array for the weakest and strongest blur <code>blur = [ 2, 9 ]</code>. As the canvas itself also has a right-angled triangle from the centre to the top edge points we can calculate the longest possible distance from the center using <code>longest = Math.sqrt( ( hw * hw ) + ( hh * hh ) )</code> where <code>hw</code> is half the width of the canvas and <code>hh</code> half the height. Now all we need to do is to calculate the factor to multiply the distance with as <code>blurfactor = blur[1] / longest</code>. The blur during the drawing of the canvas is the distance of the mouse position multiplied with the factor or <code>currentblur = parseInt( blurfactor * realdistance, 10 );</code>. We  disregard blur values below the range we defined earlier and we have our blurred shadow:</p>
<p><iframe style="width: 100%; height: 350px" src="http://jsfiddle.net/codepo8/z9Z8T/8/embedded/result,js,html,css/"></iframe><br />
<a href="http://jsfiddle.net/codepo8/z9Z8T/8">See step eight on JSFiddle</a></p>
<p>In order to make the shadow weaker the further away the mouse is we can use the alpha value of its <code>rgba()</code> colour. The same principle applies as with the blur, we set our edge values as <code>shadowalpha = [ 3, 8 ]</code> and after calculating them from the distance we apply their inverse as the alpha value with <code>c.shadowColor = 'rgba(0,0,0,' + (1 - currentalpha / 10)  + ')';</code>. This blurs and weakens the shadow:</p>
<p><iframe style="width: 100%; height: 350px" src="http://jsfiddle.net/codepo8/z9Z8T/9/embedded/result,js,html,css/"></iframe><br />
<a href="http://jsfiddle.net/codepo8/z9Z8T/9">See step nine on JSFiddle</a></p>
<p>You can do a lot more with this, for example we could also <a href="http://jsfiddle.net/codepo8/eUYKu/26/">scale the sun orb</a> the further out it gets or use a <a href="http://jsfiddle.net/PHbgN/32/">second shape to resize and blur it</a>. You can also go <a href="http://jsfiddle.net/codepo8/mTaug/17/">completely overboard</a>. </p>
<p>Found a way to optimise this? Tell us about it! </p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/08/calculated-drop-shadows-in-html5-canvas/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>speak.js: Text-to-Speech on the Web</title>
		<link>http://hacks.mozilla.org/2011/08/speak-js-text-to-speech-on-the-web/</link>
		<comments>http://hacks.mozilla.org/2011/08/speak-js-text-to-speech-on-the-web/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 18:36:56 +0000</pubDate>
		<dc:creator>azakai</dc:creator>
				<category><![CDATA[Audio]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=9213</guid>
		<description><![CDATA[Text-to-Speech (TTS) can make content more accessible, but there is so far no simple and universal way to do that on the web. One possible approach is shown in this demo, which is powered by speak.js, a new 100% pure JavaScript/HTML5 TTS implementation. speak.js is a port of eSpeak, an open source speech synthesizer, from [...]]]></description>
			<content:encoded><![CDATA[<p>Text-to-Speech (TTS) can make content more accessible, but there is so far no simple and universal way to do that on the web. One possible approach is shown in <strong><a href="http://syntensity.com/static/espeak.html">this demo</a></strong>, which is powered by <a href="https://github.com/kripken/speak.js">speak.js</a>, a new 100% pure JavaScript/HTML5 TTS implementation. speak.js is a port of <a href="http://espeak.sourceforge.net/">eSpeak</a>, an open source speech synthesizer, from C++ to JavaScript using <a href="http://emscripten.org">Emscripten</a>.</p>
<p>Compiling an existing speech synthesis engine to JavaScript is a good way to avoid writing a complicated project like eSpeak from scratch. Once compiled, the eSpeak code in speak.js doesn&#8217;t know it&#8217;s running on the web: speak.js uses the <a href="https://github.com/kripken/emscripten/wiki/Filesystem-Guide"> Emscripten emulated filesystem</a> to &#8216;fake&#8217; the normal file reading and writing calls that the eSpeak C++ code has (fopen, fread, etc.). This allows the normal eSpeak datafiles to be used (either through an xhr, or by converting them to JSON and bundling them with the script file). The result of running the compiled eSpeak code is that it &#8216;writes&#8217; a .wav file with the generated audio to the emulated filesystem. speak.js then takes that data, encodes it using base64, and creates a data URL. That URL is then loaded in an HTML5 audio element, letting the browser handle playback. (Note that while that is a very simple way to do things, it isn&#8217;t the most efficient. speak.js has not yet focused on speed, but with some additional work it could be much faster, if that turns out to be an issue.)</p>
<p>Why would you want TTS in JavaScript? Well, with speak.js you can bundle a single .js file in your website, and then generating speech is about as simple as writing</p>
<blockquote>
<pre>speak("hello world")</pre>
</blockquote>
<p>(see the <a href="https://github.com/kripken/speak.js">speak.js website</a> for instructions). The generated speech will be exactly the same on all platforms, unlike if your users each did TTS in their own way (using an OS capability, or a separate program). speak.js can also be used to build browser addons in a straightforward way, since it&#8217;s pure JavaScript &#8211; no need for platform dependent binaries, and the addon will work the same on all OSes.</p>
<p>A few more comments:</p>
<ul>
<li>JavaScript is getting more and more capable all the time. The development versions of the top JavaScript engines today can run code compiled from C++ only 3-5X slower than a fast C++ compiler, and getting even better. As a consequence, expanding the capabilities of the web platform can in many cases be done in JavaScript or by compiling to JavaScript, instead of adding new code to the browsers themselves, which inevitably takes longer &#8211; especially if you wait for all browsers to implement a particular feature.</li>
<li>While speak.js uses only standards-based APIs, due to browser limitations it can&#8217;t work everywhere yet. It won&#8217;t work in IE, Safari or Opera since they don&#8217;t support typed arrays, nor in Chrome since it doesn&#8217;t support WAV data URLs. So currently speak.js only works properly in Firefox. However, the missing features just mentioned are not huge and hopefully those browser makers will implement them soon. It is also possible to implement workarounds in speak.js for these issues (see next comment).</li>
<li>Help with improving speak.js is very welcome! One important thing we need is to implement workarounds for the issues that prevent speak.js from running on the browsers it currently can&#8217;t run on. Another goal is to build browser addons using speak.js. Please get in touch <a href="https://github.com/kripken/speak.js">on github</a> if you want to help out.</li>
<li>eSpeak supports multiple languages so speak.js can too. You do need to include the additional language files though. <a href="http://syntensity.com/static/espeak_fr.html">Here</a> is an experimental build where you can switch between English and French support (note that it is an unoptimized build, so it will run slower).</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/08/speak-js-text-to-speech-on-the-web/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Rendering 3D with CSS and JavaScript with dom3d (guest post)</title>
		<link>http://hacks.mozilla.org/2011/08/rendering-3d-with-css-and-javascript-with-dom3d-guest-post/</link>
		<comments>http://hacks.mozilla.org/2011/08/rendering-3d-with-css-and-javascript-with-dom3d-guest-post/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 07:38:22 +0000</pubDate>
		<dc:creator>Chris Heilmann</dc:creator>
				<category><![CDATA[Animations]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[css3d]]></category>
		<category><![CDATA[hack]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=9071</guid>
		<description><![CDATA[Today we have a guest post by James Long (@jlongster). James is the tech lead for mozilla.com on the Web Development team. James is passionate about interactive graphics on the open web. Today he explains how you can create 3D objects using CSS without having 3D transforms support. Take it away, James. Recently I was [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://hacks.mozilla.org/wp-content/uploads/2011/08/jameslong.png" alt="James Long"width="204" height="206" style="float:left;padding:5px;" /> Today we have a guest post by James Long (<a href="http://twitter.com/jlongster">@jlongster</a>).</p>
<p>James is the tech lead for mozilla.com on the Web Development team. James is passionate about interactive graphics on the open web. </p>
<p>Today he explains how you can create 3D objects using CSS without having 3D transforms support. Take it away, James.</p>
<hr />
<p style="clear:both;margin-top:3em">Recently I was tinkering with CSS3 and I discovered that it  enabled me to do primitive 3D rendering, which I found  fascinating! This led to the creation  of <a href="https://github.com/jlongster/dom3d">dom3d</a>, a  JavaScript library that uses CSS to render basic 3D objects.</p>
<p>Now the question is: why? Aren&#8217;t canvas, WebGL, and even SVG  better technologies to work with for this? Possibly. However,  CSS is becoming a powerful language for describing complex  effects and shapes, and we should experiment.</p>
<p>Keep that in mind, because CSS definitely  isn&#8217;t <em>intended</em> to do this, but it&#8217;s worth trying to see  where we should take CSS in the future.</p>
<h2>Advantages</h2>
<p>Although this is more of an experiment, it has a few real world benefits:</p>
<p>All rendering libraries available for the web (canvas, WebGL,  SVG) require a canvas, which is a constrained box on the page  with a specific width and height. It is not possible to render  anything outside this box. The canvas also captures all DOM  events (like clicks), even completely transparent  sections. Theoretically, this could make it difficult to do effects that  overlay large parts of the page or are somehow deeply integrated  to the content.</p>
<p>Using CSS, we aren&#8217;t constrained to a box, and the effect can overlay large portions of the page without covering any the the  links or other content requiring interaction.</p>
<p>Other advantages include no need to initialize canvas 2D or  WebGL, and a simplistic API making it easy to pick up even if  you don&#8217;t know much about 3D. It might be easier for kids to  start playing around with this before they jump into WebGL or  something else. Also, because it&#8217;s just a dump of DOM elements  you can embed it anywhere (without  animation).</p>
<p>So keep in mind that this <em>is</em> a hack, but with the above  advantages. This might be good for certain effects: <a href="http://jlongster.com/s/dom3d/cursor.html">3D cursor</a>, nav  transitions, and others.</p>
<h2>How it works</h2>
<p>Three-D objects are just a bunch of triangles put together, so let&#8217;s  start with one simple triangle. If we get that working, it&#8217;s a  simple step forward to render multiple triangles to form a 3D  object.</p>
<p>Rendering a 3d triangle on a 2D screen involves something called  &#8220;projection&#8221;. This is the act of taking a 3D point  and <em>projecting</em> it onto a 2D screen. Plug in a 3D  triangle to a simple math equation, and you get a 2D triangle  representing how the 3D one would look on the screen.</p>
<p>The math is remarkably simple but may seem weird if you aren&#8217;t familiar with linear algebra. You can   <a href="https://github.com/jlongster/dom3d/blob/e892597fc020fe18a766/renderer.js#L6">take a look at the renderer code</a>.</p>
<p>Now comes the fun part: can you render any 2D triangle simply with <a href="https://developer.mozilla.org/En/transform">CSS3 transforms</a>? Turns out you can! It just takes some fiddling  to figure out which transforms to generate. CSS3 transforms are  composed of translate, scale, rotate, and skew values, and we  need a few equations to compute these values for a specific 2D  triangle.</p>
<p>First, let&#8217;s take a simple DOM element and turn it into a  triangle. We can do this with the <code>linear-gradient</code>  background image (another way  is <a href="http://mrcoles.com/blog/callout-box-css-border-triangles-cross-browser/">border triangles</a>).</p>
<p><iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/jlongster/cHhQ3/4/embedded/"></iframe></p>
<p>Now let&#8217;s draw the following blue triangle with the points [20,  20], [50, 120], and [120, 30]. A vital step is to set a few  initial reference points which set everything in the same  space. Our equations will assume these coordinate spaces. This  is how the points A, B, C and the side AB are related.</p>
<p><img src="http://hacks.mozilla.org/wp-content/uploads/2011/08/tri-compare.png" alt="Triangle comparison"/></p>
<p>If we take a closer look at this, we can derive the transform  values. First, get an idea of which angles and values we need  and then use geometry to form the equations (in  pseudo-code). The red box represents the DOM element, the form  AB represents the side formed by points A and B, and rotation  occurs clockwise.</p>
<p><img src="http://hacks.mozilla.org/wp-content/uploads/2011/08/tri-disect.png" alt="dissecting triangles" style="float:right"/>
<pre>
rotation = atan2(AB.x, AB.y)
AC' = rotate(AC, -rotation)
width = AC'.x
height = length(AB)
skew = atan2(AC'.y, AC'.x)
translate = A</pre>
<p>Awesome! Let&#8217;s try it out. Here is a live DOM element being  transformed by applying each of our equations:</p>
<p><iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/jlongster/2eU3Q/3/embedded/"></iframe></p>
<p>The resulting triangle matches our target triangle! Here is the  final CSS:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">93px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">104px</span><span style="color: #00AA00;">;</span>
<span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> -moz-linear-gradient<span style="color: #00AA00;">&#40;</span>-0.727211rad<span style="color: #00AA00;">,</span> <span style="color: #cc00cc;">#0000FF</span> <span style="color: #933;">50%</span><span style="color: #00AA00;">,</span> <span style="color: #993333;">transparent</span> <span style="color: #933;">0pt</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
-moz-transform<span style="color: #00AA00;">:</span> translate<span style="color: #00AA00;">&#40;</span><span style="color: #933;">20px</span><span style="color: #00AA00;">,</span> <span style="color: #933;">20px</span><span style="color: #00AA00;">&#41;</span> rotate<span style="color: #00AA00;">&#40;</span>-0.291457rad<span style="color: #00AA00;">&#41;</span> skewY<span style="color: #00AA00;">&#40;</span>0.391125rad<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
-moz-transform-origin<span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">top</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span></pre></div></div>

<p><em>Note</em>: The <code>tranform-origin: top left</code> line is  important. Normally transforms happen relative to the center of  the element, but our equations assume the top left.</p>
<p><em>Note</em>: dom3d also generates code with the <code>-webkit</code>  and <code>-o</code> prefixes for WebKit and Opera support.</p>
<p>You can view the <a href="https://github.com/jlongster/dom3d/blob/master/renderer_css.js#L79">    implementation of these  equations</a>. It turns out that these equations work for any triangle, as long  as the points given are in counter-clockwise order, which is  standard in the graphics world.</p>
<h3>Taking it all the way</h3>
<p>Since we can project a 3D triangle into 2D space and render it  with CSS, all we have to do now is apply that to several 3D  triangles to form a 3D object!</p>
<p>  We need some 3D data at this point. I used Blender to export a  teapot into the simple OBJ file format and  <a href="https://github.com/jlongster/dom3d/blob/master/mesh/dump-vertices.scm">wrote a script</a> to dump the  <a href="https://github.com/jlongster/dom3d/blob/master/teapot.js">data  as JavaScript</a>. Rendering all those triangles with this technique  produces the following:</p>
<p><iframe id="teapot" style="width: 100%; height: 300px" src="http://jsfiddle.net/jlongster/BmyQD/9/embedded/"></iframe></p>
<p>Teapot! However, we can do much better. A big part of the 3D  effect is <em>shading</em>. If we calculate normals, a vector  representing where the triangle is facing, and specify a light  direction, we can take the dot product of the normal and light  for each triangle to get  <a href="http://en.wikipedia.org/wiki/Shading#Flat_vs_smooth_shading">flat shading</a>. View   <a href="https://github.com/jlongster/dom3d/blob/master/renderer.js#L69">the code for flat shading</a>.</p>
<p><iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/jlongster/avzaJ/2/embedded/"></iframe></p>
<p>There are many tweaks that take this even further. For example,  the above objects have z-indexing enabled. Without this, a  triangle that is supposed to be behind another may actually  appear on top because it was rendered  later. The dom3d uses a  <a href="https://github.com/jlongster/dom3d/blob/e892597fc020fe18a766/heap.js">heap</a> to render the triangles from  <a href="https://github.com/jlongster/dom3d/blob/e892597fc020fe18a766/dom3d.js#L61">back to front</a>.</p>
<p>Real-time animation can be achieved with a setTimeout or requestAnimationFrame function that continually renders the object. The dom3d supports the scale,  translate, yaw, and pitch transformations, but there&#8217;s nothing  stopping you from modifying the object data however you like  between renderings. See some examples over at  the <a href="http://jlongster.com/s/dom3d/">dom3d website</a>.</p>
<p>Here is the code which renders  the <a href="http://jlongster.com/s/dom3d/example3.html">teapot  animation</a> with dom3d:</p>
<p><iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/jlongster/24fK3/2/embedded/"></iframe>
<p>  It&#8217;s more appropriate for webpages to update an animation in  response to user interaction instead of constantly rendering and  hogging the CPU. See  the <a href="http://jlongster.com/s/dom3d/example.html">pole  example</a> on the dom3d site for an example.</p>
<h2>Improvements and last thoughts</h2>
<p>The most interesting possibility with this is to include actual  page elements as part of 3D objects. A navigation item could pop  out and swirl around in 3d space, and the nav item is seamlessly  transformed along with it.</p>
<p>That&#8217;s where this hack starts to show its faults, though. Unfortunately this is a little too hacky to provide an  appropriate web experience. Because it tricks DIVs into fake  triangles, it removes the possibility of integrating any page  elements with it. With the coming  of <a href="http://www.w3.org/TR/css3-3d-transforms/">3D CSS  transforms</a> though, we can start building true 3D objects  made up of any kind of page elements. The only restriction with  3D transforms is that the 3D objects need to built with  rectangles instead of triangles.</p>
<p>Other people have already experimented with 3D transforms, like  <a href="http://cubiq.org/building-a-pure-css-3d-city">building  a pure CSS 3D city</a>. There&#8217;s another cool library,  <a href="http://minimal.be/lab/Sprite3D/">Sprite3D</a>, which  provides a JavaScript API for building basic 3d objects from  page elements.</p>
<p>The most glaring problem with dom3d is the seams in the object, which appear in all browsers. Apparently there are a few bugs in  rendering engines when stressing their CSS3 transforms and using  linear-gradients!</p>
<p>The dom3d library provides an API to work with all of this, but is hasn&#8217;t  been documented very well yet. Feel free to browse the README  and <a href="http://github.com/jlongster/dom3d">code on github</a>. These APIs could be improved as well. It also  provides an SVG rendering backend,  <a href="https://github.com/jlongster/dom3d/blob/master/renderer_raphael.js">seen  here</a>, but I don&#8217;t this is the right direction to take. We  should focus on building basic 3D objects with page elements.</p>
<p>  This was a fun experiment and I&#8217;m excited by how fast and  capable browsers are becoming. The web is an exciting platform  and getting richer and more powerful every year!</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/08/rendering-3d-with-css-and-javascript-with-dom3d-guest-post/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tilt: Visualize your Web page in 3D</title>
		<link>http://hacks.mozilla.org/2011/07/tilt-visualize-your-web-page-in-3d/</link>
		<comments>http://hacks.mozilla.org/2011/07/tilt-visualize-your-web-page-in-3d/#comments</comments>
		<pubDate>Wed, 20 Jul 2011 15:18:33 +0000</pubDate>
		<dc:creator>Paul Rouget</dc:creator>
				<category><![CDATA[Add-ons]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[Developer Tools]]></category>
		<category><![CDATA[WebGL]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=8762</guid>
		<description><![CDATA[Tilt is a Firefox extension that lets you visualize any web page DOM tree in 3D. It is being developed by Victor Porof (3D developer responsible with the Firefox extension itself), along with Cedric Vivier (creating a WebGL optimized equivalent to the privileged canvas.drawWindow, see #653656) and Rob Campbell (who first thought about creating a [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Tilt</strong> is a Firefox extension that lets you visualize any web page DOM tree in 3D. It is being developed by <a href="http://twitter.com/victorporof">Victor Porof</a> (3D developer responsible with the Firefox extension itself), along with <a href="http://twitter.com/#%21/neonux">Cedric Vivier</a> (creating a WebGL optimized equivalent to the privileged canvas.drawWindow, see <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=653656">#653656</a>) and <a href="http://twitter.com/robcee">Rob Campbell</a> (who first thought about creating a 3D visualization of a webpage). Everything started initially as a Google Summer of Code project, but now, with an enthusiastic team behind it and so many new features and ideas, it has become an active Developer Tools project.</em></p>
<p><iframe type="text/html" width="500" height="360" src="http://www.youtube.com/embed/dW2eAbr5FBw" frameborder="0"></iframe></p>
<p>Tilt is a fun new Firefox extension focused on creating a 3D visualization of a webpage.</p>
<p>Since the DOM is essentially a tree-like representation of a document, this tool layers each node based on the nesting in the tree, creating stacks of elements, each having a corresponding depth and being textured according to the webpage rendering itself.</p>
<p>Unlike other developer tools or inspectors, Tilt allows for instant analysis of the relationship between various parts of a webpage in a graphical way, but also making it easy for someone to see obscured or out-of-page elements. Moreover, besides the 3D stacks, various information is available on request, regarding each node’s type, id, class, or other attributes if available, providing a way to inspect (and edit) the inner HTML and other properties.</p>
<h2>Based on WebGL</h2>
<p>The visualization is drawn using WebGL, for dynamic, fast, in-browser rendering. At initialization, Tilt creates individual 3D objects (structures describing how the webpage geometry looks like) using the DOM, with the BODY as the lowest layer and the base of the document upon which descendant nodes are layered. For each successive level, another platform is built, adding depth to the 3D webpage mesh. For example, stacks are built from DIVs, ULs, or any containing node with children.</p>
<h2>Controls</h2>
<p>Controlling the visualization is achieved using a virtual trackball (arcball), which rotates around the X and Y axes. Other mouse events exist to control yaw, pitch, roll, pan, zoom, as well as various additional keyboard shortcuts. The controller is not tied to these peripherals only however, making it accessible and easily scalable for other input methods or devices. Double clicking a node brings up the Ace Cloud9 IDE editor, showing more useful information about the node and the inner HTML.</p>
<h2>Try it</h2>
<p>You can find the Tilt source code and the latest extension builds <a href="https://github.com/victorporof/Tilt">on Github</a>, and a development blog with milestone updates on <a href="http://blog.mozilla.com/tilt">blog.mozilla.com/tilt</a>.</p>
<p>For now, to test the extension, just download the latest stable build (<a href="https://github.com/victorporof/Tilt/raw/master/bin/Tilt.xpi">tilt.xpi</a>: <em>download the file, then open it with Firefox or drag&#8217;n drop it on Firefox</em>), install it and search for Tilt inside the Tools menu. Or, you can use Ctrl+Shift+L (or Cmd+Shift+L if you’re on a Mac) to start the visualization. Close it at any time with the Esc key. Tilt works with any webpage, so you can even inspect this blog to see how it looks in 3D.</p>
<p><a href="http://hacks.mozilla.org/wp-content/uploads/2011/07/screenshot.png"><img src="http://hacks.mozilla.org/wp-content/uploads/2011/07/screenshot-500x310.png" alt="" title="Tilt" width="500" height="310" class="aligncenter size-large wp-image-8766" /></a></p>
<h2>Future</h2>
<p>More features are soon to be added, some of which include: modifying and updating the 3D webpage mesh on the fly (as the webpage changes, exposing CSS transforms for each node, plus customizing stack spacing, thickness, transparency etc.), rendering elements with absolute position or floats differently (e.g., hovering above the webpage based on their z-index), creating a more developer-friendly environment and better integration with the Ace editor and the Firefox Developer Tools. (highlighting the currently selected node, instant 3D preview), exporting the visualization to other browsers or applications (as a 3D object file, probably .obj and/or COLLADA).</p>
<p>The greatest milestone will be achieving seamless 3D navigation between webpages,  as in a normal 2D environment.</p>
<p>For more information about upcoming tasks visit the <a href="https://github.com/victorporof/Tilt/blob/master/TODO.md">TODO.md</a> list.</p>
<style>
em, p {
text-align:justify;
}
</style>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/07/tilt-visualize-your-web-page-in-3d/feed/</wfw:commentRss>
		<slash:comments>101</slash:comments>
		</item>
		<item>
		<title>Rofox, a CSS3 Animations demo</title>
		<link>http://hacks.mozilla.org/2011/06/rofox-a-css3-animations-demo/</link>
		<comments>http://hacks.mozilla.org/2011/06/rofox-a-css3-animations-demo/#comments</comments>
		<pubDate>Mon, 27 Jun 2011 14:01:29 +0000</pubDate>
		<dc:creator>Paul Rouget</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[Featured Demo]]></category>
		<category><![CDATA[Firefox 5]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=8552</guid>
		<description><![CDATA[Firefox 5 was released last week. This release comes with CSS3 Animations. Here is a demo made by Anthony Calzadilla. To illustrate what you can achieve with CSS3 Animations, we have been working on demo with Anthony Calzadilla (@acalzadilla), famous for his awesome Animation projects. Check out the demo on the Mozilla Demo Studio. And [...]]]></description>
			<content:encoded><![CDATA[<p><em>Firefox 5 was <a href="https://hacks.mozilla.org/2011/06/firefox5/">released</a> last week. This release comes with <a href="https://developer.mozilla.org/en/CSS/CSS_animations">CSS3 Animations</a>. <a href="https://developer.mozilla.org/en-US/demos/detail/rofox-css3-animation-by-anthony-calzadilla">Here</a> is a demo made by <a href="http://www.anthonycalzadilla.com/">Anthony Calzadilla</a>.</em></p>
<p>To illustrate what you can achieve with CSS3 Animations, we have been working on demo with <a href="http://www.anthonycalzadilla.com/">Anthony Calzadilla</a> (<a href="http://twitter.com/#!/acalzadilla">@acalzadilla</a>), famous for his awesome <a href="http://www.anthonycalzadilla.com/2010/10/css3-animation-hit-list/">Animation projects</a>.</p>
<p>Check out the <a href="https://developer.mozilla.org/en-US/demos/detail/rofox-css3-animation-by-anthony-calzadilla">demo on the Mozilla Demo Studio</a>.</p>
<p><iframe type="text/html" width="500" height="360" src="http://www.youtube.com/embed/q3lxspwIOx4" frameborder="0"></iframe></p>
<p>And it works on Firefox Mobile too:</p>
<p><iframe type="text/html" width="500" height="360" src="http://www.youtube.com/embed/j2NIrrIUCvY" frameborder="0"></iframe></p>
<p>The whole animation is orchestrated in CSS (<a href="https://developer.mozilla.org/en/CSS/CSS_animations#Defining_the_animation_sequence_using_keyframes">keyframe</a>) and the moves are animated transformations (<a href="https://developer.mozilla.org/En/CSS/Using_CSS_transforms">transforms</a>). The images are nested divs. If you translated a div and rotate its child, the transformations are combined. You can see the elements being transformed (bounding boxes) if you activate the debug mode.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#arm-rt</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #808080; font-style: italic;">/* ARM  SLIDING OUT FROM BODY */</span>
  transform-origin<span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #933;">50%</span><span style="color: #00AA00;">;</span>
  <span style="color: #808080; font-style: italic;">/* The syntax is:
   animation: name duration timing-function delay count direction
  */</span>
  animation<span style="color: #00AA00;">:</span> arm-rt-action-01 60s ease-out 10s <span style="color: #cc66cc;">1</span> <span style="color: #993333;">both</span><span style="color: #00AA00;">;</span> 
<span style="color: #00AA00;">&#125;</span>
<span style="color: #a1a100;">@keyframes arm-rt-action-01 {</span>
  <span style="color: #808080; font-style: italic;">/* This part of the animation starts after 10s and lasts for 60s */</span>
  <span style="color: #933;">0%</span> <span style="color: #00AA00;">&#123;</span> transform <span style="color: #00AA00;">:</span> translate<span style="color: #00AA00;">&#40;</span><span style="color: #933;">-100px</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span> rotate<span style="color: #00AA00;">&#40;</span>0deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
  <span style="color: #933;">5%</span> <span style="color: #00AA00;">&#123;</span> transform <span style="color: #00AA00;">:</span> translate<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span> rotate<span style="color: #00AA00;">&#40;</span>0deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
  <span style="color: #933;">6%</span> <span style="color: #00AA00;">&#123;</span> transform <span style="color: #00AA00;">:</span> translate<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span> rotate<span style="color: #00AA00;">&#40;</span>-16deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
  <span style="color: #933;">21%</span> <span style="color: #00AA00;">&#123;</span> transform <span style="color: #00AA00;">:</span> translate<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span> rotate<span style="color: #00AA00;">&#40;</span>-16deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
  <span style="color: #933;">22%</span> <span style="color: #00AA00;">&#123;</span> transform <span style="color: #00AA00;">:</span> translate<span style="color: #00AA00;">&#40;</span><span style="color: #933;">-100px</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span> rotate<span style="color: #00AA00;">&#40;</span>0deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
  <span style="color: #933;">100%</span> <span style="color: #00AA00;">&#123;</span> transform <span style="color: #00AA00;">:</span> translate<span style="color: #00AA00;">&#40;</span><span style="color: #933;">-100px</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span> rotate<span style="color: #00AA00;">&#40;</span>0deg<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p><img src="http://hacks.mozilla.org/wp-content/uploads/2011/06/s-500x417.png" alt="" title="Rofox" width="500" height="417" class="aligncenter size-large wp-image-8567" /></p>
<p><strong>Tip:</strong> If you want to avoid some performance issues, we encourage you to use bitmap images. SVG images can make the animation a bit shoppy.</p>
<p>Want to see more CSS3 Animations? Check out Anthony&#8217;s website: <a href="http://www.anthonycalzadilla.com">www.anthonycalzadilla.com</a>. And feel free to submit your CSS3 Animations demos to the <a href="https://developer.mozilla.org/en-US/demos/detail/rofox-css3-animation-by-anthony-calzadilla">Mozilla Demo Studio</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/06/rofox-a-css3-animations-demo/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A Wall Powered by EventSource and Server-Sent Events</title>
		<link>http://hacks.mozilla.org/2011/06/a-wall-powered-by-eventsource-and-server-sent-events/</link>
		<comments>http://hacks.mozilla.org/2011/06/a-wall-powered-by-eventsource-and-server-sent-events/#comments</comments>
		<pubDate>Thu, 16 Jun 2011 14:27:03 +0000</pubDate>
		<dc:creator>louisremi</dc:creator>
				<category><![CDATA[Aurora]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[Event]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=8405</guid>
		<description><![CDATA[EventSource landed in Aurora 6. It is a new and simplified way to open long-lived connections to a server, and let the browser create events as the server streams messages to the client. It is also available in Chrome and Opera and there are fallback solutions for other browsers. Creating a wall/feed for a social [...]]]></description>
			<content:encoded><![CDATA[<p><em><a href="http://www.w3.org/TR/eventsource/">EventSource</a> <a href="http://hacks.mozilla.org/2011/05/aurora-6-is-here/">landed in Aurora 6</a>. It is a new and simplified way to open long-lived connections to a server, and let the browser create events as the server streams messages to the client. It is also available in Chrome and Opera and there are fallback solutions for other browsers.</em></p>
<h3>Creating a wall/feed for a social app&#8230;</h3>
<p><iframe width="500" height="400" src="http://www.youtube.com/embed/BFYzCkUdyFw" frameborder="0" allowfullscreen></iframe></p>
<p>&#8230;in a few lines of code (full project <a href="https://github.com/mozilla/webowonder-demos/tree/master/demos/friends%20timeline">available on <strong>Github</strong></a>).</p>
<h3>The messages</h3>
<p>The server will send two kinds of messages:<br />
&nbsp;● simple messages, starting on a new line prefixed with &#8220;data:&#8221;<br />
&nbsp;● messages with specific event names, similar to simple messages but with &#8220;event: &lt;anEventName&gt;&#8221; on the previous line<br />
<script src="https://gist.github.com/1027192.js?file=messages.txt"></script><br />
In this case, simple messages are treated as users&#8217; statuses and specific events will be inserted in the timeline with specific colors, although they could appear in different places on the page. The message data will be sent as JSON, although it could be flat text strings.</p>
<h3>The server</h3>
<p>The server will be a dummy .php script that reads sample statuses from a text files and stream them, one at a time, to the client, using appropriate headers.<br />
<script src="https://gist.github.com/1027192.js?file=server.php"></script></p>
<h3>The Client</h3>
<p>The client will create an event source and register event handlers for each specific event name, as well as an <code>onmessage</code> handler for <i>simple messages</i>.<br />
<script src="https://gist.github.com/1027192.js?file=client.js"></script></p>
<p>The missing pieces of the code are <a href="https://github.com/mozilla/webowonder-demos/tree/master/demos/friends%20timeline">available on <strong>Github</strong></a>.</p>
<h3>Fallbacks</h3>
<p>Here is a short list of polyfills/fallbacks available for other browsers:<br />
&nbsp;● Remy Sharp&#8217;s <a href="https://github.com/remy/polyfills/blob/master/EventSource.js">polyfill</a><br />
&nbsp;● Yaffle&#8217;s <a href="https://github.com/Yaffle/polyfills">polyfill</a><br />
&nbsp;● Rick Waldron&#8217;s <a href="https://github.com/rwldrn/jquery.eventsource">jquery plugin</a></p>
<p><strong>Have you got examples of EventSource based Web app to share?</strong></p>
<style>
.gist { font-size: .8em; }
.gist-meta { font-size: .6em !important; }
.gist-file { margin: 0 !important; }
.code {font-size: .8em; background: #F8F8FF; border: 1px solid #DEDEDE; }
</style>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/06/a-wall-powered-by-eventsource-and-server-sent-events/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>MarbleRun, an HTML5 Game</title>
		<link>http://hacks.mozilla.org/2011/04/marblerun-an-html5-game/</link>
		<comments>http://hacks.mozilla.org/2011/04/marblerun-an-html5-game/#comments</comments>
		<pubDate>Wed, 27 Apr 2011 16:28:18 +0000</pubDate>
		<dc:creator>Paul Rouget</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Challenge]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[Firefox 4]]></category>
		<category><![CDATA[HTML5]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=7900</guid>
		<description><![CDATA[Take the HTML5 Canvas element, a Javascript library to do the physics (box2d), add a nice design, a social touch, and you have an awesome HTML5 Game called MarbleRun! MarbleRun was the winner of the last Mozilla Game On Challenge, among other exciting HTML5 games (check them out), and had been added to Web O&#8217; [...]]]></description>
			<content:encoded><![CDATA[<p>Take the <a href="https://developer.mozilla.org/en/Canvas_tutorial">HTML5 Canvas element</a>, a Javascript library to do the physics (<a href="https://github.com/jwagner/box2d2-js">box2d</a>), add a nice design, a social touch, and you have an awesome HTML5 Game called <a href="https://demos.mozilla.org/en-US/#marblerun">MarbleRun</a>!</p>
<p>MarbleRun was the winner of the last <a href="http://mozillalabs.com/gaming/2011/02/03/game-on-winners/">Mozilla Game On Challenge</a>, among other exciting HTML5 games (<a href="http://mozillalabs.com/gaming/2011/02/03/game-on-winners/">check them out</a>), and had been added to <a href="http://webowonder.org">Web O&#8217; Wonder</a>.</p>
<p><a href="https://demos.mozilla.org/en-US/#marblerun"><img src="https://gaming.mozillalabs.com/media/uploads/marblerun.png" width="100%"></a></p>
<p>The authors of MarbleRun visited us a couple of weeks ago and gave the following talk about the story of their game:</p>
<p><iframe title="YouTube video player" width="500" height="300" src="http://www.youtube.com/embed/hrHCLQeYz6E" frameborder="0" allowfullscreen></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/04/marblerun-an-html5-game/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fun with new technologies at the Firefox 4 launch party in London</title>
		<link>http://hacks.mozilla.org/2011/04/fun-with-new-technologies-at-the-firefox-4-launch-party-in-london/</link>
		<comments>http://hacks.mozilla.org/2011/04/fun-with-new-technologies-at-the-firefox-4-launch-party-in-london/#comments</comments>
		<pubDate>Mon, 25 Apr 2011 13:19:12 +0000</pubDate>
		<dc:creator>Chris Heilmann</dc:creator>
				<category><![CDATA[Demo]]></category>
		<category><![CDATA[Firefox 4]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Web Developers]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=7896</guid>
		<description><![CDATA[For the Firefox 4 launch party in London, England we wanted to show off to the audience why it is such a big thing that we are moving leaps and bounds in the browser market. Here are the slides and notes explaining just how much fun we can have as developers these days if we [...]]]></description>
			<content:encoded><![CDATA[<p>For the <a href="http://fuzzyfox.mozhunt.com/blog/2011/04/firefox-4-launch-party-london-londonfx4/">Firefox 4 launch party in London, England</a> we wanted to show off to the audience why it is such a big thing that we are moving leaps and bounds in the browser market. Here are the slides and notes explaining just how much fun we can have as developers these days if we use browsers to their capacity.</p>
<h2>The slides</h2>
<div style="width:425px" id="__ss_7725765"> <strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/cheilmann/oh-the-things-we-can-do-with-browsers" title="Oh the things we can do with browsers!">Oh the things we can do with browsers!</a></strong> <object id="__sse7725765" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=ohthethingswecandowithbrowsers-110425053732-phpapp02&#038;rel=0&#038;stripped_title=oh-the-things-we-can-do-with-browsers&#038;userName=cheilmann" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse7725765" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=ohthethingswecandowithbrowsers-110425053732-phpapp02&#038;rel=0&#038;stripped_title=oh-the-things-we-can-do-with-browsers&#038;userName=cheilmann" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object>
<div style="padding:5px 0 12px"> View more presentations from <a href="http://www.slideshare.net/cheilmann">Christian Heilmann</a> </div>
</p></div>
<h2>Detailed notes</h2>
<p>Currently there is a lot of confusion what HTML5 is &#8211; a lot of companies abuse the term for marketing of all kind of products and some of the demos showcasing HTML 5 don&#8217;t use it at all.</p>
<p>Today I will promise not to try to sell you any operating system or hardware with it, I won&#8217;t show any comparisons between browsers (how many Goldfish they can show at the same time) and I promise not to go native (although that is the newest fad &#8211; calling HTML5 native).</p>
<p>What HTML5 means to a large degree is that the web is the platform &#8211; more than ever. We used the web with web services and for storage for years but now we move towards a world where we deliver both data and the applications on it.</p>
<p>The browser is the platform and is everything we need to help our end users fulfil the tasks they set out to do. Instead of having software packages for everything we just go to a web app and use this one. The browser offers us APIs and is fast enough nowadays to deliver great experiences. Webmail, collaborative document editing and image manipulation is just a first step in this direction.</p>
<p>The information how to do all that is available for you for free. A lot of HTML5 resources are out there on the web with full documentation and examples of what can be done. The openness of web development makes it unnecessary to go to expensive corporate training courses or buy any software &#8211; all you need is a text editor and a browser.</p>
<p>People are the key. If you look around you, you will find that the leading lights in the HTML5 world are not from one company or were known for years. Instead a lot of them come out of the woodwork totally unexpected which shows that this is much more about enthusiasm than about corporate backing.</p>
<p>We have so many new toys as developers! If I had had all the technologies we can play with across browsers in the past, I am sure my career would have been twice as fast.</p>
<p>Native HTML5 video and audio allows you to manipulate video in any which way and make it talk to the rest of the document. For example:</p>
<ul>
<li>You can <a href="http://hacks.mozilla.org/2011/01/zooming-and-rotating-for-video-in-html5-and-css3/">rotate and zoom Video</a></li>
<li>You can <a href="http://hacks.mozilla.org/2011/03/syncing-page-content-with-html5-video/">sync page content and video content</a></li>
<li>You can <a href="http://hacks.mozilla.org/2010/12/spirit-of-indiana-jones-syncing-html5-video-with-maps/">overlay videos over animated maps</a></li>
<li>You can <a href="http://hacks.mozilla.org/2009/07/video-more-than-just-a-tag/">analyse and manipulate video with Canvas to inject content</a></li>
</ul>
<p>The latter is very interesting as Canvas and SVG (which is not HTML5 but a &#8220;friends&#8221; technology) allows you to go very rich in the interface. Using Canvas and SVG you can also <a href="http://mbostock.github.com/d3/">enhance data in the page to create beautiful visualisations</a>.</p>
<p>Another thing HTML5 offers are richer forms for the web including datepickers, sliders and autocomplete controls with in-built validation. This should safe us a lot of client side validation code that always gets out of sync and is hard to localise.</p>
<p>Other technologies do very much add to what HTML5 gives us:</p>
<p>Offline storage and local storage allows us to build applications that recognise the user&#8217;s connection state and keep them working whilst the connection is down. </p>
<p>CSS3 is the natural progression of CSS and means that we don&#8217;t have to create gradients and rounded corners as graphics but can make them easy to change and maintain in our code itself.</p>
<p>WebGL allows us to create 3D interfaces and animations in the browser. <a href="http://bodybrowser.googlelabs.com/">Google&#8217;s Body Browser</a> is a great example of how rich that can get.</p>
<p>All this becomes very powerful when you mix and match the technologies. You can <a href="http://people.mozilla.com/~prouget/demos/mashup/video.xhtml">clip, transform and run filters like blur over video content</a>, you can <a href="http://people.mozilla.com/~prouget/demos/green/green.xhtml">simulate green screen in video+canvas</a>, apply <a href="http://people.mozilla.com/~prouget/demos/tracker/tracker.xhtml">shape detection to video content</a> or even <a href="http://www.patrick-wied.at/static/nudejs/">detect nudity in photos</a>.</p>
<p>Give this technology to creative people and give them some time and beautiful things can happen. One example is <a href="http://nikebetterworld.com/">Nike&#8217;s better world</a> which looks and acts like a Flash interface but it clean HTML5. Other, ongoing examples of great use of HTML5 technology can be found at <a href="https://demos.mozilla.org/en-US/">the Mozilla demo gallery</a>.</p>
<p>Don&#8217;t think this is not you! We need everyone to play with HTML5 to make it work and be used. So if you have a cool HTML5 demo, please <a href="https://developer.mozilla.org/en-US/demos/submit">send it to us</a> so we can pimp it for you!  </p>
<p>The future of the web is a team effort and can and should not be in the hands of a chosen few following a corporate agenda, so please help us and join in! HTML5 is a debate and the more voices coming from different angles, the better the solution will be. </p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/04/fun-with-new-technologies-at-the-firefox-4-launch-party-in-london/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Firefox 4 for Mobile: Demos!</title>
		<link>http://hacks.mozilla.org/2011/03/webowonder-mobile/</link>
		<comments>http://hacks.mozilla.org/2011/03/webowonder-mobile/#comments</comments>
		<pubDate>Fri, 25 Mar 2011 16:52:27 +0000</pubDate>
		<dc:creator>Paul Rouget</dc:creator>
				<category><![CDATA[Demo]]></category>
		<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[Featured Demo]]></category>
		<category><![CDATA[Firefox 4]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=7672</guid>
		<description><![CDATA[The Release Candidate for Firefox 4 for mobile (Maemo and Android) is out. If you want to see a quick overview of Firefox for Mobile, look at Madhava&#8217;s post. Firefox 4 Desktop, Firefox 4 Mobile: same engine! And this is awesome! It means you will find the same feature in mobile and desktop: HTML5, CSS3 [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.mozilla.com/en-US/mobile/4.0/releasenotes/">Release Candidate for Firefox 4 for mobile</a> (Maemo and Android) is out. If you want to see a quick overview of Firefox for Mobile, look at <a href="http://madhava.com/egotism/archive/005055.html">Madhava&#8217;s post</a>.</p>
<p><strong>Firefox 4 Desktop, Firefox 4 Mobile: same engine!</strong></p>
<p>And this is awesome! It means you will find the same feature in mobile and desktop: HTML5, CSS3 and modern JavaScript APIs. And this is what we want to show you in our new round of demos in <a href="http://webowonder.org">Web O Wonder</a> (<em>This new round also includes more 3D Demos, see this <a href="http://hacks.mozilla.org/2011/03/webowonder-3d/">dedicated blog post</a>)</em>.</p>
<p>Meet 3 new demos:</p>
<ul>
<li><a href="https://mozillademos.org/demos/runfield-mobile/demo.html"><strong>Runfield</strong></a>: a HTML5 Canvas game. We published this game last week for Desktop <a href="https://hacks.mozilla.org/2011/03/runfield/">last week</a>.
<li><a href="https://mozillademos.org/demos/holo-mobile/demo.html"><strong>CSS3 Holo</strong></a> is a little experiment with CSS3 transforms and Orientation Events. We create a parallax effect that simulates depth and offers a 3D illusion.
<li><a href="https://mozillademos.org/demos/dashboard-mobile/demo.html"><strong>HTML5 Dashboard</strong></a> is the mobile version of our previously released <a href="https://mozillademos.org/demos/dashboard/demo.html">HTML5 Dashboard for Desktop</a>.
</ul>
<p><iframe type="text/html" width="500" height="360" src="http://www.youtube.com/embed/9p1SLs_xW20" frameborder="0"></iframe><br />
<em><a href="http://www.youtube.com/watch?v=fT6amnMTrVk">Youtube link</a>.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/03/webowonder-mobile/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>The story of an Audio &amp; WebGL Demo: No Comply</title>
		<link>http://hacks.mozilla.org/2011/03/nocomply/</link>
		<comments>http://hacks.mozilla.org/2011/03/nocomply/#comments</comments>
		<pubDate>Fri, 25 Mar 2011 16:49:27 +0000</pubDate>
		<dc:creator>Paul Rouget</dc:creator>
				<category><![CDATA[Audio]]></category>
		<category><![CDATA[Demo]]></category>
		<category><![CDATA[Firefox 4]]></category>
		<category><![CDATA[WebGL]]></category>

		<guid isPermaLink="false">http://hacks.mozilla.org/?p=7727</guid>
		<description><![CDATA[The audio team is made up of a group Mozilla volunteers who developed the Audio API and, most recently,  a new generation of WebGL demos. This is the story of the development of the No Comply demo. In the fall, after finishing Flight of the Navigator, our team of audio and WebGL hackers was looking [...]]]></description>
			<content:encoded><![CDATA[<p><em><br />
The audio team is made up of a group Mozilla volunteers who developed the <a href="https://developer.mozilla.org/en/Introducing_the_Audio_API_Extension">Audio API</a> and, most recently,  a new generation of WebGL demos. This is the story of the development of the <a href="http://webofwonder.org/en-US/#nocomply">No Comply</a> demo.<br />
</em></p>
<p><a href="http://hacks.mozilla.org/wp-content/uploads/2011/03/nocomply.png"><img src="http://hacks.mozilla.org/wp-content/uploads/2011/03/nocomply-500x293.png" alt="" title="nocomply" width="500" height="293" class="aligncenter size-large wp-image-7738" /></a></p>
<p>In the fall, after finishing <a href="https://demos.mozilla.org/en-US/#fotn">Flight of the Navigator</a>, our team of audio and WebGL hackers was looking for a new challenge.  We&#8217;d finished the new Audio API in time for Firefox 4, and were each maintaining various open web libraries, exploiting the new features of HTML5, Audio, JavaScript, and WebGL. We wanted to take another shot at testing the limits of Firefox 4 &#8211; then, still in beta.</p>
<p><a href="http://kraddyodaddy.com"><img src="http://hacks.mozilla.org/wp-content/uploads/2011/03/kraddy-logo-220x125.png" alt="" title="kraddy-logo" width="220" height="125" style="background-color: black; float: right; padding: 10px; margin: 10px;" /></a></p>
<p><a href="http://twitter.com/binder">Seth Bindernagel</a> had the answer.  He&#8217;d been in contact with a DJ and producer friend named <a href="http://kraddyodaddy.com/">Kraddy</a>, who had just finished an amazing new album.  &#8220;What if we tried to do something with his sound?&#8221;  The idea was too good to pass up, and with Kraddy&#8217;s support, we dove into the tracks and started imagining what these songs might look like, when interpreted through the medium of the web.</p>
<p><q style="font-style: italic; font-size: 1.5em; text-align: left; ">«The web that Firefox 4 makes possible is a web ready for artists, developers, filmmakers, and musicians alike»</q></p>
<p>Kraddy&#8217;s music was easy to demo because of its complex nature, with plenty of emphatic transitions and cue points&#8211;this music wants to be visualized!  The music for No Comply also provided a dark and introspective sound on which to build a narrative.  On <a href="http://kraddyodaddy.com/the-meaning-of-labyrinth/">his blog</a>, Kraddy had already written about how he understood the album&#8217;s meaning:</p>
<blockquote><p>This EP is about Theseus&#8217; decision to be a hero and his decent into the Labyrinth to kill the Minotaur. In a broader sense the EP is about the battle we all face when we challenge ourselves as people.  We must enter the Labyrinth of our minds and at the center we find our  greatest fears.  To defeat those fears we must kill a part of ourselves. And in killing a part of ourselves we create the potential to grow into a more developed person.</p></blockquote>
<p>Kraddy&#8217;s vision informed our early outlines and <a href="http://audioscene.org/scene-files/humph/boards.pdf">storyboards</a>.  We knew that we wanted to play on the story of the Minotaur and the Maze, and the idea of facing down ones&#8217; own fears.  Together we came up with the idea of re-telling the story using a mixture of real-life video and 8-bit video game styling.  Because the album was deeply personal to Kraddy, we decided to feature him in the demo.  Kraddy agreed to be filmed, and <a href="http://twitter.com/remixmanifesto">Brett Gaylor</a> used the footage to create the opening and closing video sequences.  We also used Kraddy as the inspiration for the demo&#8217;s main video game character.</p>
<p><a href="http://audioscene.org/scene-files/humph/boards.pdf"><img src="http://hacks.mozilla.org/wp-content/uploads/2011/03/sb-250x387.png" alt="" title="sb" width="210" style="float: right;  margin: 10px;" /></a></p>
<p>The launch of Firefox 4 brings a lot to the web, not least WebGL.  As the web shifts from a 2D-only to a 2D and 3D space, we wanted to explore the intersection of these two familiar graphical paradigms.  Rather than picking just one, we chose to create a hybrid, dream world, composed of 3D and 2D elements.  Many people will recognize in our 2D characters and graphics an homage to much earlier video games, like Double Dragon.  We wanted to celebrate the fact that these two paradigms can now exist together in a simple web page&#8211;everything we do in the demo is one web page, whether audio, video, 2D, 3D, or text.</p>
<p>Like the <a href="https://demos.mozilla.org/en-US/#fotn">Flight of the Navigator(FOTN)</a> demo before it, we chose the <a href="https://github.com/cjcliffe/CubicVR.js">CubicVR.js</a> engine to drive all the 3D graphics. Over the months leading up to the demo, <a href="http://twitter.com/ccliffe">Charles J. Cliffe</a> had begun the painstaking process of porting features from his C++ engine over to JavaScript.  The simple environment of WebGL and JavaScript allowed for features that even his C++ version did not yet posses to be quickly prototyped.  Many bottlenecks had to be overcome during iterations of the demo, as we wanted to push the limits further than before. The biggest hurdle was visibility and lighting.  Luckily, <a href="http://twitter.com/secretrobotron">Bobby Richter</a> came to the rescue.  Using his experience with Octrees, he was able to work with Charles to produce a visibility and lighting pipeline which provides impressive performance for the task. In contrast, FOTN has no visibility system and was shaded by a single global directional light and ambient surface textures (for window lights, etc.) to simulate the rest.  In No Comply we were able to push the limits with high poly counts and many overlapping point lights and were still able to reach the framerate cap.</p>
<p><a href="http://hacks.mozilla.org/wp-content/uploads/2011/03/nocomply2.png"><img src="http://hacks.mozilla.org/wp-content/uploads/2011/03/nocomply2-500x293.png" alt="" title="nocomply2" width="500" height="293" class="aligncenter size-large wp-image-7739" /></a></p>
<p>Creating a 3D world like the one in this demo requires a lot of original content creation, which in turn requires some sophisticated tools. Instead of developing our own, and in the open-nature of our group, we decided to use existing technology like <a href="http://www.blender.org/">Blender</a>. The community that develops Blender and creates content with it is rich and diverse, and because it&#8217;s an open tool, we could add the features we needed when they weren&#8217;t already present.</p>
<p>Our preference for open technologies also meant that the <a href="https://collada.org/mediawiki/index.php/COLLADA_-_Digital_Asset_and_FX_Exchange_Schema">COLLADA</a> scene format was an obvious choice. Unfortunately, as of version 2.49, Blender exports an Autodesk-inspired format of COLLADA, which isn&#8217;t quite up to the official standard, missing many important bits of information. Fixing this directly in Blender (with a little bit of Python hacking) let CubicVR stay standards-compliant, and let us milk Blender for all of the scene information we could think of using.</p>
<p>The demo&#8217;s 3D modelling, while important, comprises perhaps only half of No Comply&#8217;s original content. An incredible undertaking on the part of <a href="http://twitter.com/thesearethings">Omar Noory</a> provided the textures for the rich environment through which Kraddy rumbles and tumbles. Frequently, spontaneous requests for &#8220;an 8 bit trash can,&#8221; &#8220;a cool sign with our names on it,&#8221; or, &#8220;some beefy bad lookin&#8217; dudes&#8221; were answered almost instantly by Omar&#8217;s gracious and masterful digital pen.  You may have recognized Omar&#8217;s name from his claim to meme-fame with <a href="http://knowyourmeme.com/memes/haters-gonna-hate">&#8220;Haters Gonna Hate&#8221;</a>.</p>
<p>Adding the perfect amount of flare to the graphics pipeline is <a href="http://twitter.com/F1LT3R">Al MacDonald&#8217;s</a> <a href="https://github.com/F1LT3R/burst-core">Burst animation engine</a>. Al not only wrote our sprite animation engine, but also the <a href="https://github.com/F1LT3R/sprite-viking">web-based toolset</a> we used to create the animations.  The 8-bit Kraddy and all of No Comply&#8217;s 8-bit baddies are driven by animation paths prepared with Burst, and engineered with a set of tools that work right inside the browser.</p>
<p><a href="http://hacks.mozilla.org/wp-content/uploads/2011/03/screenshot2.jpg"><img src="http://hacks.mozilla.org/wp-content/uploads/2011/03/screenshot2.jpg" alt="" title="nocomply" width="458" height="230" class="aligncenter size-full wp-image-7743" /></a></p>
<p>In addition to cutting edge graphics with WebGL and &lt;canvas&gt;, we also wanted to explore how far we could push the new <a href="https://wiki.mozilla.org/Audio_Data_API">Firefox 4 Audio API</a> we&#8217;d developed.  The Audio Data API allows us to do many new things with the HTML5 &lt;audio&gt; and &lt;video&gt; tags, such as outputting generated audio and revealing realtime audio data to JavaScript. Libraries like <a href="http://twitter.com/corban">Corban Brook&#8217;s</a> <a href="https://github.com/corbanbrook/dsp.js">DSP.js</a> and and Charles&#8217; <a href="http://sourceforge.net/projects/beatdetektor/">BeatDetektor.js</a> were used to analyze the audio in realtime and trigger various effects and animation sequences. Tracks of audio triggers were also recorded for tighter sequencing of key elements in the song we wanted to emphasize. One of the really new techniques we played with a lot in the demo was controlling GLSL shaders and lighting directly with audio, punching in and out with every beat and clap.  Unlike most treatments of audio on the web, in this demo the song isn&#8217;t a background element, but is woven into the fabric of all the visuals and effects.</p>
<p>Getting  a demo of this scale to work in the browser means figuring out how to make every bit of it work fast, and keep framerates high.  Everything we do in the demo, from loading and parsing massive COLLADA models, to controlling 3D scene graphs, to analyzing real-time audio data, is done with JavaScript.  We think it&#8217;s important to point this out because so many people begin with the assumption that JavaScript isn&#8217;t fast enough for the kind of work we&#8217;re presenting.  The truth is that modern JavaScript, like that in Firefox 4, has been so heavily optimized that we all need to rethink what is and isn&#8217;t possible on the web.</p>
<p>We&#8217;ve taken advantage of a bunch of Firefox 4&#8242;s new performance features, as well as new HTML5 goodies, in order to make this all possible.  For example <a href="https://developer.mozilla.org/en/using_web_workers">Web Workers</a> let us move heavy resource parsing off the main thread, freeing it for audio analysis and 3D effects. While a large portion of each second is consumed by simply pushing information to the video card, it isn&#8217;t necessary for the browser to wait for that to happen. In the background, we can use other threads to load and parse data, so that it&#8217;s ready to draw when the main thread needs it. Of course, a host of problems arise immediately whenever concurrency is involved, but we managed to draw a large performance and overall stability increase by utilizing Web Workers.</p>
<p>Another performance trick was using JavaScript <a href="https://developer.mozilla.org/en/JavaScript_typed_arrays">Typed Arrays</a>, which give us a tremendous speed boost when working with audio and pixel data. When you&#8217;re analyzing slices of audio data hundreds of bytes wide as fast as possible, your Fourier Transform code needs to be blazingly quick. Thanks to Corban&#8217;s highly optimized dsp.js library, this was hardly on our minds.</p>
<p>Next, we spent a lot of time optimizing our JavaScript so that it could take advantage of Firefox&#8217;s <a href="https://developer.mozilla.org/En/SpiderMonkey/Internals/Tracing_JIT">Tracing</a> and Method JIT. Writing code that can be easily byte-compiled by the browser makes sure that anything we write runs as fast as possible. This is a fairly new and surprising concept, especially to those who remember the JavaScript of yesterday.
<p>Part of what appealed to us about writing this demo was that it let those of us who are browser developers, and those of us who are web developers, work together on a single project.  Most of the technology showcased in this demo was made on bleeding edge Firefox nightlies and our development process involved lots of feedback about performance or stability issues in the browser.  <a href="http://twitter.com/humphd">Dave Humphrey</a> focused on the internals of the Audio API, instrumenting and profiling our JavaScript, and helped us work closely with Mozilla&#8217;s JavaScript, graphics, and WebGL engineers.  People like Benoit Jacob and Boris Zbarsky, among others, were indispensable as we worked to fix various bottlenecks.  Part of what makes Mozilla such a successful project is that their engineers are not locked away, unable to work with web developers.  Having engineers at our beck and call was essential to our success with such a demanding schedule, and we were proud to be able to help Mozilla test and improve Firefox 4 along the way.</p>
<p>Beyond the technical aspects of the demo, it also points to the spirit of how these technologies are meant to be used.  We worked as a distributed team during evenings and on weekends, to plan and code and create everything, from the tools we needed to the graphical resources to the demo&#8217;s final code.  Some of our team are browser developers, some web and audio hackers, others are graphic designers or filmmakers, still others storytellers and writers&#8211;everyone had a place around the table, and a role to play.  We think this is part of what makes the web such a powerful platform for creative and collaborative work: there isn&#8217;t one right way to be, no single technology you need to know, and the techniques and tools are democratized and open to anyone willing to pick them up.  <strong>The web that Firefox 4 makes possible is a web ready for artists, developers, filmmakers, and musicians alike</strong>.</p>
<style>
p {text-align: justify}
</style>
]]></content:encoded>
			<wfw:commentRss>http://hacks.mozilla.org/2011/03/nocomply/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

