<?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>Jason Mooberry &#187; jQuery</title>
	<atom:link href="http://blog.jasonmooberry.com/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jasonmooberry.com</link>
	<description>Stuff I did for you.</description>
	<lastBuildDate>Sat, 29 Oct 2011 23:41:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.2</generator>
		<item>
		<title>The long dark tea-time of the node</title>
		<link>http://blog.jasonmooberry.com/2010/11/the-long-dark-tea-time-of-the-node/</link>
		<comments>http://blog.jasonmooberry.com/2010/11/the-long-dark-tea-time-of-the-node/#comments</comments>
		<pubDate>Fri, 05 Nov 2010 04:52:13 +0000</pubDate>
		<dc:creator>Jason Mooberry</dc:creator>
				<category><![CDATA[Games]]></category>
		<category><![CDATA[express]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[ndistro]]></category>
		<category><![CDATA[node.js]]></category>
		<category><![CDATA[OAuth]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://blog.jasonmooberry.com/?p=91</guid>
		<description><![CDATA[Looking back I would&#8217;ve never guessed this project would have had so many twists and turns. I set out to build a game that was about synchronizing with computer generated rhythms. Ie. Make the box move at the same rhythm as a sine wave. I really liked the idea of something so simple requiring intense [...]]]></description>
			<content:encoded><![CDATA[<p>Looking back I would&#8217;ve never guessed this project would have had so many twists and turns.  I set out to build a game that was about synchronizing with computer generated rhythms.  Ie. Make the box move at the same rhythm as a sine wave.  I really liked the idea of something so simple requiring intense concentration.  It&#8217;s immersive but really really simple.  Anyways, I set out to craft this thing with some jQuery, plain js, and recursive timeouts.  At some point I thought it might be fun to track the score and even make an easy way to share your score on Twitter or Facebook.  I figured I&#8217;d make some type of nodejs app to handle the sharing and the rest could be static html pages.</p>
<p>What followed was a journey through two different types of oauth implementations, several nodejs modules and many greps through codebases to figure out how and why this worked or didn&#8217;t.  Since much of this will be useful to some other poor sap building with the same tools, I&#8217;ll outline here what I did and how it all works together.</p>
<h3>Javascript, front to back</h3>
<p><strong>Animation</strong><br />
Building the game itself took some time but was pretty straight-forward.  jQuery is extremely useful for binding events and such, but I tried to use bare js inside the loops for efficiency.  Since I&#8217;m not using canvas here, just manipulating positions of dom objects, it&#8217;s fairly processor intensive.  With that said Chrome and Safari handle it with ease.  Firefox will make your processor fan spin up.</p>
<p>The core of the animation stuff is variations on this simple recursive timeout:</p>
<pre class="brush:js">
block = $('#block');
move_timeout = null;
(function send_it_down() {
  move_timeout = setTimeout(function() {
    block.css('top',block.css('top')+1);
    send_it_down();
  },30);
})();
// to stop animation
// clearTimeout(move_timeout);
</pre>
<p>jQuery has some nice animation functionality but it&#8217;s all based on a fixed time of execution in which each step&#8217;s timeout is based on the amount of steps to execute and the total time for the animation to run.  Rolling my own animation code allowed me to control an infinitely executing constant speed animation.</p>
<p>The <a href="http://api.jquery.com/">jQuery api docs</a> and <a href="http://www.w3schools.com/jsref/default.asp">w3schools js ref</a> got me through anything tricky and <a href="http://mathworld.wolfram.com/Nephroid.html">Wolframs Mathworld</a> was the key to the color game (intially it was a spirograph pattern, then a simpler nephroid, finally a circle).</p>
<p><strong>OAuth</strong><br />
First thing&#8217;s first.  What is <a href="http://oauth.net/">OAuth</a> and how do I use it?  In order to make sharing your score easy I had to write an app that interfaced with both <a href="http://dev.twitter.com/doc">Twitter</a> and <a href="http://developers.facebook.com/docs/">Facebook&#8217;s</a> apis.  I heard Twitter was simpler and their docs were better so I started there.  A link in the twitter docs to this <a href="http://hueniverse.com/oauth/">Oauth Guide</a> made for an easy introduction.  Twitter is currently using an OAuth 1.0 implementation that is pretty true to what you would expect after reading the guide.  The only difference was that Twitter seems to store your callback url for you on your request token call so you don&#8217;t need to supply it on your auth token call.  Facebook has an OAuth 2.0 implementation that requires ssl and has fewer steps.  The only catch on Facebook is they&#8217;re sticklers for your callback url.  It must be present in both your authorize call as well as your auth token call and it must match exactly.  </p>
<p>Now that I know what OAuth is and have a basic understanding of how to interface with Twitter and Facebook&#8217;s apis, I searched out an OAuth module for nodejs.  </p>
<p><strong>Nodejs</strong><br />
I found some buzz around <a href="http://github.com/ciaranj/node-oauth">Ciaranj&#8217;s node-oauth</a> and found it to a pretty good wrapper for the functionality I would need.  It&#8217;s lacking a way to make POST requrests in the OAuth2 code, but writing my own wasn&#8217;t too tough.  I had my eye on <a href="http://expressjs.com/">Expressjs</a> as a framework for building http handlers for this app.  It&#8217;s a sweet little implementation with a very simple premise and good docs.  For my datastore I decided to take another crack at <a href="http://www.mongodb.org/">MongoDB</a>.  I&#8217;ve built PHP apps that interfaced with Mongo but nothing with nodejs yet.  </p>
<p>So far Nodejs has been a great tool for ad hoc api clients and prototypes.  This is the first server app I&#8217;ve written with nodejs.  A few things I noticed that were unexpected.  One, nesting callbacks can become tedious.  This was especially true with the MongoDB module.  I don&#8217;t see any way to get around it, but it&#8217;s ugly.  The other thing was that error handling with callbacks is much different than exception handling within a single thread of execution.  Wrap anything with a callback in a try/catch and you&#8217;ll see it&#8217;s useless.  So becoming expectant on an onError() method for all of the connections is the norm.  </p>
<p>Other challenges with nodejs included: since my nodejs app is listening on a different port than my html pages are being served from, I had to do some extra redirection at the end of my OAuth process so that my final landing page of my popup would be able to call back to the main window and trigger a success event.  And any exception that I didn&#8217;t catch takes down my app completely.  </p>
<p>With that aside I was able to craft a sweet little app in less than a couple hundred lines that is fast and easy.  I can manage the sharing process with a couple ajax calls and a bind to my custom success event.</p>
<p><strong>nDistro</strong><br />
Installing nodejs modules can be done a few different ways.  Initially I was using <a href="http://npmjs.org/">npm</a>.  It worked ok but since it installs to a central npm repository, and nodejs and it&#8217;s modules version so quickly, it made more sense to use <a href="http://github.com/visionmedia/ndistro">nDistro</a>.  It allows you to manage your nodejs versions as well as your module versions from a simple config file.  This is super useful since I can literally break my app with the wrong version.  </p>
<p>I found one oddity using nDistro to install <a href="http://github.com/christkv/node-mongodb-native">MongoDB&#8217;s node module</a>.  If you compile the C version of the BSON parser and intend to use it with your code, it will not be available.  The reason for this is that nDistro symlinks each module to a common lib/node directory.  There is a relative path that is broken in one of mongodb&#8217;s require statements for including the compiled BSON parser.  I was able to fix this by setting up a symlink in the lib directory.  Another issue with the MongoDB module is that in order to compile the native BSON parser you need to have a full nodejs install with the node-waf binary in an available path.  The node binary installed by nDistro is not sufficient.  After all of this I had errors compiling BSON on my vps that were not on my mac..  The js parser will work fine given the size of my records.</p>
<p><strong>MongoDB</strong><br />
<a href="http://www.mongodb.org/">MongoDB</a> showed up on my radar over a year ago and since then it&#8217;s gotten easier to use and more reliable.  The 10gen developers have put a lot of effort into building good libraries in many languages to interface with MongoDB.  With the <a href="http://groups.google.com/group/mongodb-user/browse_thread/thread/528a94f287e9d77e">postmortem</a> after <a href="http://blog.foursquare.com/2010/10/05/so-that-was-a-bummer/">FourSquare&#8217;s recent outage</a>, I got a little window into the scale that mongo seems to be handling well.  FTA the outage was due to overactivity in one of the shards that was running too close to memory capacity.  Given that FourSquare is running multiple 66GB RAM EC2 mongodbs and they just hit their <a href="http://blog.foursquare.com/2010/10/06/quite-the-way-to-celebrate-our-200-millionth-check-in/">200 millionth checkin</a> I am confident that my 2 dozen friend&#8217;s tweet/post counts will be handled with grace and speed.  <img src='http://blog.jasonmooberry.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Installing MongoDB is pretty much a matter of unzipping a tgz and running mongod from the command line.  </p>
<h3>Putting it all together</h3>
<p>This post has been short on code snippets cus I&#8217;m hosting the entire codebase at the <a href="https://github.com/jasonmoo/tunr">Github</a>.  This is my first run at a nodejs app with mongo running live and I am curious to see how stable everything is.  My new linode server is purring like a kitten with anything I throw at it.  Hopefully the tunr app continues the trend.  </p>
<p>Oh and you can <a href="http://tunr.jasonmooberry.com">play tunr here</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jasonmooberry.com/2010/11/the-long-dark-tea-time-of-the-node/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Geoff &#8211; Game Life!</title>
		<link>http://blog.jasonmooberry.com/2010/06/geoff-game-life/</link>
		<comments>http://blog.jasonmooberry.com/2010/06/geoff-game-life/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 00:31:05 +0000</pubDate>
		<dc:creator>Jason Mooberry</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[game of life]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.jasonmooberry.com/?p=61</guid>
		<description><![CDATA[I've been fascinated with Conway's Game of Life for years now.  I love how there is an apparent order to things that is reproducible as well as simple in it's requirements.  Recently there was a discovery that made the tech news about a new pattern that replicated itself.  I'll be honest, the demo of the replicating code wasn't as sexy as I had hoped.  But it did remind me how much I want to make this game.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been fascinated with <a href="http://en.wikipedia.org/wiki/Conway's_Game_of_Life" target="_blank">Conway&#8217;s Game of Life</a> for years now.  I love how there is an apparent order to things that is reproducible as well as simple in it&#8217;s requirements.  Recently there was a discovery that made the tech news about a <a href="http://www.newscientist.com/article/mg20627653.800-first-replicating-creature-spawned-in-life-simulator.html" target="_blank">new pattern that replicated itself</a>.  I&#8217;ll be honest, the demo of the replicating code wasn&#8217;t as sexy as I had hoped.  But it did remind me how much I want to make this game.</p>
<p>I&#8217;ve been on a recent tear through javascript, browser and server side (node.js is just fun).  So I set out to build a little game of life with js and html.  jQuery aided with some of the lifting too.  :)</p>
<p>After a couple afternoons Geoff was born.  He&#8217;s a simple lad.  Just gives you the basics, but still fun to play with non-the-less.  I have only tested him on Chrome, FF, and Safari.  Interestingly this is a great way to see how fast your js engine is.  Chrome handles it the best on my laptop.  FF the worst.</p>
<p><strong>A little bit about the code:</strong></p>
<p>jQuery&#8217;s <a href="http://api.jquery.com/live/" target="_blank">live()</a> event handling is perfect for this situation.  Since I opted to turn a crap-ton of b tags into large pixels of my game grid, I needed to bind some mouseover/mouseout and click events to each item.  There is no need to do this to each of the ~2k tags.  Instead, using live(), jQuery will bind to the document and as events bubble up, check to see if they&#8217;re relevant to my event handlers.  This cuts memory usage as well as bind time.</p>
<p>Recursion with setTimeout().  Initially when I built this I was using setInterval() to fire my life function and advance Geoff another step.  On Chrome it was fast enough that I didn&#8217;t notice any problems.  But when I tested in Safari and FF, I found that I was &#8220;dropping frames&#8221; when I cranked up the speed.  This had weird effects.  So I used a recursive function that pauses at the end of it&#8217;s execution.  This will ensure that I don&#8217;t lose an iteration, while giving reasonable control over the speed of execution.</p>
<p>Pseudo-casting as integer.  This is a hack that I picked up from <a href="http://james.padolsey.com/javascript/double-bitwise-not/">James Padolsey&#8217;s blog</a>.  For my purposes it&#8217;s a nice way to take a boolean expression and convert it into a 0 or 1.</p>
<pre class="brush: js">
// sum up the active neighbors
active += ~~(typeof current[neighbors[j]] === 'number' &amp;&amp; current[neighbors[j]] === 1);
</pre>
<p>Best practices.  I&#8217;ve violated them .. a bit.  :)</p>
<p>Well I hope you have fun playing.  I tried drawing moo and it lived a healthy and rich life.  ;)</p>
<p><strong><a href="http://jasonmooberry.com/dev/geoff.html" target="_blank">Enjoy Geoff!</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jasonmooberry.com/2010/06/geoff-game-life/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.081 seconds -->

