<?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>Ajaxian &#187; Canvas</title>
	<atom:link href="http://ajaxian.com/by/topic/canvas/feed" rel="self" type="application/rss+xml" />
	<link>http://ajaxian.com</link>
	<description>Cleaning up the web with Ajax</description>
	<lastBuildDate>Tue, 22 May 2012 21:45:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
    	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Normal Mapped Lighting for Photos using Canvas</title>
		<link>http://ajaxian.com/archives/normal-mapped-lighting-for-photos-using-canvas</link>
		<comments>http://ajaxian.com/archives/normal-mapped-lighting-for-photos-using-canvas#comments</comments>
		<pubDate>Sat, 09 Oct 2010 15:19:14 +0000</pubDate>
		<dc:creator>Dion Almaer</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[Front Page]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=10427</guid>
		<description><![CDATA[Francois Laberge has a fun demonstration of creating 3d looking effects via lighting and normal mapping on 2d photos. The demos progressively add features, from left to right lighting, to glow effects, to a multiple colored lighting extravaganza! Very nice work Francois!]]></description>
			<content:encoded><![CDATA[<p><a href="http://twitter.com/francoislaberge">Francois Laberge</a> has a fun demonstration of <a href="http://endergen.com/labs/normal_mapping/me">creating 3d looking effects via lighting and normal mapping on 2d photos</a>.</p>
<p><object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/4ohXF1TVV7M?fs=1&amp;hl=en_US&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/4ohXF1TVV7M?fs=1&amp;hl=en_US&amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object></p>
<p>The demos progressively add features, from left to right lighting, to glow effects, to a multiple colored lighting extravaganza! Very nice work Francois!</p>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/normal-mapped-lighting-for-photos-using-canvas/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>&#8220;Or so they say&#8230;&#8221; by Mr. Doob</title>
		<link>http://ajaxian.com/archives/or-so-they-say-by-mr-doob</link>
		<comments>http://ajaxian.com/archives/or-so-they-say-by-mr-doob#comments</comments>
		<pubDate>Tue, 14 Sep 2010 11:00:51 +0000</pubDate>
		<dc:creator>Brad Neuberg</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=10228</guid>
		<description><![CDATA[Via Mr. Doob comes a cool canvas demo called "Or so they say..." he created. What's interesting about this demo is it uses HTML5 Audio and Canvas, including Mr. Doob's Three.js library and sequencer that he also used on The Wilderness Downtown project. Three.js is similar to Papervision3D in the Flash world, allowing you to <a href="http://ajaxian.com/archives/or-so-they-say-by-mr-doob">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<p><object width="640" height="360"><param name="movie" value="http://capped.micksam7.com/play.swf?vid=xplsv-or_so_they_say|mq" /><param name="wmode" value="direct" /><param name="allowFullScreen" value="true" /><param name="bgcolor" value="#000000" /><embed src="http://capped.micksam7.com/play.swf?vid=xplsv-or_so_they_say|mq" wmode="direct" bgcolor="#000000" allowFullScreen="true" width="640" height="360" type="application/x-shockwave-flash"></embed></object></p>
<p><a href="http://mrdoob.com/">Via Mr. Doob</a> <a href="http://mrdoob.com/blog/post/702">comes a cool canvas demo called "Or so they say..." he created</a>. What's interesting about this demo is it uses HTML5 Audio and Canvas, including Mr. Doob's <a href="http://github.com/mrdoob/three.js/">Three.js library and sequencer</a> that he also used on <a href="http://www.thewildernessdowntown.com/">The Wilderness Downtown</a> project.</p>
<p>Three.js is similar to <a href="http://blog.papervision3d.org/">Papervision3D</a> in the Flash world, allowing you to build up 3D scenes. Three.js then draws the 3D scene graphs using either Canvas, SVG, or WebGL. It's a very interesting library, but needs more docs (hint hint Mr. Doob!).</p>
<p>In the "Or so they say..." demo (<a href="http://github.com/mrdoob/xplsv_orsotheysay">source</a>), Mr. Doob starts by setting up Three.js, the camera, and the backing renderer:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-4');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-4">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">container = document.<span style="color: #006600;">createElement</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color: #3366CC;">'div'</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">camera = <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #006600;">Camera</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#800000;">60</span>, <span style="color:#800000;">800</span> / <span style="color:#800000;">600</span>, <span style="color:#800000;">1</span>, <span style="color:#800000;">20000</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">renderer = <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #006600;">CanvasRenderer</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">renderer.<span style="color: #006600;">setSize</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#800000;">800</span>, <span style="color:#800000;">600</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">renderer.<span style="color: #006600;">autoClear</span> = <span style="color: #003366; font-weight: bold;">false</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">container.<span style="color: #006600;">appendChild</span><span style="color:#006600; font-weight:bold;">&#40;</span> renderer.<span style="color: #006600;">domElement</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>The sequencer is then initialized and various 3D and time effects are added; a small snippet:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-5');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-5">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">sequencer = <span style="color: #003366; font-weight: bold;">new</span> Sequencer<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">sequencer.<span style="color: #006600;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> Vector3TravelEffect<span style="color:#006600; font-weight:bold;">&#40;</span> camera.<span style="color: #006600;">position</span>, <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #006600;">Vector3</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#800000;">0</span>, <span style="color:#800000;">0</span>, <span style="color:#800000;">200</span> <span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #006600;">Vector3</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#800000;">0</span>, <span style="color:#800000;">0</span>, <span style="color:#800000;">10000</span> <span style="color:#006600; font-weight:bold;">&#41;</span>&nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span>, keys<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#800000;">0</span> <span style="color:#006600; font-weight:bold;">&#93;</span> - <span style="color:#800000;">1</span>, keys<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#800000;">1</span> <span style="color:#006600; font-weight:bold;">&#93;</span> - <span style="color:#800000;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">sequencer.<span style="color: #006600;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> Vector3TravelEffect<span style="color:#006600; font-weight:bold;">&#40;</span> camera.<span style="color: #006600;">target</span>.<span style="color: #006600;">position</span>, <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #006600;">Vector3</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #006600;">Vector3</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>, keys<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#800000;">0</span> <span style="color:#006600; font-weight:bold;">&#93;</span> - <span style="color:#800000;">1</span>, keys<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#800000;">1</span> <span style="color:#006600; font-weight:bold;">&#93;</span> - <span style="color:#800000;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">sequencer.<span style="color: #006600;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> Part1Effect<span style="color:#006600; font-weight:bold;">&#40;</span> camera, renderer <span style="color:#006600; font-weight:bold;">&#41;</span>, keys<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#800000;">0</span> <span style="color:#006600; font-weight:bold;">&#93;</span>, keys<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#800000;">1</span> <span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">sequencer.<span style="color: #006600;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> Vector3TravelEffect<span style="color:#006600; font-weight:bold;">&#40;</span> camera.<span style="color: #006600;">position</span>, <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #006600;">Vector3</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#800000;">0</span>, <span style="color:#800000;">0</span>, <span style="color:#800000;">1000</span> <span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #006600;">Vector3</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#800000;">0</span>, <span style="color:#800000;">0</span>, <span style="color:#800000;">400</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>, keys<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#800000;">1</span> <span style="color:#006600; font-weight:bold;">&#93;</span> - <span style="color:#800000;">1</span>, keys<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#800000;">2</span> <span style="color:#006600; font-weight:bold;">&#93;</span> - <span style="color:#800000;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">sequencer.<span style="color: #006600;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> Vector3TravelEffect<span style="color:#006600; font-weight:bold;">&#40;</span> camera.<span style="color: #006600;">target</span>.<span style="color: #006600;">position</span>, <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #006600;">Vector3</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color: #003366; font-weight: bold;">new</span> THREE.<span style="color: #006600;">Vector3</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>, keys<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#800000;">1</span> <span style="color:#006600; font-weight:bold;">&#93;</span> - <span style="color:#800000;">1</span>, keys<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#800000;">2</span> <span style="color:#006600; font-weight:bold;">&#93;</span> - <span style="color:#800000;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">sequencer.<span style="color: #006600;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color: #003366; font-weight: bold;">new</span> Part2Effect<span style="color:#006600; font-weight:bold;">&#40;</span> camera, renderer <span style="color:#006600; font-weight:bold;">&#41;</span>, keys<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#800000;">1</span> <span style="color:#006600; font-weight:bold;">&#93;</span>, keys<span style="color:#006600; font-weight:bold;">&#91;</span> <span style="color:#800000;">2</span> <span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>The audio is straightforward:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-6');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-6">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">audio = document.<span style="color: #006600;">getElementById</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color: #3366CC;">'audio'</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">audio.<span style="color: #006600;">currentTime</span> = keys<span style="color:#006600; font-weight:bold;">&#91;</span> key <span style="color:#006600; font-weight:bold;">&#93;</span> / <span style="color:#800000;">1000</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">audio.<span style="color: #006600;">play</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>Mr. Doob has some notes about performance:</p>
<blockquote><p>It's all software rendering (no WebGL), so don't expect much performance. It's running at 20-30fps on my Mac Mini Core 2 Duo @ 2.53Ghz with Ubuntu/Linux. If you are running MacOS. I'm sorry to say that it won't run faster than 1fps... For some reason Chrome team decided to use CoreGraphics instead of Skia.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/or-so-they-say-by-mr-doob/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML5 Canvas Image Effects: Black &amp; White</title>
		<link>http://ajaxian.com/archives/html5-canvas-image-effects-black-white</link>
		<comments>http://ajaxian.com/archives/html5-canvas-image-effects-black-white#comments</comments>
		<pubDate>Fri, 10 Sep 2010 10:30:57 +0000</pubDate>
		<dc:creator>Brad Neuberg</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=10184</guid>
		<description><![CDATA[Marco Lisci has written a tutorial on creating a black and white image effect using the Canvas tag. The heart of his tutorial is using getImageData() and looping through the red, green, blue, and alpha values of each pixel to change it's luminance: So, what can we use to make an image black and white? <a href="http://ajaxian.com/archives/html5-canvas-image-effects-black-white">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://ajaxian.com/wp-content/images/bw1.png"><img class="aligncenter size-full wp-image-10185" title="bw1" src="http://ajaxian.com/wp-content/images/bw1.png" alt="" width="262" height="323" /></a></p>
<p><a href="http://www.badsharkco.com/marco-lisci/">Marco Lisci</a> <a href="http://spyrestudios.com/html5-canvas-image-effects-black-white/">has written a tutorial</a> on creating a black and white image effect using the Canvas tag.</p>
<p>The heart of his tutorial is using getImageData() and looping through the red, green, blue, and alpha values of each pixel to change it's luminance:</p>
<blockquote><p>So, what can we use to make an image black and white? The luminance. The luminance is how much a color is luminous to the human eye and it’s used to measure the clipping white in video editing systems, for example. In video editing system a white color that “break” the screen is a white that is too white to be represented on a common TV.</p>
<p>This is important because by calculating the average number between red, green and blue values we could obtain a value for every pixel that represent a static mathematical representation of the color luminance.</p>
<p>But there’s a problem, an human eye see colors dynamically. For example, if we take similar shades of blue and green, our eyes will detect the green color more luminous than the blue. In our algorithm we could use the static average formula, but our image will be too flat, and we’ll lose a lot of color depth.</p>
<p>So, let’s introduce the luminance formula: <strong>red x 0.3 + green x 0.59 + blue x 0.11</strong>.</p>
<p>This formula calculates the luminance of a color as it’s perceived by the human eye, so the green channel has more importance than the red and the blue. In our Javascript code we calculate for every pixel the grayscale number, by using exactly this formula. Then we assign this value to the red, green and blue channels of our pixel. By doing this for every pixel we are able to get a black and white version of our original image. There are obviously other more complex methods to calculate the correct grayscale value, but they could be too heavy to be used in an HTML5 canvas element, and we can say that for an everyday use, the luminance algorithm is good.</p></blockquote>
<p>Resulting in code that looks as follows:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-8');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-8">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> imgd = context.<span style="color: #006600;">getImageData</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;">0</span>, <span style="color:#800000;">0</span>, <span style="color:#800000;">500</span>, <span style="color:#800000;">300</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> pix = imgd.<span style="color: #006600;">data</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">for</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i = <span style="color:#800000;">0</span>, n = pix.<span style="color: #006600;">length</span>; i &lt;n; i += <span style="color:#800000;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> grayscale = pix<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> * .<span style="color:#800000;">3</span> + pix<span style="color:#006600; font-weight:bold;">&#91;</span>i+<span style="color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> * .<span style="color:#800000;">59</span> + pix<span style="color:#006600; font-weight:bold;">&#91;</span>i+<span style="color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span> * .<span style="color:#800000;">11</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; pix<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span> = grayscale;&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900; font-style: italic;">// red</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; pix<span style="color:#006600; font-weight:bold;">&#91;</span>i+<span style="color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> = grayscale;&nbsp; <span style="color: #009900; font-style: italic;">// green</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; pix<span style="color:#006600; font-weight:bold;">&#91;</span>i+<span style="color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span> = grayscale;&nbsp; <span style="color: #009900; font-style: italic;">// blue</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900; font-style: italic;">// alpha</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">context.<span style="color: #006600;">putImageData</span><span style="color:#006600; font-weight:bold;">&#40;</span>imgd, <span style="color:#800000;">0</span>, <span style="color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>It would be cool to see this combined as a kind of filter that can be applied to do black and white roll-over effects of elements, similar to what you can do with SVG filters.</p>
<p>[<a href="http://twitter.com/MarkMildner">via Mark Mildner]</p>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/html5-canvas-image-effects-black-white/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Design 3D Models in a Browser</title>
		<link>http://ajaxian.com/archives/design-3d-models-in-a-browser</link>
		<comments>http://ajaxian.com/archives/design-3d-models-in-a-browser#comments</comments>
		<pubDate>Mon, 30 Aug 2010 11:00:37 +0000</pubDate>
		<dc:creator>Brad Neuberg</dc:creator>
				<category><![CDATA[3D]]></category>
		<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=10090</guid>
		<description><![CDATA[Perfect for a Monday is a cool 3D model editor built using the Canvas tag and created by Jayesh Salvi:]]></description>
			<content:encoded><![CDATA[<p>Perfect for a Monday is a <a href="http://www.3dtin.com/">cool 3D model editor built using the Canvas tag</a> and created by <a href="http://www.altcanvas.com/about.html">Jayesh Salvi</a>:</p>
<p style="text-align: center;"><a href="http://ajaxian.com/wp-content/images/3dtin.png"><img class="aligncenter size-full wp-image-10091" title="3dtin" src="http://ajaxian.com/wp-content/images/3dtin.png" alt="" width="407" height="178" /></a></p>
<p><a href="http://ajaxian.com/wp-content/images/3dtin.png"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/design-3d-models-in-a-browser/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>View Source Tutorial: Content Site Using HTML5 Canvas + CSS3</title>
		<link>http://ajaxian.com/archives/view-source-tutorial-content-site-using-html5-canvas-css3</link>
		<comments>http://ajaxian.com/archives/view-source-tutorial-content-site-using-html5-canvas-css3#comments</comments>
		<pubDate>Mon, 30 Aug 2010 10:00:56 +0000</pubDate>
		<dc:creator>Brad Neuberg</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[View Source]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=10022</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p ">Via <a href="http://twitter.com/phil_franks">Phil Franks</a> comes an interesting HTML5/CSS3 site for <a href="http://www.therestudio.com/">There Studio</a>, which is a kind of coworking space in London:<br />
<a href="http://ajaxian.com/wp-content/images/therestudio1.png"><img class="aligncenter size-full wp-image-10023" title="therestudio1" src="http://ajaxian.com/wp-content/images/therestudio1.png" alt="" width="505" height="241" /></a></p>
<p>The site itself has a number of circles with information bouncing on the screen that respond to mouse clicks and moves.</p>
<p>Let's crack the site open using View Source and see how they are doing things. First, they have a repeated background with a little plus symbol with the following style rule on the &lt;body&gt; tag:</p>
<div class="igBar"><a href="javascript:showCodeTxt('css-18');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">CSS:</span>
<div id="css-18">
<div class="css">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">background</span>: #ddd <span style="color: #993333;">url</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #ff0000;">'../images/bg.gif'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#800000;">50</span>% <span style="color:#800000;">0</span> <span style="color: #993333;">repeat</span> <span style="color: #993333;">fixed</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>The textual content in each of the circles is clean semantic HTML that is search engine friendly:</p>
<div class="igBar"><a href="javascript:showCodeTxt('html-19');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">HTML:</span>
<div id="html-19">
<div class="html">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"section who first"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span> </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/h3.html"><span style="color: #000000; font-weight: bold;">&lt;h3&gt;</span></a></span>Who<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h3&gt;</span></span> </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #009900;"><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">&lt;p&gt;</span></a></span>Creatives, makers, thinkers <span style="color: #009900;"><a href="http://december.com/html/4/element/span.html"><span style="color: #000000; font-weight: bold;">&lt;span</span></a> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">"ampersand"</span><span style="color: #000000; font-weight: bold;">&gt;</span></a></span><span style="color: #ddbb00;">&amp;amp;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/span&gt;</span></span> doers<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p&gt;</span></span> </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>To turn that into this:<br />
<center><a href="http://ajaxian.com/wp-content/images/therestudio2.png"><img src="http://ajaxian.com/wp-content/images/therestudio2.png" alt="" title="therestudio2" width="209" height="213" class="aligncenter size-full wp-image-10026" /></a></center></p>
<p>The &lt;h3&gt; is first transformed into having an underline with the padding and margin being on the bottom:</p>
<div class="igBar"><a href="javascript:showCodeTxt('css-20');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">CSS:</span>
<div id="css-20">
<div class="css">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">h3 <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">border-bottom</span>: 1px <span style="color: #993333;">solid</span> #ccc;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">display</span>: <span style="color: #993333;">block</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">font-size</span>: 25px;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">font-weight</span>: <span style="color: #993333;">normal</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">padding</span>: <span style="color:#800000;">0</span> <span style="color:#800000;">0</span> 10px;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">margin</span>: <span style="color:#800000;">0</span> <span style="color:#800000;">0</span> 8px;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p><a href="http://www.therestudio.com/media/js/main.js">JavaScript creates the circle</a>. The script tags themselves are at the end of the HTML page at the bottom of the &lt;body&gt; tag, a good performance practice in general.</p>
<p>The heart of drawing each circle is in the createBall and createContentBall methods. If a ball will have HTML content, then the createContentBall method is used, otherwise the createBall method is used. Let's look at the createContentBall method; we'll break it down:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-21');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-21">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> createContentBall<span style="color:#006600; font-weight:bold;">&#40;</span>className,size,color,html<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> element = document.<span style="color: #006600;">createElement</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color: #3366CC;">'div'</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; element.<span style="color: #006600;">className</span> = className;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; element.<span style="color: #006600;">width</span> = element.<span style="color: #006600;">height</span> = size;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; element.<span style="color: #006600;">style</span>.<span style="color: #006600;">position</span> = <span style="color: #3366CC;">'absolute'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; element.<span style="color: #006600;">style</span>.<span style="color: #006600;">left</span> = -size + <span style="color: #3366CC;">'px'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; element.<span style="color: #006600;">style</span>.<span style="color: #006600;">top</span> = -size + <span style="color: #3366CC;">'px'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; element.<span style="color: #006600;">style</span>.<span style="color: #006600;">cursor</span> = <span style="color: #3366CC;">"default"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; canvas.<span style="color: #006600;">appendChild</span><span style="color:#006600; font-weight:bold;">&#40;</span>element<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; elements.<span style="color: #006600;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span> element <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> circle = document.<span style="color: #006600;">createElement</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color: #3366CC;">'canvas'</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; circle.<span style="color: #006600;">width</span> = size;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; circle.<span style="color: #006600;">height</span> = size;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>className !==<span style="color: #3366CC;">'image'</span> &amp;&amp; className !==<span style="color: #3366CC;">'image first'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> graphics = circle.<span style="color: #006600;">getContext</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color: #3366CC;">'2d'</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; graphics.<span style="color: #006600;">fillStyle</span> = color;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; graphics.<span style="color: #006600;">beginPath</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; graphics.<span style="color: #006600;">arc</span><span style="color:#006600; font-weight:bold;">&#40;</span> size * .<span style="color:#800000;">5</span>, size * .<span style="color:#800000;">5</span>, size * .<span style="color:#800000;">5</span>, <span style="color:#800000;">0</span>, PI2, <span style="color: #003366; font-weight: bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; graphics.<span style="color: #006600;">closePath</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; graphics.<span style="color: #006600;">fill</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; element.<span style="color: #006600;">appendChild</span><span style="color:#006600; font-weight:bold;">&#40;</span> circle <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; content = document.<span style="color: #006600;">createElement</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color: #3366CC;">'div'</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; content.<span style="color: #006600;">className</span> = <span style="color: #3366CC;">"content"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; content.<span style="color: #006600;">onSelectStart</span> = <span style="color: #003366; font-weight: bold;">null</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; content.<span style="color: #006600;">innerHTML</span> = html;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; element.<span style="color: #006600;">appendChild</span><span style="color:#006600; font-weight:bold;">&#40;</span>content<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>className !==<span style="color: #3366CC;">'image'</span> &amp;&amp; className !==<span style="color: #3366CC;">'image first'</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; content.<span style="color: #006600;">style</span>.<span style="color: #006600;">width</span> = <span style="color:#006600; font-weight:bold;">&#40;</span>size - contentPadding*<span style="color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span> + <span style="color: #3366CC;">'px'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; content.<span style="color: #006600;">style</span>.<span style="color: #006600;">left</span> = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>size - content.<span style="color: #006600;">clientWidth</span><span style="color:#006600; font-weight:bold;">&#41;</span> / <span style="color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> +<span style="color: #3366CC;">'px'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; content.<span style="color: #006600;">style</span>.<span style="color: #006600;">top</span> = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>size - content.<span style="color: #006600;">clientHeight</span><span style="color:#006600; font-weight:bold;">&#41;</span> / <span style="color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span> +<span style="color: #3366CC;">'px'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> b2body = <span style="color: #003366; font-weight: bold;">new</span> b2BodyDef<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> circle = <span style="color: #003366; font-weight: bold;">new</span> b2CircleDef<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; circle.<span style="color: #006600;">radius</span> = size / <span style="color:#800000;">2</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; circle.<span style="color: #006600;">density</span> = ballDensity;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; circle.<span style="color: #006600;">friction</span> = ballFriction;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; circle.<span style="color: #006600;">restitution</span> = ballRestitution;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; b2body.<span style="color: #006600;">AddShape</span><span style="color:#006600; font-weight:bold;">&#40;</span>circle<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; b2body.<span style="color: #006600;">userData</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>element: element<span style="color:#006600; font-weight:bold;">&#125;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; b2body.<span style="color: #006600;">position</span>.<span style="color: #006600;">Set</span><span style="color:#006600; font-weight:bold;">&#40;</span> Math.<span style="color: #006600;">random</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> * stage<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>, Math.<span style="color: #006600;">random</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> * <span style="color:#006600; font-weight:bold;">&#40;</span>stage<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>-size<span style="color:#006600; font-weight:bold;">&#41;</span> + size/<span style="color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; b2body.<span style="color: #006600;">linearVelocity</span>.<span style="color: #006600;">Set</span><span style="color:#006600; font-weight:bold;">&#40;</span> Math.<span style="color: #006600;">random</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> * <span style="color:#800000;">200</span>, Math.<span style="color: #006600;">random</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> * <span style="color:#800000;">200</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; bodies.<span style="color: #006600;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span> world.<span style="color: #006600;">CreateBody</span><span style="color:#006600; font-weight:bold;">&#40;</span>b2body<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>First, we create an absolutely positioned DIV that will house our Canvas tag:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-22');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-22">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> element = document.<span style="color: #006600;">createElement</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color: #3366CC;">'div'</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">element.<span style="color: #006600;">className</span> = className;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">element.<span style="color: #006600;">width</span> = element.<span style="color: #006600;">height</span> = size;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">element.<span style="color: #006600;">style</span>.<span style="color: #006600;">position</span> = <span style="color: #3366CC;">'absolute'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">element.<span style="color: #006600;">style</span>.<span style="color: #006600;">left</span> = -size + <span style="color: #3366CC;">'px'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">element.<span style="color: #006600;">style</span>.<span style="color: #006600;">top</span> = -size + <span style="color: #3366CC;">'px'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">element.<span style="color: #006600;">style</span>.<span style="color: #006600;">cursor</span> = <span style="color: #3366CC;">"default"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">canvas.<span style="color: #006600;">appendChild</span><span style="color:#006600; font-weight:bold;">&#40;</span>element<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">elements.<span style="color: #006600;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span> element <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>Then we draw the actual circle itself using the Canvas tag and append it to our container DIV (Note that an SVG circle created programmatically could have also been used here):</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-23');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-23">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> circle = document.<span style="color: #006600;">createElement</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color: #3366CC;">'canvas'</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">circle.<span style="color: #006600;">width</span> = size;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">circle.<span style="color: #006600;">height</span> = size;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>className !==<span style="color: #3366CC;">'image'</span> &amp;&amp; className !==<span style="color: #3366CC;">'image first'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> graphics = circle.<span style="color: #006600;">getContext</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color: #3366CC;">'2d'</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;graphics.<span style="color: #006600;">fillStyle</span> = color;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;graphics.<span style="color: #006600;">beginPath</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;graphics.<span style="color: #006600;">arc</span><span style="color:#006600; font-weight:bold;">&#40;</span> size * .<span style="color:#800000;">5</span>, size * .<span style="color:#800000;">5</span>, size * .<span style="color:#800000;">5</span>, <span style="color:#800000;">0</span>, PI2, <span style="color: #003366; font-weight: bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;graphics.<span style="color: #006600;">closePath</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;graphics.<span style="color: #006600;">fill</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">element.<span style="color: #006600;">appendChild</span><span style="color:#006600; font-weight:bold;">&#40;</span> circle <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>Then we create another DIV to house the HTML content itself:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-24');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-24">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">content = document.<span style="color: #006600;">createElement</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color: #3366CC;">'div'</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">content.<span style="color: #006600;">className</span> = <span style="color: #3366CC;">"content"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">content.<span style="color: #006600;">onSelectStart</span> = <span style="color: #003366; font-weight: bold;">null</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">content.<span style="color: #006600;">innerHTML</span> = html;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">element.<span style="color: #006600;">appendChild</span><span style="color:#006600; font-weight:bold;">&#40;</span>content<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>className !==<span style="color: #3366CC;">'image'</span> &amp;&amp; className !==<span style="color: #3366CC;">'image first'</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;content.<span style="color: #006600;">style</span>.<span style="color: #006600;">width</span> = <span style="color:#006600; font-weight:bold;">&#40;</span>size - contentPadding*<span style="color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span> + <span style="color: #3366CC;">'px'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;content.<span style="color: #006600;">style</span>.<span style="color: #006600;">left</span> = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>size - content.<span style="color: #006600;">clientWidth</span><span style="color:#006600; font-weight:bold;">&#41;</span> / <span style="color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> +<span style="color: #3366CC;">'px'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;content.<span style="color: #006600;">style</span>.<span style="color: #006600;">top</span> = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>size - content.<span style="color: #006600;">clientHeight</span><span style="color:#006600; font-weight:bold;">&#41;</span> / <span style="color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span> +<span style="color: #3366CC;">'px'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>Notice that we are setting content.onSelectStart to null above; this is so that when you run the mouse button over the text it doesn't select (An alternative way to do this is to use the <a href="http://hacks.mozilla.org/2009/12/pointer-events-for-html-in-firefox-3-6/">HTML pointer-events CSS property</a>).</p>
<p>Next comes code to deal with the physics of the circles, which uses <a href="http://box2d-js.sourceforge.net/">Box2D.js</a>, a JavaScript physics engine ported from Flash:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-25');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-25">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> b2body = <span style="color: #003366; font-weight: bold;">new</span> b2BodyDef<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> circle = <span style="color: #003366; font-weight: bold;">new</span> b2CircleDef<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">circle.<span style="color: #006600;">radius</span> = size / <span style="color:#800000;">2</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">circle.<span style="color: #006600;">density</span> = ballDensity;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">circle.<span style="color: #006600;">friction</span> = ballFriction;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">circle.<span style="color: #006600;">restitution</span> = ballRestitution;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">b2body.<span style="color: #006600;">AddShape</span><span style="color:#006600; font-weight:bold;">&#40;</span>circle<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">b2body.<span style="color: #006600;">userData</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>element: element<span style="color:#006600; font-weight:bold;">&#125;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">b2body.<span style="color: #006600;">position</span>.<span style="color: #006600;">Set</span><span style="color:#006600; font-weight:bold;">&#40;</span> Math.<span style="color: #006600;">random</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> * stage<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span>, Math.<span style="color: #006600;">random</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> * <span style="color:#006600; font-weight:bold;">&#40;</span>stage<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span>-size<span style="color:#006600; font-weight:bold;">&#41;</span> + size/<span style="color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">b2body.<span style="color: #006600;">linearVelocity</span>.<span style="color: #006600;">Set</span><span style="color:#006600; font-weight:bold;">&#40;</span> Math.<span style="color: #006600;">random</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> * <span style="color:#800000;">200</span>, Math.<span style="color: #006600;">random</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> * <span style="color:#800000;">200</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">bodies.<span style="color: #006600;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span> world.<span style="color: #006600;">CreateBody</span><span style="color:#006600; font-weight:bold;">&#40;</span>b2body<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>Basically, we define a circle, give it a radius, density, friction, and restitution, and then add it to our collection of shapes with a position and linear velocity. Box2D will then handle the physics and we can just take the values back out to draw things on the screen with a setInterval, which happens in the loop method:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-26');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-26">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> loop<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; delta<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> += <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;">0</span> - delta<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> * .<span style="color:#800000;">5</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; delta<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> += <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;">0</span> - delta<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> * .<span style="color:#800000;">5</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; world.<span style="color: #006600;">m_gravity</span>.<span style="color: #006600;">x</span> = <span style="color:#800000;">0</span> <span style="color: #009900; font-style: italic;">// -(0 + delta[0]);</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; world.<span style="color: #006600;">m_gravity</span>.<span style="color: #006600;">y</span> = -<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;">100</span> + delta<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; mouseDrag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; world.<span style="color: #006600;">Step</span><span style="color:#006600; font-weight:bold;">&#40;</span>timeStep, iterations<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color:#006600; font-weight:bold;">&#40;</span>i = <span style="color:#800000;">0</span>; i &lt;bodies.<span style="color: #006600;">length</span>; i++<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> body = bodies<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> element = elements<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; element.<span style="color: #006600;">style</span>.<span style="color: #006600;">left</span> = <span style="color:#006600; font-weight:bold;">&#40;</span>body.<span style="color: #006600;">m_position0</span>.<span style="color: #006600;">x</span> - <span style="color:#006600; font-weight:bold;">&#40;</span>element.<span style="color: #006600;">width</span>&gt;&gt; <span style="color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> + <span style="color: #3366CC;">'px'</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; element.<span style="color: #006600;">style</span>.<span style="color: #006600;">top</span> = <span style="color:#006600; font-weight:bold;">&#40;</span>body.<span style="color: #006600;">m_position0</span>.<span style="color: #006600;">y</span> - <span style="color:#006600; font-weight:bold;">&#40;</span>element.<span style="color: #006600;">height</span>&gt;&gt; <span style="color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> + <span style="color: #3366CC;">'px'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>ballRotation &amp;&amp; element.<span style="color: #006600;">tagName</span> == <span style="color: #3366CC;">'DIV'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> rotationStyle = <span style="color: #3366CC;">'rotate('</span> + <span style="color:#006600; font-weight:bold;">&#40;</span>body.<span style="color: #006600;">m_rotation0</span> * <span style="color:#800000;">57</span>.<span style="color:#800000;">2957795</span><span style="color:#006600; font-weight:bold;">&#41;</span> + <span style="color: #3366CC;">'deg)'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; element.<span style="color: #006600;">style</span>.<span style="color: #006600;">WebkitTransform</span> = rotationStyle;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; element.<span style="color: #006600;">style</span>.<span style="color: #006600;">MozTransform</span> = rotationStyle;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; element.<span style="color: #006600;">style</span>.<span style="color: #006600;">OTransform</span> = rotationStyle;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>This method gets called with a setInterval periodically. Basically, we apply a delta and a gravity at each time step; see if the mouse is being pressed down (with hooks for touch devices like the iPhone to see if a finger is being pressed down); tell the Box2D World about our gravity and delta and to make an iteration step; and finally use the computed physics values from Box2D to apply CSS3 rotation transforms on our parent DIV and move the element's CSS top and left values around the screen.</p>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/view-source-tutorial-content-site-using-html5-canvas-css3/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Real World Canvas Tips from Hakim El Hattab</title>
		<link>http://ajaxian.com/archives/real-world-canvas-tips</link>
		<comments>http://ajaxian.com/archives/real-world-canvas-tips#comments</comments>
		<pubDate>Wed, 25 Aug 2010 10:00:27 +0000</pubDate>
		<dc:creator>Brad Neuberg</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=9992</guid>
		<description><![CDATA[From Hakim El Hattab (who has some very nifty HTML5 experiments up) comes some nice tips on using the Canvas tag: Cross browser implementation There are no real discrepancies between the canvas outputs of different browsers so long as the JavaScript code is written correctly (if not, browsers tend to try and fix things for <a href="http://ajaxian.com/archives/real-world-canvas-tips">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://ajaxian.com/wp-content/images/hakim_experiment1.png"><img class="aligncenter size-full wp-image-9993" title="hakim_experiment1" src="http://ajaxian.com/wp-content/images/hakim_experiment1.png" alt="" width="425" height="364" /></a></p>
<p>From <a href="http://www.hakim.se/">Hakim El Hattab</a> (who has some very nifty HTML5 <a href="http://hakim.se/experiments/">experiments</a> up) comes some <a href="http://www.kontain.com/fi/entries/94636/">nice tips on using the Canvas tag</a>:</p>
<blockquote><p><span style="font-family: Arial, Verdana, sans-serif; line-height: 20px; font-size: 11.6667px; color: #333333;"> </span></p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;"><strong>Cross browser implementation</strong></p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">There are no real discrepancies between the canvas outputs of different browsers so long as the JavaScript code is written correctly (if not, browsers tend to try and fix things for you, often resulting in varying results).</p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;"><strong>Performance</strong></p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">When working with animation on canvas, performance can be a challenge since bitmap operations are very processing expensive, especially at high resolutions. One important optimization rule to follow is to reuse as many pixels as possible between frames. What I mean by that is the fewer pixels that need to be processed each frame, the faster your program will run. A good example of this is when erasing pixels with the <em>clearRect(x,y,w,h)</em>method, it is very beneficial to clear and redraw only the pixels that have changed and not, for instance, a full screen 1920x1280 sized canvas. Unlike the Flash Player’s redraw regions, this management of “dirty rectangles” needs to be done manually for canvas.</p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;"><strong>State stack &amp; transformation</strong></p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">The canvas can be manipulated via transformations such as rotation and scaling, resulting in a change to the canvas co-ordinate system. This is where it’s important to know about the state stack for which two methods are available: “save” (pushes the current state to the stack) and “restore” (reverts to the previous state). This enables you to apply transformation to a drawing and then restore back to the previous state to make sure the next shape is not affected by any earlier transformation. The states also include properties such as the fill and stroke colors.</p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;"><strong>Compositing</strong></p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">A very powerful tool at hand when working with canvas is compositing modes which, amongst other things, allow for masking and layering. As an example, you can check out <a style="cursor: pointer; outline-style: none; outline-width: initial; outline-color: initial; color: #007bb8;" href="http://hakim.se/experiments/html5/bakemono/01/" target="_blank">Bakemono</a>, where composite modes are used to mask the eye and mouth. There's a wide array of <a style="cursor: pointer; outline-style: none; outline-width: initial; outline-color: initial; color: #007bb8;" href="http://simon.html5.org/dump/html5-canvas-cheat-sheet.html" target="_blank">available composite modes</a> and they are all set through the canvas context's “globalCompositeOperation” property.</p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;"><strong>Anti-aliasing</strong></p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">To allow for sub-pixel drawings, all browser implementations of canvas employ anti-aliasing (although this does not seem to be a requirement in the HTML5 spec). Anti-aliasing can be important to keep in mind if you want to draw crisp lines and notice the result looks blurred. To work around this you will need to either round to integer values or offset by half a pixel depending on if you’re drawing fills or strokes.</p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;"><strong>Clearing the canvas</strong></p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">To clear the entire canvas of any existing pixels you would normally use the <em>clearRect(x,y,w,h) </em>function but there is another option available. Whenever the width/height of the canvas are set, even if they are set to the value they already have, the canvas is reset. This is good to know when working with a dynamically sized canvas as you will notice drawings disappearing.</p>
</blockquote>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">[<a href="http://twitter.com/mrdoob">via Mr. Doob</a>]</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">
<p><span style="font-family: Arial, Verdana, sans-serif; line-height: 20px; font-size: 11.6667px; color: #333333;"> </span></p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;"><strong>Cross browser implementation</strong></p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">There are no real discrepancies between the canvas outputs of different browsers so long as the JavaScript code is written correctly (if not, browsers tend to try and fix things for you, often resulting in varying results).</p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;"><strong>Performance</strong></p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">When working with animation on canvas, performance can be a challenge since bitmap operations are very processing expensive, especially at high resolutions. One important optimization rule to follow is to reuse as many pixels as possible between frames. What I mean by that is the fewer pixels that need to be processed each frame, the faster your program will run. A good example of this is when erasing pixels with the <em>clearRect(x,y,w,h)</em>method, it is very beneficial to clear and redraw only the pixels that have changed and not, for instance, a full screen 1920x1280 sized canvas. Unlike the Flash Player’s redraw regions, this management of “dirty rectangles” needs to be done manually for canvas.</p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;"><strong>State stack &amp; transformation</strong></p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">The canvas can be manipulated via transformations such as rotation and scaling, resulting in a change to the canvas co-ordinate system. This is where it’s important to know about the state stack for which two methods are available: “save” (pushes the current state to the stack) and “restore” (reverts to the previous state). This enables you to apply transformation to a drawing and then restore back to the previous state to make sure the next shape is not affected by any earlier transformation. The states also include properties such as the fill and stroke colors.</p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;"><strong>Compositing</strong></p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">A very powerful tool at hand when working with canvas is compositing modes which, amongst other things, allow for masking and layering. As an example, you can check out <a style="cursor: pointer; outline-style: none; outline-width: initial; outline-color: initial; color: #007bb8;" href="http://hakim.se/experiments/html5/bakemono/01/" target="_blank">Bakemono</a>, where composite modes are used to mask the eye and mouth. There's a wide array of <a style="cursor: pointer; outline-style: none; outline-width: initial; outline-color: initial; color: #007bb8;" href="http://simon.html5.org/dump/html5-canvas-cheat-sheet.html" target="_blank">available composite modes</a> and they are all set through the canvas context's “globalCompositeOperation” property.</p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;"><strong>Anti-aliasing</strong></p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">To allow for sub-pixel drawings, all browser implementations of canvas employ anti-aliasing (although this does not seem to be a requirement in the HTML5 spec). Anti-aliasing can be important to keep in mind if you want to draw crisp lines and notice the result looks blurred. To work around this you will need to either round to integer values or offset by half a pixel depending on if you’re drawing fills or strokes.</p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;"><strong>Clearing the canvas</strong></p>
<p style="color: #333333; font-size: 14px; line-height: 20px; max-width: 640px; overflow-x: hidden; overflow-y: hidden; padding: 0px; margin: 0px;">To clear the entire canvas of any existing pixels you would normally use the <em>clearRect(x,y,w,h) </em>function but there is another option available. Whenever the width/height of the canvas are set, even if they are set to the value they already have, the canvas is reset. This is good to know when working with a dynamically sized canvas as you will notice drawings disappearing.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/real-world-canvas-tips/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Want to pack JS and CSS really well? Convert it to a PNG and unpack it via Canvas</title>
		<link>http://ajaxian.com/archives/want-to-pack-js-and-css-really-well-convert-it-to-a-png-and-unpack-it-via-canvas</link>
		<comments>http://ajaxian.com/archives/want-to-pack-js-and-css-really-well-convert-it-to-a-png-and-unpack-it-via-canvas#comments</comments>
		<pubDate>Sun, 22 Aug 2010 19:30:36 +0000</pubDate>
		<dc:creator>Chris Heilmann</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[buildscript]]></category>
		<category><![CDATA[png]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=10102</guid>
		<description><![CDATA[Jacob Seidelin of nihilogic fame (remember his Super Mario in JavaScript solution) is one of my unsung heroes of JavaScript. His solutions have that Dean Edwards "genius bordering on the bat-sh*t-crazy" touch that make you shake your head in disbelief when they come out but later on become very interesting. One of his posts from <a href="http://ajaxian.com/archives/want-to-pack-js-and-css-really-well-convert-it-to-a-png-and-unpack-it-via-canvas">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<p>Jacob Seidelin of <a href="http://blog.nihilogic.dk/">nihilogic</a> fame (remember his <a href="http://www.nihilogic.dk/labs/mario/mario_small_music.htm">Super Mario in JavaScript</a> solution) is one of my unsung heroes of JavaScript. His solutions have that Dean Edwards "genius bordering on the bat-sh*t-crazy" touch that make you shake your head in disbelief when they come out but later on become very interesting. </p>
<p>One of his <a href="http://blog.nihilogic.dk/2008/05/compression-using-canvas-and-png.html">posts from 2008 entitled "Compression using Canvas and PNG-embedded data"</a> had a good idea: if you want to compress JavaScript and CSS you could <a href="http://dean.edwards.name/packer/">reverse engineer a packing algorithm in JavaScript</a> or you could use a lossless packing system that is already in use and supported in browsers. In this case the packed format is PNG and the way to unpack it is by using the canvas API's getImageData() method:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-28');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-28">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> x = <span style="color: #003366; font-weight: bold;">function</span><span style="color:#006600; font-weight:bold;">&#40;</span>z, m, ix <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color: #009900; font-style: italic;">// image, callback, chunk index</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> o = <span style="color: #003366; font-weight: bold;">new</span> Image<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; o.<span style="color: #006600;">onload</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> s = <span style="color: #3366CC;">""</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; c = d.<span style="color: #006600;">createElement</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #3366CC;">"canvas"</span><span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; t = c.<span style="color: #006600;">getContext</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #3366CC;">"2d"</span><span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; w = o.<span style="color: #006600;">width</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; h = o.<span style="color: #006600;">height</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; c.<span style="color: #006600;">width</span> = c.<span style="color: #006600;">style</span>.<span style="color: #006600;">width</span> = w;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; c.<span style="color: #006600;">height</span> = c.<span style="color: #006600;">style</span>.<span style="color: #006600;">height</span> = h;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; t.<span style="color: #006600;">drawImage</span><span style="color:#006600; font-weight:bold;">&#40;</span>o, <span style="color:#800000;">0</span>, <span style="color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> b = t.<span style="color: #006600;">getImageData</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#800000;">0</span>, <span style="color:#800000;">0</span>, w, h <span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color: #006600;">data</span>; <span style="color: #009900; font-style: italic;">//b : bucket of data</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i= <span style="color:#800000;">0</span>; i &lt;b.<span style="color: #006600;">length</span>; i += <span style="color:#800000;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> b<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>&gt; <span style="color:#800000;">0</span> <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; s += String.<span style="color: #006600;">fromCharCode</span><span style="color:#006600; font-weight:bold;">&#40;</span>b<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; m<span style="color:#006600; font-weight:bold;">&#40;</span>s, ix<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; o.<span style="color: #006600;">src</span> = z;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>As there are quite some <a href="http://10k.aneventapart.com/">interesting</a> <a href="http://js1k.com/home">competitions</a> going on that need really small JavaScript solutions <a href="http://alexle.net/archives/306">Alex Le</a> took up Jacob's work and wrapped it in a <a href="http://gist.github.com/542462">build script that concatenates, packs and converts to a PNG</a> and unpacks it for the 10K competition with a JavaScript. In the process Alex also found some bug in Internet Explorer 9's canvas implementation as it only reads the first 8192 bytes of a PNG and returns 0 for the others :(.</p>
<p>It is pretty amazing how efficient this way of packing is. What we need to test now is when and if it is worth while to have the unpacking done on the client. Imagine adding your JS and CSS to the end of an image and cropping it in CSS to have all the info you need in an app in a single HTTP request. Let the games begin.</p>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/want-to-pack-js-and-css-really-well-convert-it-to-a-png-and-unpack-it-via-canvas/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>JavaScript Gameboy Emulator: Memory and GPU</title>
		<link>http://ajaxian.com/archives/javascript-gameboy-emulator-memory-and-gpu</link>
		<comments>http://ajaxian.com/archives/javascript-gameboy-emulator-memory-and-gpu#comments</comments>
		<pubDate>Wed, 18 Aug 2010 11:00:50 +0000</pubDate>
		<dc:creator>Brad Neuberg</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=10041</guid>
		<description><![CDATA[Jack Vaughn posted on Ajaxian recently about a new blog series on building a Gameboy emulator using JavaScript. Now Parts II and III have been posted in the series: Part 1: The CPU Part 2: Memory Part 3: GPU Timings In the Memory section, Imran Nazar builds up a JavaScript MMU that can interpret the <a href="http://ajaxian.com/archives/javascript-gameboy-emulator-memory-and-gpu">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://ajaxian.com/wp-content/images/jsgb-mmu-map.png"><img class="aligncenter size-full wp-image-10042" title="jsgb-mmu-map" src="http://ajaxian.com/wp-content/images/jsgb-mmu-map.png" alt="" width="476" height="116" /></a></p>
<p><a href="http://ajaxian.com/archives/looking-at-js-emulator-core-for-gameboy">Jack Vaughn posted on Ajaxian recently</a> about a new blog series on building a Gameboy emulator using JavaScript. Now Parts II and III have been posted in the series:</p>
<ul><em></p>
<li><a href="http://imrannazar.com/GameBoy-Emulation-in-JavaScript:-The-CPU">Part 1: The CPU</a></li>
<li><a href="http://imrannazar.com/GameBoy-Emulation-in-JavaScript:-Memory">Part 2: Memory</a></li>
<li><a href="http://imrannazar.com/GameBoy-Emulation-in-JavaScript:-GPU-Timings">Part 3: GPU Timings</a></li>
<p></em></ul>
<p><a href="http://imrannazar.com/GameBoy-Emulation-in-JavaScript:-Memory">In the Memory section</a>, <a href="http://imrannazar.com/">Imran Nazar</a> builds up a JavaScript MMU that can interpret the different parts of the Gamboy's memory:</p>
<blockquote><p>In the previous part of this series, the computer was introduced as a processing unit, which fetches its instructions from memory. In almost every case, a computer's memory is not a simple contiguous region; the GameBoy is no exception in this regard. Since the GameBoy CPU can access 65,536 individual locations on its address bus, a "memory map" can be drawn of all the regions where the CPU has access.</p></blockquote>
<p style="text-align: center;"><a href="http://ajaxian.com/wp-content/images/2324726929_9a5ff4edf9.jpg"><img class="aligncenter size-full wp-image-10043" title="2324726929_9a5ff4edf9" src="http://ajaxian.com/wp-content/images/2324726929_9a5ff4edf9.jpg" alt="" width="226" height="300" /></a></p>
<p style="text-align: center;">[<em><a href="http://www.flickr.com/photos/20179579@N00/2324726929/">CC-A by Michael Ngilen</a></em>]</p>
<p><a href="http://imrannazar.com/GameBoy-Emulation-in-JavaScript:-GPU-Timings">In the third part</a> Imran is now ready to actually draw things to the screen, using the Canvas tag:</p>
<blockquote><p>Nintendo's internal name for the GameBoy is "Dot Matrix Game"; its display is a pixel LCD of dimensions 160x144. If each pixel in the LCD is treated as a pixel in a HTML5 <code>&lt;canvas&gt;</code>, a direct mapping can be made to a canvas of width 160 and height 144. In order to directly address each pixel in the LCD, the contents of the canvas can be manipulated as a "framebuffer": a single block of memory containing the entirety of the canvas, as a series of 4-byte RGBA values.</p></blockquote>
<p>Next in the series will be a blog post on backgrounds and patterns.</p>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/javascript-gameboy-emulator-memory-and-gpu/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Canto.js: An Improved Canvas API</title>
		<link>http://ajaxian.com/archives/canto-js-an-improved-canvas-api</link>
		<comments>http://ajaxian.com/archives/canto-js-an-improved-canvas-api#comments</comments>
		<pubDate>Thu, 29 Jul 2010 04:10:24 +0000</pubDate>
		<dc:creator>Michael Mahemoff</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Library]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=9889</guid>
		<description><![CDATA[Javascript author extraordinaire David Flanagan released Canto.js recently, a lightweight wrapper API for canvas, introduced here and documented at the top of the source code. Example: PLAIN TEXT JAVASCRIPT: &#160; canto&#40;"canvas_id"&#41;.moveTo&#40;100,100&#41;.lineTo&#40;200,200,100,200&#41;.closePath&#40;&#41;.stroke&#40;&#41;; &#160; Notice three things: canto() returns an abstraction of the canvas - a "Canto" object. As with jQuery and similar libraries, there's method chaining; <a href="http://ajaxian.com/archives/canto-js-an-improved-canvas-api">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<p>Javascript author extraordinaire David Flanagan released <a href="http://code.google.com/p/canto-js/">Canto.js</a> recently, a lightweight wrapper API for canvas, <a href="http://www.davidflanagan.com/2010/07/cantojs-an-impr.html">introduced here</a> and documented at <a href="http://code.google.com/p/canto-js/source/browse/trunk/canto.js">the top of the source code</a>. Example:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-31');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-31">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">canto<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #3366CC;">"canvas_id"</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color: #006600;">moveTo</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;">100</span>,<span style="color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color: #006600;">lineTo</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;">200</span>,<span style="color:#800000;">200</span>,<span style="color:#800000;">100</span>,<span style="color:#800000;">200</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color: #006600;">closePath</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color: #006600;">stroke</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>Notice three things:</p>
<ul>
<li> canto() returns an abstraction of the canvas - a "Canto" object.
</li>
<li> As with jQuery and similar libraries, there's method chaining; each method called on a Canto also returns the Canto.
</li>
<li> lineTo() has been extended to support multiple lines being drawn in a single call.
</li>
</ul>
<p>Instead of setting the ink properties and then painting it, you can do it all in one step:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-32');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-32">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">canto<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #3366CC;">"canvas_id"</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color: #006600;">moveTo</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;">100</span>,<span style="color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color: #006600;">lineTo</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;">200</span>,<span style="color:#800000;">200</span>,<span style="color:#800000;">100</span>,<span style="color:#800000;">200</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color: #006600;">closePath</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color: #006600;">stroke</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>lineWidth: <span style="color:#800000;">15</span>, strokeStyle: <span style="color: #3366CC;">"red"</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>And plenty more syntactic sugar - check out the API in the <a href="http://code.google.com/p/canto-js/source/browse/trunk/canto.js">source code comments</a>. Sweet!</p>
<p>Thanks <a href="http://twitter.com/pkeane/statuses/19785917337">@pkeane</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/canto-js-an-improved-canvas-api/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Canvas Color Cycling</title>
		<link>http://ajaxian.com/archives/canvas-color-cycling</link>
		<comments>http://ajaxian.com/archives/canvas-color-cycling#comments</comments>
		<pubDate>Tue, 27 Jul 2010 00:21:55 +0000</pubDate>
		<dc:creator>Michael Mahemoff</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=9881</guid>
		<description><![CDATA[Interest in Canvas, as well as mobile apps, has led to a renaissance of old-school 8-bit graphics. Joe Huckaby of Effect Games has been playing around with color cycling, leading to some stunning effects. Anyone remember Color cycling from the 90s? This was a technology often used in 8-bit video games of the era, to <a href="http://ajaxian.com/archives/canvas-color-cycling">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.effectgames.com/demos/canvascycle/?sound=0"><img src="http://ajaxian.com/wp-content/images/Canvas-Cycle_-True-8-bit-Color-Cycling-with-HTML5.jpg" alt="" title="Canvas Cycle_ True 8-bit Color Cycling with HTML5" width="525" height="421" class="alignnone size-full wp-image-9880" /></a></p>
<p>Interest in Canvas, as well as mobile apps, has led to a renaissance of old-school 8-bit graphics. Joe Huckaby of <a href="http://www.effectgames.com/">Effect Games</a> has been playing around with color cycling, leading to some <a href="http://www.effectgames.com/demos/canvascycle/?sound=0">stunning effects</a>.</p>
<blockquote><p>
Anyone remember <a href="http://en.wikipedia.org/wiki/Color_cycling" class="dx_external_link" target="_blank">Color cycling</a> from the 90s?  This was a technology often used in 8-bit video games of the era, to achieve interesting visual effects by cycling (shifting) the color palette.  Back then video cards could only render 256 colors at a time, so a palette of selected colors was used.  But the programmer could change this palette at will, and all the onscreen colors would instantly change to match.  It was fast, and took virtually no memory.
</p></blockquote>
<p>There's a neat optimization going on here too: instead of clearing and redrawing the entire scene with each frame, he only updates the pixels that change:</p>
<blockquote><p>
In order to achieve fast frame rates in the browser, I had to get a little crazy in the engine implementation. Rendering a 640x480 indexed image on a 32-bit RGB canvas means walking through and drawing 307,200 pixels per frame, in JavaScript. That's a very big array to traverse, and some browsers just couldn't keep up. To overcome this, I pre-process the images when they are first loaded, and grab the pixels that reference colors which are animated (i.e. are part of cycling sets in the palette). Those pixel X/Y offsets are stored in a separate, smaller array, and thus only the pixels that change are refreshed onscreen. This optimization trick works so well, that the thing actually runs at a pretty decent speed on my iPhone 3GS and iPad!
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/canvas-color-cycling/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>IE9 supports Canvas&#8230;. hardware accelerated!</title>
		<link>http://ajaxian.com/archives/ie9-supports-canvas-hardware-accelerated</link>
		<comments>http://ajaxian.com/archives/ie9-supports-canvas-hardware-accelerated#comments</comments>
		<pubDate>Wed, 23 Jun 2010 21:54:00 +0000</pubDate>
		<dc:creator>Dion Almaer</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[IE]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=9702</guid>
		<description><![CDATA[Huge news. My canvas crusade is done. IE9 is supporting canvas, and it is hardware accelerated, in the third preview release: With the third platform preview, we introduce support for the HTML5 Canvas element. As you know our approach for standards support is informed both by developer feedback and real word usage patterns today, along <a href="http://ajaxian.com/archives/ie9-supports-canvas-hardware-accelerated">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<p>Huge news. My canvas crusade is done. <a href="http://blogs.msdn.com/b/ie/archive/2010/06/23/html5-native-third-ie9-platform-preview-available-for-developers.aspx">IE9 is supporting canvas, and it is hardware accelerated</a>, in the third preview release:</p>
<blockquote><p>
With the third platform preview, we introduce support for the HTML5 Canvas element. As you know our approach for standards support is informed both by developer feedback and real word usage patterns today, along with where we see the web heading. Many web developers have asked us to support this part of HTML5 and we definitely took this feedback into account as we prioritized our work.</p>
<p>Like all of the graphics in IE9, canvas is hardware accelerated through Windows and the GPU. Hardware accelerated canvas support in IE9 illustrates the power of native HTML5 in a browser. We’ve rebuilt the browser to use the power of your whole PC to browse the web. These extensive changes to IE9 mean websites can now take advantage of all the hardware innovation in the PC industry.</p>
<p>Preview 3 completes the media landscape for modern websites with hardware accelerated video, audio, and canvas. Developers now have a comprehensive platform to build hardware accelerated HTML5 applications. This is the first browser that uses hardware acceleration for everything on the web page, on by default, available today for developers to start using for their modern site development.</p>
<p><img src="http://ieblog.members.winisp.net/images/Dean_PPB3_3.png" width="460"/></p>
<p>The third platform preview continues to support more of DOM and CSS3 standards that developers want. Examples here include DOM Traversal, full DOM L2 and L3 events, getComputedStyle from DOM Style, CSS3 Values and Units, and CSS3 multiple backgrounds.</p>
<p>Also included in the third platform preview is support for using the Web Open Font Format (WOFF) through CSS3 font face. </p>
<p><img src="http://ieblog.members.winisp.net/images/Dean_PPB3_6-2.png"/>
</p></blockquote>
<p>Oh, and Acid3 is coming along too..... as well as a lot of performance improvements.</p>
<p><img src="http://ieblog.members.winisp.net/images/Dean_PPB3_9.png" width="460"/></p>
<p>Congrats to the IE team.</p>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/ie9-supports-canvas-hardware-accelerated/feed</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Liquid Particles</title>
		<link>http://ajaxian.com/archives/liquid-particles</link>
		<comments>http://ajaxian.com/archives/liquid-particles#comments</comments>
		<pubDate>Fri, 11 Jun 2010 07:58:25 +0000</pubDate>
		<dc:creator>Dion Almaer</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[Fun]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=9557</guid>
		<description><![CDATA[Sit back and enjoy: The bulk of the code is just: PLAIN TEXT JAVASCRIPT: &#160; function run&#40;&#41; &#123; &#160; &#160; &#160; &#160; ctx.globalCompositeOperation = "source-over"; &#160; &#160; &#160; &#160; ctx.fillStyle = "rgba(8,8,12,.65)"; &#160; &#160; &#160; &#160; ctx.fillRect&#40; 0 , 0 , canvasW , canvasH &#41;; &#160; &#160; &#160; &#160; ctx.globalCompositeOperation = "lighter"; &#160; &#160; &#160; <a href="http://ajaxian.com/archives/liquid-particles">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://spielzeugz.de/html5/liquid-particles.html">Sit back and enjoy</a>:</p>
<p><a href="http://spielzeugz.de/html5/liquid-particles.html"><img src="http://ajaxian.com/wp-content/images/liquidparticles.png" alt="" title="liquidparticles" width="1440" height="900" class="alignnone size-full wp-image-9558" /></a></p>
<p>The bulk of the code is just:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-34');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-34">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> run<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; ctx.<span style="color: #006600;">globalCompositeOperation</span> = <span style="color: #3366CC;">"source-over"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; ctx.<span style="color: #006600;">fillStyle</span> = <span style="color: #3366CC;">"rgba(8,8,12,.65)"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; ctx.<span style="color: #006600;">fillRect</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#800000;">0</span> , <span style="color:#800000;">0</span> , canvasW , canvasH <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; ctx.<span style="color: #006600;">globalCompositeOperation</span> = <span style="color: #3366CC;">"lighter"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; mouseVX&nbsp; &nbsp; = mouseX - prevMouseX;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; mouseVY&nbsp; &nbsp; = mouseY - prevMouseY;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; prevMouseX = mouseX;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; prevMouseY = mouseY;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> toDist&nbsp; &nbsp;= canvasW / <span style="color:#800000;">1</span>.<span style="color:#800000;">15</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> stirDist = canvasW / <span style="color:#800000;">8</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> blowDist = canvasW / <span style="color:#800000;">2</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> Mrnd&nbsp; &nbsp;= Math.<span style="color: #006600;">random</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> Mabs&nbsp; &nbsp;= Math.<span style="color: #006600;">abs</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> Msqrt&nbsp; = Math.<span style="color: #006600;">sqrt</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> Mcos&nbsp; &nbsp;= Math.<span style="color: #006600;">cos</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> Msin&nbsp; &nbsp;= Math.<span style="color: #006600;">sin</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> Matan2 = Math.<span style="color: #006600;">atan2</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> Mmax&nbsp; &nbsp;= Math.<span style="color: #006600;">max</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> Mmin&nbsp; &nbsp;= Math.<span style="color: #006600;">min</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> i = numMovers;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span> i-- <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> m&nbsp; = movers<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> x&nbsp; = m.<span style="color: #006600;">x</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> y&nbsp; = m.<span style="color: #006600;">y</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> vX = m.<span style="color: #006600;">vX</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> vY = m.<span style="color: #006600;">vY</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> dX = x - mouseX;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> dY = y - mouseY; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> d = Msqrt<span style="color:#006600; font-weight:bold;">&#40;</span> dX * dX + dY * dY <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> a = Matan2<span style="color:#006600; font-weight:bold;">&#40;</span> dY , dX <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> cosA = Mcos<span style="color:#006600; font-weight:bold;">&#40;</span> a <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> sinA = Msin<span style="color:#006600; font-weight:bold;">&#40;</span> a <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> isMouseDown <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> d &lt;blowDist <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> blowAcc = <span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#800000;">1</span> - <span style="color:#006600; font-weight:bold;">&#40;</span> d / blowDist <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> * <span style="color:#800000;">14</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vX += cosA * blowAcc + .<span style="color:#800000;">5</span> - Mrnd<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vY += sinA * blowAcc + .<span style="color:#800000;">5</span> - Mrnd<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> d &lt;toDist <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> toAcc = <span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#800000;">1</span> - <span style="color:#006600; font-weight:bold;">&#40;</span> d / toDist <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> * canvasW * .<span style="color:#800000;">0014</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vX -= cosA * toAcc;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vY -= sinA * toAcc;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> d &lt;stirDist <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> mAcc = <span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#800000;">1</span> - <span style="color:#006600; font-weight:bold;">&#40;</span> d / stirDist <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> * canvasW * .<span style="color:#800000;">00022</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vX += mouseVX * mAcc;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vY += mouseVY * mAcc;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vX *= friction;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vY *= friction;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> avgVX = Mabs<span style="color:#006600; font-weight:bold;">&#40;</span> vX <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> avgVY = Mabs<span style="color:#006600; font-weight:bold;">&#40;</span> vY <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> avgV = <span style="color:#006600; font-weight:bold;">&#40;</span> avgVX + avgVY <span style="color:#006600; font-weight:bold;">&#41;</span> * .<span style="color:#800000;">5</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> avgVX &lt;.<span style="color:#800000;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span> vX *= Mrnd<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> * <span style="color:#800000;">3</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> avgVY &lt;.<span style="color:#800000;">1</span> <span style="color:#006600; font-weight:bold;">&#41;</span> vY *= Mrnd<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> * <span style="color:#800000;">3</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> sc = avgV * .<span style="color:#800000;">45</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sc = Mmax<span style="color:#006600; font-weight:bold;">&#40;</span> Mmin<span style="color:#006600; font-weight:bold;">&#40;</span> sc , <span style="color:#800000;">3</span>.<span style="color:#800000;">5</span> <span style="color:#006600; font-weight:bold;">&#41;</span> , .<span style="color:#800000;">4</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> nextX = x + vX;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> nextY = y + vY;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> nextX&gt; canvasW <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextX = canvasW;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vX *= -<span style="color:#800000;">1</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> nextX &lt;<span style="color:#800000;">0</span> <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextX = <span style="color:#800000;">0</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vX *= -<span style="color:#800000;">1</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> nextY&gt; canvasH <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextY = canvasH;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vY *= -<span style="color:#800000;">1</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color:#006600; font-weight:bold;">&#40;</span> nextY &lt;<span style="color:#800000;">0</span> <span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextY = <span style="color:#800000;">0</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vY *= -<span style="color:#800000;">1</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.<span style="color: #006600;">vX</span> = vX;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.<span style="color: #006600;">vY</span> = vY;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.<span style="color: #006600;">x</span>&nbsp; = nextX;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; m.<span style="color: #006600;">y</span>&nbsp; = nextY;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.<span style="color: #006600;">fillStyle</span> = m.<span style="color: #006600;">color</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.<span style="color: #006600;">beginPath</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.<span style="color: #006600;">arc</span><span style="color:#006600; font-weight:bold;">&#40;</span> nextX , nextY , sc , <span style="color:#800000;">0</span> , radCirc , <span style="color: #003366; font-weight: bold;">true</span> <span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.<span style="color: #006600;">closePath</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.<span style="color: #006600;">fill</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/liquid-particles/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Canvas optimization tip: Get image data as infrequently as possible</title>
		<link>http://ajaxian.com/archives/canvas-image-data-optimization-tip</link>
		<comments>http://ajaxian.com/archives/canvas-image-data-optimization-tip#comments</comments>
		<pubDate>Thu, 27 May 2010 12:01:46 +0000</pubDate>
		<dc:creator>Dion Almaer</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Tip]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=9372</guid>
		<description><![CDATA[We have learned to touch the DOM as little as possible for performance sakes. Batch up changes, and do one call to innerHTML say. Talk over the evil boundary of the DOM as infrequently as possible. Well, Selim Arsever has found a similar tip for Canvas that caused a ~40% performance improvement on some of <a href="http://ajaxian.com/archives/canvas-image-data-optimization-tip">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<p>We have learned to touch the DOM as little as possible for performance sakes. Batch up changes, and do one call to innerHTML say. Talk over the evil boundary of the DOM as infrequently as possible.</p>
<p>Well, Selim Arsever has <a href="http://www.onaluf.org/en/entry/13">found a similar tip for Canvas</a> that caused a ~40% performance improvement on some of his code. He had an example that did pixel twiddling, looking like:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-37');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-37">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">canvas = document.<span style="color: #006600;">getElementById</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #3366CC;">"canvas"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">context = canvas.<span style="color: #006600;">getContext</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #3366CC;">"2d"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">image = context.<span style="color: #006600;">getImageData</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;">0</span>, <span style="color:#800000;">0</span>, SCREEN_WIDTH, SCREEN_HEIGHT<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> pixels = SCREEN_WIDTH*SCREEN_HEIGHT;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span>--pixels<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;image.<span style="color: #006600;">data</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">4</span>*i+<span style="color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> = r; <span style="color: #009900; font-style: italic;">// Red value</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;image.<span style="color: #006600;">data</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">4</span>*i+<span style="color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> = g; <span style="color: #009900; font-style: italic;">// Green value</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;image.<span style="color: #006600;">data</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">4</span>*i+<span style="color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span> = b; <span style="color: #009900; font-style: italic;">// Blue value</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;image.<span style="color: #006600;">data</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">4</span>*i+<span style="color:#800000;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span> = a; <span style="color: #009900; font-style: italic;">// Alpha value</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">context.<span style="color: #006600;">putImageData</span><span style="color:#006600; font-weight:bold;">&#40;</span>image, <span style="color:#800000;">0</span>, <span style="color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>After listening to Stoyan talk perf, he wondered if there was an issue with the <code>image.data</code> access, and changed the code to:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-38');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-38">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">canvas = document.<span style="color: #006600;">getElementById</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #3366CC;">"canvas"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">context = canvas.<span style="color: #006600;">getContext</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #3366CC;">"2d"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">image = context.<span style="color: #006600;">getImageData</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;">0</span>, <span style="color:#800000;">0</span>, SCREEN_WIDTH, SCREEN_HEIGHT<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> pixels = SCREEN_WIDTH*SCREEN_HEIGHT;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> imageData = image.<span style="color: #006600;">data</span>; <span style="color: #009900; font-style: italic;">// here we detach the pixels array from DOM</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066; font-weight: bold;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span>--pixels<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;imageData<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">4</span>*i+<span style="color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span> = r; <span style="color: #009900; font-style: italic;">// Red value</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;imageData<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">4</span>*i+<span style="color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#93;</span> = g; <span style="color: #009900; font-style: italic;">// Green value</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;imageData<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">4</span>*i+<span style="color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#93;</span> = b; <span style="color: #009900; font-style: italic;">// Blue value</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;imageData<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#800000;">4</span>*i+<span style="color:#800000;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span> = a; <span style="color: #009900; font-style: italic;">// Alpha value</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">image.<span style="color: #006600;">data</span> = imageData; <span style="color: #009900; font-style: italic;">// And here we attache it back </span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">context.<span style="color: #006600;">putImageData</span><span style="color:#006600; font-weight:bold;">&#40;</span>image, <span style="color:#800000;">0</span>, <span style="color:#800000;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>He wrapped this all up in a benchmark that showed the perf diff. It actually really seemed to matter when using closures for namespaces:</p>
<p><img src="http://www.onaluf.org/media/canvas_benchmark.png"/></p>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/canvas-image-data-optimization-tip/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Music score notation in the browser</title>
		<link>http://ajaxian.com/archives/music-score-notation-in-the-browser</link>
		<comments>http://ajaxian.com/archives/music-score-notation-in-the-browser#comments</comments>
		<pubDate>Wed, 12 May 2010 10:11:10 +0000</pubDate>
		<dc:creator>Dion Almaer</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=9264</guid>
		<description><![CDATA[PLAIN TEXT CSS: &#160; score &#123; &#160; title: Hip Tune &#160; artist: Hip Person &#160; &#160; bar &#123; v8 C4 D4 E4 F4 &#40;C4 E4 G4&#41; &#125; &#160; bar &#123; v8 C4 D4 E4 F4 &#40;C4 E4 G4&#41; &#125; repeat 3 &#125; &#160; What if you could write music in a notation like the above? <a href="http://ajaxian.com/archives/music-score-notation-in-the-browser">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<div class="igBar"><a href="javascript:showCodeTxt('css-41');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">CSS:</span>
<div id="css-41">
<div class="css">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">score <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; title: Hip Tune</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; artist: Hip Person</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; bar <span style="color:#006600; font-weight:bold;">&#123;</span> v8 C4 D4 E4 F4 <span style="color:#006600; font-weight:bold;">&#40;</span>C4 E4 G4<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; bar <span style="color:#006600; font-weight:bold;">&#123;</span> v8 C4 D4 E4 F4 <span style="color:#006600; font-weight:bold;">&#40;</span>C4 E4 G4<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color: #993333;">repeat</span> <span style="color:#800000;">3</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>What if you could write music in a notation like the above? And have it render as something like this:</p>
<p><a href="http://0xfe.muthanna.com/jsnotation/demo.html"><img src="http://ajaxian.com/wp-content/images/musicscore.png" alt="" title="musicscore" width="480" height="251" class="alignnone size-full wp-image-9265"/></a></p>
<p>Mohit Muthanna has <a href="http://0xfe.blogspot.com/2010/05/music-notation-with-html5-canvas.html">implemented music notion in Canvas</a>. He has the beginnings of it right now, and still needs to tie in a nice DSL to write it out.</p>
<p>Jono of Mozilla <a href="http://evilbrainjono.net/blog?permalink=541">worked on a simple text based DSL</a> which was nice as you could just tweak text in a textarea and you are done.</p>
<p>The current API looks like this:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-42');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-42">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> VexNotationDemo1<span style="color:#006600; font-weight:bold;">&#40;</span>b<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; b = <span style="color: #003366; font-weight: bold;">new</span> Vex.<span style="color: #006600;">Music</span>.<span style="color: #006600;">Artist</span><span style="color:#006600; font-weight:bold;">&#40;</span>b, <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; scale:<span style="color:#800000;">0</span>.<span style="color:#800000;">9</span>, width:<span style="color:#800000;">900</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> c = b.<span style="color: #006600;">CreateScore</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; d = b.<span style="color: #006600;">CreateScore</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; b.<span style="color: #006600;">DrawScore</span><span style="color:#006600; font-weight:bold;">&#40;</span>c<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> e = GetBar1<span style="color:#006600; font-weight:bold;">&#40;</span>b,c<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; b.<span style="color: #006600;">DrawBar</span><span style="color:#006600; font-weight:bold;">&#40;</span>e<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; e = GetBar2<span style="color:#006600; font-weight:bold;">&#40;</span>b,c<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; b.<span style="color: #006600;">DrawBar</span><span style="color:#006600; font-weight:bold;">&#40;</span>e<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; e = GetBar3<span style="color:#006600; font-weight:bold;">&#40;</span>b,c<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; b.<span style="color: #006600;">DrawBar</span><span style="color:#006600; font-weight:bold;">&#40;</span>e<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; e = GetBar4<span style="color:#006600; font-weight:bold;">&#40;</span>b,c<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; b.<span style="color: #006600;">DrawBar</span><span style="color:#006600; font-weight:bold;">&#40;</span>e<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; c = GetBar5<span style="color:#006600; font-weight:bold;">&#40;</span>b,c<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; b.<span style="color: #006600;">DrawBar</span><span style="color:#006600; font-weight:bold;">&#40;</span>c<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; b.<span style="color: #006600;">DrawScore</span><span style="color:#006600; font-weight:bold;">&#40;</span>d<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; c = b.<span style="color: #006600;">CreateContinuingBarFrom</span><span style="color:#006600; font-weight:bold;">&#40;</span>c,d<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; b.<span style="color: #006600;">DrawBar</span><span style="color:#006600; font-weight:bold;">&#40;</span>c<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; d = GetBar7<span style="color:#006600; font-weight:bold;">&#40;</span>b,d<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; b.<span style="color: #006600;">DrawBar</span><span style="color:#006600; font-weight:bold;">&#40;</span>d<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; </div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">function</span> GetBar4_<span style="color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#40;</span>b,c<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; c = b.<span style="color: #006600;">CreateBar</span><span style="color:#006600; font-weight:bold;">&#40;</span>c<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> d = c.<span style="color: #006600;">AddLine</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; d.<span style="color: #006600;">AddNote</span><span style="color:#006600; font-weight:bold;">&#40;</span>b.<span style="color: #006600;">CreateNote</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>keys:<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color: #3366CC;">"f##/4"</span><span style="color:#006600; font-weight:bold;">&#93;</span>,duration:<span style="color: #3366CC;">"h"</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> e = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #006600;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span>b.<span style="color: #006600;">CreateNote</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>keys:<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color: #3366CC;">"a##/4"</span><span style="color:#006600; font-weight:bold;">&#93;</span>,duration:<span style="color: #3366CC;">"16"</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #006600;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span>b.<span style="color: #006600;">CreateNote</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>keys:<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color: #3366CC;">"f##/5"</span><span style="color:#006600; font-weight:bold;">&#93;</span>,duration:<span style="color: #3366CC;">"16"</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #006600;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span>b.<span style="color: #006600;">CreateNote</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>keys:<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color: #3366CC;">"f##/5"</span><span style="color:#006600; font-weight:bold;">&#93;</span>,duration:<span style="color: #3366CC;">"16"</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #006600;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span>b.<span style="color: #006600;">CreateNote</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>keys:<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color: #3366CC;">"f##/5"</span><span style="color:#006600; font-weight:bold;">&#93;</span>,duration:<span style="color: #3366CC;">"16"</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #006600;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span>b.<span style="color: #006600;">CreateNote</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>keys:<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color: #3366CC;">"f#/4"</span>,<span style="color: #3366CC;">"a/4"</span>,<span style="color: #3366CC;">"f/5"</span><span style="color:#006600; font-weight:bold;">&#93;</span>,duration:<span style="color: #3366CC;">"16"</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #006600;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span>b.<span style="color: #006600;">CreateNote</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>keys:<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color: #3366CC;">"f#/4"</span>,<span style="color: #3366CC;">"a/4"</span>,<span style="color: #3366CC;">"f/5"</span><span style="color:#006600; font-weight:bold;">&#93;</span>,duration:<span style="color: #3366CC;">"16"</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; d.<span style="color: #006600;">AddNotes</span><span style="color:#006600; font-weight:bold;">&#40;</span>e<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; e = b.<span style="color: #006600;">CreateBeam</span><span style="color:#006600; font-weight:bold;">&#40;</span>e<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; d.<span style="color: #006600;">AddBeam</span><span style="color:#006600; font-weight:bold;">&#40;</span>e<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; e = b.<span style="color: #006600;">CreateNote</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>keys:<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color: #3366CC;">"db/4"</span><span style="color:#006600; font-weight:bold;">&#93;</span>,duration:<span style="color: #3366CC;">"32"</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> f = b.<span style="color: #006600;">CreateNote</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>keys:<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color: #3366CC;">"f#/4"</span><span style="color:#006600; font-weight:bold;">&#93;</span>,duration:<span style="color: #3366CC;">"32"</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g = b.<span style="color: #006600;">CreateNote</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>keys:<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color: #3366CC;">"db/4"</span><span style="color:#006600; font-weight:bold;">&#93;</span>,duration:<span style="color: #3366CC;">"32"</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; h = b.<span style="color: #006600;">CreateNote</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>keys:<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color: #3366CC;">"f#/4"</span><span style="color:#006600; font-weight:bold;">&#93;</span>,duration:<span style="color: #3366CC;">"32"</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; d.<span style="color: #006600;">AddNote</span><span style="color:#006600; font-weight:bold;">&#40;</span>e<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; d.<span style="color: #006600;">AddNote</span><span style="color:#006600; font-weight:bold;">&#40;</span>f<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; d.<span style="color: #006600;">AddNote</span><span style="color:#006600; font-weight:bold;">&#40;</span>g<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; d.<span style="color: #006600;">AddNote</span><span style="color:#006600; font-weight:bold;">&#40;</span>h<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; b = b.<span style="color: #006600;">CreateBeam</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>e,f,g,h<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; d.<span style="color: #006600;">AddBeam</span><span style="color:#006600; font-weight:bold;">&#40;</span>b<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> c;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>SVG could be a good choice for this too, with semantics in place. Then it would be cool to have a player, that reads it and pumps out the &lt;audio&gt; :)</p>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/music-score-notation-in-the-browser/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Hummingbird: Real time view of your Web traffic</title>
		<link>http://ajaxian.com/archives/hummingbird-real-time-view-of-your-web-traffic</link>
		<comments>http://ajaxian.com/archives/hummingbird-real-time-view-of-your-web-traffic#comments</comments>
		<pubDate>Mon, 10 May 2010 20:58:31 +0000</pubDate>
		<dc:creator>Dion Almaer</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[Node]]></category>
		<category><![CDATA[Server]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=9249</guid>
		<description><![CDATA[Michael Nutt and Benny Wong have created a fun realtime web analytics product called Hummingbird. It comes with awesome buzzwords too! Node! Canvas! Web Sockets! MongoDB! To add tracking, you simple sprinkle in Gilt.Hummingbird.track(env) like this: Which calls the client: Which, as you can see, sets up a tracking gif on the server. You can <a href="http://ajaxian.com/archives/hummingbird-real-time-view-of-your-web-traffic">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://mnutt.github.com/hummingbird/"><img src="http://mnutt.github.com/hummingbird/images/hummingbird.png" width="480"/></a></p>
<p>Michael Nutt and Benny Wong have created a fun <a href="http://mnutt.github.com/hummingbird/">realtime web analytics product called Hummingbird</a>. It comes with awesome buzzwords too! Node! Canvas! Web Sockets! MongoDB!</p>
<p>To add tracking, you simple sprinkle in <code>Gilt.Hummingbird.track(env)</code> like this:</p>
<p><script src="http://gist.github.com/396581.js?file=sample-hummingbird-track.js"></script></p>
<p>Which calls the client:</p>
<p><script src="http://gist.github.com/396587.js?file=hummingbird-tracker-clientapi.js"></script></p>
<p>Which, as you can see, sets up a tracking gif on the server. You can then access the monitoring server to watch things moving:</p>
<p><object width="480" height="360"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=11613517&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=01AAEA&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=11613517&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=01AAEA&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="480" height="360"></embed></object></p>
<p>Very nice indeed!</p>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/hummingbird-real-time-view-of-your-web-traffic/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Blowing up HTML5 video</title>
		<link>http://ajaxian.com/archives/blowing-up-html5-video</link>
		<comments>http://ajaxian.com/archives/blowing-up-html5-video#comments</comments>
		<pubDate>Thu, 22 Apr 2010 15:59:42 +0000</pubDate>
		<dc:creator>José Jeria</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=9072</guid>
		<description><![CDATA[Sean Christmann has been experimenting with canvas and HTML 5 video. The results? Blowing up a HTML5 video: He considers the implementation to be ”bit of hackish” though and explains why: Don’t ask me why, but copying pixel data out of a video tag is expensive, so expensive that drawing it into a temporary canvas, <a href="http://ajaxian.com/archives/blowing-up-html5-video">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<p>Sean Christmann has been experimenting with canvas and HTML 5 video. The results? Blowing up a HTML5 video:</p>
<p><a href="http://craftymind.com/factory/html5video/CanvasVideo.html"><img class="alignnone size-full wp-image-9074" src="http://ajaxian.com/wp-content/images/html5boom1.jpg" alt="html5boom" width="470" height="284" /></a></p>
<p>He considers the implementation to be ”bit of hackish” though and explains why:</p>
<blockquote><p>
Don’t ask me why, but copying pixel data out of a video tag is expensive, so expensive that drawing it into a temporary canvas, and then drawing pieces of that temp canvas onto a final canvas is faster then just referencing the video tag repeatedly within the same loop. That’s why you’ll see 2 Canvases in the source code for the demos. I’m sure there’s a technical reason for this duplication process, but it’s a lazy reason.
</p></blockquote>
<p>Check out Sean's <a href="http://www.craftymind.com/2010/04/20/blowing-up-html5-video-and-mapping-it-into-3d-space/">blog entry</a> for more details.</p>
<p><em>Editor's Note:</em> This is the first posting from long time Ajaxian community member, José Jeria. We look forward to having him posting as he sees fun things around the Web. Welcome! Here is an introduction by José:</p>
<blockquote><p>
I am a self-taught developer that started doing web development back in 97 in Stockholm Sweden. I worked in different web agencies and ended up in Munich Germany after joining an American company called Razorfish. I worked as a web developer mainly coding HTML / CSS and JavaScript etc for 8 years before I got into developing back-end in Java (J2EE) as well. </p>
<p>The most fun I have though, is when developing rich front-ends though (I am very keen on user interface design). I have been, for the last 5 years, developing intranet web applications in data-warehouse environments using different frameworks, such as JSF, Spring MVC and lately my new favorite: Ext-GWT.
</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/blowing-up-html5-video/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML5 is my Arcade. Akihabara.</title>
		<link>http://ajaxian.com/archives/html5-is-my-arcade-akihabara</link>
		<comments>http://ajaxian.com/archives/html5-is-my-arcade-akihabara#comments</comments>
		<pubDate>Thu, 22 Apr 2010 11:40:49 +0000</pubDate>
		<dc:creator>Dion Almaer</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[Games]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=9070</guid>
		<description><![CDATA[Akihabara is a set of libraries, tools and presets to create pixelated indie-style 8/16-bit era games in Javascript. What does it consist of? Akihabara is composed of a number of libraries that use HTML5 canvas tag and some standard hooks. Gamebox module is complete and compact enough for making games: it allows multiple grouped objects <a href="http://ajaxian.com/archives/html5-is-my-arcade-akihabara">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.kesiev.com/akihabara/sshot/compose.png" style="float:right; padding:8px"/></p>
<p><a href="http://www.kesiev.com/akihabara/">Akihabara</a> is a set of libraries, tools and presets to create pixelated indie-style 8/16-bit era games in Javascript. What does it consist of?</p>
<blockquote>
<p>
Akihabara is composed of a number of libraries that use HTML5 canvas tag and some standard hooks.
</p>
<p>
<b>Gamebox</b> module is complete and compact enough for making games: it allows multiple grouped objects moving simultaneously, it helps with collisions, rendering and moving objects. It provides monospaced pixel-fonts rendering too. It handles keyboard, double buffering and evenutally FSEs. For now do not support audio via the HTML5 audio tag but only because I don't any skill on music/SFX for adding audio to the shipped demos :). I'm going to add this kinda soon.
			</p>
<p>
			<b>Gamecycle</b> module provides a complete generic game cycle: intro, menus, crossfading between stages/lifes, gameover and ending. You can use the same game cycle for many games and implement only the main game.
			</p>
<p>
			<b>Toys</b> module provides <i>lots lots</i> of common routines during the game developing: from effects for screen titles to HUD handling to platform/SHMUP/RPG oriented routines, like jumping characters, Z-Indexed objects, bullets, sparks, staff rolls, bonus screens, dialogues etc.
			</p>
<p>
			<b>Help</b> module provides some Javascript-specific functions, such object copying, randomizing functions, string/array handlers and the <b>akihabaraInit</b> function, that automatically sets a comfortable preset of configurations: 25fps, double-sized display for non-mobile devices, dynamic frameskip.
			</p>
<p>
			<b>Tool</b> module provides simple developing tools. Actually there is only a cel-composer: it can compose an image stacking a set of frames for animating objects, applying a number of filters to each frame.
			</p>
<p>
			<b>Trigo</b> module provides some math stuff for moving objects toward a direction or following a round path.
			</p>
<p>
			<b>Iphopad</b> module provides a touchpad for touch-based device (for now, Android and iDevices).
			</p>
</blockquote>
<p>When you are debugging a game you can simply add some parameters to give you great new info (debug bars, double buffering, touch UI events, set FPS, and much more).</p>
<p>View source on the games to see state machines and info such as:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-44');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-44">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">kill: <span style="color: #003366; font-weight: bold;">function</span><span style="color:#006600; font-weight:bold;">&#40;</span>by<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">frame</span>=<span style="color:#800000;">8</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">accz</span>=-<span style="color:#800000;">8</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; maingame.<span style="color: #006600;">addSmoke</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>,<span style="color: #3366CC;">"flame-red"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">killed</span>=<span style="color: #003366; font-weight: bold;">true</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; maingame.<span style="color: #006600;">playerDied</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#123;</span>wait:<span style="color:#800000;">50</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>Awesome work <a href="http://www.kesiev.com/">Francesco</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/html5-is-my-arcade-akihabara/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>&#8220;Visualize&#8221; data as graphs</title>
		<link>http://ajaxian.com/archives/visualize-data-as-graphs</link>
		<comments>http://ajaxian.com/archives/visualize-data-as-graphs#comments</comments>
		<pubDate>Fri, 16 Apr 2010 14:17:52 +0000</pubDate>
		<dc:creator>Dion Almaer</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=8991</guid>
		<description><![CDATA[How do you visualize data in interesting ways but allow the data to be accessible for all? The jQuery Visualize work is the latest library that groks HTML and replaces the table with pretty graphs: The Visualize plugin parses key content elements in a well-structured HTML table, and leverages that native HTML5 canvas drawing ability <a href="http://ajaxian.com/archives/visualize-data-as-graphs">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<p>How do you visualize data in interesting ways but allow the data to be accessible for all? The <a href="http://www.filamentgroup.com/lab/update_to_jquery_visualize_accessible_charts_with_html5_from_designing_with/">jQuery Visualize</a> work is the latest library that groks HTML and replaces the table with pretty graphs:</p>
<blockquote><p>
The Visualize plugin parses key content elements in a well-structured HTML table, and leverages that native HTML5 canvas drawing ability to transform them into a chart or graph visualization. For example, table row data values serve as chart bars, lines or pie wedges; table headers become value and legend labels; and the title and caption values provide title labels within the image. Visualize also automatically checks for the highest and lowest values in the chart and uses them to calculate x-axis values for line and bar charts. Finally, the plugin includes two different CSS styles — one light and one dark — that can be used as is, or can serve as a starting point to customize chart and graph presentation to match any application style.
</p></blockquote>
<p><img src="http://ajaxian.com/wp-content/images/jvisualize.png" alt="jvisualize" title="jvisualize" width="480" height="619" class="alignnone size-full wp-image-8992" /></p>
<p>Someone pointed out that "accessible" means many things. If you have a 10,000 row table, it may not be easy to consume.... but hey!</p>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/visualize-data-as-graphs/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Move on Objective-C, CS5 to generate Canvas and JS</title>
		<link>http://ajaxian.com/archives/move-on-objective-c-cs5-to-generate-canvas-and-js</link>
		<comments>http://ajaxian.com/archives/move-on-objective-c-cs5-to-generate-canvas-and-js#comments</comments>
		<pubDate>Mon, 12 Apr 2010 10:20:17 +0000</pubDate>
		<dc:creator>Dion Almaer</dc:creator>
				<category><![CDATA[Adobe]]></category>
		<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=8955</guid>
		<description><![CDATA[We have been reminded of MAX showing Flash CS5 generating basic HTML5 code (Canvas/JS). That's right. Adobe is a tools company. Although it helps to own the platform (hence keeping Flash alive and wanting it to win) as the competition to build the best tools is a given, that doesn't mean that they can't compete <a href="http://ajaxian.com/archives/move-on-objective-c-cs5-to-generate-canvas-and-js">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<p><object width="480" height="289"><param name="movie" value="http://www.youtube.com/v/v69S22ZBBqA&#038;color1=0xb1b1b1&#038;color2=0xcfcfcf&#038;hl=en_US&#038;feature=player_embedded&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="http://www.youtube.com/v/v69S22ZBBqA&#038;color1=0xb1b1b1&#038;color2=0xcfcfcf&#038;hl=en_US&#038;feature=player_embedded&#038;fs=1" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="480" height="289"></embed></object></p>
<p>We have been reminded of MAX showing <a href="http://www.9to5mac.com/Flash-html5-canvas-35409730">Flash CS5 generating basic HTML5 code (Canvas/JS)</a>.</p>
<p>That's right. Adobe is a tools company. Although it helps to own the platform (hence keeping Flash alive and wanting it to win) as the competition to build the best tools is a given, that doesn't mean that they can't compete on other platforms. To be frank, it isn't like the Web has a ton of amazing tools to compete against anyway in fact, so I fully hope that with the success of HTML5 in the coming years, we see great tools from Adobe to help us deliver experiences. Especially for creative designer types... Adobe really gets those chaps (and chapesses).</p>
<p>What tools would you like to see from Adobe? Maybe we can get them to dust off their SVG? ;)</p>
<p><a href="http://www.adobe.com/products/creativesuite/web/">Check out the launch of Creative Suite 5</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/move-on-objective-c-cs5-to-generate-canvas-and-js/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Open Web Video Puzzle</title>
		<link>http://ajaxian.com/archives/open-web-video-puzzle</link>
		<comments>http://ajaxian.com/archives/open-web-video-puzzle#comments</comments>
		<pubDate>Thu, 01 Apr 2010 11:10:37 +0000</pubDate>
		<dc:creator>Dion Almaer</dc:creator>
				<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Front Page]]></category>
		<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://ajaxian.com/?p=8808</guid>
		<description><![CDATA[This is a fun one. The Open Video Sliding Puzzle takes a running video (from the public videos project) cuts it up, and builds a sliding puzzle on the fly. How does it work? Why Canvas and HTML5 video of course: PLAIN TEXT JAVASCRIPT: &#160; &#160; &#160; for &#40;var i=0;i&#38;lt;12;i++&#41;&#123; &#160; &#160; &#160; &#160; var <a href="http://ajaxian.com/archives/open-web-video-puzzle">Read the rest...</a>]]></description>
			<content:encoded><![CDATA[<p>This is a fun one. The <a href="http://fczbrhackday.appspot.com/">Open Video Sliding Puzzle</a> takes a running video (from the <a href="http://alpha.publicvideos.org">public videos project</a>) cuts it up, and builds a sliding puzzle on the fly.</p>
<p><img src="http://ajaxian.com/wp-content/images/openvideoslider.png" alt="openvideoslider" title="openvideoslider" width="480" height="274" class="alignnone size-full wp-image-8809"/></p>
<p>How does it work? Why Canvas and HTML5 video of course:</p>
<div class="igBar"><a href="javascript:showCodeTxt('javascript-46');">PLAIN TEXT</a></div>
<div class="syntax_hilite"><span style="color:#000000; font-weight:bold;">JAVASCRIPT:</span>
<div id="javascript-46">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i=<span style="color:#800000;">0</span>;i&amp;lt;<span style="color:#800000;">12</span>;i++<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> can = document.<span style="color: #006600;">getElementById</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #3366CC;">'c'</span>+<span style="color:#006600; font-weight:bold;">&#40;</span>i+<span style="color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> ctx = can.<span style="color: #006600;">getContext</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; pieces_can.<span style="color: #006600;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span>can<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; pieces_ctx.<span style="color: #006600;">push</span><span style="color:#006600; font-weight:bold;">&#40;</span>ctx<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; i = setInterval<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> sWidth = dWidth = <span style="color:#800000;">160</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> sHeight = dHeight = <span style="color:#800000;">120</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; dx = dy = <span style="color:#800000;">0</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> source = document.<span style="color: #006600;">getElementById</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #3366CC;">'puzzle_image'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">for</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i=<span style="color:#800000;">0</span>;i&amp;lt;<span style="color:#800000;">12</span>;i++<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sx = <span style="color:#006600; font-weight:bold;">&#40;</span>i*sWidth<span style="color:#006600; font-weight:bold;">&#41;</span>%<span style="color:#800000;">640</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sy = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>Math.<span style="color: #006600;">floor</span><span style="color:#006600; font-weight:bold;">&#40;</span>i/<span style="color:#800000;">4</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>*sHeight<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pieces_ctx<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color: #006600;">drawImage</span><span style="color:#006600; font-weight:bold;">&#40;</span>source, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#800000;">30</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="color:#000000; font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
</ol>
</div>
</div>
</div>
<p>The bulk of <a href="http://github.com/fczuardi/openvideopuzzle">the code</a> is setting up and playing the puzzle.</p>
]]></content:encoded>
			<wfw:commentRss>http://ajaxian.com/archives/open-web-video-puzzle/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

