<?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>Joe Longstreet</title>
	<atom:link href="http://joelongstreet.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://joelongstreet.com/blog</link>
	<description>Thoughts on Front-End Development, Mostly JavaScript</description>
	<lastBuildDate>Mon, 15 Aug 2011 18:04:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Understanding How the Accelerometer and Gyroscope Work in the Browser</title>
		<link>http://joelongstreet.com/blog/2011/08/15/understanding-how-the-accelerometer-and-gyroscope-work-in-the-browser/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=understanding-how-the-accelerometer-and-gyroscope-work-in-the-browser</link>
		<comments>http://joelongstreet.com/blog/2011/08/15/understanding-how-the-accelerometer-and-gyroscope-work-in-the-browser/#comments</comments>
		<pubDate>Mon, 15 Aug 2011 17:43:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Accelerometer]]></category>
		<category><![CDATA[Gyroscope]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Touch/Mobile]]></category>
		<category><![CDATA[Zepto]]></category>
		<category><![CDATA[accelerometer]]></category>
		<category><![CDATA[gyroscope]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://joelongstreet.com/blog/?p=14</guid>
		<description><![CDATA[Final Product (must have iOS 4.0 or greater to view): http://joelongstreet.com/blog_files/ios_accelerometer_1/index.html I&#8217;ll be covering the basics of accessing the accelerometer and gyroscope with JavaScript in the post below. We&#8217;ll discuss difference between the two technologies and write a simple example &#8230; <a href="http://joelongstreet.com/blog/2011/08/15/understanding-how-the-accelerometer-and-gyroscope-work-in-the-browser/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Final Product (must have iOS 4.0 or greater to view): <a href="http://joelongstreet.com/blog_files/ios_accelerometer_1/index.html">http://joelongstreet.com/blog_files/ios_accelerometer_1/index.html</a></h2>
<p>I&#8217;ll be covering the basics of accessing the accelerometer and gyroscope with JavaScript in the post below. We&#8217;ll discuss difference between the two technologies and write a simple example to log out information when the device has detected a change in orientation. To simplify DOM manipulation, the code will depend on either <a href='http://zeptojs.com' target='_blank'>Zepto</a> or <a href='http://jquery.com' target='_blank'>jQuery</a> (I highly recommend <a href='http://zeptojs.com' target='_blank'>Zepto</a> when working in mobile).</p>
<p>In iOS 4.0 and above, Apple has allowed the web browser (via JavaScript) to access data returned from both the accelerometer and gyroscope. Supported devices include (only if the operating system is 4.0 or greater):</p>
<ul>
<li>Accelerometer &#8211; iPhone 4, iPad, iPad2, iPod Touch</li>
<li>Gyroscope &#8211; iPhone 4, iPad2, iPod Touch</li>
</ul>
<p>Unfortunately, Android devices do not currently support the use of the accelerometer or gyroscope in the web browser. You&#8217;ll have to stick to the rudimentary &#8216;onorientationchange&#8217; event. If you&#8217;d like to read more about this event, the <a href='http://developer.apple.com/library/iOS/#documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html' target='_blank'>Safari Developer Library</a> has several good explanations.</p>
<h3>What&#8217;s the Difference between the Accelerometer and the Gyroscope?</h3>
<div id="attachment_15" class="wp-caption alignnone" style="width: 663px"><a href="http://joelongstreet.com/blog/wp-content/uploads/2011/08/accelerometer_gyroscope.png"><img src="http://joelongstreet.com/blog/wp-content/uploads/2011/08/accelerometer_gyroscope.png" alt="Accelerometer vs. Gyroscope Visually Explained" title="Accelerometer and Gyroscope - X,Y,X and Alpha, Beta, Gamma Channels" width="653" height="735" class="size-full wp-image-15" /></a><p class="wp-caption-text">Accelerometer (x, y, z) and Gyroscope (a, b, g)</p></div>
<p>The accelerometer measures orientation in the x, y, and z dimensions. If we rotate the device left or right (think of steering a car), the x and y values are changing. If we let the top of the device fall towards or away from us, the z dimension is changing.</p>
<p>The gyroscope measures forces <em>relative to the original position of the device</em>. Unlike the accelerometer, all of the channels of the gyroscope will start at 0 upon initialization and will return values based on the changed orientation. Moving the device vertically through space (think of doing squats) changes alpha. Twisting the device (ringint out a rag) changes the beta. Bringing the device towards or away from you (think of face punching), effects gamma.</p>
<p>By combining the 6 forces of the gyroscope and accelerometer we can summon Captain Planet and find the exact orientation of the device, <em>but only relative to it&#8217;s starting position</em>.</p>
<h3>The Code</h3>
<p>I&#8217;m starting with an entirely blank html file. The only things we&#8217;ll need to include are a reference to <a href='http://zeptojs.com' target='_blank'>Zepto</a> and a series of DOM elements to hold the textual information we&#8217;ll be logging out. You can download the final zip package here &#8211; <a href="http://joelongstreet.com/blog_files/ios_accelerometer_1/package.zip">http://joelongstreet.com/blog_files/ios_accelerometer_1/package.zip</a>.</p>
<p>All I&#8217;m doing here is associating DOM elements with JavaScript variables. Nothing special.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> x_dom <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.x'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> y_dom <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.y'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> z_dom <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.z'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> a_dom <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> b_dom <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.b'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> g_dom <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.g'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p><strong>Accelerometer</strong> &#8211; The &#8220;ondevicemotion&#8221; event is native to browsers that support the accelerometer. We can use this event to constantly track the changes in motion related to the x, y, and z dimensions. After collecting the data from the event, we write the information to the DOM.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">window.<span style="color: #660066;">ondevicemotion</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> event.<span style="color: #660066;">accelerationIncludingGravity</span>.<span style="color: #660066;">x</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> event.<span style="color: #660066;">accelerationIncludingGravity</span>.<span style="color: #660066;">y</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> z <span style="color: #339933;">=</span> event.<span style="color: #660066;">accelerationIncludingGravity</span>.<span style="color: #660066;">z</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;x_dom.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;y_dom.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;z_dom.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>z<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<div class='trap'>
<div class='title'>Caution!</div>
<p>In the example above, the use of the word acceleration is misleading. Acceleration by definition is the rate of change of speed. This may lead you to believe that the returned values are dependent on how fast you&#8217;re moving the device, this is not the case. The values are constant and are entirely dependent on the devices position, not the acceleration required to achieve that position.</p>
</div>
<p><strong>Gyroscope</strong> &#8211; We&#8217;re doing the same thing here with the gyroscope. Like the accelerometer, this method is native to devices that support the respective instrument.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">window.<span style="color: #660066;">ondeviceorientation</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> event.<span style="color: #660066;">alpha</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> event.<span style="color: #660066;">beta</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> g <span style="color: #339933;">=</span> event.<span style="color: #660066;">gamma</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;a_dom.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;b_dom.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;g_dom.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>g<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>If you log the above code, you&#8217;ll notice the instruments are very, very sensitive and the strings returned are somewhat long. Since it&#8217;s kind of difficult to look at that kind of information in the DOM, I&#8217;ve added a rounding function to display a single whole number. Also adding a + sign to the string makes switches between positive and negative numbers less visually striking.</p>
<p>The Final Product:</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> x_dom <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.x'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> y_dom <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.y'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> z_dom <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.z'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> a_dom <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> b_dom <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.b'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> g_dom <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.g'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">//This is only for devices that support the use of a gyroscope (iPhone 4, iPad2, iPod Touch)</span><br />
window.<span style="color: #660066;">ondeviceorientation</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">alpha</span><span style="color: #339933;">*</span><span style="color: #CC0000;">1</span><span style="color: #339933;">/</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">beta</span><span style="color: #339933;">*</span><span style="color: #CC0000;">1</span><span style="color: #339933;">/</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> g <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">gamma</span><span style="color: #339933;">*</span><span style="color: #CC0000;">1</span><span style="color: #339933;">/</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>a <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> a <span style="color: #339933;">=</span> <span style="color: #3366CC;">'+'</span> <span style="color: #339933;">+</span> a<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>b <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> b <span style="color: #339933;">=</span> <span style="color: #3366CC;">'+'</span> <span style="color: #339933;">+</span> b<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>g <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> g <span style="color: #339933;">=</span> <span style="color: #3366CC;">'+'</span> <span style="color: #339933;">+</span> g<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;a_dom.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;b_dom.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;g_dom.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>g<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
window.<span style="color: #660066;">ondevicemotion</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">accelerationIncludingGravity</span>.<span style="color: #660066;">x</span><span style="color: #339933;">*</span><span style="color: #CC0000;">1</span><span style="color: #339933;">/</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">accelerationIncludingGravity</span>.<span style="color: #660066;">y</span><span style="color: #339933;">*</span><span style="color: #CC0000;">1</span><span style="color: #339933;">/</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> z <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">accelerationIncludingGravity</span>.<span style="color: #660066;">z</span><span style="color: #339933;">*</span><span style="color: #CC0000;">1</span><span style="color: #339933;">/</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> x <span style="color: #339933;">=</span> <span style="color: #3366CC;">'+'</span> <span style="color: #339933;">+</span> x<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>y <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> y <span style="color: #339933;">=</span> <span style="color: #3366CC;">'+'</span> <span style="color: #339933;">+</span> y<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>z <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> z <span style="color: #339933;">=</span> <span style="color: #3366CC;">'+'</span> <span style="color: #339933;">+</span> z<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;x_dom.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;y_dom.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;z_dom.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>z<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Finished, not a whole lot to it and pretty easy overall. I&#8217;m really looking forward to seeing how people use these new technologies to build web applications. <em>Note:</em> In real life scenarios it&#8217;s probably overkill to collect information on device motion and orientation change. </p>
<h3>Downloads</h3>
<ul>
<li>Final Product: <a href="http://joelongstreet.com/blog_files/ios_accelerometer_1/index.html">http://joelongstreet.com/blog_files/ios_accelerometer_1/index.html</a>. </li>
<li>Download the zip package : <a href="http://joelongstreet.com/blog_files/ios_accelerometer_1/package.zip">http://joelongstreet.com/blog_files/ios_accelerometer_1/package.zip</a>.</li>
</ul>
<p>Further Reading:</p>
<ul>
<li><a href='https://developer.apple.com/library/safari/#documentation/SafariDOMAdditions/Reference/DeviceMotionEventClassRef/DeviceMotionEvent/DeviceMotionEvent.html' target='_blank'>Safari Web Reference &#8211; Device Motion Event</a></li>
<li><a href='https://developer.apple.com/library/safari/#documentation/SafariDOMAdditions/Reference/DeviceOrientationEventClassRef/DeviceOrientationEvent/DeviceOrientationEvent.html' target='_blank'>Safari Web Reference &#8211; Device Orientation Event</a></li>
<li><a href='http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MotionEvents/MotionEvents.html' target='_blank'>iOS Event Handling Reference</a></li>
<li><a href='htp://zeptojs.com' target='_blank'>Zepto</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://joelongstreet.com/blog/2011/08/15/understanding-how-the-accelerometer-and-gyroscope-work-in-the-browser/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Webkit + JavaScript Particle Effect</title>
		<link>http://joelongstreet.com/blog/2011/08/02/webkit-javascript-particle-effect/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=webkit-javascript-particle-effect</link>
		<comments>http://joelongstreet.com/blog/2011/08/02/webkit-javascript-particle-effect/#comments</comments>
		<pubDate>Tue, 02 Aug 2011 05:45:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Touch/Mobile]]></category>
		<category><![CDATA[Webkit Animation]]></category>
		<category><![CDATA[Zepto]]></category>
		<category><![CDATA[CSS Effects]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Particle Effect]]></category>
		<category><![CDATA[Particle generator]]></category>
		<category><![CDATA[Particles]]></category>
		<category><![CDATA[Webkit]]></category>
		<category><![CDATA[Zepto vs. jQuery]]></category>

		<guid isPermaLink="false">http://joelongstreet.com/blog/?p=1</guid>
		<description><![CDATA[Final Product: http://joelongstreet.com/blog_files/basic_particles This post will demonstrate how to create a basic particle effect using CSS webkit animations and javascript. We&#8217;ll be using Zepto.js as our JavaScript framework to select and create DOM elements. If you&#8217;ve never used the library &#8230; <a href="http://joelongstreet.com/blog/2011/08/02/webkit-javascript-particle-effect/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Final Product: <a href='http://joelongstreet.com/blog_files/basic_particles'>http://joelongstreet.com/blog_files/basic_particles</a></h2>
<p>This post will demonstrate how to create a basic particle effect using CSS webkit animations and javascript. We&#8217;ll be using <a target='_blank' href='http://zeptojs.com'>Zepto.js</a> as our JavaScript framework to select and create DOM elements. If you&#8217;ve never used the library before, don&#8217;t worry. It&#8217;s syntax is identical to jQuery: the only difference is a smaller footprint and a far less robust API. If you&#8217;d like, you can also use the jQuery javascript framework.</p>
<p>You can see the final product here: <a href='http://joelongstreet.com/blog_files/basic_particles'>http://joelongstreet.com/blog_files/basic_particles</a>. You can download a zip package from here: <a href='http://joelongstreet.com/blog_files/basic_particles/package.zip'>http://joelongstreet.com/blog_files/basic_particles/package.zip</a>.</p>
<p>No markup is provided because all of the HTML in this tutorial is generated via JavaScript. The CSS has one major class which sets a few properties &#8211; border-radius (make the element appear as a circle), position (gives us the ability to freely move the element around the stage), and a webkit transition property.</p>
<p>In reference to the webkit transition property, &#8220;All&#8221; refers to which attribute to manipulate (ex. top, left, width, height, etc.), .75 is the time the transition should take in seconds, and the third parameter is an easing function. You can read more about CSS transition properties at <a target='_blank' href='http://www.webkit.org/blog/138/css-animation/'>webkit.org</a>. The rest of the classes specify colors for each particle.</p>
<div class="codecolorer-container css railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6666ff;">.particle</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">z-index</span><span style="color: #00AA00;">:</span><span style="color: #cc66cc;">99</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">absolute</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span><span style="color: #933;">15px</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span><span style="color: #933;">15px</span><span style="color: #00AA00;">;</span> -webkit-border-radius<span style="color: #00AA00;">:</span><span style="color: #933;">15px</span><span style="color: #00AA00;">;</span> -webkit-transition<span style="color: #3333ff;">:all </span>.75s ease-out<span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #6666ff;">.particle</span>.<span style="color: #993333;">purple</span> &nbsp;<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span>rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">108</span><span style="color: #00AA00;">,</span> 000<span style="color: #00AA00;">,</span> 072<span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #6666ff;">.particle</span>.<span style="color: #993333;">green</span>&nbsp; &nbsp;<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span>rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">189</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">190</span><span style="color: #00AA00;">,</span> 000<span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #6666ff;">.particle</span>.<span style="color: #993333;">yellow</span> &nbsp;<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span>rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">246</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">197</span><span style="color: #00AA00;">,</span> 000<span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #6666ff;">.particle</span><span style="color: #6666ff;">.orange</span> &nbsp;<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span>rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">221</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">137</span><span style="color: #00AA00;">,</span> 000<span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #6666ff;">.particle</span>.<span style="color: #993333;">red</span> &nbsp;&nbsp; &nbsp;<span style="color: #00AA00;">&#123;</span><span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span>rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">189</span><span style="color: #00AA00;">,</span> 058<span style="color: #00AA00;">,</span> 000<span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>I&#8217;ll be using random numbers frequently throughout this tutorial so let&#8217;s take advantage of a handy little function. If you pass two numbers into the function below, it will return a number within the range of the two numbers you specified. Neat, handy.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> random_from_to<span style="color: #009900;">&#40;</span>from<span style="color: #339933;">,</span> to<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">return</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>to <span style="color: #339933;">-</span> from <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> from<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>When generating particles, we know that they need to actually come from somewhere (some x and y location). Let&#8217;s start by capturing either a touch or click event and passing the corresponding x and y coordinates into a new particle.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">//Mouse Events</span><br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> e.<span style="color: #660066;">pageX</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> e.<span style="color: #660066;">pageY</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> particle <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Particles<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">//or Touch Events if you like</span><br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'touchstart'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> e.<span style="color: #660066;">targetTouches</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">pageX</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> e.<span style="color: #660066;">targetTouches</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">pageY</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> particle <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Particles<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The particle class doesn&#8217;t exist yet, so we&#8217;ll need to create it. We know we want the effect to use multiple colors, so let&#8217;s add that into the particle definition.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> Particles<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">//this will generate a random color for us whenever it's called</span><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">//you could use as many colors as you like</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">gen_color</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> rand <span style="color: #339933;">=</span> random_from_to<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>rand<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rand <span style="color: #339933;">=</span> <span style="color: #3366CC;">'purple'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rand <span style="color: #339933;">=</span> <span style="color: #3366CC;">'green'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rand <span style="color: #339933;">=</span> <span style="color: #3366CC;">'yellow'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rand <span style="color: #339933;">=</span> <span style="color: #3366CC;">'orange'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rand <span style="color: #339933;">=</span> <span style="color: #3366CC;">'red'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> rand<span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">//Our HTML template will look something like this when rendered- &lt;div class=&quot;particle green&quot; style=&quot;top:241px; left:239px;&quot;&gt;&lt;/div&gt;</span><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">//The x and y vlues come from our mouse/touch event and the generated color comes from the method above.</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> html_template <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;div class=&quot;particle '</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">gen_color</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot; style=&quot;top:'</span> <span style="color: #339933;">+</span> y <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px; left:'</span> <span style="color: #339933;">+</span> x <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px;&quot;&gt;&lt;/div&gt;'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">//Put it on screen</span><br />
&nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>html_template<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>We want more than one particle. We can specify a number, or generate a random one. Let&#8217;s do random for a more realistic effect. After we know how many particles we need to create, we then need to append each one to the stage. Move your html template inside of a for loop. Remember, the more particles we generate the slower our document will render.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> num_particles <span style="color: #339933;">=</span> random_from_to<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>num_particles<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> html_template <span style="color: #339933;">=</span> <span style="color: #3366CC;">'&lt;div class=&quot;particle '</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">gen_color</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot; style=&quot;top:'</span> <span style="color: #339933;">+</span> y <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px; left:'</span> <span style="color: #339933;">+</span> x <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px;&quot;&gt;&lt;/div&gt;'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>html_template<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>It works, but it&#8217;s difficult too tell where each particle is since they&#8217;re all overlapping. To animate the particle, we can iterate over each particle element on the stage and apply a new random x and y position. <em>If we immediately try to apply the new CSS properties to the particle element, we won&#8217;t see any animation.</em> This is because the DOM has not yet recognized the css properties of our newly added element. We can force it to update with a setTimeout, 0 milliseconds required.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.particle'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> new_x <span style="color: #339933;">=</span> random_from_to<span style="color: #009900;">&#40;</span>x <span style="color: #339933;">-</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> x <span style="color: #339933;">+</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> new_y <span style="color: #339933;">=</span> random_from_to<span style="color: #009900;">&#40;</span>y <span style="color: #339933;">-</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> y <span style="color: #339933;">+</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//but $(this) won't work...</span><br />
&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'left'</span><span style="color: #339933;">:</span>new_x<span style="color: #339933;">,</span> <span style="color: #3366CC;">'top'</span><span style="color: #339933;">:</span>new_y<span style="color: #339933;">,</span> <span style="color: #3366CC;">'opacity'</span><span style="color: #339933;">:</span><span style="color: #CC0000;">0</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>The placement of the setTimeout function interferes with our reference to the word $(this). In this instance, $(this) is actually referring to some fictional element within the timeout, we want it to refer to the DOM element. So let&#8217;s add a reference. I like to use the word self.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.particle'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> new_x <span style="color: #339933;">=</span> random_from_to<span style="color: #009900;">&#40;</span>x <span style="color: #339933;">-</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span> x <span style="color: #339933;">+</span> <span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> new_y <span style="color: #339933;">=</span> random_from_to<span style="color: #009900;">&#40;</span>y <span style="color: #339933;">-</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span> y <span style="color: #339933;">+</span> <span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; self.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'left'</span><span style="color: #339933;">:</span>new_x<span style="color: #339933;">,</span> <span style="color: #3366CC;">'top'</span><span style="color: #339933;">:</span>new_y<span style="color: #339933;">,</span> <span style="color: #3366CC;">'opacity'</span><span style="color: #339933;">:</span><span style="color: #CC0000;">0</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>If we&#8217;ve done everything correctly up to this point the basic effect should be present, but there is a problem. If you look at the DOM inspector in your browser you&#8217;ll notice that the particle elements can really start to build up. If you play with or document for a bit, you can build up hundreds or thousands of unused particles. We&#8217;ll need to remove those unused elements from the stage.</p>
<p>The easiest way is to set a timeout to remove everything with a class of particle.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.particle'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>This is fine, but it can take away from the experience when we want to generate several sets of particles before removing other particles. To accomplish this, we need some kind of unique identifier for each set of particles we generate. We could use a random number, or even better, a javascript date object. Since the date object will return a number in milliseconds, it&#8217;s plenty accurate for our purposes.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> uid <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>num_particles<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #006600; font-style: italic;">//the addition of the uid class here is so we can remove just this set</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> html_template <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&lt;div class='particle uid_&quot;</span> <span style="color: #339933;">+</span> uid <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">gen_color</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' style='top:&quot;</span> <span style="color: #339933;">+</span> y <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px; left:&quot;</span> <span style="color: #339933;">+</span> x <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px;'&gt;&lt;/div&gt;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>html_template<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> particle_uid <span style="color: #339933;">=</span> <span style="color: #3366CC;">'.uid_'</span> <span style="color: #339933;">+</span> uid<span style="color: #339933;">;</span><br />
$<span style="color: #009900;">&#40;</span>particle_uid<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> new_x <span style="color: #339933;">=</span> random_from_to<span style="color: #009900;">&#40;</span>x <span style="color: #339933;">-</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span> x <span style="color: #339933;">+</span> <span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> new_y <span style="color: #339933;">=</span> random_from_to<span style="color: #009900;">&#40;</span>y <span style="color: #339933;">-</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">,</span> y <span style="color: #339933;">+</span> <span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; self.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'left'</span><span style="color: #339933;">:</span>new_x<span style="color: #339933;">,</span> <span style="color: #3366CC;">'top'</span><span style="color: #339933;">:</span>new_y<span style="color: #339933;">,</span> <span style="color: #3366CC;">'opacity'</span><span style="color: #339933;">:</span><span style="color: #CC0000;">0</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>particle_uid<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">//make note of the 500, it should be set to the same value as your transition property in the CSS statement</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>That&#8217;s pretty much it! I&#8217;ve posted the final Particle class code below (a little cleaned up). Here&#8217;s the final working model &#8211; <a href='http://joelongstreet.com/blog_files/basic_particles'>http://joelongstreet.com/blog_files/basic_particles</a>. You could use the particle class in a variety of ways so I&#8217;ve prepared another scenario in which particles are generated automatically &#8211; <a href='http://joelongstreet.com/blog_files/basic_particles/random_position.html'>http://joelongstreet.com/blog_files/basic_particles/random_position.html</a>.</p>
<div class="codecolorer-container javascript railscasts" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> Particles<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> x_range <span style="color: #339933;">=</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> y_range <span style="color: #339933;">=</span> <span style="color: #CC0000;">500</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> max_particles <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> min_particles <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">gen_color</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> rand <span style="color: #339933;">=</span> random_from_to<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>rand<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rand <span style="color: #339933;">=</span> <span style="color: #3366CC;">'purple'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rand <span style="color: #339933;">=</span> <span style="color: #3366CC;">'green'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rand <span style="color: #339933;">=</span> <span style="color: #3366CC;">'yellow'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rand <span style="color: #339933;">=</span> <span style="color: #3366CC;">'orange'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rand <span style="color: #339933;">=</span> <span style="color: #3366CC;">'red'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> rand<span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> num_particles <span style="color: #339933;">=</span> random_from_to<span style="color: #009900;">&#40;</span>min_particles<span style="color: #339933;">,</span> max_particles<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> uid <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>num_particles<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> html_template <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&lt;div class='particle uid_&quot;</span> <span style="color: #339933;">+</span> uid <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">gen_color</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;' style='top:&quot;</span> <span style="color: #339933;">+</span> y <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px; left:&quot;</span> <span style="color: #339933;">+</span> x <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;px;'&gt;&lt;/div&gt;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>html_template<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;">var</span> particle_uid <span style="color: #339933;">=</span> <span style="color: #3366CC;">'.uid_'</span> <span style="color: #339933;">+</span> uid<span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;$<span style="color: #009900;">&#40;</span>particle_uid<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> new_x <span style="color: #339933;">=</span> random_from_to<span style="color: #009900;">&#40;</span>x <span style="color: #339933;">-</span> x_range<span style="color: #339933;">,</span> x <span style="color: #339933;">+</span> x_range<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> new_y <span style="color: #339933;">=</span> random_from_to<span style="color: #009900;">&#40;</span>y <span style="color: #339933;">-</span> y_range<span style="color: #339933;">,</span> y <span style="color: #339933;">+</span> y_range<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">'left'</span><span style="color: #339933;">:</span>new_x<span style="color: #339933;">,</span> <span style="color: #3366CC;">'top'</span><span style="color: #339933;">:</span>new_y<span style="color: #339933;">,</span> <span style="color: #3366CC;">'opacity'</span><span style="color: #339933;">:</span><span style="color: #CC0000;">0</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.<span style="color: #660066;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Further Reading:</p>
<ul>
<li><a target='_blank' href='http://zeptojs.com'>Zepto.js</a></li>
<li><a target='_blank' href='http://webkit.org'>Webkit Animation</a></li>
<li><a target='_blank' href='http://net.tutsplus.com/tutorials/javascript-ajax/the-basics-of-object-oriented-javascript/'>NetTuts &#8211; Object Oriented JavaScript</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://joelongstreet.com/blog/2011/08/02/webkit-javascript-particle-effect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

