<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Well Caffeinated</title>
 <link href="http://wellcaffeinated.net/atom.xml" rel="self"/>
 <link href="http://wellcaffeinated.net"/>
 <updated>2016-06-30T15:28:49+00:00</updated>
 <id>http://wellcaffeinated.net</id>
 <author>
   <name>Jasper Palfree</name>
   <email>jasper@wellcaffeinated.net</email>
 </author>

 
 <entry>
   <title>Sending Cross-Domain Messages to an iFrame</title>
   <link href="http://wellcaffeinated.net/articles/2014/12/09/sending-cross-domain-messages-to-an-iframe"/>
   <updated>2014-12-09T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2014/12/09/sending-cross-domain-messages-to-an-iframe</id>
   <content type="html">&lt;p&gt;While browsing &lt;a href=&quot;http://stackoverflow.com/questions/3177261/set-a-variable-in-an-iframe&quot;&gt;stackoverflow&lt;/a&gt; recently, I discovered a trick to send arbitrary data to an iframe, even if it’s across domains!&lt;/p&gt;

&lt;p&gt;If your iframe is on the same domain, you can use the regular trick:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;var iframeWin = iframe.contentWindow || iframe.contentDocument.defaultView;
iframeWin.data = myData;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;But if you want to send data to an iframe across domains, you need to be more clever. The trick is to use &lt;code class=&quot;highlighter-rouge&quot;&gt;window.location.hash&lt;/code&gt; and the &lt;code class=&quot;highlighter-rouge&quot;&gt;onhashchange&lt;/code&gt; event, then set the location of the iframe to have a different hash value (that includes your encoded data).&lt;/p&gt;

&lt;p&gt;Let’s write some code that will encode arbitrary data (as long as it’s JSON encodable) and put it into the hash value of the desired iframe.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function sendIFrameData( frame, data ){
    var idx = frame.src.indexOf(&#39;#&#39;), url = frame.src;
    if ( idx &amp;gt; -1 ){
        url = url.substr(0, idx);
    }
    
    frame.src = url + &#39;#&#39; + window.btoa(JSON.stringify(data));
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Easy enough…&lt;/p&gt;

&lt;p&gt;Now on the iframe side, let’s write a function to decode the data.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function getHashData(){
    var data;
    try {
        data = JSON.parse(window.atob(window.location.hash.substr(1)));
    } catch( e ){}
    return data;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;That’s all there is to it!&lt;/p&gt;

&lt;h2 id=&quot;an-example&quot;&gt;An example&lt;/h2&gt;

&lt;p&gt;Here’s a real life example. I created a codepen that will load an iframe on my domain, and send it some messages.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// send arbitrary data to the iframe
sendIFrameData( frame, { text: &quot;Hello world&quot; } );

setTimeout(function(){
    sendIFrameData( frame, { text: &quot;How are you today?&quot; } );
}, 2000);
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The iframe will listen to the hashchange event and log out what it hears.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;var el = document.getElementById(&#39;log&#39;);
function showMsg(){
    var data = getHashData();
    if ( data &amp;amp;&amp;amp; data.text ){
        el.innerHTML += &quot;&amp;lt;br/&amp;gt;Received msg: &quot; + data.text;
    }
}

// in case we sent data on first load.
showMsg();
// when the hash changes we check the new data and print it out
window.addEventListener(&#39;hashchange&#39;, function(){
    showMsg();
});
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Check out the working example!&lt;/p&gt;

&lt;p data-height=&quot;266&quot; data-theme-id=&quot;2533&quot; data-slug-hash=&quot;myVaQX&quot; data-default-tab=&quot;result&quot; data-user=&quot;wellcaffeinated&quot; class=&quot;codepen&quot;&gt;See the Pen &lt;a href=&quot;http://codepen.io/wellcaffeinated/pen/myVaQX/&quot;&gt;myVaQX&lt;/a&gt; by Well Caffeinated (&lt;a href=&quot;http://codepen.io/wellcaffeinated&quot;&gt;@wellcaffeinated&lt;/a&gt;) on &lt;a href=&quot;http://codepen.io&quot;&gt;CodePen&lt;/a&gt;.&lt;/p&gt;
&lt;script async=&quot;&quot; src=&quot;//assets.codepen.io/assets/embed/ei.js&quot;&gt;&lt;/script&gt;

</content>
 </entry>
 
 <entry>
   <title>PhysicsJS Contest | Building Demos and Better Renderers</title>
   <link href="http://wellcaffeinated.net/articles/2013/12/05/physicsjs-contest-building-demos-and-better-renderers"/>
   <updated>2013-12-05T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2013/12/05/physicsjs-contest-building-demos-and-better-renderers</id>
   <content type="html">&lt;p&gt;I’d just like to announce that &lt;a href=&quot;http://physicsjs.challengepost.com/?utm_source=partner&amp;amp;utm_medium=banner&amp;amp;utm_campaign=physicsjs&quot;&gt;ChallengePost is hosting a PhysicsJS contest&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://physicsjs.challengepost.com/?utm_source=partner&amp;amp;utm_medium=banner&amp;amp;utm_campaign=physicsjs&quot;&gt;&lt;img src=&quot;http://wellcaffeinated.net/images/physics-js-promo-widget.gif&quot; alt=&quot;Register for the PhysicsJS Challenge&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’ve also &lt;a href=&quot;http://wellcaffeinated.net/PhysicsJS/tutorials/creating-a-custom-renderer&quot;&gt;created a tutorial&lt;/a&gt; to help people understand how to create
custom renderers with PhysicsJS. Better renderers are sorely needed to make PhysicsJS
even easier to use. I’m hoping some clever devs out there will be able to build
PhysicsJS renderers on top of great canvas libraries like
&lt;a href=&quot;http://kineticjs.com/&quot;&gt;KineticJS&lt;/a&gt;, &lt;a href=&quot;http://jonobr1.github.io/two.js/&quot;&gt;TWO.js&lt;/a&gt;, &lt;a href=&quot;https://github.com/GoodBoyDigital/pixi.js/&quot;&gt;Pixi.js&lt;/a&gt;, &lt;a href=&quot;http://paperjs.org/&quot;&gt;Paper.js&lt;/a&gt;, or &lt;a href=&quot;http://www.createjs.com/#!/EaselJS&quot;&gt;Easel.js&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I raise my coffee cup to you, Internet! Don’t let me down!&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Building a 2D Asteroids Game with PhysicsJS</title>
   <link href="http://wellcaffeinated.net/articles/2013/12/02/building-a-2d-browser-game-with-physicsjs"/>
   <updated>2013-12-02T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2013/12/02/building-a-2d-browser-game-with-physicsjs</id>
   <content type="html">&lt;p&gt;You might have heard a little while ago that I created a javascript physics engine called
&lt;a href=&quot;http://wellcaffeinated.net/PhysicsJS&quot;&gt;PhysicsJS&lt;/a&gt;. Turns out it’s been getting a lot of
attention, but I’m behind on the documentation. I’m working on it though! I recently
published a tutorial as a guest blog on &lt;strong&gt;flippin’ awesome&lt;/strong&gt;;
&lt;a href=&quot;http://flippinawesome.org/2013/12/02/building-a-2d-browser-game-with-physicsjs/&quot;&gt;Building a 2D Asteroids Game with PhysicsJS&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;As a web developer and a physics geek, I always felt like something was lacking whenever 
I tried to do 2D physics in JavaScript. I wanted an extensible framework that followed 
in the footsteps of other modern JavaScript APIs. I didn’t want it to assume that I wanted 
gravity to point downwards, or that I wanted gravity at all… That’s why I was driven to 
create PhysicsJS, which I hope will live up to its tagline:&lt;/p&gt;

  &lt;blockquote&gt;
    &lt;p&gt;A modular, extendable, and easy-to-use physics engine for JavaScript.&lt;/p&gt;
  &lt;/blockquote&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can &lt;a href=&quot;http://flippinawesome.org/2013/12/02/building-a-2d-browser-game-with-physicsjs/&quot;&gt;read the full article here&lt;/a&gt;.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>A Web Developer's Tribute to autumn</title>
   <link href="http://wellcaffeinated.net/articles/2013/09/22/a-web-developers-tribute-to-autumn"/>
   <updated>2013-09-22T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2013/09/22/a-web-developers-tribute-to-autumn</id>
   <content type="html">&lt;p&gt;I created a little tribute to autumn using &lt;a href=&quot;http://wellcaffeinated.net/PhysicsJS&quot;&gt;PhysicsJS&lt;/a&gt;. Seems like people like it… enjoy :)&lt;/p&gt;

&lt;p data-height=&quot;485&quot; data-theme-id=&quot;963&quot; data-slug-hash=&quot;fApHw&quot; data-user=&quot;wellcaffeinated&quot; data-default-tab=&quot;result&quot; class=&quot;codepen&quot;&gt;See the Pen &lt;a href=&quot;http://codepen.io/wellcaffeinated/pen/fApHw&quot;&gt;Autumn Leaves | PhysicsJS Demo&lt;/a&gt; by Well Caffeinated (&lt;a href=&quot;http://codepen.io/wellcaffeinated&quot;&gt;@wellcaffeinated&lt;/a&gt;) on &lt;a href=&quot;http://codepen.io&quot;&gt;CodePen&lt;/a&gt;&lt;/p&gt;
&lt;script async=&quot;async&quot; src=&quot;http://codepen.io/assets/embed/ei.js&quot;&gt;
&lt;/script&gt;

</content>
 </entry>
 
 <entry>
   <title>PhysicsJS: I wrote a physics engine for javascript</title>
   <link href="http://wellcaffeinated.net/articles/2013/09/10/physicsjs-i-wrote-a-physics-engine-for-javascript"/>
   <updated>2013-09-10T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2013/09/10/physicsjs-i-wrote-a-physics-engine-for-javascript</id>
   <content type="html">&lt;p&gt;I’ve finally &lt;a href=&quot;http://wellcaffeinated.net/PhysicsJS/&quot;&gt;released a little project&lt;/a&gt; I’ve been working on lately: &lt;a href=&quot;http://wellcaffeinated.net/PhysicsJS/&quot;&gt;PhysicsJS&lt;/a&gt;. It’s a physics engine written in javascript. For those who haven’t used or heard of a physics engine before, a physics engine is a library that helps simulate physical realism in a computer program. For example, here’s a little jsFiddle of a classic tearable cloth simulation I made with PhysicsJS. You can use your mouse to move and even tear the cloth.&lt;/p&gt;

&lt;iframe width=&quot;100%&quot; height=&quot;300&quot; src=&quot;http://jsfiddle.net/wellcaffeinated/MEgG3/embedded/result,js,html,css/&quot; allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot;&gt;
&lt;/iframe&gt;

&lt;p&gt;It’s not quite the same as the type of physics simulation a physicist would use, because generally a physics engine will do computations in real time, so the algorithms need to be fast. The algorithms in (real-time) physics engines will cut some corners to acheive an effect that is physically plausible to the eye, even if it’s not what would &lt;em&gt;really&lt;/em&gt; happen. Usually they are used in game development but there are, of course, other applications also.&lt;/p&gt;

&lt;p&gt;There are actually a few javascript physics engines that have been released recently. The two that I’ve most recently come across are &lt;a href=&quot;http://subprotocol.com/verlet-js/&quot;&gt;VerletJS&lt;/a&gt; and &lt;a href=&quot;http://soulwire.co.uk/experiments/coffee-physics/&quot;&gt;coffeephysics&lt;/a&gt;. While both are really cool, and have great example simulations, neither have good documentation, and both are very specialized to particular types of effect. Of course, there’s always the classic &lt;a href=&quot;https://github.com/kripken/box2d.js/&quot;&gt;Box2DJS&lt;/a&gt;, which was ported over from a flash implementation… and it shows in the API. While I do love what Box2DJS can do, it’s also a monster of a library, weighing 724 KB &lt;strong&gt;minified&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;After playing around with physics engines and creating modest simulations of my own, I decided to see if I could pool all of my work into a little library, that hopefully could evolve into something people would find useful. The result is &lt;a href=&quot;http://wellcaffeinated.net/PhysicsJS/&quot;&gt;PhysicsJS&lt;/a&gt;. It’s generally well documented and the code is written as cleanly as I could. It’s also &lt;strong&gt;modular&lt;/strong&gt;, so the bare-bones functionality is only 32 KB (minified) and the (current) full functionality is 52 KB (minified), but it was written with AMD in mind, so you can &lt;em&gt;include exactly what you need to get the job done&lt;/em&gt; with requreJS. It’s also &lt;strong&gt;extendable&lt;/strong&gt;; I wanted to make sure that it would be easy to mixin new functionality of any kind, and replace what isn’t suitable. I’m hoping people will start writing plugins to extend the capabilities, which is an exciting prospect! It’s also fun to use. Only time will tell whether or not it’s an intuitive API… but I’d like to think it is.&lt;/p&gt;

&lt;p&gt;Anyways, &lt;a href=&quot;http://wellcaffeinated.net/PhysicsJS/&quot;&gt;go check it out and play around&lt;/a&gt;. There are some cool demos of the functionality. I’m really interested to see what people think of it so send me your feedback. If you’re interested in contributing, I’d love people to make some plugins to extend the functionality, and also fix some of the quirks in its algorithms.&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Playing with asm.js and Browser Physics</title>
   <link href="http://wellcaffeinated.net/articles/2013/04/16/playing-with-asm-js-and-browser-physics"/>
   <updated>2013-04-16T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2013/04/16/playing-with-asm-js-and-browser-physics</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;/demos/asm-js-physics.html&quot;&gt;&lt;img style=&quot;float: right&quot; src=&quot;http://content.screencast.com/users/jpalfr/folders/Jing/media/8d32d26c-7384-440c-b99b-c86c7e77952f/00000016.png&quot; alt=&quot;Asm.js Physics Demo&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lately I’ve been &lt;strong&gt;very&lt;/strong&gt; intrigued about Mozilla’s &lt;a href=&quot;http://asmjs.org/&quot; title=&quot;asm.js spec homepage&quot;&gt;asm.js specification&lt;/a&gt;.
&lt;strong&gt;ASM.js is a subset of javascript&lt;/strong&gt; that allows javascript interpreters to compile
code ahead of time, directly into assembly code. This results in huge speed increases.&lt;/p&gt;

&lt;p&gt;Current benchmarks of asm.js code running in Firefox’s Nightly build, place
javascript at only half the speed of native compiled code! A big difference
from the usual, which is around 4 to 10 times slower. One fun showcase
of this was the recently ported Unreal Engine 3 to Asm.js.&lt;/p&gt;

&lt;p&gt;Yes. We’re running high performance 3D games in web browsers now…&lt;/p&gt;

&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;http://www.youtube.com/embed/XsyogXtyU9o&quot; frameborder=&quot;0&quot; allowfullscreen=&quot;allowfullscreen&quot;&gt;
&lt;/iframe&gt;

&lt;p&gt;&lt;a href=&quot;http://ejohn.org/blog/asmjs-javascript-compile-target/&quot; title=&quot;Asm.js: The JavaScript Compile Target&quot;&gt;John Resig has a nice writeup about asm.js&lt;/a&gt;, including a QA transcript 
with David Herman (Senior Researcher at Mozilla Research). You should give it
a read if you’re interested.&lt;/p&gt;

&lt;p&gt;Of course, being curious and well caffeinated, I like to play around. I did a simple
benchmark and a little proof of concept of my own. But first, let’s quickly
go over asm.js.&lt;/p&gt;

&lt;h2 id=&quot;asmjs-modules-in-a-nutshell&quot;&gt;asm.js Modules in a nutshell&lt;/h2&gt;

&lt;p&gt;Firstly, a disclaimer. asm.js code is a pain to write by hand. (I do so because I
drink too much coffee…) I don’t really recommend starting any large project thinking that you’ll
write it in asm.js from scratch. Likely, the best way to leverage asm.js will be to write in
another language (like C++, or something like &lt;a href=&quot;http://jlongster.com/Compiling-LLJS-to-asm.js,-Now-Available-&quot; title=&quot;Compiling LLJS to asm.js, Now Available!&quot;&gt;LLJS&lt;/a&gt;), and then
“compile” it into asm.js. The painful part is mainly that it feels like
writing in two languages at once. It’s javascript… but it feels like something
else… anyways, let’s move on.&lt;/p&gt;

&lt;p&gt;Asm.js contexts begin with a declaration like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&quot;use asm&quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This is in the spirit of the &lt;code class=&quot;highlighter-rouge&quot;&gt;&quot;use strict&quot;;&lt;/code&gt; declaration, that you are hopefully
familiar with. (If not, google it. It’s good to know).&lt;/p&gt;

&lt;p&gt;This will most often be placed inside a function that acts as a “factory”
for an asm.js module. This asm.js module format looks like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function my_module( stdlib, foreign, heap ){
    &quot;use asm&quot;;

    // magic... (not really)

    return {
        someMethod: someMethod
    };
}

var instance = my_module(
        window,
        {
            customProperty: value,
            externalFunction: function(){
                // ...
            }
        },
        new ArrayBuffer( size )
    );
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;When this function is run, it will return an instance of an asm.js module.
If asm.js is supported, the methods in the returned object will be compiled
into native code. If not, they will be javascript with identical functionality.&lt;/p&gt;

&lt;p&gt;Inside the module factory, &lt;code class=&quot;highlighter-rouge&quot;&gt;stdlib&lt;/code&gt; will hold standard global functionality
(like Math), &lt;code class=&quot;highlighter-rouge&quot;&gt;foreign&lt;/code&gt; is user specified and can be used to escape back into
regular javascript, and &lt;code class=&quot;highlighter-rouge&quot;&gt;heap&lt;/code&gt; is a fixed-size ArrayBuffer for storage.&lt;/p&gt;

&lt;p&gt;The magic that happens in between relies on a very restrictive requirement
that all primitives must be &lt;strong&gt;statically typed&lt;/strong&gt; as integers, or floats. 
Numerical computation is pretty much all you can do in an asm.js context
(and that’s why it’s so fast). Of course, javascript doesn’t allow for declaring
types, so this is done by transforming variables into their types like so:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;variable_int = variable_int|0;
variable_float = +variabla_float;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I’m not going to go into great detail about the specifics of hand crafting
asm.js code. For that you should check out &lt;a href=&quot;http://ejohn.org/blog/asmjs-javascript-compile-target/&quot; title=&quot;Asm.js: The JavaScript Compile Target&quot;&gt;John Resig’s article&lt;/a&gt; and
&lt;a href=&quot;http://asmjs.org/&quot; title=&quot;asm.js spec homepage&quot;&gt;read the specification&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;However, one of the big pains to writing it by hand is dealing with memory allocation.
The only thing you really have access to in terms of memory, is the fixed-size
&lt;a href=&quot;https://developer.mozilla.org/en-US/docs/JavaScript/Typed_arrays/ArrayBuffer&quot;&gt;ArrayBuffer&lt;/a&gt; you specify when you instantiate the asm.js module. You then
have to then create &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/JavaScript/Typed_arrays/ArrayBufferView?redirectlocale=en-US&amp;amp;redirectslug=JavaScript_typed_arrays%2FArrayBufferView&quot;&gt;views into the array buffer&lt;/a&gt; to manipulate data.&lt;/p&gt;

&lt;h2 id=&quot;easier-asmjs-memory-management&quot;&gt;Easier asm.js Memory Management&lt;/h2&gt;

&lt;p&gt;Memory management becomes cumbersome. After playing around with it, I decided I needed to write
a quick &lt;a href=&quot;https://gist.github.com/wellcaffeinated/5399067&quot;&gt;helper module&lt;/a&gt; to make 
this easier.&lt;/p&gt;

&lt;p&gt;Basically, the helper lets you manipulate “collections” of objects that store
all of their (primitive) properties in an array buffer. For example:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;var coln = ASMHelpers.Collection({
        foo: &#39;int16&#39;
    });
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This would create a collection instance that holds objects that hold
16 bit integers in their &lt;code class=&quot;highlighter-rouge&quot;&gt;foo&lt;/code&gt; properties. You can then add one of these
objects to the collection and all of the ArrayBuffer memory management is
taken care of behind the scenes:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;coln.add({
    foo: 42
});

var obj = coln.at( 0 );
obj.foo; // =&amp;gt; 42
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The objects have &lt;em&gt;getters&lt;/em&gt; and &lt;em&gt;setters&lt;/em&gt; defined, so if you change the
property, it will get changed in the array buffer.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;obj.foo = 34; // changed in array buffer
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Unfortunately, non of those tricks can be used in the context of an asm.js
module. But that’s ok, because the helper collection sets up
pointer addresses that &lt;em&gt;can&lt;/em&gt; be used in asm.js. Here’s how:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// include asm.js module methods into the collection
coln.include(function(stdlib, coln, heap){
    &quot;use asm&quot;;

    // general purpose std functions
    var sqrt = stdlib.Math.sqrt;
    // set up our view to look into the heap
    // this will be used to access our int16 properties
    // in our example, this would be the &quot;foo&quot; property
    var int16 = new stdlib.Int16Array( heap );

    // object property pointers, relative to object ptr
    var $foo = coln.$foo|0;
    
    // function to get the number of objects in the collection
    var getLen = coln.getLen;
    // the size of each object in bytes
    var size = coln.objSize|0;
    // starting point for iteration (ie: the address of first object)
    var iterator = coln.ptr|0;

    // example function that increments the foo property 
    // of every object by specified number
    //
    function incrementFoo( n ){
        // declare n as an integer
        n = n|0;

        // declare local variables
        var i = 0, l = 0, ptr = 0;
        ptr = iterator|0;
        l = getLen()|0;

        // loop through objects
        while ((i|0) &amp;lt; (l|0)){

            // foo += n;
            int16[(ptr + $foo) &amp;gt;&amp;gt; 1] = ( (n|0) + (int16[(ptr + $foo) &amp;gt;&amp;gt; 1]|0) )|0;
            // i++;
            i = ((i|0) + 1)|0;
            // ptr += size;
            ptr = ((ptr|0) + (size|0))|0;
        }
    }

    // these functions will get mixed into the collection
    return {

        incrementFoo: incrementFoo
    }
});
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;benchmarking-with-jsperf&quot;&gt;Benchmarking with JSPerf&lt;/h2&gt;

&lt;p&gt;Using this helper, I wrote a very simple 1D physics engine
to test performance difference between simulating numbers of 
objects with the “use asm” declaration, and without it.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://jsperf.com/asm-js-benchmark-with-simple-physics&quot;&gt;Here’s the benchmark on JSPerf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The results in Firefox 23 nightly build:&lt;/p&gt;

&lt;iframe width=&quot;500&quot; height=&quot;300&quot; frameborder=&quot;0&quot; src=&quot;https://docs.google.com/spreadsheet/pub?key=0Akupmpq7rS__dDhCdEhGM19HdVpJV0hPbnVxeDlxYnc&amp;amp;single=true&amp;amp;gid=0&amp;amp;output=html&amp;amp;widget=true&quot;&gt;
&lt;/iframe&gt;

&lt;p&gt;Aside for some weirdness on the last test, &lt;strong&gt;enabling asm.js is significantly faster&lt;/strong&gt; for
this physics algorithm.&lt;/p&gt;

&lt;h2 id=&quot;building-a-simple-physics-engine-in-asmjs&quot;&gt;Building a simple physics engine in asm.js&lt;/h2&gt;

&lt;p&gt;Of course, benchmarks are nice… but graphics are more fun! I wanted to keep going and
see what this speed increase felt like for some real-time browser simulation.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/demos/asm-js-physics.html&quot;&gt;&lt;img src=&quot;http://content.screencast.com/users/jpalfr/folders/Jing/media/8d32d26c-7384-440c-b99b-c86c7e77952f/00000016.png&quot; alt=&quot;asm.js Physics Demo&quot; /&gt;&lt;/a&gt;
&lt;a href=&quot;/demos/asm-js-physics.html&quot;&gt;To that end, I created this fun demo&lt;/a&gt;.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Creating a 3D Histogram with CSS 3D Transforms</title>
   <link href="http://wellcaffeinated.net/articles/2013/01/12/creating-a-3d-histogram-with-css-3d-transforms"/>
   <updated>2013-01-12T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2013/01/12/creating-a-3d-histogram-with-css-3d-transforms</id>
   <content type="html">&lt;p&gt;Using CSS 3D transforms can be a bit challenging. It’s new, support is sketchy, and it requires a type of spatial thinking that we aren’t used to on the web (yet). But the results are worth it. With a bit of CSS and some javascript for event binding, you can turn a boring old table into a 3D histogram like this one:&lt;/p&gt;

&lt;style&gt;
.noselect *,
.viewport * {
    -webkit-user-select: none;  
       -moz-user-select: none;    
        -ms-user-select: none;      
            user-select: none;
}

.viewport {
    position: relative;
    width: 100%;
    padding-bottom: 100%;
    cursor: move;

    -webkit-perspective: 4000px;
       -moz-perspective: 4000px;
        -ms-perspective: 4000px;
            perspective: 4000px;
    
    -webkit-perspective-origin: 50% -100%;
       -moz-perspective-origin: 50% -100%;
        -ms-perspective-origin: 50% -100%;
            perspective-origin: 50% -100%;
}

.viewport .world {
    position: absolute;
    top:0;
    left:0;
    right:0;
    bottom:0;

    -webkit-transform: rotateX(-15deg) rotateY(-20deg);
       -moz-transform: rotateX(-15deg) rotateY(-20deg);
        -ms-transform: rotateX(-15deg) rotateY(-20deg);
            transform: rotateX(-15deg) rotateY(-20deg);
}

.viewport .world, 
.viewport .world * { /* firefox needs this * selector */
    -webkit-transform-style: preserve-3d;
       -moz-transform-style: preserve-3d;
        -ms-transform-style: preserve-3d;
            transform-style: preserve-3d;
}

.viewport .ground {
    position: absolute;
    z-index: 1;
    top: 50%;
    left: 50%;
    width: 90%;
    height: 90%;
    margin-left: -50%;
    margin-top: -50%;
    background: #eee;
    
    -webkit-transform: rotateX(90deg);
       -moz-transform: rotateX(90deg);
        -ms-transform: rotateX(90deg);
            transform: rotateX(90deg);
}

.viewport .histogram-3d {
    width: 80%;
    height: 80%;
    margin: 10% auto;
    border-collapse: collapse;
    border-style: solid;

    -webkit-transform: translateZ(1px);
       -moz-transform: translateZ(1px);
        -ms-transform: translateZ(1px);
            transform: translateZ(1px);
}

.viewport .histogram-3d td {
    position: relative;
    width: 30%;
    height: 30%;
    padding: 10px;
    border: 2px solid #555;
    z-index: 0;
}

.viewport .bar {
    position: relative;
    width: 100%;
    height: 100%;
    z-index: 1;
}

.viewport .bar .face {
    background: hsl(0, 100%, 50%);
    position: absolute;
    width: 100%;

    overflow: hidden;
    z-index: 1;
}

.viewport .bar .face.front {
    background: hsl(0, 100%, 20%);
    bottom: 0;
    height: 1em;

    -webkit-transform-origin: bottom center;
       -moz-transform-origin: bottom center;
        -ms-transform-origin: bottom center;
            transform-origin: bottom center;
    
    -webkit-transform: rotateX(-90deg);
       -moz-transform: rotateX(-90deg);
        -ms-transform: rotateX(-90deg);
            transform: rotateX(-90deg);
}

.viewport .bar .face.right {
    top: 0;
    right: 0;
    width: 1em;
    height: 100%;

    -webkit-transform-origin: center right;
       -moz-transform-origin: center right;
        -ms-transform-origin: center right;
            transform-origin: center right;
    
    -webkit-transform: rotateY(90deg);
       -moz-transform: rotateY(90deg);
        -ms-transform: rotateY(90deg);
            transform: rotateY(90deg);
}

.viewport .bar .face.left {
    background: hsl(0, 100%, 45%);
    top: 0;
    left: 0;
    width: 1em;
    height: 100%;

    -webkit-transform-origin: center left;
       -moz-transform-origin: center left;
        -ms-transform-origin: center left;
            transform-origin: center left;
    
    -webkit-transform: rotateY(-90deg);
       -moz-transform: rotateY(-90deg);
        -ms-transform: rotateY(-90deg);
            transform: rotateY(-90deg);
}

.viewport .bar .face.back {
    top: 0;
    height: 1em;

    -webkit-transform-origin: top center;
       -moz-transform-origin: top center;
        -ms-transform-origin: top center;
            transform-origin: top center;
    
    -webkit-transform: rotateX(90deg);
       -moz-transform: rotateX(90deg);
        -ms-transform: rotateX(90deg);
            transform: rotateX(90deg);
}

.viewport .bar .face.top {
    background: hsl(0, 100%, 40%);
    height: 100%;
    width: 100%;
    top: 0;

    -webkit-transform: translateZ(1em);
       -moz-transform: translateZ(1em);
        -ms-transform: translateZ(1em);
            transform: translateZ(1em);
}

/* fixes for firefox 
**********************/
.viewport .histogram-3d * {
    /* this improves jagged edges in firefox */
    outline: 1px solid transparent;
}

/* don&#39;t use table displays... need to use a combination of block and inline-block. Eww. */
.no-3dtablelayout .viewport .histogram-3d,
.no-3dtablelayout .viewport .histogram-3d tbody,
.no-3dtablelayout .viewport .histogram-3d tr,
.no-3dtablelayout .viewport .histogram-3d td { 
    display: block; 
    -webkit-box-sizing: border-box;
       -moz-box-sizing: border-box;
        -ms-box-sizing: border-box;
            box-sizing: border-box;
}

.no-3dtablelayout .viewport .histogram-3d tbody { height: 100%; }
.no-3dtablelayout .viewport .histogram-3d tr {
    text-align: center;
    height: 33%;
    /* fix grid */
    letter-spacing: -0.5em;
    margin-top: -1px;
}
.no-3dtablelayout .viewport .histogram-3d td {
    display: inline-block;
    height: 100%;
}

.no-csstransforms3d .viewport {
    display: none;
}

.csstransforms3d #histogram-3d-fallback {
    display: none;
}
&lt;/style&gt;

&lt;div class=&quot;viewport&quot;&gt;
    &lt;div class=&quot;world&quot;&gt;
        &lt;div class=&quot;ground&quot;&gt;
            &lt;table class=&quot;histogram-3d&quot;&gt;
                &lt;tr&gt;
                    &lt;td&gt;30&lt;/td&gt;
                    &lt;td&gt;20&lt;/td&gt;
                    &lt;td&gt;60&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                    &lt;td&gt;80&lt;/td&gt;
                    &lt;td&gt;10&lt;/td&gt;
                    &lt;td&gt;40&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                    &lt;td&gt;20&lt;/td&gt;
                    &lt;td&gt;50&lt;/td&gt;
                    &lt;td&gt;30&lt;/td&gt;
                &lt;/tr&gt;
            &lt;/table&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;img id=&quot;histogram-3d-fallback&quot; src=&quot;http://content.screencast.com/users/jpalfr/folders/Jing/media/c3dbb76a-0788-4f0d-b3e6-71b6a2ed46f6/00000001.png&quot; /&gt;&lt;/p&gt;

&lt;script id=&quot;bartpl&quot; type=&quot;text/template&quot;&gt;
  &lt;div class=&quot;bar&quot;&gt;
    &lt;div class=&quot;face top&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;face front&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;face back&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;face left&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;face right&quot;&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/script&gt;

&lt;script&gt;
require([&#39;boot-index&#39;],function(){
require([
    &#39;jquery&#39;
], function(
    $
){
    var dragStart = {}
        ,dragging = false
        ,curpos = {x:100,y:-75}
        ;

    if ($.browser.mozilla){
        $(&#39;body&#39;).addClass(&#39;no-3dtablelayout&#39;);
    }

    $(function(){
    
        // get the template
        var tpl = $(&#39;#bartpl&#39;).html();

        // insert template markup into each td
        // and set the font size to be the value of the td
        $(&#39;.histogram-3d td&#39;).each(function(){
            var val = this.innerHTML;
            $(this)
                .html(tpl)
                .css(&#39;font-size&#39;, val+&#39;px&#39;)
                ;
        });

        var touch = Modernizr.touch
            ,$vp = $(&#39;.viewport:first&#39;)
            ;
        
        $vp.on(touch?&#39;touchstart&#39;:&#39;mousedown&#39;, function(e){
          
            var evt = touch? e.originalEvent.touches[0] : e;
            dragStart = {
                x: evt.screenX + curpos.x,
                y: evt.screenY + curpos.y
            };

            dragging = true;
            $(&#39;body&#39;).addClass(&#39;noselect&#39;);
        });
        
        $(document).on(touch?&#39;touchend&#39;:&#39;mouseup&#39;, function(){
            dragging = false;
            $(&#39;body&#39;).removeClass(&#39;noselect&#39;);
        });
        
        $(document).on(touch?&#39;touchmove&#39;:&#39;mousemove&#39;, function(e){
          
            if (!dragging) return;

            e.preventDefault();

            var evt = touch? e.originalEvent.touches[0] : e
                ,x = dragStart.x - evt.screenX
                ,y = dragStart.y - evt.screenY
                ,amp = 0.2
                ;

            curpos.x = x;
            curpos.y = y;

            $vp.find(&#39;.world&#39;).css(
                Modernizr.prefixed(&#39;transform&#39;),
                [&#39;rotateX(&#39;,y*amp,&#39;deg) rotateY(&#39;,-x*amp,&#39;deg)&#39;].join(&#39;&#39;)
            );

        });
    
    });
});
});
&lt;/script&gt;

&lt;h2 id=&quot;step-1---markup&quot;&gt;Step 1 - Markup&lt;/h2&gt;

&lt;p&gt;First, we need the HTML. We’ll create two container elements that are quite common in 3D css work:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;a viewport: acts as the projection layer or “camera”&lt;/li&gt;
  &lt;li&gt;a world: which holds every other child element which we can easily rotate&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I also like to add another element, &lt;em&gt;the ground&lt;/em&gt;, which is just a plane that objects will sit on.&lt;/p&gt;

&lt;p&gt;Inside this tree of three elements, we’ll add our table, which contains the 3D histogram values. Here’s the markup:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;div class=&quot;viewport&quot;&amp;gt;
    &amp;lt;div class=&quot;world&quot;&amp;gt;
        &amp;lt;div class=&quot;ground&quot;&amp;gt;
            &amp;lt;table class=&quot;histogram-3d&quot;&amp;gt;
                &amp;lt;tr&amp;gt;
                    &amp;lt;td&amp;gt;30&amp;lt;/td&amp;gt;
                    &amp;lt;td&amp;gt;20&amp;lt;/td&amp;gt;
                    &amp;lt;td&amp;gt;60&amp;lt;/td&amp;gt;
                &amp;lt;/tr&amp;gt;
                &amp;lt;tr&amp;gt;
                    &amp;lt;td&amp;gt;80&amp;lt;/td&amp;gt;
                    &amp;lt;td&amp;gt;10&amp;lt;/td&amp;gt;
                    &amp;lt;td&amp;gt;40&amp;lt;/td&amp;gt;
                &amp;lt;/tr&amp;gt;
                &amp;lt;tr&amp;gt;
                    &amp;lt;td&amp;gt;20&amp;lt;/td&amp;gt;
                    &amp;lt;td&amp;gt;50&amp;lt;/td&amp;gt;
                    &amp;lt;td&amp;gt;30&amp;lt;/td&amp;gt;
                &amp;lt;/tr&amp;gt;
            &amp;lt;/table&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;step-2---the-environment-css&quot;&gt;Step 2 - The environment CSS&lt;/h2&gt;

&lt;p&gt;Next we need to set up our 3D environment. &lt;strong&gt;For the sake of simplicity, I will omit -vendor-prefixed values&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;First, the viewport. I some relative positioning, and dimensions, but the important values are the &lt;code class=&quot;highlighter-rouge&quot;&gt;perspective&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;perspective-origin&lt;/code&gt; values. Roughly speaking, the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/CSS/perspective&quot;&gt;perspective&lt;/a&gt; controls how far the “camera” is from the origin, and &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/CSS/perspective-origin&quot;&gt;perspective-origin&lt;/a&gt; defines its position.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.viewport {
    position: relative;
    width: 100%;
    padding-bottom: 100%;
    cursor: move;

    perspective: 4000px;
    perspective-origin: 50% -100%;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Next, the world. Again, we set some positioning and dimension values, but the important values for the world are &lt;code class=&quot;highlighter-rouge&quot;&gt;tranform&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;transform-style&lt;/code&gt;. The transform value will rotate the world to its starting orientation. The transform-style is set to &lt;code class=&quot;highlighter-rouge&quot;&gt;preserve-3d&lt;/code&gt;. This is so that all children of the world will also have 3D appearances. If we didn’t set this, all children of the world element would be smushed into the world plane with no depth. &lt;strong&gt;Note&lt;/strong&gt;: we’re also setting &lt;code class=&quot;highlighter-rouge&quot;&gt;preserve-3d&lt;/code&gt; on every child of the world too. Firefox doesn’t seem to properly propagate this setting down the DOM tree… so this gets around that.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.viewport .world {
    position: absolute;
    top:0;
    left:0;
    right:0;
    bottom:0;

    transform: rotateX(-15deg) rotateY(-20deg);
}

.viewport .world, 
.viewport .world * {
    transform-style: preserve-3d;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Finally, the ground. Easy peasy. Set some positioning (absolute centered alignement), give it a background, and rotate it along the X axis so that it lies flat on the world’s X-Z plane.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.viewport .ground {
    position: absolute;
    z-index: 1;
    top: 50%;
    left: 50%;
    width: 90%;
    height: 90%;
    margin-left: -50%;
    margin-top: -50%;
    background: #eee;
    
    transform: rotateX(90deg);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Try it out in a JSFiddle with the appropriate vendor prefixes and you should see a boring grey square skewed a bit with the table data inside.&lt;/p&gt;

&lt;h2 id=&quot;step-3---the-histogram&quot;&gt;Step 3 - The Histogram&lt;/h2&gt;

&lt;p&gt;Here’s where things get tricky… and fun! First, we want to set up the table to be the grid of the histogram. That’s easy. Just set some dimensions and borders on the tables, like you normally would.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.viewport .histogram-3d {
    width: 80%;
    height: 80%;
    margin: 10% auto;
    border-collapse: collapse;
    border-style: solid;

    /* make sure grid is raised above ground */
    transform: translateZ(1px);
}

.viewport .histogram-3d td {
    position: relative;
    width: 30%;
    height: 30%;
    padding: 10px;
    border: 2px solid #555;
    z-index: 0;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The tricky part is creating the bars. What we need to do is use some javascript to replace the numeric values in the table with some markup to act as the 3D bar. The markup we want to insert into every table cell looks like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;script id=&quot;bartpl&quot; type=&quot;text/template&quot;&amp;gt;
    &amp;lt;div class=&quot;bar&quot;&amp;gt;
        &amp;lt;div class=&quot;face top&quot;&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class=&quot;face front&quot;&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class=&quot;face back&quot;&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class=&quot;face left&quot;&amp;gt;&amp;lt;/div&amp;gt;
        &amp;lt;div class=&quot;face right&quot;&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;.bar&lt;/code&gt; element just contains the five faces (we don’t need a bottom face). To do the replacement, it’s easiest to use a template. We can store this markup in a script tag with a &lt;code class=&quot;highlighter-rouge&quot;&gt;type=&quot;text/template&quot;&lt;/code&gt; or similar. The browser won’t try to run this as javascript, and we can simply use &lt;code class=&quot;highlighter-rouge&quot;&gt;.innerHTML&lt;/code&gt; to get the content. The javascript will look like this (using jQuery):&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// get the template
var tpl = $(&#39;#bartpl&#39;).html();

// insert template markup into each td
// and set the font size to be the value of the td
$(&#39;.histogram-3d td&#39;).each(function(){
    var val = this.innerHTML;
    $(this)
        .html(tpl)
        .css(&#39;font-size&#39;, val+&#39;px&#39;)
        ;
});
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Pretty straightforward, but &lt;em&gt;why are we setting a font size!?&lt;/em&gt; This is the magic bullet to control the &lt;strong&gt;size of the 3D bars&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We’re going to use dimensions relative to the font size (em), in order to control the height of the bars. So if we set a font-size on the bar element, the faces will resize appropriately to the correct height. Snazzy, eh? &lt;a href=&quot;http://tympanus.net/codrops/2012/05/21/animated-3d-bar-chart-with-css3/&quot;&gt;I can’t take full credit for this strategy, though&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So, let’s add the bar css. First, we simply resize the bar container to fill the table cell, and set a relative position. Then for every face element, set a background, an absolute position, relative size, and orientation in 3D space. The differences in color correspond to different &lt;em&gt;shadings&lt;/em&gt; of each face. If you want to get fancy, you can use &lt;a href=&quot;http://photon.attasi.com/&quot;&gt;the Photon CSS lighting engine&lt;/a&gt;, but beware, I found some buggy behaviour in firefox when using it table elements. More about that later. Here’s the rest of the CSS.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.viewport .bar {
    position: relative;
    width: 100%;
    height: 100%;
    z-index: 1;
}

.viewport .bar .face {
    background: hsl(0, 100%, 50%);
    position: absolute;
    width: 100%;

    overflow: hidden;
    z-index: 1;
}

.viewport .bar .face.front {
    background: hsl(0, 100%, 20%);
    bottom: 0;
    height: 1em;

    transform-origin: bottom center;
    transform: rotateX(-90deg);
}

.viewport .bar .face.right {
    top: 0;
    right: 0;
    width: 1em;
    height: 100%;

    transform-origin: center right;
    transform: rotateY(90deg);
}

.viewport .bar .face.left {
    background: hsl(0, 100%, 45%);
    top: 0;
    left: 0;
    width: 1em;
    height: 100%;

    transform-origin: center left;
    transform: rotateY(-90deg);
}

.viewport .bar .face.back {
    top: 0;
    height: 1em;

    transform-origin: top center;
    transform: rotateX(90deg);
}

.viewport .bar .face.top {
    background: hsl(0, 100%, 40%);
    height: 100%;
    width: 100%;
    top: 0;

    transform: translateZ(1em);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;step-4---make-it-rotate&quot;&gt;Step 4 - Make it rotate!&lt;/h2&gt;

&lt;p&gt;The last step can be done in many different ways. We need to attach mouse/touch events to change the world element’s orientation in order to rotate the histogram.&lt;/p&gt;

&lt;p&gt;Generally this means tracking the mouse/touch event coordinates and mapping them to an angle. This is how I’ve done it, but you can probably come up with a cleaner and more general way to do this, right? :)&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;var dragStart = {}
    ,dragging = false
    ,curpos = {x:100,y:-75}
    ;

var touch = Modernizr.touch
    ,$vp = $(&#39;.viewport:first&#39;)
    ;

$vp.on(touch?&#39;touchstart&#39;:&#39;mousedown&#39;, function(e){
  
    var evt = touch? e.originalEvent.touches[0] : e;
    dragStart = {
        x: evt.screenX + curpos.x,
        y: evt.screenY + curpos.y
    };

    dragging = true;
    $(&#39;body&#39;).addClass(&#39;noselect&#39;);
});

$(document).on(touch?&#39;touchend&#39;:&#39;mouseup&#39;, function(){
    dragging = false;
    $(&#39;body&#39;).removeClass(&#39;noselect&#39;);
});

$(document).on(touch?&#39;touchmove&#39;:&#39;mousemove&#39;, function(e){
  
    if (!dragging) return;

    e.preventDefault();

    var evt = touch? e.originalEvent.touches[0] : e
        ,x = dragStart.x - evt.screenX
        ,y = dragStart.y - evt.screenY
        ,amp = 0.2
        ;

    curpos.x = x;
    curpos.y = y;

    $vp.find(&#39;.world&#39;).css(
        Modernizr.prefixed(&#39;transform&#39;),
        [&#39;rotateX(&#39;,y*amp,&#39;deg) rotateY(&#39;,-x*amp,&#39;deg)&#39;].join(&#39;&#39;)
    );

});
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;caveats&quot;&gt;Caveats&lt;/h2&gt;

&lt;p&gt;Unfortunately, things get messier because of cross-browser support. I’m not even talking about IE. I’m talking about Firefox. Turns out &lt;strong&gt;3D transforms won’t work with CSS table layouts in Firefox&lt;/strong&gt;. This means, for Firefox (and perhaps other browsers), we need to reset the layouts of the table, tr, and td elements to use only &lt;code class=&quot;highlighter-rouge&quot;&gt;block&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;inline-block&lt;/code&gt; display values… which is annoying. What I’ve done on this page is browser sniff for firefox, and add a body class &lt;code class=&quot;highlighter-rouge&quot;&gt;no-3dtablelayout&lt;/code&gt;. Then I override the layouts within that scope like so:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/* don&#39;t use table displays... need to use a combination of block and inline-block. Eww. */
.no-3dtablelayout .viewport .histogram-3d,
.no-3dtablelayout .viewport .histogram-3d tbody,
.no-3dtablelayout .viewport .histogram-3d tr,
.no-3dtablelayout .viewport .histogram-3d td { 
    display: block; 
    -webkit-box-sizing: border-box;
       -moz-box-sizing: border-box;
        -ms-box-sizing: border-box;
            box-sizing: border-box;
}

.no-3dtablelayout .viewport .histogram-3d tbody { height: 100%; }
.no-3dtablelayout .viewport .histogram-3d tr {
    text-align: center;
    height: 33%;
    /* fix grid */
    letter-spacing: -0.5em;
    margin-top: -1px;
}
.no-3dtablelayout .viewport .histogram-3d td {
    display: inline-block;
    height: 100%;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To add insult to injury, the result looks a bit ugly. Firefox doesn’t seem to be using anti-aliasing for the face edges. To make it look a bit better, we can &lt;a href=&quot;http://stackoverflow.com/a/9333891&quot;&gt;apply a little hack&lt;/a&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.viewport .histogram-3d * {
    /* this improves jagged edges in firefox */
    outline: 1px solid transparent;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;That’s all for now. I’d love to hear any other cross-browser issues you find.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Visualization of Corner Reflectors</title>
   <link href="http://wellcaffeinated.net/articles/2012/12/16/visualization-of-corner-reflectors"/>
   <updated>2012-12-16T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/12/16/visualization-of-corner-reflectors</id>
   <content type="html">&lt;p&gt;I recently made &lt;a href=&quot;http://wellcaffeinated.github.com/Optics/corner-reflector&quot;&gt;a little visualization&lt;/a&gt; to compliment the video about corner reflectors by Henry (&lt;a href=&quot;http://minutephysics.com/&quot;&gt;MinutePhysics&lt;/a&gt;) and Destin (&lt;a href=&quot;http://www.youtube.com/user/destinws2&quot;&gt;Smarter Every Day&lt;/a&gt;). It took me around 6 hours – mostly thinking time – using &lt;a href=&quot;http://raphaeljs.com/&quot;&gt;Raphael&lt;/a&gt;. You can &lt;a href=&quot;http://wellcaffeinated.github.com/Optics/corner-reflector&quot;&gt;visit the full version (and watch the video)&lt;/a&gt;, or play with this embedded version. Enjoy :)&lt;/p&gt;

&lt;iframe width=&quot;500&quot; height=&quot;500&quot; src=&quot;http://wellcaffeinated.github.com/Optics/corner-reflector/embed.html&quot; frameborder=&quot;0&quot;&gt; &lt;/iframe&gt;
</content>
 </entry>
 
 <entry>
   <title>Very Simple CSS-only Proportional Resizing of Elements</title>
   <link href="http://wellcaffeinated.net/articles/2012/12/10/very-simple-css-only-proportional-resizing-of-elements"/>
   <updated>2012-12-10T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/12/10/very-simple-css-only-proportional-resizing-of-elements</id>
   <content type="html">&lt;p&gt;A while ago I posted about &lt;a href=&quot;http://wellcaffeinated.net/articles/2012/06/26/proportional-resizing-of-web-page-elements-using-only-css&quot;&gt;Proportional Resizing of Web Page Elements Using Only CSS&lt;/a&gt;. At the time, it seemed like the only solution… but fortunately a slightly counter intuitive CSS standard provides a &lt;em&gt;much better way&lt;/em&gt; to get this same effect. Credit goes to &lt;a href=&quot;http://stackoverflow.com/a/6615994/1729163&quot;&gt;Nathan D. Ryan for his solution on Stackoverflow&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The concept is nicely discussed in &lt;a href=&quot;http://mattsnider.com/css-using-percent-for-margin-and-padding/&quot;&gt;Matt Snider’s Blog Post&lt;/a&gt;…
&amp;gt; Doing a little research, I was reminded that when you use percents to apply the margin of an element, the browsers determine &lt;strong&gt;the actual size of the margin by multiply the percent against the width of the parent node&lt;/strong&gt; (this is true for margin-top, -right, -bottom, and -left). It is not based on either the height or width of the element that the margin is applied to. […] I was surprised to relearn that the &lt;strong&gt;padding is also relative to the width of the applied elements parent node&lt;/strong&gt;. So using percentages with padding works exactly as it does with margin.&lt;/p&gt;

&lt;p&gt;This could come in handy for a few things. It’s a good fact to note if you’re doing CSS work. For proportional resizing purposes, it makes matters extremely simple: &lt;strong&gt;Define the width of an element as a percentage (eg: 100%) of the parent’s width, then define the element’s padding-top (or -bottom) as a percentage so that the height is the aspect ratio you need&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For example, if I have a container with a child element like so:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;div class=&quot;container&quot;&amp;gt;
    &amp;lt;div class=&quot;child&quot;&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I can use the following css to give the child element an aspect ratio of 4:3&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.container { 
    width: 500px; /* pick whatever width you want */
}
.container .child {
    width: 100%;
    padding-top: 75%;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;But, of course, if we put any content inside the child it will change the height and throw off our aspect ratio. So what we need is another element inside which is positioned absolutely over the child element which obtains its size. We do that like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;div class=&quot;container&quot;&amp;gt;
    &amp;lt;div class=&quot;outer&quot;&amp;gt;
        &amp;lt;div class=&quot;inner&quot;&amp;gt;Your content&amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;and css…&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.container { 
    width: 500px; /* pick whatever width you want */
}
.container .outer {
    width: 100%;
    padding-top: 75%; /* defines aspect ratio */
    position: relative;
}
.container .outer .inner {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And that’s it! All in all, very intuitive solution that uses a counter intuitive feature of CSS.&lt;/p&gt;

&lt;p&gt;Here’s the &lt;a href=&quot;http://jsfiddle.net/wellcaffeinated/8Frb6/&quot;&gt;full jsFiddle&lt;/a&gt;:&lt;/p&gt;

&lt;iframe style=&quot;width: 100%; height: 300px&quot; src=&quot;http://jsfiddle.net/wellcaffeinated/8Frb6/embedded/result,html,css,js&quot; allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot;&gt; &lt;/iframe&gt;
</content>
 </entry>
 
 <entry>
   <title>Quick Tip: Creating a "Draw" animation with svg paths</title>
   <link href="http://wellcaffeinated.net/articles/2012/12/04/quick-tip-creating-a-draw-animation-with-svg-paths"/>
   <updated>2012-12-04T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/12/04/quick-tip-creating-a-draw-animation-with-svg-paths</id>
   <content type="html">&lt;p&gt;Let’s say you want to create an animation that draws some kind of shape. Like watching a pencil draw a line on paper. Turns out this is really easy with SVG paths.&lt;/p&gt;

&lt;iframe style=&quot;width: 100%; height: 300px&quot; src=&quot;http://jsfiddle.net/wellcaffeinated/6tc8x/embedded/result&quot; allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot;&gt; &lt;/iframe&gt;

&lt;p&gt;If you haven’t heard of SVG, you should check it out. It’s a &lt;a href=&quot;http://caniuse.com/#feat=svg&quot;&gt;well-supported way&lt;/a&gt; of creating complex shapes with DOM elements. &lt;a href=&quot;http://raphaeljs.com/&quot;&gt;RaphaelJS is a library&lt;/a&gt; that makes this process even easier by providing javascript methods to do complex drawings.&lt;/p&gt;

&lt;p&gt;One way of creating a shape is by creating it as an SVG path. This is exactly what it sounds like. It’s a directional outline of a shape, or a way of drawing a curvy line from point A to point B. You can draw along this path with different stroke styles (solid, dashed, etc). The neat thing about creating a dashed line along a path is that you can control the length of the dashes. What happens if you make the dash as long as the line? Magic.&lt;/p&gt;

&lt;p&gt;If you create a path, and then specify the dash length to be much greater than the path itself, you can animate the dash offset to give the effect of “drawing” along that path. Here’s how you do it:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Create a path (see &lt;a href=&quot;http://raphaeljs.com/&quot;&gt;RaphaelJS&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Set the properties: &lt;code class=&quot;highlighter-rouge&quot;&gt;path-dasharray&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;path-dashoffset&lt;/code&gt; to be equal, and a pixel value much greater than the path itself (eg: 99999px). Let’s call this &lt;code class=&quot;highlighter-rouge&quot;&gt;maxLen&lt;/code&gt; for now.&lt;/li&gt;
  &lt;li&gt;Animate the &lt;code class=&quot;highlighter-rouge&quot;&gt;path-dashoffset&lt;/code&gt; from its starting value, to the value of the &lt;code class=&quot;highlighter-rouge&quot;&gt;path-dasharray&lt;/code&gt; minus the path length (&lt;code class=&quot;highlighter-rouge&quot;&gt;maxLen - path length&lt;/code&gt;). You can use jQuery for this.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here’s a &lt;a href=&quot;http://jsfiddle.net/wellcaffeinated/uZp3q/&quot;&gt;jsFiddle showing an example of this technique&lt;/a&gt;.&lt;/p&gt;

&lt;iframe style=&quot;width: 100%; height: 500px&quot; src=&quot;http://jsfiddle.net/wellcaffeinated/uZp3q/embedded/result,js,css,html,resources&quot; allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot;&gt; &lt;/iframe&gt;
</content>
 </entry>
 
 <entry>
   <title>Quick Tip: Google Analytics Tracking for external links</title>
   <link href="http://wellcaffeinated.net/articles/2012/11/11/quick-tip-google-analytics-tracking-for-external-links"/>
   <updated>2012-11-11T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/11/11/quick-tip-google-analytics-tracking-for-external-links</id>
   <content type="html">&lt;p&gt;Here’s a nice snippet I’ve been using to track visitor exit flows on some of the sites I’ve built. You see, google analytics doesn’t automatically log what &lt;em&gt;external&lt;/em&gt; link visitors click on to exit your site. You might want to know this if your site is trying to reach a certain goal of funneling visitors to a particular place on another server.&lt;/p&gt;

&lt;p&gt;For example, if you’re trying to boost views particular YouTube video by directing traffic there, you want to know how effective your website is at doing that. So here’s what you do. You set up a bit of javascript to log a google analytics “event” every time users try to visit an external link (click). If you use jQuery, you can basically copy and paste this snippet anywhere into your code (after jQuery is loaded):&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(function($, window, document){

$(document).on(&#39;click&#39;, &#39;a[href^=http]&#39;, function(e){

    var $this = $(this)
        ,url = $this.attr(&#39;href&#39;)
        ,newtab = ($this.attr(&#39;target&#39;) === &#39;_blank&#39; || e.metaKey || e.ctrlKey)
        ;

    window._gaq = window._gaq || [];
    
    try {

        window._gaq.push([&#39;_trackEvent&#39;, &#39;Outbound Links&#39;, e.currentTarget.host, url, 0]);
        
        if (!newtab) {

            e.preventDefault();
            setTimeout(function(){
                document.location = url;
            }, 100);
        }
    } catch (err){}
});

})(jQuery, this, this.document);
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This will detect whether or not the user has tried to load it in a new tab or if you’re using &lt;code class=&quot;highlighter-rouge&quot;&gt;target=&quot;_blank&quot;&lt;/code&gt; and accommodate for those outcomes. It will also work before domReady has fired because we’re using &lt;a href=&quot;http://api.jquery.com/on/&quot;&gt;event delegation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Enjoy :)&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Sheep Bounce (like an ideal gas) for Science!</title>
   <link href="http://wellcaffeinated.net/articles/2012/10/25/sheep-bounce-like-an-ideal-gas-for-science"/>
   <updated>2012-10-25T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/10/25/sheep-bounce-like-an-ideal-gas-for-science</id>
   <content type="html">&lt;p&gt;After seeing the silly, yet addictive website, &lt;a href=&quot;http://cat-bounce.com&quot;&gt;cat-bounce.com&lt;/a&gt;, I decided to &lt;a href=&quot;http://minutephysics.github.com/Sheep-Bounce/&quot;&gt;quickly remix the idea&lt;/a&gt; - &lt;em&gt;For Science&lt;/em&gt;!&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://minutephysics.github.com/Sheep-Bounce/&quot;&gt;&lt;img src=&quot;/images/sheepbounce.gif&quot; alt=&quot;Sheep Bounce&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using artwork created by Henry Reich (of &lt;a href=&quot;http://minutephysics.com&quot;&gt;MinutePhysics&lt;/a&gt;), I created a small web app that allows you to play around with sheep that bounce around the screen. The sheep resemble how atoms might jiggle around in an ideal gas. My hope is that it sparks people’s interest to know more about ideal gases. The silly app could be the beginning of a question about physics, so I’ve placed a button leading to one of Henry’s videos in the side-panel.&lt;/p&gt;

&lt;p&gt;Will this work to get people to watch more MinutePhysics? I’m not sure. I’ll have to wait until the analytics build up to make any assessment.&lt;/p&gt;

&lt;p&gt;I see this as the beginning of a direction towards interaction as a learning process. I hope to build more highly-interactive web apps (with Henry) to engage people in the learning process. Ideally these interfaces would be much more active in education than &lt;em&gt;Sheep Bounce&lt;/em&gt;, and would provide not just a top-down approach, but also a bottom-up. By that I mean: &lt;em&gt;learning by interacting&lt;/em&gt; with the interface. More on this later…&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Calculating the Acceleration of an Airplane using a Smartphone</title>
   <link href="http://wellcaffeinated.net/articles/2012/10/06/calculating-the-acceleration-of-an-airplane-using-a-smartphone"/>
   <updated>2012-10-06T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/10/06/calculating-the-acceleration-of-an-airplane-using-a-smartphone</id>
   <content type="html">&lt;p&gt;As a web developer who loves physics, I’m always looking for ways to use what I know about web browser technology to do neat physics experiments. A few months ago I was sitting in an airport ready to return home. I had been thinking about the &lt;a href=&quot;/articles/fun-with-javascript-and-device-orientation&quot;&gt;device orientation&lt;/a&gt; detection capability of smartphones for a while, and was wondering what I could do with it. With airplanes on the mind, I decided to use the small amount of time I had to write a quick webapp to record the orientation of my phone during takeoff. Since I could write this purely in javascript, I could safely put my phone into “Airplane Mode” and continue to monitor its orientation. Javascript’s “localStorage” could also save the data I accumulated. With the orientation data, I could then get a rough estimate of the acceleration profile of the airplane during takeoff (or any other time, really).&lt;/p&gt;

&lt;p&gt;The webapp I wrote in about 30 minutes is not worth putting up here. It basically involved a start and stop button for the data. But it did the trick. Don’t worry, soon I’ll write one that will be easy for everyone to load onto their phones and tinker around with acceleration measurements.&lt;/p&gt;

&lt;p&gt;All I needed to do was place my phone on the armrest of the seat and click start. Here’s a graph of the data:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://docs.google.com/spreadsheet/oimg?key=0Akupmpq7rS__dEhtYVE3dDZkZnI5dTAweHlWUndQMXc&amp;amp;oid=2&amp;amp;zx=x1yjztp0l3so&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Not too shabby! It’s pretty noisy, but you can see the takeoff pretty clearly.&lt;/p&gt;

&lt;p&gt;So, what are we measuring?&lt;/p&gt;

&lt;p&gt;We’re measuring the &lt;em&gt;perceived&lt;/em&gt; tilt angle of the phone. Normally, if you tilted your phone forwards and backwards, the phone would measure the direction of gravitational force relative to the screen. So if you placed your phone on a level table, it would measure gravity directly into the screen. This would give you a tilt angle of zero (in front-back, and left-right axes). When you tilt your phone, the phone will detect gravity at some angle, and be able to give you the measure of that angle. You can &lt;a href=&quot;/demos/device-orientation&quot;&gt;play around with that here&lt;/a&gt;, if your phone or laptop supports it.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://simplescientist.files.wordpress.com/2010/10/airplane1.png&quot; alt=&quot;Airplane Takeoff&quot; /&gt;&lt;/p&gt;

&lt;p&gt;But what happens during takeoff? Well, gravity is no longer the only force the phone detects. Now it’s detecting the combination of gravity and the thrust of the airplane. As the airplane is rolling along the runway, this thrust will be horizontal (perpendicular to gravity). If you add up these effects, the result is that the phone interprets this as a tilt. The amount of tilt measured can be easily calculated:&lt;/p&gt;

&lt;script type=&quot;math/tex; mode=display&quot;&gt;\\tan(\\theta) = \\frac{F_{\\textrm{thrust}}}{F_{\\textrm{gravity}}}&lt;/script&gt;

&lt;p&gt;If we want to, we can divide the top and bottom of that equation by the mass of the airplane. Then we can put everything in terms of accelerations:&lt;/p&gt;

&lt;script type=&quot;math/tex; mode=display&quot;&gt;\\tan(\\theta) = \\frac{a_{\\textrm{thrust}}}{a_{\\textrm{gravity}}}&lt;/script&gt;

&lt;p&gt;We have the angle, and the gravitational acceleration value (&lt;code class=&quot;highlighter-rouge&quot;&gt;9.8 m/s/s&lt;/code&gt;), so we can get the acceleration of the airplane. The data I’ve got here is pretty noisy, so let’s just do a rough estimation. At around 140s, the angle fluctuates between 16 and 24 degrees. So let’s ballpark it at 20 degrees.&lt;/p&gt;

&lt;script type=&quot;math/tex; mode=display&quot;&gt;a_{\\textrm{thrust}} = 9.8 \\\\ \\tan(20\\deg) \\\\ m/s^2&lt;/script&gt;

&lt;p&gt;… which gives us around &lt;code class=&quot;highlighter-rouge&quot;&gt;3.5 m/s/s&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;How does this compare to the “actual” acceleration? Well, it’s hard to say what the real acceleration of the airplane was. This will depend on many factors; air resistance, number of passengers, … But still, we can estimate this from the &lt;a href=&quot;http://en.wikipedia.org/wiki/Airbus_A320_family#Specifications&quot;&gt;airplane specs&lt;/a&gt;. For an A320, the thrust can vary between &lt;code class=&quot;highlighter-rouge&quot;&gt;111 - 120 kN&lt;/code&gt;, its operating empty weight is &lt;code class=&quot;highlighter-rouge&quot;&gt;42600 kg&lt;/code&gt;, and its maximum takeoff weight is around &lt;code class=&quot;highlighter-rouge&quot;&gt;77110 kg&lt;/code&gt;. So the maximum acceleration is maximum thrust divided by the weight while the airplane is empty:&lt;/p&gt;

&lt;script type=&quot;math/tex; mode=display&quot;&gt;a_{\\textrm{thrust}} = \\frac{120 \\\\ \\textrm{kN}}{42600 \\\\ \\textrm{kg}}&lt;/script&gt;

&lt;p&gt;which is about &lt;code class=&quot;highlighter-rouge&quot;&gt;2.8 m/s/s&lt;/code&gt;. The minimum acceleration is minimum thrust divided by maximum takeoff weight, which gives you &lt;code class=&quot;highlighter-rouge&quot;&gt;1.4 m/s/s&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, we’re a bit off, but remember, this is quite a rough estimate from the data. Aside from the noisiness of the data, one major caveat is that we didn’t take into account the angle of the phone when the airplane wasn’t moving (it wasn’t quite at zero). But it still gives you an idea of the fun physics you can do with your phone.&lt;/p&gt;

&lt;p&gt;Stay tuned for more smartphone physics fun.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Your JavaScript is Slow - Common and not-so Common Optimizations</title>
   <link href="http://wellcaffeinated.net/articles/2012/09/07/your-javascript-is-slow-common-and-not-so-common-optimizations"/>
   <updated>2012-09-07T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/09/07/your-javascript-is-slow-common-and-not-so-common-optimizations</id>
   <content type="html">&lt;p&gt;Optimization considerations for any language can lead down a long and complex road. For JavaScript, things get even more complex because the same code can be run on a huge number of permutations of browser, version, OS, and machine (think mobile devices too).&lt;/p&gt;

&lt;p&gt;Still, I was surprised how little information I found on the subject. A quick google search turns up a few great resources. To mention a few: &lt;a href=&quot;http://jonraasch.com/blog/10-javascript-performance-boosting-tips-from-nicholas-zakas&quot;&gt;An overview of Nicholas Zakas’s tips&lt;/a&gt;, &lt;a href=&quot;https://developers.google.com/speed/articles/optimizing-javascript&quot;&gt;Google’s “Make the Web Faster” article&lt;/a&gt;, and &lt;a href=&quot;http://home.earthlink.net/~kendrasg/info/js_opt/&quot;&gt;An older article by Jeff Greenberg&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;These all are great resources, but they lack a few of the more surprising offenders in terms of bottlenecks. So I decided to post some information that I’ve discovered during my long hours on &lt;a href=&quot;http://jsperf.com/&quot;&gt;JSPerf&lt;/a&gt;. If you haven’t heard of JSPerf before, check it out.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What is it, you ask&lt;/em&gt;?&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;jsPerf aims to provide an easy way to create and share test cases, comparing the performance of different JavaScript snippets by running benchmarks. But even if you don’t add tests yourself, you can still use it as a JavaScript performance knowledge base.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, you don’t need to wait around for someone to benchmark a test for you. You can do it yourself! (&lt;em&gt;aside: At a later date, I’ll try to write some guidelines for intelligently creating test cases.&lt;/em&gt;) For now, I’m going to go over some of the common and not-so common bottlenecks to look for while trying to optimize your code.&lt;/p&gt;

&lt;h2 id=&quot;most-importantly&quot;&gt;Most Importantly…&lt;/h2&gt;

&lt;p&gt;Firstly, &lt;strong&gt;Don’t optimize on the fly&lt;/strong&gt;. The big reason for this is that your implementation may change as you develop your code… so you might end up burning hours on unnecessary optimizations for algorithms that you end up scrapping by the end. You should also go hunting for your largest bottlenecks first. You don’t want to spend hours optimizing in the wrong place. Use a profiler (like chrome dev tools) to find these bottlenecks.&lt;/p&gt;

&lt;p&gt;Secondly, &lt;strong&gt;Don’t sacrifice readability for speed! That’s what minifiers are there for&lt;/strong&gt;. You might come across optimization tips like “don’t use long variable names”, or “avoid creating unnecessary variables”… but I say forget that! You should be minifying your code. Always. Period. That’s the most important optimization tip you can take away from this.&lt;/p&gt;

&lt;p&gt;Consider the following…&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// sure... this...
var NumberOfFingers = 4 + 1;

// is not as fast as this...
var n = 5;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;… the first option is more readable. Similarly,&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// this is faster...
function toRad( deg ){
	return deg*Math.Pi/180;
}
//... than...
function toRad( degrees ){
	var radiansPerDegrees = Math.Pi/180;
    return degrees * radiansPerDegrees;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;the second option here will make people much less frustrated when they try to understand your program. And when you put that second option through a minifier like &lt;a href=&quot;http://closure-compiler.appspot.com/home&quot;&gt;Closure Compiler&lt;/a&gt;, look what it becomes:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;function toRad(a){return a*(Math.Pi/180)};
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;So there.&lt;/p&gt;

&lt;p&gt;Finally, &lt;strong&gt;Don’t trust everything you hear!&lt;/strong&gt; Javascript engines are changing all the time, and some optimization tips that applied to certain browsers in 2009 may not apply now. And those that apply now, may not apply in a year. It’s best to test these things yourself.&lt;/p&gt;

&lt;h2 id=&quot;unsurprising-common-optimizations&quot;&gt;Unsurprising (common) Optimizations&lt;/h2&gt;

&lt;h3 id=&quot;loops&quot;&gt;Loops&lt;/h3&gt;

&lt;p&gt;Ok. So. Let’s start with some easy ones. What’s wrong with the following for loop?&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;for (var i = 0; i &amp;lt; catPictures.length; i++)
	catPictures[ i ].comment = &#39;lolz&#39;;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;You guessed it. We aren’t caching the &lt;code class=&quot;highlighter-rouge&quot;&gt;length&lt;/code&gt; of the &lt;code class=&quot;highlighter-rouge&quot;&gt;catPictures&lt;/code&gt; array. It’s being recalculated every single cycle of the loop. If you don’t believe me, &lt;a href=&quot;http://jsperf.com/simple-loop-optimizations&quot;&gt;try it on jsperf&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As for which type of looping method is the fastest… results may vary. &lt;a href=&quot;http://jsperf.com/faster-loop/5&quot;&gt;Here’s a suite of looping tests on jsperf&lt;/a&gt; for your pleasure. Generally it seems that the standard cached length &lt;code class=&quot;highlighter-rouge&quot;&gt;for&lt;/code&gt; loop is empirically faster than other loops (including &lt;code class=&quot;highlighter-rouge&quot;&gt;while&lt;/code&gt; loops and especially built-in &lt;code class=&quot;highlighter-rouge&quot;&gt;.each&lt;/code&gt; methods).&lt;/p&gt;

&lt;h3 id=&quot;object-lookups&quot;&gt;Object lookups&lt;/h3&gt;

&lt;p&gt;Here’s another one that might be a walk in the park.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;for (var i = 0, l = ids.length; i &amp;lt; l; i++)
    myFramework.utils.addText( ids[ i ], &#39;text&#39; );
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The problem with this is we can speed this up by caching the &lt;code class=&quot;highlighter-rouge&quot;&gt;addText&lt;/code&gt; method. Every time we use a &lt;code class=&quot;highlighter-rouge&quot;&gt;.&lt;/code&gt; to dig deeper into our objects we loose time. Here’s some better code:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;var addText = myFramework.utils.addText;
for (var i = 0, l = ids.length; i &amp;lt; l; i++)
    addText( ids[ i ], &#39;text&#39; );
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note: this is especially useful when you’re using &lt;code class=&quot;highlighter-rouge&quot;&gt;document.getElementById&lt;/code&gt; and similar methods.&lt;/strong&gt;&lt;/p&gt;

&lt;h3 id=&quot;local-scope--global-scope&quot;&gt;Local Scope &amp;gt; Global Scope&lt;/h3&gt;

&lt;p&gt;Finally, here’s one that I found surprising at first, but it makes total sense. Consider the following code.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;var Fn1 = 0;
var Fn2 = 1;

function nextFibonacci() {
    var ret = Fn1 + Fn2;
    Fn1 = Fn2;
    Fn2 = ret;
    return ret;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;We can actually improve on this by cacheing the values of the variables that are outside the scope of that function. This speeds things up because the interpreter has to look for variable names up the scope chain until it finds what it’s looking for. If the variables are declared within the same scope… it’s faster. Here’s the optimized version:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;var Fn1 = 0;
var Fn2 = 1;

function nextFibonacci() {
    var f1 = Fn1,
        f2 = Fn2,
        f3 = f1 + f2;

    Fn1 = f2;
    Fn2 = f3;
    return f3;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;If you don’t believe me, &lt;a href=&quot;http://jsperf.com/caching-scope-vars/5&quot;&gt;check out the JSPerf for this optimization&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;avoiding-anything-new&quot;&gt;Avoiding anything &lt;code class=&quot;highlighter-rouge&quot;&gt;new&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;new&lt;/code&gt; keyword certainly has its uses and circumstances. But if you’re creating a bunch of objects, especially in a loop, you should try to avoid creating them with &lt;code class=&quot;highlighter-rouge&quot;&gt;new&lt;/code&gt;. Instead of &lt;code class=&quot;highlighter-rouge&quot;&gt;var thing = new myObj()&lt;/code&gt;, try just using javascript object literals: &lt;code class=&quot;highlighter-rouge&quot;&gt;var thing = { ... }&lt;/code&gt;. It will vastly speed up your code. Here’s the &lt;a href=&quot;http://jsperf.com/data-structure-new-vs-literal-instantiation&quot;&gt;JSPerf for new vs object litterals&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;surprising-uncommon-optimizations&quot;&gt;Surprising (uncommon) Optimizations&lt;/h2&gt;

&lt;h3 id=&quot;function-declaration-vs-function-expression&quot;&gt;Function declaration vs. Function expression&lt;/h3&gt;

&lt;p&gt;In my countless hours of tinkering around on &lt;a href=&quot;http://jsperf.com/&quot;&gt;JSPerf&lt;/a&gt;, I’ve discovered a lot of kooky thinks to watch out for in terms of performance. One of the most basic is &lt;em&gt;how you declare your functions&lt;/em&gt;! To create a function you can either do it as a function declaration or a function expression. Here’s the difference:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// function declaration
function foo(){
	// stuff...
}

// function expression
var foo = function(){
	// stuff...
};

// self-executing function expressions
(function(){
	// stuff...
})();
// or...
!function(){
	// stuff...
}();
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;You can visit &lt;a href=&quot;http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/&quot;&gt;JavaScript Weblog&lt;/a&gt; for some of the finer points on the differences between function declarations and function expressions.&lt;/p&gt;

&lt;p&gt;So turns out, that &lt;a href=&quot;http://jsperf.com/function-declarations-vs-function-expressions&quot;&gt;function &lt;strong&gt;declarations&lt;/strong&gt; are much faster to create, as seen from this JSPerf&lt;/a&gt;. So if you’re creating a helper function, you might as well &lt;em&gt;declare&lt;/em&gt; it and reap some of the speed benefits.&lt;/p&gt;

&lt;h3 id=&quot;use-of-the-arguments-object&quot;&gt;Use of the &lt;code class=&quot;highlighter-rouge&quot;&gt;arguments&lt;/code&gt; object&lt;/h3&gt;

&lt;p&gt;You know that ever-so-useful &lt;code class=&quot;highlighter-rouge&quot;&gt;arguments&lt;/code&gt; object you can use inside a function to access the arguments? Yeah. Don’t use it unless you have to. There’s a significant performance hit. Even if you just put the word “arguments” in your function… even in code that will never run… you will see a performance hit. Here are the specifics of &lt;a href=&quot;http://jsperf.com/the-arguments-object-s-effect-on-speed&quot;&gt;arguments object performance effects&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;clever-ways-around-slow-native-methods&quot;&gt;Clever ways around slow native methods&lt;/h3&gt;

&lt;p&gt;Have you ever wondered if &lt;code class=&quot;highlighter-rouge&quot;&gt;array = array.concat(otherarray)&lt;/code&gt; is really the fastest way to concatenate an array? Well, it may not be depending on your situation. Specifically, if you don’t mind modifying the original array, you can certainly get a vast improvement in speed by doing the following:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;var arr1 = [1,2,3];
var arr2 = [4,5,6];
var push = Array.prototype.push;

push.apply(arr1, arr2); // arr1 == [1,2,3,4,5,6]
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And here’s the &lt;a href=&quot;http://jsperf.com/concat-push/6&quot;&gt;JSPerf of this push-concat method&lt;/a&gt;. And even if you don’t want to modify the original array, you can perform an array copy of the original (with &lt;code class=&quot;highlighter-rouge&quot;&gt;array.slice(0)&lt;/code&gt;) and use that one and it will &lt;em&gt;still&lt;/em&gt; be faster!&lt;/p&gt;

&lt;p&gt;Why does this work? It’s because the &lt;code class=&quot;highlighter-rouge&quot;&gt;.push()&lt;/code&gt; method can push several elements passed as arguments (eg: &lt;code class=&quot;highlighter-rouge&quot;&gt;.push(1,2,3,4)&lt;/code&gt;). So when you use it as the second parameter in an apply call, it’s like you’re doing: &lt;code class=&quot;highlighter-rouge&quot;&gt;arr1.push(arr2[0], arr2[1], ...)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Another nice workaround that is even more dependant on the situation involves the situation of array element removal. If you want to remove an object from an array, an easy way to do it is:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;myArray.splice( index, 1 );
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;But if you &lt;strong&gt;don’t care about the order&lt;/strong&gt; then a faster method would be:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if ( index === (myArray.length - 1) )
	myArray.pop();
else
	myArray[index] = myArray.pop();
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;It’s a very particular circumstance, but still, a great concept to keep in mind. &lt;a href=&quot;http://jsperf.com/splice-vs-remove&quot;&gt;Here’s the JSPerf&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;putting-it-all-together&quot;&gt;Putting it all together&lt;/h2&gt;

&lt;p&gt;If you want to see how this all fits together, I’ve created a quick implementation of a PubSub library to show what kind of effect these optimizations can have. The actual usefulness of this pubsub implementation is questionable, I made it more for instructional purposes. Here’s a &lt;a href=&quot;http://codepen.io/wellcaffeinated/full/Hladx&quot;&gt;codepen.io of the pubsub implementations&lt;/a&gt;. What I did was create two versions of it; an unoptimized version, and an optimized version (using the previously mentioned suggestions). I then created some test cases to test for instantiation time and execution time. Here is a &lt;a href=&quot;http://jsperf.com/unoptimized-pubsub-vs-optimized-pubsub/2&quot;&gt;JSPerf of the side-by-side performance results&lt;/a&gt;. You’ll probably find that the graph won’t help you, you should check the tabular version of the results and look at how the unoptimized/optimized versions of each operation compare.&lt;/p&gt;

&lt;p&gt;Happy coding.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Better Content Editing in Wolf CMS</title>
   <link href="http://wellcaffeinated.net/articles/2012/08/31/better-content-editing-in-wolf-cms"/>
   <updated>2012-08-31T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/08/31/better-content-editing-in-wolf-cms</id>
   <content type="html">&lt;p&gt;Originally, I created this blog using a modified version of &lt;a href=&quot;http://www.madebyfrog.com&quot;&gt;Frog CMS&lt;/a&gt;, but soon upgraded to &lt;a href=&quot;http://www.wolfcms.org&quot;&gt;Wolf CMS&lt;/a&gt; which is a fork of Frog under active development. I thought that Wolf would bring about huge improvements in the backend design, and while it improved slightly, one major gap still stood out: &lt;em&gt;the page editor&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The Wolf page editor sucks. All it is is a text area element. Don’t get me wrong… I’m not hoping for a TinyMCE sort of thing… I write in &lt;a href=&quot;http://daringfireball.net/projects/markdown/basics&quot;&gt;Markdown&lt;/a&gt; which is pretty much text anyways. But there are those times when I want to write code snippets, and having the tab key pull focus outside of my writing area is annoying to say the least. Not to mention the lack of syntax highlighting.&lt;/p&gt;

&lt;p&gt;Fortunately, being a developer means you can always have your way (as long as you put in the work). So I decided to make a &lt;a href=&quot;https://github.com/wellcaffeinated/Code-Mirror-for-Wolf-CMS&quot;&gt;Wolf CMS plugin that uses Code Mirror for page editing&lt;/a&gt;. Free for download, obviously. All you need to do is drop it into the Wolf Plugins directory and activate it.&lt;/p&gt;

&lt;p&gt;It still lacks some features I’d like to add. I’m sure you’d probably find some nice-to-haves of your own. But the code is on GitHub, for your forking pleasure. Enjoy.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Education isn't a place you go, it's a thing you do</title>
   <link href="http://wellcaffeinated.net/articles/2012/07/30/education-isnt-a-place-you-go-its-a-thing-you-do"/>
   <updated>2012-07-30T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/07/30/education-isnt-a-place-you-go-its-a-thing-you-do</id>
   <content type="html">&lt;p&gt;Spend any time in the technology industry, and you’ll begin to notice “working from home” is becoming more commonplace. Large and small tech agencies are slowly opening up to the idea of letting their employees work remotely. A day or two a week, I build websites in the comfort of my own home. With all of the recent developments in technology, it’s very easy to keep up to date and connected without going into an office. Some technology companies don’t even have an office at all.&lt;/p&gt;

&lt;p&gt;Strangely, I never thought about this kind of a process in the context of education. But this article in the Wall Street Journal, “&lt;a href=&quot;wsj&quot;&gt;My Education in Home Schooling&lt;/a&gt;”, made me wonder… &lt;em&gt;can and will education begin to make this switch too&lt;/em&gt;?&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“Imagine that your high-school junior spends half of every day at the brick-and-mortar school up the street. Two afternoons a week, he logs into an art-history seminar being taught by a grad student in Paris. He takes computer animation classes at the local college, sings in the church choir and dives at the community pool. He studies Web design on YouTube. He and three classmates see a tutor at the public library who preps them for AP Chemistry. He practices Spanish on Skype and takes cooking lessons at a nearby restaurant every Saturday morning.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I think we tend to subconsciously fall into the habit of thinking about “education” as a place that you go, rather than a thing that you do. I remember near the end of my high school years, I decided I was curious about the guitar. I happened to type in “guitar chords” into google, not knowing what I’d find. It turned out to be a never-ending library of “How to” articles and resources. I finally realized that learning new skills was something I could do on my own. I didn’t have to wait for school to get around to teaching it to me. It was a very liberating feeling.&lt;/p&gt;

&lt;p&gt;Online repositories like &lt;a href=&quot;ocw&quot;&gt;MIT’s Open CourseWare&lt;/a&gt; are bringing complex subjects out of the walls of universities. I’m not saying that any kid can learn something like Quantum Field Theory by browsing the internet, but there’s no reason that great learning can’t take place out of the classroom. And using the new tools that internet technology provides naturally facilitate this.&lt;/p&gt;

&lt;p&gt;There’s a lot of effort these days to bring technology into the classroom. And sometimes it gets a lot of &lt;a href=&quot;slate&quot;&gt;bad press&lt;/a&gt; (that I don’t actually think hits the mark). Perhaps we’re thinking about this the wrong way around. Maybe, it should be less about bringing technology to the classroom, and more about &lt;strong&gt;using technology to bring learning out of the classroom&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The author offers the term “roam schooling” to describe this idea. Decouple the idea of education from any particular institution, location, or person. The process of learning a particular subject or skill can freely draw from whatever resource is most suitable at the time. Why not have an education system like this, even in part? It seems like a very attractive idea, especially since it can easily adapt to the student’s individual goals, interests, and aptitudes. How can we build on this idea? What resources would students, teachers, and parents need to make this a viable option?&lt;/p&gt;

&lt;p&gt;Easy access to information, of course, is not equal to education… but it certainly helps. A great teacher is more of a “learning facilitator” than simply a lecturer or an encyclopedia. And some of this facilitation and guidance may or may not be able to come from parents. I don’t know what a “roam school” education system would look like… but it seems to me that we’re struggling to fix a broken education system largely isolated from the technological shifts that are changing every other aspect of our lives. Technology may not be the whole answer… but maybe we’re just asking the wrong question.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Edit: Looks like the &lt;a href=&quot;http://t.co/nt0xhEky&quot;&gt;most recent TED talk&lt;/a&gt; is about this very subject…&lt;/em&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Proportional Resizing of Web Page Elements Using Only CSS</title>
   <link href="http://wellcaffeinated.net/articles/2012/06/26/proportional-resizing-of-web-page-elements-using-only-css"/>
   <updated>2012-06-26T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/06/26/proportional-resizing-of-web-page-elements-using-only-css</id>
   <content type="html">&lt;p&gt;There are times, throughout the development process where you are surprised to find something that really should be easy. But it isn’t.&lt;/p&gt;

&lt;p&gt;I came across such a thing recently while developing a responsive website for a client. One of the components involved a calendar layout that had to resize depending on the browser width. Usually, this is as easy as setting a percent width on the parent element, but this calendar had thumbnail images for each day that needed to be resized to &lt;em&gt;preserve the aspect ratio&lt;/em&gt;. This meant that the whole calendar had to preserve its aspect ratio (more or less). This is actually a bit more tricky that it first looks. Let’s take a look.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;EDIT: I’ve recently discovered a better solution for this. I recommend &lt;a href=&quot;/articles/2012/12/10/very-simple-css-only-proportional-resizing-of-elements/&quot;&gt;this new method&lt;/a&gt; over the one described below.&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;something-that-doesnt-work&quot;&gt;Something that doesn’t work&lt;/h2&gt;

&lt;p&gt;Consider our friend the simple DIV element.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;div class=&quot;outer&quot;&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;with styles…&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.outer {
    width: 100%;
    height: 100%;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Some may think that setting a percentage height on the element will solve the problem. This is of course, wrong. The height will be a percentage &lt;em&gt;relative to its parent height&lt;/em&gt;. Meaning the height will be completely independent of the width. That’s not what we want.&lt;/p&gt;

&lt;h2 id=&quot;a-solution&quot;&gt;A solution&lt;/h2&gt;

&lt;p&gt;You could use JavaScript. That will certainly work. You’d have to monitor resize events and set the height and width appropriately. But that’s a bit cumbersome and a CSS only solution would be much lighter on resources. What we can do instead is use something that already resizes proportionally in the browser: images! We can use an image to fill our container and force the appropriate height and width to preserve aspect ratio. Then we can position our content absolutely and have it expand to fit the width and height that the image has specified for us. This would be the code:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;div class=&quot;outer&quot;&amp;gt;
    &amp;lt;div class=&quot;inner&quot;&amp;gt;
        Lorum Ipsum
    &amp;lt;/div&amp;gt;
    &amp;lt;img src=&quot;http://placekitten.com/500/500&quot; class=&quot;scaling-img&quot; /&amp;gt; &amp;lt;!-- don&#39;t specify height and width so browser resizes it appropriately --&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;and the css:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.outer {
    position: relative;
}
.scaling-image {
    position: relative;
    visibility: hidden; /* make sure it&#39;s hidden */
    width: 100%;
    min-width: 100%;
}
.inner {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    overflow:hidden;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;So, this will work. But notice we’re loading a specific image in here that we’re not actually displaying. This uses unnecessary resources. So let’s improve on this by using data URIs.&lt;/p&gt;

&lt;h2 id=&quot;data-uri-to-the-rescue&quot;&gt;Data URI to the rescue&lt;/h2&gt;

&lt;p&gt;Data URIs, among other things, allow us to place image data inside the HTML markup. To make sure we’re loading the least resources possible, let’s try our solution with the smallest possible image, which turns out to be a plain white 1x1 pixel GIF image:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;img class=&quot;scaling-img&quot; src=&quot;data:image/gif;base64,R0lGODlhAQABAIAAAP7//wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==&quot; /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now our image comes with the aspect ratio of 1:1. In order to get different aspect radios for our content area, all we need to do is add some padding to our scaling image to get the appropriate aspect ratio. For example, if we want a 1:2 aspect ratio, we add a &lt;strong&gt;top padding of 50%&lt;/strong&gt;. If we want a 2:1 radio, we’ll have to add a &lt;strong&gt;top margin of -50%&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here is the &lt;a href=&quot;http://jsfiddle.net/wellcaffeinated/4tFKC/&quot;&gt;JSfiddle showing the full example code&lt;/a&gt; using this image to proportionally scale our content:&lt;/p&gt;

&lt;iframe style=&quot;width: 100%; height: 400px&quot; src=&quot;http://jsfiddle.net/wellcaffeinated/4tFKC/embedded/result%2Chtml%2Ccss%2Cjs/&quot; allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot;&gt; &lt;/iframe&gt;

&lt;p&gt;You’ll notice that the content is hinting at a tabular layout (like a calendar), but you can put whatever you want inside the inner element. I’ve tested this solution on &lt;strong&gt;Chrome, Safari, Firefox 3.6+, and IEs. It works on all except IE7 and below.&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&quot;errata&quot;&gt;Errata&lt;/h2&gt;

&lt;p&gt;If you start resizing this example, you’ll notice that the inner boxes don’t quite match up to fill the width and height of our &lt;code class=&quot;highlighter-rouge&quot;&gt;.inner&lt;/code&gt; element. This is due to rounding errors from the percentage calculation in CSS. If you want to avoid this, you’ll need to either use &lt;a href=&quot;flexbox&quot;&gt;CSS3 flexbox layout&lt;/a&gt;, or use this &lt;a href=&quot;equid&quot;&gt;equidistant object hack from CSS-Tricks&lt;/a&gt;.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Dancing for Science (part 2) - TEDx Waterloo Video</title>
   <link href="http://wellcaffeinated.net/articles/2012/06/23/dancing-for-science-part-2-tedx-waterloo-video"/>
   <updated>2012-06-23T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/06/23/dancing-for-science-part-2-tedx-waterloo-video</id>
   <content type="html">&lt;p&gt;Ever since we &lt;a href=&quot;/articles/swing-dancing-for-science-at-tedx-waterloo&quot;&gt;danced at TEDx Waterloo&lt;/a&gt;, I’ve been waiting for the video to get onto youtube. Especially since there were apparently technical difficulties with the live stream, and many friends weren’t able to see us dance. Fortunately, it’s finally here! (I’m especially pleased with how they’ve edited “Classical Physics” into the video).&lt;/p&gt;

&lt;div data-src=&quot;http://www.youtube.com/embed/d4970eguBJ0&quot; class=&quot;youtube&quot;&gt;&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>Irresponsible Subway Poster Bible Messages?</title>
   <link href="http://wellcaffeinated.net/articles/2012/03/27/irresponsible-subway-poster-bible-messages"/>
   <updated>2012-03-27T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/03/27/irresponsible-subway-poster-bible-messages</id>
   <content type="html">&lt;p&gt;There has recently been a trend of religious and atheist organizations circulating posters and billboards that promote their respective world views. Christian posters generally claim that &lt;a href=&quot;http://2.bp.blogspot.com/_NpQEWDE3XBg/TRThMAftZLI/AAAAAAAABsI/7wqfKxmf9kM/s1600/God+is.jpg&quot;&gt;god is great, you should love him&lt;/a&gt;, islamic posters claim that &lt;a href=&quot;http://images.smh.com.au/2011/05/27/2391014/ipad-art-wide-jesus-2-420x0.jpg&quot;&gt;Jesus is a prophet of islam&lt;/a&gt;, and atheist posters suggest that you can be a good person if you don’t believe in god. Atheist posters seem to get most of the criticism for some odd reason.&lt;/p&gt;

&lt;p&gt;From time to time there are some &lt;a href=&quot;http://s3-ec.buzzfed.com/static/imagebuzz/terminal01/2009/3/9/10/offensive-christian-billboard-18267-1236608489-2.jpg&quot;&gt;racist ones&lt;/a&gt;, some &lt;a href=&quot;http://28.media.tumblr.com/tumblr_lbsg89kaQI1qdqya3o1_400.jpg&quot;&gt;pathetic ones&lt;/a&gt;, and some &lt;a href=&quot;http://1.bp.blogspot.com/_j9oDqG2zBGE/SSBmEUKZppI/AAAAAAAACYY/IkRkchfKeEs/s400/christian+billboard+two.jpg&quot;&gt;hilarious ones&lt;/a&gt;. Today, however, it was pointed out to me that a rather irresponsible one was circulating on the subway system here in Toronto.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://yfrog.com/h8stwbmj&quot;&gt;&lt;img src=&quot;http://a.yfrog.com/img620/4164/stwbm.jpg&quot; alt=&quot;Irresponsible christian subway advertisement&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quote:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Dear Jesus, My mom and dad do drugs at home and it scares me. Will you help them stop? Thank you for hearing my prayer.&lt;/p&gt;

  &lt;p&gt;“Don’t worry about anything; instead, pray about everything. Tell God what you need, and thank him for all he has done.
If you do this you will experience God’s peace, which is far more wonderful than the human mind can understand.”
– The Bible: Philippians 40607 New Living Translation&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Apparently, children who are afraid of the parent’s drug addictions should sit quietly and talk to themselves while they remain in a dangerous environment. What I think about religion is almost irrelevant to state here. This is a totally inappropriate and dangerous message to give anyone &lt;em&gt;let alone children&lt;/em&gt;! Fortunately, I’ve been informed that action is being taken to remove this particular poster.&lt;/p&gt;

&lt;p&gt;I looked through their website which displays &lt;a href=&quot;http://www.busstopbiblestudies.com/&quot; rel=&quot;nofollow&quot;&gt;150 posters that they circulate on the Toronto subway system&lt;/a&gt;. Most of them are benign metaphysical claims and misguided life advice. If they want to put those up, fine… god bless. But some of them are genuinely questionable. Like this one:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/bsbs-violence.jpg&quot; alt=&quot;Christian poster about violence&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quote:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Dear Jesus, I’m scared of all the violence around me and my family. Will you help me please? Thank you for hearing my prayer.&lt;/p&gt;

  &lt;p&gt;“Don’t worry about anything; instead, pray about everything. […] etc.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And this one:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/bsbs-grades.jpg&quot; alt=&quot;Christian poster about school&quot; /&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Dear Jesus, My grades are slipping at school and I’m having trouble concentrating. Will you help me please? Thank you for hearing my prayer.&lt;/p&gt;

  &lt;p&gt;“Don’t worry about anything; […] etc.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sounds like that child has a learning disability. Perhaps he should ask for help from someone who exists? Or is ADD caused by lack of prayer?&lt;/p&gt;

&lt;p&gt;In any case, it’s a moot point, because apparently “A thorough knowledge of the Bible is worth more than a college education”.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/bsbs-education.jpg&quot; alt=&quot;Christian subway poster concerning the value of education&quot; /&gt;&lt;/p&gt;

&lt;p&gt;And if you get weary of life, you can just go be with Jesus… I hope they don’t mean what it looks like…&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/bsbs-life.jpg&quot; alt=&quot;Christian subway poster concerning life&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Again, I’m not saying that posters that are motivated by or contain bible quotes should not be allowed. I’m saying that the message matters, and more care should be taken to ensure that the messages any poster portrays won’t potentially cause harm to those who follow it.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Starbucks officially supports gay marriage</title>
   <link href="http://wellcaffeinated.net/articles/2012/03/26/starbucks-officially-supports-gay-marriage"/>
   <updated>2012-03-26T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/03/26/starbucks-officially-supports-gay-marriage</id>
   <content type="html">&lt;p&gt;Starbucks coffee is not my favorite, to say the least. I actually have trouble drinking their regular roasts… &lt;strong&gt;they taste burnt&lt;/strong&gt;. There, I said it. But something new is brewing at Starbucks; &lt;em&gt;support for marriage equality&lt;/em&gt;. I first heard about this from &lt;em&gt;Coffee Loving Skeptic&lt;/em&gt;, &lt;a href=&quot;http://coffeelovingskeptic.com/?p=1387&quot; title=&quot;Starbucks being boycotted for supporting gay marriage&quot;&gt;that has a nice writeup of the situation&lt;/a&gt;. I think it’s worth quoting Starbucks’ official statement:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;To: U.S. partners&lt;/p&gt;

  &lt;p&gt;Date: January 24, 2012&lt;/p&gt;

  &lt;p&gt;Re: A Message from Kalen Holmes: Starbucks Supports Marriage Equality&lt;/p&gt;

  &lt;p&gt;Dear partners,&lt;/p&gt;

  &lt;p&gt;Starbucks is proud to join other leading Northwest employers in support of Washington State legislation 
recognizing marriage equality for same-sex couples. Starbucks strives to create a company culture that 
puts our partners first, and our company has a lengthy history of leading and supporting policies that 
promote equality and inclusion.&lt;/p&gt;

  &lt;p&gt;This important legislation is aligned with Starbucks business practices and upholds our belief in the
equal treatment of partners. It is core to who we are and what we value as a company. We are proud
of our Pride Alliance Partner Network group, which is one of the largest Employer Resource Groups for
Lesbian, Gay, Bisexual and Transgender (LGBT) employees in the U.S., helping to raise awareness about
issues in the communities where we live and work.&lt;/p&gt;

  &lt;p&gt;For the last 20 years, our benefits program has offered domestic partner benefits in the U.S. These benefits
include medical, dental, vision, prescription drugs and alternative health care coverage. All partners
(part-time and full-time) in all work locations, whether in a store, a roasting plant or a corporate office,
adhere to the same eligibility requirements for health coverage and have access to the same comprehensive
health plans.&lt;/p&gt;

  &lt;p&gt;We are deeply dedicated to embracing diversity and treating one another with respect and dignity, and remain
committed to providing an inclusive, supportive and safe work environment for all of our partners.&lt;/p&gt;

  &lt;p&gt;We look forward to seeing this legislation enacted into law.&lt;/p&gt;

  &lt;p&gt;Regards,&lt;/p&gt;

  &lt;p&gt;Kalen Holmes
executive vice president, Partner Resources&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Impressive. That makes me want to buy a Starbucks coffee. (Likely one of their clover coffees that are actually pretty good, if expensive).&lt;/p&gt;

&lt;p&gt;Of course, as the saying goes, “If you aren’t pissing someone off, you aren’t doing it right”. Naturally there are a &lt;a href=&quot;http://www.nationformarriage.org/site/apps/nlnet/content2.aspx?c=omL2KeN0LzH&amp;amp;b=5075187&amp;amp;ct=11668189&amp;amp;notoc=1&quot; rel=&quot;nofollow&quot;&gt;group of bigots&lt;/a&gt; who have started a “Dump Starbucks” campaign in the name of, so called, sanctity of marriage.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“We are today announcing a sustained public campaign calling on Starbucks to stop waging war against marriage, and the views of more than half its worldwide customers.
Starbucks should be in the business of offering all its diverse customers a great cup of coffee, not taking sides against the views of its customers, vendors, and employees
around the world.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;“Waging war”, eh? I find it tiresome that the “go to” rebuttal used when there is a difference of opinion now involves declaring that the other side is waging war. I’m not gay, and marriage is not my cup of tea (coffee?), but one shouldn’t need to belong to a particular demographic to support &lt;strong&gt;equality&lt;/strong&gt;. I find it ridiculous that this is still an issue. I could go on, but it’s already been said by many people. &lt;a href=&quot;http://www.youtube.com/watch?v=tyX_uI9AdIE&quot; title=&quot;What&#39;s so queer about gay marriage?&quot;&gt;For some very well thought out arguments for marriage equality, try this lecture&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As a counter attack, there is a petition going around to thank Starbucks for making a stand on equality that betters human well-being. &lt;a href=&quot;http://sumofus.org/campaigns/thank-starbucks/?sub=taf&quot; title=&quot;Thank Starbucks for supporting gay marriage&quot;&gt;You can sign the thank-you card here&lt;/a&gt;.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>A Quantum Leap at TEDx Waterloo</title>
   <link href="http://wellcaffeinated.net/articles/2012/03/24/a-quantum-leap-at-tedx-waterloo"/>
   <updated>2012-03-24T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/03/24/a-quantum-leap-at-tedx-waterloo</id>
   <content type="html">&lt;p&gt;Fortunately, our show at TEDx Waterloo was a hit! The videos aren’t up yet, but will be soon. In the meantime, here’s the picture of our quantum leap. You can &lt;a href=&quot;http://flic.kr/p/bs82EL&quot;&gt;see some more pictures here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://flic.kr/p/bs82y3&quot;&gt;&lt;img src=&quot;http://farm8.staticflickr.com/7123/6859167582_388d629c92.jpg&quot; alt=&quot;A Quantum Leap at TEDx Waterloo 2012&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It was definitely the largest crowd I’d ever performed for. Nerves were affecting our dress rehearsals the night before. At one point we were practicing the aerial (tossing the girls in a backflip) and the lights cut out! In the end it all came together, though.&lt;/p&gt;

&lt;p&gt;If the response on twitter is any indication of success, we’re in luck. &lt;a href=&quot;https://twitter.com/#!/search/realtime/quantum%20OR%20swing%20OR%20dance%20OR%20krister%20OR%20shalm%20%23tedxwaterloo&quot;&gt;Searching twitter for some keywords you can find a slew of positive reactions&lt;/a&gt;. Some of my favorites are:&lt;/p&gt;

&lt;blockquote class=&quot;twitter-tweet&quot;&gt;&lt;p&gt;Jaw dropped when swing dancers explained quantum entanglement at &lt;a href=&quot;https://twitter.com/search/%2523TEDxwaterloo&quot;&gt;#TEDxwaterloo&lt;/a&gt;. This cartoon on my fridge!&lt;a href=&quot;http://t.co/VmsLCLbb&quot; title=&quot;http://yfrog.com/h4rfocnj&quot;&gt;yfrog.com/h4rfocnj&lt;/a&gt;&lt;/p&gt;&amp;mdash; Jane Mitchell (@janemitchell) &lt;a href=&quot;https://twitter.com/janemitchell/status/182650033756311552&quot; data-datetime=&quot;2012-03-22T02:08:56+00:00&quot;&gt;March 22, 2012&lt;/a&gt;&lt;/blockquote&gt;

&lt;blockquote class=&quot;twitter-tweet&quot;&gt;&lt;p&gt;Wishing my HS physics classes had featured Lindy Hop... amazing, Krister Shalm! &lt;a href=&quot;https://twitter.com/search/%2523tedxwaterloo&quot;&gt;#tedxwaterloo&lt;/a&gt;&lt;/p&gt;&amp;mdash; Taryn Graham (@gastronavigator) &lt;a href=&quot;https://twitter.com/gastronavigator/status/182538068673306625&quot; data-datetime=&quot;2012-03-21T18:44:01+00:00&quot;&gt;March 21, 2012&lt;/a&gt;&lt;/blockquote&gt;

&lt;blockquote class=&quot;twitter-tweet&quot;&gt;&lt;p&gt;I think that was quite honestly my favourite part of &lt;a href=&quot;https://twitter.com/search/%2523TEDxWaterloo&quot;&gt;#TEDxWaterloo&lt;/a&gt; yet!!! Awesome dance by &lt;a href=&quot;https://twitter.com/search/%2523KristerShalm&quot;&gt;#KristerShalm&lt;/a&gt; illustrating quantum entanglements&lt;/p&gt;&amp;mdash; Jacqui Laporte (@notrunningfast) &lt;a href=&quot;https://twitter.com/notrunningfast/status/182537476089446401&quot; data-datetime=&quot;2012-03-21T18:41:40+00:00&quot;&gt;March 21, 2012&lt;/a&gt;&lt;/blockquote&gt;

&lt;script src=&quot;//platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt; &lt;/script&gt;

&lt;p&gt;Hopefully this will inspire people to look deeper into the physics of quantum mechanics. Maybe the image of dancers and magic will provide some sense of familiarity with these baffling ideas. Especially for those who feel more comfortable with dance than physics. Maybe we can get some domain cross-over… dancers learning physics… and physicists learning dance.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Swing Dancing For Science at TEDx Waterloo</title>
   <link href="http://wellcaffeinated.net/articles/2012/03/20/swing-dancing-for-science-at-tedx-waterloo"/>
   <updated>2012-03-20T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/03/20/swing-dancing-for-science-at-tedx-waterloo</id>
   <content type="html">&lt;p&gt;The past few months have been a buzz of excitement and exhaustion. I, along with several fellow swing dancers, have been working with physicist and swing dancer, Krister Shalm, on a project that mixes quantum mechanics and swing dancing! This project has been particularly exciting for me because, as some may know, I share Krister’s enthusiasm for science communication, physics and Lindy Hop. Being able to mix them together makes this really fun… and makes for some well caffeinated weekends.&lt;/p&gt;

&lt;p&gt;The result of this project will involve dancing live on stage at TEDx Waterloo tomorrow. You can apparently &lt;strong&gt;&lt;a href=&quot;http://tedxwaterloo.com/home/livestream/&quot;&gt;watch it live here&lt;/a&gt; at 2:10pm (EST) on Wednesday March 21st&lt;/strong&gt;. I can’t tell you what to expect, but perhaps I can say that Krister’s talk is one of the more difficult talks logistically… so I’ll let you draw your own predictions.&lt;/p&gt;

&lt;p&gt;This talk is also the result of what has been dubbed “&lt;a href=&quot;http://www.quantumpie.com/project-q/&quot;&gt;Project Q&lt;/a&gt;”. Which has enlisted help from swing dancers around the world to help illustrate some of the key concepts going into this TED talk.&lt;/p&gt;

&lt;p&gt;Consequently, I’ve been able to apply some of my web development skills too! I built a little map that showcases the Project Q video submissions from around the globe. You can see that on the &lt;a href=&quot;http://www.quantumpie.com/project-q/&quot;&gt;Project Q page too&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Nervous? A little bit. We’ll see how it goes.&lt;/p&gt;

&lt;p&gt;Time to break out and dance…&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For Science!&lt;/strong&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Fun with JavaScript and Device Orientation</title>
   <link href="http://wellcaffeinated.net/articles/2012/03/11/fun-with-javascript-and-device-orientation"/>
   <updated>2012-03-11T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/03/11/fun-with-javascript-and-device-orientation</id>
   <content type="html">&lt;p&gt;There’s some new “black magic” in the world of JavaScript… and its name is “device orientation”. This emerging feature allows JavaScript to monitor the physical orientation of the current device. This is not to be confused with the &lt;code class=&quot;highlighter-rouge&quot;&gt;orientationchange&lt;/code&gt; event, which triggers when mobile devices change from portrait to landscape views and vice versa. No, instead, this means I can tilt my computer or phone or tablet and have JavaScript do stuff based on the way I’ve tilted it.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://upload.wikimedia.org/wikipedia/commons/thumb/a/a1/Eulerangles.svg/300px-Eulerangles.svg.png&quot; alt=&quot;Euler Angles&quot; /&gt;&lt;/p&gt;

&lt;p&gt;For a “wow” effect, take a look at this (ugly) &lt;a href=&quot;http://jsfiddle.net/wellcaffeinated/rWj3Y/embedded/result/&quot;&gt;demo I quickly whipped up&lt;/a&gt; – &lt;em&gt;works on modern browsers only&lt;/em&gt;. There is a lot of ugliness in that code, but the idea is there. I can perform a physical action (tilt my computer) and interact with the virtual content in an intuitive way. Very satisfying.&lt;/p&gt;

&lt;h2 id=&quot;how-do-i-do-this&quot;&gt;How do I do this?&lt;/h2&gt;

&lt;p&gt;Firstly, I should point out the awesome &lt;a href=&quot;http://www.html5rocks.com/en/tutorials/device/orientation/&quot;&gt;article about device orientation on html5rocks&lt;/a&gt;. It’s got enough to get you started. Basically the idea is you subscribe to a &lt;code class=&quot;highlighter-rouge&quot;&gt;deviceorientation&lt;/code&gt; event using &lt;code class=&quot;highlighter-rouge&quot;&gt;document.addEventListener()&lt;/code&gt;. The event will pass along some information about the device’s current orientation. Results will vary depending on the device and the browser.&lt;/p&gt;

&lt;p&gt;The information you can possibly have access to includes information from the accelerometer (tilt of the device) and compass information (direction on the earth). If you are familiar with &lt;a href=&quot;http://en.wikipedia.org/wiki/Euler_angles&quot;&gt;Euler angles&lt;/a&gt;, then that will help. The information that the event passes back is essentially the Euler angles. Here’s a &lt;a href=&quot;http://www.youtube.com/watch?v=UpSMNYTVqQI&quot;&gt;video showing a quick demo&lt;/a&gt; of what Euler angles measure.&lt;/p&gt;

&lt;p&gt;In my jsFiddle above, what I’m doing is monitoring the angle &lt;em&gt;gamma&lt;/em&gt; which corresponds to the left-right tilt of your device. Once I have that angle, I use some easy trigonometry to figure out the horizontal and vertical acceleration of virtual gravity relative to the viewport.&lt;/p&gt;

&lt;p&gt;Still interested? Ok. &lt;code class=&quot;highlighter-rouge&quot;&gt;data.lr * Math.PI / 180&lt;/code&gt; converts the angle from degrees to radians so that I can put it into &lt;code class=&quot;highlighter-rouge&quot;&gt;sin()&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;cos()&lt;/code&gt;. Those in turn, give me the amount of gravity in the horizontal and vertical directions when I multiply them by my total (virtual) gravity.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ay = a * Math.cos(data.lr * Math.PI / 180);
ax = a * Math.sin(data.lr * Math.PI / 180);
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;free-code-anyone&quot;&gt;Free code, anyone?&lt;/h2&gt;

&lt;p&gt;I’ve been playing around with this device orientation voodoo for a little while and decided to write some AMD modules to make my life easier. One of them basically copies the code from &lt;em&gt;html5rocks&lt;/em&gt;. The other (called device-gravity) gives you the projection of gravity along your device. It will give you the magnitude, angle, and x and y components.&lt;/p&gt;

&lt;p&gt;“Projection of gravity? What does that do?”&lt;/p&gt;

&lt;p&gt;I’m glad you asked. Imagine placing your laptop or tablet down on a level surface. Now place a ball on it. Right now  – if the surface is very level – the ball won’t move. That means the projection of gravity is &lt;em&gt;zero&lt;/em&gt; along the surface of your device. In other words, gravity points directly into the surface of your device. If you tilt your device, then the ball starts to accelerate. The projection of gravity  along your device is the direction the ball moves, and the magnitude of the projection is the amount at which it accelerates.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/demos/device-orientation&quot;&gt;Maybe a little demo is in order&lt;/a&gt;. This demo shows a projection-o-meter that shows you the projection of gravity. It also shows another example of what you can do with device orientation.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/wellcaffeinated/Orientation&quot;&gt;The code and documentation is on GitHub&lt;/a&gt;. Feedback is most welcome.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>A Responsive JavaScript Event Binding Method</title>
   <link href="http://wellcaffeinated.net/articles/2012/02/19/a-responsive-javascript-event-binding-method"/>
   <updated>2012-02-19T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/02/19/a-responsive-javascript-event-binding-method</id>
   <content type="html">&lt;p&gt;I’ve recently been doing a lot of &lt;a href=&quot;http://www.alistapart.com/articles/responsive-web-design/&quot;&gt;responsive web development&lt;/a&gt; work. I was building a component that could have several different states depending on the size of the viewport. Each of these states dictated different user interaction. For example, a mouse move interaction in one state could turn into a mouse click interaction in another. So what I needed was a way to easily activate and deactivate JavaScript events depending on the viewport size.&lt;/p&gt;

&lt;p&gt;One way to do this (that isn’t so pretty) would be to monitor the viewport size and unbind and rebind the appropriate events between certain viewport size breakpoints. Similarly, you could manually add &lt;code class=&quot;highlighter-rouge&quot;&gt;if()&lt;/code&gt; statements in the event callbacks to only use the callback if the breakpoint was the correct one. Neither of these solutions seemed easy enough to manage for me, so I came up with a more elegant solution.&lt;/p&gt;

&lt;p&gt;The solution I came up with uses the &lt;a href=&quot;https://developer.mozilla.org/en/DOM/window.matchMedia&quot;&gt;window.matchMedia()&lt;/a&gt; method and &lt;a href=&quot;http://api.jquery.com/on/&quot;&gt;jQuery 1.7 .on()&lt;/a&gt; event delegation. The idea in a nutshell is to add a listener to a media query using &lt;code class=&quot;highlighter-rouge&quot;&gt;matchMedia()&lt;/code&gt; and add a classname corresponding to the state of your component. Then by delegating events based on that state classname the events will automatically be disabled when that classname changes.&lt;/p&gt;

&lt;p&gt;First, let’s start with some very simple HTML markup:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;div id=&quot;my-component&quot;&amp;gt;
    &amp;lt;div class=&quot;inner&quot;&amp;gt;
        &amp;lt;div class=&quot;call-to-action&quot;&amp;gt;
            Do Stuff...
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;So here &lt;code class=&quot;highlighter-rouge&quot;&gt;#my-component&lt;/code&gt; is the main container of your component, &lt;code class=&quot;highlighter-rouge&quot;&gt;.inner&lt;/code&gt; is an inner wrapper that we are going to add the state class names to, and &lt;code class=&quot;highlighter-rouge&quot;&gt;.call-to-action&lt;/code&gt; is what we’ll be expecting interaction events from. Now lets go to the javascript and define some states:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;var breakpoints = {
    bp1: &#39;screen and (min-width: 0px) and (max-width: 320px)&#39;,
    bp2: &#39;screen and (min-width: 321px)&#39;
    //etc...
};
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;So here we have two states. The first will be active in a viewport with a width less than 320px and the second will be active in widths larger than 321px. What we need to do now is use matchMedia to listen for these viewport changes and in the callback add a class name to the &lt;code class=&quot;highlighter-rouge&quot;&gt;.inner&lt;/code&gt; element which corresponds to the currently active state. We do this by using the &lt;code class=&quot;highlighter-rouge&quot;&gt;.addListener()&lt;/code&gt; method.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;for ( var name in breakpoints ){

    // need to scope variables in a for loop
    !function(breakName, query){

        // the callback
        function cb(data){
            // add class name associated to current breakpoint match
            $( &#39;#my-component .inner&#39; ).toggleClass( breakName, data.matches );
            // potentially do other stuff if you want...
        }

        // run the callback on current viewport
        cb({
            media: query,
            matches: matchMedia(query).matches
        });

        // subscribe to breakpoint changes
        matchMedia(query).addListener( cb );

    }(name, breakpoints[name]);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now whenever we change between these viewport ranges the &lt;code class=&quot;highlighter-rouge&quot;&gt;.inner&lt;/code&gt; element will have a class name of either &lt;code class=&quot;highlighter-rouge&quot;&gt;.bp1&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;.bp2&lt;/code&gt;. All we need to do now is delegate the events we want based on these class names. To do this we use jQuery’s &lt;code class=&quot;highlighter-rouge&quot;&gt;.on()&lt;/code&gt; with a query string that scopes the &lt;code class=&quot;highlighter-rouge&quot;&gt;.call-to-action&lt;/code&gt; within either &lt;code class=&quot;highlighter-rouge&quot;&gt;.bp1&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;.bp2&lt;/code&gt;, like so:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$( &#39;#my-component&#39; )
.on({

        //click events
        click: function(e){ 
            $(this).html(&#39;You clicked&#39;);
        }

    },

    //query string to match .call-to-action but only in first breakpoint
    &#39;.bp1 .call-to-action&#39;
)
.on({

        //mouse events
        mouseenter: function(e){
            $(this).html(&#39;You mouseentered&#39;);
        },

        mouseleave: function(e){ 
            $(this).html(&#39;You mouse...left...&#39;);
        }

    },

    //query string to match .call-to-action on all EXCEPT first breakpoint
    &#39;.bp2 .call-to-action&#39;
);
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And voila. Now these event callbacks will automatically only work inside their appropriate breakpoints. Adding more events or breakpoints becomes very easy to manage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Unfortunately there are a few problems with the &lt;code class=&quot;highlighter-rouge&quot;&gt;window.matchMedia()&lt;/code&gt; implementation, as Nicholas C. Zakas outlines in &lt;a href=&quot;http://www.nczonline.net/blog/2012/01/19/css-media-queries-in-javascript-part-2/&quot;&gt;his informative article&lt;/a&gt;. Fortunately, in the same article, he provides a solution to these problems through the implementation of a YUI module… which can easily be ported to jQuery or anything else. In future I’ll provide a jQuery implementation based on Zakas’ code.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://jsfiddle.net/wellcaffeinated/5N2Dz/3/&quot;&gt;Here’s a jsFiddle&lt;/a&gt; of the above code so you can see how it works. I have used Zakas’ fixes for firefox, so there are a few lines of extra JS in there to deal with that. Try resizing the result screen and clicking and mousing over!&lt;/p&gt;

&lt;iframe style=&quot;width: 100%; height: 300px&quot; src=&quot;http://jsfiddle.net/wellcaffeinated/5N2Dz/3/embedded/result,js,html,css&quot; allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot;&gt; &lt;/iframe&gt;
</content>
 </entry>
 
 <entry>
   <title>Font Smoothing Detection... Modernizr Style!</title>
   <link href="http://wellcaffeinated.net/articles/2012/01/25/font-smoothing-detection-modernizr-style"/>
   <updated>2012-01-25T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/01/25/font-smoothing-detection-modernizr-style</id>
   <content type="html">&lt;p&gt;I recently have been playing around with &lt;a href=&quot;http://www.google.com/webfonts&quot;&gt;Google Webfonts&lt;/a&gt; which allow you much more freedom in font selection on the web… for free. I’ve been using Google Webfonts on this very website, which is why the fonts are so pretty… at least I think so.&lt;/p&gt;

&lt;p&gt;There is one downside though. Sometimes you’ll find that awesome font you want to use, and it looks &lt;strong&gt;great&lt;/strong&gt; in OSX, but when you check how it looks on Windows… you can be quite disappointed.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/images/font-smoothing.png&quot;&gt;&lt;img alt=&quot;Comparison of wellcaffeinated.net as viewed on chrome in OSX and Windows&quot; src=&quot;/images/font-smoothing.png&quot; width=&quot;100%&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;whats-going-on&quot;&gt;What’s going on!?&lt;/h2&gt;

&lt;p&gt;Well, it turns out Windows and OSX have different &lt;em&gt;default&lt;/em&gt; ways of rendering fonts. Roughly speaking, &lt;a href=&quot;http://en.wikipedia.org/wiki/Glyph&quot;&gt;glyphs&lt;/a&gt; of a particular font are stored as black pixels or white pixels. On a high resolution medium this isn’t a problem… but on a computer screen, simply using black and white pixels can lead to jagged edges.&lt;/p&gt;

&lt;p&gt;This is where font smoothing comes in. Font smoothing algorithms use semi-transparent pixels to smooth out these jagged edges. Whereas OSX uses something called Quartz font smoothing, Windows, it seems by default doesn’t use any font smoothing. (Although you can turn it on by going to Control Panel-&amp;gt;Display-&amp;gt;Appearance-&amp;gt;Effects on XP, for example).&lt;/p&gt;

&lt;h2 id=&quot;how-do-i-make-my-fonts-look-nice-on-the-web&quot;&gt;How do I make my fonts look nice on the web?&lt;/h2&gt;

&lt;p&gt;Fortunately, there’s a way to detect font smoothing using a little javascript. This method was the product of some &lt;a href=&quot;http://www.useragentman.com/blog/2009/11/29/how-to-detect-font-smoothing-using-javascript/trackback&quot;&gt;great investigation by Zoltan Hawryluk&lt;/a&gt;, a strong proponent for better web typography.&lt;/p&gt;

&lt;p&gt;The code he’s provided is great and works out of the box… but I, being a fan of &lt;a href=&quot;http://modernizr.com&quot;&gt;Modernizr&lt;/a&gt;, wanted a way to detect font smoothing as a Modernizer test. That way, I could just plug the test into my head of my website and use css classes to display particular typefaces depending on whether the user has font smoothing enabled.&lt;/p&gt;

&lt;p&gt;I’ve rewritten the font smoothing detection algorithm as a Modernizr test you can cut and paste into your website. Without further ado, here it is:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// The following is adapted from...

/*
 * TypeHelpers version 1.0
 * Zoltan Hawryluk, Nov 24 2009.  
 * 
 * Released under the MIT License. http://www.opensource.org/licenses/mit-license.php
 * 
 */

Modernizr.addTest(&#39;fontsmoothing&#39;, function() {
    // IE has screen.fontSmoothingEnabled - sweet!      
    if (typeof(screen.fontSmoothingEnabled) != &quot;undefined&quot;) {
        return screen.fontSmoothingEnabled;
    } else {
        try {
            // Create a 35x35 Canvas block.
            var canvasNode = document.createElement(&quot;canvas&quot;)
              , test = false
              , fake = false
              , root = document.body || (function () {
                    fake = true;
                    return document.documentElement.appendChild(document.createElement(&#39;body&#39;));
              }());
            
            canvasNode.width = &quot;35&quot;;
            canvasNode.height = &quot;35&quot;

            // We must put this node into the body, otherwise 
            // Safari Windows does not report correctly.
            canvasNode.style.display = &quot;none&quot;;
            root.appendChild(canvasNode);
            var ctx = canvasNode.getContext(&quot;2d&quot;);

            // draw a black letter &quot;O&quot;, 32px Arial.
            ctx.textBaseline = &quot;top&quot;;
            ctx.font = &quot;32px Arial&quot;;
            ctx.fillStyle = &quot;black&quot;;
            ctx.strokeStyle = &quot;black&quot;;

            ctx.fillText(&quot;O&quot;, 0, 0);

            // start at (8,1) and search the canvas from left to right,
            // top to bottom to see if we can find a non-black pixel.  If
            // so we return true.
            for (var j = 8; j &amp;lt;= 32; j++) {
                for (var i = 1; i &amp;lt;= 32; i++) {
                    var imageData = ctx.getImageData(i, j, 1, 1).data;
                    var alpha = imageData[3];

                    if (alpha != 255 &amp;amp;&amp;amp; alpha != 0) {
                        test = true; // font-smoothing must be on.
                        break;
                    }
                }
            }
            
            //clean up
            root.removeChild(canvasNode);
            if (fake) {
                document.documentElement.removeChild(root);
            }
            
            return test;
        }
        catch (ex) {
            root.removeChild(canvasNode);
            if (fake) {
                document.documentElement.removeChild(root);
            }
            // Something went wrong (for example, Opera cannot use the
            // canvas fillText() method.  Return false.
            return false;
        }
    }
});
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And &lt;a href=&quot;http://jsfiddle.net/wellcaffeinated/tYyqA/&quot;&gt;here it is on jsFiddle&lt;/a&gt; with an example usage:&lt;/p&gt;

&lt;iframe style=&quot;width: 100%; height: 300px&quot; src=&quot;http://jsfiddle.net/wellcaffeinated/tYyqA/embedded/result,js,html,css&quot; allowfullscreen=&quot;allowfullscreen&quot; frameborder=&quot;0&quot;&gt; &lt;/iframe&gt;

&lt;p&gt;I haven’t yet implemented this on my website, but expect prettier fonts for windows machines (without font smoothing) soon…&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>The Ten Secular Commandments (dot com)</title>
   <link href="http://wellcaffeinated.net/articles/2012/01/20/the-ten-secular-commandments-dot-com"/>
   <updated>2012-01-20T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2012/01/20/the-ten-secular-commandments-dot-com</id>
   <content type="html">&lt;p&gt;Amidst the craziness that is “work”, I have managed to find the time, not only to build one website (this one), but TWO! Behold, &lt;a href=&quot;http://thetensecularcommandments.com&quot;&gt;The Ten Secular Commandments&lt;/a&gt;. It’s still in the works, but it’s there, and functional.&lt;/p&gt;

&lt;h2 id=&quot;why&quot;&gt;Why?&lt;/h2&gt;

&lt;p&gt;Well, it started when I heard about the &lt;em&gt;Richard Dawkins Foundation&lt;/em&gt; &lt;a href=&quot;http://richarddawkins.net/articles/644208&quot;&gt;Social Media Contest&lt;/a&gt;… or Video Contest… I don’t know what they’re officially calling it. The idea came after seeing one of the &lt;a href=&quot;http://www.youtube.com/watch?v=sGuUPnmooro&quot;&gt;promotional videos&lt;/a&gt;, which described the contest as an effort to come up with a new version of the Ten Commandments. So I thought, &lt;em&gt;who am I to develop ten commandments that govern anybody?&lt;/em&gt; It makes much more sense that these commandments be constructed collaboratively – this is the “age of the interface” after all. So it seemed natural that I use one of my primary skills to help… web development.&lt;/p&gt;

&lt;p&gt;Quoting from the &lt;em&gt;about&lt;/em&gt; section of the website,&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The Ten Secular Commandments is a living document, giving the free-thinking community a forum for creating moral guidelines that draws on mankind’s 
greatest innovation: the scientific method. It allows people to collaboratively suggest, discuss, and continually tweak a set of commandments so as to 
create a moral framework that isn’t derived from doctrine.&lt;/p&gt;

  &lt;p&gt;These commandments can change, as new evidence is uncovered about the human condition and how best to maximize well-being.&lt;/p&gt;

  &lt;p&gt;They are participatory, obliging no one to be governed by any rules they could not help create.&lt;/p&gt;

  &lt;p&gt;They are peer-reviewed. Users can make critiques, suggest modifications, or recommend/disagree with posts and suggested commandments submitted by others.&lt;/p&gt;

  &lt;p&gt;They will not be policed. We hope that a framework will emerge that is generally appealing to everyone to follow.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It would be nice to win… but I more hope the website gets used and proves to be a productive effort for a cause I care about.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Simple Javascript Templating</title>
   <link href="http://wellcaffeinated.net/articles/2011/11/24/simple-javascript-templating"/>
   <updated>2011-11-24T00:00:00+00:00</updated>
   <id>http://wellcaffeinated.net/articles/2011/11/24/simple-javascript-templating</id>
   <content type="html">&lt;p&gt;Suppose you need just a little bit of templating functionality, here’s a code snippet for you:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/**
 * Very simple find/replace template.
 *
 * Optional filter function that is used on tags with three curlies. Ex: `}`
 * 
 * @function {mixin} Application.template
 * @param {string} t templateable string
 * @param {object} d data object
 * @... {string} yourKey containing value to replace
 * @param {function(value)} filter a filter function that is passed members of `d` that are templated by triple curlys
 * @return {string} string with templates filled
 * @author [Jasper Palfree](http://wellcaffeinated.net)
 * @license [GPLv3](http://www.gnu.org/licenses/)
 * @example
 * This will return the text: `this is a template!`
 *
 *

 * 	template(&quot;this is a &quot;, {what: &quot;template!&quot;});
 * 	
 * @example
 * This will return the text: `this is a template!`
 *
 *

 * 	template(&quot;this is a &quot;, {nested: {what:&#39;template!&#39;} });
 */
function template(t, d, filter){
	var data = d;
	var ret = t;
	
	var segments = ret.match(/\\{?\\{\\{[a-zA-Z$_][a-zA-Z0-9_.$]*\\}\\}\\}?/g);
	for(var x=0, len = segments.length; x &amp;lt; len; x++){
                    var tags = segments[x].replace(/[\\{\\}]*/g,&#39;).split(&#39;.&#39;);
		var id = tags.shift();
                    var repl = (tags.length &amp;gt; 0)? arguments.callee(&#39;+tags.join(&#39;,data[id],filter) : &#39;+data[id];
		if(segments[x].match(/^\\{\\{\\{/) &amp;amp;&amp;amp; typeof filter === &#39;function&#39;){
			repl = filter(repl);
		}
		ret = ret.replace(segments[x], repl);
	}
	
	return ret;
};
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;what-can-you-do-with-it&quot;&gt;What can you do with it?&lt;/h2&gt;

&lt;p&gt;Well, let’s say you have some data in javascript that you want to put into the DOM.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// mimicking retrieval from data source
var entries = [
    {id:&#39;1&#39;, data: {title:&quot;Title1&quot;, content:&quot;&amp;lt;p&amp;gt;This is an entry&amp;lt;/p&amp;gt;&quot;}},
    {id:&#39;2&#39;, data: {title:&quot;Title2&quot;, content:&quot;&amp;lt;p&amp;gt;This is another entry&amp;lt;/p&amp;gt;&quot;}},
    {id:&#39;3&#39;, data: {title:&quot;Title3&quot;, content:&quot;&amp;lt;p&amp;gt;This is yet another fracking entry&amp;lt;/p&amp;gt;&quot;}}
];
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;What you can do is set up a template in your HTML, by placing it in script tags and specify a type such as &lt;code class=&quot;highlighter-rouge&quot;&gt;type=&quot;text/html&quot;&lt;/code&gt;. For example, you may do something like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;script type=&quot;text/html&quot; id=&quot;tpl-entry&quot;&amp;gt;
    &amp;lt;div class=&quot;entry&quot;&amp;gt;
        &amp;lt;h2&amp;gt;&amp;lt;/h2&amp;gt;
        &amp;lt;div class=&quot;content&quot;&amp;gt;
            }
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
 &amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now you can retrieve the inner html of that script tag and use it as the template for your data, like so:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;var el = document.getElementById(&#39;entryWrap&#39;),
    tpl = document.getElementById(&#39;tpl-entry&#39;).innerHTML,
    html = &#39;;
for(var x in entries){
    html += template(tpl, entries[x]);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;If you like, you can also add a filter function that can filter any data in the template called with a triple-curly.&lt;/p&gt;

&lt;p&gt;Here’s the full &lt;a href=&quot;http://jsfiddle.net/wellcaffeinated/szASd/&quot;&gt;example usage on jsFiddle&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 
</feed>