<?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>Refactored scope &#187; javascript</title>
	<atom:link href="http://ominian.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://ominian.com</link>
	<description>PyMethius project notes</description>
	<lastBuildDate>Sun, 06 May 2012 05:03:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>CraftJS; interesting potential</title>
		<link>http://ominian.com/2012/05/05/craftjs-interesting-potential/</link>
		<comments>http://ominian.com/2012/05/05/craftjs-interesting-potential/#comments</comments>
		<pubDate>Sun, 06 May 2012 05:01:27 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[canvas]]></category>
		<category><![CDATA[Game development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[brain_dump]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=194</guid>
		<description><![CDATA[The other month/week I resumed some dabbling/hacking experiments in working with HTML5&#8242;s canvas. This go around, I decided to give CraftyJS a try. The short version of my experiments, CraftyJS has a lot of potential if it can get it&#8217;s memory leaks under control, which might be a daunting task. Here&#8217;s the example Pong game <a href='http://ominian.com/2012/05/05/craftjs-interesting-potential/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>The other month/week I resumed some dabbling/hacking experiments in working with HTML5&#8242;s canvas.  This go around, I decided to give CraftyJS a try.</p>
<p>The short version of my experiments, CraftyJS has a lot of potential if it can get it&#8217;s memory leaks under control, which might be a daunting task.  </p>
<p>Here&#8217;s the example Pong game implementation that can be found on CraftyJS&#8217;s website:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
Crafty.<span style="color: #660066;">init</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">600</span><span style="color: #339933;">,</span><span style="color: #CC0000;">300</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Crafty.<span style="color: #660066;">background</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;rgb(127,127,127)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//Paddles</span>
Crafty.<span style="color: #660066;">e</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Paddle, 2D, DOM, Color, Multiway&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">color</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'rgb(255,0,0)'</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> x<span style="color: #339933;">:</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> w<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> h<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">multiway</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> W<span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> S<span style="color: #339933;">:</span> <span style="color: #CC0000;">90</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
Crafty.<span style="color: #660066;">e</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Paddle, 2D, DOM, Color, Multiway&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">color</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'rgb(0,255,0)'</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> x<span style="color: #339933;">:</span> <span style="color: #CC0000;">580</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> w<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> h<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">multiway</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> UP_ARROW<span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> DOWN_ARROW<span style="color: #339933;">:</span> <span style="color: #CC0000;">90</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//Ball</span>
Crafty.<span style="color: #660066;">e</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;2D, DOM, Color, Collision&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">color</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'rgb(0,0,255)'</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> x<span style="color: #339933;">:</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #CC0000;">150</span><span style="color: #339933;">,</span> w<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> h<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>
            dX<span style="color: #339933;">:</span> Crafty.<span style="color: #660066;">math</span>.<span style="color: #660066;">randomInt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            dY<span style="color: #339933;">:</span> Crafty.<span style="color: #660066;">math</span>.<span style="color: #660066;">randomInt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'EnterFrame'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">//hit floor or roof</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">||</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">290</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dY</span> <span style="color: #339933;">*=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">600</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">;</span>
            Crafty<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;LeftPoints&quot;</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>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">points</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; Points&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">;</span>
            Crafty<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;RightPoints&quot;</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>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">points</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; Points&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">+=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dX</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">+=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dY</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">onHit</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Paddle'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dX</span> <span style="color: #339933;">*=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//Score boards</span>
Crafty.<span style="color: #660066;">e</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;LeftPoints, DOM, 2D, Text&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> x<span style="color: #339933;">:</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> w<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> h<span style="color: #339933;">:</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> points<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: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;0 Points&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Crafty.<span style="color: #660066;">e</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;RightPoints, DOM, 2D, Text&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> x<span style="color: #339933;">:</span> <span style="color: #CC0000;">515</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> w<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> h<span style="color: #339933;">:</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> points<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: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;0 Points&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now I will break this down.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Crafty.<span style="color: #660066;">init</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">600</span><span style="color: #339933;">,</span><span style="color: #CC0000;">300</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Crafty.<span style="color: #660066;">background</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;rgb(127,127,127)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Relatively straight forward; this generates a HTML5 canvas element of 600&#215;300 pixels, sets the color to a neutral color range.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Crafty.<span style="color: #660066;">e</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Paddle, 2D, DOM, Color, Multiway&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">color</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'rgb(255,0,0)'</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> x<span style="color: #339933;">:</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> w<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> h<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">multiway</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> W<span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">90</span><span style="color: #339933;">,</span> S<span style="color: #339933;">:</span> <span style="color: #CC0000;">90</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Part of Crafty&#8217;s cleverness is in it&#8217;s component based entity system.  The first line tells the Crafty library that you&#8217;re defining a new entity called &#8220;Paddle&#8221; which is a 2D, DOM behavior like, color&#8217;d objected that reacts to multiple user inputs.   Break down:</p>
<p>.color(&#8230;) defines the paddles color<br />
.attr(&#8230;) defines the entities dimensions AND position.<br />
.<a href="http://craftyjs.com/api/Multiway.html">multiway</a>(&#8230;) is a clear API method which states<br />
the entities movement speed, and then which direction a triggered keyboard input will send the entitiy.  So in the above case W is north, and S is south.</p>
<p>Skipping the other paddle, we come to the ball in a pong game.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Crafty.<span style="color: #660066;">e</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;2D, DOM, Color, Collision&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">color</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'rgb(0,0,255)'</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> x<span style="color: #339933;">:</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #CC0000;">150</span><span style="color: #339933;">,</span> w<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> h<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>
            dX<span style="color: #339933;">:</span> Crafty.<span style="color: #660066;">math</span>.<span style="color: #660066;">randomInt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            dY<span style="color: #339933;">:</span> Crafty.<span style="color: #660066;">math</span>.<span style="color: #660066;">randomInt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'EnterFrame'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">//hit floor or roof</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">&lt;=</span> <span style="color: #CC0000;">0</span> <span style="color: #339933;">||</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">290</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dY</span> <span style="color: #339933;">*=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">600</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">;</span>
            Crafty<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;LeftPoints&quot;</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>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">points</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; Points&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">;</span>
            Crafty<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;RightPoints&quot;</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>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">points</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; Points&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">+=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dX</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">+=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dY</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">onHit</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Paddle'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dX</span> <span style="color: #339933;">*=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Something I glossed over, Crafty&#8217;s e(&#8230;) function is actually a Javascript prototype class generator.  It&#8217;s important to note that because of the interplay between .attr() and every other post e(&#8230;) method call.</p>
<p>In the above</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> x<span style="color: #339933;">:</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">,</span> y<span style="color: #339933;">:</span> <span style="color: #CC0000;">150</span><span style="color: #339933;">,</span> w<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span> h<span style="color: #339933;">:</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">,</span>
            dX<span style="color: #339933;">:</span> Crafty.<span style="color: #660066;">math</span>.<span style="color: #660066;">randomInt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            dY<span style="color: #339933;">:</span> Crafty.<span style="color: #660066;">math</span>.<span style="color: #660066;">randomInt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>defines additional properties of the entity.  x/y &#038; w/h are already used by the 2D and DOM components to determine the entities position on but in the later .bind(&#8220;EnterFrame&#8221;) notice that the logic involved is referencing this attributes.</p>
<p>So inside the EnterFrame handler for the Ball are two crucial sections.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">600</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">;</span>
            Crafty<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;LeftPoints&quot;</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>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">points</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; Points&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">300</span><span style="color: #339933;">;</span>
            Crafty<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;RightPoints&quot;</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>
                <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">++</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">points</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; Points&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;</span>pre<span style="color: #339933;">&gt;</span>
&nbsp;
These two <span style="color: #000066; font-weight: bold;">if</span> clauses handle bounds check<span style="color: #3366CC;">'s and then depending on position award the appropriate side a point.
&nbsp;
Notice the Crafty(&quot;&quot;) calls.  In this case Crafty(&quot;LeftPoints&quot;) calls up the appropriate entity or entities ( in this case there are only 1 per side ) and applies a closure to increment the winning side'</span>s <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">points</span> and body text.
&nbsp;
&nbsp;
<span style="color: #660066;">The</span> other crucial section <span style="color: #000066; font-weight: bold;">is</span> the <span style="color: #CC0000;">2</span> line closure 
&nbsp;
<span style="color: #339933;">&lt;</span>pre lang<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;javascript&quot;</span><span style="color: #339933;">&gt;</span>
.<span style="color: #660066;">onHit</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Paddle'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dX</span> <span style="color: #339933;">*=</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A fairly cool concept in Crafty is that collision detection between almost any entity is dealt with by Crafty, so to determine if the Ball hits a paddle, you just have to ask Crafty.</p>
<h3>Summary</h3>
<p>I was fairly impressed with CraftyJS, it is very much the spiritual relative of jQuery for a dirt simple and comfortably clever API.  Unfortunately Crafty can blind side you with it&#8217;s cleverness if you&#8217;re not careful.  What I mean is that in experimenting with CraftyJS I found it fairly easy to create non-obvious memory leaks and or abuse closures to much.</p>
<p>Still it&#8217;s a fairly nice library and though this post is slightly long, I am only covering a thin scratch of the depth that is in CraftJS.  This could be a fantastic building stage for making a slew of quick and dirty games.</p>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2012/05/05/craftjs-interesting-potential/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A web-console implemented in nodeJS</title>
		<link>http://ominian.com/2011/05/17/a-web-console-implemented-in-nodejs/</link>
		<comments>http://ominian.com/2011/05/17/a-web-console-implemented-in-nodejs/#comments</comments>
		<pubDate>Tue, 17 May 2011 16:44:47 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[howto]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[NodeJS]]></category>
		<category><![CDATA[hacks]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=107</guid>
		<description><![CDATA[Reading over this ( http://gonzalo123.wordpress.com/2011/05/16/web-console-with-node-js/ ) , I felt this was a pretty good and relatively real world example of nodeJS&#8217;s capabilities. I did find one line near the end very poignant and reflective of my own opinions at the end of the show &#038; tell post I don’t know if node.js is the future <a href='http://ominian.com/2011/05/17/a-web-console-implemented-in-nodejs/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>Reading over <a href="http://gonzalo123.wordpress.com/2011/05/16/web-console-with-node-js/">this</a> ( http://gonzalo123.wordpress.com/2011/05/16/web-console-with-node-js/ ) , I felt this was a pretty good and relatively real world example of nodeJS&#8217;s capabilities.  I did find one line near the end very poignant and reflective of my own opinions at the end of the show &#038; tell post </p>
<blockquote><p>I don’t know if node.js is the future or is just another hype, but it’s easy. And cool. Really cool.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2011/05/17/a-web-console-implemented-in-nodejs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New language to the toolset: NodeJS</title>
		<link>http://ominian.com/2011/05/16/new-language-to-the-toolset-nodejs/</link>
		<comments>http://ominian.com/2011/05/16/new-language-to-the-toolset-nodejs/#comments</comments>
		<pubDate>Mon, 16 May 2011 21:35:01 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[howto]]></category>
		<category><![CDATA[NodeJS]]></category>
		<category><![CDATA[about]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[woot]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=104</guid>
		<description><![CDATA[I&#8217;ve started self-teaching myself NodeJS and not surprisingly there was a recent popular reddit link pointing to a not very short introduction to nodejs here ( http://anders.janmyr.com/2011/05/not-very-short-introduction-to-nodejs.html ) . At this point I haven&#8217;t found any good books for reference lookup&#8230; but hopefully in a week or so I&#8217;ll have gotten spooled up enough to <a href='http://ominian.com/2011/05/16/new-language-to-the-toolset-nodejs/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve started self-teaching myself NodeJS and not surprisingly there was a recent popular reddit link pointing to a not very short introduction to nodejs <a href="http://anders.janmyr.com/2011/05/not-very-short-introduction-to-nodejs.html">here</a> ( http://anders.janmyr.com/2011/05/not-very-short-introduction-to-nodejs.html ) .  At this point I haven&#8217;t found any good books for reference lookup&#8230; but hopefully in a week or so I&#8217;ll have gotten spooled up enough to be able to report back.</p>
<p>In the meantime as I use Ubuntu for everything but games, this short article <a href="http://www.codediesel.com/linux/installing-node-js-on-ubuntu-10-04/">here</a> ( http://www.codediesel.com/linux/installing-node-js-on-ubuntu-10-04/ ) was enough to get a working environment running though I recommend using a pristine virtual machine instance to keep things simple.</p>
<p>Last point, a reliable friend &#038; peer of mine recommended I check out this MVC like framework for NodeJS ( http://expressjs.com/ ).  Preliminary it feels like a hybrid between Twistd and CherryPy in Python or Limonade PHP as far as style&#8230; aiming more for simplicity over feature/complexity.</p>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2011/05/16/new-language-to-the-toolset-nodejs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JQuery dataTable plugin quick notes</title>
		<link>http://ominian.com/2011/03/31/jquery-datatable-plugin-quick-notes/</link>
		<comments>http://ominian.com/2011/03/31/jquery-datatable-plugin-quick-notes/#comments</comments>
		<pubDate>Thu, 31 Mar 2011 16:09:34 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[howto]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[educational]]></category>
		<category><![CDATA[oh_snap]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=97</guid>
		<description><![CDATA[Server-side processing To return the names of the columns you want, you need to use aoColumndefs and provided individual objects with the property sName. Below is most of the implementation for a jeditable Ajax driven dataTables, this is the Alpha version and still needs a lot of love in places, but the gist of it <a href='http://ominian.com/2011/03/31/jquery-datatable-plugin-quick-notes/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<h2>Server-side processing</h2>
<p>
   To return the names of the columns you want, you need to use aoColumndefs and provided individual objects with the property sName.
</p>
<p>  Below is most of the implementation for a jeditable Ajax driven dataTables, this is the Alpha version and still needs a lot of love in places, but the gist of it is here</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> semesterTable <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#semesterViewTable&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dataTable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #3366CC;">&quot;bProcessing&quot;</span>   <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span>
            <span style="color: #339933;">,</span>   <span style="color: #3366CC;">&quot;bServerSide&quot;</span>   <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span>
            <span style="color: #339933;">,</span>   <span style="color: #3366CC;">&quot;sAjaxSource&quot;</span>   <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&lt;?php echo url_for(&quot;</span>roster<span style="color: #339933;">/</span>ajax<span style="color: #339933;">/</span><span style="color: #009900;">&#123;</span>$semester_id<span style="color: #009900;">&#125;</span><span style="color: #3366CC;">&quot;); ?&gt;&quot;</span>
            <span style="color: #339933;">,</span>   <span style="color: #3366CC;">&quot;fnRowCallback&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> nRow<span style="color: #339933;">,</span> aData<span style="color: #339933;">,</span> iDisplayIndex<span style="color: #339933;">,</span> iDisplayIndexFull <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                    <span style="color: #006600; font-style: italic;">//console.log(nRow, aData, iDisplayIndex, iDisplayIndexFull);</span>
                                    <span style="color: #003366; font-weight: bold;">var</span> recordId <span style="color: #339933;">=</span> aData<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                                    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;td&quot;</span><span style="color: #339933;">,</span> nRow<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;recordId&quot;</span><span style="color: #339933;">,</span> recordId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                    <span style="color: #000066; font-weight: bold;">return</span> nRow<span style="color: #339933;">;</span>
                                <span style="color: #009900;">&#125;</span>
            <span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;fnDrawCallback&quot;</span>  <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                                    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;td.editableMajor&quot;</span><span style="color: #009900;">&#41;</span>
                                        .<span style="color: #660066;">editable</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;?php echo url_for(&quot;</span>roster<span style="color: #339933;">/</span>edit_record<span style="color: #3366CC;">&quot;);?&gt;&quot;</span><span style="color: #339933;">,</span>
                                                    <span style="color: #009900;">&#123;</span>                                                        
                                                          loadurl<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&lt;?php echo url_for(&quot;</span>roster<span style="color: #339933;">/</span>distinct<span style="color: #339933;">/</span>major<span style="color: #339933;">/</span><span style="color: #009900;">&#123;</span>$semester_id<span style="color: #009900;">&#125;</span><span style="color: #3366CC;">&quot;) ?&gt;&quot;</span>
                                                        <span style="color: #339933;">,</span> type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;select&quot;</span>
                                                        <span style="color: #339933;">,</span> submit<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;OK&quot;</span>
                                                        <span style="color: #339933;">,</span> method<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;post&quot;</span>
                                                        <span style="color: #339933;">,</span> submitdata<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>
                                                            console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                                            <span style="color: #003366; font-weight: bold;">var</span> $this <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>
                                                            <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
                                                                    recordId<span style="color: #339933;">:</span> $this.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;recordId&quot;</span><span style="color: #009900;">&#41;</span>
                                                                    <span style="color: #339933;">,</span> classes<span style="color: #339933;">:</span> $this.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #009900;">&#41;</span>
                                                                <span style="color: #009900;">&#125;</span>
                                                        <span style="color: #009900;">&#125;</span>
                                                    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                                <span style="color: #009900;">&#125;</span>
            <span style="color: #009966; font-style: italic;">/* Seriously a meta-generator for this would be nice */</span>
            <span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;aoColumnDefs&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
                <span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span> sName<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;record_id&quot;</span><span style="color: #339933;">,</span>          aTargets<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>00<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> bSearchable<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> bVisible<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span> <span style="color: #009900;">&#125;</span>
                <span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span> sName<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_date&quot;</span><span style="color: #339933;">,</span>         aTargets<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>01<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> sClass<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_date&quot;</span><span style="color: #009900;">&#125;</span>
                <span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span> sName<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_name_both&quot;</span><span style="color: #339933;">,</span>    aTargets<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>02<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> sClass<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_name_both&quot;</span><span style="color: #009900;">&#125;</span>
                <span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span> sName<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_student_id&quot;</span><span style="color: #339933;">,</span>   aTargets<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>03<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> sClass<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_student_id&quot;</span><span style="color: #009900;">&#125;</span>
                <span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span> sName<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_major1&quot;</span><span style="color: #339933;">,</span>       aTargets<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>04<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> sClass<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_major1 editableMajor&quot;</span><span style="color: #009900;">&#125;</span>
                <span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span> sName<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_major2&quot;</span><span style="color: #339933;">,</span>       aTargets<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>05<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> sClass<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_major2 editableMajor&quot;</span><span style="color: #009900;">&#125;</span>
                <span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span> sName<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_major3&quot;</span><span style="color: #339933;">,</span>       aTargets<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>06<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> sClass<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_major3 editableMajor&quot;</span><span style="color: #009900;">&#125;</span>
                <span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span> sName<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_major4&quot;</span><span style="color: #339933;">,</span>       aTargets<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>07<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> sClass<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_major4 editableMajor&quot;</span><span style="color: #009900;">&#125;</span>
                <span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span> sName<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_minor1&quot;</span><span style="color: #339933;">,</span>       aTargets<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>08<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> sClass<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_minor1 editableMajor&quot;</span><span style="color: #009900;">&#125;</span>
                <span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span> sName<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_minor2&quot;</span><span style="color: #339933;">,</span>       aTargets<span style="color: #339933;">:</span><span style="color: #009900;">&#91;</span>09<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> sClass<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;roster_minor2 editableMajor&quot;</span><span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>  For the tbody, just put something like  </p>
<pre>
        &lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Chill the fuck out, it's loading&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;
</pre>
<h3>Server side data contract</h3>
<p>dataTables expects a JSON&#8217;ified construct like this</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$responseBody</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">&quot;sEcho&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sEcho'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;iTotalRecords&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$count</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;iTotalDisplayRecords&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;aaData&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$data</span>
            <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<ul>
<li> sEcho is a security variable used by dataTables, basically just send it back and don&#8217;t worry about it</li>
<li>iTotalRecords &#8211; is the MAXIMUM # of records in the data set without any filtering</li>
<li>iTotalDisplayRecords &#8211; is the MAXIMUM # of records in the data set WITH filtering</li>
<li>An array of numerically indexed arrays, dataTables doesn&#8217;t like associative arrays</li>
</ul>
<h1>Total and Display total records</h1>
<p>   If the blink tag still worked reliably, I&#8217;d put a neon sign here.   Basically you need to do 3 queries per Ajax call&#8230; call 1 is to get a</p>
<pre>
   SELECT count(1) FROM sourceTABLE
</pre>
<p>call 2 is</p>
<pre> SELECT count(1) FROM sourceTable WHERE someCriteria </pre>
<p> and finally call 3 is</p>
<pre> SELECT yourColumnList FROM sourceTable WHERE someCriteria</pre>
<p>With memcache or such, I&#8217;d advise caching the total count but no matter what you unfortunately need to make these three SQL calls <img src='http://ominian.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2011/03/31/jquery-datatable-plugin-quick-notes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ping documentation in progress</title>
		<link>http://ominian.com/2011/03/27/ping-documentation-in-progress/</link>
		<comments>http://ominian.com/2011/03/27/ping-documentation-in-progress/#comments</comments>
		<pubDate>Sun, 27 Mar 2011 23:27:57 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[canvas]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ping toolset]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[status]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=89</guid>
		<description><![CDATA[I&#8217;ve been writing class/method level documentation and ideally plan to make a step by step explanation of what the heck is going on inside the Ping toolset. Until then it&#8217;s pretty doubtful anyone will want to use it. Never mind everything is still in Alpha state and there is very little guarantees that something won&#8217;t <a href='http://ominian.com/2011/03/27/ping-documentation-in-progress/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been writing class/method level documentation and ideally plan to make a step by step explanation of what the heck is going on inside the Ping toolset.  Until then it&#8217;s pretty doubtful anyone will want to use it.  Never mind everything is still in Alpha state and there is very little guarantees that something won&#8217;t be written.</p>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2011/03/27/ping-documentation-in-progress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Snake game prototype</title>
		<link>http://ominian.com/2011/03/27/snake-game-prototype/</link>
		<comments>http://ominian.com/2011/03/27/snake-game-prototype/#comments</comments>
		<pubDate>Sun, 27 Mar 2011 21:22:03 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[canvas]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[about]]></category>
		<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=87</guid>
		<description><![CDATA[Running live here User input is fed off the left &#038; right cursor keys. Ping additions included a formalized Quad tree library into the ping.Lib namespace but I want to fix up the factory function I wrote to take an instance of the Canvas rendering class instead of manually writing out the root dimensions. Otherwise <a href='http://ominian.com/2011/03/27/snake-game-prototype/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>Running live <a href="http://ominian.com/examples/js/pinglib/snakes.html">here</a></p>
<p>User input is fed off the left &#038; right cursor keys.</p>
<p>Ping additions included a formalized Quad tree library into the ping.Lib namespace but I want to fix up the factory function I wrote to take an instance of the Canvas rendering class instead of manually writing out the root dimensions.  Otherwise collision checks are still a tad wonky&#8230; there&#8217;s a high propensity for near misses unfortunately <img src='http://ominian.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2011/03/27/snake-game-prototype/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Canvas pixel collision detection</title>
		<link>http://ominian.com/2011/03/09/canvas-pixel-collision-detection/</link>
		<comments>http://ominian.com/2011/03/09/canvas-pixel-collision-detection/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 22:28:52 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[canvas]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[hacks]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=82</guid>
		<description><![CDATA[So&#8230; a fair number of people arrive here looking for how to detect when a pixel/object/thing has collided with something else. I&#8217;ll add more information in the future but in the interim, the quick and dirty advice I can provide: Given p = x, y where this is the point your trying to determine if <a href='http://ominian.com/2011/03/09/canvas-pixel-collision-detection/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>So&#8230; a fair number of people arrive here looking for how to detect when a pixel/object/thing has collided with something else.</p>
<p>I&#8217;ll add more information in the future but in the interim, the quick and dirty advice I can provide:</p>
<p>Given p = x, y where this is the point your trying to determine if it intersects another&#8230; the best place is to start with bounding box logic.  Regardless of what shape your object is, if wrap it in an invisible box boundry, it&#8217;s relatively trivial to do:</p>
<p>box upper left origin is bX, bY off size sizeX, sizeY.   If p.x greater then bX and p.x less then bX + sizeX AND p.y greater then bY and p.y less then bY + sizeY   you&#8217;re inside the bounding box and it&#8217;s highly likely your point has or will soon be colliding with an object.</p>
<p>That&#8217;s pretty much it and is perfect if you don&#8217;t mine lagging the crap out of whatever is running this logic.    To make these collision checks performant, you need to make the computer do less work.   For two dimensional space, my personnel research has led me to the Quad tree structure ( <a title="Quad Trees" href="http://en.wikipedia.org/wiki/Quadtree" target="_blank">wikipedia article</a> ).  I&#8217;ve got a brute force / naive implementation prototyped  @ <a href="https://github.com/devdave/Ping---canvas-helper-library/blob/master/mapping.js">here</a> &amp; <a href="https://github.com/devdave/Ping---canvas-helper-library/blob/master/mapping.html">here</a> but as of 2011 March 9th it is not finished and needs some more work.</p>
<p>Basically Quad tree&#8217;s provides a super quick way for you to determine if there is any other objects in relative space to an entity.  Doing a quick lookup, if there&#8217;s nothing nearby then you can skip the bounding box checks and then the stupidly expensive perimeter intersection checks.</p>
<p>Full library is <a href="https://github.com/devdave/Ping---canvas-helper-library">here</a> and like most of my pet projects is heavily undocumented and completely absent of unit-testing.</p>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2011/03/09/canvas-pixel-collision-detection/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Deferreds for JQuery 1.5</title>
		<link>http://ominian.com/2011/03/06/deferreds-for-jquery-1-5/</link>
		<comments>http://ominian.com/2011/03/06/deferreds-for-jquery-1-5/#comments</comments>
		<pubDate>Sun, 06 Mar 2011 20:32:53 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[howto]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[about]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[utilities]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=76</guid>
		<description><![CDATA[Anyone familiar with async programming like Node.JS or Python&#8217;s twistd library will be familiar with deferreds. Its usually a simple nuisance to re-implement when needed, so it&#8217;s nice to hear that&#8217;s not an issue anymore with JQuery&#8217;s $.when &#038; $.then methods Really useful guide here What does this mean? Say you&#8217;ve got a compound action <a href='http://ominian.com/2011/03/06/deferreds-for-jquery-1-5/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>Anyone familiar with async programming like Node.JS or Python&#8217;s <a href="http://twistedmatrix.com/documents/10.1.0/core/howto/defer.html">twistd library</a> will be familiar with deferreds.  Its usually a simple nuisance to re-implement when needed, so it&#8217;s nice to hear that&#8217;s not an issue anymore with JQuery&#8217;s $.when &#038; $.then methods </p>
<p>Really useful guide <a href="http://www.erichynds.com/jquery/using-deferreds-in-jquery/">here </a></p>
<h3>What does this mean?</h3>
<p>Say you&#8217;ve got a compound action you want to complete.  For example a user clicks on a leaf in a tree like directory<br />
menu.  Now you want to update both the menu to add any possible sub nodes of that leaf PLUS update a content panel and once its finished, maybe update the hash tag to the current page&#8217;s url.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
  $.<span style="color: #660066;">when</span><span style="color: #009900;">&#40;</span> updateMenu<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> updateContent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
     .<span style="color: #660066;">success</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>
           console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Both menu &amp; content panel have been updated!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
     .<span style="color: #660066;">fail</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>
           console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Oh noes, we didn't finish... try again?&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p> That&#8217;s pretty much the gist of deferreds in a nutshell.  Furthermore it appears that the author of this fantastic addition to jQuery really grokked the concept of deferreds because you can chain one deferred result to multiple child deferred&#8217;s allowing for cascading events to fire in response to one event.</p>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2011/03/06/deferreds-for-jquery-1-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Return to canvas tag</title>
		<link>http://ominian.com/2011/03/03/return-to-canvas-tag/</link>
		<comments>http://ominian.com/2011/03/03/return-to-canvas-tag/#comments</comments>
		<pubDate>Thu, 03 Mar 2011 21:59:16 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[canvas]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[brain_dump]]></category>
		<category><![CDATA[utilities]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=74</guid>
		<description><![CDATA[Currently in between clients and waiting for the silent alpha of my delicious clone to wrap up&#8230;.so its back to experiments with the canvas tag. That means back to my unframework canvas library Ping. Some of the code still makes sense, but other bits are&#8230; well special. Currently there is an .ex object thats appended <a href='http://ominian.com/2011/03/03/return-to-canvas-tag/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>Currently in between clients and waiting for the silent alpha of my delicious clone to wrap up&#8230;.so its back to experiments with the canvas tag.</p>
<p>That means back to my unframework canvas library <a href="http://code.google.com/p/devdave/source/browse/#svn%2Ftrunk%2Fjavascript%2Fcanvastag%2Fping">Ping</a>.  Some of the code still makes sense, but other bits are&#8230; well special.  Currently there is an .ex object thats appended to the CanvasRenderingContext2D internal class and it works, but what I&#8217;m thinking of is re-working the code so that they&#8217;re added post-instantiation like a factory.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
canvasMaker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>elemId<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> element <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>elemId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> context <span style="color: #339933;">=</span> element.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;2d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">//Here is where all the extension methods would be dynamically assigned to the 2d canvas instance</span>
    wrappedContext <span style="color: #339933;">=</span> wrapContext<span style="color: #009900;">&#40;</span>context<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">//Maybe add a singleton check around the bulk of this that keys on the elemId</span>
    <span style="color: #000066; font-weight: bold;">return</span> wrappedContext<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>   The problem I&#8217;m trying to work around specifically is the need should ever arise to have n+1 managed tags on the same document.  Currently there is so many hardwired references that it would be impossible to have the two cooperate without collisions and undesirable behavior.</p>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2011/03/03/return-to-canvas-tag/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Canvas tag: Collision detection &amp; pixel decay</title>
		<link>http://ominian.com/2010/03/02/canvas-tag-collision-detection-pixel-decay/</link>
		<comments>http://ominian.com/2010/03/02/canvas-tag-collision-detection-pixel-decay/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 22:49:55 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[canvas]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[about]]></category>
		<category><![CDATA[utilities]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=13</guid>
		<description><![CDATA[UPDATED: March 14, 2011 here Continuing my tests/experiments with the Canvas tag has led to the Ping prototype. I had 3 minimal things I wanted to accomplish: Detecting the intersection of a 360 degree arc of ray/line segments to previously defined in map shapes; a visual decay/fade out of intersection points on the canvas, and <a href='http://ominian.com/2010/03/02/canvas-tag-collision-detection-pixel-decay/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>UPDATED: March 14, 2011 <a href="http://ominian.com/2011/03/09/canvas-pixel-collision-detection/" target="_blank">here</a></p>
<p>Continuing my tests/experiments with the Canvas tag has led to the <a href="http://ominian.com/examples/js/canvastag/ping">Ping prototype</a>.  I had 3 minimal things I wanted to accomplish:  Detecting the intersection of a 360 degree arc of ray/line segments to previously defined in map shapes; a visual decay/fade out of intersection points on the canvas, and lastly a test of performance.  In addition I decided to experiment with another approach to Javascript object construction in the hopes of getting a performance gain.<br />
<span id="more-13"></span><br />
<strong> performance</strong><br />
None of the code involved is optimized or really geared towards performance [ I am using JQuery.each(array|object, function() ) ] and a fairly inefficient pattern for determining intersection ( leveraging CPU with little or no caching ) but was mostly happy with the results when using Google Chrome on Ubuntu 9.10 and utterly surprised with Firefox 3.5 &amp; 3.6 on the same system.  It was literally a night and day difference with Chrome purring along and Firefox struggling to keep up.</p>
<p><strong>JS Objects</strong></p>
<p>Besides the canvas tag related stuff, I was experimenting with another approach to class construction that for the moment seems to goofy to be viable.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">appLib.<span style="color: #660066;">Point</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <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>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> y<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
app.<span style="color: #660066;">w</span><span style="color: #009900;">&#40;</span>appLib.<span style="color: #660066;">Point</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>
&nbsp;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">dist</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> tX <span style="color: #339933;">=</span> Math.<span style="color: #660066;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">-</span> o.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> tY <span style="color: #339933;">=</span> Math.<span style="color: #660066;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">-</span> o.<span style="color: #660066;">y</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>tX<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>tY<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">/**
     *Bad design choice
     *@deprecated */</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">slope</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>o<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">y</span> <span style="color: #339933;">-</span> o.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">x</span> <span style="color: #339933;">-</span> o.<span style="color: #660066;">x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>app.w( scope, anonymous) basically makes this in context of the anonymous function == scope.prototype.  Therefore making it easier to add prototypical methods to the final product.  My thought was to avoid closure based creation and rely on Function.prototype.method = function to make the objects leaner/faster.  I figured that Javascript engines have already been optimized to a degree to build an object from it&#8217;s prototype.   From my perspective, there didn&#8217;t seem to be a noticeable difference.</p>
<p><strong>Final thoughts</strong><br />
Ping functions as expected on Chrome but otherwise if not for my quad-core environment, I imagine it would be a rather abysmal user experience.  According to top and Chrome&#8217;s built in task manager, its devouring near 100% cpu time and has a semi-stable memory leak ( drifting up and down around 1,500 KB ever 30 seconds ) when in a resting state.</p>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2010/03/02/canvas-tag-collision-detection-pixel-decay/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

