<?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</title>
	<atom:link href="https://hacks.mozilla.org/feed/" rel="self" type="application/rss+xml" />
	<link>https://hacks.mozilla.org</link>
	<description>hacks.mozilla.org</description>
	<lastBuildDate>Tue, 03 Dec 2013 09:42:11 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.7.1</generator>
	<item>
		<title>How the Manana app was built</title>
		<link>https://hacks.mozilla.org/2013/12/how-the-manana-app-was-built/</link>
		<comments>https://hacks.mozilla.org/2013/12/how-the-manana-app-was-built/#comments</comments>
		<pubDate>Tue, 03 Dec 2013 09:40:23 +0000</pubDate>
		<dc:creator><![CDATA[Alberto Granzotto]]></dc:creator>
				<category><![CDATA[Apps]]></category>
		<category><![CDATA[Firefox OS]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23160</guid>
		<description><![CDATA[We saw Firefox OS as a great opportunity and a challenge to deliver a product true to the values of open web and best standards. We found it exciting to be able to deliver an app that will offer a smooth UX even for people using lower end devices. As many of the users have [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>We saw Firefox OS as a great opportunity and a challenge to deliver a product true to the values of open web and best standards. We found it exciting to be able to deliver an app that will offer a smooth UX even for people using lower end devices. As many of the users have a very limited data plan we decided that a proper reading app offering the possibility to enjoy their favourite articles offline and without unnecessary distractions would be a key product. And so we decided to build Manana.</p>
<p>Here is the story of how we did it.</p>
<p><img alt="" src="https://hacks.mozilla.org/wp-content/uploads/2013/12/01-Splashpage.png" /></p>
<p>On a technological side, we decided to go vanilla but with a little help from <a href="http://zeptojs.com/">ZeptoJS</a> to handle the DOM manipulation. We also integrated Web Activities to hook into the sharing options in the browser, and to share the selected links via email. We are using also the Readability API to have a nice formatted version of the link. The app is simple and that&#8217;s how we wanted it to be. but we have a great plus: it is internationalized in English, Spanish, Portuguese, Polish, German, Italian. Hungarian and Greek are coming soon.</p>
<p>You can <a href="https://marketplace.firefox.com/app/manana">install Manana from the Firefox Marketplace</a>.</p>
<p><a href="https://marketplace.firefox.com/app/manana"><img alt="" src="https://hacks.mozilla.org/wp-content/uploads/2013/11/manana-marketplace.png" /></a></p>
<h2>Building the layout and managing transitions between the views</h2>
<p>To provide a branded UI/UX, we use a small framework built in-house. With some clever CSS properties and a few lines of JavaScript (to handle the visibility of the Views) the whole thing was quite easy to set up. Basically, we have each View arranged like cards in a deck. Every time we need one of them, we pull it out while the old one goes in the deck again (becoming hidden). When the animation is ended, we capture the <code>animationend</code> event, so right after that we remove the class that brings the animation properties and at the same time we add a <code>current</code> class, which lets the card stay in place and be visible. The old one is hidden automatically since that&#8217;s its default state.</p>
<p><img alt="" src="https://hacks.mozilla.org/wp-content/uploads/2013/12/03-Drawer.png" /></p>
<p>Below you will find a small example of how it works with a <code>moveRight</code> animation.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/how-Views-move.gif"><img title="Showing the way Views move" alt="Showing the way Views move" src="https://hacks.mozilla.org/wp-content/uploads/2013/12/animation-gif-app.gif" /></a></p>
<h2>Optimize CSS3 transitions</h2>
<p>Every smartphone comes with at least a small GPU, so that the CPU doesn’t have to do all the graphical calculations. It’s very much in fashion to take advantage of the Hardware Acceleration and so we do. There are some CSS3 properties that are automatically handled by the GPU, but others are not &#8211; usually the ones that are related to 3D. This is because the element (tied to the animated property) is <em>promoted</em> to a new layer that doesn’t suffer from repaints imposed by other properties (like <code>left</code>, for example. But this can change in the future).</p>
<p><strong>tl;dr</strong> Where you can, avoid expensive CPU work, especially on big or complicated elements. Here is a small extract of how we implemented such properties.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* moveLeftIn directive */</span>
<span style="color: #6666ff;">.moveLeftIn</span> <span style="color: #00AA00;">&#123;</span>
    animation<span style="color: #00AA00;">:</span> 0.5s moveLeftIn cubic-bezier<span style="color: #00AA00;">&#40;</span>…<span style="color: #00AA00;">&#41;</span> forwards<span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* 1 */</span>
    backface-visibility<span style="color: #00AA00;">:</span> <span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* 2 */</span>
    transform<span style="color: #00AA00;">:</span> translateY<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span> translateX<span style="color: #00AA00;">&#40;</span><span style="color: #933;">99%</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* 3 */</span>
    <span style="color: #808080; font-style: italic;">/* 1. `forwards` let the animation stop at the last keyframe */</span>
    <span style="color: #808080; font-style: italic;">/* 2. avoid some possible glitches caused by 3D rendering */</span>
    <span style="color: #808080; font-style: italic;">/* 3. take advantage of the GPU with 3D-related properties */</span>
<span style="color: #00AA00;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* moveLeftIn animation */</span>
<span style="color: #a1a100;">@keyframes moveLeftIn {</span>
    <span style="color: #933;">0%</span>   <span style="color: #00AA00;">&#123;</span> transform<span style="color: #00AA00;">:</span> translateY<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span> translateX<span style="color: #00AA00;">&#40;</span><span style="color: #933;">99%</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">999</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> translateY<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span> translateX<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>   <span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">999</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<h2>Hooking to the Firefox OS specific Web Activities</h2>
<p>One of our favourite APIs of the WebAPIs, is <a href="https://developer.mozilla.org/en-US/docs/WebAPI/Web_Activities">Web Activities</a>. An application emits an Activity (for example, <em>save bookmark</em>) then the user is prompted to chose among other applications that handles that activity. At the moment it is possible to register an application as an activity handler through the app manifest (<em>declaration registration</em>), but in the future this could be done inside the app itself (<em>dynamic registration</em>).</p>
<p><img alt="" src="https://hacks.mozilla.org/wp-content/uploads/2013/12/04-SaveToManana.png" /></p>
<p>Saving a link from the browser to Manana is an example of Web Activity (<em>save-bookmark</em>). Registering an activity is trivial:</p>
<h3>manifest.webapp</h3>

<div class="wp_syntax"><table><tr><td class="code"><pre class="json" style="font-family:monospace;">&quot;activities&quot;: {
    &quot;save-bookmark&quot;: {
      &quot;disposition&quot;: &quot;inline&quot;,
        &quot;index.html#/savebookmark&quot;,
        &quot;returnValue&quot;: true
    }
},</pre></td></tr></table></div>

<p><strong>returnValue</strong> is of great importance, otherwise the handler application (Manana in our case) will never return to the caller (Browser). The handling in the app is also very easy:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">navigator.<span style="color: #660066;">mozSetMessageHandler</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;activity&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>activity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ... <span style="color: #660066;">check</span> <span style="color: #000066; font-weight: bold;">if</span> activity is savebookmark
&nbsp;
    <span style="color: #000066; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> activity.<span style="color: #660066;">source</span>.<span style="color: #660066;">data</span><span style="color: #339933;">;</span>
&nbsp;
    ... <span style="color: #000066; font-weight: bold;">do</span> stuff with activity data
&nbsp;
    activity.<span style="color: #660066;">postResult</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;saved&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><img alt="" src="https://hacks.mozilla.org/wp-content/uploads/2013/12/05-ArticleSaved.png" /></p>
<p>The applications included in your phone are part of <a href="https://github.com/mozilla-b2g/gaia">Gaia</a>, the UI of Firefox OS, and you can actually browse the source. They provide a lot of examples on how to use WebAPIs. If you are interested in the <strong>save-bookmark</strong> activity, have a look at the <a href="https://github.com/mozilla-b2g/gaia/tree/master/apps/homescreen">Homescreen app source code</a>.</p>
<p>Manana users can also share their resources via email, directly from the app. To achieve that, we&#8217;ve implemented the <code>email</code> Web Activity. It&#8217;s easy to pre-populate the email subject and body, just follow the <code>mailto</code> URI scheme and you are done.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">DB.<span style="color: #660066;">Get</span><span style="color: #009900;">&#40;</span>id<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>rs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> subject       <span style="color: #339933;">=</span> rs.<span style="color: #660066;">title</span><span style="color: #339933;">,</span>
        body          <span style="color: #339933;">=</span> rs.<span style="color: #660066;">url</span><span style="color: #339933;">,</span>
        shareActivity <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> MozActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            name<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;new&quot;</span><span style="color: #339933;">,</span>
            data<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;mail&quot;</span><span style="color: #339933;">,</span>
                url <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;mailto:?body=&quot;</span> <span style="color: #339933;">+</span> encodeURIComponent<span style="color: #009900;">&#40;</span>body<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span>
                      <span style="color: #3366CC;">&quot;&amp;subject=&quot;</span> <span style="color: #339933;">+</span> encodeURIComponent<span style="color: #009900;">&#40;</span>subject<span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Retrieving and storing data with IndexedDB</h2>
<p>IndexedDB is a client side NoSQL DB. It has some nice features which make it easier working with app updates and is easy to integrate in your app, even if you&#8217;re using a framework like AngularJS. Setting up models is fairly easy and extensively documented on the web and in Gaia Apps.</p>
<p>You don&#8217;t have a quota limit, although the Firefox OS Simulator seems to have a limit when dealing with datasets larger than 5mb. Each database is tied to an app and a version. Only one version can run at a time, which makes it easier not to break compatibility, especially if you rely on some external API.</p>
<p>Since it is asynchronous, dealing with IndexedDB operations requires having callback. Below is an example on how we deal with the action of adding a link:</p>
<ol>
<li>Call Link.Add function, passing a link object and a success callback</li>
<li>Link.Add get a DB connection by calling getDB
<ul>
<li>In the case a DB does not exist, the <strong>onupgradeneeded</strong> callback is executed
<ul>
<li>Create DB</li>
<li>Create collections</li>
<li>Create indexes</li>
</ul>
</li>
<li>getDB check if the connection is already open, otherwise it opens a new one</li>
</ul>
</li>
<li>Link.Add fills the missing property of links with default values</li>
<li>We call the objectStore <strong>put</strong> method to save/update new data into the collection
<ul>
<li>If the Link object we are going to save already contains a primary key attribute, IndexedDB does an update</li>
<li>Otherwise, an insert</li>
</ul>
</li>
<li>If the <strong>put</strong> method is successful it calls a success callback, returning the primary key in <strong>event.target.result</strong></li>
</ol>
<p>Below is an example of DB action defined in our models: (for the sake of brevity we omitted error checking code)</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">Add<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> onsuccess<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    getDB<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>db<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">var</span> transaction <span style="color: #339933;">=</span> db.<span style="color: #660066;">transaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;links&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;readwrite&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                objectStore <span style="color: #339933;">=</span> transaction.<span style="color: #660066;">objectStore</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;links&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                request <span style="color: #339933;">=</span> objectStore.<span style="color: #660066;">put</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            request.<span style="color: #660066;">onsucces</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                onsuccess<span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">target</span>.<span style="color: #660066;">result</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// e.target.result is the id of the new Link</span>
            <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></td></tr></table></div>

<p>This is how you call it from the other part of the app:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> newLink <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://zombo.com&quot;</span><span style="color: #339933;">,</span>
               title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Welcome to zombo com&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Link.<span style="color: #660066;">Add</span><span style="color: #009900;">&#40;</span>newLink<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>linkId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    window.<span style="color: #660066;">location</span>.<span style="color: #660066;">hash</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;#/read/:&quot;</span> <span style="color: #339933;">+</span> linkId<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Enhance localization with Markdown support</h2>
<p>Localization is a key feature in our app. Manana currently supports 6 languages (soon it will be 8). There is no official recommendation, but after some research we decided to use the same <a href="https://hacks.mozilla.org/2013/08/localizing-firefox-os-apps/">technique</a> that Gaia is using. This is a great <em>feature</em> of working with Firefox OS: the whole source code is there, and you can get inspiration by reading it.</p>
<p>L10n has been used in two different cases: to translate the <code>text</code> content of DOM elements and to translate internal help pages. To make the Localization process as simple as possible, we avoided putting HTML tags in the strings and in the internal help pages; we used markdown instead. By design, we avoided having strings in the Javascript code, all messages and alerts are contained in the HTML.</p>
<p>To support markdown we added some glue between the l10n.js library and <a href="https://github.com/chjj/marked">marked.js</a>.</p>
<p>To load the help resources, Manana first does a lookup in the current installation to find the file named <code>&lt;resourceName&gt;.&lt;locale&gt;.md</code>. If the lookup fails, the fallback is the English version of the resource. Once the file is retrieved succesfully, the content is applied to the <code>marked</code> function to generate the HTML, and then added to the DOM.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> resourceInit<span style="color: #009900;">&#40;</span>$oldView<span style="color: #339933;">,</span> $newView<span style="color: #339933;">,</span> resource<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> fallback  <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;/locales/resources/&quot;</span> <span style="color: #339933;">+</span> resource <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;.en.md&quot;</span><span style="color: #339933;">,</span>
        localized <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;/locales/resources/&quot;</span> <span style="color: #339933;">+</span> resource <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;.&quot;</span> <span style="color: #339933;">+</span> getLanguage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;.md&quot;</span><span style="color: #339933;">,</span>
        renderMd  <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>txt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.js-subpage&quot;</span><span style="color: #339933;">,</span> $newView<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>marked<span style="color: #009900;">&#40;</span>txt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        renderError  <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>txt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.js-subpage&quot;</span><span style="color: #339933;">,</span> $newView<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ouch, cannot find: &quot;</span> <span style="color: #339933;">+</span> resource<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    getFile<span style="color: #009900;">&#40;</span>localized<span style="color: #339933;">,</span>
            renderMd<span style="color: #339933;">,</span>
            <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> getFile<span style="color: #009900;">&#40;</span>fallback<span style="color: #339933;">,</span> renderMd<span style="color: #339933;">,</span> renderError<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>A note on retrieving local files with a XMLHttpRequest</h2>
<p>Retrieving files asynchronously from your local app is as easy as doing a <code>XMLHttpRequest</code>, but there is a case that took us some time to sort out. If the local file you are retrieving is missing, the request won&#8217;t fire the <code>success</code> callback (of course), but neither the <code>error</code> callback. It will just hang there forever.</p>
<p>After some research, we noticed a <code>try { ... } catch</code> block in the <code>l10n.js</code> library. Basically, &#8220;in Firefox OS with the <code>app://</code> protocol, trying to XHR a non-existing URL will raise an exception.&#8221;</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> getFile <span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> success<span style="color: #339933;">,</span> error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> xhr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    xhr.<span style="color: #660066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;get&quot;</span><span style="color: #339933;">,</span> url<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    xhr.<span style="color: #660066;">setRequestHeader</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Content-Type&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;text/plain;charset=UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    xhr.<span style="color: #660066;">onreadystatechange</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</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>xhr.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">4</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>xhr.<span style="color: #660066;">status</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">200</span> <span style="color: #339933;">||</span> xhr.<span style="color: #660066;">status</span> <span style="color: #339933;">===</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
                success<span style="color: #009900;">&#40;</span>xhr.<span style="color: #660066;">responseText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">else</span>
                error <span style="color: #339933;">&amp;&amp;</span> error<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: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    xhr.<span style="color: #660066;">onerror</span> <span style="color: #339933;">=</span> error<span style="color: #339933;">;</span>
    xhr.<span style="color: #660066;">ontimeout</span> <span style="color: #339933;">=</span> error<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Here it is!</span>
    <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
        xhr.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</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;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        error <span style="color: #339933;">&amp;&amp;</span> error<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: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Keeping the UI synchronized with the Database</h2>
<p>One of the most challenging aspect of an HTML app is synchronization of state between a data source (db, file, api, etc) and the UI. Frameworks like Backbone and Angular make your life easier but they are not easy beasts to tame. Going vanilla is fun, mostly because JavaScript gives you a lot of flexibility when deciding how to do things.</p>
<p>We really like OO programming by message passing, so we came up with a message based architecture. The <code>body</code> element acts as our global dispatcher while view root element is the local dispatcher. Global events get registered when the application starts and live as long as the app. Local events get registered when entering a view and live until we leave that view.</p>
<p>An example of global events are the CRUD operations we want to perform on our model.</p>
<p>Each action on the UI which changes the state of models is tied to a global event, registered on <code>document.body</code>. On entering a new view, we call a constructor function, which executes some initialization code.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> events <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// global events are never unbinded</span>
        global<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">&quot;save-link&quot;</span><span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
            <span style="color: #3366CC;">&quot;remove-link&quot;</span><span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
            ...
&nbsp;
            <span style="color: #3366CC;">&quot;update-link-collecetion&quot;</span><span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
            ...
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// view specific events are unbinded everytime we leave that view</span>
        home<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">&quot;update-link-collection-complete&quot;</span><span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">var</span> $caller <span style="color: #339933;">=</span> e.<span style="color: #660066;">detail</span>.$caller<span style="color: #339933;">;</span>
&nbsp;
                ...
            <span style="color: #009900;">&#125;</span>  
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
        ...
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">function</span> homeInit<span style="color: #009900;">&#40;</span>$oldView<span style="color: #339933;">,</span> $newView<span style="color: #339933;">,</span> dispatch<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// $oldView is the previous view</span>
        <span style="color: #006600; font-style: italic;">// $newView is the current view</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// dispatch in an helper which wrap </span>
        <span style="color: #006600; font-style: italic;">// document.body.dispatchEvent(new CustomEvent(...))</span>
        dispatch<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;update-link-collection&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Leaving a view calls a destructor on that view, and unbinds all the local events. At the end of the day we can open the app source code without spending hours trying to figure out how things work because of framework related black magic.</p>
<h2>Integration with git</h2>
<p>When it comes to Git, our hearts melt. We love git. Seriously. Git and Vim are the best pieces of software ever written. Love.</p>
<p>Ok, lets get serious again.</p>
<p>Our application is composed from two components, the app itself and an API Server. The API server is a Golang API proxy to Readability API. So our app directory layout looks like this:</p>
<pre>manana/
 |- distrib.sh
 |- srv/
 |- app/</pre>
<p><strong>distrib.sh</strong> is a simple bash script which perform three simple tasks:</p>
<ul>
<li>Install a post-{commit, merge, rebase} hook, if not already installed</li>
<li>Update <strong>app/js/revision.js</strong> writing the current git revision hash.</li>
<li>Create a clean zip containing the app, ready for the app store or your device.</li>
</ul>
<p>This makes our lives easier.</p>
<h2>Conclusion. What Next?</h2>
<p>Manana relies on the Readability API to get the stripped down version of the web page. Readability is doing a really good job, but we&#8217;d like to experiment with other solutions, like parsing the page directly in the device. It&#8217;s a hard job, but it&#8217;d be cool to do it on the client side, and some work has already <a href="http://code.google.com/p/arc90labs-readability/source/browse/trunk/js/readability.js">been done</a>.</p>
<p>We had a great time developing for Firefox OS. The most important thing for us, techwise, is that we developed a webapp, not an <strong>OS-specific app</strong>. We can use all the know-how we gained in the past years, all the great open source libraries we want, and we can even use the browser debug tools we use every day.</p>
<p>To help newcomers in building a Firefox OS app, we have created <a href="http://tmp.devcharm.com/pages/10-resources-you-need-to-develop-a-FirefoxOS-app">a list of resources</a> we found indispensable.</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/12/how-the-manana-app-was-built/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Firefox and FireCAT as a Platform for Ethical Hacking</title>
		<link>https://hacks.mozilla.org/2013/11/firefox-and-firecat-as-a-platform-for-ethical-hacking/</link>
		<comments>https://hacks.mozilla.org/2013/11/firefox-and-firecat-as-a-platform-for-ethical-hacking/#comments</comments>
		<pubDate>Thu, 28 Nov 2013 09:19:46 +0000</pubDate>
		<dc:creator><![CDATA[Maximiliano Soler]]></dc:creator>
				<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23129</guid>
		<description><![CDATA[Some years ago &#8211; in early 2007, while working as freelancers &#8211; we were challenged to do a penetration test on a web application. It was really simple but had a condition-based methodology, and therefore was impossible to use any automated tool; we could only use a web browser. Our solution was to convert Firefox [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Some years ago &#8211; in early 2007, while working as freelancers &#8211; we were challenged to do a penetration test on a web application. It was really simple but had a condition-based methodology, and therefore was impossible to use any automated tool; we could only use a web browser.</p>
<p>Our solution was to convert Firefox into a Platform for Ethical Hacking.</p>
<h2>How did we do it?</h2>
<p>We searched the Web, looking for a solution that satisfied the requirements and obligations to not use automated tools. We found many, a lot of them being add-ons and extensions. Many of them were unknown, unnoticed and unofficial.</p>
<p>Having compiled some useful findings, our next challenge was to create something that maintained the essence of Mozilla Firefox: open, free and easy for everyone. The project was named <a href="http://firecat.toolswatch.org">FireCAT (Firefox Catalog of Auditing exTensions)</a></p>
<p><a href="http://firecat.toolswatch.org"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/Firecat.png" alt="" /></a></p>
<h2>What is FireCAT?</h2>
<p>FireCAT is a mind map organized into different categories and subcategories with a variety of add-ons available. These are themed around application security auditing and assessment, adding news functionality, customizing your browser and providing the ability to adapt it to different user&#8217;s need.</p>
<h3>Current Status</h3>
<p>The <a href="http://firecat.toolswatch.org/news.html">latest version is 2.0</a>, and was presented at Black Hat Arsenal during 2011. From the web site it is possible to list the content and browse the categories, directly accessing the add-ons on <a href="https://addons.mozilla.org/en-US/firefox/">Mozilla Add-ons</a>; you can also download the entire mind map with the categories:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/FireCAT_000_Mindmap.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/FireCAT_000_Mindmap-500.png" alt="" /></a></p>
<h3>What&#8217;s New?</h3>
<ul>
<li>Current Version: 2.0</li>
<li>Add-ons: > 90</li>
<li>Categories: 7</li>
<li>Subcategories: 19</li>
</ul>
<p>FireCAT v2.0 &#8211; <a href="http://firecat.toolswatch.org/the_catalog.html">Firefox Catalog of Auditing exTensions<br />
</a></p>
<ul>
<li>Information Gathering
<ul>
<li>Whois</li>
<li>Location Info</li>
<li>Enumeration &amp; Fingerprint</li>
<li>Data Mining</li>
<li>Googling &amp; Spidering</li>
</ul>
</li>
<li>Proxies &amp; Web Utilities</li>
<li>Editors</li>
<li>Network Utilities
<ul>
<li>Intrusion Detection System</li>
<li>Sniffers</li>
<li>Wireless</li>
<li>Passwords</li>
<li>Protocols &amp; Applications</li>
</ul>
</li>
<li>Misc
<ul>
<li>Tweaks &amp; Hacks</li>
<li>Encryption / Hashing</li>
<li>Antivirus &amp; Malware Scanner</li>
<li>Anti Spoof</li>
<li>Anti-phishing / Pharming / Jacking</li>
<li>Automation</li>
<li>Logs / History</li>
<li>Backup &amp; Synchronization</li>
<li>Protection</li>
</ul>
</li>
<li>IT Security Related</li>
<li>Application Auditing</li>
</ul>
<h2>What can we find in the categories?</h2>
<p>The 7 main categories are as follows:</p>
<h3>Information Gathering</h3>
<p>An important part of the process of conducting vulnerability assessment or penetration testing is related to obtaining information in a passive or active way about our objective, to be analyzed later.</p>
<p>From the Firefox platform, we could identify IP addresses, query DNS, verify HTTP headers, generate reports of domains via Whois, use different search engines, and identify applications and versions from Web Server, modules, etc.</p>
<h4>Example</h4>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/FireCAT_001_Show-IP.png" alt="" /></p>
<p>&#8220;Show IP&#8221; shows the IP address(es) of the current page in the status bar. It also allows querying of custom services by IP (right mouse button) and hostname (left mouse button), like whois, netcraft, etc.</p>
<h3>Proxies &amp; Web Utilities</h3>
<p>Sometimes it&#8217;s necessary to use proxies to connect with different hosts or through networks for exaple using TOR to hide our real IP, analyze time responses, detect load balancer, etc.</p>
<h4>Example</h4>
<p>The User Agent Switcher extension adds a menu and a toolbar button to switch the user agent of the browser:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/FireCAT_002_User-Agent-Switcher.png" alt="" /></p>
<h3>Editors</h3>
<p>From the ubiquitous FireBug to different JavaScript or XML editors, the available editors can help us to visualize and modify content as required.</p>
<h4>Example</h4>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/FireCAT_003_JavaScript_Deobfuscator.png" alt="" /></p>
<p>The JavaScript Deobfuscator will show you what JavaScript gets to run on a web page, even if it is obfuscated and generated on the fly.</p>
<h3>Network Utilities</h3>
<p>In this section you can use and connect with applications and services such as FTP, DNS and databases in real time.</p>
<h3>Miscellaneous</h3>
<p>This section contains a lot of useful add-ons, for example tools to convert your ASCII into md5, sha1, base64, hex, etc., and methods to identify the WOT (Web of Trust) in a web site, guage reputation and ratings, modify your HTTP Referer, automate actions through Macros, and make backups of your configuration and installed addons.</p>
<h4>Example</h4>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/FireCAT_004_Certificate-Patrol.png" alt="" /></p>
<p>The Certificate Patrol add-on reveals when certificates are updated, so you can ensure it was a legitimate change.</p>
<h3>IT Security Related</h3>
<p>Sometimes it is necessary to use search engines to find information within some Web sites, content related to errors, configurations, and news vulnerabilities by CVE.</p>
<h3>Application Auditing</h3>
<p>The HackBar is probably the best known addon in this group, but I recommend you take a deeper look because many of them are useful. For example, Exploit-Me&#8217;s suite to perform vulnerability analysis of cross-site scripting and SQL Injection.</p>
<h4>Example</h4>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/FireCAT_005_SQLite-Manager.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/FireCAT_005_SQLite-Manager-500.png" alt="" /></a></p>
<p>The SQLite Manager allows you to manage any SQLite database on your computer.</p>
<h2>Recommended add-ons</h2>
<p>The top ten recommended add-ons would be:</p>
<ul>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/1843">Firebug</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/60">Web Developer</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/3899">HackBar</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/2464">FoxyProxy</a></li>
<li><a href="http://labs.securitycompass.com/index.php/exploit-me">Exploit Me</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/966">Tamper Data</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/3863">iMacros for Firefox</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/5817">SQLite Manager</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/3829">Live HTTP Headers</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/6196">PassiveRecon</a></li>
</ul>
<h2>Final Words</h2>
<p>I invite you to explore the different add-ons and extensions available, identifying which ones are most interesting and helpful for your work, and their relative performance and compatibilities. Firefox was a great help during our work!</p>
<p>If you decide to install a large amount of add-ons, memory usage might be a problem: consider this carefully.</p>
<p>Feel free to send us your comments and recommendations for new options to add to the mind map!</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/firefox-and-firecat-as-a-platform-for-ethical-hacking/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Implementing In-App Payments in Your Firefox OS App</title>
		<link>https://hacks.mozilla.org/2013/11/implementing-in-app-payments-in-your-firefox-os-app/</link>
		<comments>https://hacks.mozilla.org/2013/11/implementing-in-app-payments-in-your-firefox-os-app/#comments</comments>
		<pubDate>Tue, 26 Nov 2013 20:29:36 +0000</pubDate>
		<dc:creator><![CDATA[James Long]]></dc:creator>
				<category><![CDATA[Apps]]></category>
		<category><![CDATA[Firefox OS]]></category>
		<category><![CDATA[Payments]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=21950</guid>
		<description><![CDATA[At Mozilla, we have been working on bringing payments to the web. We think it is important that developers have an easy way to monetize their apps. It also gives us the chance to offer deeper platform integration, allowing things like carrier billing in addition to credit/debit cards. We&#8217;ve talked about in-app payments before in [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>At Mozilla, we have been working on bringing payments to the web. We think it is important that developers have an easy way to monetize their apps. It also gives us the chance to offer deeper platform integration, allowing things like carrier billing in addition to credit/debit cards.</p>
<p>We&#8217;ve talked about in-app payments before in <a href="https://hacks.mozilla.org/2013/04/introducing-navigator-mozpay-for-web-payments/">navigator.mozPay() For Web Payments</a>, but this is an overview from an app developer&#8217;s standpoint. This also includes a few updates, such as real payments being live in the Firefox Marketplace. Payments are different than simply being a paid app, which charges the user once on the initial purchase. In-app payments is a different workflow, and allows you to charge the customer for specific items within your app.</p>
<h2>Architecture Overview</h2>
<p>Payments are only enabled on Firefox OS devices for now. We are working on enabling them across other platforms, even desktop Firefox, but that is a ways off. For the rest of this article, we will assume the Firefox OS environment.</p>
<p>The <a href="https://developer.mozilla.org/en-US/docs/Web/Apps/Publishing/In-app_payments#Overview">MDN page for in-app payments</a> has a good overview of the process, but here&#8217;s an even quicker overview. You need a server to generate the payments requests, because otherwise a hacker could easily change the prices to 0. The server creates <a href="http://openid.net/specs/draft-jones-json-web-token-07.html">JWT</a> tokens, passes them to your app, and your app calls <code>navigator.mozPay</code> with the token. The user will be taken to a built-in payments workflow, and when finished, your server will be notified. The <a href="https://hacks.mozilla.org/2013/04/introducing-navigator-mozpay-for-web-payments/">previous Hacks article on payments</a> goes into the lower-level mechanics and reasons for payments more thoroughly.</p>
<p>It&#8217;s up to you to handle the rest of the workflow: updating the app when an item is successfully purchased, linking purchases to accounts, etc.</p>
<h2>Getting a Simulation Key</h2>
<p>In order to use payments, you’ll need use the app key and secret generated for you by the Marketplace. If you just want to simulate payments, you can easily <a href="https://marketplace.firefox.com/developers/in-app-keys/">grab a testing key and secret</a>. The only thing you need to do is create an account if you haven&#8217;t done so already.</p>
<p>You will only be able to simulate payments with that key. When you are ready to use real money, you need to submit your app to the Marketplace, setup payments, and obtain a real key and secret. All of this is explained in more detail below.</p>
<h2>Setting up the Server-Side Code</h2>
<p>The first thing you need to do is set up the server-side code. A &#8220;mozpay&#8221; module exists in <a href="https://developer.mozilla.org/en-US/docs/Web/Apps/Publishing/In-app_payments#Code_libraries">a few different languages</a> which implements the payment workflow for you. If it does not exist in your programming language, you&#8217;ll need to use a <a href="https://developer.mozilla.org/en-US/docs/Web/Apps/Publishing/In-app_payments#Code_libraries">JWT module</a> and do some more legwork. Check the rather <a href="https://github.com/mozilla/mozpay-js/blob/master/lib/mozpay.js">simple code</a> from <a href="https://github.com/mozilla/mozpay-js">mozpay-js</a> for an example of what you need to do.</p>
<p>I wrote a game for Firefox OS called <a href="https://github.com/jlongster/webfighter">Webfighter</a> which uses <a href="http://nodejs.org/">node.js</a> as the backend, so I used <a href="https://github.com/mozilla/mozpay-js">mozpay-js</a> on the server-side. Webfighter is a shooter with a store where you can buy more weapons and ships, and it&#8217;s a good reference for developers to see actual code for working with payments. Examples below are taken from Webfighter. <a href="https://github.com/jlongster/webfighter">You can check out Webfighter&#8217;s source here.</a></p>
<p>First, require and configure the mozpay module:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> pay <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mozpay'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> settings <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'settings'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
pay.<span style="color: #660066;">configure</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    mozPayKey<span style="color: #339933;">:</span> settings.<span style="color: #660066;">payKey</span><span style="color: #339933;">,</span>
    mozPaySecret<span style="color: #339933;">:</span> settings.<span style="color: #660066;">paySecret</span><span style="color: #339933;">,</span>
    mozPayAudience<span style="color: #339933;">:</span> <span style="color: #3366CC;">'marketplace.firefox.com'</span><span style="color: #339933;">,</span>
    mozPayType<span style="color: #339933;">:</span> <span style="color: #3366CC;">'mozilla/payments/pay/v1'</span><span style="color: #339933;">,</span>
    mozPayRoutePrefix<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/mozpay'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The <code>settings</code> module is just a file with JSON that specifies configuration of the app. It&#8217;s nice to separate the configuration so that you can easily swap it out in different environments, like development and production.</p>
<p>Use the key and secret that you got from the Marketplace. You should only change the audience and type options if you are using a different payment server than Mozilla&#8217;s. The routePrefix is optional and specifies the URL prefix for postbacks from the payment server.</p>
<p>Next you need to define a route that creates the data used for the payment transaction. The client is the one making the actual payments request (so that you don&#8217;t have to handle credit card details, and it&#8217;s possible to even be charged through carrier billing). You just need to create an object that represents a purchase. This example uses <a href="http://expressjs.com/">express.js </a>as the http server.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> purchaseQueue <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
app.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/sign-jwt'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>req<span style="color: #339933;">,</span> res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> token <span style="color: #339933;">=</span> <span style="color: #3366CC;">'o'</span> <span style="color: #339933;">+</span> <span style="">Date</span>.<span style="color: #660066;">now</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;">var</span> item <span style="color: #339933;">=</span> getItem<span style="color: #009900;">&#40;</span>req.<span style="color: #660066;">body</span>.<span style="color: #660066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">var</span> jwt <span style="color: #339933;">=</span> pay.<span style="color: #660066;">request</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        id<span style="color: #339933;">:</span> item.<span style="color: #660066;">name</span><span style="color: #339933;">,</span>
        name<span style="color: #339933;">:</span> item.<span style="color: #660066;">name</span><span style="color: #339933;">,</span>
        description<span style="color: #339933;">:</span> item.<span style="color: #660066;">description</span><span style="color: #339933;">,</span>
        icons<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'64'</span><span style="color: #339933;">:</span> settings.<span style="color: #660066;">url</span> <span style="color: #339933;">+</span> item.<span style="color: #660066;">icon</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        pricePoint<span style="color: #339933;">:</span> item.<span style="color: #660066;">price</span><span style="color: #339933;">,</span>
        productData<span style="color: #339933;">:</span> token<span style="color: #339933;">,</span>
        postbackURL<span style="color: #339933;">:</span> settings.<span style="color: #660066;">url</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'mozpay/postback'</span><span style="color: #339933;">,</span>
        chargebackURL<span style="color: #339933;">:</span> settings.<span style="color: #660066;">url</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'mozpay/chargeback'</span><span style="color: #339933;">,</span>
        simulate<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> result<span style="color: #339933;">:</span> <span style="color: #3366CC;">'postback'</span> <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Keep track of which JWT objects we are waiting on</span>
    purchaseQueue<span style="color: #009900;">&#91;</span>token<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'processing'</span><span style="color: #339933;">;</span>
&nbsp;
    res.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span>JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        jwt<span style="color: #339933;">:</span> jwt<span style="color: #339933;">,</span>
        token<span style="color: #339933;">:</span> token
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Notice the <code>settings</code> object again; it&#8217;s just a dict that has some configuration properties about our app. <code>settings.url</code> is the base URL where our app is hosted, so that all of the passed URLs are absolute.</p>
<p>We call <code>getItem</code> to get the details about an item (which we don&#8217;t define here). I found it easiest to define a JSON file containing info about all the available items. You can see the <a href="https://github.com/jlongster/webfighter/blob/master/store.js">JSON for available items in Webfighter</a> on GitHub.</p>
<p>There are <a href="https://developer.mozilla.org/en-US/docs/Web/Apps/Publishing/In-app_payments#Set_up_your_server_to_sign_JWTs">several fields for a purchase request</a>. <code>id</code> is just a unique identifer, and we simply use the product name. Other fields describe the product, such as <code>icons</code> which lists icons of various sizes. The <code>pricePoint</code> is a number that maps to a specific price for each available region, and you should look at the <a href="https://developer.mozilla.org/en-US/docs/Web/Apps/Publishing/App_pricing#Pricing_with_price_points">price point table</a> to find the one you need. For example, a price point of 10 indicates a price of $0.99 in the US.</p>
<p>The <code>simulate</code> parameter tells the payments system to just simulate the request. If it&#8217;s passed, no real charges are made and you will be taken through a simulated workflow. This is useful for development. <strong>Just remember</strong> to remove this flag in production.</p>
<p>You can store anything you want to in <code>productData</code>, which you can later use when handling events. Here I generated an arbitrary token to track requests, and I store it in the global object <code>purchaseQueue</code>. We use this when we handle events.</p>
<p>Next, subscribe to the <code>postback</code> event from the mozpay module:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">pay.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'postback'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// the payment was successful</span>
    <span style="color: #000066; font-weight: bold;">var</span> req <span style="color: #339933;">=</span> data.<span style="color: #660066;">request</span><span style="color: #339933;">;</span>
    purchaseQueue<span style="color: #009900;">&#91;</span>req.<span style="color: #660066;">productData</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'success'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can also subscribe to the <code>chargeback</code> event. Chargebacks happen when a customer disputes a charge and gets the money back. This could happen a month later, and is difficult to process. Webfighter keeps track of chargebacks, but the app does not check for them and revoke items. You would need to check periodically or on startup if the user still has access to all purchased items.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">pay.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'chargeback'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> req <span style="color: #339933;">=</span> data.<span style="color: #660066;">request</span><span style="color: #339933;">;</span>
    purchaseQueue<span style="color: #009900;">&#91;</span>req.<span style="color: #660066;">productData</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'chargeback'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That&#8217;s it for the server-side.</p>
<h2>Using mozPay On the Client Side</h2>
<p>On the client-side, you need to post to <code>/sign-swt</code> and use <code>mozPay</code> to initiate the purchase. In the following code, we check if <code>mozPay</code> is available and if it&#8217;s not we just give the user the item. Remember, it&#8217;s just a test app! Once a purchase is initiated, we start polling the server until we get a successful purchase.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> buy<span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Purchase an item by requesting a JWT object from the</span>
    <span style="color: #006600; font-style: italic;">// server, and posting it to the mozPay API</span>
    $.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/sign-jwt'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> name<span style="color: #339933;">:</span> name <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>res<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>navigator.<span style="color: #660066;">mozPay</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">var</span> req <span style="color: #339933;">=</span> navigator.<span style="color: #660066;">mozPay</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>res.<span style="color: #660066;">jwt</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            req.<span style="color: #660066;">onerror</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mozPay error: '</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">error</span>.<span style="color: #660066;">name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                clearPolling<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>
&nbsp;
            <span style="color: #006600; font-style: italic;">// Poll to see when the payment is complete</span>
            startPolling<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: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'in-app payments unavailable, so giving it to you for free'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            onPurchase<span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here are the polling functions which wait until the server responds with a successful purchase. Note that we only check for a successful purchase. The user will be notified of any errors within the native payment screen, and when they close the screen an error event is fired on the <code>mozPay</code> request object, which we handle and stop the polling. See the above code with <code>req.onerror</code>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> pollQueue<span style="color: #009900;">&#40;</span>token<span style="color: #339933;">,</span> name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Poll the server every second to see the status of our</span>
    <span style="color: #006600; font-style: italic;">// payment request</span>
&nbsp;
    $.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'/purchaseQueue?token='</span> <span style="color: #339933;">+</span> token<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>res<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>res <span style="color: #339933;">==</span> <span style="color: #3366CC;">'success'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            onPurchase<span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
            clearPolling<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: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">var</span> pollTimer<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">function</span> startPolling<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    pollTimer <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> pollQueue<span style="color: #009900;">&#40;</span>res.<span style="color: #660066;">token</span><span style="color: #339933;">,</span> name<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: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">function</span> clearPolling<span style="color: #009900;">&#40;</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>pollTimer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        clearInterval<span style="color: #009900;">&#40;</span>pollTimer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    pollTimer <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We call <code>onPurchase</code> when an item is successfully purchased and should be enabled for the user. You should create that function and write your app-specific code.</p>
<h2>Real payments: submitting app to the Firefox Marketplace</h2>
<p>When you are ready to run real payments, remove the <code>simulate</code> field when the server creates the payment request, and <a href="https://marketplace.firefox.com/developers/submit/">submit your app</a> to the Firefox Marketplace. It needs to be submitted so that you can hook up real payments.</p>
<h3>Get paid: configuring bank account details and payment options</h3>
<p>Once your app is submitted, <a href="https://marketplace.firefox.com/developers/submissions">edit your app</a> and click on &#8220;Compatibility &amp; Payments&#8221; on the left side and select &#8220;Paid / In-App&#8221;. Under &#8220;Payment Accounts&#8221;, and add your bank account. One thing that tripped me up was needing the SWIFT code for my bank. In the future, Marketplace will hopefully autofill it, but for now you can look it up on the <a href="http://www.theswiftcodes.com/">theswiftcodes.com</a>.</p>
<p>Under &#8220;Price &amp; Countries&#8221; you can set the price for your app. You may just want &#8220;Free with in-app payments&#8221;, which is what Webfighter is. Additionally, you can selectively choose which region your app is available in. Apps with payments are only available in regions with them enabled.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/prices-and-countries.png"><img alt="" src="https://hacks.mozilla.org/wp-content/uploads/2013/11/prices-and-countries-500.png" /></a></p>
<h3>Use a real key &amp; secret</h3>
<p>Now click the &#8220;In-App Payments&#8221; link in the left sidebar. You&#8217;ll see a screen where you can grab a new key &amp; secret for real payments. Use these in the <code>configure</code> call on the server. Never share your secret.</p>
<p>At this time of writing, carrier billing is only available in specific countries. Check out the <a href="https://marketplace.firefox.com/developers/docs/payments/status">Payments Status page</a> for where it&#8217;s available. When it is not available, the user will be presented with a credit card screen.</p>
<h2>Go make some money with your app!</h2>
<p>We want you to be successful in participating in the app ecosystem and economy. Let us know what your experience is like! We are happy to answer any questions over on the <a href="https://lists.mozilla.org/listinfo/dev-marketplace">marketplace mailing list</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/implementing-in-app-payments-in-your-firefox-os-app/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Firefox OS Security: Part 2 – User Experience and Security Updates</title>
		<link>https://hacks.mozilla.org/2013/11/firefox-os-security-part-2-user-experience-and-security-updates/</link>
		<comments>https://hacks.mozilla.org/2013/11/firefox-os-security-part-2-user-experience-and-security-updates/#comments</comments>
		<pubDate>Thu, 21 Nov 2013 16:59:18 +0000</pubDate>
		<dc:creator><![CDATA[Chris Heilmann]]></dc:creator>
				<category><![CDATA[Apps]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Firefox OS]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Videoseries]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23110</guid>
		<description><![CDATA[When presenting Firefox OS to people, security is a big topic. Can an operating system built on web technologies be secure? What has Mozilla built in to avoid drive-by downloads and malware? How can a browser-based app be secure without making the UX suffer by asking the user to react to a lot of &#8220;do [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>When presenting Firefox OS to people, security is a big topic. Can an operating system built on web technologies be secure? What has Mozilla built in to avoid drive-by downloads and malware? How can a browser-based app be secure without making the UX suffer by asking the user to react to a lot of &#8220;do you want to allow this?&#8221;. In this, the second of a two part video series Christian Heilmann (<a href="http://twitter.com/codepo8" >@codepo8</a>), principal evangelist of Mozilla, talks to Michael Coates (<a href="https://twitter.com/_mwc" >@_mwc</a>), chair of <a href="http://twitter.com/OWASP" >@OWASP</a> Board about the user experience when it comes to installing and using apps and the security update model for Firefox OS.</p>
<p><a href="https://www.youtube.com/watch?v=YHx2Ne7vBB8">You can watch the video on YouTube.</a></p>
<p><iframe width="500" height="281" src="//www.youtube-nocookie.com/embed/YHx2Ne7vBB8?rel=0" frameborder="0" allowfullscreen></iframe></p>
<p>Firefox OS was built on top of the technologies that power the Web. Following Mozilla’s security practices and knowledge from over 10 years of securing Firefox, Firefox OS is engineered as a multi-tiered system that protects users while delivering the power of the mobile web. The design ensures users are in control of their data and developers have APIs and technologies at their disposal to unlock the power of the Web.</p>
<p>Additional links for more information:</p>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Security_model#Secure_Architecture" >Firefox OS security architecture</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Security_model#Permissions_Management_and_Enforcement" >Firefox OS app permission model</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Application_security" >Application security</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Security_model#Sandboxed_Data" >Data sandboxing</a></li>
<li><a href="https://blog.mozilla.org/futurereleases/2013/07/19/mozillas-heartbeat-quarterly-firefox-os-releases/" >Firefox OS release schedule</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/firefox-os-security-part-2-user-experience-and-security-updates/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Handling click-to-activate plugins using JavaScript</title>
		<link>https://hacks.mozilla.org/2013/11/handling-click-to-activate-plugins-using-javascript/</link>
		<comments>https://hacks.mozilla.org/2013/11/handling-click-to-activate-plugins-using-javascript/#comments</comments>
		<pubDate>Thu, 21 Nov 2013 16:03:28 +0000</pubDate>
		<dc:creator><![CDATA[Chris Mills]]></dc:creator>
				<category><![CDATA[Firefox]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[click-to-activate]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23072</guid>
		<description><![CDATA[From Firefox 26 onwards — and in the case of insecure Flash/Java in older Firefox versions — most plugins will not be automatically activated. We therefore can no longer plugins starting immediately after they have been inserted into the page. This article covers JavaScript techniques we can employ to handle plugins, making it less likely [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>From Firefox 26 onwards — and in the case of insecure Flash/Java in older Firefox versions — <a href="https://blog.mozilla.org/futurereleases/2013/09/24/plugin-activation-in-firefox/">most plugins will not be automatically activated</a>. We therefore can no longer plugins starting immediately after they have been inserted into the page. This article covers JavaScript techniques we can employ to handle plugins, making it less likely that affected sites will break.</p>
<h2>Using a script to determine if a plugin is installed</h2>
<p>To detect if a plugin is actually installed, we can query <a href="https://developer.mozilla.org/docs/Web/API/NavigatorPlugins.mimeTypes"><code>navigator.mimeTypes</code></a> for the plugin MIME type we intend to use, to differentiate between plugins that are not installed and those that are click-to-activate. For example:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> isJavaAvailable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'application/x-java-applet'</span> <span style="color: #000066; font-weight: bold;">in</span> navigator.<span style="color: #660066;">mimeTypes</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Note: Do not iterate through <code><a href="https://developer.mozilla.org/docs/Web/API/NavigatorPlugins.mimeTypes">navigator.mimeTypes</a></code> or <code><a href="https://developer.mozilla.org/docs/Web/API/NavigatorPlugins.plugins">navigator.plugins</a></code>, as enumeration may well be removed as a privacy measure in a future version of Firefox.</p>
<h2>Using a script callback to determine when a plugin is activated</h2>
<p>The next thing to be careful of is scripting plugins immediately after instances are created on the page, to avoid breakage due to the plugin not being properly loaded. The plugin should make a call into JavaScript after it is created, using <code><a href="https://developer.mozilla.org/docs/Gecko_Plugin_API_Reference/Scripting_plugins">NPRuntime</a></code> scripting:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> pluginCreated<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myPlugin'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">callPluginMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;object</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;application/x-my-plugin&quot;</span> <span style="color: #000066;">data</span>=<span style="color: #ff0000;">&quot;somedata.mytype&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;myPlugin&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;callback&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;pluginCreated()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Note that the &#8220;callback&#8221; parameter (or something equivalent) must be implemented by your plugin. This can be done in Flash using the <code>flash.external.ExternalInterface</code> API, or in Java using the <code>netscape.javascript</code> package.</p>
<h2>Using properties on the plugin to determine when it activated</h2>
<p>When using a plugin that doesn&#8217;t allow us to specify callbacks and we can&#8217;t modify it, an alternative technique is to test for properties that the plugin should have, using code constructs like so:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;myNotification&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Waiting for the plugin to activate!<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;object</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;myPlugin&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;application/x-my-plugin&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">window.<span style="color: #660066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</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>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myPlugin'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">myProperty</span> <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">undefined</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myNotification'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span>
        document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myPlugin'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">callPluginMethod</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: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Plugin not activated yet.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setTimeout<span style="color: #009900;">&#40;</span>checkPlugin<span style="color: #339933;">,</span> <span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Making plugins visible on the page</h2>
<p>When a site wants the user to enable a plugin, the primary indicator is that the plugin is visible on the page, for example:</p>
<p><img alt="Screenshot of the silverlight plugin activation on the Netflix website." src="https://mdn.mozillademos.org/files/6305/silverlight-CtA.png" /></p>
<p>If a page creates a plugin that is very small or completely hidden, the only visual indication to the user is the small icon in the Firefox location bar. Even if the plugin element will eventually be hidden, pages should create the plugin element visible on the page, and then resize or hide it only after the user has activated the plugin. This can be done in a similar fashion to the callback technique we showed above:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> pluginCreated<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// We don't need to see the plugin, so hide it by resizing</span>
  <span style="color: #000066; font-weight: bold;">var</span> plugin <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'myPlugin'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  plugin.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
  plugin.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
  plugin.<span style="color: #660066;">callPluginMethod</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- Give the plugin an initial size so it is visible --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;object</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;application/x-my-plugin&quot;</span> <span style="color: #000066;">data</span>=<span style="color: #ff0000;">&quot;somedata.mytype&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;myPlugin&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;300&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;callback&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;pluginCreated()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/object<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p><strong>Note:</strong> For more basic information on how plugins operate in Firefox, read <a href="https://support.mozilla.org/en-US/kb/why-do-i-have-click-activate-plugins">Why do I have to click to activate plugins?</a> on support.mozilla.org.</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/handling-click-to-activate-plugins-using-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Pond &#8211; building a multi-platform HTML5 game</title>
		<link>https://hacks.mozilla.org/2013/11/the-pond-building-a-multi-platform-html5-game/</link>
		<comments>https://hacks.mozilla.org/2013/11/the-pond-building-a-multi-platform-html5-game/#comments</comments>
		<pubDate>Wed, 20 Nov 2013 23:47:32 +0000</pubDate>
		<dc:creator><![CDATA[Zoli Kahan]]></dc:creator>
				<category><![CDATA[Games]]></category>
		<category><![CDATA[HTML5]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23046</guid>
		<description><![CDATA[Introducing The Pond The Pond is a multi-platform HTML5 game (source code) that explores minimalistic design and resolution independent gameplay. The Pond isn&#8217;t about reaching a high score, or about buying weapon upgrades. It&#8217;s about relaxing and exploring a beautiful world. It is available on all these platforms/in all these stores: In making The Pond [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><a href="http://thepond.zolmeister.com/"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/the-pond.png" alt="The Pond" /></a></p>
<h2>Introducing The Pond</h2>
<p><a href="http://thepond.zolmeister.com/">The Pond</a> is a multi-platform HTML5 game (<a href="https://github.com/Zolmeister/pond">source code</a>) that explores minimalistic design and resolution independent gameplay. The Pond isn&#8217;t about reaching a high score, or about buying weapon upgrades. It&#8217;s about relaxing and exploring a beautiful world.</p>
<p>It is available on all these platforms/in all these stores:</p>
<ul class="the-pond-stores-list">
<li><a href="https://marketplace.firefox.com/app/the-pond/" title="Firefox Marketplace"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/firefox-marketplace.png" alt="Firefox Marketplace" /></a></li>
<li><a href="https://play.google.com/store/apps/details?id=com.Zolmeister.ThePond" title="Google Play"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/google-play-store.png" alt="Google Play" /></a></li>
<li><a href="https://chrome.google.com/webstore/detail/the-pond/aonjkompolbfgipkpgmcgiakfghibjmm" title="Chrome Web Store"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/chrome-store.png" alt="Chrome Web Store" /></a></li>
<li><a href="http://www.amazon.com/Zolmeister-The-Pond/dp/B00FROOKHI" title="Amazon App store"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/amazon.png" alt="Amazon App store" /></a></li>
<li><a href="http://clay.io/game/thepond" title="Clay.io"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/clay.io_.png" alt="Clay.io" /></a></li>
<li><a href="https://www.pokki.com/app/The-Pond" title="Pokki"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/Pokki.png" alt="Pokki" /></a></li>
</ul>
<p>In making The Pond I came across many performance obstacles which I will explore in detail (especially when optimizing the codebase for mobile).</p>
<h2>Tools</h2>
<p>Before I begin, I would like to mention the two tools that made coding The Pond both efficient and highly enjoyable: Light Table and CocoonJS.</p>
<p><a href="http://www.lighttable.com/">Light Table</a> is an IDE (still in alpha) which provides an integrated development environment for real-time javascript code injection. This means that javascript edited within the editor can be previewed without reloading the page. If we look at the shape of the fish in the game we notice that it is comprised of <a href="http://en.wikipedia.org/wiki/B%C3%A9zier_curve">Bézier curves</a>. Instead of trying to find an editor for creating Bézier curves, I simply estimated a basic shape and modified the variables in real-time until I was satisfied with it&#8217;s look and feel.</p>
<p><a href="http://www.ludei.com/tech/cocoonjs">CocoonJS</a> on the otherhand provides a canvas optimized compatibility layer for improved performance on mobile devices. Not only does it optimize, it also provides an interface for exporting our application to many devices (Android, iOS, Amazon (android), Pokki, and Chrome Web Store).</p>
<h2>Physics</h2>
<p>The Pond may seem simple on the outside, but on the inside it&#8217;s full of performance optimizations and responsive features. As we resize the game, it updates and re-optimizes itself to render less objects and spawn less fish, and if that&#8217;s not enough the framerate degrades smoothly to keep physics in check. This is thanks to the use of a <strong>fixed interval physics time step</strong>. <a href="http://gameprogrammingpatterns.com/game-loop.html">Gameprogrammingpatterns.com</a> provides a good explanation for how to do this and why it matters, but honestly the code makes the most sense:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> MS_PER_UPDATE <span style="color: #339933;">=</span> <span style="color: #CC0000;">18</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Time between physics calculations</span>
<span style="color: #000066; font-weight: bold;">var</span> lag <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.0</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// accumulate lag over frames</span>
<span style="color: #000066; font-weight: bold;">var</span> previousTime <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.0</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// used for calculating the time delta</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// main game loop</span>
<span style="color: #000066; font-weight: bold;">function</span> draw<span style="color: #009900;">&#40;</span>time<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  requestAnimFrame<span style="color: #009900;">&#40;</span>draw<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// immidiately queue another frame</span>
  lag <span style="color: #339933;">+=</span> time <span style="color: #339933;">-</span> previousTime<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// add time delta</span>
  previousTime <span style="color: #339933;">=</span> time<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">var</span> MAX_CYCLES <span style="color: #339933;">=</span> <span style="color: #CC0000;">18</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// prevent infinite looping/hanging on slow machines</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// physics calculations</span>
  while<span style="color: #009900;">&#40;</span>lag <span style="color: #339933;">&gt;=</span> MS_PER_UPDATE <span style="color: #339933;">&amp;&amp;</span> MAX_CYCLES<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// user input, movement, and animation calculations</span>
    physics<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    lag <span style="color: #339933;">-=</span> MS_PER_UPDATE<span style="color: #339933;">;</span>
    MAX_CYCLES<span style="color: #339933;">--;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// if we exhausted our cycles, the client must be lagging</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>MAX_CYCLES <span style="color: #339933;">===</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// adaptive quality</span>
    lowerQuality<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// if 5 frames behind after update, jump</span>
  <span style="color: #006600; font-style: italic;">// this prevents an infinite input lag from ocurring</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>lag<span style="color: #339933;">/</span>MS_PER_UPDATE <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">75</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    lag <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.0</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// draw to canvas</span>
  paint<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>What&#8217;s important to notice here is that physics is not calculated based on the time delta, instead it&#8217;s calculated at a fixed 18ms interval. This is important because it means that any client lag will not be reflected in physics calculations, and that slower machines will simply lose framerate.</p>
<h3>Dynamic Quality</h3>
<p>The next optimization we notice is the <code>lowerQuality()</code> function, which adaptively decreases the render quality of the game. The way this works is simply by re-sizing the drawing canvas (it&#8217;s still full screen, it simply gets streched out), which in-turn leads to reduced spawns and collisions.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> resizeWindow<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// quality is a global variable, updated by lowerQuality()</span>
  $canv.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">innerWidth</span> <span style="color: #339933;">*</span> quality<span style="color: #339933;">/</span><span style="color: #CC0000;">10</span>
  $canv.<span style="color: #660066;">height</span> <span style="color: #339933;">=</span> window.<span style="color: #660066;">innerHeight</span> <span style="color: #339933;">*</span> quality<span style="color: #339933;">/</span><span style="color: #CC0000;">10</span>
  ctx <span style="color: #339933;">=</span> $canv.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span>
  ctx.<span style="color: #660066;">lineJoin</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'round'</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// resize HUD elements, and reduce spawning</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>GAME.<span style="color: #660066;">state</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">'playing'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    GAME.<span style="color: #660066;">spawner</span>.<span style="color: #660066;">resize</span><span style="color: #009900;">&#40;</span>$canv.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> $canv.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span>
    GAME.<span style="color: #660066;">levelBar</span>.<span style="color: #660066;">resize</span><span style="color: #009900;">&#40;</span>$canv.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> $canv.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span>
    GAME.<span style="color: #660066;">levelBalls</span>.<span style="color: #660066;">resize</span><span style="color: #009900;">&#40;</span>$canv.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> $canv.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</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;">if</span><span style="color: #009900;">&#40;</span>ASSETS.<span style="color: #660066;">loaded</span><span style="color: #009900;">&#41;</span> drawMenu<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>Spawning</h3>
<p>Now, we&#8217;ve been talking about reducing spawning to improve performance so let me explain how that happens. The spawning algorithm works by creating a virtual grid sized based on the window size. As the player travels from one grid zone to another, the adjacent zones are populated with enemies:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/grid-spawner.png" alt="Grid Spawner" /></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">Spawner.<span style="color: #000066; font-weight: bold;">prototype</span>.<span style="color: #660066;">spawn</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>zone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// spawn 1-3  fish per 500sqpx, maybe larger maybe smaller than player</span>
  <span style="color: #006600; font-style: italic;">// 0.5 chance that it will be bigger/smaller</span>
  <span style="color: #000066; font-weight: bold;">var</span> mult <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">width</span><span style="color: #339933;">*</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">height</span><span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">500</span><span style="color: #339933;">*</span><span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span>
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> l<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span><span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span><span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> mult<span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> l<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// spawn coordinates random within a zone</span>
    <span style="color: #000066; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> zone<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">width</span><span style="color: #339933;">*</span><span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">width</span><span style="color: #339933;">/</span><span style="color: #CC0000;">2</span>
    <span style="color: #000066; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> zone<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">height</span><span style="color: #339933;">*</span><span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">height</span><span style="color: #339933;">/</span><span style="color: #CC0000;">2</span>
    <span style="color: #000066; font-weight: bold;">var</span> size <span style="color: #339933;">=</span> <span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0.5</span> <span style="color: #339933;">?</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">player</span>.<span style="color: #660066;">size</span> <span style="color: #339933;">+</span> <span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span><span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">player</span>.<span style="color: #660066;">size</span> <span style="color: #339933;">-</span> <span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span><span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// spawn a new fish</span>
    <span style="color: #000066; font-weight: bold;">var</span> fish <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Fish<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> size<span style="color: #339933;">,</span> <span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="">Math</span>.<span style="color: #660066;">PI</span><span style="color: #339933;">*</span><span style="color: #CC0000;">2</span><span style="color: #339933;">-</span><span style="">Math</span>.<span style="color: #660066;">PI</span><span style="color: #339933;">,</span> <span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="">Math</span>.<span style="color: #660066;">PI</span><span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fishes</span>.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>fish<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> zone
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The last piece of the puzzle is removing enemies as they move far enough away:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// if far enough away from player, remove</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>distance<span style="color: #009900;">&#40;</span>fish<span style="color: #339933;">,</span> player<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="">Math</span>.<span style="color: #660066;">max</span><span style="color: #009900;">&#40;</span>$canv.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> $canv.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  fish.<span style="color: #660066;">dead</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>Collisions</h3>
<p>The next performance optimization lies with the collision code. Colliding irregularly shaped objects can be extremely difficult and resource intensive. One option is to do color based collision (scan for overlapping colors), but that is much too slow. Another option might be to mathematically calculate Bézier curve collisions, however this is not only CPU intensive, it is also quite difficult to code. I finally opted for an approximation approach using circles. Basically I calculate the position of circles within each fish and detect circle collision among the fish. Boolean circle collision is extremely efficient, as it simply requires measuring the distance between objects. This ends up looking like this (debug mode):</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/collisions-debug-model.png" alt="debug mode" /></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">Fish.<span style="color: #000066; font-weight: bold;">prototype</span>.<span style="color: #660066;">collide</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>fish<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// the fish has been killed and is being removed or it is far away</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dying</span> <span style="color: #339933;">||</span> fish.<span style="color: #660066;">dying</span> <span style="color: #339933;">||</span> distance<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> fish<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">size</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">5</span> <span style="color: #339933;">+</span> fish.<span style="color: #660066;">size</span><span style="color: #339933;">*</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// there are 6 circles that make up the collision box of each fish</span>
  <span style="color: #000066; font-weight: bold;">var</span> c1<span style="color: #339933;">,</span> c2
  <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> i<span style="color: #339933;">=-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> l <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">circles</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i <span style="color: #339933;">&lt;</span> l<span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    c1 <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">circles</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> j<span style="color: #339933;">=-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> n <span style="color: #339933;">=</span> fish.<span style="color: #660066;">circles</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>j <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      c2 <span style="color: #339933;">=</span> fish.<span style="color: #660066;">circles</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// check if they touch</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>distance<span style="color: #009900;">&#40;</span>c1<span style="color: #339933;">,</span> c2<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;=</span> c2.<span style="color: #660066;">r</span> <span style="color: #339933;">+</span> c1.<span style="color: #660066;">r</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We also avoid unnecessary collision calculations by only checking the fish that are visible (or near-visible):</p>

<div class="wp_syntax"><table><tr><td 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="">Math</span>.<span style="color: #660066;">abs</span><span style="color: #009900;">&#40;</span>fish2.<span style="color: #660066;">x</span> <span style="color: #339933;">-</span> player.<span style="color: #660066;">x</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> $canv.<span style="color: #660066;">width</span> <span style="color: #339933;">&amp;&amp;</span> <span style="">Math</span>.<span style="color: #660066;">abs</span><span style="color: #009900;">&#40;</span>fish2.<span style="color: #660066;">y</span> <span style="color: #339933;">-</span> player.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> $canv.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// check</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Drawing</h2>
<p>After getting the physics+ out of the way, it&#8217;s time to optimize drawing operations. Many games use sprite maps for animation (<a href="http://www.zolmeister.com/2013/09/senshi-mmo-battle-royale-inspired-html5.html">Senshi</a> for example) which can be highly optimized. Unfortunately our fish are dynamically generated so we must find other ways to optimizing drawing. First lets use Chrome&#8217;s javascript profiler to identify bottlenecks:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/The-Pond-CPU-profile.png" alt="The Pond CPU profile" /></p>
<p>What we see here is that <code>stroke</code> is using a lot of resources. Truth be told, <code>fill</code> used to be there too. This is because both were called heavily when drawing fish. The game looked a bit like this:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/The-Pond-Old-rendering.png" alt="The Pond - Old rendering" /></p>
<p>After removing <code>fill</code> I saw a huge performance increase, and the game looked much better. The reason the <code>drawImage</code> function is up there as well is because I take advantage of <a href="https://hacks.mozilla.org/2013/05/optimizing-your-javascript-game-for-firefox-os/">offscreen canvas</a> rendering. Each fish is drawn on its own offscreen canvas which is then rendered onto the larger visible canvas. This is also what allowed me to easily explode the fish into particles by reading pixel data:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">Fish.<span style="color: #000066; font-weight: bold;">prototype</span>.<span style="color: #660066;">toParticles</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">var</span> particles <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// read canvas pixel data</span>
  <span style="color: #000066; font-weight: bold;">var</span> pixels <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">ctx</span>.<span style="color: #660066;">getImageData</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><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">canv</span>.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">canv</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">data</span>
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> pixels.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #CC0000;">36</span> <span style="color: #339933;">*</span> <span style="">Math</span>.<span style="color: #660066;">ceil</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">size</span><span style="color: #339933;">/</span><span style="color: #CC0000;">20</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>isMobile <span style="color: #339933;">?</span> <span style="color: #CC0000;">6</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> r <span style="color: #339933;">=</span> pixels<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>
    <span style="color: #000066; font-weight: bold;">var</span> g <span style="color: #339933;">=</span> pixels<span style="color: #009900;">&#91;</span>i <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>
    <span style="color: #000066; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> pixels<span style="color: #009900;">&#91;</span>i <span style="color: #339933;">+</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// black pixel - no data</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>r <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>g <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>b<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">continue</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Math to calculate position</span>
    <span style="color: #000066; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> i<span style="color: #339933;">/</span><span style="color: #CC0000;">4</span> <span style="color: #339933;">%</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">canv</span>.<span style="color: #660066;">width</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">canv</span>.<span style="color: #660066;">width</span><span style="color: #339933;">/</span><span style="color: #CC0000;">2</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">size</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000066; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> <span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">/</span><span style="color: #CC0000;">4</span> <span style="color: #339933;">/</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">canv</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">canv</span>.<span style="color: #660066;">height</span><span style="color: #339933;">/</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000066; font-weight: bold;">var</span> relativePos <span style="color: #339933;">=</span> rot<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dir</span><span style="color: #009900;">&#41;</span>
    x<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">+</span> relativePos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>
    y<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">+</span> relativePos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">var</span> col <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Color<span style="color: #009900;">&#40;</span>r<span style="color: #339933;">,</span> g<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span>
    <span style="color: #000066; font-weight: bold;">var</span> dir <span style="color: #339933;">=</span> directionTowards<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>x<span style="color: #339933;">:</span> x<span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> y<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>
    particles.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">new</span> Particle<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> col<span style="color: #339933;">,</span> target<span style="color: #339933;">,</span> <span style="">Math</span>.<span style="color: #660066;">PI</span><span style="color: #339933;">*</span><span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">2</span> <span style="color: #339933;">-</span> <span style="">Math</span>.<span style="color: #660066;">PI</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">size</span><span style="color: #339933;">/</span><span style="color: #CC0000;">20</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> particles
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>The End</h2>
<p>In the end the performance optimizations paid off and made the game feel more polished and playable even on lower-end mobile devices.</p>
<p>If you enjoyed this post, I regularly blog about my development projects over at <a href="http://www.zolmeister.com/">http://zolmeister.com</a>.</p>
<p><a href="http://thepond.zolmeister.com/">The Pond</a> awaits exploring&#8230;</p>
<style>
.the-pond-stores-list {list-style: none;}
.the-pond-stores-list li {display: inline-block; width: 30%; text-align: center;}
.the-pond-stores-list {list-style: none;}
</style>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/the-pond-building-a-multi-platform-html5-game/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Launching developer Q&amp;A on Stack Overflow</title>
		<link>https://hacks.mozilla.org/2013/11/launching-developer-q-a-on-stack-overflow/</link>
		<comments>https://hacks.mozilla.org/2013/11/launching-developer-q-a-on-stack-overflow/#comments</comments>
		<pubDate>Tue, 19 Nov 2013 21:56:42 +0000</pubDate>
		<dc:creator><![CDATA[Robert Nyman [Editor]]]></dc:creator>
				<category><![CDATA[Apps]]></category>
		<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[Firefox OS]]></category>
		<category><![CDATA[HTML5]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23091</guid>
		<description><![CDATA[One thing that is very important for us at Mozilla is the need to directly interact with you developers and help you with challenges and issues while developing using open technologies. We are now happy to announce our presence on Stack Overflow! Stack Overflow is one of &#8211; if not the &#8211; most well-known question [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>One thing that is very important for us at Mozilla is the need to directly interact with you developers and help you with challenges and issues while developing using open technologies. We are now happy to announce our presence on Stack Overflow!</p>
<p>Stack Overflow is one of &#8211; if not the &#8211; most well-known question and answer sites among developers, and we believe it&#8217;s very important to go where developers are and choose to have their discussions while developing.</p>
<p>Initially our main focus is on Firefox OS app development and Open Web Apps in general. There are three tags that we are monitoring:</p>
<ul>
<li><a href="http://stackoverflow.com/questions/tagged/firefox">firefox</a></li>
<li><a href="http://stackoverflow.com/questions/tagged/firefox-os">firefox-os</a></li>
<li><a href="http://stackoverflow.com/questions/tagged/html5-apps">html5-apps</a></li>
</ul>
<p>All tags and posts are available at <a href="http://stackoverflow.com/r/mozilla">our landing page on Stack Overflow: http://stackoverflow.com/r/mozilla</a></p>
<h2>Take part!</h2>
<p>We&#8217;d love for you to participate with both questions and answers, so we together can collaborate and make the Open Web and open technologies the first choice for developers!</p>
<p><a href="http://stackoverflow.com/r/mozilla"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/stack-overflow.png" alt="" /></a></p>
<p>A little extra motivation might be that if you answer a lot of questions, you&#8217;ll show up in the Top Users page for that tag, e.g. <a href="http://stackoverflow.com/tags/firefox-os/topusers">http://stackoverflow.com/tags/firefox-os/topusers</a></p>
<p>Resources that might be good for you about HTML5, Apps, Developer Tools and more are:</p>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5">HTML5 on the Mozilla Developer Network (MDN)</a></li>
<li><a href="https://developer.mozilla.org/en-US/Apps">App Center on MDN</a></li>
<li><a href="https://developer.mozilla.org/en-US/Firefox_OS">Firefox OS on MDN</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/tools">Firefox Developer Tools on MDN</a></li>
</ul>
<p>See you on Stack Overflow!</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/launching-developer-q-a-on-stack-overflow/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Firefox OS Security: Part 1 &#8211; The Web Security Model</title>
		<link>https://hacks.mozilla.org/2013/11/firefox-os-security-part-1-the-web-security-model/</link>
		<comments>https://hacks.mozilla.org/2013/11/firefox-os-security-part-1-the-web-security-model/#comments</comments>
		<pubDate>Thu, 14 Nov 2013 16:06:08 +0000</pubDate>
		<dc:creator><![CDATA[Chris Heilmann]]></dc:creator>
				<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Firefox OS]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Video]]></category>
		<category><![CDATA[Videoseries]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23080</guid>
		<description><![CDATA[When presenting Firefox OS to people, security is a big topic. Can an operating system built on web technologies be secure? What has Mozilla built in to avoid drive-by downloads and malware? In this two part video series Christian Heilmann (@codepo8), principal evangelist of Mozilla, talks to Michael Coates (@_mwc), chair of @OWASP Board about [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>When presenting Firefox OS to people, security is a big topic. Can an operating system built on web technologies be secure? What has Mozilla built in to avoid drive-by downloads and malware? In this two part video series Christian Heilmann (<a href="http://twitter.com/codepo8">@codepo8</a>), principal evangelist of Mozilla, talks to Michael Coates (<a href="https://twitter.com/_mwc">@_mwc</a>), chair of <a href="http://twitter.com/OWASP">@OWASP</a> Board about all things security in Firefox OS.</p>
<p>Firefox OS was built on top of the technologies that power the Web. Following Mozilla&#8217;s security practices and knowledge from over 10 years of securing Firefox, Firefox OS is engineered as a multi-tiered system that protects users while delivering the power of the mobile web. The design ensures users are in control of their data and developers have APIs and technologies at their disposal to unlock the power of the Web.</p>
<p>Watch the following video where we talk more about the security design and controls present in Firefox OS. In this, the first of two videos on Firefox OS security, we&#8217;ll cover items such as the multi-tiered architecture, the permission model, run time decision making, protection of users data and the update model. <a href="https://www.youtube.com/watch?v=9T1PH85SC0U">You can watch the video on YouTube</a>.</p>
<p><iframe width="500" height="281" src="//www.youtube-nocookie.com/embed/9T1PH85SC0U?rel=0" frameborder="0" allowfullscreen></iframe></p>
<p>Additional links for more information:</p>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Security_model#Secure_Architecture">Firefox OS security architecture</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Security_model#Permissions_Management_and_Enforcement">Firefox OS app permission model</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Application_security">Application security</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/Firefox_OS/Security/Security_model#Sandboxed_Data">Data sandboxing</a></li>
<li><a href="https://blog.mozilla.org/futurereleases/2013/07/19/mozillas-heartbeat-quarterly-firefox-os-releases/">Firefox OS release schedule</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/firefox-os-security-part-1-the-web-security-model/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Introducing the Whiteboard Drum &#8211; WebRTC and Web Audio API magic</title>
		<link>https://hacks.mozilla.org/2013/11/introducing-the-whiteboard-drum-webrtc-and-web-audio-api-magic/</link>
		<comments>https://hacks.mozilla.org/2013/11/introducing-the-whiteboard-drum-webrtc-and-web-audio-api-magic/#comments</comments>
		<pubDate>Thu, 14 Nov 2013 07:18:08 +0000</pubDate>
		<dc:creator><![CDATA[Tatsuya Shinyagaito]]></dc:creator>
				<category><![CDATA[Audio]]></category>
		<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[WebRTC]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=22982</guid>
		<description><![CDATA[Browser functionality has expanded rapidly, way beyond merely &#8220;browsing&#8221; a document. Recently, Web browsers finally gained audio processing abilities with the Web Audio API. It is powerful to the point of building serious music applications. Not only that, but it is also very interesting when combined with other APIs. One of these APIs is getUserMedia(), [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Browser functionality has expanded rapidly, way beyond merely &#8220;browsing&#8221; a document. Recently, Web browsers finally gained audio processing abilities with the <a href="https://developer.mozilla.org/en-US/docs/Web_Audio_API">Web Audio API</a>. It is powerful to the point of building serious music applications.</p>
<p>Not only that, but it is also very interesting when combined with other APIs. One of these APIs is <code>getUserMedia()</code>, which allows us to capture audio and/or video from the local PC&#8217;s microphone / camera devices. <a href="http://www.g200kg.com/whiteboarddrum/">Whiteboard Drum</a> (<a href="https://github.com/g200kg/WhiteboardDrum">code on GitHub</a>) is a music application, and a great example of what can be achieved using Web Audio API and <code>getUserMedia()</code>.</p>
<p>I demonstrated Whiteboard Drum at the Web Music Hackathon Tokyo in October. It was a very exciting event on the subject of Web Audio API and Web MIDI API. Many instruments can collaborate with the browser, and it can also create new interfaces to the real world.</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/20131020hack1.jpeg" alt="" /></p>
<p>I believe this suggests further possibilities of Web-based music applications, especially using Web Audio API in conjunction with other APIs. Let&#8217;s explain how the key features of Whiteboard Drum work, showing relevant code fragments as we go.</p>
<h2>Overview</h2>
<p>First of all, let me show you a picture from the Hackathon:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/hackathon.jpg" alt="" /></p>
<p>And a easy movie demo:</p>
<p><iframe width="500" height="281" src="//www.youtube.com/embed/aFLKXIravec?rel=0" frameborder="0" allowfullscreen></iframe></p>
<p>As you can see, Whiteboard Drum plays a rhythm according to the matrix pattern on the whiteboard. The whiteboard has no magic; it just needs to be pointed at by a WebCam. Though I used magnets in the demo, you can draw the markers using pen if you wish. Each row represents the corresponding instruments of Cymbal, Hi-Hat, Snare-Drum and Bass-Drum, and each column represents timing steps. In this implementation, the sequence has 8 steps. The color blue will activate the grid normally, and the red will activate with accent.</p>
<p>The processing flow is:</p>
<ol>
<li>The whiteboard image is captured by the WebCam</li>
<li>The matrix pattern is analysed</li>
<li>This pattern is fed to the drum sound generators to create the corresponding sound patterns</li>
</ol>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/desc1.png" alt="" /></p>
<p>Although it uses nascent browser technologies, each process itself is not so complicated. Some key points are described below.</p>
<h2>Image capture by getUserMedia()</h2>
<p><code>getUserMedia()</code> is a function for capturing video/audio from webcam/microphone devices. It is a part of <a href="http://www.webrtc.org/">WebRTC</a> and a fairly new feature in web browsers. Note that the user&#8217;s permission is required to get the image from the WebCam. If we were just displaying the WebCam image on the screen, it would be trivially easy. However, we want to access the image&#8217;s raw pixel data in JavaScript for more processing, so we need to use <code>canvas</code> and the <code>createImageData()</code> function.</p>
<p>Because pixel-by-pixel processing is needed later in this application, the captured image&#8217;s resolution is reduced to 400 x 200px; that means one rhythm grid is 50 x 50 px in the rhythm pattern matrix.</p>
<p>Note: Though most recent laptops/notebooks have embedded WebCams, you will get the best results on Whiteboard Drum from an external camera, because the camera needs to be precisely aimed at the picture on the whiteboard. Also, the selection of input from multiple available devices/cameras is not standardized currently, and cannot be controlled in JavaScript. In Firefox, it is selectable in the permission dialog when connecting, and it can be set up from &#8220;contents setup&#8221; option of the setup screen in Google Chrome.</p>
<h3>Get the WebCam video</h3>
<p>We don&#8217;t want to show these parts of the processing on the screen, so first we hide the <code>video</code>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;video</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;video&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display:none&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/video<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Now to grab the video:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">video <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;video&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
navigator.<span style="color: #660066;">getUserMedia</span><span style="color: #339933;">=</span>navigator.<span style="color: #660066;">getUserMedia</span><span style="color: #339933;">||</span>navigator.<span style="color: #660066;">webkitGetUserMedia</span><span style="color: #339933;">||</span>navigator.<span style="color: #660066;">mozGetUserMedia</span><span style="color: #339933;">;</span>
navigator.<span style="color: #660066;">getUserMedia</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;video&quot;</span><span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>stream<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        video.<span style="color: #660066;">src</span><span style="color: #339933;">=</span> window.<span style="color: #660066;">URL</span>.<span style="color: #660066;">createObjectURL</span><span style="color: #009900;">&#40;</span>stream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        video.<span style="color: #660066;">play</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: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>err<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Camera Error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Capture it and get pixel values</h3>
<p>We also hide the <code>canvas</code>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;canvas</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;capture&quot;</span> <span style="color: #000066;">width</span>=400 <span style="color: #000066;">height</span>=200 <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;display:none&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/canvas<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Then capture our video data on the <code>canvas</code>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> Capture<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ctxcapture.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>video<span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">400</span><span style="color: #339933;">,</span><span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    imgdatcapture<span style="color: #339933;">=</span>ctxcapture.<span style="color: #660066;">getImageData</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><span style="color: #CC0000;">400</span><span style="color: #339933;">,</span><span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The video from the WebCam will be drawn onto the <code>canvas</code> at periodic intervals.</p>
<h2>Image analyzing</h2>
<p>Next, we need to get the 400 x 200 pixel values with <code>getImageData()</code>. The analyzing phase analyses the 400 x 200 image data in an 8 x 4 matrix rhythm pattern, where a single matrix grid is 50 x 50 px. All necessary input data is stored in the <code>imgdatcapture.data</code> array in RGBA format, 4 elements per pixel.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> pixarray <span style="color: #339933;">=</span> imgdatcapture.<span style="color: #660066;">data</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> step<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">8</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>x<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> px <span style="color: #339933;">=</span> x <span style="color: #339933;">*</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>invert<span style="color: #009900;">&#41;</span>
        step<span style="color: #339933;">=</span><span style="color: #CC0000;">7</span><span style="color: #339933;">-</span>x<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span>
        step<span style="color: #339933;">=</span>x<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> y <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> py <span style="color: #339933;">=</span> y <span style="color: #339933;">*</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">var</span> lum <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">var</span> red <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> dx <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> dx <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>dx<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> dy <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> dy <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">50</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>dy<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>py <span style="color: #339933;">+</span> dy<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">400</span> <span style="color: #339933;">+</span> px <span style="color: #339933;">+</span> dx<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">4</span><span style="color: #339933;">;</span>
                lum <span style="color: #339933;">+=</span> pixarray<span style="color: #009900;">&#91;</span>offset<span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">3</span> <span style="color: #339933;">+</span> pixarray<span style="color: #009900;">&#91;</span>offset<span style="color: #339933;">+</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">6</span> <span style="color: #339933;">+</span> pixarray<span style="color: #009900;">&#91;</span>offset<span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                red <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span>pixarray<span style="color: #009900;">&#91;</span>offset<span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span>pixarray<span style="color: #009900;">&#91;</span>offset<span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>lum <span style="color: #339933;">&lt;</span> lumthresh<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>red <span style="color: #339933;">&gt;</span> redthresh<span style="color: #009900;">&#41;</span>
                rhythmpat<span style="color: #009900;">&#91;</span>step<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>y<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">else</span>
                rhythmpat<span style="color: #009900;">&#91;</span>step<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>y<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">else</span>
            rhythmpat<span style="color: #009900;">&#91;</span>step<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>y<span style="color: #009900;">&#93;</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: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This is honest pixel-by-pixel analysis of grid-by-grid loops. In this implementation, the analysis is done for the luminance and redness. If the grid is &#8220;dark&#8221;, the grid is activated; if it is red, it should be accented.</p>
<p>The luminance calculation uses a simplified matrix — R * 3 + G * 6 + B — that will get ten times the value &#8211; meaning &#8211; which means getting the value of range 0 to 2550 for each pixel. And the redness R &#8211; B is a experimental value because all that is required is a decision of Red or Blue. The result is stored in the <code>rhythmpat</code> array, with a value of 0 for nothing, 1 for blue or 2 for red.</p>
<h2>Sound generation through the Web Audio API</h2>
<p>Because the <a href="https://developer.mozilla.org/en-US/docs/Web_Audio_API">Web Audio API</a> is a very cutting edge technology, it is not yet supported by every web browser. Currently, Google Chrome/Safari/Webkit-based Opera and Firefox (25 or later) support this API. <strong>Note:</strong> Firefox 25 is the latest version released at the end of October.</p>
<p>For other web browsers, I have developed a polyfill that falls back to Flash: <a href="https://github.com/g200kg/WAAPISim">WAAPISim, available on GitHub</a>. It provides almost all functions of the Web Audio API to unsupported browsers, for example Internet Explorer.</p>
<p>Web Audio API is a large scale specification, but in our case, the sound generation part requires just a very simple use of the Web Audio API: load one sound for each instrument and trigger them at the right times. First we create an audio context, taking care of vendor prefixes in the process. Prefixes currently used are webkit or no prefix.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">audioctx <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">AudioContext</span><span style="color: #339933;">||</span>window.<span style="color: #660066;">webkitAudioContext</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Next we load sounds to buffers via XMLHttpRequest. In this case, different sounds for each instrument (bd.wav / sd.wav / hh.wav / cy.wav) are loaded into the <code>buffers</code> array:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> buffers <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> req <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> loadidx <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> files <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
    <span style="color: #3366CC;">&quot;samples/bd.wav&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;samples/sd.wav&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;samples/hh.wav&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;samples/cy.wav&quot;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">function</span> LoadBuffers<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    req.<span style="color: #660066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span> files<span style="color: #009900;">&#91;</span>loadidx<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    req.<span style="color: #660066;">responseType</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;arraybuffer&quot;</span><span style="color: #339933;">;</span>
    req.<span style="color: #660066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</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>req.<span style="color: #660066;">response</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            audioctx.<span style="color: #660066;">decodeAudioData</span><span style="color: #009900;">&#40;</span>req.<span style="color: #660066;">response</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                buffers<span style="color: #009900;">&#91;</span>loadidx<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>b<span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span>loadidx <span style="color: #339933;">&lt;</span> files.<span style="color: #660066;">length</span><span style="color: #009900;">&#41;</span>
                    LoadBuffers<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: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    req.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The Web Audio API generates sounds by routing graphs of nodes. Whiteboard Drum uses a simple graph that is accessed via <code>AudioBufferSourceNode</code> and <code>GainNode</code>. <code>AudioBufferSourceNode</code> play back the AudioBuffer and route to destination(output) directly (for normal *blue* sound), or route to destination via the <code>GainNode</code> (for accented *red* sound). Because the <code>AudioBufferSourceNode</code> can be used just once, it will be newly created for each trigger.</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/nodes.png" alt="" /></p>
<p>Preparing the <code>GainNode</code> as the output point for accented sounds is done like this.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">gain<span style="color: #339933;">=</span>audioctx.<span style="color: #660066;">createGain</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    gain.<span style="color: #660066;">gain</span>.<span style="color: #660066;">value</span><span style="color: #339933;">=</span><span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
    gain.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>audioctx.<span style="color: #660066;">destination</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And the trigger function looks like so:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> Trigger<span style="color: #009900;">&#40;</span>instrument<span style="color: #339933;">,</span>accent<span style="color: #339933;">,</span>when<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> src<span style="color: #339933;">=</span>audioctx.<span style="color: #660066;">createBufferSource</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    src.<span style="color: #660066;">buffer</span><span style="color: #339933;">=</span>buffers<span style="color: #009900;">&#91;</span>instrument<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>accent<span style="color: #009900;">&#41;</span>
        src.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>gain<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span>
        src.<span style="color: #660066;">connect</span><span style="color: #009900;">&#40;</span>audioctx.<span style="color: #660066;">destination</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    src.<span style="color: #660066;">start</span><span style="color: #009900;">&#40;</span>when<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>All that is left to discuss is the accuracy of the playback timing, according to the rhythm pattern. Though it would be simple to keep creating the triggers with a <code>setInterval()</code> timer, it is not recommended. The timing can be easily messed up by any CPU load.</p>
<p>To get accurate timing, using the time management system embedded in the Web Audio API is recommended. It calculates the <code>when</code> argument of the <code>Trigger()</code> function above.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// console.log(nexttick-audioctx.currentTime);</span>
while<span style="color: #009900;">&#40;</span>nexttick <span style="color: #339933;">-</span> audioctx.<span style="color: #660066;">currentTime</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0.3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> p <span style="color: #339933;">=</span> rhythmpat<span style="color: #009900;">&#91;</span>step<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span>
        Trigger<span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> p<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> nexttick<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><span style="color: #339933;">++</span>step <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">8</span><span style="color: #009900;">&#41;</span>
        step <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    nexttick <span style="color: #339933;">+=</span> deltatick<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In Whiteboard Drum, this code controls the core of the functionality. <code>nexttick</code> contains the accurate time (in seconds) of the next step, while <code>audioctx.currentTime</code> is the accurate current time (again, in seconds). Thus, this routine is getting triggered every 300ms &#8211; meaning look ahead to 300ms in the future (triggered in advance while nextticktime &#8211; currenttime &lt; 0.3). The commented <code>console.log</code> will print the timing margin. While this routine is called periodically, the timing is collapsed if this value is negative.</p>
<p>For more detail, here is a helpful document: <a href="http://www.html5rocks.com/en/tutorials/audio/scheduling/">A Tale of Two Clocks &#8211; Scheduling Web Audio with Precision</a></p>
<h2>About the UI</h2>
<p>Especially in music production software like DAW or VST plugins, the UI is important. Web applications do not have to emulate this exactly, but something similar would be a good idea. Fortunately, the very handy <a href="https://github.com/g200kg/webaudio-controls">WebComponent library webaudio-controls</a> is available, allowing us to define knobs or sliders with just a single HTML tag.</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/controls.png" alt="" /></p>
<p><strong>NOTE</strong>: webaudio-controls uses Polymer.js, which sometimes has stability issues, causing unexpected behavior once in a while, especially when combining it with complex APIs.</p>
<h2>Future work</h2>
<p>This is already an interesting application, but it can be improved further. Obviously the camera position adjustment is an issue. Analysis can be more smarter if it has an auto adjustment of the position (using some kind of marker?), and adaptive color detection. Sound generation could also be improved, with more instruments, more steps and more sound effects.</p>
<p>How about a challenge?</p>
<p>Whiteboard Drum is available at <a href="http://www.g200kg.com/whiteboarddrum/">http://www.g200kg.com/whiteboarddrum/</a>, and the <a href="https://github.com/g200kg/WhiteboardDrum">code is on GitHub</a>.</p>
<p>Have a play with it and see what rhythms you can create!</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/introducing-the-whiteboard-drum-webrtc-and-web-audio-api-magic/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Live editing WebGL shaders with Firefox Developer Tools</title>
		<link>https://hacks.mozilla.org/2013/11/live-editing-webgl-shaders-with-firefox-developer-tools/</link>
		<comments>https://hacks.mozilla.org/2013/11/live-editing-webgl-shaders-with-firefox-developer-tools/#comments</comments>
		<pubDate>Tue, 12 Nov 2013 17:19:17 +0000</pubDate>
		<dc:creator><![CDATA[Jeff Griffiths]]></dc:creator>
				<category><![CDATA[Developer Tools]]></category>
		<category><![CDATA[WebGL]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=23013</guid>
		<description><![CDATA[If you’ve seen Epic Games’ HTML5 port of ‘Epic Citadel’, you have no doubt been impressed by the amazing performance and level of detail. A lot of the code that creates the cool visual effects you see on screen are written as shaders linked together in programs &#8211; these are specialized programs that are evaluated [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>If you’ve seen Epic Games’ HTML5 port of <a href="http://www.unrealengine.com/html5/">‘Epic Citadel’</a>, you have no doubt been impressed by the amazing performance and level of detail. A lot of the code that creates the cool visual effects you see on screen are written as <em>shaders</em> linked together in <em>programs</em> &#8211; these are specialized programs that are evaluated directly on the GPU to provide high performance real-time visual effects.</p>
<p>Writing Vertex and Fragment shaders are an essential part of developing 3D on the web even if you are using a library, in fact the <a href="https://blog.mozilla.org/futurereleases/2013/05/02/epic-citadel-demo-shows-the-power-of-the-web-as-a-platform-for-gaming/">Epic Citadel demo</a> includes over 200 shader programs. This is because most rendering is customised and optimised to fit a game’s needs. Shader development is currently awkward for a few reasons:</p>
<ul>
<li>Seeing your changes requires a refresh</li>
<li>Some shaders are applied under very specific conditions</li>
</ul>
<p>Here is a screencast that shows a how to manipulate shader code using a relatively simple WebGL demo:</p>
<p><iframe width="500" height="375" src="//www.youtube.com/embed/hnoKqFuJhu0" frameborder="0" allowfullscreen></iframe></p>
<p>Starting in Firefox 27 we&#8217;ve introduced a new tool called the &#8216;Shader Editor&#8217; that makes working with shader programs much simpler: the editor lists all shader programs running in the WebGL context, and you can live-edit shaders and see immediate results without interrupting any animations or state. Additionally editing shaders should <strong>not</strong> impact WebGL performance.</p>
<h2>Enabling the Shader Editor</h2>
<p>The Shader Editor is not shown by default, because not all the web pages out there contain WebGL, but you can easily enable it:</p>
<ol>
<li>Open the Toolbox by pressing either F12 or Ctrl/Cmd + Shift + I.</li>
<li>Click on the &#8216;gear&#8217; icon near the top edge of the Toolbox to open the &#8216;Toolbox Options&#8217;.</li>
<li>On the left-hand side under &#8216;Default Firefox Developer Tools&#8217; make sure &#8216;Shader Editor&#8217; is checked. You should immediately see a new &#8216;Shader Editor&#8217; Tool tab.</li>
</ol>
<h2>Using the Shader Editor</h2>
<p>To see the Shader Editor in action, just go to a WebGL demo <a href="http://learningwebgl.com/lessons/lesson04/index.html">such as this one</a> and open the toolbox. When you click on the shader editor tab, you&#8217;ll see a reload button you will need to click in order to get the editor attached to the WebGL context. Once you&#8217;ve done this you&#8217;ll see the Shader Editor UI:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/shader-editor-loaded.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/shader-editor-loaded-500.png" alt="The WebGL Shader Editor" /></a></p>
<ul>
<li>On the left you have a list of programs, a vertex and fragment shader corresponds to each program and their source is displayed and syntax highlighted in the editors on the right.</li>
<li>The shader type is displayed underneath each editor.</li>
<li>Hovering a program highlights the geometry drawn by its corresponding shaders in red – this is useful for finding the right program to work on.</li>
<li>Clicking on the eyeball right next to each program hides the rendered geometry (useful in the likely case an author wants to focus solely on some geometry but not other, or to hide overlapping geometry).</li>
<li>The tool is responsive when docked to the side.</li>
</ul>
<h2>Editing Shader Programs</h2>
<p>The first thing you&#8217;ll notice about Shader program code is that it is <strong>not</strong> JavaScript. For more information on how Shader programs work, I highly recommend you start with the WebGL demo on the <a href="http://www.khronos.org/webgl/wiki/Tutorial#Creating_the_Shaders">Khronos wiki</a> and/or Paul Lewis&#8217; excellent <a href="http://www.html5rocks.com/en/tutorials/webgl/shaders/">HTML5 Rocks post</a>. There also some great long standing tutorials on the <a href="http://learningwebgl.com/blog/?page_id=1217">Learning WebGL</a> blog. The Shader Editor gives you direct access to the programs so you can play around with how they work:</p>
<ul>
<li>Editing code in any of the editors will compile the source and apply it as soon as the user stops typing;</li>
<li>If an error was made in the code, the rendering won’t be affected, but an error will be displayed in the editor, highlighting the faulty line of code; hovering the icon gutter will display a tooltip describing the error.</li>
</ul>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/shader-editor-error-info.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/shader-editor-error-info-500.png" alt="Errors in shaders" /></a></p>
<p>Learn more about the <a href="https://developer.mozilla.org/en-US/docs/Tools/Shader_Editor/">Shader Editor on the Mozilla Developer Network</a>.</p>
<p>Here is a second screencast showing how you could directly edit the shader programs in the Epic Citadel demo:</p>
<p><iframe width="500" height="375" src="//www.youtube.com/embed/Mwi_1bNft1o" frameborder="0" allowfullscreen></iframe></p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/live-editing-webgl-shaders-with-firefox-developer-tools/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Make your Firefox OS app feel alive with video and audio</title>
		<link>https://hacks.mozilla.org/2013/11/make-your-firefox-os-app-feel-alive-with-video-and-audio/</link>
		<comments>https://hacks.mozilla.org/2013/11/make-your-firefox-os-app-feel-alive-with-video-and-audio/#comments</comments>
		<pubDate>Thu, 07 Nov 2013 14:44:21 +0000</pubDate>
		<dc:creator><![CDATA[Frédéric Harper]]></dc:creator>
				<category><![CDATA[Audio]]></category>
		<category><![CDATA[Firefox OS]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=22949</guid>
		<description><![CDATA[Firefox OS applications aren&#8217;t just about text: there is no better way to make your app feel alive than adding some videos or audio to it. Let&#8217;s explore different ways we can use as developers to enhance our mobile masterpiece. Audio and video HTML tags Since we are talking about HTML, it makes total sense [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Firefox OS applications aren&#8217;t just about text: there is no better way to make your app feel alive than adding some videos or audio to it. Let&#8217;s explore different ways we can use as developers to enhance our mobile masterpiece.</p>
<h2>Audio and video HTML tags</h2>
<p>Since we are talking about HTML, it makes total sense to think about using the <code>&lt;audio&gt;</code>, and <code>&lt;video&gt;</code> tag to play those media in your Firefox OS app. If you want to add a video in your application, just use this code.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;video</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://v2v.cc/~j/theora_testsuite/320x240.ogg&quot;</span> controls<span style="color: #000000; font-weight: bold;">&gt;</span></span>
  Your browser does not support the video element.
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/video<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>In this code example, the user will see a video player with controls, and will have the opportunity to start the video. If your application is running in a browser not supporting the <code>video</code> tag, the user will see the text between the tag. It&#8217;s still a good practice to do so, even if your primary target is a Firefox OS app, because since it uses HTML5, someone may access it from another browser if it&#8217;s a hosted app. Note that you can use other <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/video" title="Mozilla Developer Network article on HTML video tag">attributes</a> for this element.</p>
<p>As for the audio tag, it&#8217;s basically the same.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;audio</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;demo&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;/music/audio.mp3&quot;</span> autoplay loop<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/audio<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>In this example, the audio will start automatically, and will play the audio file, in a loop, from the relative path: it&#8217;s perfect for background music if you are building a game. Note that you can add other <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio" title="Mozilla Developer Network article on HTML audio tag">attributes</a> to this element too.</p>
<p>Of course, using those elements without JavaScript give you basic features, but no worries, you can programmatically control them with code. Once you have your HTML element, like the <code>audio</code> example you just saw, you can use JavaScript to play, pause, change the volume, and more.</p>

<div class="wp_syntax"><table><tr><td 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;">&quot;#demo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Play the Audio</span>
document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#demo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">pause</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Pause the Audio</span>
document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#demo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">volume</span><span style="color: #339933;">+=</span><span style="color: #CC0000;">0.1</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Increase Volume</span>
document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#demo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">volume</span><span style="color: #339933;">-=</span><span style="color: #CC0000;">0.1</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//Decrease Volume</span></pre></td></tr></table></div>

<p>You can read more on what you can do with those two elements in the <a title="Documentation on the HTML video, and audio tag from the Mozilla Developer Network" href="https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_HTML5_audio_and_video">Mozilla Developer Network documentation</a>. You also want to give a closer look to the <a href="https://developer.mozilla.org/en-US/docs/HTML/Supported_media_formats" title="Media formats supported by the HTML audio and video elements on Mozilla Developer Network">supported format list</a>.</p>
<h2>Use audio while the screen is locked</h2>
<p>Maybe you are building a podcast app, or at least you need to be able to play audio while the screen is locked? There is a way to do it by using the <code>audio</code> tag. You simply need to add the <code>mozaudiochannel</code> attribute with the value of <code>content</code> to your actual tag.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>audio mozaudiochannel<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;content&quot;</span> preload<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;none&quot;</span> 
  src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://upload.wikimedia.org/wikipedia/en/4/45/ACDC_-_Back_In_Black-sample.ogg&quot;</span> 
  autoplay<span style="color: #339933;">&gt;&lt;/</span>audio<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Actually, it&#8217;s not quite true as this code won&#8217;t work as is. You also need to add a permission to the manifest file.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;">&quot;permissions&quot;: {
  &quot;audio-channel-content&quot;:{
    &quot;description&quot;:&quot;Use the audio channel for the music player&quot;
  }
}</pre></td></tr></table></div>

<p>Having the manifest line above will authorize your application to use the audio channel to play music, even when the screen is locked. Having said that, you probably realize that this code <strong>is specific to Firefox OS for now</strong>. I intentionally put the end of the last sentence in bold as it&#8217;s one thing you need to understand about Firefox OS: we had to create some APIs, features or elements to give the power HTML deserve for developers, but we are working with the W3C to make those standards. In the case that the standards won&#8217;t be the same as what we created, we&#8217;ll change it to reflect it.</p>
<h2>Firefox OS Web activities</h2>
<p>Finally, something very handy for Firefox OS developers: the <a href="https://developer.mozilla.org/en-US/docs/WebAPI/Web_Activities" title="Web Activities article on Mozilla Developer Network">Web Activities</a>. They define a way for applications to delegate an activity to another (usually user-chosen) application. They aren&#8217;t standardized, at the time of writing. In the case that will be interesting to us, we&#8217;ll use the Web Activity call <code>open</code>, to open music or video files. Note that for video, you can also use the <code>view</code> activity that basically does the same. Let&#8217;s say I want to open a remote video when someone clicks on a button with the id <code>open-video</code>: I&#8217;ll use the following code in my JavaScript to make it happen.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> openVideo <span style="color: #339933;">=</span> document.<span style="color: #660066;">querySelector</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#open-video&quot;</span><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>openVideo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    openVideo.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> openingVideo <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> MozActivity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            name<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;open&quot;</span><span style="color: #339933;">,</span>
            data<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                type<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
                  <span style="color: #3366CC;">&quot;video/webm&quot;</span><span style="color: #339933;">,</span>
                  <span style="color: #3366CC;">&quot;video/mp4&quot;</span><span style="color: #339933;">,</span>
                  <span style="color: #3366CC;">&quot;video/3gpp&quot;</span><span style="color: #339933;">,</span>
                  <span style="color: #3366CC;">&quot;video/youtube&quot;</span>
                <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
                url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://v2v.cc/~j/theora_testsuite/320x240.ogg&quot;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In that situation, the video player of Firefox OS will open, and play the video: it&#8217;s that easy!</p>
<h2>In the end&#8230;</h2>
<p>You may or may not need to use those tricks in your app, but adding videos or audio can enhance the quality of your application, and make it feel alive. At the end, you have to give a strong experience to your users, and it&#8217;s what will make the difference between a good and a great app!</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/make-your-firefox-os-app-feel-alive-with-video-and-audio/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
<enclosure url="http://v2v.cc/~j/theora_testsuite/320x240.ogg" length="285310" type="audio/ogg" />
<enclosure url="http://upload.wikimedia.org/wikipedia/en/4/45/ACDC_-_Back_In_Black-sample.ogg" length="191675" type="audio/ogg" />
		</item>
		<item>
		<title>Firefox Developer Tools: Episode 27 &#8211; Edit as HTML, Codemirror &amp; more</title>
		<link>https://hacks.mozilla.org/2013/11/firefox-developer-tools-episode-27-edit-as-html-codemirror-more/</link>
		<comments>https://hacks.mozilla.org/2013/11/firefox-developer-tools-episode-27-edit-as-html-codemirror-more/#comments</comments>
		<pubDate>Wed, 06 Nov 2013 16:51:59 +0000</pubDate>
		<dc:creator><![CDATA[Paul Rouget]]></dc:creator>
				<category><![CDATA[Developer Tools]]></category>
		<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[Firefox Aurora]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=22957</guid>
		<description><![CDATA[Firefox 27 was just uplifted to the Aurora release channel which means we are back to report on new features in Firefox Developer Tools. Below are just some of the new features, you can also take a look at all bugs resolved in DevTools for this release). JS Debugger: Break on DOM Events You can [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Firefox 27 was just uplifted to the <a href="http://www.mozilla.org/en-US/firefox/aurora/">Aurora release channel</a> which means we are back to report on new features in Firefox Developer Tools. Below are just some of the new features, you can also take a look at <a href="http://mzl.la/HbV06a ">all bugs resolved in DevTools for this release</a>).</p>
<h2>JS Debugger: Break on DOM Events</h2>
<p><iframe width="500" height="281" src="//www.youtube.com/embed/unnqd2P9XgI?rel=0" frameborder="0" allowfullscreen></iframe></p>
<p>You can now automatically break on a variety of DOM events, without needing to manually set a breakpoint. To do this, click on the &#8220;Expand Panes&#8221; button on the top right of the debugger panel (right next to the search box). Then flip over to the events tab. Click on an event name to automatically pause the next time it happens. This will only show events that currently have listeners bound from your code. If you click on one of the headings, like &#8220;Mouse&#8221; or &#8220;Keyboard&#8221;, then all of the relevant events will be selected.</p>
<h2>Inspector improvements</h2>
<p>We&#8217;ve listened to feedback from web developers and made a number of enhancements to the Inspector:</p>
<h3>Edit as HTML</h3>
<p>Now in the inspector, you can right click on an element and open up an editor that allows you to set the outerHTML on an element. </p>
<p><iframe width="500" height="375" src="//www.youtube.com/embed/H97grUcDsC8" frameborder="0" allowfullscreen></iframe></p>
<h3>Select default color format</h3>
<p>You now have an option to select the default color format in the option panel:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/dev-tools-default-color-format.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/dev-tools-default-color-format-500.png" alt=""></a></h3>
<h3>Color swatch previews</h3>
<p>The Developer Tools now offer color swatch previews that show up in the rule view:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/dev-tools-color-swatch-preview.png" alt=""></h3>
<h3>Image previews for background image urls</h3>
<p>Highly requested, we now offer image previews for background image URLs:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/dev-tools-image-previews.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/dev-tools-image-previews-500.png" alt=""></a></p>
<p>In addition to above improvements, <strong>Mutated DOM elements are now highlighted</strong> in the Inspector.</p>
<p>Keep an eye out for more <a href="https://groups.google.com/forum/#!topic/mozilla.dev.developer-tools/de0xBvHmN4s">tooltips</a> coming soon, and feel free to chime in if you have any others you&#8217;d like to see!</p>
<h2>Codemirror</h2>
<p><a href="http://codemirror.net/">Codemirror</a> is a popular HTML5-based code editor component used on web sites.  It is customizable and theme-able. The Firefox Devtools now use CodeMirror in various places: Style editor, Debugger, Inspector (Edit as HTML) and Scratchpad.</p>
<p>From the Option panel, the user can select which theme to use (dark or light).</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/dev-tools-themes.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/dev-tools-themes-500.png" alt=""></a></p>
<h2>WebConsole: Reflow Logging</h2>
<p>When the layout is invalidated (CSS or DOM changes), gecko needs to re-compute the position of some nodes. This computation doesn&#8217;t happen immediatly. It&#8217;s triggered for various reasons. For example, if you do &#8220;node.clientTop&#8221;, gecko needs to do this computation. This computation is called a &#8220;reflow&#8221;. Reflows are expensive. Avoiding reflows is important for responsiveness.</p>
<p>To enable reflow logging, check the &#8220;Log&#8221; option under the &#8220;CSS&#8221; menu in the Console tab. Now, everytime a reflow happens, a log will be printed with the name of the JS function that triggered this reflow (if caused by JS).</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/11/dev-tools-reflow-logging.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/11/dev-tools-reflow-logging-500.png" alt=""></a></p>
<p>That&#8217;s all for this time. Hope you like the new improvements!</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/firefox-developer-tools-episode-27-edit-as-html-codemirror-more/feed/</wfw:commentRss>
		<slash:comments>50</slash:comments>
		</item>
		<item>
		<title>Designing Web Apps For Multiple Devices</title>
		<link>https://hacks.mozilla.org/2013/11/designing-web-apps-for-multiple-devices/</link>
		<comments>https://hacks.mozilla.org/2013/11/designing-web-apps-for-multiple-devices/#comments</comments>
		<pubDate>Tue, 05 Nov 2013 11:21:11 +0000</pubDate>
		<dc:creator><![CDATA[Doug Reeder]]></dc:creator>
				<category><![CDATA[Apps]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=22720</guid>
		<description><![CDATA[Before 2010, there were few devices on the Web other than smartphones and desktop/laptop computers. Developers could assume phone users had a small screen, low bandwidth and used a webapp for brief moments. Desktop users were assumed to have a large screen, high bandwidth and spend large amounts of time within an application. Designing a [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Before 2010, there were few devices on the Web other than smartphones and desktop/laptop computers. Developers could assume phone users had a small screen, low bandwidth and used a webapp for brief moments. Desktop users were assumed to have a large screen, high bandwidth and spend large amounts of time within an application.</p>
<p>Designing a webapp now means designing for 2014 and beyond, where users buy giant phones, phablets and tablets in many sizes.  4G connections may be faster than DSL or the throttled Wi-Fi at a coffee-shop. A number of laptop and &#8220;all-in-one&#8221; computers have touchscreens.  Users may spend an evening hour on the couch with a tablet.  And it was never true that all desktop users wanted apps to take up their whole screen &#8211; writing an e-mail or report often requires consulting another app, which ought to be content with half the screen.</p>
<p>Segmenting users by device today typically leads to bad assumptions for large minorities. Users use multiple devices throughout the day and dislike learning different interaction patterns for different devices.  Apple&#8217;s &#8220;Back to the Mac&#8221; initiative sought to bring smartphone features to OS X, and Windows 8 and <a href="http://www.jonobacon.org/2013/08/27/ubuntu-in-a-nutshell-unity-and-convergence/">Ubuntu Unity</a> strive to have a single experience from phones to desktops.</p>
<h2>App examples</h2>
<p>It is difficult to achieve a good user experience for all devices and screen sizes, as seen by the criticism of Windows 8 and &#8220;Back to the Mac&#8221;. It can be done for a range of devices, especially by new apps unburdened by legacy expectations.  I present here two web apps optimized for smartphones through desktops (but not &#8220;smart&#8221; TVs, feature phones nor smartwatches): a photo gallery and a memo app.</p>
<h3>Photo gallery</h3>
<p>The photo gallery is half of a photo sharing app. <a href="http://hominidsoftware.com:1234/">A sample album of the gallery</a> can be seen online. (The <a href="http://hominidsoftware.com/zapphotoshare/index.html">application described as a whole</a> is worth seeing.)</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/ss_tablet_en_3_1.1.9_500.png" alt="photo gallery: thumbnail grid" title="thumbnail grid"></p>
<p>The photo gallery follows the paradigm used by a number of prior photo browsing apps. Thumbnails are presented in a grid which scrolls vertically. The number and width of columns depends on the screen width. Grid cells could be small or large; I chose the minimum cell size to be the smallest common screen width for smartphones: 320 CSS pixels. Thus, the number of columns is <code>floor(windowWidth / 320)</code> and the cell size is <code>windowWidth / number of columns</code>. Columns normally are from 320 to 639 pixels wide.</p>
<table>
<tr>
<th>Window Width</th>
<th># Columns</th>
<th>Typical Device</th>
</tr>
<tr>
<td>320-639</td>
<td>1</td>
<td>phone/phablet (portrait)</td>
</tr>
<tr>
<td>640-959</td>
<td>2</td>
<td>phablet (landscape), tablet (portrait)</td>
</tr>
<tr>
<td>960-1279</td>
<td>3</td>
<td>tablet (landscape)</td>
</tr>
<tr>
<td>1280-</td>
<td>4</td>
<td>laptop, desktop</td>
</tr>
</table>
<p>The number of columns is capped at four, so scrolling doesn&#8217;t outpace data transfer.</p>
<p>One approach would use fixed size thumbnails of 300&#215;300 and increase the padding as cells get larger. However, the image library available (libjpeg) only supports half-, quarter- and eighth-size reductions, so padding is fixed and images are resized to fill.  The web app requests a photo of appropriate size and the server (written in node.js, and normally running on a phone) returns the smallest version of the photo that fills the requested size cell.</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/Zap_ss_carousel_500.png" alt="photo gallery: carousel" title="carousel"></p>
<p>Tapping on a thumbnail switches to a carousel which fills the window.   The user can zoom in further by double-tapping the image or using the mousewheel.  Originally, full-screen mode was engaged when the carousel was displayed; however, changing to full-screen takes several seconds on many devices, so now full-screen mode must be manually set.  Touchscreen users can swipe to navigate forward and backward, while desktop users can use the keyboard.  A weakness is that a mouse user can click on a picture to navigate forward, but must swipe (or use the keyboard) to navigate backward.  Mouse users could benefit from  visible navigation buttons, but mice aren&#8217;t easy for a web app to detect.</p>
<p>Switching modes from the thumbnail gallery to the carousel and back is easy on any device.  This supports the typical scenario of: user skims thumbnails to photo of interest, taps to enlarge, uses carousel to go through related photos, and returns to thumbnails. Users can navigate a substantial number of photos the same way on any device. A larger screen is shows more thumbnails at once and more detail on individual photos, but interaction is the same.</p>
<p>Development was eased by creative use of the <a href="https://github.com/enyojs/enyo/wiki/Lists">List widget of Enyo 2</a> (though any list widget could have been used). Application code sizes the cells and fills each row with 1-4 thumbnails, while the List widget handles creating and re-using DOM elements. The carousel is Enyo&#8217;s <a href="http://enyojs.com/api/#enyo.ImageCarousel">ImageCarousel</a>, which is designed from the ground up to run well on multiple types of devices.</p>
<h3>Memo app</h3>
<p>The second example is a deceptively simple memo app, which <a href="http://searchablenotes.hominidsoftware.com/install.html">can be used on-line</a> or installed <a href="https://marketplace.firefox.com/app/searchable-memos/">on Firefox OS from the Marketplace</a></p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/Memos_ss_phab_land_2col_500.png" alt="Memos: phablet landscape (two columns)" title="phablet landscape (two columns)"></p>
<p>The fundamental layout is two panels side-by-side. In windows narrower than 640 CSS pixels, the second panel slides in on top of the first.  Collapse is based solely on window width, not device class, so phablet users will have one column in portrait mode and two columns in landscape mode.</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/Memos_ss_phab_port_1col_281.png" alt="Memos: phablet portrait (one column)" title="phablet portrait (one column)" width="281" height="500" class="aligncenter" /></p>
<p>Tablet and desktop users don&#8217;t need to muck with dialogs or layers, while phone users have fast transitions. Most importantly, the shift in mental models is easy when going from a phone to a larger device. While this app does not currently sync user data between devices, adding that would give a seamless experience across devices.</p>
<p>Presently, the Help pane displaces the Details pane.  In windows wider than 960px, Help should really be a third panel.</p>
<p>The collapsing panels use the Enyo <a href="http://enyojs.com/api/#enyo.Panels">Panels widget</a>. Another instance of Panels is used to swap the Details and Help panes in the second panel. Using a JavaScript framework designed to support multiple devices really speeds development.</p>
<p>To maximize display of user data the list foregoes controls on items in favor of direct manipulation: items can be swiped (or double-clicked) for operations such as Delete. Reordering is done by hold-then-drag. Discoverability was traded off for power &#8211; many apps will need to strike a different balance. A possible future enhancement on larger screens would be to add visible controls or at least tool tips.</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/Notes_ss_phn_en_2_1.0.0.png" alt="Memos: live search (phone)" title="live search (phone)" class="aligncenter" ></p>
<p>Fixed groupings such as folders or notebooks would require the user to navigate an extra level to access data. Search allows a memo to be in multiple groupings, and requires less UI. (However, it is not appropriate for users who rely on spatial memory.)</p>
<p>While typing on a touchscreen is laborious, the app minimizes typing by using prefix searches. Searching for &#8220;s fra&#8221; will find all memos that contain &#8220;San Francisco&#8221; and few false positives among hundreds of memos. The on-screen keyboard on most phones obscures all but a few list items, but live search and the counter of hits tells the user when she has narrowed the search to a handful of memos. Skimming the list then allows the user to select the desired record. Live full-text search is implemented using IndexedDB.</p>
<p>Typing is also minimized by localizing the search to the preferred language of the browser or OS. In Spanish ñ is a different letter than n (and Spanish keyboards have a key devoted to it), just as w is a different letter than u in English. For Spanish users, peña never matches pena, reducing false matches. However, users are not required to know languages other than their own, so for a Spanish user &#8220;cote&#8221; matches the French cote, coté, côte and côté.</p>
<p>Hardware keyboards allow users to type more quickly and accurately, larger screens minimize scrolling, and faster processors and disks make complicated searches nearly instantaneous, but the interface works the same for all devices.</p>
<h2>Mobile first</h2>
<p>Both apps were designed &#8220;mobile-first&#8221;, which tends to result in lean apps, with fewer features than standard desktop apps. Given that most features in desktop apps are rarely used, that&#8217;s not necessarily a problem for desktop users.  Carefully-chosen defaults eliminate the need for preference settings.</p>
<p>Working out a user interface design that works well across a range of devices often requires more effort initially, but can result in a more user friendly experience with the added benefit of being easier to maintain.  A few powerful user-interface elements can eliminate the need for many panes, dialogs, dropdowns and buttons.</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/designing-web-apps-for-multiple-devices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reintroducing the Firefox Developer Tools, part 2: the Scratchpad and the Style Editor</title>
		<link>https://hacks.mozilla.org/2013/11/reintroducing-the-firefox-developer-tools-part-2-the-scratchpad-and-the-style-editor/</link>
		<comments>https://hacks.mozilla.org/2013/11/reintroducing-the-firefox-developer-tools-part-2-the-scratchpad-and-the-style-editor/#comments</comments>
		<pubDate>Mon, 04 Nov 2013 18:07:15 +0000</pubDate>
		<dc:creator><![CDATA[Jason Weathersby]]></dc:creator>
				<category><![CDATA[Developer Tools]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=22934</guid>
		<description><![CDATA[This is part two, out of five, focusing on the built-in Developer Tools in Firefox, their features and where we are now with them. The intention is to show you all the possibilities available, the progress and what we are aiming for. In the first post in the series we discussed the Web Console and [&#8230;]]]></description>
				<content:encoded><![CDATA[<blockquote><p>This is part two, out of five, focusing on the built-in <a href="https://developer.mozilla.org/en-US/docs/Tools">Developer Tools in Firefox</a>, their features and where we are now with them. The intention is to show you all the possibilities available, the progress and what we are aiming for.</p>
</blockquote>
<p>In the first post in the series <a href="https://hacks.mozilla.org/2013/09/reintroducing-the-firefox-developer-tools-part-1-the-web-console-and-the-javascript-debugger/">we discussed the Web Console and the JavaScript Debugger</a>. While these two tools are powerful and provide capabilities to interrogate and alter your web applications, additional tools are available to further enhance the developer experience while building and debugging your apps.  In this post we briefly cover the Scratchpad and the Style editor.</p>
<p>As with the first post, we present each tool with a quick screencast demonstrating some of their capabilities.</p>
<h2>The Style Editor</h2>
<p>The Style Editor is primarily used to edit, debug or create new stylesheets within the context of the current app.  Changes made in the style editor are automatically reflected in the loaded page.  If you are not familiar with Cascading Style Sheets (CSS), please be sure to take a look at the <a href="https://developer.mozilla.org/en-US/docs/Web/CSS">CSS MDN documentation</a>.</p>
<p>The Style Editor allows saving the changes made while using the editor.  In addition you can also import existing stylesheets and apply them to the current page or individually disable specific stylesheets.  The Style Editor is also linked to the Inspector allowing developers quick access to the stylesheet for the inspected element.  The following screencast presents an overview of the Style Editor’s features.</p>
<p><iframe width="500" height="281" src="//www.youtube.com/embed/7839qc55r7o" frameborder="0" allowfullscreen></iframe></p>
<p>For more detailed <a href="https://developer.mozilla.org/en-US/docs/Tools/Style_Editor">information on the Style Editor</a> take a look at the MDN documentation.</p>
<h2>Scratchpad</h2>
<p>The Scratchpad has many uses and is essentially a live JavaScript editor and prototyping tool.  Using the Scratchpad, a developer can access the current page’s objects, variables and script.  In addition, complete functions can be written and tested in the editor within the scope of the live page.  These changes can then be attached and saved with the current application.</p>
<p>External JavaScript files can also be loaded and tested.  Several run options are available to allow a developer to just execute the code, execute the code and inspect the returned object, or execute the code and print out the results as a comment within the Scratchpad.  The following screencast illustrates some of the features of the Scratchpad.  Note that Scratchpad script runs in the same<br />
context as a script loaded into the page. In the screencast, the example uses the jQuery library and some custom script to illustrate this feature.
</p>
<p><iframe width="500" height="281" src="//www.youtube.com/embed/Pt7DZACyClM" frameborder="0" allowfullscreen></iframe></p>
<p>For more <a href="https://developer.mozilla.org/en-US/docs/Tools/Scratchpad">information on the Scratchpad</a>, see the MDN Debugger documentation.</p>
<p>If you are not very familiar with JavaScript, make sure to check out the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript">MDN documentation for a comprehensive list of resources on learning the language</a> and mechanics.</p>
<h2>Learn More</h2>
<p>These screencasts give a quick introduction to the main features of these tools. For the <a href="https://developer.mozilla.org/en-US/docs/Tools">full details on all of the Developer Tools</a>, check out the full MDN Tools documentation.</p>
<h2>Coming Up</h2>
<p>In the next post, we will focus on some Mobile design features including the Responsive Design View and Remote debugging using the App Manager. Please provide your suggestions on what features you would like to see explained in more detail in this upcoming post, by commenting below.</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/11/reintroducing-the-firefox-developer-tools-part-2-the-scratchpad-and-the-style-editor/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Monetization with Inneractive on Firefox OS</title>
		<link>https://hacks.mozilla.org/2013/10/monetization-with-inneractive-on-firefox-os/</link>
		<comments>https://hacks.mozilla.org/2013/10/monetization-with-inneractive-on-firefox-os/#comments</comments>
		<pubDate>Thu, 31 Oct 2013 22:20:24 +0000</pubDate>
		<dc:creator><![CDATA[Louis Stowasser]]></dc:creator>
				<category><![CDATA[Firefox OS]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=22913</guid>
		<description><![CDATA[Monetization is important for any viable platform so developers can benefit from their hard work and to further encourage quality apps. Mozilla teamed up with the ad network, Inneractive, to create a simple library for integrating ads into apps and games specifically for Firefox OS. This article will go through the process of integrating Inneractive [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Monetization is important for any viable platform so developers can benefit from their hard work and to further encourage quality apps. Mozilla teamed up with the ad network, <a href="http://inner-active.com">Inneractive</a>, to create a simple library for integrating ads into apps and games specifically for Firefox OS.</p>
<p>This article will go through the process of integrating Inneractive ads in your Firefox OS app.</p>
<h2>Getting Started</h2>
<ol>
<li>Download the library from the <a href="https://github.com/mozilla/inneractive">Github page</a>, specifically <code>inneractive.js</code>.</li>
<li>Include the <code>inneractive.js</code> script in your HTML (or through any other script loader):</li>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;inneractive.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<li>Create an account at <a href="https://console.inner-active.com/iamp/iamp/publisher/register">Inneractive</a>. Once your account is approved you can access the console and create an App.
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/Screen-Shot-2013-10-28-at-10.48.57-AM.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/Screen-Shot-2013-10-28-at-10.48.57-AM-500x225.png" alt="Create App - Inneractive Console" width="500" height="225" class="alignnone size-large wp-image-22919" /></a></p>
<p>This will generate a unique &#8216;App ID&#8217; and can be found at the bottom of the Dashboard.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/Screen-Shot-2013-10-28-at-10.50.29-AM.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/Screen-Shot-2013-10-28-at-10.50.29-AM-500x50.png" alt="Dashboard - Inneractive Console" width="500" height="50" class="alignnone size-large wp-image-22920" /></a></p>
</li>
</ol>
<h2>Creating the Ad</h2>
<p>Your app should have access to the global <code>Inneractive</code> object. Create an ad with the function <code>createAd</code>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> myAd <span style="color: #339933;">=</span> Inneractive.<span style="color: #660066;">createAd</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p><code>options</code> is an object where you customize the ad. The available options<br />
are:</p>
<ul>
<li><strong>APP_ID</strong> &#8211; This can be found in the previous section when creating an App through the Inneractive Console.</li>
<li><strong>TYPE</strong> &#8211; Can be one of three types of ads:
<ul>
<li><code>Banner</code>: Small ad that is usually constant at the bottom of the screen.</li>
<li><code>Rectangle</code>: Medium sized ad that is usually centered in the middle of<br />
the screen.</li>
<li><code>Interstitial</code>: Fullscreen ad to display usually during levels or<br />
screens.</li>
</ul>
</li>
<li><strong>REFRESH_RATE</strong> &#8211; Time in seconds between rotating ads. Minimum is 15 seconds, default is 30.</li>
</ul>
<h3>Example</h3>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> options <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    TYPE<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Banner&quot;</span><span style="color: #339933;">,</span>
    REFRESH_RATE<span style="color: #339933;">:</span> <span style="color: #CC0000;">18</span><span style="color: #339933;">,</span>
    APP_ID<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Test_App_ID&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">var</span> myAd <span style="color: #339933;">=</span> Inneractive.<span style="color: #660066;">createAd</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Placing the Ad</h2>
<p>Once the ad has been created with your options, you need to place it on the screen.</p>
<p>The function <code>addTo</code> will place the ad into the DOM tree under a parent node. You can usually just use <code>document.body</code> for this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">myAd.<span style="color: #660066;">addTo</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will place the ad under the <code>&lt;body&gt;</code> element of your webpage.</p>
<p>Then you need to position the ad with the function <code>placement</code>. This function takes two arguments, vertical position and horizontal position where the options are <code>top</code>, <code>bottom</code>, <code>center</code> and <code>left</code>, <code>right</code>, <code>center</code>.</p>
<p>For a banner ad to sit at the bottom of the screen you would do the following:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">myAd.<span style="color: #660066;">placement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;bottom&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;center&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>A rectangle that is exactly in the center of the screen:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">myAd.<span style="color: #660066;">placement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;center&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;center&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Removing the Ad</h2>
<p>If you need to remove the ad from the screen for whatever reason, use the function <code>remove</code>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">myAd.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Once the ad is removed you cannot bring it back and will need to create a new ad with <code>Inneractive.createAd()</code>.</p>
<p>If you have any issues or need support using the library you can file issues on the <a href="https://github.com/mozilla/inneractive/issues">Github Issue Tracker</a>.</p>
<h2>More Monetization options</h2>
<p>With Firefox OS you may use any of your favorite ad networks that would work in a browser such as Google Adsense using their integration code.</p>
<p>You also have the option of integrating <a href="https://developer.mozilla.org/en-US/Apps/Publishing/In-app_payments">In-app payments</a> for selling digital goods through your app or game.</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/10/monetization-with-inneractive-on-firefox-os/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Halloween artist</title>
		<link>https://hacks.mozilla.org/2013/10/halloween-artist/</link>
		<comments>https://hacks.mozilla.org/2013/10/halloween-artist/#comments</comments>
		<pubDate>Wed, 30 Oct 2013 08:33:26 +0000</pubDate>
		<dc:creator><![CDATA[Owen Swerkstrom]]></dc:creator>
				<category><![CDATA[Apps]]></category>
		<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Firefox OS]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=22718</guid>
		<description><![CDATA[A while back, I made a little toy that simulates carving pumpkins. It was during that narrow window when the WebOS-running TouchPad was new and hot. Since then, web browsers have gown up a lot, and nowadays Mozilla is executing the vision of a browser-based operating system with Firefox OS. In any case, I&#8217;ve been [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>A while back, I made a little toy that <a href="http://penduin.blogspot.com/2011/10/halloween-artist.html">simulates carving pumpkins</a>. It was during that narrow window when the WebOS-running TouchPad was new and hot.</p>
<p>Since then, web browsers have gown up a lot, and nowadays Mozilla is executing the vision of a browser-based operating system with Firefox OS. In any case, I&#8217;ve been digging back and dusting off some of my old apps.  When you get your app running on FirefoxOS, you don&#8217;t just port it to yet another device &#8211; you port it to the web. So now, Halloween Artist runs on near anything, including those awesome (and affordable!) Firefox phones that are starting to spring up everywhere.</p>
<p>The platform-agnostic web app: <a href="http://halloweenartist.penduin.net">http://halloweenartist.penduin.net</a><br />
On the Firefox Marketplace: <a href="https://marketplace.firefox.com/app/halloween-artist">https://marketplace.firefox.com/app/halloween-artist</a></p>
<p>Play around with it before reading on, if Halloween Artist is new to you.</p>
<p>But enough history; the point of this post is to dive into the jack-o-lantern guts and talk about how the program actually works!</p>
<h2>References</h2>
<p>Before we begin, some links!</p>
<ul>
<li>To this day, I <a href="https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Canvas_tutorial">start here whenever I tinker with canvas</a></li>
<li>Back in the day I only used mouse events, and just recently added actual touch support.  Nowadays, I&#8217;d suggest going the other way &#8217;round, or at least <a href="https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Events/Touch_events">keeping both in mind</a>.</li>
<li>There are many guides for doing simple drawing programs using canvas.  <a href="http://dev.opera.com/articles/view/html5-canvas-painting/">This one is very detailed</a>.</li>
</ul>
<h2>Let&#8217;s go already!</h2>
<p>The first step is to get ourselves a pumpkin image in the background, and a canvas layered on top.  This canvas will track mouse and touch events and let the user trace out shapes.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig1.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig1-500.png" alt=""></a></p>
<p>Next, we need an &#8220;inside&#8221; image that will show through the carved shapes.  Over that, we&#8217;ll draw the pumpkin but with the user-drawn parts cut out (made transparent).  As luck would have it, the canvas API has some handy compositing modes that are perfect for these tasks.  The main operation we need is &#8220;source-out&#8221;.  Keep the destination image, except where it intersects with the source shape.  Then it&#8217;s just a matter of doing a normal, source-over composite.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> face <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;draw&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> faceCtx <span style="color: #339933;">=</span> face.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;2d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> glow <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;glow&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> dest <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;bg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> destCtx <span style="color: #339933;">=</span> dest.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;2d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> img <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;pumpkin&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
dest.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> dest.<span style="color: #660066;">width</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// reset the destination canvas</span>
faceCtx.<span style="color: #660066;">globalCompositeOperation</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;source-out&quot;</span><span style="color: #339933;">;</span>
faceCtx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>img<span style="color: #339933;">,</span>
                  <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</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>
                  <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> size<span style="color: #339933;">,</span> size<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// draw glowing background (to dest)</span>
destCtx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>glow<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> glow.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> glow.<span style="color: #660066;">height</span><span style="color: #339933;">,</span>
                  <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> size<span style="color: #339933;">,</span> size<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// apply the face</span>
destCtx.<span style="color: #660066;">globalAlpha</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
destCtx.<span style="color: #660066;">globalCompositeOperation</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;source-over&quot;</span><span style="color: #339933;">;</span>
destCtx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>face<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig22.png" alt=""></p>
<p>It&#8217;s a start!  But to look like an actual carved pumpkin, we need to add some 3D magic to draw the inside edges.  Actually, scratch that &#8211; we&#8217;re going to cheat!  :^)  We&#8217;ll start with a lower-resolution, slightly-blown-up pumpkin image:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig3a.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig3a.png" alt="" width=250 height=250></a></p>
<p>&#8230;then we&#8217;ll lighten it up using canvas&#8217;s &#8220;lighter&#8221; globalCompositeOperation and a globalAlpha value of, oh let&#8217;s say 0.5:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig3b.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig3b-500.png" alt="" width=250 height=250></a></p>
<p>&#8230;then we&#8217;ll &#8220;source-out&#8221; the face, same as we did with the foreground:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig3c.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig3c-500.png" alt="" width=250 height=250></a></p>
<p>&#8230;then shrink it a bit, center it, and draw it between the inside background and the outer face:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig4.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig4-500.png" alt=""></a></p>
<p>We&#8217;re getting there!  But depending on the shape, our corners might not look very convincing.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig5.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig5-500.png" alt=""></a></p>
<p>Fortunately, all this cheating we&#8217;ve been doing &#8211; these composite operations and scaling &#8211; it&#8217;s all very fast.  Even on mobile browsers.  Let&#8217;s turn up the cheating to maximum and draw that middle layer in a loop, shrinking it less each time.</p>
<p>&#8230;While we&#8217;re at it, each step could lighten up the current layer to a lesser degree than the previous (more &#8220;inner&#8221;) one.  And while we&#8217;re at that, let&#8217;s lighten it using a more yellow color; our first pass looks a little pinkish.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// build pumpkin by layer and draw it shrunk, inner to outer (to dest)</span>
<span style="color: #000066; font-weight: bold;">var</span> i<span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> darken <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.4</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">56</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">-=</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// mix a color for the layer</span>
    scratchCtx.<span style="color: #660066;">globalCompositeOperation</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;source-over&quot;</span><span style="color: #339933;">;</span>
    scratchCtx.<span style="color: #660066;">globalAlpha</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    scratchCtx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>glow<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> glow.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> glow.<span style="color: #660066;">height</span><span style="color: #339933;">,</span>
                         <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">768</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">768</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>       <span style="color: #006600; font-style: italic;">// bright glow...</span>
    scratchCtx.<span style="color: #660066;">globalAlpha</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0.3</span><span style="color: #339933;">;</span>
    scratchCtx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>flick<span style="color: #339933;">,</span>                 <span style="color: #006600; font-style: italic;">// light...</span>
                         <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">16</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">16</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">768</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">768</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    scratchCtx.<span style="color: #660066;">globalAlpha</span> <span style="color: #339933;">=</span> darken<span style="color: #339933;">;</span>
    scratchCtx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>flesh<span style="color: #339933;">,</span>                 <span style="color: #006600; font-style: italic;">// darken with flesh</span>
                         <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">256</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">256</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">8</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">768</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">16</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">768</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    darken <span style="color: #339933;">+=</span> <span style="color: #CC0000;">0.02</span><span style="color: #339933;">;</span>                             <span style="color: #006600; font-style: italic;">// ...more each time</span>
    <span style="color: #006600; font-style: italic;">// cut out the face</span>
    <span style="color: #006600; font-style: italic;">// NOTE: &quot;face&quot; is already the outer layer.</span>
    <span style="color: #006600; font-style: italic;">//       we want to copy its alpha mask, so &quot;-in&quot; instead of &quot;-out&quot;.</span>
    scratchCtx.<span style="color: #660066;">globalCompositeOperation</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;destination-in&quot;</span><span style="color: #339933;">;</span>
    scratchCtx.<span style="color: #660066;">globalAlpha</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    scratchCtx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>face<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// draw layer</span>
    destCtx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>scratch<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">768</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">768</span><span style="color: #339933;">,</span>
                      i<span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> <span style="color: #CC0000;">768</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">*</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">768</span> <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">*</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>By putting that middle-layer step inside a loop and making a few tweaks, we get much more realistic edges:</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig6.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig6-500.png" alt=""></a></p>
<p>So, we draw a whole bunch of these middle layers, only to draw right over most of it during the next pass.  It&#8217;s a bit wasteful if you think about it that way, but consider how much simpler this is than trying to simulate all these arbitrary cut-out surfaces &#8220;for real&#8221;.  Instead, this code builds little bits of pumpkin shell, one layer at a time, from the inside out.  It&#8217;s a fairly elegant illusion if I do say so myself; we can make a passably-realistic image with a sense of depth, without actually doing any complex calculations or intensive processing.</p>
<h2>Adding polish</h2>
<p>Warning: more history ahead.  I&#8217;ll try to keep it brief and on-point.  :^)</p>
<p>Figuring out how &#8220;deep&#8221; to start (how much to shrink), and how many steps to draw in between, all while keeping the &#8220;carve&#8221; function reasonably fast, was one of those fun bits of experimentation and compromise.  On a high-resolution display, there can still be artifacts if you draw very steep, jagged shapes.  But as far as bang per buck, compatibility with low-end devices, and the 99%-of-shapes use cases, I&#8217;m pretty pleased.</p>
<p>I wanted this to be pick-up-and-play friendly, so every day I&#8217;d load the latest version onto my TouchPad and hand it to my coworkers, giving them no instructions.</p>
<p>Early on, it was suggested that the carved image should flicker as if the candle inside were burning unevenly.  Easy!  The carve function now produces two images, the normal one and a slightly brighter version, which is positioned (using CSS) right over the main one.  It fades in and out using a randomized timeout and CSS animation on the &#8220;opacity&#8221; property.  A few minutes of polish, and the illusion was even better.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.fade</span> <span style="color: #00AA00;">&#123;</span>
    transition<span style="color: #00AA00;">:</span> opacity 0.5s linear<span style="color: #00AA00;">;</span>
    -moz-transition<span style="color: #00AA00;">:</span> opacity 0.5s linear<span style="color: #00AA00;">;</span>
    -webkit-transition<span style="color: #00AA00;">:</span> opacity 0.5s linear<span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #6666ff;">.fade</span><span style="color: #6666ff;">.out</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></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> animateFlicker<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> flicker <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;flicker&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>HA.<span style="color: #660066;">flickerTimer</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        clearTimeout<span style="color: #009900;">&#40;</span>HA.<span style="color: #660066;">flickerTimer</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        HA.<span style="color: #660066;">flickerTimer</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</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><span style="color: #339933;">!</span>HA.<span style="color: #660066;">settings</span>.<span style="color: #660066;">flicker</span><span style="color: #009900;">&#41;</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>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>flicker.<span style="color: #660066;">className</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;fade out&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        flicker.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;fade &quot;</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>
        flicker.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;fade out&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    HA.<span style="color: #660066;">flickerTimer</span> <span style="color: #339933;">=</span> setTimeout<span style="color: #009900;">&#40;</span>animateFlicker<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I forget who, but one coworker went right for the &#8220;Carve&#8221; button before drawing anything.  Natural enough instinct.  So, added some logic to see if any shapes had been drawn yet, and if not, give the user some quick instructions.  I&#8217;m really glad I caught that, because in showing the app to more people later, about a quarter did the same thing.  Much better to show a hint popup than have users wonder why nothing&#8217;s happening.</p>
<p>What if the user drew outside the pumpkin?  All kinds of goofy artifacts, that&#8217;s what.  So, I used that handy canvas compositing and masked the user&#8217;s input before carving.  As a nice side effect, you can now carve all the way out to the edge of a pumpkin, as though you&#8217;d chopped it in half.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig7a.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig7a-500.png" alt=""></a></p>
<p>This solution led to another problem.  When people realized they could recklessly carve giant holes, they&#8217;d see the empty, glowing inside surface of the pumpkin.  Where was the light coming from?</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig7b.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/fig7b-500.png" alt=""></a></p>
<p>So, between drawing the background and drawing the scaled flesh layers, I dropped in a candle.  I actually took some photos of a nice white candle and GIMPed it into the shape of my blocky &#8220;penduin&#8221; avatar.  May as well include some kind of signature in this program, eh?  I made two different versions, one for each of the flicker-fading images, and made the flame a bit bigger on the brighter version.  It gives a nice little touch of animation, and adds a bit more to the illusion.</p>
<p>Then of course there was the less exciting stuff.  Take out those nasty hard-coded values and make it work at any screen size.  Make sure touch and mouse support both work as expected.  Rearrange the buttons if the screen is small and they&#8217;d be in the way.  All that jazz.</p>
<h2>Code on GitHub &#8211; Happy Halloween!</h2>
<p>Well, that about covers it.  You&#8217;re free and encouraged to poke around in the source if you&#8217;d like to learn more or add your own tweaks.  (Mind the mess; I left some experimental tweaks and previous-attempts in there, commented out.) Halloween Artist is GPLv3, and since you might not feel like scraping down the source from the web app itself (and since my lousy DSL might be down at any given moment) I&#8217;ve made it <a href="https://github.com/penduin/halloweenartist">available on GitHub</a>.</p>
<p>Have fun, and Happy Halloween!  :^)</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/end.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/end-500.png" alt=""></a></p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/10/halloween-artist/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Songs of Diridum: Pushing the Web Audio API to Its Limits</title>
		<link>https://hacks.mozilla.org/2013/10/songs-of-diridum-pushing-the-web-audio-api-to-its-limits/</link>
		<comments>https://hacks.mozilla.org/2013/10/songs-of-diridum-pushing-the-web-audio-api-to-its-limits/#comments</comments>
		<pubDate>Tue, 29 Oct 2013 15:04:19 +0000</pubDate>
		<dc:creator><![CDATA[Tom Söderlund]]></dc:creator>
				<category><![CDATA[Audio]]></category>
		<category><![CDATA[Featured Article]]></category>
		<category><![CDATA[Games]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=22889</guid>
		<description><![CDATA[When we at Goo Technologies heard that the Web Audio API would be supported in an upcoming version of Mozilla Firefox, we immediately started brainstorming about what we could build with that. We started discussing the project with the game developers behind “Legend of Diridum” (see below) and came up with the idea of a [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>When we at <a href="http://www.gootechnologies.com">Goo Technologies</a> heard that the Web Audio API would be supported in an upcoming version of Mozilla Firefox, we immediately started brainstorming about what we could build with that.</p>
<p>We started discussing the project with the game developers behind “Legend of Diridum” (see below) and came up with the idea of a small market place and a small jazz band on a stage. The feeling we wanted to capture was that of a market place coming to life. The band is playing a song to warm up and the crowd has not gathered around yet. The evening is warm and the party is about to start.</p>
<p>We call the resulting demo <a href="http://www.gooengine.com/mozilla-web-audio-demo-built-in-goo-engine/">“Songs of Diridum”</a>.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/Songs-of-Diridum-5.jpg"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/Songs-of-Diridum-5-500.jpg" alt="" /></a></p>
<h2>What the Web Audio API can do for you</h2>
<p>We will take a brief look at the web audio system from three perspectives. These are game design, audio engineering and programming.</p>
<p>From a game designer perspective we can use the functionality of the Web Audio API to tune the soundscape of our game. We can run a whole lot of separate sounds simultaneously while also adjusting their character to fit an environment or a game mechanic. You can have muffled sounds coming through a closed door and open the filters for these sounds to unmuffle them gradually as the door opens. In real time. We can add reflecting sounds of the environment to the footsteps of my character as we walk from a sidewalk into a church. The ambient sounds of the street will be echoing around in the church together with my footsteps. We can attach the sounds of a roaring fire to my magicians fireball, hurl it away and hear the fireball moving towards its target. We can hear the siren of a police car approaching and hear how it passes by from the pitch shift known as doppler effect. And we know we can use these features without needing to manage the production of an audio engine. Its already there and it works.</p>
<p>From an audio engineering perspective we view the Web Audio API as a big patch bay with a slew of outboard gear tape stations and mixers. On a low level we feel reasonably comfortable with the fundamental aspects of the system. We can work comfortably with changing the volume of a sound while it is playing without running the risk of inducing digital distortion from the volume level changing from one sample to another. The system will make the interpolation needed for this type of adjustment. We can also build the type of effects we want and hook them up however we want. As long as we keep my system reasonably small we can make a nice studio with the Web Audio API.</p>
<p>From a programmer perspective we can write the code needed for our project with ease. If we run into a problem we will usually find a good solution to it on the web. We don’t have to spend our time with learning how to work with some poorly documented proprietary audio engine. The type of problem we will be working with the most is probably related to how the code is structured. We will be figuring out how to handle the loading of the sounds and which sounds to load when. How to provide these sounds to the game designer through some suitable data structure or other design pipelines. We will also work with the team to figure out how to handle the budgeting of the sound system. How much data can we use? How many sounds can we play at the same time? How many effects can we use on the target platform? It is likely that the hardest problem, the biggest technical risk, is related to handling the diversity of hardware and browsers running on the web.</p>
<h2>About Legend of Diridum</h2>
<p>This sound demo called “Songs of Diridum” is actually a special demo based on graphics and setting from the upcoming game <a href="http://www.lodsaga.com">“LOD: Legend of Diridum”</a>. The LOD team is led by the game design veteran Michael Stenmark.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/Songs-of-Diridum-3.jpg"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/Songs-of-Diridum-3-500.jpg" alt="" /></a></p>
<p>LOD is an easy to learn, user-friendly fantasy role playing game set on top of a so called sandbox world. It is a mix of Japanese fantasy and roleplaying games drawing inspiration from Grandia, Final Fantasy, Zelda and games like Animal Crossing and Minecraft.</p>
<p>The game is set in a huge fantasy world, in the aftermath of a terrible magic war. The world is haunted by the monsters, ghosts and undead that was part of the warlocks armies and the player starts the game as the Empire’s official ghost hunter to cleanse the lands of the evil and keep the people of Diridum safe. LOD is built in the Goo Engine and can be played in almost any web browser without the need download anything.</p>
<h2>About the music</h2>
<p>The song was important to set the mood and to embrace the warm feeling of a hot summer night turning into a party. Adam Hagstrand, the composer, nailed it right away. We love the way he got it very laid back, jazzy. Just that kind of tune a band would warm up with before the crowd arrives.</p>
<h2>Quickly building a 3D game with Goo Engine</h2>
<p>We love the web, and we love HTML5. HTML5 runs in the browser on multiple devices and does not need any special client software to be downloaded and installed. This allows for games to be published on nearly every conceivable web site, and since it runs directly in the browser, it opens up unprecedented viral opportunities and social media integration.</p>
<p>We wanted to build Songs of Diridum as a HTML5 browser game, but how to do that in 3D? The answer was WebGL. WebGL is a new standard in HTML5 that allows games to gain access to hardware acceleration, just like native games. The introduction of WebGL in HTML5 is a massive leap in what the browser can deliver and it allows for web games to be built with previously unseen graphics quality. WebGL powered HTML5 does not require that much bandwidth during gameplay. Since the game assets are downloaded (pre-cached) before and during gameplay, even modest speed internet connections suffice.</p>
<p>But building a WebGL game from scratch is a massive undertaking. The <a href="http://www.gooengine.com">Goo Platform</a> from Goo Technologies is the solution for making it much easier to build WebGL games and applications. In November, Goo Create is released making it even more accessible and easy to use.</p>
<p>Goo is a HTML5 and WebGL based graphics development platform capable of powering the next generation of web games and apps. From the ground up, it’s been built for amazing graphics smoothness and performance while at the same time making things easy for graphics creators. Since it’s HTML5, it enables creators to build and distribute advanced interactive graphics on the web without the need for special browser plugins or software downloads. With Goo you have the power to publish hardware accelerated games &amp; apps on desktops, laptops, smart TVs, tablets or mobile devices. It gives instant access to smooth rich graphics and previously unimagined browser game play.</p>
<p><strong>UPDATE:</strong> Goo Technologies has just launched their interactive 3D editor, <a href="http://www.gootechnologies.com/products/create/">Goo Create</a>, which radically simplifies the process of creating interactive WebGL graphics.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/Songs-of-Diridum-2.jpg"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/Songs-of-Diridum-2-500.jpg" alt="" /></a></p>
<h2>Building Songs of Diridum</h2>
<p>We built this demo project with a rather small team working for a relatively short time. In total we have had about seven or so people involved with the production. Most team members have done sporadic updates to our data and code. Roughly speaking we have not been following any conventional development process but rather tried to get as good a result as we can without going into any bigger scale production.</p>
<p>The programming of the demo has two distinct sides. One for building the world and one for wiring up the sound system. Since the user interface primarily is used for controlling the state of the sound system we let the sound system drive the user interface. It’s a simple approach but we also have a relatively simple problem to solve. Building a small 3D world like this one is mostly a matter of loading model data to various positions in 3D space. All the low level programming needed to get the scene to render with the proper colors and shapes is handled by the Goo Engine so we have not had to write any code for those parts.</p>
<p>We defined a simple data format for adding model data to the scene, we also included the ability to add sounds to the world and some slightly more complex systems such as the animated models and the bubbly water sound effect.</p>
<p>The little mixer panel in which you can play around with to change the mix of the jazz band is dynamically generated by the sound system:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/Songs-of-Diridum-1.png" alt="" /></p>
<p>Since we expected this app to be used on touch screen devices we also decided to only use clickable buttons for interface. We would not have enough time to test the usability of any other type of controls when aiming at a diffuse collection of mobile devices.</p>
<h3>Using Web Audio in a 3D world</h3>
<p>To build the soundscape of a 3D world we have access to spatialization of sound sources and the ears of the player. The spatial aspects of sounds and listeners are boiled down to position, direction and velocity. Each sound can also be made to emit its sound in a directional cone, in short this emulates the difference between the front and back of loudspeaker. A piano would not really need any directionality as it sounds quite similar in all directions. A megaphone on the other hand is comparably directional and should be louder at the front than at the back.</p>
<pre><code>if (is3dSource) {
    // 3D sound source
    this.panNode = this.context.createPanner();
    this.gainNode.connect(this.panNode);
    this.lastPos = [0,0,0];
    this.panNode.rolloffFactor = 0.5;
} else {
    // Stereo sound source “hack”
    this.panNode = mixNodeFactory.buildStereoChannelSplitter(this.gainNode, context);
    this.panNode.setPosition(0, this.context.currentTime);
}
</code></pre>
<p>The position of the sound is used to determine panning or which speaker the sound is louder in and how loud the sound should be.</p>
<p>The velocity of the sound and the listener together with their positions provide the information needed to doppler shift all sound sources in the world accordingly. For other worldly effects such as muffling sounds behind a door or changing the sound of the room with reverbs we’ll have to write some code and configure processing nodes to meet with our desired results.</p>
<p>For adding sounds to the user interface and such direct effects, we can hook the sounds up without going through the spatialization, which makes them a bit simpler. You can still process the effects and be creative if you like. Perhaps pan the sound source based on where the mouse pointer clicked.</p>
<h3>Initializing Web Audio</h3>
<p>Setting up for using Web Audio is quite simple. The tricky parts are related to figuring out how much sound you need to preload and how much you feel comfortable with loading later. You also need to take into account that loading a sound contains two asynchronous and potentially slow operations. The first is the download, the second is the decompression from some small format such as OGG or MP3 to arraybuffer. When developing against a local machine you’ll find that the decompression is a lot slower than the download and as with download speeds in general we can expect to not know how much time this will require for any given user.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/Songs-of-Diridum-6.jpg"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/Songs-of-Diridum-6-500.jpg" alt="" /></a></p>
<h3>Playing sound streams</h3>
<p>Once you have a sound decompressed and ready it can be used to create a sound source. A sound source is a relatively low level object which streams its sound data at some speed to its selected target node. For the simplest system this target node is the speaker output. Even with this primitive system, you can already manipulate the playback rate of the sound, this changes its pitch and duration. There is a nice feature in the Web Audio API which allows you to adjust the behaviour of interpolating a change like this to fit your desires.</p>
<h3>Adding sound effects: reverb, delay, and distortion</h3>
<p>To add an effect to our simple system you put a processor node between the source and the speakers. us audio engineers wants to split the source to have a “dry” and a “wet” component at this point. The dry component is the non-processed sound and the wet is processed. Then you’ll send both the wet and the dry to the speakers and adjust the mix between them by adding a gain node on each of these tracks. Gain is an engineery way of saying “volume”. You can keep on like this and add nodes between the source and the speakers as you please. Sometimes you’ll want effects in parallel and sometimes you want them serially. When coding your system its probably a good idea to make it easy to change how this wiring is hooked up for any given node.</p>
<h2>Conclusion</h2>
<p>We’re quite happy with how “Songs of Diridum” turned out, and we’re impressed with the audio and 3D performance available in today’s web browsers. Hopefully the mobile devices will catch up soon performance-wise, and HTML5 will become the most versatile cross-platform game environment available.<br />
Now go <a href="http://www.gooengine.com/mozilla-web-audio-demo-built-in-goo-engine/">play “Songs of Diridum”</a>!</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/Songs-of-Diridum-4.jpg"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/Songs-of-Diridum-4-500.jpg" alt="" /></a></p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/10/songs-of-diridum-pushing-the-web-audio-api-to-its-limits/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Progress report on cross-platform Open Web Apps</title>
		<link>https://hacks.mozilla.org/2013/10/progress-report-on-cross-platform-open-web-apps/</link>
		<comments>https://hacks.mozilla.org/2013/10/progress-report-on-cross-platform-open-web-apps/#comments</comments>
		<pubDate>Mon, 28 Oct 2013 15:34:27 +0000</pubDate>
		<dc:creator><![CDATA[Bill Walker]]></dc:creator>
				<category><![CDATA[Apps]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=22793</guid>
		<description><![CDATA[Here in the Hacks blog we&#8217;ve written a lot about building apps for Firefox OS using HTML, JS, and CSS. We&#8217;re working to ensure that those same apps can also run on Android, Windows, Mac OS X, and Linux devices. If your app can adapt to those screen sizes, CPU&#8217;s, and device capabilities, then we&#8217;ve [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Here in the Hacks blog we&#8217;ve written a lot about <a href="https://hacks.mozilla.org/category/apps/">building apps for Firefox OS using HTML, JS, and CSS</a>. We&#8217;re working to ensure that those same apps can also run on Android, Windows, Mac OS X, and Linux devices. If your app can adapt to those screen sizes, CPU&#8217;s, and device capabilities, then we&#8217;ve got a plan to ensure that your apps install, launch, quit, and uninstall as native apps on each of those platforms.</p>
<p><iframe width="500" height="375" src="//www.youtube.com/embed/4rWYjN-ShOw" frameborder="0" allowfullscreen></iframe></p>
<p>I&#8217;ve created a short video that shows how <a href="https://developer.mozilla.org/en/Apps">Open Web Apps</a> from Firefox OS will work on any platform where Gecko is available.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/1-fxos.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/1-fxos-500.png" alt=""></a></p>
<p>Firefox OS is our benchmark platform for Open Web Apps. On Firefox OS, users can discover apps in the Firefox Marketplace and install them directly onto the phone&#8217;s home screen. As an example I&#8217;m using my app Shotclock, an open web app for computing sun angles for outdoor photographers. Let&#8217;s find out what happens when we install this app on other platforms.</p>
<h2>Android</h2>
<p>Android users discover apps in Firefox Marketplace using the Firefox for Android browser. Firefox Marketplace has approved Shotclock for Android, so we just click the install button as we did on Firefox OS. We will automatically repackage the Open Web App as a native Android app to give our users a native app experience for Open Web Apps.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/2-droid-recent-apps.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/2-droid-recent-apps-500.png" alt=""></a></p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/2-droid-drawer.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/2-droid-drawer-500.png" alt=""></a></p>
<p>Because we installed it from an android APK, we can manage it from the recent app list and we find it in the app drawer like every other app.</p>
<h2>Windows</h2>
<p>Windows users discover apps in the Firefox Marketplace using desktop Firefox. Firefox Marketplace has approved Shotclock for Windows laptops too, so we just click the Marketplace install button. We will automatically repackage the open web app as a native Windows app.</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/3-windows-running.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/3-windows-running-500.png" alt=""></a></p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/3-windows-startmenu.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/3-windows-startmenu-500.png" alt=""></a></p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/3-windows-uninstall.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/3-windows-uninstall-500.png" alt=""></a></p>
<p>Here&#8217;s Shotclock running on Windows, just like a real app. Our repackaging will mean that users can launch their open web apps from the Windows Start menu and quit them from the File menu. Users will also uninstall them from the Programs control panel.</p>
<h2>Mac OS X</h2>
<p>Mac OS X users also discover apps in the Firefox Marketplace using desktop Firefox. We will automatically repackage the open web app as a native Mac OS X app. When the user clicks the install button, we install Shotclock in the Mac OS X Applications folder. </p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/4-mac-controltab.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/4-mac-controltab-500.png" alt=""></a></p>
<p>From there, it launches and runs just like a real app. The native packaging means users can switch between open web apps by pressing Control-Tab, and quit them from the File menu. How much code did the app developer rewrite? Zero. </p>
<h2>Privileged Apps</h2>
<p>So far we&#8217;ve looked at unprivileged Apps. We will also support privileged apps on all these platforms. Here is Kitchen Sink, our app for testing the Firefox OS privileged APIs. What happens when we install it on Android?</p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/5-droid-KS-permissions.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/5-droid-KS-permissions-500.png" alt=""></a></p>
<p>The experience of discovering and installing privileged apps will follow the Android convention of presenting a list of permissions to the user at install time. These permissions are copied from the open web app manifest. After the user completes the installation process, the App is ready to use, and is able to access the phone hardware.</p>
<h2>Linux Desktop</h2>
<p>The email application that comes with Firefox OS is basically a privileged App that uses the Socket API for networking. Marco Castelluccio, our open web apps intern, got it running on it on his Linux laptop. </p>
<p><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/6-mail-linux.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/6-mail-linux-500.png" alt=""></a></p>
<p>He copied over the app package from Gaia and made one tweak to the app manifest. So, if you like the apps that come with your firefox os phone and want to run them on your other devices,  cross-platform open web apps can make that happen.</p>
<h2>iOS</h2>
<p>We’d love to support Open Web Apps on iOS devices, but iOS does not, at this time, include the option to install a Gecko-based web browser, which is currently needed to support Open Web Apps.</p>
<p>Edit: We&#8217;re working with the Cordova community, both to allow Cordova apps to run unmodified on Firefox OS and to allow Open Web Apps packaged by Cordova to run on iOS. For more details see the <a href="https://wiki.mozilla.org/CordovaFirefoxOS">Cordova Firefox OS project page</a> and the <a href="https://github.com/apache/cordova-firefoxos">Cordova Firefox OS GitHub repository</a>. </p>
<h2>Timetable</h2>
<p>Desktop &#8212; You can install hosted, unprivileged apps on your desktops and laptops using Firefox 16 or newer. Privileged app support should land in Firefox Nightly in the next two months.</p>
<p>Android &#8212; You can install apps using Mobile Firefox Aurora, but you won&#8217;t get a native app experience yet. The native app experience should land in Mobile Firefox Nightly in December.</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/10/progress-report-on-cross-platform-open-web-apps/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Building a Firefox OS App for my favorite Internet radio station</title>
		<link>https://hacks.mozilla.org/2013/10/building-a-firefoxos-app-for-my-favorite-internet-radio-station/</link>
		<comments>https://hacks.mozilla.org/2013/10/building-a-firefoxos-app-for-my-favorite-internet-radio-station/#comments</comments>
		<pubDate>Thu, 24 Oct 2013 17:23:08 +0000</pubDate>
		<dc:creator><![CDATA[Aras Balali Moghaddam]]></dc:creator>
				<category><![CDATA[Apps]]></category>
		<category><![CDATA[Audio]]></category>
		<category><![CDATA[Firefox OS]]></category>
		<category><![CDATA[apps]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[firefoxos]]></category>
		<category><![CDATA[gesture]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=22604</guid>
		<description><![CDATA[I recently created a Firefox OS app for my favourite radio station &#8212; radio paradise. It was a lot of fun making this app, so I thought it would be good to share some notes about how I built it. The audio tag It started by implementing the main functionality of the app, playing an [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I recently created <a href="https://marketplace.firefox.com/app/radio-paradise/">a Firefox OS app for my favourite radio station</a> &#8212; radio paradise. It was a lot of fun making this app, so I thought it would be good to share some notes about how I built it.</p>
<h2>The audio tag</h2>
<p>It started by implementing the main functionality of the app, playing an ogg stream I got from the Internet radio station, using the <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio">HTML5 audio</a> element</p>
<p><code></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;audio</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://stream-sd.radioparadise.com/rp_192m.ogg&quot;</span> controls preload<span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/audio<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p></code></p>
<p><audio src="http://stream-sd.radioparadise.com/rp_192m.ogg" controls preload></audio></p>
<p>That was easy! At this point our app is completely functional. If you don’t believe me, checkout <a href="http://jsfiddle.net/arasbm/7sQAr/12/">this jsfiddle</a>. But please continue reading, since there will be a few more sweet features added. In fact, checkout the short video below to see how it will turn out.</p>
<p><iframe width="500" height="375" src="//www.youtube-nocookie.com/embed/-MvVDko8irE" frameborder="0" allowfullscreen></iframe></p>
<p>Because this content belongs to radio paradise, before implementing the app, I contacted them to ask for their permission to make a Firefox OS app for their radio station; they responded:</p>
<blockquote><p>Thanks. We&#8217;d be happy to have you do that. <a href="http://www.radioparadise.com/rp_2p.php?slideshow=no">Our existing web player</a> is html5-based. That might be a place to start. Firefox should have native support for our Ogg Vorbis streams.
</p></blockquote>
<p>I couldn&#8217;t have asked for a more encouraging response, and that was enough to set things in motion.</p>
<h2>Features of the app</h2>
<p>I wanted the app to be very minimal and simple &#8212; both in terms of user experience and the code backing it. Here is a list of the features I decided to include:</p>
<ul>
<li>A single, easy to access, button to play and pause the music</li>
<li>Artist name, song title and album cover for the current song playing should fill up the interface</li>
<li>Setting option to select song quality (for situation when bandwidth is not enough to handle highest quality)</li>
<li>Setting option to start app with music playing or paused</li>
<li>Continue playing even when the app is sent to the background</li>
<li>Keep the screen on when the app is running in the forground</li>
</ul>
<h2></h2>
<p>Instead of using the HTML tag, I decided to create the audio element and configure it in JavaScript. Then I hooked up an event listener for a button to play or stop music.</p>
<p><code></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #000066; font-weight: bold;">var</span> btn <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'play-btn'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">var</span> state <span style="color: #339933;">=</span> <span style="color: #3366CC;">'stop'</span><span style="color: #339933;">;</span>
  btn.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> stop_play<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// create an audio element that can be played in the background</span>
  <span style="color: #000066; font-weight: bold;">var</span> audio <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Audio<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  audio.<span style="color: #660066;">preload</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'auto'</span><span style="color: #339933;">;</span>
  audio.<span style="color: #660066;">mozAudioChannelType</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'content'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">function</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    audio.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    state <span style="color: #339933;">=</span> <span style="color: #3366CC;">'playing'</span><span style="color: #339933;">;</span>
    btn.<span style="color: #660066;">classList</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'stop'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    btn.<span style="color: #660066;">classList</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'playing'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">function</span> stop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    audio.<span style="color: #660066;">pause</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    state <span style="color: #339933;">=</span> <span style="color: #3366CC;">'stop'</span><span style="color: #339933;">;</span>
    btn.<span style="color: #660066;">classList</span>.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'stop'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    btn.<span style="color: #660066;">classList</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'playing'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// toggle between play and stop state</span>
  <span style="color: #000066; font-weight: bold;">function</span> stop_play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#40;</span>state <span style="color: #339933;">==</span> <span style="color: #3366CC;">'stop'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> play<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> stop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p></code></p>
<h2>Accessing current song information</h2>
<p>The first challenge I faced was accessing the current song information. Normally we should not need any special privilege to access third party API’s as long as they provide correct header information. However, the link radio paradise provided me for getting the current song information did not allow for cross origin access. Luckily FirefoxOS has a special power reserved for this kind of situation &#8212; systemXHR comes to the rescue.</p>
<p><code></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> get_current_songinfo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">var</span> cache_killer <span style="color: #339933;">=</span> <span style="">Math</span>.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span><span style="">Math</span>.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">var</span> playlist_url <span style="color: #339933;">=</span>
    <span style="color: #3366CC;">'http://www.radioparadise.com/ajax_rp2_playlist.php?'</span> <span style="color: #339933;">+</span>
    cache_killer<span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">var</span> song_info <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'song-info-holder'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">var</span> crossxhr <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>mozSystem<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  crossxhr.<span style="color: #660066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> infoArray <span style="color: #339933;">=</span> crossxhr.<span style="color: #660066;">responseText</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'|'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    song_info.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> infoArray<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    next_song <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span>get_current_songinfo<span style="color: #339933;">,</span> infoArray<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    update_info<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>
  crossxhr.<span style="color: #660066;">onerror</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Error getting current song info'</span><span style="color: #339933;">,</span> crossxhr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    nex_song <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span>get_current_singinfo<span style="color: #339933;">,</span> <span style="color: #CC0000;">200000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  crossxhr.<span style="color: #660066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GET'</span><span style="color: #339933;">,</span> playlist_url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  crossxhr.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  clearInterval<span style="color: #009900;">&#40;</span>next_song<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p></code></p>
<p>This meant that the app would have to be privileged and thus packaged. I normally would try to keep my apps hosted, because that is very natural for a web app and has several benefits including the added bonus of being accessible to search engines. However, in cases such as this we have no other option but to package the app and give it the special privileges it needs.</p>
<p><code></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;version&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;1.1&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Radio Paradise&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;launch_path&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;/index.html&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;description&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;An unofficial app for radio paradise&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;privileged&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;icons&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;32&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;/img/rp_logo32.png&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;60&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;/img/rp_logo60.png&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;64&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;/img/rp_logo64.png&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;128&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;/img/rp_logo128.png&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;developer&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Aras Balali Moghaddam&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;url&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://arasbm.com&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;permissions&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;systemXHR&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #3366CC;">&quot;description&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Access current song info on radioparadise.com&quot;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;audio-channel-content&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #3366CC;">&quot;description&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Play music when app goes into background&quot;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;installs_allowed_from&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;*&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;default_locale&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;en&quot;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p></code></p>
<h2>Updating song info and album cover</h2>
<p>That XHR call to radio paradise proides me with three important pieces of information:</p>
<ul>
<li>Name of the current song playing and it’s artist</li>
<li>An image tag containing the album cover</li>
<li>Time left to the end of current song in miliseconds</li>
</ul>
<p>Time left to the end of current song is very nice to have. It means that I can execute the XHR call and update the song information only once for every song. I first tried using the setTimeout function like this:</p>
<p><code></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//NOT working example. Can you spot the error?</span>
crossxhr.<span style="color: #660066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">var</span> infoArray <span style="color: #339933;">=</span> crossxhr.<span style="color: #660066;">responseText</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'|'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  song_info.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> infoArray<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  setTimeout<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'get_current_songinfo()'</span><span style="color: #339933;">,</span> infoArray<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  update_info<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></td></tr></table></div>

<p></code></p>
<p>To my surprise, that did not work, and I got a nice error in logcat about a <a href="https://developer.mozilla.org/en-US/Apps/CSP">CSP restriction</a>. It turns out that any attempt at dynamically executing code is banned for security reasons. All we have to do in this scenario to avoid the CSP issue is to pass a callable object, instead of a string.</p>
<p><code></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// instead of passing a string to setTimout we pass</span>
  <span style="color: #006600; font-style: italic;">// a callable object to it</span>
  setTimeout<span style="color: #009900;">&#40;</span>get_current_songinfo<span style="color: #339933;">,</span> infoArray<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></code></p>
<p><strong style="color:red;">Update:</strong> Mindaugas <a href="https://hacks.mozilla.org/2013/10/building-a-firefoxos-app-for-my-favorite-internet-radio-station/comment-page-1/#comment-2153793">pointed out in the comments below</a> that using <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element.innerHTML">innerHTML</a> to parse unknown content in this way, introduces some security risks. Because of these security implications, we should retrieve the remote content as text instead of HTML. One way to do this is to use <em>song_info.textContent</em> which does not interpret the passed content as HTML. Another option, as Frederik Braun <a href="https://hacks.mozilla.org/2013/10/building-a-firefoxos-app-for-my-favorite-internet-radio-station/comment-page-1/#comment-2153896">pointed out</a> is to use a text node which can not render HTML.</p>
<div id="attachment_22731" style="width: 490px" class="wp-caption aligncenter"><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/2013-10-16-19-37-45.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/2013-10-16-19-37-45.png" alt="radio paradise mobile web app running on FirefoxOS" width="480" height="320" class="size-full wp-image-22731" /></a><p class="wp-caption-text">With a bit of CSS magic, things started to fall into place pretty quickly</p></div>
<h2>Adding a unique touch</h2>
<p>One of the great advantages of developing mobile applications for the web is that you are completely free to design your app in any way you want. There is no enforcement of style or restriction on interaction design innovation. Knowing that, it was hard to hold myself back from trying to explore new ideas and have some fun with the app. I decided to hide the settings behind the main content and then add a feature so user can literally cut open the app in the middle to get to setting. That way they are tucked away, but still can be discovered in an intuitive way. For UI elements in the setting page to toggle options I decided to give <a href="http://mozilla.github.io/brick/index.html">Brick</a> a try., with a bit of custom styling added.</p>
<div id="attachment_22732" style="width: 490px" class="wp-caption aligncenter"><a href="https://github.com/arasbm/radio-paradise/blob/master/scripts/helper/setting.js"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/2013-10-16-19-38-22.png" alt="radio paradise app settings" width="480" height="320" class="size-full wp-image-22732" /></a><p class="wp-caption-text">User can slide open the cover image to access app settings behind it</p></div>
<h2>Using the swipe gesture</h2>
<p>As you saw in the video above, to open and close the cover image I use pan and swipe gestures. To implement that, I took <a href="https://github.com/mozilla-b2g/gaia/blob/master/shared/js/gesture_detector.js">gesture detector from Gaia</a>. It was very easy to integrated the gesture code as a module into my app and hook it up to the cover image.</p>
<h2>Organizing the code</h2>
<p>For an app this small, we do not have to use modular code. However, since I have recently started to learn about AMD practices, I decided to use a module system. I asked <a href="https://twitter.com/jrburke">James Burke</a> about implications of using requirejs in an app like this. He suggested I use <a href="https://github.com/requirejs/alameda">Alameda</a> instead, since it is geared toward modern browsers.</p>
<h2>Saving app settings</h2>
<p>I wanted to let users choose stream quality as well as whether they want the app to start playing music as soon as it opens. Both of these options need to be persisted somewhere and retrieved when the app starts. I just needed to save a couple of key/value pairs. I went to <a href="irc://irc.mozilla.org/openwebapps">#openwebapps</a> irc channel and asked for advice. <a href="https://twitter.com/fabricedesre">Fabrice</a> pointed me to a nice piece of code in Gaia (again!) that is used for <a href="https://github.com/mozilla-b2g/gaia/blob/master/shared/js/async_storage.js">asynchronous storing</a> of key/value pairs and even whole objects. That was perfect for my use case, so I took it as well. <a href="https://github.com/mozilla-b2g/gaia">Gaia</a> appears to be a goldmine. Here is the module I created for settings.</p>
<p><code></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">define<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'helper/async_storage'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>asyncStorage<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">var</span> setting <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    values<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
      quality<span style="color: #339933;">:</span> <span style="color: #3366CC;">'high'</span><span style="color: #339933;">,</span>
      play_on_start<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    get_quality<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> setting.<span style="color: #660066;">values</span>.<span style="color: #660066;">quality</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    set_quality<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>q<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      setting.<span style="color: #660066;">values</span>.<span style="color: #660066;">quality</span> <span style="color: #339933;">=</span> q<span style="color: #339933;">;</span>
      setting.<span style="color: #660066;">save</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>
    get_play_on_start<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">return</span> setting.<span style="color: #660066;">values</span>.<span style="color: #660066;">play_on_start</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    set_play_on_start<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>p<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      setting.<span style="color: #660066;">values</span>.<span style="color: #660066;">play_on_start</span> <span style="color: #339933;">=</span> p<span style="color: #339933;">;</span>
      setting.<span style="color: #660066;">save</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>
    save<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      asyncStorage.<span style="color: #660066;">setItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'setting'</span><span style="color: #339933;">,</span> setting.<span style="color: #660066;">values</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    load<span style="color: #339933;">:</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      asyncStorage.<span style="color: #660066;">getItem</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'setting'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>values_obj<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>values_obj<span style="color: #009900;">&#41;</span> setting.<span style="color: #660066;">values</span> <span style="color: #339933;">=</span> values_obj<span style="color: #339933;">;</span>
        callback<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: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">return</span> setting<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></code></p>
<h2>Splitting the cover image</h2>
<p>Now we get to the really fun part that is splitting the cover image in half. To achieve this effect, I made two identical overlapping canvas element both of which are sized to fit the device width. One canvas clips the image and keeps the left portion of it while the other keeps the right side.</p>
<div id="attachment_22613" style="width: 508px" class="wp-caption alignnone"><a href="https://hacks.mozilla.org/wp-content/uploads/2013/10/canvas_clip_500.png"><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/canvas_clip_500.png" alt="Each canvas clips and renders half of the image" width="498" height="157" class="size-full wp-image-22613" /></a><p class="wp-caption-text">Each canvas clips and renders half of the image</p></div>
<p>Here is the code for draw function where most of the action is happening. Note that this function runs only once for each song, or when user changes the orientation of the device from portrait to landscape and vice versa.</p>
<p><code></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">function</span> draw<span style="color: #009900;">&#40;</span>img_src<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  width <span style="color: #339933;">=</span> cover.<span style="color: #660066;">clientWidth</span><span style="color: #339933;">;</span>
  height <span style="color: #339933;">=</span> cover.<span style="color: #660066;">clientHeight</span><span style="color: #339933;">;</span>
  draw_half<span style="color: #009900;">&#40;</span>left_canvas<span style="color: #339933;">,</span> <span style="color: #3366CC;">'left'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  draw_half<span style="color: #009900;">&#40;</span>right_canvas<span style="color: #339933;">,</span> <span style="color: #3366CC;">'right'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">function</span> draw_half<span style="color: #009900;">&#40;</span>canvas<span style="color: #339933;">,</span> side<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    canvas.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'width'</span><span style="color: #339933;">,</span> width<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    canvas.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'height'</span><span style="color: #339933;">,</span> height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> ctx <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> img <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Image<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> clip_img <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> Image<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// opacity 0.01 is used to make any glitch in clip invisible</span>
    ctx.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'rgba(255,255,255,0.01)'</span><span style="color: #339933;">;</span>
&nbsp;
    ctx.<span style="color: #660066;">beginPath</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;">if</span> <span style="color: #009900;">&#40;</span>side <span style="color: #339933;">==</span> <span style="color: #3366CC;">'left'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      ctx.<span style="color: #660066;">moveTo</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: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #006600; font-style: italic;">// add one pixel to ensure there is no gap</span>
      <span style="color: #000066; font-weight: bold;">var</span> center <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>width <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</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>
      ctx.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span>width<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">var</span> center <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>width <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>width <span style="color: #339933;">/</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Draw a wavy pattern down the center</span>
    <span style="color: #000066; font-weight: bold;">var</span> step <span style="color: #339933;">=</span> <span style="color: #CC0000;">40</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">var</span> count <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>height <span style="color: #339933;">/</span> step<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> count<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>center<span style="color: #339933;">,</span> i <span style="color: #339933;">*</span> step<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// alternate curve control point 20 pixels, every other time</span>
      ctx.<span style="color: #660066;">quadraticCurveTo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">%</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> center <span style="color: #339933;">-</span> <span style="color: #CC0000;">20</span> <span style="color: #339933;">:</span>
        center <span style="color: #339933;">+</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> i <span style="color: #339933;">*</span> step <span style="color: #339933;">+</span> step <span style="color: #339933;">*</span> <span style="color: #CC0000;">0.5</span><span style="color: #339933;">,</span> center<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> step<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>center<span style="color: #339933;">,</span> height<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>side <span style="color: #339933;">==</span> <span style="color: #3366CC;">'left'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      ctx.<span style="color: #660066;">lineTo</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: #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>
      ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>width<span style="color: #339933;">,</span> height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span>width<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    ctx.<span style="color: #660066;">closePath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ctx.<span style="color: #660066;">fill</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ctx.<span style="color: #660066;">clip</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    img.<span style="color: #660066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">var</span> h <span style="color: #339933;">=</span> width <span style="color: #339933;">*</span> img.<span style="color: #660066;">height</span> <span style="color: #339933;">/</span> img.<span style="color: #660066;">width</span><span style="color: #339933;">;</span>
      ctx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>img<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> width<span style="color: #339933;">,</span> h<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    img.<span style="color: #660066;">src</span> <span style="color: #339933;">=</span> img_src<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p></code></p>
<h2>Keeping the screen on</h2>
<p>The last feature I needed to add was keeping the screen on when the app is running in foreground and that turned out to be very easy to implement as well. We need to request a <a href="https://developer.mozilla.org/en-US/docs/Web/API/window.navigator.requestWakeLock">screen wake lock</a></p>
<p><code></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #000066; font-weight: bold;">var</span> lock <span style="color: #339933;">=</span> window.<span style="color: #660066;">navigator</span>.<span style="color: #660066;">requestWakeLock</span><span style="color: #009900;">&#40;</span>resourceName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></code></p>
<p>The screen wake lock is actually pretty smart. It will be automatically released when app is sent to the background, and then will given back to your app when it comes to the foreground. Currently in this app I have not provided an option to release the lock. If in future I get requests to add that option, all I have to do is release the lock that has been obtained before setting the option to false</p>
<p><code></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">  lock.<span style="color: #660066;">unlock</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p></code></p>
<h2>Getting the app</h2>
<p>If you have a FirefoxOS device and like great music, you can now install this app on your device. Search for &#8220;radio paradise&#8221; in the marketplace, or install it directly<a href="https://marketplace.firefox.com/app/radio-paradise/"> from this link</a>. You can also checkout the <a href="https://github.com/arasbm/radio-paradise">full source code from github</a>. Feel free to fork and modify the app as you wish, to create your own Internet Radio apps! I would love it if you report <a href="https://github.com/arasbm/radio-paradise/issues?state=open">issues</a>, ask for features or send pull requests.</p>
<h2>Conclusion</h2>
<p>I am more and more impressed by how quickly we can create very functional and unique mobile apps using web technologies. If you have not build a mobile web app for Firefox OS yet, you should definitely give it a try. The future of open web apps is very exciting, and Firefox OS provides a great platform to get a taste of that excitement.</p>
<p>Now it is your turn to leave a comment. What is your favourite feature of this app? What things would you have done differently if you developed this app? How could we make this app better (both code and UX)?</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/10/building-a-firefoxos-app-for-my-favorite-internet-radio-station/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Working with receipts for paid apps</title>
		<link>https://hacks.mozilla.org/2013/10/working-with-receipts-for-paid-apps/</link>
		<comments>https://hacks.mozilla.org/2013/10/working-with-receipts-for-paid-apps/#comments</comments>
		<pubDate>Wed, 23 Oct 2013 14:37:26 +0000</pubDate>
		<dc:creator><![CDATA[Andy McKay]]></dc:creator>
				<category><![CDATA[Firefox OS]]></category>

		<guid isPermaLink="false">https://hacks.mozilla.org/?p=22761</guid>
		<description><![CDATA[You&#8217;ve put your hard work into building a great app. If you want to get paid for your app then the Firefox Marketplace supports app receipts and verification of those receipts. Receipt verification is how we ensure that your app has been paid for in the case of both hosted and packaged apps. It&#8217;s important [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>You&#8217;ve put your hard work into building a great app. If you want to get paid for your app then the <a href="https://marketplace.firefox.com/">Firefox Marketplace</a> supports app receipts and verification of those receipts.</p>
<p>Receipt verification is how we ensure that your app has been paid for in the case of both hosted and packaged apps. It&#8217;s important to point out that we don&#8217;t limit the installation or distribution of apps from the Marketplace. They can be installed by anyone without limitation, they just won&#8217;t have the receipt.</p>
<p>That means as developers, it is essential to check the receipt in your app. This is the only way to ensure that the payment has been processed.</p>
<h2>Receipt format</h2>
<p>Receipts are based on the <a href="https://wiki.mozilla.org/Apps/WebApplicationReceipt">Web Application Receipt specification</a>. A receipt is a JSON object that contains information about the purchase. That JSON is then signed using the <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-key">JSON Web Token</a> specification.</p>
<p>When the App is installed after purchase, the <a href="https://developer.mozilla.org/en-US/docs/Web/Apps/JavaScript_API">receipt is installed</a> along with the app. To check that a receipt is installed you can access the receipt through the <code>mozApps</code> API, for example:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> request <span style="color: #339933;">=</span> window.<span style="color: #660066;">navigator</span>.<span style="color: #660066;">mozApps</span>.<span style="color: #660066;">getSelf</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
request.<span style="color: #660066;">onsuccess</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>let k <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> k <span style="color: #339933;">&lt;</span> request.<span style="color: #660066;">result</span>.<span style="color: #660066;">receipts</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> k<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        let receipt <span style="color: #339933;">=</span> request.<span style="color: #660066;">result</span>.<span style="color: #660066;">receipts</span><span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Got receipt: '</span> <span style="color: #339933;">+</span> receipt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Once you&#8217;ve decoded and expanded a receipt it would look something like the following:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;product&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;url&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://example.com&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;storedata&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;id=111111&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;iss&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;https://marketplace.firefox.com&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;verify&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;https://receiptcheck.marketplace.firefox.com/verify/111111&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// The verify URL</span>
  <span style="color: #3366CC;">&quot;detail&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;https://marketplace.firefox.com/en-US/purchases/111111&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;reissue&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;https://marketplace.firefox.com/en-US/app/example/purchase/reissue&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;user&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;directed-identifier&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;1234-abcd99ef-a123-456b-bbbb-cccc11112222&quot;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;exp&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">1353028900</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;iat&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">1337304100</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;typ&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;purchase-receipt&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;nbf&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">1337304100</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Receipt verifier library</h2>
<p>Just checking for the presence of the receipt is not enough, there are a few checks that should be performed as well:</p>
<ul>
<li>That the receipt is correctly signed and has not be tampered with</li>
<li>That the receipt is from a marketplace you approve of</li>
<li>That the receipt is for your app</li>
</ul>
<p>There are two optional steps you can perform:</p>
<ul>
<li>That the crypto signing on the receipt is correct</li>
<li>That the receipt is still valid with the store</li>
</ul>
<p>These last steps require the app to have internet access to be able to call servers.</p>
<p>An easy way to perform most of these checks is to use the <a href="https://github.com/mozilla/receiptverifier">receipt verifier library</a>. After including it:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> verifier <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">new</span> mozmarket.<span style="color: #660066;">receipts</span>.<span style="color: #660066;">Verifier</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// checks that the receipt is for your app.</span>
  productURL<span style="color: #339933;">:</span> <span style="color: #3366CC;">'http://example.com'</span><span style="color: #339933;">,</span>
  <span style="color: #006600; font-style: italic;">// only allow apps that came from the firefox marketplace.</span>
  installs_allowed_from<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'https://marketplace.firefox.com'</span><span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
verifier.<span style="color: #660066;">verify</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>verifier<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>verifier.<span style="color: #660066;">state</span> <span style="color: #000066; font-weight: bold;">instanceof</span> verifier.<span style="color: #660066;">states</span>.<span style="color: #660066;">OK</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// everything is good.</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'good'</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: #006600; font-style: italic;">// something went wrong.</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'wrong'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>See the docs for a full <a href="https://github.com/mozilla/receiptverifier#options">list of the options</a>.</p>
<p>The receipt verifier returns a <a href="https://github.com/mozilla/receiptverifier#states-and-errors">state object</a> that tells you the exact error. We don&#8217;t prescribe what the app should do under those circumstances, that is left completely to the developer. For example a <code>NetworkError</code> indicates that we couldn&#8217;t connect to the verification server. That may, or may not be, a fatal condition for your app.</p>
<p>The receipt verifier library also includes a basic user interface for showing errors to the user. It is great for testing, but since the user interface in your app is going to be different, the chances are you&#8217;ll want to display messages back to the user in your own style. If you include <code>receiptverifier-ui.js</code>, then you can use the prompter like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">mozmarket.<span style="color: #660066;">receipts</span>.<span style="color: #660066;">Prompter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  storeURL<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;https://marketplace.mozilla.org/app/myapp&quot;</span><span style="color: #339933;">,</span>
  supportHTML<span style="color: #339933;">:</span> <span style="color: #3366CC;">'&amp;lt;a href=&quot;mailto:me@example.com&quot;&amp;gt;email me@example.com&amp;lt;/a&amp;gt;'</span><span style="color: #339933;">,</span>
  verify<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If you ran the app without the receipt installed you would see a message like this:</p>
<p><img src="https://hacks.mozilla.org/wp-content/uploads/2013/10/app-no-receipt-installed.png" alt="" /></p>
<p>One last thing that the receipt verifier library won&#8217;t do is verify that a receipt has not been shared between users. That is left as something the app developer will need to implement: an example might be using the <a href="https://github.com/andymckay/django-receipts">django-receipts library</a>.</p>
<h2>Getting help</h2>
<p>If you need more help then there&#8217;s a <a href="https://lists.mozilla.org/listinfo/dev-marketplace">mailing list</a> or a helpful IRC channel at <a href="https://wiki.mozilla.org/IRC">#marketplace</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://hacks.mozilla.org/2013/10/working-with-receipts-for-paid-apps/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>