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

<channel>
	<title>zdima.net &#187; HTML5</title>
	<atom:link href="http://zdima.net/blog/archives/tag/html5/feed" rel="self" type="application/rss+xml" />
	<link>http://zdima.net/blog</link>
	<description></description>
	<lastBuildDate>Tue, 22 May 2012 23:28:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>The Prototype Pattern &#8211; Techniques, Strategies and Patterns for Structuring JavaScript Code</title>
		<link>http://zdima.net/blog/archives/16371</link>
		<comments>http://zdima.net/blog/archives/16371#comments</comments>
		<pubDate>Mon, 01 Aug 2011 22:02:00 +0000</pubDate>
		<dc:creator>dwahlin</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Contributors]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[HTML5]]></category>

		<guid isPermaLink="false">http://www.zdima.net/blog/?guid=647267fd8672d12e52a1c3ecb31f6638</guid>
		<description><![CDATA[Using the JavaScript Prototype Pattern  This is the 2nd post in a series on techniques, strategies and patterns for writing JavaScript code. In my previous post I introduced what I call “function spaghetti code” and explained some of the problems i...<p class="read-more"><a href="http://zdima.net/blog/archives/16371">> Read more</a></p>]]></description>
			<content:encoded><![CDATA[<h2>Using the JavaScript Prototype Pattern</h2>
<p>This is the <a href="http://weblogs.asp.net/dwahlin/archive/2011/07/31/techniques-strategies-and-patterns-for-structuring-javascript-code.aspx">2nd post in a series</a> on techniques, strategies and patterns for writing JavaScript code. In my previous post I introduced what I call “function spaghetti code” and explained some of the problems it introduces. I also talked about the impact of global variables and how closures add a much needed solution. In this post I’ll introduce the Prototype Pattern and show how it relies on built-in functionality in the JavaScript language.</p>
<p>I’ve worked through some of the patterns I’m covering in this series with my good friend <a href="http://www.johnpapa.net">John Papa</a> over Skype and we’ve talked about pros and cons of different approaches. For example, he initially recommended defining multiple variables using a single var based on JSLint suggestions (that was covered in the <a href="http://weblogs.asp.net/dwahlin/archive/2011/07/31/techniques-strategies-and-patterns-for-structuring-javascript-code.aspx">previous post</a>) and I agreed that it was a better approach than using var for every variable. It’s interesting how code evolves with feedback from multiple people. John and I have definitely come to enjoy working with a few different patterns and although the Prototype Pattern isn’t in my favorites list, I’ve always felt that it’s good to understand and know the different options out there. That’s why I decided to include it in this series.</p>
<p>The Prototype Pattern can be broken out into two main sections including a constructor section and a prototype section. Prototyping allows functions and properties to be associated with objects. However, instead of each object instance getting a copy of all functions/properties each time an object is created, only one set of functions/properties exists across all objects resulting in less memory consumption. In other words, functions and properties are defined once per prototype rather than once per object.</p>
<p>As a quick review, I showed the following code in a previous post. The code simply lists all functions directly with no encapsulation and defines several global variables. While the code works fine this way, I’ll examine how we can restructure it to follow the Prototype Pattern.    </p>
<pre>window.onload = <span style="color:blue">function </span>() {
    eqCtl = document.getElementById(<span style="color:maroon">'eq'</span>);
    currNumberCtl = document.getElementById(<span style="color:maroon">'currNumber'</span>);
};

<span style="color:blue">var </span>eqCtl,
    currNumberCtl,
    operator,
    operatorSet = <span style="color:blue">false</span>,
    equalsPressed = <span style="color:blue">false</span>,
    lastNumber = <span style="color:blue">null</span>;

<span style="color:blue">function </span>add(x,y) {
    <span style="color:blue">return </span>x + y;
}

<span style="color:blue">function </span>subtract(x, y) {
    <span style="color:blue">return </span>x - y;
}

<span style="color:blue">function </span>multiply(x, y) {
    <span style="color:blue">return </span>x * y;
}

<span style="color:blue">function </span>divide(x, y) {
    <span style="color:blue">if </span>(y == 0) {
        alert(<span style="color:maroon">&quot;Can&#39;t divide by 0&quot;</span>);
        <span style="color:blue">return </span>0;
    }
    <span style="color:blue">return </span>x / y;
}

<span style="color:blue">function </span>setVal(val) {
    currNumberCtl.innerHTML = val;
}

<span style="color:blue">function </span>setEquation(val) {
    eqCtl.innerHTML = val;
}

<span style="color:blue">function </span>clearNumbers() {
    lastNumber = <span style="color:blue">null</span>;
    equalsPressed = operatorSet = <span style="color:blue">false</span>;
    setVal(<span style="color:maroon">'0'</span>);
    setEquation(<span style="color:maroon">''</span>);
}

<span style="color:blue">function </span>setOperator(newOperator) {
    <span style="color:blue">if </span>(newOperator == <span style="color:maroon">'='</span>) {
        equalsPressed = <span style="color:blue">true</span>;
        calculate();
        setEquation(<span style="color:maroon">''</span>);
        <span style="color:blue">return</span>;
    }

    <span style="color:#006400">//Handle case where = was pressed
    //followed by an operator (+, -, *, /)
    </span><span style="color:blue">if </span>(!equalsPressed) calculate();
    equalsPressed = <span style="color:blue">false</span>;
    operator = newOperator;
    operatorSet = <span style="color:blue">true</span>;
    lastNumber = parseFloat(currNumberCtl.innerHTML);
    <span style="color:blue">var </span>eqText = (eqCtl.innerHTML == <span style="color:maroon">''</span>) ?
        lastNumber + <span style="color:maroon">' ' </span>+ operator + <span style="color:maroon">' ' </span>:
        eqCtl.innerHTML + <span style="color:maroon">' ' </span>+ operator + <span style="color:maroon">' '</span>;
    setEquation(eqText);
}

<span style="color:blue">function </span>numberClick(e) {
    <span style="color:blue">var </span>button = (e.target) ? e.target : e.srcElement;
    <span style="color:blue">if </span>(operatorSet == <span style="color:blue">true </span>|| currNumberCtl.innerHTML == <span style="color:maroon">'0'</span>) {
        setVal(<span style="color:maroon">''</span>);
        operatorSet = <span style="color:blue">false</span>;
    }
    setVal(currNumberCtl.innerHTML + button.innerHTML);
    setEquation(eqCtl.innerHTML + button.innerHTML);
}

<span style="color:blue">function </span>calculate() {
    <span style="color:blue">if </span>(!operator || lastNumber == <span style="color:blue">null</span>) <span style="color:blue">return</span>;
    <span style="color:blue">var </span>currNumber = parseFloat(currNumberCtl.innerHTML),
        newVal = 0;
    <span style="color:#006400">//eval() would&#39;ve made this a whole lot simpler
    //but didn&#39;t want to use it in favor of a more
    //&quot;robust&quot; set of methods to demo patterns
    </span><span style="color:blue">switch </span>(operator) {
        <span style="color:blue">case </span><span style="color:maroon">'+'</span>:
            newVal = add(lastNumber, currNumber);
            <span style="color:blue">break</span>;
        <span style="color:blue">case </span><span style="color:maroon">'-'</span>:
            newVal = subtract(lastNumber, currNumber);
            <span style="color:blue">break</span>;
        <span style="color:blue">case </span><span style="color:maroon">'*'</span>:
            newVal = multiply(lastNumber, currNumber);
            <span style="color:blue">break</span>;
        <span style="color:blue">case </span><span style="color:maroon">'/'</span>:
            newVal = divide(lastNumber, currNumber);
            <span style="color:blue">break</span>;
    }
    setVal(newVal);
    lastNumber = newVal;
}</pre>
<p> </p>
<p>To start using the Prototype Pattern you need to first create a constructor as shown next. The constructor can accept one or more parameters and define any variables that the object needs. Note that the variables are scoped to the object rather than to the global scope. </p>
<p></p>
<pre><span style="color:blue">var </span>Calculator = <span style="color:blue">function </span>(tb, eq) {
    <span style="color:blue">this</span>.eqCtl = document.getElementById(eq);
    <span style="color:blue">this</span>.currNumberCtl = document.getElementById(tb);
    <span style="color:blue">this</span>.operator = <span style="color:blue">null</span>;
    <span style="color:blue">this</span>.operatorSet = <span style="color:blue">false</span>;
    <span style="color:blue">this</span>.equalsPressed = <span style="color:blue">false</span>;
    <span style="color:blue">this</span>.lastNumber = <span style="color:blue">null</span>;
};</pre>
<p></p>
<p>
  <br />Once the constructor is defined a prototype can be created using the prototype keyword. Although you can create a prototype for each function, it’s more convenient (and less typing) to take advantage of JSON-style syntax where the function name represents the JSON property name and the value represents the function. An example of defining two functions in a prototype is shown next. Notice that each function is separated with a comma just as you would separate properties defined in a normal JSON object. It&#8217;s officially called a JavaScript object literal, but if you&#8217;re familiar with JSON it uses the same standard syntax (thanks to Craig Stuntz for clarifying that): </p>
<p></p>
<pre>Calculator.prototype = {

    add: <span style="color:blue">function </span>(x, y) {
        <span style="color:blue">return </span>x + y;
    },

    subtract: <span style="color:blue">function </span>(x, y) {
        <span style="color:blue">return </span>x - y;
    }
}</pre>
<p>
  <br />The original function-based calculator code shown earlier can be refactored to follow the Prototype Pattern as shown shown next. A prototype is created for the Calculator object and functions/properties are defined within the prototype using JavaScript object literal syntax. </p>
<pre>Calculator.prototype = {

    add: <span style="color:blue">function </span>(x, y) {
        <span style="color:blue">return </span>x + y;
    },

    subtract: <span style="color:blue">function </span>(x, y) {
        <span style="color:blue">return </span>x - y;
    },

    multiply: <span style="color:blue">function </span>(x, y) {
        <span style="color:blue">return </span>x * y;
    },

    divide: <span style="color:blue">function </span>(x, y) {
        <span style="color:blue">if </span>(y == 0) {
            alert(<span style="color:maroon">&quot;Can&#39;t divide by 0&quot;</span>);
        }
        <span style="color:blue">return </span>x / y;
    },

    setVal: <span style="color:blue">function</span>(val) {
        <span style="color:blue">this</span>.currNumberCtl.innerHTML = val;
    },

    setEquation: <span style="color:blue">function </span>(val) {
        <span style="color:blue">this</span>.eqCtl.innerHTML = val;
    },

    clearNumbers: <span style="color:blue">function </span>() {
        <span style="color:blue">this</span>.lastNumber = <span style="color:blue">null</span>;
        <span style="color:blue">this</span>.equalsPressed = <span style="color:blue">this</span>.operatorSet = <span style="color:blue">false</span>;
        <span style="color:blue">this</span>.setVal(<span style="color:maroon">'0'</span>);
        <span style="color:blue">this</span>.setEquation(<span style="color:maroon">''</span>);
    },

    setOperator: <span style="color:blue">function </span>(newOperator) {
        <span style="color:blue">if </span>(newOperator == <span style="color:maroon">'='</span>) {
            <span style="color:blue">this</span>.equalsPressed = <span style="color:blue">true</span>;
            <span style="color:blue">this</span>.calculate();
            <span style="color:blue">this</span>.setEquation(<span style="color:maroon">''</span>);
            <span style="color:blue">return</span>;
        }

        <span style="color:#006400">//Handle case where = was pressed
        //followed by an operator (+, -, *, /)
        </span><span style="color:blue">if </span>(!<span style="color:blue">this</span>.equalsPressed) <span style="color:blue">this</span>.calculate();
        <span style="color:blue">this</span>.equalsPressed = <span style="color:blue">false</span>;
        <span style="color:blue">this</span>.operator = newOperator;
        <span style="color:blue">this</span>.operatorSet = <span style="color:blue">true</span>;
        <span style="color:blue">this</span>.lastNumber = parseFloat(<span style="color:blue">this</span>.currNumberCtl.innerHTML);
        <span style="color:blue">var </span>eqText = (<span style="color:blue">this</span>.eqCtl.innerHTML == <span style="color:maroon">''</span>) ?
            <span style="color:blue">this</span>.lastNumber + <span style="color:maroon">' ' </span>+ <span style="color:blue">this</span>.operator + <span style="color:maroon">' ' </span>:
            <span style="color:blue">this</span>.eqCtl.innerHTML + <span style="color:maroon">' ' </span>+ <span style="color:blue">this</span>.operator + <span style="color:maroon">' '</span>;
        <span style="color:blue">this</span>.setEquation(eqText);
    },

    numberClick: <span style="color:blue">function </span>() {
        <span style="color:blue">var </span>button = (event.target) ? event.target : event.srcElement;
        <span style="color:blue">if </span>(<span style="color:blue">this</span>.operatorSet == <span style="color:blue">true </span>|| <span style="color:blue">this</span>.currNumberCtl.innerHTML == <span style="color:maroon">'0'</span>) {
            <span style="color:blue">this</span>.setVal(<span style="color:maroon">''</span>);
            <span style="color:blue">this</span>.operatorSet = <span style="color:blue">false</span>;
        }
        <span style="color:blue">this</span>.setVal(<span style="color:blue">this</span>.currNumberCtl.innerHTML + button.innerHTML);
        <span style="color:blue">this</span>.setEquation(<span style="color:blue">this</span>.eqCtl.innerHTML + button.innerHTML);
    },

    calculate: <span style="color:blue">function </span>() {
        <span style="color:blue">if </span>(!<span style="color:blue">this</span>.operator || <span style="color:blue">this</span>.lastNumber == <span style="color:blue">null</span>) <span style="color:blue">return</span>;
        <span style="color:blue">var </span>displayedNumber = parseFloat(<span style="color:blue">this</span>.currNumberCtl.innerHTML)
            newVal = 0;
        <span style="color:#006400">//eval() would&#39;ve made this a whole lot simpler
        //but didn&#39;t want to use it in favor of a more
        //&quot;robust&quot; set of methods to demo patterns
        </span><span style="color:blue">switch </span>(<span style="color:blue">this</span>.operator) {
            <span style="color:blue">case </span><span style="color:maroon">'+'</span>:
                newVal = <span style="color:blue">this</span>.add(<span style="color:blue">this</span>.lastNumber, displayedNumber);
                <span style="color:blue">break</span>;
            <span style="color:blue">case </span><span style="color:maroon">'-'</span>:
                newVal = <span style="color:blue">this</span>.subtract(<span style="color:blue">this</span>.lastNumber, displayedNumber);
                <span style="color:blue">break</span>;
            <span style="color:blue">case </span><span style="color:maroon">'*'</span>:
                newVal = <span style="color:blue">this</span>.multiply(<span style="color:blue">this</span>.lastNumber, displayedNumber);
                <span style="color:blue">break</span>;
            <span style="color:blue">case </span><span style="color:maroon">'/'</span>:
                newVal = <span style="color:blue">this</span>.divide(<span style="color:blue">this</span>.lastNumber, displayedNumber);
                <span style="color:blue">break</span>;
        }
        <span style="color:blue">this</span>.setVal(newVal);
        <span style="color:blue">this</span>.lastNumber = newVal;
    }
};</pre>
<p>
  <br />To use the Calculator object, create a new instance and pass the names of the HTML container objects into the constructor (the container objects identify the IDs of controls used to display calculations – see the HTML code below): </p>
<p></p>
<pre><span style="color:blue">var </span>calc = <span style="color:blue">null</span>;
window.onload = <span style="color:blue">function </span>() {
    calc = <span style="color:blue">new </span>Calculator(<span style="color:maroon">'currNumber'</span>, <span style="color:maroon">'eq'</span>);
};</pre>
<p>
  <br />The HTML used to render the calculator can reference the calc object created when the page loads. The following code demonstrates how this can be done to handle events as different div elements are clicked. I normally prefer to wire-up events to event handlers using jQuery in “real-world” applications to keep the HTML clean but wanted to focus on JavaScript patterns and avoid introducing additional libraries for this post. I’m not a big fan of defining onclick and other event handlers directly on HTML elements but let it slide for this post. </p>
<pre><span style="color:blue">&lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Calculator&quot;&gt;
    &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;CalculatorHead&quot;&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">id</span><span style="color:blue">=&quot;eq&quot; </span><span style="color:red">class</span><span style="color:blue">=&quot;Equation&quot;&gt;&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">id</span><span style="color:blue">=&quot;currNumber&quot; </span><span style="color:red">class</span><span style="color:blue">=&quot;CurrentNumber&quot;&gt;</span>0<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
    &lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
    &lt;</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.numberClick(event);&quot;&gt;</span>7<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.numberClick(event);&quot;&gt;</span>8<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.numberClick(event);&quot;&gt;</span>9<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.setOperator(&#39;/&#39;);&quot;&gt;</span>/<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button rowspan2&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.clearNumbers();&quot;&gt;</span>C<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
    &lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
    &lt;</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.numberClick(event);&quot;&gt;</span>4<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.numberClick(event);&quot;&gt;</span>5<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.numberClick(event);&quot;&gt;</span>6<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.setOperator(&#39;*&#39;);&quot;&gt;</span>*<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
    &lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
    &lt;</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.numberClick(event);&quot;&gt;</span>1<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.numberClick(event);&quot;&gt;</span>2<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.numberClick(event);&quot;&gt;</span>3<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.setOperator(&#39;-&#39;)&quot;&gt;</span>-<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button rowspan2&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.setOperator(&#39;=&#39;);&quot;&gt;</span>=<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
    &lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
    &lt;</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button colspan2&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.numberClick(event);&quot;&gt;</span>0<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.numberClick();&quot;&gt;</span>.<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
        &lt;</span><span style="color:maroon">div </span><span style="color:red">class</span><span style="color:blue">=&quot;Button&quot; </span><span style="color:red">onclick</span><span style="color:blue">=&quot;calc.setOperator(&#39;+&#39;);&quot;&gt;</span>+<span style="color:blue">&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
    &lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
&lt;/</span><span style="color:maroon">div</span><span style="color:blue">&gt;
</span></pre>
<p></p>
<p> </p>
<p>The Prototype Pattern provides a nice way to structure JavaScript code but there are several other roads you can travel if desired. In this next post I’ll talk about the Revealing Module Pattern and explain how it can be used.</p>
<p>Demos of all the patterns covered in this series can be downloaded below.</p>
<h2><a href="http://dl.dropbox.com/u/6037348/HTML5AndJavaScript/StructuringJavaScript.zip">Download Code</a></h2>
<p><img src="http://weblogs.asp.net/aggbug.aspx?PostID=7892965" width="1" height="1"></p>
]]></content:encoded>
			<wfw:commentRss>http://zdima.net/blog/archives/16371/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a Line Chart using the HTML 5 Canvas</title>
		<link>http://zdima.net/blog/archives/15936</link>
		<comments>http://zdima.net/blog/archives/15936#comments</comments>
		<pubDate>Wed, 25 May 2011 06:20:36 +0000</pubDate>
		<dc:creator>dwahlin</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Contributors]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[HTML5]]></category>

		<guid isPermaLink="false">http://zdima.net/blog/?p=15936</guid>
		<description><![CDATA[The HTML 5 canvas is capable of rendering lines, shapes, images, text and more without relying on a plugin. Although the canvas element isn’t supported by older browsers, the latest version of all major browsers (IE, Safari, Chrome, Firefox and Opera...<p class="read-more"><a href="http://zdima.net/blog/archives/15936">> Read more</a></p>]]></description>
			<content:encoded><![CDATA[<p>The HTML 5 canvas is capable of rendering lines, shapes, images, text and more without relying on a plugin. Although the canvas element isn’t supported by older browsers, the latest version of all major browsers (IE, Safari, Chrome, Firefox and Opera) now support the canvas making it an option for rendering charts, graphs and other types of visual data. In cases where a browser doesn’t support the canvas, a fallback can be provided that renders data using Silverlight, Flash or another type of plugin.</p>
<p>In a <a href="http://weblogs.asp.net/dwahlin/archive/2011/05/06/getting-started-with-the-html-5-canvas.aspx">previous post</a> I walked through the fundamentals of using the HTML 5 canvas to render different types of shapes. In this post I’ll discuss how the canvas can be used to render a line chart using JavaScript. An example of the chart that will be discussed is shown next:</p>
<p><img src="http://weblogs.asp.net/blogs/dwahlin/image_16EE917F.png"></p>
<p> </p>
<p>To render the chart a JavaScript object named CanvasChart was created that handles rendering all of the lines, shapes and text shown above. An example of defining CanvasChart settings and calling its render() function is show next. The render() function accepts the canvas element ID as well as a JSON object that defines chart properties and data to be used in the rendering process.   </p>
<pre><span style="color:blue">&lt;!</span><span style="color:maroon">DOCTYPE </span><span style="color:red">html</span><span style="color:blue">&gt;
&lt;</span><span style="color:maroon">html</span><span style="color:blue">&gt;
&lt;</span><span style="color:maroon">head</span><span style="color:blue">&gt;
    &lt;</span><span style="color:maroon">title</span><span style="color:blue">&gt;</span>Canvas Chart Demo<span style="color:blue">&lt;/</span><span style="color:maroon">title</span><span style="color:blue">&gt;
    &lt;</span><span style="color:maroon">script </span><span style="color:red">src</span><span style="color:blue">=&quot;Scripts/jquery-1.6.min.js&quot; </span><span style="color:red">type</span><span style="color:blue">=&quot;text/javascript&quot;&gt;&lt;/</span><span style="color:maroon">script</span><span style="color:blue">&gt;
    &lt;</span><span style="color:maroon">script </span><span style="color:red">src</span><span style="color:blue">=&quot;Scripts/canvasChart.js&quot; </span><span style="color:red">type</span><span style="color:blue">=&quot;text/javascript&quot;&gt;&lt;/</span><span style="color:maroon">script</span><span style="color:blue">&gt;
    &lt;</span><span style="color:maroon">script </span><span style="color:red">type</span><span style="color:blue">=&quot;text/javascript&quot;&gt;

        </span>$(document).ready(<span style="color:blue">function </span>() {
            <span style="color:blue">var </span>dataDef = { title: <span style="color:maroon">&quot;US Population Chart&quot;</span>,
                            xLabel: <span style="color:maroon">'Year'</span>,
                            yLabel: <span style="color:maroon">'Population (millions)'</span>,
                            labelFont: <span style="color:maroon">'19pt Arial'</span>,
                            dataPointFont: <span style="color:maroon">'10pt Arial'</span>,
                            renderTypes: [CanvasChart.renderType.lines, CanvasChart.renderType.points],
                            dataPoints: [{ x: <span style="color:maroon">'1790'</span>, y: 3.9 },
                                         { x: <span style="color:maroon">'1810'</span>, y: 7.2 },
                                         { x: <span style="color:maroon">'1830'</span>, y: 12.8 },
                                         { x: <span style="color:maroon">'1850'</span>, y: 23.1 },
                                         { x: <span style="color:maroon">'1870'</span>, y: 36.5 },
                                         { x: <span style="color:maroon">'1890'</span>, y: 62.9 },
                                         { x: <span style="color:maroon">'1910'</span>, y: 92.2 },
                                         { x: <span style="color:maroon">'1930'</span>, y: 123.2 },
                                         { x: <span style="color:maroon">'1950'</span>, y: 151.3 },
                                         { x: <span style="color:maroon">'1970'</span>, y: 203.2 },
                                         { x: <span style="color:maroon">'1990'</span>, y: 248.7 },
                                         { x: <span style="color:maroon">'2010'</span>, y: 308.7}]
                           };
            CanvasChart.render(<span style="color:maroon">'canvas'</span>, dataDef);
        });

    <span style="color:blue">&lt;/</span><span style="color:maroon">script</span><span style="color:blue">&gt;
&lt;/</span><span style="color:maroon">head</span><span style="color:blue">&gt;
&lt;</span><span style="color:maroon">body </span><span style="color:red">style</span><span style="color:blue">=&quot;</span><span style="color:red">margin-left</span><span style="color:blue">:50px;</span><span style="color:red">margin-top</span><span style="color:blue">:50px;&quot;&gt;
    &lt;</span><span style="color:maroon">canvas </span><span style="color:red">id</span><span style="color:blue">=&quot;canvas&quot; </span><span style="color:red">width</span><span style="color:blue">=&quot;800&quot; </span><span style="color:red">height</span><span style="color:blue">=&quot;600&quot;&gt;&lt;/</span><span style="color:maroon">canvas</span><span style="color:blue">&gt;
&lt;/</span><span style="color:maroon">body</span><span style="color:blue">&gt;
&lt;/</span><span style="color:maroon">html</span><span style="color:blue">&gt;
</span></pre>
<p>
  <br />While the CanvasChart object is only a prototype at this point, it demonstrates several key features of the canvas element that can be used in applications including rendering lines, shapes, gradients, text and even transformed text. Let’s take a look at how the CanvasChart object was created. </p>
<p></p>
<h2>Rendering Gradients and Text</h2>
<p>The code for the CanvasChart object is located in a file named canvasChart.js. The code starts by defining a CanvasChart object that exposes two members named <strong>renderType</strong> and <strong>render</strong>. renderType is used to define what will be rendered on the chart (currently it supports rendering lines and points) while render() is used to render the data on the canvas as well as the associated labels for the x and y axes. The skeleton code for CanvasObject is shown next:</p>
<p></p>
<pre><span style="color:blue">var </span>CanvasChart = <span style="color:blue">function </span>() {
    <span style="color:blue">var </span>ctx;
    <span style="color:blue">var </span>margin = { top: 40, left: 75, right: 0, bottom: 75 };
    <span style="color:blue">var </span>chartHeight, chartWidth, yMax, xMax, data;
    <span style="color:blue">var </span>maxYValue = 0;
    <span style="color:blue">var </span>ratio = 0;
    <span style="color:blue">var </span>renderType = { lines: <span style="color:maroon">'lines'</span>, points: <span style="color:maroon">'points' </span>};

    <span style="color:blue">return </span>{
        renderType: renderType,
        render: render
    };
} ();</pre>
<p> </p>
<p>The render() function accepts the canvas ID within the page as well as a JSON object that defines details about labels, font sizes, data points and more:<br />
  </p>
<pre><span style="color:blue">var </span>render = <span style="color:blue">function</span>(canvasId, dataObj) {
    data = dataObj;
    getMaxDataYValue();
    <span style="color:blue">var </span>canvas = document.getElementById(canvasId);
    chartHeight = canvas.attr(<span style="color:maroon">'height'</span>);
    chartWidth = canvas.attr(<span style="color:maroon">'width'</span>);
    xMax = chartWidth - (margin.left + margin.right);
    yMax = chartHeight - (margin.top + margin.bottom);
    ratio = yMax / maxYValue;
    ctx = canvas.getContext(<span style="color:maroon">&quot;2d&quot;</span>);
    renderChart();
}</pre>
<p> </p>
<p>The function starts by assigning the dataObj parameter to a variable within the CanvasChart object and then calls an internal function named getMaxDataYValue(). The getMaxDataYValue() function determines the maximum Y value for the data points. From there, the render() function locates the target canvas element within the page, calculates width and height values, and accesses the canvas’s 2D context that will be used to draw. Finally, a call is made to renderChart() to start the rendering process.</p>
<p>The renderChart() function orchestrates different drawing functions and handles rendering the background, lines, labels and data by calling the respective functions:<br />
  </p>
<pre><span style="color:blue">var </span>renderChart = <span style="color:blue">function </span>() {
    renderBackground();
    renderText();
    renderLinesAndLabels();

    <span style="color:#006400">//render data based upon type of renderType(s) that client supplies
    </span><span style="color:blue">if </span>(data.renderTypes == undefined || data.renderTypes == <span style="color:blue">null</span>) data.renderTypes = [renderType.lines];
    <span style="color:blue">for </span>(<span style="color:blue">var </span>i = 0; i &lt; data.renderTypes.length; i++) {
        renderData(data.renderTypes[i]);
    }
}</pre>
<p> </p>
<p>Different canvas features are used such as gradients and transforms in the CanvasChart object. For example, the renderBackground() function shows how linear gradients can be created:<br />
  </p>
<pre><span style="color:blue">var </span>renderBackground = <span style="color:blue">function </span>() {
    <span style="color:blue">var </span>lingrad = ctx.createLinearGradient(margin.left, margin.top, xMax - margin.right, yMax);
    lingrad.addColorStop(0.0, <span style="color:maroon">'#D4D4D4'</span>);
    lingrad.addColorStop(0.2, <span style="color:maroon">'#fff'</span>);
    lingrad.addColorStop(0.8, <span style="color:maroon">'#fff'</span>);
    lingrad.addColorStop(1, <span style="color:maroon">'#D4D4D4'</span>);
    ctx.fillStyle = lingrad;
    ctx.fillRect(margin.left, margin.top, xMax - margin.left, yMax - margin.top);
    ctx.fillStyle = <span style="color:maroon">'black'</span>;
}</pre>
<p> </p>
<p>The renderBackground() function uses the 2D context’s createLinearGradient() function to define a gradient that has 4 gradient stops. Once the gradient is defined it is assigned to the fillStyle property and then rendered to a rectangular area using the fillRect() function.</p>
<p>CanvasChart also demonstrates how text can be manipulated using transforms. The text displayed on the Y axis is rotated so that it displays vertically:</p>
<p> </p>
<p><a href="http://weblogs.asp.net/blogs/dwahlin/image_59332AA2.png"><img style="border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/dwahlin/image_thumb_6AA38B7A.png" width="76" height="302"></a></p>
<p> </p>
<p> This is accomplished by using the canvas element’s rotate transform functionality which is found in the renderText() function:<br />
  </p>
<pre><span style="color:blue">var </span>renderText = <span style="color:blue">function </span>renderText() {
    <span style="color:blue">var </span>labelFont = (data.labelFont != <span style="color:blue">null</span>) ? data.labelFont : <span style="color:maroon">'20pt Arial'</span>;
    ctx.font = labelFont;
    ctx.textAlign = <span style="color:maroon">&quot;center&quot;</span>;

    <span style="color:#006400">//Title
    </span><span style="color:blue">var </span>txtSize = ctx.measureText(data.title);
    ctx.fillText(data.title, (chartWidth / 2), (margin.top / 2));

    <span style="color:#006400">//X-axis text
    </span>txtSize = ctx.measureText(data.xLabel);
    ctx.fillText(data.xLabel, margin.left + (xMax / 2) - (txtSize.width / 2), yMax + (margin.bottom / 1.2));

    <span style="color:#006400">//Y-axis text
    </span>ctx.save();
    ctx.rotate(-Math.PI / 2);
    ctx.font = labelFont;
    ctx.fillText(data.yLabel, (yMax / 2) * -1, margin.left / 4);
    ctx.restore();
}</pre>
<p> </p>
<p>The key section of this code is the call to ctx.save() (toward the bottom of the function shown above) which saves the current state of the canvas so that it can be restored. This is necessary so that the entire canvas isn’t rotated. Once the current canvas state is saved, a call to the rotate() function is made to rotate the canvas. The text is then drawn for the vertical axis using the fillText() function. Once the rotated text is rendered the canvas is restored back to its saved state (the state before the rotate transform was applied and the text was rendered). </p>
<p>After the x and y axis text is rendered, the CanvasChart object makes a call to renderLinesAndLabels() to handle rendering the horizontal and vertical lines.<br />
  </p>
<pre><span style="color:blue">var </span>renderLinesAndLabels = <span style="color:blue">function </span>renderLinesAndLabels() {
    <span style="color:#006400">//Vertical guide lines
    </span><span style="color:blue">var </span>yInc = yMax / data.dataPoints.length;
    <span style="color:blue">var </span>yPos = 0;
    <span style="color:blue">var </span>yLabelInc = (maxYValue * ratio) / data.dataPoints.length;
    <span style="color:blue">var </span>xInc = getXInc();
    <span style="color:blue">var </span>xPos = margin.left;
    <span style="color:blue">for </span>(<span style="color:blue">var </span>i = 0; i &lt; data.dataPoints.length; i++) {
        yPos += (i == 0) ? margin.top : yInc;
        <span style="color:#006400">//Draw horizontal lines
        </span>drawLine(margin.left, yPos, xMax, yPos, <span style="color:maroon">'#E8E8E8'</span>);

        <span style="color:#006400">//y axis labels
        </span>ctx.font = (data.dataPointFont != <span style="color:blue">null</span>) ? data.dataPointFont : <span style="color:maroon">'10pt Calibri'</span>;
        <span style="color:blue">var </span>txt = Math.round(maxYValue - ((i == 0) ? 0 : yPos / ratio));
        <span style="color:blue">var </span>txtSize = ctx.measureText(txt);
        ctx.fillText(txt, margin.left - ((txtSize.width &gt;= 14)?txtSize.width:10) - 7, yPos + 4);

        <span style="color:#006400">//x axis labels
        </span>txt = data.dataPoints[i].x;
        txtSize = ctx.measureText(txt);
        ctx.fillText(txt, xPos, yMax + (margin.bottom / 3));
        xPos += xInc;
    }

    <span style="color:#006400">//Vertical line
    </span>drawLine(margin.left, margin.top, margin.left, yMax, <span style="color:maroon">'black'</span>);

    <span style="color:#006400">//Horizontal Line
    </span>drawLine(margin.left, yMax, xMax, yMax, <span style="color:maroon">'black'</span>);
}</pre>
<p> </p>
<p>Lines are normally drawn using the 2D context’s moveTo() and lineTo() functions which I wrapped in a function named drawLine() to simplify the process:<br />
  </p>
<pre><span style="color:blue">var </span>drawLine = <span style="color:blue">function </span>drawLine(startX, startY, endX, endY, strokeStyle, lineWidth) {
    <span style="color:blue">if </span>(strokeStyle != <span style="color:blue">null</span>) ctx.strokeStyle = strokeStyle;
    <span style="color:blue">if </span>(lineWidth != <span style="color:blue">null</span>) ctx.lineWidth = lineWidth;
    ctx.beginPath();
    ctx.moveTo(startX, startY);
    ctx.lineTo(endX, endY);
    ctx.stroke();
    ctx.closePath();
}</pre>
<p>
  <br />At this point the canvas looks like the following:</p>
<p></p>
<p><a href="http://weblogs.asp.net/blogs/dwahlin/image_25F6F139.png"><img style="border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px" title="image" border="0" alt="image" src="http://weblogs.asp.net/blogs/dwahlin/image_thumb_4539980C.png" width="661" height="493"></a></p>
<p></p>
<h2>Rendering Data</h2>
<p>Once the labels and lines are rendered, CanvasChart handles rendering the data points by calling a function named renderData(). This function handles iterating through the JSON   data points and drawing lines, points or both depending upon the settings passed to CanvasChart’s render() function. Lines are drawn to connect the different data points through calls to the drawLine() function shown earlier while circles are drawn for specific data points by making calls to the 2D context’s arc() function. The circles that are rendered have a radial gradient applied to them using the createRadialGradient() function:</p>
<p> </p>
<pre><span style="color:blue">var </span>renderData = <span style="color:blue">function </span>renderData(type) {
    <span style="color:blue">var </span>xInc = getXInc();
    <span style="color:blue">var </span>prevX, prevY = 0;

    <span style="color:blue">for </span>(<span style="color:blue">var </span>i = 0; i &lt; data.dataPoints.length; i++) {
        <span style="color:blue">var </span>pt = data.dataPoints[i];
        <span style="color:blue">var </span>ptY = (maxYValue - pt.y) * ratio;
        <span style="color:blue">if </span>(ptY &lt; margin.top) ptY = margin.top;
        <span style="color:blue">var </span>ptX = (i * xInc) + margin.left;

        <span style="color:blue">if </span>(i &gt; 0 &amp;&amp; type == renderType.lines) {
            <span style="color:#006400">//Draw connecting lines
            </span>drawLine(ptX, ptY, prevX, prevY, <span style="color:maroon">'black'</span>, 2);
        }

        <span style="color:blue">if </span>(type == renderType.points) {
            <span style="color:blue">var </span>radgrad = ctx.createRadialGradient(ptX, ptY, 8, ptX - 5, ptY - 5, 0);
            radgrad.addColorStop(0, <span style="color:maroon">'Green'</span>);
            radgrad.addColorStop(0.9, <span style="color:maroon">'White'</span>);
            ctx.beginPath();
            ctx.fillStyle = radgrad;
            <span style="color:#006400">//Render circle
            </span>ctx.arc(ptX, ptY, 8, 0, 2 * Math.PI, <span style="color:blue">false</span>)
            ctx.fill();
            ctx.lineWidth = 1;
            ctx.strokeStyle = <span style="color:maroon">'#000'</span>;
            ctx.stroke();
            ctx.closePath();
        }

        prevX = ptX;
        prevY = ptY;
    }
}</pre>
<p> </p>
<p>Once the data points are rendered the chart looks like the following:<br />
  </p>
<p><img src="http://weblogs.asp.net/blogs/dwahlin/image_16EE917F.png"></p>
<h2> </h2>
<h2>Conclusion</h2>
<p>You can see that there’s a fair amount of JavaScript code required to use the canvas object. However, once the different API functions are understood it’s simply a process of calling the appropriate functions to render lines, text or shapes. Although the CanvasChart object shown here is only a prototype at this point, I hope it provides insight into what the HTML 5 canvas is capable of rendering and how some of the features it provides can be used.<br />
  </p>
<p><a href="http://www.xmlforasp.net/CodeBank/Download/Blog/HTML5/CanvasDemos.zip"><font size="4"><strong>Download Canvas Demo</strong></font></a></p>
<p><img src="http://weblogs.asp.net/aggbug.aspx?PostID=7802952" width="1" height="1"></p>
]]></content:encoded>
			<wfw:commentRss>http://zdima.net/blog/archives/15936/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The HTML5 Readiness Chart Highlights How Well Your Browser Handles the Future [Infographic]</title>
		<link>http://zdima.net/blog/archives/15036</link>
		<comments>http://zdima.net/blog/archives/15036#comments</comments>
		<pubDate>Thu, 13 May 2010 18:00:00 +0000</pubDate>
		<dc:creator>ZDima</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[Contributors]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Google Chrome]]></category>
		<category><![CDATA[Internet Explorer]]></category>
		<category><![CDATA[Safari]]></category>
		<category><![CDATA[Video]]></category>
		<category><![CDATA[Web Browsing]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[infographic]]></category>
		<category><![CDATA[Web Design]]></category>

		<guid isPermaLink="false">http://zdima.net/blog/?p=15036</guid>
		<description><![CDATA[<div style="float:left;padding-right:10px">
										
					<div><a title="Click here to read The HTML5 Readiness Chart Highlights How Well Your Browser Handles the Future" href="http://lifehacker.com/5538188/the-html5-readiness-chart-highlights-how-well-your-browser-handles-the-future">
						<img style="border-color:#B3B3B3;border-width:0 1px 1px;border-style:none solid solid" height="120" width="160" alt="Click here to read The HTML5 Readiness Chart Highlights How Well Your Browser Handles the Future" src="http://cache-03.gawkerassets.com/assets/images/17/2010/05/160x120_HTML5%20Readiness.jpg">
						<span></span>					</a></div>
									</div>
				We've said for a while that <a href="http://lifehacker.com/5416100/how-html5-will-change-the-way-you-use-the-web">HTML5 will change the way you use the web</a>, but not all browsers are ready for the big change. This interactive chart highlights which features are still missing in your browser of  choice.				<a href="http://lifehacker.com/5538188/the-html5-readiness-chart-highlights-how-well-your-browser-handles-the-future" title="Click here to read more about The HTML5 Readiness Chart Highlights How Well Your Browser Handles the Future [Infographic]">More »</a>
				<br />
			<br />
<br />
<a href="http://ads.pheedo.com/click.phdo?s=a49d3d32f93fc6079585267dc00bff2c&#38;p=1"><img alt="" style="border:0" border="0" src="http://ads.pheedo.com/img.phdo?s=a49d3d32f93fc6079585267dc00bff2c&#38;p=1"></a>
<img alt="" height="0" width="0" border="0" src="http://a.triggit.com/px?u=pheedo&#38;rtv=TechBiz&#38;rtv=p28252&#38;rtv=f5734"><img alt="" height="0" width="0" border="0" src="http://pixel.quantserve.com/pixel/p-8bUhLiluj0fAw.gif?labels=pub.28252.rss.TechBiz.5734,cat.TechBiz.rss"><div>
<a href="http://feeds.gawker.com/~ff/lifehacker/full?a=y6L_fz0pE7E:lIa-ECeTvns:H0mrP-F8Qgo"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?d=H0mrP-F8Qgo" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=y6L_fz0pE7E:lIa-ECeTvns:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?d=yIl2AUoC8zA" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=y6L_fz0pE7E:lIa-ECeTvns:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?i=y6L_fz0pE7E:lIa-ECeTvns:D7DqB2pKExk" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=y6L_fz0pE7E:lIa-ECeTvns:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?i=y6L_fz0pE7E:lIa-ECeTvns:V_sGLiPBpWU" border="0"></a>
</div><img src="http://feeds.feedburner.com/~r/lifehacker/full/~4/y6L_fz0pE7E" height="1" width="1"><p class="read-more"><a href="http://zdima.net/blog/archives/15036">> Read more</a></p>]]></description>
			<content:encoded><![CDATA[<div style="float:left;padding-right:10px">
<div><a title="Click here to read The HTML5 Readiness Chart Highlights How Well Your Browser Handles the Future" href="http://lifehacker.com/5538188/the-html5-readiness-chart-highlights-how-well-your-browser-handles-the-future"><br />
						<img style="border-color:#B3B3B3;border-width:0 1px 1px;border-style:none solid solid" height="120" width="160" title="Click here to read The HTML5 Readiness Chart Highlights How Well Your Browser Handles the Future" alt="Click here to read The HTML5 Readiness Chart Highlights How Well Your Browser Handles the Future" src="http://cache-03.gawkerassets.com/assets/images/17/2010/05/160x120_HTML5%20Readiness.jpg"><br />
						<span></span>					</a></div>
</p></div>
<p>				We&#8217;ve said for a while that <a href="http://lifehacker.com/5416100/how-html5-will-change-the-way-you-use-the-web">HTML5 will change the way you use the web</a>, but not all browsers are ready for the big change. This interactive chart highlights which features are still missing in your browser of  choice.				<a href="http://lifehacker.com/5538188/the-html5-readiness-chart-highlights-how-well-your-browser-handles-the-future" title="Click here to read more about The HTML5 Readiness Chart Highlights How Well Your Browser Handles the Future [Infographic]">More »</a><br />
				<br style="clear:both"><br />
			<br style="clear:both"><br />
<br style="clear:both"><br />
<a href="http://ads.pheedo.com/click.phdo?s=a49d3d32f93fc6079585267dc00bff2c&amp;p=1"><img alt="" style="border:0" border="0" src="http://ads.pheedo.com/img.phdo?s=a49d3d32f93fc6079585267dc00bff2c&amp;p=1"></a><br />
<img alt="" height="0" width="0" border="0" src="http://a.triggit.com/px?u=pheedo&amp;rtv=TechBiz&amp;rtv=p28252&amp;rtv=f5734"><img alt="" height="0" width="0" border="0" src="http://pixel.quantserve.com/pixel/p-8bUhLiluj0fAw.gif?labels=pub.28252.rss.TechBiz.5734,cat.TechBiz.rss">
<div>
<a href="http://feeds.gawker.com/~ff/lifehacker/full?a=y6L_fz0pE7E:lIa-ECeTvns:H0mrP-F8Qgo"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?d=H0mrP-F8Qgo" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=y6L_fz0pE7E:lIa-ECeTvns:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?d=yIl2AUoC8zA" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=y6L_fz0pE7E:lIa-ECeTvns:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?i=y6L_fz0pE7E:lIa-ECeTvns:D7DqB2pKExk" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=y6L_fz0pE7E:lIa-ECeTvns:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?i=y6L_fz0pE7E:lIa-ECeTvns:V_sGLiPBpWU" border="0"></a>
</div>
<p><img src="http://feeds.feedburner.com/~r/lifehacker/full/~4/y6L_fz0pE7E" height="1" width="1"></p>
]]></content:encoded>
			<wfw:commentRss>http://zdima.net/blog/archives/15036/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Embed HTML5 Videos with Multi-Browser Support on Your Site [HTML5]</title>
		<link>http://zdima.net/blog/archives/15026</link>
		<comments>http://zdima.net/blog/archives/15026#comments</comments>
		<pubDate>Tue, 11 May 2010 13:00:00 +0000</pubDate>
		<dc:creator>Kevin Purdy</dc:creator>
				<category><![CDATA[Contributors]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Streaming Video]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[codecs]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Web Design]]></category>

		<guid isPermaLink="false">http://zdima.net/blog/?p=15026</guid>
		<description><![CDATA[<div style="float:left;padding-right:10px">
										
					<div><a title="Click here to read Embed HTML5 Videos with Multi-Browser Support on Your Site" href="http://lifehacker.com/5536050/embed-html5-videos-with-multi+browser-support-on-your-site">
						<img style="border-color:#B3B3B3;border-width:0 1px 1px;border-style:none solid solid" height="120" width="160" alt="Click here to read Embed HTML5 Videos with Multi-Browser Support on Your Site" src="http://cache-04.gawkerassets.com/assets/images/17/2010/05/160x120_streaming_video_thumb.jpg">
											</a></div>
									</div>
				If you've got a blog, personal site, or another web platform and find yourself wondering how you can get in on the <a href="http://lifehacker.com/5416100/how-html5-will-change-the-way-you-use-the-web">no-plugin-needed streaming video in HTML5</a>, Webmonkey has a handy guide to understanding and embedding HTML5 videos on your site.				<a href="http://lifehacker.com/5536050/embed-html5-videos-with-multi+browser-support-on-your-site" title="Click here to read more about Embed HTML5 Videos with Multi-Browser Support on Your Site [HTML5]">More »</a>
				<br />
			<br />
<br />
<a href="http://ads.pheedo.com/click.phdo?s=fbec7abcb6f293aac025574595dad96e&#38;p=1"><img alt="" style="border:0" border="0" src="http://ads.pheedo.com/img.phdo?s=fbec7abcb6f293aac025574595dad96e&#38;p=1"></a>
<img alt="" height="0" width="0" border="0" src="http://ib.adnxs.com/seg?add=24595&#38;t=2"><br />
<img src="http://ads.pheedo.com/img.phdo?kw="> 
<a href="http://ads.pheedo.com/click.phdo?s=fbec7abcb6f293aac025574595dad96e&#38;p=64&#38;kw=HTML5">HTML5</a> - <a href="http://ads.pheedo.com/click.phdo?s=fbec7abcb6f293aac025574595dad96e&#38;p=64&#38;kw=HTML">HTML</a> - <a href="http://ads.pheedo.com/click.phdo?s=fbec7abcb6f293aac025574595dad96e&#38;p=64&#38;kw=Data+Formats">Data Formats</a> - <a href="http://ads.pheedo.com/click.phdo?s=fbec7abcb6f293aac025574595dad96e&#38;p=64&#38;kw=Markup+Languages">Markup Languages</a> - <a href="http://ads.pheedo.com/click.phdo?s=fbec7abcb6f293aac025574595dad96e&#38;p=64&#38;kw=Flash">Flash</a><div>
<a href="http://feeds.gawker.com/~ff/lifehacker/full?a=RkVNy3j9Lak:Wn3vqT3zGto:H0mrP-F8Qgo"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?d=H0mrP-F8Qgo" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=RkVNy3j9Lak:Wn3vqT3zGto:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?d=yIl2AUoC8zA" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=RkVNy3j9Lak:Wn3vqT3zGto:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?i=RkVNy3j9Lak:Wn3vqT3zGto:D7DqB2pKExk" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=RkVNy3j9Lak:Wn3vqT3zGto:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?i=RkVNy3j9Lak:Wn3vqT3zGto:V_sGLiPBpWU" border="0"></a>
</div><img src="http://feeds.feedburner.com/~r/lifehacker/full/~4/RkVNy3j9Lak" height="1" width="1"><p class="read-more"><a href="http://zdima.net/blog/archives/15026">> Read more</a></p>]]></description>
			<content:encoded><![CDATA[<div style="float:left;padding-right:10px">
<div><a title="Click here to read Embed HTML5 Videos with Multi-Browser Support on Your Site" href="http://lifehacker.com/5536050/embed-html5-videos-with-multi+browser-support-on-your-site"><br />
						<img style="border-color:#B3B3B3;border-width:0 1px 1px;border-style:none solid solid" height="120" width="160" title="Click here to read Embed HTML5 Videos with Multi-Browser Support on Your Site" alt="Click here to read Embed HTML5 Videos with Multi-Browser Support on Your Site" src="http://cache-04.gawkerassets.com/assets/images/17/2010/05/160x120_streaming_video_thumb.jpg"><br />
											</a></div>
</p></div>
<p>				If you&#8217;ve got a blog, personal site, or another web platform and find yourself wondering how you can get in on the <a href="http://lifehacker.com/5416100/how-html5-will-change-the-way-you-use-the-web">no-plugin-needed streaming video in HTML5</a>, Webmonkey has a handy guide to understanding and embedding HTML5 videos on your site.				<a href="http://lifehacker.com/5536050/embed-html5-videos-with-multi+browser-support-on-your-site" title="Click here to read more about Embed HTML5 Videos with Multi-Browser Support on Your Site [HTML5]">More »</a><br />
				<br style="clear:both"><br />
			<br style="clear:both"><br />
<br style="clear:both"><br />
<a href="http://ads.pheedo.com/click.phdo?s=fbec7abcb6f293aac025574595dad96e&amp;p=1"><img alt="" style="border:0" border="0" src="http://ads.pheedo.com/img.phdo?s=fbec7abcb6f293aac025574595dad96e&amp;p=1"></a><br />
<img alt="" height="0" width="0" border="0" src="http://ib.adnxs.com/seg?add=24595&amp;t=2"><br />
<img src="http://ads.pheedo.com/img.phdo?kw="><br />
<a href="http://ads.pheedo.com/click.phdo?s=fbec7abcb6f293aac025574595dad96e&amp;p=64&amp;kw=HTML5">HTML5</a> &#8211; <a href="http://ads.pheedo.com/click.phdo?s=fbec7abcb6f293aac025574595dad96e&amp;p=64&amp;kw=HTML">HTML</a> &#8211; <a href="http://ads.pheedo.com/click.phdo?s=fbec7abcb6f293aac025574595dad96e&amp;p=64&amp;kw=Data+Formats">Data Formats</a> &#8211; <a href="http://ads.pheedo.com/click.phdo?s=fbec7abcb6f293aac025574595dad96e&amp;p=64&amp;kw=Markup+Languages">Markup Languages</a> &#8211; <a href="http://ads.pheedo.com/click.phdo?s=fbec7abcb6f293aac025574595dad96e&amp;p=64&amp;kw=Flash">Flash</a>
<div>
<a href="http://feeds.gawker.com/~ff/lifehacker/full?a=RkVNy3j9Lak:Wn3vqT3zGto:H0mrP-F8Qgo"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?d=H0mrP-F8Qgo" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=RkVNy3j9Lak:Wn3vqT3zGto:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?d=yIl2AUoC8zA" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=RkVNy3j9Lak:Wn3vqT3zGto:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?i=RkVNy3j9Lak:Wn3vqT3zGto:D7DqB2pKExk" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=RkVNy3j9Lak:Wn3vqT3zGto:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?i=RkVNy3j9Lak:Wn3vqT3zGto:V_sGLiPBpWU" border="0"></a>
</div>
<p><img src="http://feeds.feedburner.com/~r/lifehacker/full/~4/RkVNy3j9Lak" height="1" width="1"></p>
]]></content:encoded>
			<wfw:commentRss>http://zdima.net/blog/archives/15026/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>YouTube Offers No-Flash HTML5 Videos for Chrome and Safari [Streaming Video]</title>
		<link>http://zdima.net/blog/archives/13380</link>
		<comments>http://zdima.net/blog/archives/13380#comments</comments>
		<pubDate>Thu, 21 Jan 2010 12:40:00 +0000</pubDate>
		<dc:creator>Kevin Purdy</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[Chrome]]></category>
		<category><![CDATA[Contributors]]></category>
		<category><![CDATA[Google Chrome]]></category>
		<category><![CDATA[Safari]]></category>
		<category><![CDATA[Streaming Video]]></category>
		<category><![CDATA[Video]]></category>
		<category><![CDATA[YouTube]]></category>
		<category><![CDATA[HTML5]]></category>

		<guid isPermaLink="false">http://zdima.net/blog/?p=13380</guid>
		<description><![CDATA[<p><img src="http://cache.gawker.com/assets/images/lifehacker/2010/01/youtube_html5_2.jpg" width="340">If you&#39;re running Chrome or Safari as your main browser, Google&#39;s now offering up YouTube videos without Flash. That&#39;s right—fewer system hangs, browser crashes, and other issues, and just straight-up <a href="http://lifehacker.com/5416100/how-html5-will-change-the-way-you-use-the-web">video through HTML5 standards</a>.</p><p>Google has previously allowed Chrome, Safari, and <a href="http://lifehacker.com/5365253/google-chrome-frame-turns-internet-explorer-into-a-frankenchrome-browser">Internet-Explorer-using-Chrome-Frame</a> browsers to try out a few HTML5 video demos at its site, but now Google&#39;s given you the option to always play videos through the h.264 codec, if they&#39;re available. If they have ads, or aren&#39;t available in h.264, YouTube will serve up the standard Flash player—though that&#39;s been upgraded, too, with a nice video format chooser in the lower-right corner.</p>
<p>The notable missing piece here is Firefox. Firefox does support HTML5's video streaming through Ogg Theora, a non-patented, license-free codec that its makers consider more free, while Google, and Apple, have moved their sites and browsers toward supporting h.264 streaming.</p>
<p>Enough web politics! If you're rocking Chrome, Safari, or Chrome Frame inside IE, head to <a href="http://youtube.com/html5">YouTube's HTML5 page</a> to sign yourself into the beta. If you're signed up for other YouTube lab projects in the <a href="http://youtube.com/testtube">TestTube</a> section, you might want to sign yourself out of them—except for Feather, which works fine with HTML5 and makes it even lighter and snappier.</p>
<p>Is HTML5-powered YouTube a better fit for your browsing? Like the Flash player better? Tell us your take in the comments.</p>
<div><a href="http://youtube-global.blogspot.com/2010/01/introducing-youtube-html5-supported.html">Introducing YouTube HTML5 Supported Videos</a> [YouTube Blog via <a href="http://googlesystem.blogspot.com/2010/01/youtubes-html5-player.html">Google Operating System</a>]</div><br />
<br />
<a href="http://ads.pheedo.com/click.phdo?s=e76700641b4862f76da1083dfbb9fee3&#38;p=1"><img alt="" style="border:0" border="0" src="http://ads.pheedo.com/img.phdo?s=e76700641b4862f76da1083dfbb9fee3&#38;p=1"></a>
<img alt="" height="0" width="0" border="0" src="http://a.rfihub.com/eus.gif?eui=2225"><p></p><div>
<a href="http://feeds.gawker.com/~ff/lifehacker/full?a=n5HSmsz7S4g:l_GAB05ODh4:H0mrP-F8Qgo"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?d=H0mrP-F8Qgo" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=n5HSmsz7S4g:l_GAB05ODh4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?d=yIl2AUoC8zA" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=n5HSmsz7S4g:l_GAB05ODh4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?i=n5HSmsz7S4g:l_GAB05ODh4:D7DqB2pKExk" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=n5HSmsz7S4g:l_GAB05ODh4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?i=n5HSmsz7S4g:l_GAB05ODh4:V_sGLiPBpWU" border="0"></a>
</div><img src="http://feeds.feedburner.com/~r/lifehacker/full/~4/n5HSmsz7S4g" height="1" width="1"><p class="read-more"><a href="http://zdima.net/blog/archives/13380">> Read more</a></p>]]></description>
			<content:encoded><![CDATA[<p><img src="http://cache.gawker.com/assets/images/lifehacker/2010/01/youtube_html5_2.jpg" width="340">If you&#39;re running Chrome or Safari as your main browser, Google&#39;s now offering up YouTube videos without Flash. That&#39;s right—fewer system hangs, browser crashes, and other issues, and just straight-up <a href="http://lifehacker.com/5416100/how-html5-will-change-the-way-you-use-the-web">video through HTML5 standards</a>.</p>
<p>Google has previously allowed Chrome, Safari, and <a href="http://lifehacker.com/5365253/google-chrome-frame-turns-internet-explorer-into-a-frankenchrome-browser">Internet-Explorer-using-Chrome-Frame</a> browsers to try out a few HTML5 video demos at its site, but now Google&#39;s given you the option to always play videos through the h.264 codec, if they&#39;re available. If they have ads, or aren&#39;t available in h.264, YouTube will serve up the standard Flash player—though that&#39;s been upgraded, too, with a nice video format chooser in the lower-right corner.</p>
<p>The notable missing piece here is Firefox. Firefox does support HTML5&#8242;s video streaming through Ogg Theora, a non-patented, license-free codec that its makers consider more free, while Google, and Apple, have moved their sites and browsers toward supporting h.264 streaming.</p>
<p>Enough web politics! If you&#8217;re rocking Chrome, Safari, or Chrome Frame inside IE, head to <a href="http://youtube.com/html5">YouTube&#8217;s HTML5 page</a> to sign yourself into the beta. If you&#8217;re signed up for other YouTube lab projects in the <a href="http://youtube.com/testtube">TestTube</a> section, you might want to sign yourself out of them—except for Feather, which works fine with HTML5 and makes it even lighter and snappier.</p>
<p>Is HTML5-powered YouTube a better fit for your browsing? Like the Flash player better? Tell us your take in the comments.</p>
<div><a href="http://youtube-global.blogspot.com/2010/01/introducing-youtube-html5-supported.html">Introducing YouTube HTML5 Supported Videos</a> [YouTube Blog via <a href="http://googlesystem.blogspot.com/2010/01/youtubes-html5-player.html">Google Operating System</a>]</div>
<p><br ><br />
<br ><br />
<a href="http://ads.pheedo.com/click.phdo?s=e76700641b4862f76da1083dfbb9fee3&amp;p=1"><img alt=""  border="0" src="http://ads.pheedo.com/img.phdo?s=e76700641b4862f76da1083dfbb9fee3&amp;p=1"></a><br />
<img alt="" height="0" width="0" border="0" src="http://a.rfihub.com/eus.gif?eui=2225">
<p><iframe src="http://feedads.g.doubleclick.net/~ah/f/rakd0gtdk7723gpnhframh3eso/300/250?ca=1&amp;fh=280#http%3A%2F%2Flifehacker.com%2F5453591%2Fyoutube-offers-no%2Bflash-html5-videos-for-chrome-and-safari" width="100%" height="280" frameborder="0" scrolling="no" marginwidth="0" marginheight="0"></iframe></p>
<div>
<a href="http://feeds.gawker.com/~ff/lifehacker/full?a=n5HSmsz7S4g:l_GAB05ODh4:H0mrP-F8Qgo"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?d=H0mrP-F8Qgo" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=n5HSmsz7S4g:l_GAB05ODh4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?d=yIl2AUoC8zA" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=n5HSmsz7S4g:l_GAB05ODh4:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?i=n5HSmsz7S4g:l_GAB05ODh4:D7DqB2pKExk" border="0"></a> <a href="http://feeds.gawker.com/~ff/lifehacker/full?a=n5HSmsz7S4g:l_GAB05ODh4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/lifehacker/full?i=n5HSmsz7S4g:l_GAB05ODh4:V_sGLiPBpWU" border="0"></a>
</div>
<p><img src="http://feeds.feedburner.com/~r/lifehacker/full/~4/n5HSmsz7S4g" height="1" width="1"></p>
]]></content:encoded>
			<wfw:commentRss>http://zdima.net/blog/archives/13380/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

