<?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>simonsarris.com</title>
	<atom:link href="http://simonsarris.com/feed" rel="self" type="application/rss+xml" />
	<link>http://simonsarris.com</link>
	<description></description>
	<lastBuildDate>Sun, 29 Jan 2012 00:22:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A Gentle Introduction to Making HTML5 Canvas Interactive</title>
		<link>http://simonsarris.com/blog/510-making-html5-canvas-useful</link>
		<comments>http://simonsarris.com/blog/510-making-html5-canvas-useful#comments</comments>
		<pubDate>Tue, 20 Dec 2011 06:10:21 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://simonsarris.com/?p=510</guid>
		<description><![CDATA[This is a big overhaul of one of my tutorials on making and moving shapes on an HTML5 Canvas. This new tutorial is vastly cleaner than my old one, but if you still want to see that one or are looking for the concept of a &#8220;ghost context&#8221; you can find that one here. This [...]]]></description>
			<content:encoded><![CDATA[<p><em>This is a big overhaul of one of my tutorials on making and moving shapes on an HTML5 Canvas. This new tutorial is vastly cleaner than my old one, but if you still want to see that one or are looking for the concept of a &#8220;ghost context&#8221; you can find that one <a href="http://simonsarris.com/blog/140-canvas-moving-selectable-shapes">here.</a></em></p>
<p>This tutorial will show you how to create a simple data structure for shapes on an HTML5 canvas and how to have them be selectable. The finished canvas will look like this:</p>
<div id="container">
<canvas id="canvas1" width="300" height="300" style="border: 1px solid black;"><br />
  This text is displayed if your browser does not support HTML5 Canvas.<br />
</canvas>
</div>
<p><i>Click to drag the shapes. Double click to add a new shape.</i></p>
<pre><script type="text/javascript" src="http://simonsarris.com/project/canvasdemo/shapes.js"></script>
<script>init();</script></pre>
<p>We&#8217;ll be going over a few things that are essential to interactive apps such as games (drawing loop, hit testing), and in later tutorials I will probably turn this example into a small game of some kind. The code also contains simple examples of using JavaScript prototypes and closures. I will try to accommodate JavaScript beginners but this introduction does expect at least a rudimentary understanding of JS. Not every piece of code is explained in the text, but almost every piece of code is thoroughly commented!</p>
<h3>The HTML5 Canvas</h3>
<p>A Canvas is made by using the &lt;canvas&gt; tag in HTML:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">   <span style="color: #009900;">&lt;canvas <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;canvas&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;400&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;300&quot;</span>&gt;</span>
    This text is displayed if your browser does not support HTML5 Canvas.
   <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>canvas&gt;</span></pre></div></div>

<p>A canvas isn&#8217;t smart: it&#8217;s just a place for drawing pixels. If you ask it to draw something it will execute the drawing command and then immediately forget everything about what it has just drawn. This is sometimes referred to as an immediate drawing surface, as contrasted with SVG as a retained drawing surface, since SVG keeps a reference to everything drawn. Because we have no such references, we have to keep track ourselves of all the things we want to draw (and re-draw) each frame.</p>
<p>Canvas also has no built-in way of dealing with animation. If you want to make something that you&#8217;ve drawn move, you have to clear the entire canvas and redraw all of the objects with one or more of them in a new location. And you have to do it often, of course, if you want a semblance of animation or motion.</p>
<p>So we&#8217;ll need to add:</p>
<ol>
<li>Code for keeping track of objects</li>
<li>Code for keeping track of canvas state</li>
<li>Code for mouse events</li>
<li>Code for drawing the objects as they are made and move around</li>
</ol>
<h3>The things we draw</h3>
<p>To keep things simple for this example we will start with a Shape class to represent rectangular objects.</p>
<p>JavaScript doesn&#8217;t technically have classes, but that isn&#8217;t a problem because JavaScript programmers are very good at playing pretend. Functionally (well, for our example) we are going to have a Shape class and create Shape instances with it. What we are really doing is defining a function named Shape and adding functions to Shape&#8217;s prototype. You can make new instances of the function Shape and all instances will share the functions defined on Shape&#8217;s prototype.</p>
<p>If you&#8217;ve never encountered prototypes in JavaScript before or if the above sounds confusing to you, I highly recommend reading Crockford&#8217;s <a href="http://www.amazon.com/gp/product/0596517742/ref=as_li_ss_tl?ie=UTF8&#038;tag=simonsarris-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0596517742">JavaScript: The Good Parts</a><img src="http://www.assoc-amazon.com/e/ir?t=simonsarris-20&#038;l=as2&#038;o=1&#038;a=0596517742" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />. The book is an intermediate overview of JavaScript that gives a good understanding of why programmers choose to create objects in different ways, why certain conventions are frowned upon, and just what makes JavaScript so different.</p>
<p>Here&#8217;s our Shape constructor and one of the two prototype methods, which are comparable to a class instance methods:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Constructor for Shape objects to hold data for all drawn objects.</span>
<span style="color: #006600; font-style: italic;">// For now they will just be defined as rectangles.</span>
<span style="color: #003366; font-weight: bold;">function</span> Shape<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> w<span style="color: #339933;">,</span> h<span style="color: #339933;">,</span> fill<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// This is a very simple and unsafe constructor. </span>
  <span style="color: #006600; font-style: italic;">// All we're doing is checking if the values exist.</span>
  <span style="color: #006600; font-style: italic;">// &quot;x || 0&quot; just means &quot;if there is a value for x, use that. Otherwise use 0.&quot;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> x <span style="color: #339933;">||</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> y <span style="color: #339933;">||</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">w</span> <span style="color: #339933;">=</span> w <span style="color: #339933;">||</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">h</span> <span style="color: #339933;">=</span> h <span style="color: #339933;">||</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fill</span> <span style="color: #339933;">=</span> fill <span style="color: #339933;">||</span> <span style="color: #3366CC;">'#AAAAAA'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Draws this shape to a given context</span>
Shape.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">draw</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ctx<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  ctx.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">fill</span><span style="color: #339933;">;</span>
  ctx.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">w</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>They are pretty self-explanatory. The shape constructor has &#8220;defaults&#8221; if you give it no arguments, and calling draw on a shape will set the fill and draw a rectangle on the given context corresponding to the measurements of the Shape.</p>
<h3>Keeping track of canvas state</h3>
<p>We&#8217;re going to have a second class (function) called CanvasState. We&#8217;re only going to make one instance of this function and it will hold all of the state in this tutorial that is not associated with Shapes themselves.</p>
<p>CanvasState is going to foremost need a reference to the Canvas and a few other field for convenience. We&#8217;re also going to compute and save the border and padding (if there is any) so that we can get accurate mouse coordinates.</p>
<p>In the CanvasState constructor we will also have a collection of state relating to the objects on the canvas and the current status of dragging. We&#8217;ll make an array of shapes, a flag &#8220;dragging&#8221; that will be true while we are dragging, a field to keep track of which object is selected and a &#8220;valid&#8221; flag that will be set to false will cause the Canvas to clear everything and redraw.</p>
<p>is going to need an array of Shapes to keep track of whats been drawn so far.</p>
<p>I&#8217;m going to add a bunch of variables for keeping track of the drawing and mouse state. I already added boxes[] to keep track of each object, but we&#8217;ll also need a var for the canvas, the canvas&#8217; 2d context (where wall drawing is done), whether the mouse is dragging, width/height of the canvas, and so on. We&#8217;ll also want to make a <i>second</i> canvas, for selection purposes, but I&#8217;ll talk about that later.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> CanvasState<span style="color: #009900;">&#40;</span>canvas<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// ...</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// I removed some setup code to save space</span>
  <span style="color: #006600; font-style: italic;">// See the full source at the end</span>
&nbsp;
&nbsp;
  <span style="color: #006600; font-style: italic;">// **** Keep track of state! ****</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">valid</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// when set to true, the canvas will redraw everything</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">shapes</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// the collection of things to be drawn</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dragging</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Keep track of when we are dragging</span>
  <span style="color: #006600; font-style: italic;">// the current selected object.</span>
  <span style="color: #006600; font-style: italic;">// In the future we could turn this into an array for multiple selection</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">selection</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dragoffx</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// See mousedown and mousemove events for explanation</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dragoffy</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Mouse events</h3>
<p>We&#8217;ll add events for <strong>mousedown</strong>, <strong>mouseup</strong>, and <strong>mousemove </strong>that will control when an object starts and stops dragging. We&#8217;ll also disable the <strong>selectstart </strong>event, which stops double-clicking on canvas from accidentally selecting text on the page. Finally we&#8217;ll add a double-click (<strong>dblclick</strong>) event that will create a new Shape and add it to the CanvasState&#8217;s list of shapes.</p>
<p>The <strong>mousedown </strong>event begins by calling getMouse on our CanvasState to return the x and y position of the mouse. We then iterate through the list of Shapes to see if any of them contain the mouse position. We go through them backwards because they are drawn forwards, and we want to select the one that appears topmost, so we must find the potential shape that was drawn last.</p>
<p>If we find a shape then we want to select it. We save the offset, save a reference to that shape as the CanvasState&#8217;s this.selection, set this.dragging to true and set the this.valid flag to false. Already we&#8217;ve used most of our state! Finally if we didn&#8217;t find any objects we need to see if there was a selection saved from last time. Since we clicked on nothing, we obviously didn&#8217;t click on the already-selected object, so we want to &#8220;deselect&#8221; and clear the selection reference. Clearing the selection means we will have to clear the canvas and redraw everything without the selection ring, so we set the valid flag to false.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
  <span style="color: #006600; font-style: italic;">// ...</span>
  <span style="color: #006600; font-style: italic;">// (We are still in the CanvasState constructor)</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// This is an example of a closure!</span>
  <span style="color: #006600; font-style: italic;">// Right here &quot;this&quot; means the CanvasState. But we are making events on the Canvas itself,</span>
  <span style="color: #006600; font-style: italic;">// and when the events are fired on the canvas the variable &quot;this&quot; is going to mean the canvas!</span>
  <span style="color: #006600; font-style: italic;">// Since we still want to use this particular CanvasState in the events we have to save a reference to it.</span>
  <span style="color: #006600; font-style: italic;">// This is our reference!</span>
  <span style="color: #003366; font-weight: bold;">var</span> myState <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">//fixes a problem where double clicking causes text to get selected on the canvas</span>
  canvas.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selectstart'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #006600; font-style: italic;">// Up, down, and move are for dragging</span>
  canvas.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mousedown'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> mouse <span style="color: #339933;">=</span> myState.<span style="color: #660066;">getMouse</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> mx <span style="color: #339933;">=</span> mouse.<span style="color: #660066;">x</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> my <span style="color: #339933;">=</span> mouse.<span style="color: #660066;">y</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> shapes <span style="color: #339933;">=</span> myState.<span style="color: #660066;">shapes</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> l <span style="color: #339933;">=</span> shapes.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> l<span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>shapes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">contains</span><span style="color: #009900;">&#40;</span>mx<span style="color: #339933;">,</span> my<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> mySel <span style="color: #339933;">=</span> shapes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #006600; font-style: italic;">// Keep track of where in the object we clicked</span>
        <span style="color: #006600; font-style: italic;">// so we can move it smoothly (see mousemove)</span>
        myState.<span style="color: #660066;">dragoffx</span> <span style="color: #339933;">=</span> mx <span style="color: #339933;">-</span> mySel.<span style="color: #660066;">x</span><span style="color: #339933;">;</span>
        myState.<span style="color: #660066;">dragoffy</span> <span style="color: #339933;">=</span> my <span style="color: #339933;">-</span> mySel.<span style="color: #660066;">y</span><span style="color: #339933;">;</span>
        myState.<span style="color: #660066;">dragging</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        myState.<span style="color: #660066;">selection</span> <span style="color: #339933;">=</span> mySel<span style="color: #339933;">;</span>
        myState.<span style="color: #660066;">valid</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #006600; font-style: italic;">// havent returned means we have failed to select anything.</span>
    <span style="color: #006600; font-style: italic;">// If there was an object selected, we deselect it</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>myState.<span style="color: #660066;">selection</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      myState.<span style="color: #660066;">selection</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
      myState.<span style="color: #660066;">valid</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Need to clear the old selection border</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The <strong>mousemove</strong> event checks to see if we have set the dragging flag to true. If we have it gets the current mouse positon and moves the selected object to that position, remembering the offset of where we were grabbing it. If the dragging flag is false the <strong>mousemove</strong> event does nothing.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  canvas.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mousemove'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>myState.<span style="color: #660066;">dragging</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> mouse <span style="color: #339933;">=</span> myState.<span style="color: #660066;">getMouse</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #006600; font-style: italic;">// We don't want to drag the object by its top-left corner,</span>
      <span style="color: #006600; font-style: italic;">// we want to drag from where we clicked.</span>
      <span style="color: #006600; font-style: italic;">// Thats why we saved the offset and use it here</span>
      myState.<span style="color: #660066;">selection</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> mouse.<span style="color: #660066;">x</span> <span style="color: #339933;">-</span> myState.<span style="color: #660066;">dragoffx</span><span style="color: #339933;">;</span>
      myState.<span style="color: #660066;">selection</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> mouse.<span style="color: #660066;">y</span> <span style="color: #339933;">-</span> myState.<span style="color: #660066;">dragoffy</span><span style="color: #339933;">;</span>   
      myState.<span style="color: #660066;">valid</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Something's dragging so we must redraw</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The <strong>mouseup</strong> event is simple, all it has to do is update the CanvasState so that we are no longer dragging! So once you lift the mouse, the <strong>mousemove</strong> event is back to doing nothing.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  canvas.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'mouseup'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    myState.<span style="color: #660066;">dragging</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The <strong>dblclick</strong> event we&#8217;ll use to add more Shapes to our canvas. It calls addShape on the CanvasState with a new instance of Shape. all addShape does is add the argument to the list of Shapes in the CanvasState.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// double click for making new Shapes</span>
  canvas.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'dblclick'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> mouse <span style="color: #339933;">=</span> myState.<span style="color: #660066;">getMouse</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    myState.<span style="color: #660066;">addShape</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Shape<span style="color: #009900;">&#40;</span>mouse.<span style="color: #660066;">x</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> mouse.<span style="color: #660066;">y</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span>
                               <span style="color: #3366CC;">'rgba(0,255,0,.6)'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There are a few options I implemented, what the selection ring looks like and how often we redraw. setInterval simply calls our CanvasState&#8217;s draw method. Our interval of 30 means that we call the draw method every 30 milliseconds.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// **** Options! ****</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">selectionColor</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'#CC0000'</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">selectionWidth</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>  
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">interval</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">30</span><span style="color: #339933;">;</span>
  setInterval<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> myState.<span style="color: #660066;">draw</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> myState.<span style="color: #660066;">interval</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Drawing</h3>
<p>Now we&#8217;re set up to draw every 30 milliseconds, which will allow us to continuously update the canvas so it appears like the shapes we drag are smoothly moving around. However, drawing doesn&#8217;t just mean drawing the shapes over and over; we also have to clear the canvas on every draw. If we don&#8217;t clear it, dragging will look like the shape is making a solid line because none of the old shape-positions will go away.</p>
<p>Because of this, we clear the entire canvas before each Draw frame. This can get expensive, and we only want to draw if something has actually changed within our framework, which is why we have the &#8220;valid&#8221; flag in our CanvasState.</p>
<p>After everything is drawn the draw method will set the valid flag to true. Then, ocne we do something like adding a new Shape or trying to drag a Shape, the state will get invalidated and draw() will clear, redraw all objects, and set the valid flag again.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// While draw is called as often as the INTERVAL variable demands,</span>
<span style="color: #006600; font-style: italic;">// It only ever does something if the canvas gets invalidated by our code</span>
CanvasState.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">draw</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// if our state is invalid, redraw and validate!</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">valid</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> ctx <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">ctx</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> shapes <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">shapes</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">clear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// ** Add stuff you want drawn in the background all the time here **</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// draw all shapes</span>
    <span style="color: #003366; font-weight: bold;">var</span> l <span style="color: #339933;">=</span> shapes.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> l<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> shape <span style="color: #339933;">=</span> shapes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #006600; font-style: italic;">// We can skip the drawing of elements that have moved off the screen:</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>shape.<span style="color: #660066;">x</span> <span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">width</span> <span style="color: #339933;">||</span> shape.<span style="color: #660066;">y</span> <span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">height</span> <span style="color: #339933;">||</span>
          shape.<span style="color: #660066;">x</span> <span style="color: #339933;">+</span> shape.<span style="color: #660066;">w</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">||</span> shape.<span style="color: #660066;">y</span> <span style="color: #339933;">+</span> shape.<span style="color: #660066;">h</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
      shapes<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">draw</span><span style="color: #009900;">&#40;</span>ctx<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// draw selection</span>
    <span style="color: #006600; font-style: italic;">// right now this is just a stroke along the edge of the selected Shape</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">selection</span> <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      ctx.<span style="color: #660066;">strokeStyle</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">selectionColor</span><span style="color: #339933;">;</span>
      ctx.<span style="color: #660066;">lineWidth</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">selectionWidth</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> mySel <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">selection</span><span style="color: #339933;">;</span>
      ctx.<span style="color: #660066;">strokeRect</span><span style="color: #009900;">&#40;</span>mySel.<span style="color: #660066;">x</span><span style="color: #339933;">,</span>mySel.<span style="color: #660066;">y</span><span style="color: #339933;">,</span>mySel.<span style="color: #660066;">w</span><span style="color: #339933;">,</span>mySel.<span style="color: #660066;">h</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// ** Add stuff you want drawn on top all the time here **</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">valid</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We go through all of shapes[] and draw each one in order. This will give the nice appearance of later shapes looking as if they are on top of earlier shapes. After all the shapes are drawn, a selection handle (if there is a selection) gets drawn around the shape that this.selection references.</p>
<p>If you wanted a background (like a city) or a foreground (like clouds), one way to add them is to put them before or after the main two drawing bits. There are often better ways though, like using multiple canvases or a CSS background-image, but we won&#8217;t go over that here.</p>
<h3>Getting mouse coordinates on Canvas</h3>
<p>Getting good mouse coordinates is a little tricky on Canvas. You could use offsetX/Y and LayerX/Y, but LayerX/Y is deprecated in webkit (Chrome and Safari) and Firefox does not have offsetX/Y.</p>
<p>The most bulletproof way to get the correct mouse position is shown below. You have to walk up the tree adding the offsets together. Then you must add any padding or border to the offset. Finally, to fix coordinate problems when you have fixed-position elements on the page (like the wordpress admin bar or a stumbleupon bar) you must add the &lt;html&gt;&#8217;s offsetTop and offsetLeft.</p>
<p>Then you simply subtract that offset from the e.pageX/Y values and you&#8217;ll get perfect coordinates in almost every possible situation.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Creates an object with x and y defined,</span>
<span style="color: #006600; font-style: italic;">// set to the mouse position relative to the state's canvas</span>
<span style="color: #006600; font-style: italic;">// If you wanna be super-correct this can be tricky,</span>
<span style="color: #006600; font-style: italic;">// we have to worry about padding and borders</span>
CanvasState.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">getMouse</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> element <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">canvas</span><span style="color: #339933;">,</span> offsetX <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> offsetY <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> mx<span style="color: #339933;">,</span> my<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Compute the total offset</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>element.<span style="color: #660066;">offsetParent</span> <span style="color: #339933;">!==</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">do</span> <span style="color: #009900;">&#123;</span>
      offsetX <span style="color: #339933;">+=</span> element.<span style="color: #660066;">offsetLeft</span><span style="color: #339933;">;</span>
      offsetY <span style="color: #339933;">+=</span> element.<span style="color: #660066;">offsetTop</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>element <span style="color: #339933;">=</span> element.<span style="color: #660066;">offsetParent</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Add padding and border style widths to offset</span>
  <span style="color: #006600; font-style: italic;">// Also add the &lt;html&gt; offsets in case there's a position:fixed bar</span>
  offsetX <span style="color: #339933;">+=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">stylePaddingLeft</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">styleBorderLeft</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">htmlLeft</span><span style="color: #339933;">;</span>
  offsetY <span style="color: #339933;">+=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">stylePaddingTop</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">styleBorderTop</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">htmlTop</span><span style="color: #339933;">;</span>
&nbsp;
  mx <span style="color: #339933;">=</span> e.<span style="color: #660066;">pageX</span> <span style="color: #339933;">-</span> offsetX<span style="color: #339933;">;</span>
  my <span style="color: #339933;">=</span> e.<span style="color: #660066;">pageY</span> <span style="color: #339933;">-</span> offsetY<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// We return a simple javascript object (a hash) with x and y defined</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>x<span style="color: #339933;">:</span> mx<span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> my<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>At long last</h3>
<p>From here its just a few lines to draw some shapes to move around. We make one instance of CanvasState, passing it a reference to the canvas we want to use, then we can add any number of new shapes to it. The code below produces the example at the top of this page:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> s <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> CanvasState<span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'canvas1'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
s.<span style="color: #660066;">addShape</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Shape<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span><span style="color: #CC0000;">50</span><span style="color: #339933;">,</span><span style="color: #CC0000;">50</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// The default is gray</span>
s.<span style="color: #660066;">addShape</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Shape<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">60</span><span style="color: #339933;">,</span><span style="color: #CC0000;">140</span><span style="color: #339933;">,</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span><span style="color: #CC0000;">60</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'lightskyblue'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Lets make some partially transparent</span>
s.<span style="color: #660066;">addShape</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Shape<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">80</span><span style="color: #339933;">,</span><span style="color: #CC0000;">150</span><span style="color: #339933;">,</span><span style="color: #CC0000;">60</span><span style="color: #339933;">,</span><span style="color: #CC0000;">30</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'rgba(127, 255, 212, .5)'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
s.<span style="color: #660066;">addShape</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> Shape<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">125</span><span style="color: #339933;">,</span><span style="color: #CC0000;">80</span><span style="color: #339933;">,</span><span style="color: #CC0000;">30</span><span style="color: #339933;">,</span><span style="color: #CC0000;">80</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'rgba(245, 222, 179, .7)'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There are a few little methods I added that are not shown, such as Shape&#8217;s method to see if a point is inside its bounds. You can see and download the full demo source <a href="http://simonsarris.com/project/canvasdemo/shapes.html">here</a>.</p>
<p>Now that we have a basic structure down, it is easy to write code that handles more complex shapes, like paths or images or video. Rotation and scaling these things takes a bit more work, but is quite doable with the Canvas and our selection method is already set up to deal with them.</p>
<p>If you would like to see this code enhanced in future posts (or have any fixes), let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://simonsarris.com/blog/510-making-html5-canvas-useful/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>The most amazing thing</title>
		<link>http://simonsarris.com/blog/503-the-most-amazing-thing</link>
		<comments>http://simonsarris.com/blog/503-the-most-amazing-thing#comments</comments>
		<pubDate>Fri, 16 Dec 2011 05:43:48 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[smartphone]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://simonsarris.com/?p=503</guid>
		<description><![CDATA[I live my life in a state of constant, quiet amazement. Almost everything is impressive to me, from the last book I read to the last soup I ate. I don&#8217;t get bored, and am often happy to just sit and think about things, pretty much any thing. It&#8217;s just reached midnight in New Hampshire, [...]]]></description>
			<content:encoded><![CDATA[<p>I live my life in a state of constant, quiet amazement. Almost everything is impressive to me, from the last book I read to the last soup I ate. I don&#8217;t get bored, and am often happy to just sit and think about things, pretty much any thing.</p>
<p>It&#8217;s just reached midnight in New Hampshire, and &#8220;yesterday&#8221; the Galaxy Nexus was released for Verizon in the United States.</p>
<p>I&#8217;m sure to lots of people this is just yet-another-android-phone. Or for many its simply an upgrade from one gadget to the next. People will yawn, or fawn, or get on with their lives with or without it. I myself would probably be pleased as Punch with just about any modern smartphone.</p>
<p>Yesterday I bought a Galaxy Nexus, my first smartphone and second-ever cellphone.</p>
<p>Learning to set my alarm for tomorrow morning, I find myself reflecting. I really am completely in awe. Starstruck. I am a computer person, and here is a thing more powerful than most of the computers I have ever owned.</p>
<p>In New Hampshire 4G was rolled out Wednesday or so my coworkers tell me. The data capabilities of the phone are impressive to me, but then again 56k speeds on the phone would not be any less impressive. How could they be?</p>
<p>Somehow I have made it almost to 2012 without owning a GPS or an MP3 player or a pocket camera. It&#8217;s not that I&#8217;m against them, I just never found much need. Now I have a device with a scope and power so large I cannot <em>believe</em> it is this small.</p>
<p>The age of &#8220;wondering why&#8221; is over. This is instant portable access to the largest knowledge and communication infrastructure in the world. A rough approximation of the sum of human knowledge is literally carried along in my pocket. I can express and communicate and learn from anywhere. At my desk, in my bed, on the road, in the woods.</p>
<p>How is this not the most amazing thing?</p>
]]></content:encoded>
			<wfw:commentRss>http://simonsarris.com/blog/503-the-most-amazing-thing/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Understanding the HTML5 Canvas image security rules</title>
		<link>http://simonsarris.com/blog/480-understanding-the-html5-canvas-image-security-rules</link>
		<comments>http://simonsarris.com/blog/480-understanding-the-html5-canvas-image-security-rules#comments</comments>
		<pubDate>Wed, 30 Nov 2011 06:56:24 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://simonsarris.com/?p=480</guid>
		<description><![CDATA[There&#8217;s a common point of confusion regarding when one can use HTML5 Canvas getImageData() and toDataUrl() methods. Certain operations will cause these methods to throw a security error instead of functioning normally. The rules for what one can and cannot do are laid out in the spec, though the reasoning behind them isn&#8217;t so obvious. [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a common point of confusion regarding when one can use HTML5 Canvas <span style="font-family: Courier New;">getImageData()</span> and <span style="font-family: Courier New;">toDataUrl()</span> methods. Certain operations will cause these methods to throw a security error instead of functioning normally.</p>
<p>The rules for what one can and cannot do are laid out in the spec, though the reasoning behind them isn&#8217;t so obvious. The most typical violation is when a programmer uses <span style="font-family: Courier New;">drawImage()</span> with an image that is from a different domain (than the page that the canvas is on) or an image that is on the local file system. When <span style="font-family: Courier New;">drawImage()</span> is used in one of these two ways, the canvas internally sets its <i>origin-clean</i> flag to false.</p>
<p>From the moment a canvas has its <i>origin-clean</i> flag set to false one is not allowed to use the <span style="font-family: Courier New;">getImageData()</span> and <span style="font-family: Courier New;">toDataUrl()</span> methods of that canvas, instead the security error will be thrown. There are a few more cases where the origin-clean flag will be set to false, you can read about them in the spec <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#security-with-canvas-elements">here.</a></p>
<p>The reason for this security is to prevent something called information leakage. To see why this is a security issue, consider the following hypothetical situation:</p>
<p>Say you are on a work network and so you have access to internal, private company sites and your (private!) hard-drive. You can accessThe private sites might be something like <span style="font-family: Courier New;">www.internal.myCompany.com</span> and your hard drive would be accessible from urls like <span style="font-family: Courier New;">file:///C:/SomeOfMyPhotos/</span>.</p>
<p>Now suppose you visited a website with a hidden canvas and while you were browsing the site that canvas was constantly calling drawImage() onto that canvas with urls that it was guessing might exist. These urls would be things like an image on the private subdomain:</p>
<p><span style="font-family: Courier New;">www.internal.myCompany.com/secret/secret-plans.jpg</span></p>
<p>Or an image on your hard drive:</p>
<p><span style="font-family: Courier New;">file:///C:/SomeOfMyPhotos/thatEmbarassingPhoto.png</span></p>
<p>The malicious site could keep trying different combinations of private-to-you urls until it found one that was actually a file. Then it would draw it to the canvas. Then it would get the imageData from the canvas and send it off to the server.</p>
<p>Voila! The malicious site owner now has your secret plans and your embarrassing photos, much without your consent.</p>
<p>Now we know that the above scenario is not very probable: In the real world, secret plans are almost always in PNG format whereas embarassing photos are almost universally in JPG format! But it stands that situations like the above could happen and so the security implications of canvas must take this into account.</p>
]]></content:encoded>
			<wfw:commentRss>http://simonsarris.com/blog/480-understanding-the-html5-canvas-image-security-rules/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A transformation class for Canvas to keep track of the transformation matrix</title>
		<link>http://simonsarris.com/blog/471-a-transformation-class-for-canvas-to-keep-track-of-the-transformation-matrix</link>
		<comments>http://simonsarris.com/blog/471-a-transformation-class-for-canvas-to-keep-track-of-the-transformation-matrix#comments</comments>
		<pubDate>Fri, 23 Sep 2011 04:37:37 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://simonsarris.com/?p=471</guid>
		<description><![CDATA[The HTML5 Canvas does not have a method for getting the current transformation matrix. For some applications, keeping track of the current transformation matrix would be a nice feature to have. I&#8217;ve made this easier by creating a simple Transform class for use with Canvas. You can have a look at it here. It has [...]]]></description>
			<content:encoded><![CDATA[<p>The HTML5 Canvas does not have a method for getting the current transformation matrix. For some applications, keeping track of the current transformation matrix would be a nice feature to have.</p>
<p>I&#8217;ve made this easier by creating a simple Transform class for use with Canvas. You can have a look at it <a href="https://github.com/simonsarris/Canvas-tutorials/blob/master/transform.js" title="here" target="_blank">here</a>.</p>
<p>It has all of the Canvas equivalents, and can be used alongside canvas to record the matrix state or can be used instead of and then applied to the canvas.</p>
<p>In other words, a start-to-finish use of the Transform would be like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> t <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Transform<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
t.<span style="color: #660066;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> m <span style="color: #339933;">=</span> t.<span style="color: #660066;">m</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">setTransform</span><span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> m<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> m<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> m<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> m<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> m<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Which will do the exact same thing as this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">ctx.<span style="color: #660066;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Or the shorter:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> t <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Transform<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
t.<span style="color: #660066;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>But of course allow you to keep track of it!</p>
<p>If you wanted, you could easily call the class to take a context and always do the operations when it is called.</p>
]]></content:encoded>
			<wfw:commentRss>http://simonsarris.com/blog/471-a-transformation-class-for-canvas-to-keep-track-of-the-transformation-matrix/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Increasing Performance by Caching Paths on HTML5 Canvas</title>
		<link>http://simonsarris.com/blog/427-increasing-performance-by-caching-paths-on-canvas</link>
		<comments>http://simonsarris.com/blog/427-increasing-performance-by-caching-paths-on-canvas#comments</comments>
		<pubDate>Fri, 29 Jul 2011 04:42:35 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://simonsarris.com/?p=427</guid>
		<description><![CDATA[This article is one in a series of performance articles I am writing on Canvas. I’m considering writing a small (e-)book on Canvas performance issues, considerations and tips. If you’d be interested in that sort of thing, let me know. Much of the functionality of Canvas comes from its path drawing functions. Unfortunately for game [...]]]></description>
			<content:encoded><![CDATA[<p><em>This article is one in a series of <a href="http://simonsarris.com/blog/tag/performance">performance articles</a> I am writing on Canvas. I’m considering writing a small (e-)book on Canvas performance issues, considerations and tips. If you’d be interested in that sort of thing, let me know.</em></p>
<p>Much of the functionality of Canvas comes from its path drawing functions. Unfortunately for game designers and animators, re-drawing paths over and over can amount to a tangible performance hit. To increase performance, let&#8217;s take a look at caching paths as images to avoid redrawing them traditionally.</p>
<p><center><br />
<a title='By Signey Paget (1860-1908) (Strand Magazine) [Public domain], via Wikimedia Commons' href='http://commons.wikimedia.org/wiki/File:Paget_holmes.png'><img width='240' alt='Paget holmes' src='http://upload.wikimedia.org/wikipedia/commons/7/73/Paget_holmes.png'/></a><br />
<em>Gentlemen waiting for a path to finish rendering</em><br />
</center></p>
<p>First we need to ensure that caching a path will actually lead to a performance increase. We can devise a simple test for this using JSPerf. We need a path to test, so let&#8217;s write something fairly simple.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">ctx.<span style="color: #660066;">beginPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">strokeStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'red'</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">lineWidth</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">moveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">10</span><span style="color: #339933;">,</span><span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">30</span><span style="color: #339933;">,</span><span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">lineTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">40</span><span style="color: #339933;">,</span><span style="color: #CC0000;">70</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">quadraticCurveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">72</span><span style="color: #339933;">,</span><span style="color: #CC0000;">43</span><span style="color: #339933;">,</span><span style="color: #CC0000;">22</span><span style="color: #339933;">,</span><span style="color: #CC0000;">12</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">quadraticCurveTo</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">12</span><span style="color: #339933;">,</span><span style="color: #CC0000;">43</span><span style="color: #339933;">,</span><span style="color: #CC0000;">12</span><span style="color: #339933;">,</span><span style="color: #CC0000;">102</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">stroke</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This does not produce a particularly complex or exciting path:</p>
<p><center><br />
<img src="http://simonsarris.com/images/sample-path.png"/><br />
</center></p>
<p>But it will do.</p>
<p>The thing we want to ponder here is whether redrawing this path, in other words executing every one of the instructions needed to make the path, will be a slower process than if we cached it. We can achieve caching by drawing the path from these instructions only once, to an in-memory canvas, and then using drawImage from our in-memory canvas onto our real canvas to redraw the path.</p>
<p>That isn&#8217;t the only way to cache. We could instead draw it to a canvas and then make a PNG out of it, and call drawImage from that PNG instead, but for the sake of making a simpler test we will stick with using an in-memory canvas.</p>
<p>So let us take all of the drawing instructions above and execute them on the in-memory canvas. Then in our draw loop, instead of drawing out the path every time, we simply draw the in-memory canvas to our real canvas:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// can2 is our in-memory canvas</span>
ctx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>can2<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The test is simple enough. Giving it a go, the results are immediately clear: pre-rendering and using drawImage is more than ten times as fast as drawing the path, even for the relatively simple path used!</p>
<p><center><br />
<img src="http://simonsarris.com/images/canvaspath-render-pre.PNG"/><br />
</center></p>
<p>The more complex the path, the more time you will save with caching. If you&#8217;re using a lot of complex paths to render shapes, such an optimization ought to speed up your draw loop by a great deal. The <a href="http://jsperf.com/render-vs-prerender">JSPerf page</a> shows a simple setup if you want to make the test for yourself.</p>
<h2>Other considerations</h2>
<p>Pre-rendering paths isn&#8217;t a magic bullet, there are still a lot of uses for drawing paths constantly in canvas. If you are making a live drawing application, or otherwise constructing dynamic paths and/or moving and animating paths on the fly, then any kind of pre-rendering is going to be nearly pointless or even harmful to performance. After all, what&#8217;s the use of drawing something to a separate canvas and drawing that state back to the original canvas if the path changes constantly? You&#8217;d need to re-draw the in-memory canvas just as often, so you&#8217;d lose all benefit.</p>
<p>It is also worth mentioning that you might want to play around with using PNGs instead of in-memory canvases. Another thing to test is putting multiple paths onto one in-memory canvas versus putting them all in their own separate canvases, effectively making a sprite sheet. From previous tests, it seems that there is a slight advantage to giving each sprite its own png instead of using a single-png (or single in-memory canvas) sprite-sheet, but it wasn&#8217;t that big of a difference.</p>
<p>If you do choose to use a sprite sheet, note that there are a lot of decent tools out there for compressing and organizing them, such as <a href="http://spritesheetpacker.codeplex.com/">Sprite Sheet Packer.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://simonsarris.com/blog/427-increasing-performance-by-caching-paths-on-canvas/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Maybe Google+&#8217;s trickle of invites is a wise and calculated move</title>
		<link>http://simonsarris.com/blog/418-maybe-googles-trickle-of-invites-is-a-wise-move</link>
		<comments>http://simonsarris.com/blog/418-maybe-googles-trickle-of-invites-is-a-wise-move#comments</comments>
		<pubDate>Tue, 05 Jul 2011 21:51:56 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://simonsarris.com/?p=418</guid>
		<description><![CDATA[Friends, HNers, and redditors alike are still complaining that they have yet to be invited to Google+. Initially I thought that Google+ being invite-only was a folly on Google&#8217;s part; some silly thing that they didn&#8217;t work out beforehand that could be a large blow in its infancy. But after several days of having Google+ [...]]]></description>
			<content:encoded><![CDATA[<p>Friends, HNers, and redditors alike are still complaining that they have yet to be invited to Google+. Initially I thought that Google+ being invite-only was a folly on Google&#8217;s part; some silly thing that they didn&#8217;t work out beforehand that could be a large blow in its infancy.</p>
<p>But after several days of having Google+ I realized something: Every single day since I&#8217;ve had it I have gotten at least some new Google+ notifications, as 4 or 5 more people add me per day.</p>
<p>In other words, a day has yet to go by where Google+ has not reminded me of its existence. A day has yet to go by where I have not wandered over to Goole+ to see whats up with the newcomers.</p>
<p>Instead of signing up, looking around to see what there is to see, then perhaps not logging back in for a week or a month, the trickle of Google+ invites is causing me to go look around at least once a day.</p>
<p>I&#8217;m beginning to think that the invites were actually a calculated move to keep the initial users coming back, welcoming their friends as they join and &#8220;showing them around&#8221; so to speak, all the while adding more activity to Google+.</p>
<p>Instead of creating a flash-mob that might have dispersed after a month or two, Google has created a pilgrimage. Very clever, I think, if intentional.</p>
]]></content:encoded>
			<wfw:commentRss>http://simonsarris.com/blog/418-maybe-googles-trickle-of-invites-is-a-wise-move/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How you clear your HTML5 Canvas matters</title>
		<link>http://simonsarris.com/blog/346-how-you-clear-your-canvas-matters</link>
		<comments>http://simonsarris.com/blog/346-how-you-clear-your-canvas-matters#comments</comments>
		<pubDate>Fri, 10 Jun 2011 20:20:57 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://simonsarris.com/?p=346</guid>
		<description><![CDATA[Stop the press! As seen in jsperf, the nightly build of Chrome (14) has optimized the width-setting case and now swings heavily the other way. As with all optimizations, be sure to bench often on the platforms and browser versions you are targeting. I&#8217;m considering writing a small (e-)book on Canvas performance issues, considerations and [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Stop the press!</strong> As seen in <a href="http://jsperf.com/canvasclear">jsperf</a>, the nightly build of Chrome (14) has optimized the width-setting case and now swings <em>heavily</em> the other way. As with all optimizations, be sure to bench often on the platforms and browser versions you are targeting.</p>
<p><em>I&#8217;m considering writing a small (e-)book on Canvas performance issues, considerations and tips. If you&#8217;d be interested in that sort of thing, let me know.</em></p>
<p>As much as we all like to see dramatic performance increases from clever optimizations, getting the best Canvas performance often means scrutinizing every little place in our code. Today we&#8217;ll take a look at how canvases are cleared.</p>
<p><center><br />
<a title='By Sidney Paget (1860 - 1908) (Strand Magazine) [Public domain], via Wikimedia Commons' href='http://commons.wikimedia.org/wiki/File:Seco-06.jpg'><img width='240' alt='Seco-06' src='http://upload.wikimedia.org/wikipedia/commons/5/5b/Seco-06.jpg'/></a><br />
<em>A man&#8217;s careful search for his receding hairline</em><br />
</center></p>
<p>One of the ways that is implicitly endorsed in the spec and often used in people&#8217;s apps to clear a canvas is this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">canvas.<span style="color: #660066;">width</span> <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">width</span><span style="color: #339933;">;</span></pre></div></div>

<p>There is of course another common way to clear the canvas using a context method:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">ctx.<span style="color: #660066;">clearRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> canvas.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> canvas.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>They may seem to do the same thing on the surface, but the end difference between the two methods is large: Setting the canvas width to itself not only clears the canvas, it clears <em>all</em> canvas state. This means it clears the transformation matrix, the current clipping region, and all of the following attributes: strokeStyle, fillStyle, globalAlpha, lineWidth, lineCap, lineJoin, miterLimit, shadowOffsetX, shadowOffsetY, shadowBlur, shadowColor, globalCompositeOperation, font, textAlign, and textBaseline.</p>
<p>Much of the time clearing above doesn&#8217;t matter, except maybe the transformation matrix, because canvas programmers tend to set the relevant properties before they redraw each shape anyway.</p>
<p>The performance difference between the two above methods is also large, often an order of magnitude or more. The context&#8217;s clearRect method is much faster than setting the canvas width to itself. I have a jsperf page up <a href="http://jsperf.com/canvasclear">here</a> where you can see the results per browser.</p>
<p>Curiously, clearRect is faster on every browser except Safari on Windows, where it is the other way around. I can think of a few possible reasons why that might be the case, but I don&#8217;t want to speculate out loud. If someone on a Mac could test Safari for me, I&#8217;d be interested to know what it benches.</p>
<p>Back to clearRect. Not all is well all the time when using this method. After all, if your canvas context has anything but the identity transform, there&#8217;s a good chance you won&#8217;t be clearing the entire canvas. This leads some people to end up erroneously using the width-setting method. Additionally, many people want to clear the canvas but keep their transformation matrix the same. Both of these problems can be fixed in one go, so in the interest of completeness, lets see a safer way:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// I have lots of transforms right now</span>
ctx.<span style="color: #660066;">save</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">setTransform</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Will always clear the right space</span>
ctx.<span style="color: #660066;">clearRect</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> canvas.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> canvas.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ctx.<span style="color: #660066;">restore</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Still have my old transforms</span></pre></div></div>

<p>Not only does it clear the screen, but it ensures that any existing transformation won&#8217;t get in the way, and also allows you to keep that transformation should you need it. If you don&#8217;t need the transformation, you can of course remove the calls to save() and restore().</p>
<p>Because of the large performance discrepancy, I tentatively suggest the use of clearRect over setting the canvas width equal to itself, though the canvas width method is still useful for doing a complete reset of the context state. Of course, browser development happens rapidly and you should always test on the browsers and systems you are targeting.</p>
]]></content:encoded>
			<wfw:commentRss>http://simonsarris.com/blog/346-how-you-clear-your-canvas-matters/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>HTML5 Canvas drawText considered harmful</title>
		<link>http://simonsarris.com/blog/322-canvas-drawtext-considered-harmful</link>
		<comments>http://simonsarris.com/blog/322-canvas-drawtext-considered-harmful#comments</comments>
		<pubDate>Sat, 23 Apr 2011 02:44:11 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://simonsarris.com/?p=322</guid>
		<description><![CDATA[I have in my possession sufficient evidence for the conviction of Canvas&#8217; drawText(). The charge? Performance murder. drawText() resisting arrest I suspected drawText() of being slow ever since adding blocks of text as interactive objects to a Canvas library I am working on. I&#8217;ve also begun writing a canvas performance guidebook, so the sacrament of [...]]]></description>
			<content:encoded><![CDATA[<p>I have in my possession sufficient evidence for the conviction of Canvas&#8217; drawText().</p>
<p>The charge? <em>Performance murder.</em></p>
<p><center><br />
<a title='By Sidney Paget (1860 - 1908) (Strand Magazine) [Public domain], via Wikimedia Commons' href='http://commons.wikimedia.org/wiki/File:Reig-05.jpg'><img width='240' alt='Reig-05' src='http://upload.wikimedia.org/wikipedia/commons/c/c1/Reig-05.jpg'/></a><br />
<em>drawText() resisting arrest</em><br />
</center></p>
<p>I suspected drawText() of being slow ever since adding blocks of text as interactive objects to a Canvas library I am working on. I&#8217;ve also begun writing a canvas performance guidebook, so the sacrament of confirmation was doubly in order.</p>
<p>Since most of the slowness in canvas applications isn&#8217;t drawing something once, but redrawing it many times per second, I wondered if there were a faster way to redraw text than using the Canvas context&#8217;s drawText() method.</p>
<p>This is especially pertinent when drawing vertical text, since every new letter has to be on another line, and thus another call to the drawText() method.</p>
<p>I puzzled around with ways to avoid all the calls, and settled on trying something: Instead of calling drawText() to redraw my text objects each frame, I would instead create a new canvas (one never added to the DOM) for every single text object, and call drawText() on each object only once, drawing the text to its personal canvas.</p>
<p>Then, every time I wanted to (re)draw that text object, I would call draw<strong>Image()</strong> on my real canvas, passing in the object&#8217;s personal canvas, instead of using drawText(). This gives exactly the same per-pixel drawing, but is it faster?</p>
<p>I wrote a small, distilled test for the purpose, hypothesizing that it probably wasn&#8217;t worth doing for normal text but may be worth doing for vertical text. There&#8217;s a link at the end if you&#8217;re interested in trying it yourself.</p>
<p>There are a lot of scenarios though: What if I had just one text object that had to be redrawn 60 times a second? Surely &#8211; if drawImage() is faster than drawText() &#8211; having an additional canvas would be a small price to pay in overhead.</p>
<p>But what if I had 200 text objects? Maybe each text object having a canvas would cause it to be far slower.</p>
<p>I won&#8217;t bore you much longer, the definitive answer is this: <strong>If you are redrawing several text objects more than just a few times, it is far faster to give each object its own canvas and use drawImage() instead of drawText().</strong> Not just for vertical text, but for <em>any</em> text, even if its just drawing a single character.</p>
<p>Some of the tests are described below in my poorly-made graphs. The first row of graphs are closer to real-use scenarios. Please note the Y-axis range (milliseconds) changes from graph to graph.</p>
<p><center>Six of the tests. Smaller bars are better.</center><br />
<img width='600' src="http://simonsarris.com/images/canvastext-performance.PNG"/></p>
<p>As you can see, drawing a text object of just 1 character several hundred times is far faster in Chrome, Firefox and, IE9 when using drawImage(), and thus very much worth it to have that extra canvas around.</p>
<p>The second row of graphs were some fooling around to find the limit of this method&#8217;s utility. If you have 200 text objects (and thus 200 additional canvases) of just 1 character each, and render them only once, using drawImage() becomes a bit of a waste. But drawing those 200 objects merely three times and you can see that it will be faster in chrome to use drawImage() than to use drawText(). Firefox and IE9 are a little more resilient here, but they don&#8217;t take very long at all for drawImage() to become worth it.</p>
<p>So giving each text object its own Canvas is a pretty workable solution even if the objects are redrawn very few times. If you are trying to achieve 30 frames per second, giving each text object its own Canvas and using drawImage() is a no-brainer, and should probably a default consideration.</p>
<p>Of course <strong>Canvas applications can differ wildly, and you should draw up tests for your own situation before deciding how to draw text.</strong> Perhaps exceptionally large canvases or exceptionally small text yield different results, or perhaps memory on the targeted machine means that so making so many canvases are an untenable proposition, and so on.</p>
<p><a href="https://github.com/simonsarris/Canvas-tutorials/blob/master/verticalText.js">Here&#8217;s a link to the test I made.</a> I would of course appreciate any corroboration of results that anyone is willing to do.</p>
<p>EXTRA CREDIT:</p>
<p>You&#8217;d need to do more work to get this to work with scaling text, since you&#8217;d have to size the temporary canvases appropriately. It does however have the added benefit of fixing the scaling + rotating text bug that currently exists in <a href="http://simonsarris.com/misc/scaleText.html">Chrome and Opera.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://simonsarris.com/blog/322-canvas-drawtext-considered-harmful/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Javascript: Allowing a copy function in a base class to create an instance of a subclass</title>
		<link>http://simonsarris.com/blog/291-javascript-copy-function</link>
		<comments>http://simonsarris.com/blog/291-javascript-copy-function#comments</comments>
		<pubDate>Wed, 02 Feb 2011 01:53:27 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://simonsarris.com/?p=291</guid>
		<description><![CDATA[Everyone knows Javascript doesn&#8217;t have classes. Thankfully most Javascript programmers are very good at playing pretend. Because so many people attempt to introduce a classical structure to their Javscript code, I figure I&#8217;ll share some of my findings. Below is one way of allowing a base class function to copy an instance of a subclass [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone knows Javascript doesn&#8217;t have classes. Thankfully most Javascript programmers are very good at playing pretend.</p>
<p>Because so many people attempt to introduce a classical structure to their Javscript code, I figure I&#8217;ll share some of my findings. Below is one way of allowing a base class function to copy an instance of a subclass while maintaining proper type.</p>
<p>Let&#8217;s say we have an Animal class and we subclass it to create a Cat class. We also want the Animal class to have a clone() method that will return work with its subclasses. That is, if I clone a Cat, I want &#8220;instanceof Cat&#8221; and &#8220;instanceof Animal&#8221; to both be true.</p>
<p>Let&#8217;s make the classes real quick. Here&#8217;s an Animal:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> Animal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// animal stuff</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// All of Animal's methods will go into its prototype</span>
Animal.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  saySomething<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;I am an animal!&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
  copy<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> newcopy<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subConstructor</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      newcopy <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">subConstructor</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      newcopy <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Animal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// maybe copy over other values</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> newcopy<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And then the Cat. Typically the easiest way to make a Javascript function be a &#8220;subclass&#8221; of another function is to set the prototype of the subclass equal to &#8220;new baseclass.&#8221; With cat we&#8217;ll go a little further and also save the constructor.</p>
<p>Cat:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> Cat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  Animal.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// call animal's constructor</span>
  <span style="color: #006600; font-style: italic;">// cat stuff</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// See below</span>
derive<span style="color: #009900;">&#40;</span>Animal<span style="color: #339933;">,</span> Cat<span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// override the saySomething function</span>
Cat.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">saySomething</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Meow!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There you have it. Cats created this way will be &#8220;instanceof Cat&#8221; and &#8220;instanceof Animal&#8221;, and so will their clones. We should of course make tests to see, and lucky for us all of them check out OK:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// some tests and their results in the comment</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Cat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>c <span style="color: #000066; font-weight: bold;">instanceof</span> Animal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">// true</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>c <span style="color: #000066; font-weight: bold;">instanceof</span> Cat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>      <span style="color: #006600; font-style: italic;">// true</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>c.<span style="color: #660066;">saySomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>      <span style="color: #006600; font-style: italic;">// &quot;Meow!&quot;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Animal<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>a <span style="color: #000066; font-weight: bold;">instanceof</span> Animal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   <span style="color: #006600; font-style: italic;">// true</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>a <span style="color: #000066; font-weight: bold;">instanceof</span> Cat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>      <span style="color: #006600; font-style: italic;">// false</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>a.<span style="color: #660066;">saySomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>      <span style="color: #006600; font-style: italic;">// &quot;I am an animal!&quot;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> copyCat <span style="color: #339933;">=</span> c.<span style="color: #660066;">copy</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>copyCat <span style="color: #000066; font-weight: bold;">instanceof</span> Animal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>copyCat <span style="color: #000066; font-weight: bold;">instanceof</span> Cat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">// true</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>copyCat.<span style="color: #660066;">saySomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">// &quot;Meow!&quot;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> copyAnimal <span style="color: #339933;">=</span> a.<span style="color: #660066;">copy</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>copyAnimal <span style="color: #000066; font-weight: bold;">instanceof</span> Animal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>copyAnimal <span style="color: #000066; font-weight: bold;">instanceof</span> Cat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">// false</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>copyAnimal.<span style="color: #660066;">saySomething</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #006600; font-style: italic;">// &quot;I am an animal!&quot;</span></pre></div></div>

<p>You might want to use the four lines needed for subclassing elsewhere so its a good idea to stuff them into a function that you&#8217;d put in a utility class/var somewhere. So we could make:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> derive<span style="color: #009900;">&#40;</span>baseclass<span style="color: #339933;">,</span> subclass<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">function</span> subproto<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
    subproto.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> baseclass.<span style="color: #660066;">prototype</span><span style="color: #339933;">;</span>
    subclass.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> subproto<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    subclass.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">constructor</span> <span style="color: #339933;">=</span> subclass<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And then making a clone-able subclass would be as easy as calling derive after each constructor:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> Cat<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">constructor</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// call animal's constructor</span>
  <span style="color: #006600; font-style: italic;">// cat stuff</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
derive<span style="color: #009900;">&#40;</span>Animal<span style="color: #339933;">,</span> Cat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// override the saySomething function</span>
Cat.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">saySomething</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Meow!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://simonsarris.com/blog/291-javascript-copy-function/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Looking over Google&#8217;s Cr-48 Chrome OS laptop</title>
		<link>http://simonsarris.com/blog/268-looking-over-cr-48</link>
		<comments>http://simonsarris.com/blog/268-looking-over-cr-48#comments</comments>
		<pubDate>Fri, 10 Dec 2010 03:26:33 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[cr-48]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[laptop]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://simonsarris.com/?p=268</guid>
		<description><![CDATA[Merely two days after I apply to test drive a Chrome OS notebook, one arrives in the mail! It was packaged inside a very happy box. The contents were minimal: One laptop, one battery, one charger, one single page instruction card and one business card from Intel. Challenge accepted. The exterior of the laptop has [...]]]></description>
			<content:encoded><![CDATA[<p>Merely two days after I apply to test drive a Chrome OS notebook, one arrives in the mail! It was packaged inside a very happy box.</p>
<p><img alt="" src="http://simonsarris.com/images/cr-48/01.jpg" /></p>
<p>The contents were minimal: One laptop, one battery, one charger, one single page instruction card and one business card from Intel.</p>
<p><img alt="" src="http://simonsarris.com/images/cr-48/02.jpg" /></p>
<p>Challenge accepted.</p>
<p><img alt="" src="http://simonsarris.com/images/cr-48/03.jpg" /></p>
<p><img alt="" src="http://simonsarris.com/images/cr-48/04.jpg" /></p>
<p><img alt="" src="http://simonsarris.com/images/cr-48/05.jpg" /></p>
<p>The exterior of the laptop has been detailed by plenty of others, so I won&#8217;t get into that. <a href="http://gdgt.com/google/cr-48/">gdgt.com</a> already has a decent page on it.</p>
<p>But before I started to really use it I wanted to poke around inside. Looking over the external didn&#8217;t yield anything interesting: Just a few screws. Oh, and this?</p>
<p><img alt="" src="http://simonsarris.com/images/cr-48/06.jpg" /></p>
<p>To the right of the battery connector was a tiny black piece of tape-like material. Peeling it away revealed a small switch. For the sake of curiosity I flipped it.</p>
<p><img alt="" src="http://simonsarris.com/images/cr-48/07.jpg" /></p>
<p>Uh oh. Apparently the switch controls something memory related. (note that there&#8217;s no hard drive in the Cr-48, just flash memory).</p>
<p>I turned the computer off and turned the switch back, hopeful that I might not have totally destroyed Google&#8217;s gift. Thankfully, it seems to have a way to reset itself.</p>
<p><img alt="" src="http://simonsarris.com/images/cr-48/08.jpg" /></p>
<p>I had to re-enter my wireless password and login credentials, but otherwise the notebook was back to &#8220;new.&#8221; Not that there&#8217;s much of a difference &#8211; one of the things Google is trying to tout here is how easily one can change computers and still keep all of one&#8217;s &#8220;stuff&#8221; in order.</p>
<p>Despite taking out all the bottom (and behind-the-foot) screws the laptop would not come apart, at least with gentle amounts of pressure. I wasn&#8217;t keen on continuing further without first having a few days of experiencing the OS, so seeing the Atom Inside will have to wait.</p>
<p>On the actually-using-the-product side of things, I&#8217;ve really enjoyed it so far. The keyboard is unorthodox, but mostly suited to a browser-only experience. The F1-F12 are gone in favor of a set of laptop+browser specific keys. Unfortunately, the page-up/down keys are forgone in favor of ALT + Up/Down. Similarly, the delete key is hidden. I didn&#8217;t see any way to get home/end functionality, which unfortunately are necessary for any real writing in my common usage of computers.</p>
<p>I was surprised to see that Chrome OS is really nothing <i>but</i> Chrome. No file browser, no terminal (EDIT: Yes there is! Press CTRL+ALT+T for a very limited terminal), no desktop, just a web browser. For most users this is enough, since I suspect that the majority of computer users in the world use a web browser, a word processor, and a chat client or two, all of which can now have counterparts that exist in the browser. I think it would have great potential as a writing laptop were it not for the loss of the dedicated home/end/delete keys.</p>
<p>After using the Cr-48, it almost seems silly that netbooks attempt to be full computers. Google&#8217;s beta laptop instead seems to be what netbooks should have been from the start: a platform for all the thousands of browser apps, big and small, that make up the vast majority of common user&#8217;s tasks and games, without any of the complications or cruft of an entire operating system dragging its feet around.</p>
<p>ChromeOS is certainly no Windows/Ubuntu/OSX replacement, but with both WiFi and cellphone-network enabled internet as options, I think it will be a great little thing to bring around, from cafes to traveling. Certainly better than my powerhouse HP Envy, after all, since I&#8217;m not going to be playing much Starcraft 2 while drinking soup and coffee or touring Belgium.</p>
<p>Instead I&#8217;ll be reading email and the paper and reddit, or any number of things that I do on computers most of the time, all from a browser that just happens to be an operating system.</p>
]]></content:encoded>
			<wfw:commentRss>http://simonsarris.com/blog/268-looking-over-cr-48/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

