<?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>HTML5 Doctor &#187; canvas</title>
	<atom:link href="http://html5doctor.com/tag/canvas/feed/" rel="self" type="application/rss+xml" />
	<link>http://html5doctor.com</link>
	<description>helping you implement HTML5 today</description>
	<lastBuildDate>Wed, 01 Feb 2012 09:28:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Your Questions #16</title>
		<link>http://html5doctor.com/your-questions-16/</link>
		<comments>http://html5doctor.com/your-questions-16/#comments</comments>
		<pubDate>Tue, 01 Mar 2011 14:30:38 +0000</pubDate>
		<dc:creator>Richard Clark</dc:creator>
				<category><![CDATA[Questions]]></category>
		<category><![CDATA[aside]]></category>
		<category><![CDATA[b]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[HTML 5]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[i]]></category>
		<category><![CDATA[nav]]></category>
		<category><![CDATA[section]]></category>

		<guid isPermaLink="false">http://html5doctor.com/?p=2748</guid>
		<description><![CDATA[<p>The clinic is getting busy with more HTML5 ailments! This week, we'll cover the separation of formatting and content, custom elements, using aside for social links, sections with no visible titles, and canvas in the DOM.</p>]]></description>
			<content:encoded><![CDATA[<p><img src="http://html5doctor.com/wp-content/uploads/2009/07/html5doctor-treatment.gif" alt="Doctor treating a patient illustration" class="alignright size-full wp-image-424" /> The clinic is getting busy with more <abbr>HTML</abbr>5 ailments! This week, we&#8217;ll cover the separation of formatting and content, custom elements, using <code>&lt;aside&gt;</code> for social links, sections with no visible titles, and <code>&lt;canvas&gt;</code> in the <abbr>DOM</abbr>.</p>
<section>
<h2>Separation of formatting and content</h2>
<p>Graham asked:</p>
<blockquote>
<p>When I first started learning html and <abbr>CSS</abbr> I was constantly told not to use <code>b</code> or <code>i</code> tags as the idea was to completely separate formatting from content. Is this now not the case?</p>
</blockquote>
<p>In <abbr>HTML</abbr> 4 (and <abbr>XHTML</abbr> 1.x), the elements <code>&lt;b&gt;</code> and <code>&lt;i&gt;</code> (plus some others) were presentational — they only had visual meaning. This doesn’t work so well for blind users, for example.</p>
<p>In <abbr>HTML</abbr>5, presentational elements have either been cut or, as in the case of <code>&lt;b&gt;</code> and <code>&lt;i&gt;</code>, given semantic meanings to make them media-independent. You can find out more here: <a href="http://html5doctor.com/i-b-em-strong-element/">The <code>&lt;i&gt;</code>, <code>&lt;b&gt;</code>, <code>&lt;em&gt;</code>, and <code>&lt;strong&gt;</code> elements</a> and <a href="http://html5doctor.com/small-hr-element/">The <code>&lt;small&gt;</code> and <code>&lt;hr&gt;</code> elements</a>.</p>
<p>So to directly answer your question, in <abbr>HTML</abbr>5, <code>&lt;b&gt;</code> and <code>&lt;i&gt;</code> are now content (semantic), not formatting (presentational).</p>
<p>peace &#8211; Oli</p>
</section>
<section>
<h2>Custom elements</h2>
<p>Eric asked:</p>
<blockquote>
<p>It is possible to use custom elements in <abbr>HTML</abbr>5 to be more semantic; however, is it ill-advised? For example:</p>
<p>    <code>
<pre>&lt;footer id="page-footer"&gt;
&lt;copyright&gt;Copyright ©...&lt;/copyright&gt;
&lt;/footer&gt;</pre>
<p></code></p>
</blockquote>
<p>Umm, no, <abbr>HTML</abbr>5 doesn&#8217;t allow &#8220;custom elements&#8221;. While browsers will try to recover gracefully if you do this, making stuff up is definitely ill-advised — something like <code>&lt;copyright&gt;</code> will have no meaning for browsers and could lead to problems in <abbr>IE</abbr>.</p>
<p>If you want an element for a copyright statement or other short legalese, there’s already a perfectly <a href="http://html5doctor.com/small-hr-element/">good one in <code>&lt;small&gt;</code></a>.</p>
<p>If you can’t find a more semantically appropriate element, use <code>&lt;span&gt;</code> for phrasing (inline) content, <code>&lt;p&gt;</code> for a block of phrasing content, or <code>&lt;div&gt;</code> for flow (block-level) content, and add a class name that indicates the semantics — e.g. <code>&lt;span class="lorem"&gt;Lorem ipsum&lt;/span&gt;</code>.</p>
<p>Hope that helps!</p>
<p>peace &#8211; Oli</p>
</section>
<section>
<h2>Marking up social links</h2>
<p>A reader asked:</p>
<blockquote>
<p>Could a list of share icons (twitter, facebook, etc) be considered a candidate for use of the nav element? and do links in a <code>nav</code> element <strong>have to be</strong> into the same domain name&#8221;?</p>
</blockquote>
<p><code>&lt;nav&gt;</code> is for navigation around your content. On my personal site, I have a link to my Flickr photostream marked up in the same container as links to my own contact page, etc. I think the link to Flickr is conceptually no different to navigation within my personal site: it&#8217;s all <em>my</em> content. If my photos were on my own domain (as they used to be), I wouldn&#8217;t hesitate to include them in <code>&lt;nav&gt;</code>, and I wouldn&#8217;t hesitate to include this same content within <code>&lt;nav&gt;</code> today.</p>
<p>A list of share icons isn&#8217;t <code>&lt;nav&gt;</code> for two separate reasons.</p>
<p>First, they aren&#8217;t meant to take you somewhere else. They&#8217;re designed to perform an action of tweeting/&#8221;liking&#8221; a link to a page.</p>
<p>Second, and most importantly, they&#8217;re not navigation around your content. As the <a href="http://dev.w3.org/html5/spec/sections.html#the-nav-element">spec says</a> <q>&#8220;Not all groups of links on a page need to be in a nav element — only sections that consist of major navigation blocks are appropriate for the nav element.&#8221;</q></p>
<p>As a list of share icons is tangential to your content, I&#8217;d probably use <a href="http://dev.w3.org/html5/spec/sections.html#the-nav-element">the <code>&lt;aside&gt;</code> element</a>: <q>&#8220;The aside element represents a section of a page that consists of content that is tangentially related to the content around the aside element, and which could be considered separate from that content. Such sections are often represented as sidebars in printed typography&#8221;</q>. For more details, check out our <a href="http://html5doctor.com/aside-revisited/"><code>&lt;aside&gt;</code> revisited</a> article.</p>
<p>Social links are the kind of thing that could be cut from a feed reader without diminishing the value of the content. As an additional note, remember that you can also place an <code>&lt;aside&gt;</code> at the end of an article, like a footer. In spite of its name, <code>&lt;aside&gt;</code> doesn&#8217;t have to be a sidebar.</p>
<p>Thanks, Bruce and Mike</p>
</section>
<section>
<h2>Sections with no visible titles</h2>
<p>Francesco asked:</p>
<blockquote>
<p>I have some divs, in a page, that semantically would make more sense as sections as there are natural headings for them, but they&#8217;re not written *inside* them, they&#8217;re outside. There are tabs to switch the visibility of these sections, and the tab navigation is on their left&#8230; so the actual title is only there, and not repeated inside of the sections. But besides that they really are sections&#8230; they even had &#8220;class=section&#8221; in the old version. <img src='http://html5doctor.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>What would the best solution be? Put an <code>h1</code> in there with <code>display:none</code>? It would make the outline correct, but it looks like a hack.</p>
</blockquote>
<p>You should reorganise your markup to ensure the headings are within the section. Then you can use some <abbr>CSS</abbr> positioning trickery to move them into place. Don&#8217;t use <code>display:none</code>, though, as that&#8217;s invisible to everyone, including assistive technologies like screen readers.</p>
<p>Cheers, Rich</p>
</section>
<section>
<h2><abbr>HTML</abbr>5 <code>&lt;canvas&gt;</code> and the <abbr>DOM</abbr></h2>
<p>Elango asked:</p>
<blockquote>
<p>Is it possible to get the elements i have drawn into the canvas using some API. Say for eg i draw 2 circle and 2 lines is that possible for me to get these information from Canvas by using DOM API&#8217;s</p>
</blockquote>
<p>No. Think of a canvas like a canvas you would physically paint on. If you paint a red stroke and then paint over that in blue, you can&#8217;t get back to the original red stroke. The canvas <abbr>API</abbr> works the same way: once your <strong>pixels</strong> are committed, you can&#8217;t go back.</p>
<p>You want <abbr>SVG</abbr> (or try out <a href="http://raphaeljs.com">Raphaël</a>).</p>
<p>Cheers, Remy</p>
</section>
<section>
<h2>Got a question for us?</h2>
<p>That wraps up this round of questions. If you&#8217;ve got a query about the <abbr>HTML</abbr>5 spec or how to implement it, you can <a href="http://html5doctor.com/ask-the-doctor/">get in touch</a> with us and we&#8217;ll do our best to help.</p>
</section>
<div id="crp_related">
<h3>Related Posts:</h3>
<ul class="related">
<li><a href="http://html5doctor.com/your-questions-answered-10/" rel="bookmark" class="crp_title">Your Questions Answered #10</a></li>
<li><a href="http://html5doctor.com/your-questions-answered-7/" rel="bookmark" class="crp_title">Your Questions Answered #7</a></li>
<li><a href="http://html5doctor.com/your-questions-14/" rel="bookmark" class="crp_title">Your Questions #14</a></li>
<li><a href="http://html5doctor.com/your-questions-15/" rel="bookmark" class="crp_title">Your Questions #15</a></li>
<li><a href="http://html5doctor.com/your-questions-13/" rel="bookmark" class="crp_title">Your Questions #13</a></li>
</ul>
</div>
<p><a href="http://html5doctor.com/your-questions-16/" rel="bookmark">Your Questions #16</a> originally appeared on <a href="http://html5doctor.com">HTML5 Doctor</a> on March 1, 2011.</p>
]]></content:encoded>
			<wfw:commentRss>http://html5doctor.com/your-questions-16/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>video + canvas = magic</title>
		<link>http://html5doctor.com/video-canvas-magic/</link>
		<comments>http://html5doctor.com/video-canvas-magic/#comments</comments>
		<pubDate>Wed, 20 Oct 2010 14:34:53 +0000</pubDate>
		<dc:creator>Tab Atkins Jnr</dc:creator>
				<category><![CDATA[Elements]]></category>
		<category><![CDATA[JavaScript APIs]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[HTML 5]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://html5doctor.com/?p=2554</guid>
		<description><![CDATA[<p>You've already learned about the <code>&#60;video&#62;</code> and <code>&#60;canvas&#62;</code> elements, but did you know that they were designed to be used together?  In fact, the two elements are absolutely wondrous when you combine them together.  I'm going to show off a few super-simple demos using the two elements together, which should help suggest cool future projects for you fellow web authors.</p>]]></description>
			<content:encoded><![CDATA[<p>You&#8217;ve already learned about the <a href="http://html5doctor.com/the-video-element/"><code>&lt;video&gt;</code></a> and <a href="http://html5doctor.com/an-introduction-to-the-canvas-2d-api/"><code>&lt;canvas&gt;</code></a> elements, but did you know that they were designed to be used together? In fact, the two elements are absolutely wondrous when you combine them! I&#8217;m going to show off a few super-simple demos using these two elements, which I hope will prompt cool future projects from you fellow web authors. (All of these demos work in every modern browser except Internet Explorer.)</p>

<h2>First, the basics</h2>

<p>If you&#8217;re just starting with <abbr>HTML</abbr>5, you may not be familiar with the <code>&lt;video&gt;</code> element and how to use it. Here&#8217;s a simple example that we&#8217;ll be using in the later demos:</p>

<pre><code>&lt;video controls loop&gt;
    &lt;source src=video.webm type=video/webm&gt;
    &lt;source src=video.ogg type=video/ogg&gt;
    &lt;source src=video.mp4 type=video/mp4&gt;
&lt;/video&gt;
</code></pre>

<p>The <code>&lt;video&gt;</code> element contains two attributes: <code>@controls</code> and <code>@loop</code>. <code>@controls</code> tells the browser to give the video the standard set of video controls: play/pause, scrubber, volume, etc. <code>@loop</code> tells the browser to start the video over again from the beginning once it ends.</p>

<p>Then, inside the <code>&lt;video&gt;</code> element, we have three child <code>&lt;source&gt;</code> elements, each pointing to a different encoding of the same video. The browser will try each source in order and play the first one that it understands.</p>

<p><a href="http://html5doctor.com/demos/video-canvas-magic/demo0.html">See this code in action</a>, playing the intro to one of the greatest cartoon series of all time.</p>

<figure>
  <img src="http://html5doctor.com/wp-content/uploads/2010/10/video_canvas_fig1.jpg" alt="" />
  <figcaption>HTML5 Video playing in Chrome</figcaption>
</figure>

<p>(A note about fallback: all of these demos assume that your browser has <code>&lt;video&gt;</code> support, which isn&#8217;t true in <abbr>IE</abbr>8 or earlier. Normally, it&#8217;s good practice to specify a Flash fallback or similar for those browsers, but that wouldn&#8217;t accomplish much here — all of the techniques I demonstrate rely on basic integration between the <code>&lt;video&gt;</code> element and the <code>&lt;canvas&gt;</code> element, which you can&#8217;t achieve with a Flash player. So I&#8217;ve omitted any non-<code>&lt;video&gt;</code> fallback content in these examples. I&#8217;ve still provided multiple sources, though, so all current browsers that do support <code>&lt;video&gt;</code> will be able to play it.)</p>

<h2>Now, a simple example</h2>

<p>Now that we know how to play a video, let&#8217;s mix in some <code>&lt;canvas&gt;</code> shenanigans. First, <a href="http://html5doctor.com/demos/video-canvas-magic/demo1.html">check out the demo</a>, then come back here for a code walkthrough.  I&#8217;ll wait.</p>

<p>&hellip;</p>

<figure>
  <img src="http://html5doctor.com/wp-content/uploads/2010/10/video_canvas_fig2.jpg" alt="" />
  <figcaption>Drawing video onto the canvas at full screen</figcaption>
</figure>

<p>Done? Cool! Now, how does this work? Surely it requires a few hundred lines of JavaScript, right? If you&#8217;ve cheated and already looked at the source code of the demo page, you&#8217;ll know how easy it is.</p>
  
<p>We start with this <abbr>HTML</abbr>:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;title&gt;Video/Canvas Demo 1&lt;/title&gt;

&lt;canvas id=c&gt;&lt;/canvas&gt;
&lt;video id=v controls loop&gt;
    &lt;source src=video.webm type=video/webm&gt;
    &lt;source src=video.ogg type=video/ogg&gt;

    &lt;source src=video.mp4 type=video/mp4&gt;
&lt;/video&gt;
</code></pre>

<p>Same video code as before, but now with a <code>&lt;canvas&gt;</code> element thrown into the mix. Kinda empty and useless at the moment, though. We&#8217;ll script it into action later.</p>

<p>Now, let&#8217;s pair that with some <abbr>CSS</abbr> to get things positioned right:</p>

<pre><code>&lt;style&gt;

body {
    background: black;
}

#c {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 100%;
}

#v {
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -180px 0 0 -240px;
}
&lt;/style&gt;
</code></pre>

<p>This just centers the video in the screen and stretches the canvas to the full width and height of the browser window. Since the canvas comes first in the document, it&#8217;ll be behind the video, exactly where we want it.</p>

<p>Now, here comes the magic!</p>

<pre><code>&lt;script&gt;
document.addEventListener('DOMContentLoaded', function(){
    var v = document.getElementById('v');
    var canvas = document.getElementById('c');
    var context = canvas.getContext('2d');

    var cw = Math.floor(canvas.clientWidth / 100);
    var ch = Math.floor(canvas.clientHeight / 100);
    canvas.width = cw;
    canvas.height = ch;

    v.addEventListener('play', function(){
        draw(this,context,cw,ch);
    },false);

},false);

function draw(v,c,w,h) {
    if(v.paused || v.ended) return false;
    c.drawImage(v,0,0,w,h);
    setTimeout(draw,20,v,c,w,h);
}
&lt;/script&gt;
</code></pre>

<p>Take that in for a moment. Just breathe deeply and absorb it. So short, so sweet, so pretty&hellip;</p>

<p>Now, let&#8217;s step through it.</p>

<pre><code>    var v = document.getElementById('v');
    var canvas = document.getElementById('c');
    var context = canvas.getContext('2d');

    var cw = Math.floor(canvas.clientWidth / 100);
    var ch = Math.floor(canvas.clientHeight / 100);
    canvas.width = cw;
    canvas.height = ch;
</code></pre>

<p>This part is simple. I grab hold of the video and canvas elements on the page, and I grab the canvas&#8217;s 2D-context so I can draw on it. Then I do some quick calculating to find out how wide and tall I want the canvas&#8217;s drawing surface to be. The <code>&lt;canvas&gt;</code> element itself is already stretched to the size of the screen via <abbr>CSS</abbr>, so this&#8217;ll make each pixel of the drawing surface equal to about 100&#215;100 pixels on the screen.</p>

<p>That last bit may need some explanation if you&#8217;re new to canvas. Normally, the visual size and the drawing-surface size of a <code>&lt;canvas&gt;</code> element will be the same. In that case, drawing a line 50px long will display a line 50px long. But that doesn&#8217;t have to be true — you can set the drawing surface&#8217;s size through the <code>@width</code> and <code>@height</code> properties on the <code>&lt;canvas&gt;</code> element itself, and then change the visual size of the canvas with <abbr>CSS</abbr> to be something different. The browser will then automatically upscale or downscale the drawing appropriately to make the drawing surface fill the visual size. In this case, I&#8217;m setting the drawing surface of the canvas to be very small — on most screens, it&#8217;ll be about 10px wide and 7px tall — and then stretching the visual size with <abbr>CSS</abbr> so that each pixel I draw gets blown up 100-fold by the browser. That&#8217;s what causes the cool visual effect in the demo.</p>

<pre><code>    v.addEventListener('play', function(){
        draw(v,context,cw,ch);
    },false);
</code></pre>

<p>Another simple part. Here I attach some code to the &#8220;play&#8221; event on the video element. This event gets fired whenever the user hits the &#8220;play&#8221; button to start watching the video. All I do is call the <code>draw()</code> function with the appropriate parameters: the video itself, the canvas&#8217;s drawing context, and the canvas&#8217;s width and height.</p>

<pre><code>function draw(v,c,w,h) {
    if(v.paused || v.ended) return false;
    c.drawImage(v,0,0,w,h);
    setTimeout(draw,20,v,c,w,h);
}
</code></pre>

<p>The first line just makes the function stop immediately if the user pauses or stops the video, so it&#8217;s not burning <abbr>CPU</abbr> when nothing&#8217;s changing. The third line calls the <code>draw()</code> function again, allowing the browser a little breathing space to do other things like update the video itself. I&#8217;m putting in a 20ms delay, so we&#8217;ll get roughly 50fps, which is more than enough.</p>

<p>The second line is where the magic happens — it draws the current frame of the video directly onto the canvas.  Yes, it&#8217;s exactly as simple as it looks. Just pass the video element and the x, y, width, and height of the rectangle on the canvas you want it to draw into. In this case, it&#8217;s filling up the entire canvas, but you could do less (or more!) if you wanted.</p>

<p>I&#8217;m using another trick here. Remember how the canvas is really tiny? The video will be at least 20 times bigger than the canvas on most screens, so how do we draw it onto such a tiny canvas? The <code>drawImage()</code> function handles that for us — it automatically scales whatever you hand it in the first argument so that it fills the rectangle you specify. That means we authors don&#8217;t have to worry about averaging the pixel colors (or extrapolating, if you&#8217;re drawing a small video into a big rectangle) because the browser does it all for us. I&#8217;ll use this trick more in the future, so watch out for it.</p>

<p>And&hellip;that&#8217;s it! The entire demo is done in 20 lines of easy-to-read JavaScript code, instantly producing a nifty background effect for any video you wish to play. You can trivially adjust the size of the &#8220;pixels&#8221; on the canvas by adjusting the lines that set the <var>cw</var> and <var>ch</var> variables.</p>

<h2>Directly manipulating video pixels</h2>

<p>The last demo was cool, but it just let the browser do all the heavy lifting. The browser downscaled the video, drew it onto the canvas, and then upscaled the canvas pixels, all automatically. Let&#8217;s try our hand at doing some of this ourselves! <a href="http://html5doctor.com/demos/video-canvas-magic/demo2.html">Check out the demo</a> to see this in action, where I convert the video to grayscale on the fly.</p>

<figure>
  <img src="http://html5doctor.com/wp-content/uploads/2010/10/video_canvas_fig3.jpg" alt="" />
  <figcaption>Video made greyscale with canvas manipulation</figcaption>
</figure>

<p>The HTML for the page is basically identical:</p>

<pre><code>&lt;video id=v controls loop&gt;
    &lt;source src=video.webm type=video/webm&gt;
    &lt;source src=video.ogg type=video/ogg&gt;
    &lt;source src=video.mp4 type=video/mp4&gt;
&lt;/video&gt;
&lt;canvas id=c&gt;&lt;/canvas&gt;

</code></pre>

<p>Nothing new here, so let&#8217;s move onto the script.</p>

<pre><code>document.addEventListener('DOMContentLoaded', function(){
    var v = document.getElementById('v');
    var canvas = document.getElementById('c');
    var context = canvas.getContext('2d');
    var back = document.createElement('canvas');
    var backcontext = back.getContext('2d');

    var cw,ch;

    v.addEventListener('play', function(){
        cw = v.clientWidth;
        ch = v.clientHeight;
        canvas.width = cw;
        canvas.height = ch;
        back.width = cw;
        back.height = ch;
        draw(v,context,backcontext,cw,ch);
    },false);

},false);

function draw(v,c,bc,w,h) {
    if(v.paused || v.ended) return false;
    // First, draw it into the backing canvas
    bc.drawImage(v,0,0,w,h);
    // Grab the pixel data from the backing canvas
    var idata = bc.getImageData(0,0,w,h);
    var data = idata.data;
    // Loop through the pixels, turning them grayscale
    for(var i = 0; i &lt; data.length; i+=4) {
        var r = data[i];
        var g = data[i+1];
        var b = data[i+2];
        var brightness = (3*r+4*g+b)&gt;&gt;&gt;3;
        data[i] = brightness;
        data[i+1] = brightness;
        data[i+2] = brightness;
    }
    idata.data = data;
    // Draw the pixels onto the visible canvas
    c.putImageData(idata,0,0);
    // Start over!
    setTimeout(function(){ draw(v,c,bc,w,h); }, 0);
}
</code></pre>

<p>The script is a bit longer this time, because we&#8217;re actually doing some work. But it&#8217;s still really simple!</p>

<pre><code>document.addEventListener('DOMContentLoaded', function(){
    var v = document.getElementById('v');
    var canvas = document.getElementById('c');
    var context = canvas.getContext('2d');
    var back = document.createElement('canvas');
    var backcontext = back.getContext('2d');

    var cw,ch;

    v.addEventListener('play', function(){
        cw = v.clientWidth;
        ch = v.clientHeight;
        canvas.width = cw;
        canvas.height = ch;
        back.width = cw;
        back.height = ch;
        draw(v,context,backcontext,cw,ch);
    },false);
</code></pre>

<p>This is almost the same as I had before, with two real differences.</p>

<p>First, I&#8217;m creating a second canvas and pulling the context out of it as well. This is a &#8220;backing canvas&#8221;, which I&#8217;ll use to perform intermediate operations before painting the final result into the visible canvas in the markup. The backing canvas doesn&#8217;t even need to be added to the document. It can just hang out here in my script. This strategy will be used a lot in later examples, and it&#8217;s quite useful in general, so take note of it.</p>

<p>Second, I&#8217;m waiting to resize the canvases until the video is played, rather than just sizing them immediately.  This is because the <code>&lt;video&gt;</code> element probably hasn&#8217;t loaded its video up when the <code>DOMContentLoaded</code> event fires, so it&#8217;s still using the default size for the element. By the time it&#8217;s ready to play, though, it knows the size of the video and has sized itself appropriately. At that point, we can set up the canvases to be the same size as the video.</p>

<pre><code>function draw(v,c,bc,w,h) {
    if(v.paused || v.ended) return false;
    bc.drawImage(v,0,0,w,h);
</code></pre>

<p>Same as the first demo, the <code>draw()</code> function begins by checking if it should stop, then just draws the video onto a canvas. Note that I&#8217;m drawing it onto the <em>backing</em> canvas, which, again, is just sitting in my script and isn&#8217;t displayed in the document. The visible canvas is reserved for the displaying the grayscale version, so I use the backing canvas to load up the initial video data.</p>

<pre><code>    var idata = bc.getImageData(0,0,w,h);
    var data = idata.data;
</code></pre>

<p>Here&#8217;s the first new bit. You can draw something onto a canvas with either the normal canvas drawing functions or <code>drawImage()</code>, or you can just manipulate the pixels directly through the ImageData object.  <code>getImageData()</code> returns the pixels from a rectangle of the canvas. In this case, I&#8217;m just getting the whole thing.</p>

<p><strong>Warning!</strong> If you&#8217;re following along and trying to run these demos on your desktop, this is where you&#8217;ll probably run into trouble. The <code>&lt;canvas&gt;</code> element keeps track of where the data inside of it comes from, and if it knows that you got something from another website (for example, if the <code>&lt;video&gt;</code> element you painted into the canvas is pointing to a cross-origin file), it&#8217;ll &#8220;taint&#8221; the canvas. You&#8217;re not allowed to grab the pixel data from a tainted canvas. Unfortunately, <code>file:</code> urls count as &#8220;cross-origin&#8221; for this purpose, so you can&#8217;t run this on your desktop. Either fire up a web server on your computer and view the page from localhost, or upload it to some other server you control.</p>

<pre><code>    for(var i = 0; i &lt; data.length; i+=4) {
        var r = data[i];
        var g = data[i+1];
        var b = data[i+2];
</code></pre>

<p>Now, a quick note about the ImageData object. It returns the pixels in a special way in order to make them easy to manipulate. If you have, say, a 100&#215;100 pixel canvas, it contains a total of 10,000 pixels. The ImageData array for it will then have 40,000 elements, because the pixels are broken up by component and listed sequentially. Each group of four elements in the ImageData array represent the red, green, blue, and alpha channels for that pixel. To loop through the pixels, just increment your counter by 4 every time, like I do here. Each channel, then, is an integer between 0 and 255.</p>

<pre><code>        var brightness = (3*r+4*g+b)&gt;&gt;&gt;3;
        data[i] = brightness;
        data[i+1] = brightness;
        data[i+2] = brightness;
</code></pre>

<p>Here, a quick bit of math converts the <abbr>RGB</abbr> value of the pixel into a single &#8220;brightness&#8221; value. As it turns out, our eyes respond most strongly to green light, slightly less so to red, and much less so to blue. So, I weight the channels appropriately before taking the average. Then, we just feed that single value back to all three channels. As we probably all know, when the red, green, and blue values of a color are equal, you get gray. (During this whole process, I&#8217;m completely ignoring the fourth member of each group, the alpha channel, because it&#8217;s always going to be 255.)</p>

<pre><code>    idata.data = data;
</code></pre>

<p>Shove the modified pixel array back into the ImageData object&hellip;</p>

<pre><code>    c.putImageData(idata,0,0);
</code></pre>

<p>&hellip;and then shove the whole thing into the visible canvas! We didn&#8217;t need to do any complicated drawing at all! Just grab the pixels, manipulate them, and shove them back in. So easy!</p>

<p>A final note: real-time full-video pixel manipulation is one of those rare places where micro-optimizations actually matter. You can see their effects in my code here. Originally, I didn&#8217;t pull the pixel data out of the ImageData object, and just wrote &#8220;var r = idata.data[i];&#8221; and so on each time, which meant several extra property lookups in every iteration of the loop. I also originally just divided the brightness by 8 and floored the value, which is slightly slower than bit-shifting by 3 places. In normal code, these sorts of things are <em>completely</em> insignificant, but when you&#8217;re doing them several million times per second (the video is 480&#215;360, and thus contains nearly 200,000 pixels, each of which is individually handled roughly 100 times a second), those tiny delays add up into a noticeable lag.</p>

<h2>More advanced pixel manipulation</h2>

<p>You can operate on more than just a single pixel at a time, too, composing some fairly complex visual effects. As I noted at the end of the previous section, performance matters a lot here, but you&#8217;d be surprised what you can squeeze out with a little creativity. <a href="http://html5doctor.com/demos/video-canvas-magic/demo4.html">As you can see in the demo</a>, I&#8217;ll be creating an emboss effect in this example, which requires you to use several input pixels together to compute the value of each output pixel.</p>

<figure>
  <img src="http://html5doctor.com/wp-content/uploads/2010/10/video_canvas_fig4.jpg" alt="" />
  <figcaption>Embossed video with canvas manipulation</figcaption>
</figure>

<p>Here&#8217;s the code. The <abbr>HTML</abbr> and most of the beginning code is identical to the previous example, so I&#8217;ve omitted everything but the <code>draw()</code> function:</p>

<pre><code>function draw(v,c,bc,cw,ch) {
    if(v.paused || v.ended) return false;
    // First, draw it into the backing canvas
    bc.drawImage(v,0,0,cw,ch);
    // Grab the pixel data from the backing canvas
    var idata = bc.getImageData(0,0,cw,ch);
    var data = idata.data;
    var w = idata.width;
    var limit = data.length
    // Loop through the subpixels, convoluting each using an edge-detection matrix.
    for(var i = 0; i &lt; limit; i++) {
        if( i%4 == 3 ) continue;
        data[i] = 127 + 2*data[i] - data[i + 4] - data[i + w*4];
    }
    // Draw the pixels onto the visible canvas
    c.putImageData(idata,0,0);
    // Start over!
    setTimeout(draw,20,v,c,bc,cw,ch);
}
</code></pre>

<p>Now let&#8217;s step through that.</p>

<pre><code>function draw(v,c,bc,cw,ch) {
    if(v.paused || v.ended) return false;
    // First, draw it into the backing canvas
    bc.drawImage(v,0,0,cw,ch);
    // Grab the pixel data from the backing canvas
    var idata = bc.getImageData(0,0,cw,ch);
    var data = idata.data;
</code></pre>

<p>Same as the last example. Check to see if we should stop, then draw the video onto the backing canvas and grab the pixel data from it.</p>

<pre><code>var w = idata.width;
</code></pre>

<p>The significance of this line needs some explanation. I&#8217;m already passing the canvas&#8217;s width into the function (as the <var>cw</var> variable), so why am I re-measuring its width here? Well, I was actually lying to you earlier when I explained how large the pixel array will be. The browser <em>might</em> have one pixel of canvas map to one pixel of ImageData, but browsers are allowed to use higher resolutions in the image data, representing each pixel of canvas as a 2&#215;2 block of ImageData pixels, or maybe 3&#215;3, or maybe even greater!</p>

<p>If they use a &#8220;high-resolution backing store&#8221;, as this is called, it means better display, as aliasing artifacts (jagged edges on diagonal lines) become much smaller and less noticeable. It also means that rather than a 100&#215;100 pixel canvas giving you an ImageData.data object with 40,000 numbers, it might have 160,000 numbers instead. By asking the ImageData for its width and height, we ensure that we loop through the pixel data properly no matter whether the browser uses a low-res or high-res backing store for it.</p>

<p>It&#8217;s very important that you use this properly whenever you need the width or height of the data you pulled out as an ImageData object. If too many people screw it up and just use the canvas&#8217;s width and height instead, then browsers will be forced to always use a low-res backing store to be compatible with those broken scripts!</p>

<pre><code>    var limit = data.length;
    for(var i = 0; i &lt; limit; i++) {
        if( i%4 == 3 ) continue;
        data[i] = 127 + 2*data[i] - data[i + 4] - data[i + w*4];
    }
</code></pre>

<p>I&#8217;m grabbing the data&#8217;s length and stuffing it into a variable, so I don&#8217;t have to pay for a property access on every single iteration of the loop. (Remember, micro-optimizations matter when you&#8217;re doing real-time video manipulation!) Then I just loop through the pixels, like I did before. If the pixel happens to be for the alpha channel (every fourth number in the array), I can just skip it — I don&#8217;t want to change the transparency. Otherwise, I&#8217;ll do a little math to find the difference between the current pixel&#8217;s color channel and the similar channels of the pixels below and to the right, then just combine that difference with the &#8220;average&#8221; gray value of 127. This has the effect of making areas where the pixels are the same color a flat medium gray, but edges where the color suddenly changes will turn either bright or dark.</p>

<p>There&#8217;s another optimization here. Because I&#8217;m only comparing the current pixel with pixels &#8220;further ahead&#8221; in the data which I haven&#8217;t looked at yet, I can just store the changed value right back in the original data, because nothing will ever look at the current pixel&#8217;s data again after this point. This means I don&#8217;t have to allocate a big array to hold the results before turning it back into an ImageData object.</p>

<pre><code>    c.putImageData(idata,0,0);
    setTimeout(draw,20,v,c,bc,cw,ch);
</code></pre>

<p>Finally, draw the modified ImageData object into the visible canvas, and set up another call to the function in 20 milliseconds. This is the same as the previous example.</p>

<h2>Wrapping up</h2>

<p>So, we&#8217;ve explored the basics of combining <abbr>HTML</abbr>5&#8242;s <code>&lt;canvas&gt;</code> and <code>&lt;video&gt;</code> elements today. The demos were very basic, but they illustrated all the essential techniques you&#8217;ll need to do something even cooler on your own:</p>

<ol>
<li>You can draw a video directly onto a canvas.</li>
<li>When you draw onto a canvas, the browser will automatically scale the image for you if necessary.</li>
<li>When you display a canvas, the browser will again scale it automatically if the visible size is different from the size of the backing-store.</li>
<li>You can do direct pixel-level manipulation of a canvas by just grabbing the ImageData, changing it, and drawing it back in.</li>
</ol>

<p>In Part 2 of this article [Ed: coming soon!], I&#8217;ll explore some more interesting applications of video/canvas integration, including a real-time video-to-ASCII converter!</p>
<div id="crp_related"><h3>Related Posts:</h3><ul class="related"><li><a href="http://html5doctor.com/an-introduction-to-the-canvas-2d-api/" rel="bookmark" class="crp_title">An introduction to the Canvas 2D API</a></li><li><a href="http://html5doctor.com/the-video-element/" rel="bookmark" class="crp_title">The video element</a></li><li><a href="http://html5doctor.com/review-html5-now-dvd/" rel="bookmark" class="crp_title">Review: HTML5 Now (DVD)</a></li><li><a href="http://html5doctor.com/your-questions-16/" rel="bookmark" class="crp_title">Your Questions #16</a></li><li><a href="http://html5doctor.com/native-drag-and-drop/" rel="bookmark" class="crp_title">Native Drag and Drop</a></li></ul></div><p><a href="http://html5doctor.com/video-canvas-magic/" rel="bookmark">video + canvas = magic</a> originally appeared on <a href="http://html5doctor.com">HTML5 Doctor</a> on October 20, 2010.</p>
]]></content:encoded>
			<wfw:commentRss>http://html5doctor.com/video-canvas-magic/feed/</wfw:commentRss>
		<slash:comments>47</slash:comments>
		</item>
		<item>
		<title>Your Questions #13</title>
		<link>http://html5doctor.com/your-questions-13/</link>
		<comments>http://html5doctor.com/your-questions-13/#comments</comments>
		<pubDate>Tue, 28 Sep 2010 14:15:52 +0000</pubDate>
		<dc:creator>Richard Clark</dc:creator>
				<category><![CDATA[Questions]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[custom tags]]></category>
		<category><![CDATA[h1]]></category>
		<category><![CDATA[header]]></category>
		<category><![CDATA[hgroup]]></category>
		<category><![CDATA[HTML 5]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[seo]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://html5doctor.com/?p=2382</guid>
		<description><![CDATA[The clinic is getting busy with more <abbr>HTML</abbr>5 ailments. This week, we'll cover server-side validation, immutable images with <code>&#60;canvas&#62;</code>, retrieving drawn objects from a <code>&#60;canvas&#62;</code>, creating custom tags, the <code>role</code> attribute, and the effects of <code>&#60;hgroup&#62;</code> on <abbr>SEO</abbr>.]]></description>
			<content:encoded><![CDATA[<p><img src="http://html5doctor.com/wp-content/uploads/2009/07/html5doctor-treatment.gif" alt="Doctor treating a patient illustration" class="alignright size-full wp-image-424" /> The clinic is getting busy with more <abbr>HTML</abbr>5 ailments. This week, we&#8217;ll cover server-side validation, immutable images with <code>&lt;canvas&gt;</code>, retrieving drawn objects from a <code>&lt;canvas&gt;</code>, creating custom tags, the <code>role</code> attribute, and the effects of <code>&lt;hgroup&gt;</code> on <abbr>SEO</abbr>.</p>
<section>
<h2>Server-side validation</h2>
<p>Brian asked:</p>
<blockquote>
<p>We currently use <abbr>PHP</abbr>-Tidy to validate the <abbr>HTML</abbr> mark-up of content regions of our site. Our content regions have images that represent modules in our system that get translated into code for things like processing video, forms, etc. I&#8217;ve been making the move to upgrade these modules to use new <abbr>HTML</abbr>5 elements, but I&#8217;m finding that Tidy is stripping out these elements and I&#8217;m doing a lot of dancing around these issues right now. Are you aware of any server-side processing scripts that have been updated to work with <abbr>HTML</abbr>5 and perhaps with <abbr>ARIA</abbr> support as well?</p>
</blockquote>
<p>The current contender is <a href="http://code.google.com/p/html5lib/">html5lib</a>. The <abbr>PHP</abbr> version is v0.1, so <abbr title="Your Mileage May Vary">YMMV</abbr>.</p>
<p>It seems that <a href="http://lists.w3.org/Archives/Public/html-tidy/2010JanMar/0005.html"><abbr>HTML</abbr>5 support won’t be coming to Tidy anytime soon</a>.</p>
<p>Good luck! Peace &#8211; Oli</p>
</section>
<section>
<h2>Immutable image with <code>&lt;canvas&gt;</code></h2>
<p>Mike asked:</p>
<blockquote>
<p>We do have a medical app and we want to use <code>&lt;canvas&gt;</code> to let docs draw over a still picture.  The only thing I cannot do is keep the picture unchanged when they use an &#8220;eraser&#8221; (basically a white pen). I haven&#8217;t found any example on the web so may be it&#8217;s impossible. It seems that the background image either part of the <code>canvas</code> of a <code>div</code> containing the canvas becomes part of the canvas itself. If it&#8217;s possible what&#8217;s the secret? Thanks</p>
</blockquote>
<p>This is possible in a variety of ways. Here&#8217;s a quick demonstration of <a href="http://jsbin.com/icumi3/2">one of the solutions</a>. Open the demo up in Firefox 3.6 or later, drop an image in the box, draw over it, and click the &#8220;Drawn image as PNG&#8221; button to retrieve what you drew (without the background image).</p>
<p>Here&#8217;s what&#8217;s happening: the canvas is sitting inside of a <code>&lt;div&gt;</code> containing my still image, but I&#8217;m drawing on the nested <code>&lt;canvas&gt;</code> (I think you had it the other way around, which was causing your problems).</p>
<p>Hope that helps, Remy</p>
</section>
<section>
<h2>Retrieving objects from <code>&lt;canvas&gt;</code></h2>
<p>DJ asked:</p>
<blockquote>
<p>I wanted to know if there is any way in which the drawn objects say rectangle, circle, line, &hellip; within canvas can be identified based on the selection at later point in time (after they are drawn).</p>
<p>If there are no direct <abbr>API</abbr>s how could we achieve it. Do we have to store the co-ordinates of each of the created object within and do the object identification based on mouse cursor? Thanks</p>
</blockquote>
<p>Unfortunately, there&#8217;s no way to retrieve these objects unless you write your own system to handle it. There&#8217;s no native support for this in <code>&lt;canvas&gt;</code>.</p>
<p>It sounds like you actually need to use <abbr>SVG</abbr>, which will allow to you to hook event listeners and query the <abbr>DOM</abbr> tree that&#8217;s created. If you need some convincing that <abbr>SVG</abbr> is the right tool for the job, have a look at the <a href="http://raphaeljs.com/">Raphaël JavaScript Library</a>. It uses <abbr>SVG</abbr> exclusively and is able to create some very impressive drawings and animations.</p>
<p>Cheers, Remy</p>
</section>
<section>
<h2>Custom tags</h2>
<p>Mike asked:</p>
<blockquote>
<p>So by using some JavaScript I can insert unfamiliar tags into <abbr>IE</abbr>, and using <abbr>CSS</abbr> I can format them. My question is why stop with <abbr>HTML</abbr>5 tags like section and nav? What are the pros and cons of custom tags like <code>&lt;content&gt;</code>, <code>&lt;story&gt;</code>, <code>&lt;comment&gt;</code>, <code>&lt;blog&gt;</code> or even <code>&lt;mike&gt;</code>, <code>&lt;was&gt;</code>, <code>&lt;here&gt;</code>?</p>
</blockquote>
<p>Custom elements go against having a standard like <abbr>HTML</abbr>5. Standards map out the set of elements, attributes, and <abbr>API</abbr>s that the browsers need to implement so web developers can use them, and they provide those developers with a common approach to marking up a web page.</p>
<p>If custom elements were allowed, we would have an infinite number of ways to mark up content, many of which would share a common goal but require different implementations. As an example, here is a number of different elements I can dream up for some primary content: <code>&lt;article&gt;</code>, <code>&lt;blog&gt;</code>, <code>&lt;entry&gt;</code>, <code>&lt;post&gt;</code>, <code>&lt;page&gt;</code>, <code>&lt;main&gt;</code>, <code>&lt;primary&gt;</code>, <code>&lt;content&gt;</code>, <code>&lt;document&gt;</code>, <code>&lt;doc&gt;</code>, <code>&lt;blogpost&gt;</code>, <code>&lt;publication&gt;</code>, <code>&lt;thenameofthearticlewithnospacesorpunctuation&gt;</code>, <code>&lt;item&gt;</code>, <code>&lt;block&gt;</code>, <code>&lt;blob&gt;</code>, <code>&lt;text&gt;</code>, <code>&lt;txt&gt;</code>, and <code>&lt;paper&gt;</code>. Many of them are bad ideas, but hopefully you see my point. This doesn&#8217;t even account for the more predictable <code>&lt;contentone&gt;</code>, <code>&lt;contenttwo&gt;</code>, <code>&lt;articlefifty&gt;</code> that would likely also be used.</p>
<p>This sort of markup would make <abbr>HTML</abbr> a nightmare to maintain. A developer coming into an existing site would have to learn which elements have been used and what their purpose is. And it&#8217;s not just painful for developers. Browser vendors would have to find ways to parse these elements and define how they should be used. Is this element supposed to be block-level? Is it interactive? Should it impact the document outline? And what about search engines? How do they know that <code>&lt;myobscureelement&gt;</code> defines the most important content on the page, the content that should really be indexed?</p>
<p>Standards narrow the possibilities and ensure developers, browsers, and machines (search engines and the like) are all speaking the same language. Many people spend a great deal of time debating the specification, trying to reach consensus on which proposals should be standardized and how they should be implemented.</p>
<p>So stick to the standards! They exist for everyone&#8217;s benefit. As browsers continue to implement the specification correctly (even <abbr>IE</abbr> is catching up), our jobs will be made easier and we can spend more time creating really cool things <img src='http://html5doctor.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>John Alsopp&#8217;s fantastic article <a href="http://www.alistapart.com/articles/semanticsinhtml5">Semantics in <abbr>HTML</abbr>5</a> goes into more detail about this issue. You can also see where some of the element names came from by looking at the work Hixie did researching <a href="http://code.google.com/webstats/2005-12/classes.html">class names in Google&#8217;s index</a>.</p>
<p>Regards, Mike</p>
</section>
<section>
<h2>The <code>role</code> attribute, SEO, and <code>&lt;hgroup&gt;</code></h2>
<p>Robson asked:</p>
<blockquote>
<p>What about the role attribute? Was it dropped from specification? What will be the &#8220;role&#8221; of the role attribute in <abbr>HTML</abbr>5?</p>
<p>Today, just the home page should have the name of the site into a H1 element. On others pages, the H1 should be used to the title of the articles. How do search engines interpret the HGROUP and multiples HEADER and H1 elements today? How to implement the HGROUP and the HEADER today without impact the SEO? Thanks, guys!</p>
</blockquote>
<p>To answer your first three questions, <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/content-models.html#annotations-for-assistive-technology-products-(aria)"><code>role</code> is in</a>. You can use it belt-and-suspenders style until assistive technology catches up with <abbr>HTML</abbr>5. Just be careful: “<q>Authors may use the <abbr>ARIA</abbr> role and aria-* attributes on <abbr>HTML</abbr> elements, in accordance with the requirements described in the <abbr>ARIA</abbr> specifications, except where these conflict with the strong native semantics described below</q>”.</p>
<p>For the second part of your question, that&#8217;s not true. You can use more than one <code>&lt;h1&gt;</code> in <abbr>HTML</abbr> 4/<abbr>XHTML</abbr> 1. It&#8217;s not advised to make every heading <code>&lt;h1&gt;</code> in <abbr>HTML</abbr> 4/<abbr>XHTML</abbr> 1 (because historically some people did that to spam), but it may be appropriate in some cases — e.g., site name and page title. Using two <code>&lt;h1&gt;</code>&#8216;s on a single page has no effect on <abbr>SEO</abbr>.</p>
<p>With regard to <code>&lt;hgroup&gt;</code> and <code>&lt;header&gt;</code>, you’re asking the wrong question. Search engines care about high-quality, relevant content. Search engines penalise spamming, but writing markup according to the specification is not spamming. The <abbr>HTML</abbr>5 editor works at Google, so they’re well aware of the spec. html5doctor.com <em>has</em> implemented <code>&lt;hgroup&gt;</code> and <code>&lt;header&gt;</code>, and it hasn&#8217;t hurt our search engine rankings any <img src='http://html5doctor.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>For more, see our articles on <a href="http://html5doctor.com/the-header-element/">the header element</a> and <a href="http://html5doctor.com/the-hgroup-element/">the hgroup element</a>.</p>
<p>You probably don’t want to use <code>&lt;h1&gt;</code> everywhere anyhow, as <abbr>CSS</abbr> selectors are not that smart. <em>If</em> you wrap every <code>&lt;h1&gt;</code>-<code>&lt;h6&gt;</code> in a sectioning element (<code>&lt;section&gt;</code>, <code>&lt;article&gt;</code>, <code>&lt;nav&gt;</code>, <code>&lt;aside&gt;</code>), you don’t have to worry about keeping a logical order for your headings. Doing this means you don’t need to overwrite <abbr>CSS</abbr> as much. The old style, however, with the requirement to keep a logical order for your headings, still works.</p>
<p>Again, you&#8217;re concerned with the wrong thing. Good SEO = good content. Worrying about placement or what search engines think is a waste of time — worry about good content.</p>
<p>Peace &#8211; Oli</p>
</section>
<section>
<h2>Got a question for us?</h2>
<p>That wraps up this round of questions. If you&#8217;ve got a query about the <abbr>HTML</abbr>5 spec or how to implement it, you can <a href="http://html5doctor.com/ask-the-doctor/">get in touch</a> with us and we&#8217;ll do our best to help.</p>
</section>
<div id="crp_related">
<h3>Related Posts:</h3>
<ul class="related">
<li><a href="http://html5doctor.com/your-questions-answered-11/" rel="bookmark" class="crp_title">Your Questions Answered #11</a></li>
<li><a href="http://html5doctor.com/your-questions-16/" rel="bookmark" class="crp_title">Your Questions #16</a></li>
<li><a href="http://html5doctor.com/your-questions-answered-7/" rel="bookmark" class="crp_title">Your Questions Answered #7</a></li>
<li><a href="http://html5doctor.com/your-questions-5/" rel="bookmark" class="crp_title">Your Questions Answered #5</a></li>
<li><a href="http://html5doctor.com/your-questions-18/" rel="bookmark" class="crp_title">Your Questions 18</a></li>
</ul>
</div>
<p><a href="http://html5doctor.com/your-questions-13/" rel="bookmark">Your Questions #13</a> originally appeared on <a href="http://html5doctor.com">HTML5 Doctor</a> on September 28, 2010.</p>
]]></content:encoded>
			<wfw:commentRss>http://html5doctor.com/your-questions-13/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>An introduction to the Canvas 2D API</title>
		<link>http://html5doctor.com/an-introduction-to-the-canvas-2d-api/</link>
		<comments>http://html5doctor.com/an-introduction-to-the-canvas-2d-api/#comments</comments>
		<pubDate>Tue, 03 Aug 2010 13:30:06 +0000</pubDate>
		<dc:creator>Remy Sharp</dc:creator>
				<category><![CDATA[Elements]]></category>
		<category><![CDATA[JavaScript APIs]]></category>
		<category><![CDATA[2d context]]></category>
		<category><![CDATA[canvas]]></category>

		<guid isPermaLink="false">http://html5doctor.com/?p=2264</guid>
		<description><![CDATA[<p>If the video element is the poster boy of <abbr>HTML</abbr>5, then canvas is definitely Danny Zuko. The canvas element is (still) part of the HTML5 specification, but the 2D drawing <abbr>API</abbr> has been moved into a separate document (in case you go looking and can't find it).</p>]]></description>
			<content:encoded><![CDATA[<p>If the <code>&lt;video&gt;</code> element is the poster boy of <abbr>HTML</abbr>5, then <code>&lt;canvas&gt;</code> is definitely <a href="http://en.wikipedia.org/wiki/Danny_Zuko">Danny Zuko</a>. The <code>&lt;canvas&gt;</code> element is (still) <a href="http://dev.w3.org/html5/spec/the-canvas-element.html#the-canvas-element">part of the HTML5 specification</a>, but the <a href="http://dev.w3.org/html5/2dcontext/">2D drawing <abbr>API</abbr></a> has been moved into a separate document (in case you go looking and can&#8217;t find it).</p>

<p><code>&lt;canvas&gt;</code> has a wealth of features, like:</p>

<ul>
<li>drawing shapes,</li>
<li>filling colours,</li>
<li>creating gradients and patterns,</li>
<li>rendering text,</li>
<li>copying images, video frames, and other canvases,</li>
<li>manipulating pixels, and</li>
<li>exporting the contents of a <code>&lt;canvas&gt;</code> to a static file. </li>
</ul>

<p>In fact, the canvas <abbr>API</abbr> is so interesting, I wouldn&#8217;t be surprised to see entire books dedicated to it (and no, I don&#8217;t plan to write <em>that</em> book!).</p>

<p>It&#8217;s important when working with <code>&lt;canvas&gt;</code> to treat it like a real painting canvas. Say you lay down a strip of red paint on a real canvas. If you paint over it in blue, you can&#8217;t get back to your original red paint. It&#8217;s the same with the canvas element. There&#8217;s no concept of layers. The <code>&lt;canvas&gt;</code> element is a <strong>bitmap</strong> drawing <abbr>API</abbr>, and once you&#8217;ve committed to a set of pixels, you&#8217;re stuck with them. </p>

<p>Four of the Big Five browsers support canvas. We&#8217;re naturally missing <abbr>IE</abbr>8, but there&#8217;s hope: <abbr>IE</abbr>9 <em>does</em> support canvas. In fact, it supports <em>hardware accelerated drawing to the canvas</em> — other browsers currently don&#8217;t, making <abbr>IE</abbr>9 preview 3 the fastest (canvas) kid on the block!</p>

<h2>Always consider the options</h2>

<p>Before we dive in to the canvas <abbr>API</abbr>, I want to remind you to make sure you&#8217;re using the right technology for the job.</p>

<p><a href="http://www.w3.org/TR/SVG/"><abbr>SVG</abbr></a> is the alternative drawing <abbr>API</abbr>. It&#8217;s vector-based and <em>does</em> support layers. <abbr>SVG</abbr> also exists in the <abbr>DOM</abbr>, making it easy to attach event handlers for interactivity, and it&#8217;s easier to deal with collision detection (in games, for example). It also supports animation either through <abbr>SMIL</abbr> or JavaScript. There&#8217;s an excellent JavaScript library called <a href="http://raphaeljs.com/">Raphaël</a> that uses <abbr>SVG</abbr> to render images and animations.</p>

<p>The <code>&lt;canvas&gt;</code> element is good for pixel manipulation and highly active animations. Brad Neuberg does a <a href="http://www.youtube.com/watch?v=siOHh0uzcuY#t=17m00s">really good job of explaining the differences</a> in his Google <abbr>IO</abbr> talk from back in 2009.</p>

<p>With all that in mind, let&#8217;s crack on with the canvas <abbr>API</abbr>.</p>

<h2>Hello Canvas</h2>

<p>The <code>&lt;canvas&gt;</code> element by itself is an invisible block of space, by default 300&#215;150 pixels (in all browsers):</p>

<pre><code>&lt;canvas id="c"&gt;&lt;/canvas&gt;
</code></pre>

<p>So now we&#8217;ve got a blank canvas in front of us. To draw, we need the <em>drawing context</em>, which we can get using JavaScript. Once we have the context, we can draw abominations to our hearts&#8217; content:</p>

<pre><code>&lt;script&gt;
// declare the two variables at once using a comma
var canvas = document.getElementById("c"),
    context = canvas.getContext("2d");

// now you're ready to draw
&lt;/script&gt;
</code></pre>

<p><small><a href="http://jsbin.com/abagi3/edit">Code snippet</a></small></p>

<p>The context is our direct access to draw and paint on the canvas. Without it, we can&#8217;t paint a thing.</p>

<h2>Painting Shapes</h2>

<p>The 2D drawing API is fairly large (not too huge, but bigger than most other <abbr>HTML</abbr>-esque <abbr>API</abbr>s), so I&#8217;m just going to show you how to draw something simple: a solid blue rectangle and a pink semi-circle.</p>

<p>Using the <code>context</code> object from earlier, we&#8217;re going to call <code>fillRect()</code>, passing the method the coordinates of the top left corner (x, y) and the width and height of the rectangle we want to paint:</p>

<pre><code>&lt;script&gt;
var canvas = document.getElementById("c"),
    context = canvas.getContext("2d");

// x = 10, y = 20, width = 200, height = 100
context.fillRect(10, 20, 200, 100);
&lt;/script&gt;
</code></pre>

<p><small><a href="http://jsbin.com/abagi3/2/edit">Code snippet</a></small></p>

<p><figure><img style="border: 1px solid #ccc;" src="http://html5doctor.com/wp-content/uploads/2010/07/canvas1.png" alt="Simple filled rectangle" title="Simple filled rectangle" width="300" height="150" class="alignnone size-full wp-image-2269" /><figcaption>Simple filled rectangle created with canvas</figcaption></figure></p>

<p>If you don&#8217;t specify a colour, the default fill and stroke colours will be black. So let&#8217;s change that to blue by setting the <code>fillStyle()</code> <em>before</em> we call <code>fillRect()</code>. We&#8217;re choosing our colour before drawing because this <code>&lt;canvas&gt;</code> is just like a real canvas — if you&#8217;re going to paint, you need to dip your brush in to the paint pot first:</p>

<pre><code>var canvas = document.getElementById("c"),
    context = canvas.getContext("2d");

context.fillStyle = 'blue';
context.fillRect(10, 20, 200, 100);
</code></pre>

<p><small><a href="http://jsbin.com/abagi3/3/edit">Code snippet</a></small></p>

<p><figure><img style="border: 1px solid #ccc;" src="http://html5doctor.com/wp-content/uploads/2010/07/canvas2.png" alt="" title="Blue filled rectangle" width="300" height="150" class="alignnone size-full wp-image-2270" /><figcaption>Blue filled rectangle created with canvas</figcaption></figure></p>

<p>Being the eagle-eyed <abbr>HTML</abbr>5 ninja that you are, you noticed that I&#8217;ve used the string <code>blue</code> as the fill colour. You can use any CSS colour properties in the canvas <abbr>API</abbr>. That means <code>blue</code>, <code>#0000ff</code>, <code>#00f</code>, <code>rgb(0, 0, 255)</code>, and even <code>rgba(0, 0, 255, 0.5)</code> are all valid colours.</p>

<p>How about we turn this thing up to eleven? Let&#8217;s add a pink semi-circle. <a href="http://www.youtube.com/watch?v=cbpccXp6Swo">Oh yeah! Let&#8217;s do this!</a></p>

<pre><code>var canvas = document.getElementById("c"),
    context = canvas.getContext("2d");

context.fillStyle = 'blue';
context.fillRect(10, 20, 200, 100);

// setup the line style
context.strokeStyle = '#fa00ff';
context.lineWidth = 5;
context.lineCap = 'round';

// draw the arc path
// (I'll walk you through these values momentarily - bear with me!)
context.arc(50, 50, 20, 0, Math.PI, false);

// colour the path
context.stroke();
</code></pre>

<p><small><a href="http://jsbin.com/abagi3/4/edit">Code snippet</a></small></p>

<p><figure><img style="border:1px solid #ccc;" src="http://html5doctor.com/wp-content/uploads/2010/07/canvas3.png" alt="" title="Blue filled rectangle with semi-circle" width="300" height="150" class="alignnone size-full wp-image-2271" /><figcaption>Blue filled rectangle with a semi-circle created with canvas</figcaption></figure></p>

<p>There are three things that we&#8217;ve added to our JavaScript:</p>

<ol>
<li>Configuring the line style</li>
<li>Drawing the semi-circle <strong>path</strong></li>
<li>Stroking the path (i.e., painting the line)</li>
</ol>

<p>When drawing paths, until you <code>fill</code> or <code>stroke</code> the path, nothing appears on the canvas. In this case, our path is an arc of a 180 degrees. The <code>arc()</code> method takes the following arguments: x coordinate, y coordinate, radius, start angle, end angle, and whether the arc should be drawn anti-clockwise. All of these arguments are required. (Technically, if you&#8217;re drawing a circle, it doesn&#8217;t matter whether you&#8217;re going clockwise or anti-clockwise, but you still need the argument.)</p>

<p>The tricky parts are the start and end angle. They&#8217;re both in radians. Remember those? I didn&#8217;t, so I&#8217;ll forgive you if you don&#8217;t! Here&#8217;s how to convert from degrees to radians:</p>

<pre><code>var radians = degrees * Math.PI / 180;
</code></pre>

<p>It&#8217;s common to pass 360 degrees to the drawing methods, which is simply <code>Math.PI * 2</code>. Similarly, 180 degrees is <code>Math.PI</code>, which we used to create our semi-circle.</p>

<p>Once you have your path, you need to <code>stroke</code> (or <code>fill</code>) it. This applies the line style and colours to the path and completes our drawing. Do please remember that with this drawing: a) you can&#8217;t recover those lost blue pixels under the pink semi-circle, and b) this example would be much simpler in <abbr>SVG</abbr>!</p>

<p>So remember to choose the <code>&lt;canvas&gt;</code> element based on its strengths as I outlined at the top of this article.</p>

<h2>Exporting &#038; Saving</h2>

<p>One thing that <abbr>SVG</abbr> <em>can&#8217;t</em> do is save the resulting image as a bitmap. It&#8217;s easy for <code>&lt;canvas&gt;</code> because the element is already a bitmap in the first place! The canvas can export its image to a data <abbr>URL</abbr> (e.g., <code>data:image/png;base64,iVBORw0KGg...</code>). This data may then be rendered in the browser, which could then be saved or dragged to the desktop, used in a new canvas, and so on.</p>

<p>The browser must support <abbr>PNG</abbr> images, and it may have varying support for <abbr>GIF</abbr> and <abbr>JPG</abbr>. For our example, we&#8217;ll stick with <abbr>PNG</abbr> since it supports alpha transparency, and where we haven&#8217;t drawn on the canvas, it&#8217;ll be transparent.</p>

<p>To get the data <abbr>URL</abbr>, we simply call <code>canvas.toDataURL('image/png')</code>. Note that we&#8217;re calling <code>toDataURL()</code> on the <code>&lt;canvas&gt;</code> element, <em>not</em> on the 2D context. This is because we&#8217;re getting all the pixels in the canvas, not just the pixels in a particular context.</p>

<p>So taking the example we&#8217;ve put together already, we&#8217;ll make the browser redirect to a <abbr>PNG</abbr> version of the image when a user clicks on the <code>&lt;canvas&gt;</code> element (a contrived example, I know!):</p>

<pre><code>canvas.onclick = function () {
  window.location = canvas.toDataURL('image/png');
};
</code></pre>

<p><small><a href="http://jsbin.com/abagi3/5/edit">Code snippet</a></small></p>

<p>If you go to the <a href="http://jsbin.com/abagi3/5/">live canvas example</a> and click on the <code>&lt;canvas&gt;</code> element, it will load a <abbr>PNG</abbr> version of the canvas. Now you can save that to your desktop, email it to your friends, or even tweet it to your followers!</p>

<h2>That&#8217;s just the start</h2>

<p>This is just a small preview of what the canvas 2D <abbr>API</abbr> can do. There&#8217;s a whole lot more:</p>

<ul>
<li>Gradients, fills, and patterns</li>
<li>Paths</li>
<li>Text</li>
<li>Pixel manipulation</li>
<li>Animations (though old school, flip book style)</li>
</ul>

<p>And don&#8217;t worry, the <abbr>HTML</abbr>5 Doctors will publish more articles explaining the canvas <abbr>API</abbr> in the future. We&#8217;ll even have special guest <a href="http://twitter.com/tabatkins">Tab Atkins</a> explaining how he got a video to render entirely in <abbr>ASCII</abbr>!</p>

<p>In the meantime, here are a few useful resources to keep you going.</p>

<h2>Further reading</h2>

<ul>
<li><a href="https://developer.mozilla.org/en/canvas_tutorial">Mozilla&#8217;s canvas tutorial</a></li>
<li><a href="http://dev.opera.com/articles/view/html-5-canvas-the-basics/">Opera&#8217;s canvas tutorial</a></li>
<li><a href="http://billmill.org/static/canvastutorial/">Breakout game live tutorial</a></li>
<li><a href="http://diveinto.html5doctor.com/canvas.html">Mark Pilgrim&#8217;s Dive in to HTML5 canvas chapter</a> — but don&#8217;t forget there are <a href="http://introducinghtml5.com">other good books</a> you can buy too <img src='http://html5doctor.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> 
<div id="crp_related"><h3>Related Posts:</h3><ul class="related"><li><a href="http://html5doctor.com/video-canvas-magic/" rel="bookmark" class="crp_title">video + canvas = magic</a></li><li><a href="http://html5doctor.com/your-questions-16/" rel="bookmark" class="crp_title">Your Questions #16</a></li><li><a href="http://html5doctor.com/your-questions-13/" rel="bookmark" class="crp_title">Your Questions #13</a></li><li><a href="http://html5doctor.com/your-questions-answered-11/" rel="bookmark" class="crp_title">Your Questions Answered #11</a></li><li><a href="http://html5doctor.com/review-html5-now-dvd/" rel="bookmark" class="crp_title">Review: HTML5 Now (DVD)</a></li></ul></div></li>
</ul>
<p><a href="http://html5doctor.com/an-introduction-to-the-canvas-2d-api/" rel="bookmark">An introduction to the Canvas 2D API</a> originally appeared on <a href="http://html5doctor.com">HTML5 Doctor</a> on August 3, 2010.</p>
]]></content:encoded>
			<wfw:commentRss>http://html5doctor.com/an-introduction-to-the-canvas-2d-api/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Your Questions Answered #11</title>
		<link>http://html5doctor.com/your-questions-answered-11/</link>
		<comments>http://html5doctor.com/your-questions-answered-11/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 14:00:13 +0000</pubDate>
		<dc:creator>Richard Clark</dc:creator>
				<category><![CDATA[Questions]]></category>
		<category><![CDATA[boilerplate]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[header]]></category>
		<category><![CDATA[hgroup]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[meter]]></category>
		<category><![CDATA[placeholder]]></category>
		<category><![CDATA[progress]]></category>
		<category><![CDATA[web forms]]></category>

		<guid isPermaLink="false">http://html5doctor.com/?p=2328</guid>
		<description><![CDATA[<p>The clinic is busy as ever with more <abbr>HTML</abbr>5 ailments. This week, we'll show you how (and whether) to store a <code>&#60;canvas&#62;</code> on the server, whether to use <code>&#60;progress&#62;</code> or <code>&#60;meter&#62;</code>, more on <code>&#60;header&#62;</code>, the <code>placeholder</code> attribute, and <abbr>HTML</abbr>5 minification.</p>]]></description>
			<content:encoded><![CDATA[<p><img src="http://html5doctor.com/wp-content/uploads/2009/07/html5doctor-treatment.gif" alt="Doctor treating a patient illustration" class="alignright size-full wp-image-424" /> The clinic is busy as ever with more <abbr>HTML</abbr>5 ailments. This week, we&#8217;ll show you how (and whether) to store a <code>&lt;canvas&gt;</code> on the server, whether to use <code>&lt;progress&gt;</code> or <code>&lt;meter&gt;</code>, more on <code>&lt;header&gt;</code>, the <code>placeholder</code> attribute, and <abbr>HTML</abbr>5 minification.</p>
    
    <section>
      <h2>Getting info from a canvas to the server</h2>
      <p>Stephen asked:</p>
      <blockquote>
        <p>I want to use a canvas element as a layout plan and allow people to move things about and the like but I then want to be able to submit the changed layout to the server so it can be kept not just as a bitmap but as the various current layout of the plan.  Is this possible?</p>
      </blockquote>
      <p>You could call <code>canvas.toDataURL()</code> and store this output on the server. (Note that <code>canvas</code> is the <abbr>DOM</abbr> node and not the 2D context.) This will store the current state of your canvas, but it&#8217;s effectively a bitmap at this point. If you want to store the element for later editing, I&#8217;d suggest you use <abbr>SVG</abbr> instead. You can achieve the same graphical effect, but you&#8217;ll have access to the actual vector data since it&#8217;s <abbr>XML</abbr>.</p>

	  <p>Hope that helps,</p>
	  
	  <p>Remy</p>
    </section>
    
    <section>
      <h2>Progress or meter?</h2>
      <p>Pierre asked:</p>
      <blockquote>
        <p>Hello doctor,</p>

<p>I&#8217;m beginning with <abbr>HTML</abbr>5. I just wonder if I&#8217;m using the progress and meter elements correctly. Just imagine a player. Is it correct to use: the progress element to specify a load bar; the meter element to specify the volume set.</p>

<p>Thank you Doctor, have a good day. <img src='http://html5doctor.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
      </blockquote>
      <p><code>&lt;progress&gt;</code> is spot on for a loading bar, yes.</p>

	  <p><code>&lt;meter&gt;</code> is read only, however, so it&#8217;s not appropriate for a volume control. There are some examples in <a href="http://html5doctor.com/measure-up-with-the-meter-tag/">Tom&#8217;s article on <code>&lt;meter&gt;</code></a>. To create a volume control, you could use one of the new input types specified in <abbr>HTML</abbr>5, perhaps <code>&lt;input type="range"&gt;</code>.</p>

	  <p>Cheers,</p>
	  
	  <p>Rich</p>
    </section>
    
    <section>
      <h2>Headers</h2>
      <p>Brad asked:</p>
      <blockquote>
        <p>Having read you can use the header tag within almost any other element, is it fair to say you no longer &#8220;design&#8221; the header but simply design the elements in each header?  How does that work?  Would you use header classes?</p>
		
        <p>In the past you define your header with an image or text etc.  You can no longer do that if you use multiple headers?</p>
      </blockquote>
      <p>The <code>&lt;header&gt;</code> element should be used if you have any related information within the &#8220;header&#8221; of that section that needs to be grouped. This means, for example, it isn&#8217;t necessary to wrap a lone <code>&lt;h1&gt;</code> in a <code>&lt;header&gt;</code> element. Also remember that the header doesn&#8217;t <em>have</em> to appear at the top of a section of page.</p>

    <p>Our articles on the <a href="http://html5doctor.com/the-header-element/"><code>&lt;header&gt;</code> element</a> and <a href="http://html5doctor.com/the-hgroup-element/"><code>&lt;hgroup&gt;</code> element</a> explain this some more.</p>

    <p>Whether you need to use classes depends on your site.</p>

    <p>Ta,</p>
    
    <p>Rich</p>
    </section>
    
    <section>
      <h2>The <code>placeholder</code> attribute</h2>
      <p>José asked:</p>
      <blockquote>
        <p>I recall once having seen text inputs that had tips inside them. They didn&#8217;t need Javascript in order to manage those tips. Would you consider discussing that a little better? That&#8217;d be great! Thanks!</p>
      </blockquote>
      <p>We&#8217;re publishing an article on forms shortly, so be sure to look out for it.</p>

	  <p>Meanwhile, you&#8217;re looking for the <code>placeholder</code> attribute. Currently, this is supported only on Webkit browsers, although Mozilla is also working on it. You can use <code>placeholder</code> like this:</p>

      <pre><code>&lt;input placeholder="Search" title="Type your search here" ...&gt;</code></pre>
      
	  <p>We&#8217;ve also written a JavaScript solution using feature detection (i.e., it will check whether the browser supports <code>placeholder</code> and adds support if not). Just add <a href="http://gist.github.com/330318">this script</a> to the end of your markup (note that it should be the end since it needs to run once the <abbr>DOM</abbr> is ready).</p>

	  <p>Hope that helps,</p>
	  
	  <p>Bruce &amp; Remy</p>

    </section>
    
    <section id="html5-minified">
      <h2>HTML5 Minified</h2>
      <p>Grant asked:</p>
      <blockquote>
        <p>Hello! Big fan of your website. I wanted to know about the so called &#8220;HTML Minified&#8221; feature that allows you to strip the <code>html</code>, <code>head</code> and <code>body</code> tags from your page.</p>

		<p>The W3C Validator Conformance Checker for HTML5 says this is valid, but the Conformance Checker is beta and is supposedly unreliable. I haven&#8217;t heard from anywhere else that stripping these tags is a feature.</p>

		<p>So is it really a feature? Or just a bug (A BIG one at that) in the experimental validator? I know that it works, but I would like to see any confirmation other than the validator that this is in fact an intended standard before I start using it on my websites. Thanks!</p>
      </blockquote>
      <p>According to the spec, it is now explicitly permitted to omit all kinds of <del datetime="2010-07-31T13:33+09:00">elements</del> <ins datetime="2010-07-31T13:33+09:00">tags</ins>, including the ones you mention. Browsers are able to deduce the document structure based on context (e.g., some elements are only allowed within a <code>&lt;body&gt;</code> element) and will fix the <abbr>DOM</abbr> as they go. In fact, these have always been optional tags in <abbr>HTML</abbr> (but not <abbr>XHTML</abbr>).</p>

	  <p>Browsers are so good at this, though, because they’ve had to deal with the pants code out there on the interwebs. Going this route means you’re relying on browser error handling to render your document <del datetime="2010-07-31T13:34+09:00">(although said error handling has admittedly been standardised in <abbr>HTML</abbr>5)</del>. I personally see it as a more extreme version of quoting element attributes. Of course you don’t have to, but being strict makes errors much easier to find, helps out parsers that may not be as sophisticated as a full-fledged browser (like text editor syntax highlighting), and makes it easier for beginners and veterans alike to learn and use <abbr>HTML</abbr>.</p>

	  <p>In general, machines understand you better when you’re explicit, so I’d advise against omitting these elements.</p>
      
      <p>For more detailed articles on <abbr>HTML</abbr>5 minification, check out Remy&#8217;s <a href="http://html5doctor.com/html-5-boilerplates/">HTML5 Boilerplates</a> and Bruce&#8217;s <a href="http://www.brucelawson.co.uk/2010/a-minimal-html5-document/">A minimal HTML5 document</a>.</p>

	  <p>Peace,</p>
	  
	  <p>Oli</p>
    </section>
    
	<section>
		<h2>Got a question for us?</h2>
		<p>That wraps up this round of questions. If you&#8217;ve got a query about the <abbr>HTML</abbr>5 spec or how to implement it, you can <a href="http://html5doctor.com/ask-the-doctor/">get in touch</a> with us and we&#8217;ll do our best to help.</p>
	</section>
<div id="crp_related"><h3>Related Posts:</h3><ul class="related"><li><a href="http://html5doctor.com/your-questions-answered-9/" rel="bookmark" class="crp_title">Your Questions Answered 9</a></li><li><a href="http://html5doctor.com/your-questions-answered-7/" rel="bookmark" class="crp_title">Your Questions Answered #7</a></li><li><a href="http://html5doctor.com/your-questions-13/" rel="bookmark" class="crp_title">Your Questions #13</a></li><li><a href="http://html5doctor.com/your-questions-5/" rel="bookmark" class="crp_title">Your Questions Answered #5</a></li><li><a href="http://html5doctor.com/your-questions-answered-8/" rel="bookmark" class="crp_title">Your Questions Answered #8</a></li></ul></div><p><a href="http://html5doctor.com/your-questions-answered-11/" rel="bookmark">Your Questions Answered #11</a> originally appeared on <a href="http://html5doctor.com">HTML5 Doctor</a> on July 30, 2010.</p>
]]></content:encoded>
			<wfw:commentRss>http://html5doctor.com/your-questions-answered-11/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Your Questions Answered #10</title>
		<link>http://html5doctor.com/your-questions-answered-10/</link>
		<comments>http://html5doctor.com/your-questions-answered-10/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 12:45:00 +0000</pubDate>
		<dc:creator>Richard Clark</dc:creator>
				<category><![CDATA[Questions]]></category>
		<category><![CDATA[block links]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[footer]]></category>
		<category><![CDATA[headings]]></category>
		<category><![CDATA[HTML 5]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[inline]]></category>
		<category><![CDATA[outline]]></category>
		<category><![CDATA[section]]></category>

		<guid isPermaLink="false">http://html5doctor.com/?p=1991</guid>
		<description><![CDATA[<p>The clinic is getting busy with more  <abbr>HTML</abbr>5 ailments. This week, we'll cover using sections within a footer, <code>&#60;canvas&#62;</code> vs. Flash security, why <abbr>HTML</abbr>5 elements are treated as inline, using offline with storage, and marking up block quotes.</p>]]></description>
			<content:encoded><![CDATA[<p><img src="http://html5doctor.com/wp-content/uploads/2009/07/html5doctor-treatment.gif" alt="Doctor treating a patient illustration" class="alignright size-full wp-image-424" /> The clinic is getting busy with more  <abbr>HTML</abbr>5 ailments. This week, we&#8217;ll cover using sections within a footer, <code>&lt;canvas&gt;</code> vs. Flash security, why <abbr>HTML</abbr>5 elements are treated as inline, using offline with storage, and marking up block quotes.</p>

<section>
<h2>Footer with sections</h2>

<p>Tommie asked:</p>
<blockquote><p>Hi, I&#8217;ll get to the point right away. I want to use, lets say 3 section elements inside a footer each floating left and inside each of this sections I have some standard information. Is this the right way to go, or should I put my section elements outside the footer and only put copyright text etc. inside the footer?</p>

<p>Thanks!</p></blockquote>

<p>You could use a <code>&lt;section&gt;</code> within <code>&lt;footer&gt;</code>. <del>but <code>&lt;footer&gt;</code> is a sectioning root anyway</del> <ins><code>&lt;footer&gt;</code> is flow content and can contain sectioning content.</ins> If you want them to appear in the outline within the footer, then it makes perfect sense to do as you suggest.</p>

<p>See more in Jack&#8217;s article on <a href="http://html5doctor.com/the-footer-element/">the <code>&lt;footer&gt;</code> element.</a> There are also some more examples in <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/sections.html#the-footer-element">the spec</a>.</p>

<p>Cheers, Rich</p>
</section>

<section>
<h2><code>&lt;canvas&gt;</code> vs. Flash (security and copyright)</h2>
<p>John asked:</p>
<blockquote><p>I&#8217;m seeing lots of opinions that the <code>canvas</code> element might free us of the Adobe Flash monopoly, but what mechanisms can we use to protect our works when using straight HTML and JavaScript?</p>

<p>Right now I&#8217;m thinking about simple flash games, but I&#8217;m sure this is a concern for others wanting to use (free) <abbr>HTML</abbr> methods to perform functions now reserved more for flash.</p>

<p>Thanks for any input.</p></blockquote>

<p>I&#8217;m not sure of any security mechanisms that we can use to protect <code>&lt;canvas&gt;</code> content. It isn&#8217;t really any different than an image or any other type of resource. Having said that, decompilers can circumvent some of Flash&#8217;s security measures as well, so you may not be much worse off using <code>&lt;canvas&gt;</code>.</p>

<p>If we come across anything, we&#8217;ll be sure to let you know.</p>

<p>Cheers, Rich</p>
</section>

<section>
<h2><abbr>HTML</abbr>5 tags inline?</h2>
<p>Tami asked:</p>
<blockquote><p>So, I&#8217;ve been experimenting with the new <abbr>HTML</abbr>5 elements, and one frustration I have is understanding the native display of the new elements, and how it works within the document flow. So far, experimenting with them has shown them to be very unpredictable. At best, they&#8217;re kind of behaving like a bastard span. They seem to be behaving like inline elements but that seems like it is wrong. In some cases, setting to display: block gives the expected behavior, but on the footer tag, it&#8217;s not interacting with floated elements properly.</p>

<p>So, are these new elements inline or are browsers still just trying to catch up? I tried looking in in the <abbr>HTML</abbr>5 working draft spec, but so far I am not understanding the way its written (<abbr>HTML</abbr>4&#8242;s specs clearly spelled out whether an element was display or inline).</p></blockquote>

<p>Here&#8217;s an article from my personal blog that I wrote a while ago describing <a href="http://www.brucelawson.co.uk/2009/why-browsers-treat-html5-elements-as-inline/">why browsers treat <abbr>HTML</abbr>5 elements as inline</a>.</p>

<p>Thanks, Bruce.</p>
</section>

<section>
<h2>Offline + Storage</h2>
<blockquote><p>Dear Doctor, I&#8217;m trying to work out if <abbr>HTML</abbr>5 will be able to do the job that I want. I have a number of documents (<abbr>PDF</abbr>, <abbr>PPT</abbr>, etc.) that I want to share with people in my team. These documents may change from time to time. Also, the people may not always be connected online.</p>

<p>Would it be possible to &#8220;cache&#8221; documents in an offline database in the browser so that when they are not connected they can still view them but when they are, they get the latest versions (which also refresh the cache)?</p>

<p>That way I can do a fairly pretty interface for sorting these documents so that they can easily find them.</p>

<p>Thanks in advance for any help you can provide.</p></blockquote>

<p>You <em>can</em>, but I would strongly encourage you to think carefully about whether it&#8217;s worth it.</p>

<p>You can include these &#8220;assets&#8221; in the manifest, which will make the files available to the offline applications (note that this <strong>isn&#8217;t</strong> storage, just caching, which is what you&#8217;re after).</p>

<p>The problem is that when the visitor first hits the page with the manifest — which contains all the <abbr>PDF</abbr>s, <abbr>PPT</abbr>s, etc. — it will download <strong>all</strong> of these files. This is the bit that I&#8217;d encourage you to consider carefully. To compound that, I believe there&#8217;s a 5MB limit on the cache, so you might run into problems if you&#8217;re trying to cache more than that.</p>

<p>I&#8217;ve not covered offline cache on <abbr>HTML</abbr>5 Doctor yet (though it&#8217;s on the list), but I have described the process on my <a href="http://24ways.org/2009/breaking-out-the-edges-of-the-browser">24ways article</a>.</p> 

<p>If there are new versions of the cached files, you need to change the <em>contents</em> of the manifest — e.g., include a version number, which will trigger a download of <strong>all</strong> the assets again.  Not ideal, but that&#8217;s the current situation.</p>

<p>Hope that helps a bit, Remy</p>
</section>

<section>
<h2>Correct markup with blockquote</h2>
<p>Phil asked:</p>
<blockquote><p>Hi, in <abbr>HTML</abbr> 4.0 Strict and <abbr>XHTML</abbr> 1.0 Strict, text inside a <code>blockquote</code> element is required to be nested inside another block-level element, e.g. <code>p</code>.</p>

<p>In <abbr>HTML</abbr>5 that requirement seems to have been relaxed, as the following element validates successfully:</p>

<pre><code>&lt;blockquote&gt;This is a blockquote.&lt;/blockquote&gt;</code></pre>

<p>The HTML5 spec uses the <code>p</code> element in the usage examples, but does not mention whether it is required.</p>

<p>Can you please confirm if this requirement has now been deprecated?</p></blockquote>

<p>Great question. As I expected, this validates:</p>

<pre><code>&lt;!doctype html&gt;
&lt;meta charset=utf-8&gt;
&lt;title&gt;blockquote test&lt;/title&gt;
&lt;blockquote&gt;Tiger tiger burning bright&lt;/blockquote&gt;</code></pre>

<p>We double-checked with the <abbr>WHATWG</abbr> mailing list and confirmed that <code>&lt;blockquote&gt;</code> can contain &#8216;flow&#8217; content (i.e., text, paragraphs, etc.).</p>

<p>To summarise, it would be valid not to further mark up content within a <code>&lt;blockquote&gt;</code>, but authors are encouraged to use a <code>&lt;p&gt;</code> (or other more appropriate element).</p>

<p>Bruce</p>
</section>

<section>
<h2>Got a question for us?</h2>
<p>That wraps up this round of questions! If you&#8217;ve got a query about the <abbr>HTML</abbr>5 spec or how to implement it, you can <a href="http://html5doctor.com/ask-the-doctor/">get in touch</a> with us and we&#8217;ll do our best to help.</p>
</section><div id="crp_related"><h3>Related Posts:</h3><ul class="related"><li><a href="http://html5doctor.com/go-offline-with-application-cache/" rel="bookmark" class="crp_title">Go offline with application cache</a></li><li><a href="http://html5doctor.com/your-questions-answered-9/" rel="bookmark" class="crp_title">Your Questions Answered 9</a></li><li><a href="http://html5doctor.com/your-questions-16/" rel="bookmark" class="crp_title">Your Questions #16</a></li><li><a href="http://html5doctor.com/your-questions-12/" rel="bookmark" class="crp_title">Your Questions Answered #12</a></li><li><a href="http://html5doctor.com/your-questions-answered-7/" rel="bookmark" class="crp_title">Your Questions Answered #7</a></li></ul></div><p><a href="http://html5doctor.com/your-questions-answered-10/" rel="bookmark">Your Questions Answered #10</a> originally appeared on <a href="http://html5doctor.com">HTML5 Doctor</a> on July 9, 2010.</p>
]]></content:encoded>
			<wfw:commentRss>http://html5doctor.com/your-questions-answered-10/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Why designers should care about HTML5</title>
		<link>http://html5doctor.com/why-designers-should-care-about-html5/</link>
		<comments>http://html5doctor.com/why-designers-should-care-about-html5/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 13:01:06 +0000</pubDate>
		<dc:creator>Cennydd Bowles</dc:creator>
				<category><![CDATA[Comment]]></category>
		<category><![CDATA[JavaScript APIs]]></category>
		<category><![CDATA[audio]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[drag and drop]]></category>
		<category><![CDATA[HTML 5]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[semantics]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://html5doctor.com/?p=973</guid>
		<description><![CDATA[After a while on the fringes of our collective consciousness, HTML5 is finally getting the attention it deserves. The development community (as typified by the SuperFriends) has come together to debate practical elements of the spec, argue over the inclusion of controversial elements, and assess the timeframe over which we can unleash HTML5 in the wild.]]></description>
			<content:encoded><![CDATA[<p>After a while on the fringes of our collective consciousness, <abbr title="Hypertext Markup Language 5">HTML5</abbr> is finally getting the attention it deserves. The development community (as typified by the <a href="http://www.zeldman.com/superfriends/">SuperFriends</a>) has come together to debate practical elements of the spec, argue over the inclusion of controversial elements, and assess the timeframe over which we can unleash <abbr>HTML</abbr>5 in the wild.</p>
<p>However those of us more accustomed to the world of Post-Its, sketches, and .psds – the designers – haven’t been so vocal. Perhaps we&#8217;ve been distracted by the bright lights of <a href="http://www.css3.info/">CSS3</a> and those surface thrills we’ve longed for. (Rounded corners! Gradients! Transparency!) Or, alternatively, we&#8217;ve been in the thrall of <code>@font-face</code> and looking forward to the coming age of passable web typography.</p>
<p>Understandable. But it’s time designers got excited about <abbr>HTML</abbr>5 too.</p>
<p>Partly, it’s just good practice. Whatever your flavour of design &ndash; visual, web, interaction, user experience &ndash; knowing the native technology makes you better at your job. Just as composers should understand the capabilities of the orchestra&#8217;s instruments, designers need to understand the language of the web.</p>
<p>But there’s more to <abbr>HTML</abbr>5 than simply keeping our skills sharp. It could make a big difference to the way we design for the web.</p>
<h2>Semantic elements</h2>
<p>Information architects (and, by extension, user experience designers) should be excited by the new <abbr>HTML</abbr>5 elements – <code>&lt;nav&gt;</code>, <code>&lt;header&gt;</code>, <code>&lt;aside&gt;</code> and so on. While they won’t immediately revolutionise today’s web, they’re an investment for the future. Doing useful stuff with information is the central theme of <abbr title="information architecture">IA</abbr>, and therefore its practitioners should be at the forefront of the new experiences that machine-readable semantics will offer. <abbr>HTML</abbr>5 allows us to mark text up in a more meaningful way than a sea of <code>&lt;div&gt;</code>s, meaning we’ll soon see applications appearing at a sub-page level. We’ve started to scratch the surface – think about the <a href="https://addons.mozilla.org/en-US/firefox/addon/4106">Operator toolbar</a> or customisable UIs à la <a href="http://www.google.com/ig">iGoogle</a> – but we’ll need detailed design thinking to work out how to bring the benefits of semantic richness to the end user.</p>
<h2>APIs and other extensions</h2>
<p>While it’s clear that <a href="http://www.quirksmode.org/blog/archives/2009/09/the_html5_drag.html">some of the <abbr>HTML</abbr>5 APIs are far from perfect</a> right now, when they&#8217;re refined they will offer us intriguing new opportunities and challenges.</p>
<p>Designers of location-based services should of course find the <a href="http://dev.w3.org/geo/api/spec-source.html">geolocation API</a> invaluable. The <a href="http://blog.whatwg.org/the-road-to-html-5-contenteditable">contentEditable attribute</a> gives us further power to make the web truly read/write without resorting to JavaScript and custom interfaces. New input types (eg <code>type=&quot;search&quot;</code>) can provide extra visual cues about input function, although of course this depends on the solutions chosen by the browser manufacturers.</p>
<p>Until now, it’s been easy to consider our domain as bounded by the viewport and the web server. But <abbr>HTML</abbr>5 is another step toward seamlessness: the merging of desktop, offline and online. For instance, the <a href="/native-drag-and-drop/">drag and drop API</a> could see the line between online and desktop experience blur further. Local storage could allow for a web-like experience in areas of poor connectivity. This convergence is clearly a good thing, but we must also design how to expose those hidden seams at the user’s request. Users should stay in control of how their locations are published and what data is synchronised to their machine.</p>
<h2>&lt;video&gt;, &lt;audio&gt;, &lt;canvas&gt;</h2>
<p>There is of course something of a reported schism between the standards world and the Flash world. Some see the advent of these new media elements (particularly <code>&lt;canvas&gt;</code>) as heralding the death of Adobe’s poster child.</p>
<p>I don&#8217;t think this is either likely or desirable. Neither technology is perfect. Flash is, of course, proprietary and thus subject to the whims of a third party that stands between browser and user. <code>&lt;canvas&gt;</code> has <a href="http://esw.w3.org/topic/HTML/AddedElementCanvas">known accessibility problems</a>. But the two can live in harmony, if we play to their respective strengths. Some current Flash applications might be better suited to <code>&lt;canvas&gt;</code>, particularly those based around dynamic visualisation: graphs, animations, infographics. Some applications will benefit from the powerful capabilities of Flash: games, heavily interactive widgets.</p>
<p>This aside, there’s clearly a user experience benefit in not having to rely on an external plugin to play rich media elements, and it will be interesting to see the uptake of the <code>&lt;video&gt;</code> and <code>&lt;audio&gt;</code> elements. Although it will initially be down to browser makers to define the interface elements involved, we will need to figure out how to integrate them into everyday web experiences. The good news is that they can be styled in the same way as any other <abbr>HTML</abbr> element. If your visual aesthetic relies on slanted images with box shadows, it&#8217;s trivial to apply this to video too.</p>
<p>That said, we can&#8217;t ignore the elephant in the room: <a href="http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2009-June/020620.html">the thorny codec issue</a>. I&#8217;m sure we&#8217;d all agree that the sooner it&#8217;s resolved, the better.</p>
<h2>What can designers do?</h2>
<p>Only the most patient and detail-oriented designer will relish the idea of reading the spec in full and arguing the finer points on the <a href="http://www.whatwg.org/mailing-list">WHATWG list</a>. That’s just not the way designers roll.</p>
<p>But as a community it’s important that we start talking about <abbr>HTML</abbr>5. If you&#8217;re new to <abbr>HTML</abbr>, now&#8217;s a great time to learn. The <a href="/article-archive/">articles on this site</a> and <a href="http://www.alistapart.com/articles/previewofhtml5/">A preview of HTML5</a> give useful guidance on the differences between <abbr>HTML</abbr>5 and its predecessors. Above all, designers should get chatting with their developer friends: there can’t be many left who no longer have an opinion on this technology. How do they see their practices changing? What can we do today to prepare our sites for the advent of HTML5? How can we build on its strong points to make the web a better place?</p>
<p>We don&#8217;t yet know what we&#8217;ll accomplish with <abbr>HTML</abbr>5, but then it&#8217;s not often that the vocabulary of the web changes this deeply. However, one thing is clear: if we prepare now, we have a great chance to bring innovation to our users&#8217; online lives.</p>
<p class="disclaimer">This article was jointly published on <a href="http://www.cennydd.co.uk/">Ineffable</a>, Cennydd&#8217;s personal website.</p>
<div id="crp_related">
<h3>Related Posts:</h3>
<ul class="related">
<li><a href="http://html5doctor.com/reviewing-html5-for-web-designers/" rel="bookmark" class="crp_title">Reviewing HTML5 for Web Designers</a></li>
<li><a href="http://html5doctor.com/html5-briefing-notes-journalists-analysts/" rel="bookmark" class="crp_title">HTML5: briefing notes for journalists and analysts</a></li>
<li><a href="http://html5doctor.com/native-audio-in-the-browser/" rel="bookmark" class="crp_title">Native Audio in the browser</a></li>
<li><a href="http://html5doctor.com/web-directions-atmedia-2010/" rel="bookmark" class="crp_title">HTML5 Doctor at Web Directions @media</a></li>
<li><a href="http://html5doctor.com/draw-attention-with-mark/" rel="bookmark" class="crp_title">Draw attention with mark</a></li>
</ul>
</div>
<p><a href="http://html5doctor.com/why-designers-should-care-about-html5/" rel="bookmark">Why designers should care about HTML5</a> originally appeared on <a href="http://html5doctor.com">HTML5 Doctor</a> on October 14, 2009.</p>
]]></content:encoded>
			<wfw:commentRss>http://html5doctor.com/why-designers-should-care-about-html5/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

