<?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 Rowe &#187; JavaScript</title>
	<atom:link href="http://jasonrowe.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://jasonrowe.com</link>
	<description>enjoying the web</description>
	<lastBuildDate>Tue, 03 Jan 2012 02:03:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Strophejs ejabberd IIS Setup</title>
		<link>http://jasonrowe.com/2011/11/18/strophejs-ejabberd-iis-setup/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=strophejs-ejabberd-iis-setup</link>
		<comments>http://jasonrowe.com/2011/11/18/strophejs-ejabberd-iis-setup/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 19:08:17 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[XMPP]]></category>
		<category><![CDATA[ejabberd]]></category>
		<category><![CDATA[strophejs]]></category>

		<guid isPermaLink="false">http://jasonrowe.com/?p=1547</guid>
		<description><![CDATA[This is a simple guide for getting strophe.js, ejabberd, and IIS all working together on a local dev machine. This assumes you have IIS 7.5 installed and are familiar with the basics of IIS Manager Step 1: Download and install&#8230;]]></description>
			<content:encoded><![CDATA[<p>This is a simple guide for getting strophe.js, ejabberd, and IIS all working together on a local dev machine. This assumes you have IIS 7.5 installed and are familiar with the basics of IIS Manager</p>

<img src="http://jasonrowe.com/wp-content/uploads/2011/11/ej_iis_stro_blog1.png" alt="strophe ejabberd IIS" title="ej_iis_stro_blog" width="400" height="100" class="alignleft size-full wp-image-1563" />

<p><strong>Step 1</strong>: Download and install <a href="http://www.process-one.net/en/ejabberd/downloads">ejabberd</a>. The install is straight forward and the default configuration has everything you need. During the install it will ask you for a domain. I would recommend setting up a host alias &#8220;localhost.demo.com&#8221; because it looks nicer and easier to use but you could also just use localhost. After install check to make sure the http-bind endpoint is working http://localhost:5280/http-bind/. You should see a simple page that has &#8220;ejabberd mod_http_bind&#8221; at the top</p>

<p><strong>Step 1.5</strong>: Setup a host name if needed. If you used localhost you can skip this step. You can do this in Windows 7 via the host file which should be located &#8220;C:\Windows\System32\drivers\etc\hosts&#8221;. Add a line at the bottom with your IP and new local DNS.</p>

<p><strong>Step 2</strong>: Setup new IIS application and copy in strophe.js examples. Go into IIS manager and create a new application named &#8220;xmppExample&#8221;. Then download the latest <a href="http://strophe.im/strophejs/">strophe.js</a> and copy the contents to your new site. You should be able to access the following now. http://localhost.demo.com/xmppExample/strophejs/examples/basic.html. You will have to change the first line in basic.js to setup your endpoint. It should look like this <strong>BOSH_SERVICE = &#8216;http://localhost.demo.com/http-bind&#8217;</strong></p>

<p><strong>Step 3</strong>: Setup proxy for ejabberd http-bind endpoint. To make things simple we need everything on the same domain. Right now, the bosch http-bind endpoint is this http://localhost:5280/http-bind/ and we need it to be this http://localhost.demo.com/http-bind. Download and install the Application Request Routing for IIS. Once installed follow these steps to enable proxy.
</p>

<ol>
	<li>Open IIS Manager</li>
	<li>Select a server node in the tree view on the left hand side and then click on the &#8220;Application Request Routing&#8230;&#8221; the full name shows up as &#8220;Application Request Routing Cache&#8221;</li>
	<li>Then on the far right side click on &#8220;Server Proxy Settings&#8230;</li>
	<li>check the box next to Enable proxy and apply settings (keep everything else default)</li>
</ol>

<p>
Finally, add a rewrite rule to the web.config file in your inetpub root dir. For example, mine is located &#8220;C:\inetpub\wwwroot\web.config&#8221;. Here is my full XML configuration file:
</p>

<pre class="brush: xml; ">

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration&gt;
    &lt;system.webServer&gt;
	&lt;rewrite&gt;
    		&lt;rules&gt;
          		&lt;rule name=&quot;ejabberd.BOSH&quot; stopProcessing=&quot;true&quot;&gt;
               			&lt;match url=&quot;^http-bind&quot; /&gt;
               			&lt;action type=&quot;Rewrite&quot; url=&quot;http://localhost.demo.com:5280/http-bind/&quot; /&gt;
          		&lt;/rule&gt;
    		&lt;/rules&gt;
	&lt;/rewrite&gt;
        &lt;validation validateIntegratedModeConfiguration=&quot;false&quot; /&gt;
    &lt;/system.webServer&gt;
&lt;/configuration&gt;

</pre>

<p>Now you should be able to run the demo at http://localhost.demo.com/xmppExample/strophejs/examples/basic.html. You will need to login with admin@localhost.demo.com (if admin is the username you selected during install) and the password you selected during install.</p>

<blockquote>I can also be found on Google+

<a title="Jason Rowe is on Google+" rel="author" href="http://profiles.google.com/jasonrowe?rel=author" alt="Google+" title="Google+">Jason Rowe</a> and twitter <a title="Jason Rowe is on twitter" rel="author" href="http://twitter.com/jsonrow?rel=author" alt="twitter" title="twitter">@jsonrow</a>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://jasonrowe.com/2011/11/18/strophejs-ejabberd-iis-setup/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting back into JavaScript</title>
		<link>http://jasonrowe.com/2011/11/02/getting-back-into-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-back-into-javascript</link>
		<comments>http://jasonrowe.com/2011/11/02/getting-back-into-javascript/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 04:17:32 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[General Programming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[charting]]></category>

		<guid isPermaLink="false">http://jasonrowe.com/?p=1498</guid>
		<description><![CDATA[The last time I did any serious JavaScript was around the time Sizzle was added to JQuery, Google Chrome was brand new, and MarketWatch.com started using canvas charts. Note: Here is a quick overview of JavaScript that is as short&#8230;]]></description>
			<content:encoded><![CDATA[<p>The last time I did any serious JavaScript was around the time Sizzle was added to JQuery, Google Chrome was brand new, and MarketWatch.com started using canvas charts.</p> 

<p><strong>Note:</strong> Here is a quick <a href="http://www.2ality.com/2011/10/javascript-overview.html">overview of JavaScript</a> that is as short as possible, but explains every major feature. I would also recommend following the <a href="http://www.2ality.com/">2ality</a> website</p>

<p>Today JavaScript projects have the complexity, size, and structure of projects written in languages like C# and Java. Thankfully, the developer tools for debugging and logging are solid across browsers. *<i>Opera is still a little funky about logging using &#8220;opera.postError.apply(opera, arguments);&#8221;</i>. Looking particularly good, <a href="http://code.google.com/chrome/devtools/">Google&#8217;s version of the webkit dev tools</a> and some API&#8217;s they have in <a href="http://code.google.com/chrome/extensions/dev/experimental.devtools.html">beta</a>. I’ve started to use <a href="http://docs.jquery.com/QUnit">quint</a> for unit testing. I’ve also been looking leveraging these frameworks: <a href="http://documentcloud.github.com/underscore/">underscore.js</a>, <a href="http://jashkenas.github.com/coffee-script/">coffee script</a>, <a href="http://raphaeljs.com/">raphaeljs</a>, and <a href="http://code.google.com/closure/compiler/">closure compiler</a>.</p> <p>It’s amazing how much has changed. Now <a href="https://developer.mozilla.org/en/JavaScript/Reference">examples of good JavaScript</a> are much easier to find. Hell, here is a step by step walkthrough of how to <a href="http://dailyjs.com/tags.html#lmaf">build your very own JavaScript framework</a>.</p> 

<p>So to get some practice I went and found some JS that needed cleanup. I wanted to focus on structure and namespaces and this <a href="http://www.elated.com/res/File/articles/development/javascript/snazzy-animated-pie-chart-html5-jquery/">script</a> needed some love. View source on that page and you will notice global variables and one big pile of code.</p> <p>The first thing I did was break up the logic into separate functional script files that will later be compiled and minified into one. You can see the python script I use to minify <a href="https://raw.github.com/JasonRowe/SimpleHTML5.Charts/master/build.py">here</a>.</p> 

<ol> 
<li><a href="https://github.com/JasonRowe/SimpleHTML5.Charts/blob/master/src/animation.js">animation.js</a></li> 
<li><a href="https://github.com/JasonRowe/SimpleHTML5.Charts/blob/master/src/builder.js">builder.js</a></li> 
<li><a href="https://github.com/JasonRowe/SimpleHTML5.Charts/blob/master/src/core.js">core.js</a></li> 
<li><a href="https://github.com/JasonRowe/SimpleHTML5.Charts/blob/master/src/pie.js">pie.js</a></li> 
<li><a href="https://github.com/JasonRowe/SimpleHTML5.Charts/blob/master/src/rendering.js">rendering.js</a></li> 
<li><a href="https://github.com/JasonRowe/SimpleHTML5.Charts/blob/master/src/styles.js">styles.js</a></li>
</ol> 

<p>I then added a <a href="https://github.com/JasonRowe/SimpleHTML5.Charts/tree/master/test">test folder</a> and setup the same file names but which had test cases for each script. Then an index.html file to display the test groups or “modules” as they are called in QUnit. Here is an example of a test that always passes</p> 

<pre class="brush: js; ">

module(&quot;sample test&quot;);

test(&quot;a sample qunit test&quot;, function () {
    ok(true, &quot;always fine&quot;);
});


</pre>  

<p>The next thing I worked on was getting a namespace around the code. I went with the following which seems to work for my needs.</p> 

<pre class="brush: js; ">

var MYAPP = MYAPP || {};

MYAPP.namespace = function (ns_string) { var parts = ns_string.split(&#039;.&#039;), parent = MYAPP, i; // strip redundant leading global 

if (parts[0] === &quot;MYAPP&quot;) { parts = parts.slice(1); } for (i = 0; i &lt; parts.length; i += 1) { // create a property if it doesn&#039;t exist 

if (typeof parent[parts[i]] === &quot;undefined&quot;) { parent[parts[i]] = {}; 

} parent = parent[parts[i]]; } return parent; }; 


</pre>  

<p>Stefanov, Stoyan (2010). JavaScript Patterns (pp. 89-90). OReilly Media &#8211; A. Kindle Edition. </p>

<p>So after refactoring, I’ve got the pie chart setup code to the following.</p>

<pre class="brush: js; ">

var pieElm = document.getElementById(&#039;pieChart&#039;);
var pieData = $(&#039;#chartData td&#039;);

var pieChart = new SimpleHtml5.Core.Pie(pieElm, pieData);

$(&#039;#pieChart&#039;).click (pieChart, SimpleHtml5.Pie.Animation.HandleChartClick);

</pre>

<p>To customize the style I allow passing in a custom style object to override the defaults.</p>

<pre class="brush: js; ">


var customStyles = {
    chartSizePercent: 49,
    maxPullOutDistance: 10,
    pullOutFrameStep: 4,
    pullOutFrameInterval: 40,
    pullOutLabelPadding: 42
};

var pieElm = document.getElementById(&#039;pieChart&#039;);
var pieData = $(&#039;#chartData td&#039;);
var pieChart = new SimpleHtml5.Core.Pie(pieElm, pieData, customStyles);
$(&#039;#pieChart&#039;).click (pieChart, SimpleHtml5.Pie.Animation.HandleChartClick);

</pre>

<p>
If you are curious about how the other parts of the chart were organized you can see the source code on <a href="https://github.com/JasonRowe/SimpleHTML5.Charts">github</a>.
</p>

<p>

<blockquote>I can also be found on Google+

<a title="Jason Rowe is on Google+" rel="author" href="http://profiles.google.com/jasonrowe?rel=author" alt="Google+" title="Google+">Jason Rowe</a> and twitter <a title="Jason Rowe is on twitter" rel="author" href="http://twitter.com/jsonrow?rel=author" alt="twitter" title="twitter">@jsonrow</a>
</blockquote>


</p>

]]></content:encoded>
			<wfw:commentRss>http://jasonrowe.com/2011/11/02/getting-back-into-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changing Chart Color by Previous Close</title>
		<link>http://jasonrowe.com/2011/01/16/changing-chart-color-by-previous-close/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=changing-chart-color-by-previous-close</link>
		<comments>http://jasonrowe.com/2011/01/16/changing-chart-color-by-previous-close/#comments</comments>
		<pubDate>Mon, 17 Jan 2011 01:40:14 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[charting]]></category>
		<category><![CDATA[gRaphaël]]></category>

		<guid isPermaLink="false">http://jasonrowe.com/?p=1438</guid>
		<description><![CDATA[&#160; I frequently get asked how to create a chart in JavaScript that shows a different line color above and below the previous close price. Here is a example of what I mean. View this test page if you want&#8230;]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p> 

<p>I frequently get asked how to create a chart in JavaScript that shows a different line color above and below the previous close price. Here is a example of what I mean.</p> 

<p><a href="http://jasonrowe.com/wp-content/uploads/2011/01/final_example.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="final_example" border="0" alt="final_example" src="http://jasonrowe.com/wp-content/uploads/2011/01/final_example_thumb.png" width="244" height="177"></a></p> 

<p>View this <a href="http://jasonrowe.com/misc/chart/prevcloseline.html">test page</a> if you want skip the details and see a working example in JavaScript/SVG</p>

<p>At first glance, I’ve always thought this would be very easy and instructed them to create a new path depending on if the line is above or below the previous close line (the blue line in the image above). Simple enough right?</p> 

<p>It turns out I was missing a crucial step that might go unnoticed if you are using many data points. The problem turns up when using sparse data like the example shown above.</p> <p>Here is what it looks like if you do what I said literally.</p> <p><a href="http://jasonrowe.com/wp-content/uploads/2011/01/simple_start_broken.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="simple_start_broken" border="0" alt="simple_start_broken" src="http://jasonrowe.com/wp-content/uploads/2011/01/simple_start_broken_thumb.png" width="244" height="178"></a>&nbsp; </p> <p>&nbsp;</p> <p>That missing step I mentioned is connecting the new lines. Seems obvious now that I look at an example. So how will this be done? Here is an image showing the paths with labels for easy reference.</p> 

<p><a href="http://jasonrowe.com/wp-content/uploads/2011/01/simple_paths_connections.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="simple_paths_connections" border="0" alt="simple_paths_connections" src="http://jasonrowe.com/wp-content/uploads/2011/01/simple_paths_connections_thumb.png" width="244" height="179"></a></p> 

<p>What I have found works best is to find the intersection point between the paths and the previous close line. So you would create one line using the last point from path 1 and the first points from path2. The second line would just be the previous close line points. Unfortunately, just having the points is not enough to calculate the intersection.</p> 

<p>Below is the needed format and calculation. I created a function to do this in JS which is provided below but I wanted to include the <a href="http://www.topcoder.com/tc?module=Static&#038;d1=tutorials&#038;d2=geometry2">full explanation. </a></p> 
<blockquote>

Say we are given two different points, (x1, y1) and (x2, y2), and want to find A, B and C for the equation above. We can do so by setting

A = y2-y1
B = x1-x2
C = A*x1+B*y1

Regardless of how the lines are specified, you should be able to generate two different points along the line, and then generate A, B and C. Now, lets say that you have lines, given by the equations:

A1x + B1y = C1
A2x + B2y = C2

To find the point at which the two lines intersect, we simply need to solve the two equations for the two unknowns, x and y. 

 double det = A1*B2 &#8211; A2*B1
    if(det == 0){
        //Lines are parallel
    }else{
        double x = (B2*C1 &#8211; B1*C2)/det
        double y = (A1*C2 &#8211; A2*C1)/det
    }
</blockquote>


<p>Here is the full function in JS to create this format and find the intersection.</p> 



<pre class="brush: javascript; ">

  //Line-Line Intersection
        var getXCrossing = function(p1x, p1y, p2x, p2y, marker, markerMaxX) {
               
                var retValX = 0;
                var retValY = 0;
               
                //graph lines
                var a1 = p2y - p1y;
                var b1 = p1x - p2x;
                var c1 = a1 * p1x + b1 * p1y;
                
                //marker line is always horizontal
                var a2 = marker - marker;
                var b2 = 0 - markerMaxX;
                var c2 = a2 * 0 + b2 * marker;
                
                var det = a1 * b2 - a2 * b1;
                
                if(det === 0)
                {
                    //Lines are parallel
                }
                else
                {
                    retValX = (b2 * c1 - b1 * c2) / det;
                    retValY = (a1 * c2 - a2 * c1) / det;
                }   
                
                return {
                        x: retValX,
                        y: retValY
                    };
                };

</pre>

<p>Here is a <a href="http://jasonrowe.com/misc/chart/prevcloseline.html">test page</a> which shows a working example in SVG using <a href="http://g.raphaeljs.com/">gRaphaël</a> JavaScript library.</p>
]]></content:encoded>
			<wfw:commentRss>http://jasonrowe.com/2011/01/16/changing-chart-color-by-previous-close/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Force Directed Graph with Raphael</title>
		<link>http://jasonrowe.com/2010/09/25/force-directed-graph-with-raphael/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=force-directed-graph-with-raphael</link>
		<comments>http://jasonrowe.com/2010/09/25/force-directed-graph-with-raphael/#comments</comments>
		<pubDate>Sun, 26 Sep 2010 01:49:11 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Raphael]]></category>
		<category><![CDATA[SVG]]></category>

		<guid isPermaLink="false">http://jasonrowe.com/?p=1360</guid>
		<description><![CDATA[Trigger Graph &#160; I’ve recently been looking around the Canvas and SVG development space and things have changed. Many amazing drawing and charting libraries are now available. The one in particular that caught my eye was Raphael. As the site&#8230;]]></description>
			<content:encoded><![CDATA[<div style="margin-left:20px">
<div id="canvas"></div><a href="#" onclick="StartGraph();return false">Trigger Graph</a></div>
<script type="text/javascript" src="http://jasonrowe.com/misc/raphael-min.js"></script>

  <script type="text/javascript">

        var paper = null;

        var _nodes = new Array();

        StartGraph();

        var intervalID;

        function Animate() {
            var loop = 0;
            return setInterval(function () {
                ApplyForces(_nodes, 29, 0.35, 1);
                loop++;

                if (loop > 15) {
                    clearInterval(intervalID);
                }
            }, 200);
        }

        function Clear() {
            clearInterval(intervalID);
            paper.clear()
        }

        function StartGraph() {

            clearInterval(intervalID);

            if (paper !== null) {
                paper.clear();
                paper.remove();
            }

            paper = Raphael("canvas", 320, 200);

            // rectangle with rounded corners
            var c = paper.rect(1, 0, 305, 190, 10);

            _nodes = [];

            for (var i = 0; i < 20; i++) {

                var randomnumberX = Math.floor(Math.random() * 30);
                var randomnumberY = Math.floor(Math.random() * 30);

                var circle = paper.circle(120 + randomnumberX, 90 + randomnumberY, 10);

                circle.attr("fill", "#f00");

                circle.attr("stroke", "#fff");

                var element = { "circle": circle, "mass": 20, "line": null };

                element.IsConnectedTo = function (node) {


                    node.circle.attr("cx")
                    var nodeX = this.circle.attr("cx");
                    var nodeY = this.circle.attr("cy");

                    var otherNodeX = node.circle.attr("cx");
                    var otherNodeY = node.circle.attr("cy");

                    var d = Distance(nodeX, nodeY, otherNodeX, otherNodeY);

                    if (d < 30) 
                    {
                        if (d > 15) 
                        {//hack for wordpress formatting doesnt allow and sign in if...

                            if (this.line === null) {
                                this.line = paper.path("M{0},{1}L{2},{3}", nodeX, nodeY, otherNodeX, otherNodeY);
                                this.line.attr({ stroke: "#f00" });
                            }

                            if (node.line === null) {
                                node.line = paper.path("M{0},{1}L{2},{3}", otherNodeX, otherNodeY, nodeX, nodeY);
                                node.line.attr({ stroke: "#f00" });
                            }

                            return true;
                        }
                        // hack for wordpress formatting
                        if (this.line !== null) {
                            this.line.remove();
                            this.line = null;
                        }

                        return false;

                     }
                else {

                        if (this.line !== null) {
                            this.line.remove();
                            this.line = null;
                        }

                        return false;
                    }

                };

                _nodes.push(element)

            }

            intervalID = Animate();
        }

        function Distance(x1, y1, x2, y2) {

            return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));

        }


        function ApplyForces(nodes, spring, charge, damping) {

            for (var i in nodes) {

                var node = nodes[i];

                for (var j in nodes) {
                    var otherNode = nodes[j];

                    if (node != otherNode) {

                        var iniOtherNodePosition = otherNode.circle.getBBox();
                        var iniNodePosition = node.circle.getBBox();

                        var dx = iniOtherNodePosition.x - iniNodePosition.x;

                        var dy = iniOtherNodePosition.y - iniNodePosition.y;

                        var hypotenuse = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));

                        var force = 0;

                        if (node.IsConnectedTo(otherNode)) {

                            force = (hypotenuse - spring) / 2.0;
                        }
                        else {

                            force = -((node.mass * otherNode.mass) / Math.pow(hypotenuse, 2)) * charge;

                        }

                        dx /= hypotenuse;

                        dy /= hypotenuse;

                        dx *= force;

                        dy *= force;

                        var nodeCxSum = node.circle.attr("cx") + dx;

                        if (nodeCxSum < 0) {
                            nodeCxSum = 15;
                        }

                        if (nodeCxSum > 310) {
                            nodeCxSum = 290;
                        }


                        node.circle.attr({ cx: nodeCxSum });

                        var nodeCySum = node.circle.attr("cy") + dy;

                        if (nodeCySum < 0) {
                            nodeCySum = 15;
                        }

                        if (nodeCySum > 190) {
                            nodeCySum = 170;
                        }

                        node.circle.attr({ cy: nodeCySum });
                    }
                }


                var cxValue = node.circle.attr("cx");

                var nodePosition = node.circle.getBBox();


                var nodeX = nodePosition.x + cxValue;

                if (nodeX < 0) {
                    nodeX = 15;
                }

                if(nodeX > 310)
                {
                    nodeX = 290;
                }

                node.circle.attr({ x: nodeX });

                var cyValue = node.circle.attr("cy");

                var nodeY = nodePosition.y + cyValue;

                if(nodeY < 0)
                {
                    nodeY = 15;
                }

                if (nodeY > 190) {
                    nodeY = 170;
                }

                node.circle.attr({ y: nodeY });

                if (cxValue < 0) {
                    nodeY = 0;
                }

                if (cxValue > 310) {
                    nodeY = 290;
                }

                if (cyValue < 0) {
                    nodeY = 15;
                }

                if (cyValue > 190) {
                    nodeY = 170;
                }

                node.circle.attr({ cx: cxValue * damping });

                node.circle.attr({ cy: cyValue * damping });
            }

        }
    </script>



<p>&nbsp;</p> <p>I’ve recently been looking around the Canvas and SVG development space and things have changed. Many amazing drawing and charting libraries are now available. The one in particular that caught my eye was <a href="http://raphaeljs.com/">Raphael</a>. As the site says, “Raphael is a small JavaScript library that should simplify your work with vector graphics on the web” and it does.</p> 

<p>I decided to try out the library by building a <a href="http://en.wikipedia.org/wiki/Force-based_algorithms_%28graph_drawing%29">force directed graph</a>. Mine is a pretty simple one and I probably didn’t get all the calculations exactly correct but the point wasn’t really the graph. It was trying out the library and I’m really glad I did. Here is what the graph looks like. Hopefully you saw a working example at the top of this post.</p> 

<a href="http://jasonrowe.com/wp-content/uploads/2010/09/ForceGraphCapture.png"><img src="http://jasonrowe.com/wp-content/uploads/2010/09/ForceGraphCapture.png" alt="" title="ForceGraphCapture" width="325" height="210" class="alignright size-full wp-image-1380" /></a>

<p>As you can see it’s rendered in SVG for supported browsers and VML in IE. I’ve only tested this in the latest versions of Google, Firefox, and IE so sorry if all you see is a broken element or script error.</p> <p>What I really like about the library is it does not focus on one particular thing like charts or plotting. It’s just a drawing library. It has a great focus on providing easy to use shapes and paths. I was able to prototype the graph very quickly. I was shocked at how well it worked across all the browsers.</p>

<p>So I encourage you to go try the Raphael library. You will be building interesting scalable images in no time.</p>]]></content:encoded>
			<wfw:commentRss>http://jasonrowe.com/2010/09/25/force-directed-graph-with-raphael/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Determine if two rectangles overlap each other in JavaScript</title>
		<link>http://jasonrowe.com/2009/03/14/determine-if-two-rectangles-overlap-each-other-in-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=determine-if-two-rectangles-overlap-each-other-in-javascript</link>
		<comments>http://jasonrowe.com/2009/03/14/determine-if-two-rectangles-overlap-each-other-in-javascript/#comments</comments>
		<pubDate>Sat, 14 Mar 2009 07:13:07 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://rowejason.com/?p=338</guid>
		<description><![CDATA[Stack Overflow has a good post on determining if two rectangles are overlapping.  I used the discussed algorithm to check for an overlap and then move one away.  I&#8217;m only moving up or down depending on which rectangle is higher&#8230;]]></description>
			<content:encoded><![CDATA[Stack Overflow has a good <a href="http://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other">post</a> on determining if two rectangles are overlapping.  I used the discussed algorithm to check for an overlap and then move one away.  I&#8217;m only moving up or down depending on which rectangle is higher or lower and if it will fit in a specified areas min or max.

<pre class="brush: js; ">


&lt;script type=&quot;text/javascript&quot;&gt;

var rectA = {x: 2, y: 5, width: 6, height: 6};
var rectB = {x: 4, y: 10, width: 6, height: 6};
var area = {min: 0, max: 100};

var overlap = rectOverlap(rectA,rectB);
alert(&quot;Does rectA and rectB overlap? &quot; + overlap);

if(overlap)
{
if(rectA.y &gt;= rectB.y)
{
if(increaseY(rectA.width, rectB.y + rectB.height, area.max))
{
rectA.y = rectB.y + rectB.height + 1;
}
else if (rectA.y - rectB.height &gt; area.min)
{
rectB.y = rectA.y - rectB.height;
}
}
else
{
if(increaseY(rectB.width, rectA.y + rectA.height, area.max))
{
rectB.y = rectA.y + rectA.height + 1;
}
else if (rectB.y - rectA.height &gt; area.min)
{
rectA.y = rectB.y - rectA.height;
}
}
}

alert(&quot;Does rectA and rectB overlap? &quot; + rectOverlap(rectA,rectB));

function increaseY(width, bottomEdge, Max)
{
return (bottomEdge + width &lt; Max);
}

function valueInRange(value, min, max)
{

return (value &lt;= max) &amp;&amp; (value &gt;= min);

}

function rectOverlap(A, B)
{
var xOverlap = valueInRange(A.x, B.x, B.x + B.width) ||
valueInRange(B.x, A.x, A.x + A.width);

var yOverlap = valueInRange(A.y, B.y, B.y + B.height) ||
valueInRange(B.y, A.y, A.y + A.height);

return xOverlap &amp;&amp; yOverlap;
}

&lt;/script&gt;


</pre>]]></content:encoded>
			<wfw:commentRss>http://jasonrowe.com/2009/03/14/determine-if-two-rectangles-overlap-each-other-in-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SmoothGallery is good stuff</title>
		<link>http://jasonrowe.com/2009/01/18/smoothgallery-is-good-stuff/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=smoothgallery-is-good-stuff</link>
		<comments>http://jasonrowe.com/2009/01/18/smoothgallery-is-good-stuff/#comments</comments>
		<pubDate>Sun, 18 Jan 2009 00:51:37 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://rowejason.com/?p=331</guid>
		<description><![CDATA[I was recently shown the SmoothGallery javascript picture carousel and I really like.  If you need a simple way to display pictures and do some pretty flashy things quickly this is your best bet. I only ran into a few&#8230;]]></description>
			<content:encoded><![CDATA[I was recently shown the<a href="http://smoothgallery.jondesign.net/"> SmoothGallery</a> javascript picture carousel and I really like.  If you need a simple way to display pictures and do some pretty flashy things quickly this is your best bet. I only ran into a few issue.

1) I had to run my JQuery code in no conflict mode. Which is a very simple change.

<pre class="brush: js; ">


var J = jQuery.noConflict();


</pre>

Then replace all your #&#8217;s with J.

2) I had an issue in IE7 where my drop down menu&#8217;s would go behind the SmoothGallery module. It turned out to be a very simple z-index issue and making sure the menu was not absolutely positioned.

Other then those quick things I had a very nice fully functional picture gallery up and running in minutes.  It was also very refreshing to see a library with very simple setup instructions.]]></content:encoded>
			<wfw:commentRss>http://jasonrowe.com/2009/01/18/smoothgallery-is-good-stuff/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JS Memory Leak Tools &amp; Tips</title>
		<link>http://jasonrowe.com/2008/12/23/js-memory-leak-tools-tips/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=js-memory-leak-tools-tips</link>
		<comments>http://jasonrowe.com/2008/12/23/js-memory-leak-tools-tips/#comments</comments>
		<pubDate>Tue, 23 Dec 2008 22:09:06 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://rowejason.com/?p=211</guid>
		<description><![CDATA[Javascript Closure If you refresh the browser or go to another page and the memory doesn&#8217;t go down you probably have a closure. I just ran into a tricky situation where my closure was being caused during the successful call&#8230;]]></description>
			<content:encoded><![CDATA[<strong>Javascript Closure</strong>

If you refresh the browser or go to another page and the memory doesn&#8217;t go down you probably have a closure. I just ran into a tricky situation where my closure was being caused during the successful call back of a JQuery Ajax call.

The function that was leaking looked like this:

<pre class="brush: js; ">


function &lt;span&gt;GetData&lt;/span&gt;(object) {

var requestObject = object.buildRequest()

$.ajax({
type: &quot;POST&quot;,
url: object.url,
dataType: &quot;json&quot;,
data: requestObject,
success: function(jsonResult) {
object.data = jsonResult;
object.DoStuffWithData();
}
});
}

</pre>

The fix was to remove the object from being a local variable.

<pre class="brush: js; ">


function GetData(object) {

var requestObject = object.buildRequest()

$.ajax({
type: &quot;POST&quot;,
url: object.url,
dataType: &quot;json&quot;,
data: requestObject,
success: function(data) {
//On Success Call Data Updater
data.id = object.Id;
DataUpdater(data);
}
});

requestObject = null;
}

function DataUpdater(data) {

//loop through Requesters Array

//must add object to global array before making DataUpdater
for (i = 0; i &lt; Requesters.length; i++) {

var temp = Requesters[i];

var requestId = temp.Id;
var dataId = data.Id;

//Find object with matching id
if (requestId == dataId ) {

temp .data = data;
temp .DoStuffWithData();
}
}

}

</pre>

<strong>Drip
While tracking down this memory leak I decided to try out a memory leak tool called <a href="http://www.outofhanwell.com/ieleak/index.php?title=Main_Page">Drip</a>. Unfortunately, It didn&#8217;t work in my situation. Drip actually started causing memory leaks so it made it hard to tell if I actually fixed the problem or not. I think the reason it doesn&#8217;t work is the script is recursive using setInterval.

<strong></strong><strong></strong><strong>Other Tools</strong>

<strong></strong><strong></strong><strong> </strong>

Setting up IE and Firefox private bytes counters under process in Perfmon is the most reliable way to monitor JS memory leaks.

<strong></strong><strong><a rel="attachment wp-att-313" href="http://rowejason.com/2008/12/23/js-memory-leak-tools-tips/perfmon/"><img class="alignnone size-full wp-image-313" title="perfmon" src="http://rowejason.com/wp-content/uploads/2008/12/perfmon.jpg" alt="perfmon" width="578" height="347" /></a></strong>

Another common gotcha is the FireFox web developer plug-in. Turn that off if you are going to do any JS memory leak testing. It&#8217;s probably a good idea to turn off any IE plug-ins too just incase but I haven&#8217;t ran into any problems with theirs yet.

IE also has a tool bar for JS memory leaks but it didn&#8217;t work in my situation very well.

Some good pages for review:
<ul>
	<li><a href="http://msdn.microsoft.com/en-us/library/bb250448.aspx">Understanding and Solving Internet Explorer Leak Patterns</a></li>
	<li><a href="http://www.javascriptkit.com/javatutors/closuresleak/index.shtml">JavaScript and memory leaks</a></li>
	<li><a href="http://www.codeproject.com/KB/scripting/leakpatterns.aspx">Memory Leakage in Internet Explore</a></li>
	<li><a href="http://javascript.crockford.com/memory/leak.html">http://javascript.crockford.com/memory/leak.html</a></li>
</ul>
<strong></strong><strong><a href="http://www.codeproject.com/KB/scripting/leakpatterns.aspx"></a></strong>]]></content:encoded>
			<wfw:commentRss>http://jasonrowe.com/2008/12/23/js-memory-leak-tools-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>yui number sorting issue</title>
		<link>http://jasonrowe.com/2008/01/17/yui-number-sorting-issue/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=yui-number-sorting-issue</link>
		<comments>http://jasonrowe.com/2008/01/17/yui-number-sorting-issue/#comments</comments>
		<pubDate>Thu, 17 Jan 2008 07:05:31 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://rowejason.com/2008/01/17/yui-number-sorting-issue/</guid>
		<description><![CDATA[So if you are using the YUI DataTable with XHR and numbers you might have noticed the sort is not working correctly. Below is a quick explination and a work around. &#8220;DataTable currently sorts strings, numbers, and dates, but only&#8230;]]></description>
			<content:encoded><![CDATA[So if you are using the YUI DataTable with XHR and numbers you might have noticed the sort is not working correctly. Below is a quick explination and a work around.
<blockquote>&#8220;DataTable currently sorts strings, numbers, and dates, but <em>only when the values are already those types in JavaScript</em>. In contrast, if you get values over the wire (<abbr title="XMLHttpRequest">XHR</abbr>), DataTable will treat all values as strings in those cases. As a workaround, you could implement a custom sort function to do the data type conversion. Data parsed from markup will be typed correctly, and so we’re a part of the way there; before we get out of beta we plan to provide a mechanism so a value can be treated as a specific type regardless of its origin. I can tell you that in the next release (coming soon) we’ll have a hook for you to do your own conversion of data coming over <abbr title="XMLHttpRequest">XHR</abbr>, and in the subsequent release it will do a lot of conversions for you out of the box and also provide a mechanism for you to do more sophisticated custom conversions.&#8221;</blockquote>
Here is the custom sort function I created to fix the issue. It still doesn&#8217;t work well if you have multipage result lists because the string has to go through the function before it becomes an integer. It works well for small lists or where all the results appear right away.

In the snippet below<strong>, &#8220;u&#8221;</strong> would be the variable you  want to get sorting correctly in the data grid results. If you are not familiar with using custom sort functions take a look at the yui data grid docs. It&#8217;s very easy.
<pre>
<pre class="brush: javascript; ">
 

var myFormatStringFix = 

function(elCell, oRecord, oColumn, oData) 

{ 

oRecord._oData.u = parseInt(oRecord._oData.u); 

elCell.innerHTML = oRecord._oData.u; 

} 


</pre></pre>]]></content:encoded>
			<wfw:commentRss>http://jasonrowe.com/2008/01/17/yui-number-sorting-issue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Carousel Control CD Collection</title>
		<link>http://jasonrowe.com/2007/07/18/carousel-control-cd-collection/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=carousel-control-cd-collection</link>
		<comments>http://jasonrowe.com/2007/07/18/carousel-control-cd-collection/#comments</comments>
		<pubDate>Wed, 18 Jul 2007 05:29:00 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[YUI]]></category>

		<guid isPermaLink="false">http://rowejason.com/?p=96</guid>
		<description><![CDATA[I wanted to find an easy way to display a CD collection using javascript instead of flash. While looking around I found a YUI extension called carousel. Here is my small static example of a CD collection. Bill Scott actually&#8230;]]></description>
			<content:encoded><![CDATA[<a href="http://rowejason.com/search"><img style="float: left; margin: 0px 10px 10px 0px; cursor: hand" src="http://www.rowejason.com/uploaded_images/carousel_example-779114.JPG" border="0" alt="snapshot of carousel" /></a> I wanted to find an easy way to display a CD collection using javascript instead of flash. While looking around I found a YUI extension called carousel. Here is my small static example of a CD collection.

Bill Scott actually doesn&#8217;t support this anymore and the YUI team has taken this on. The new version is very clean and I did try it out. I converted my carousel to the new YUI version but later decided to go with a JQuery extention called jCarousel. They both worked but I think I am going to move in the Jquery direction for awhile.
<a href="http://jasonrowe.com/search"> http://jasonrowe.com/search</a>

References:
<a href="http://billwscott.com/carousel/">http://billwscott.com/carousel/</a>

<a href="http://sorgalla.com/projects/jcarousel/">http://sorgalla.com/projects/jcarousel/</a>

<a href="http://developer.yahoo.com/yui/carousel/">http://developer.yahoo.com/yui/carousel/</a>]]></content:encoded>
			<wfw:commentRss>http://jasonrowe.com/2007/07/18/carousel-control-cd-collection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript: Create Advanced Web Applications With Object-Oriented Techniques</title>
		<link>http://jasonrowe.com/2007/05/07/javascript-create-advanced-web-applications-with-object-oriented-techniques/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=javascript-create-advanced-web-applications-with-object-oriented-techniques</link>
		<comments>http://jasonrowe.com/2007/05/07/javascript-create-advanced-web-applications-with-object-oriented-techniques/#comments</comments>
		<pubDate>Mon, 07 May 2007 15:47:00 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://rowejason.com/?p=83</guid>
		<description><![CDATA[I just started working with OOP in javascript and found this article very informative. The section on prototypes was very helpful as I have been digging around in the YUI library lately and found that area confusing. JavaScript: Create Advanced&#8230;]]></description>
			<content:encoded><![CDATA[I just started working with OOP in javascript and found this article very informative. The section on prototypes was very helpful as I have been digging around in the YUI library lately and found that area confusing. <a href="http://msdn.microsoft.com/msdnmag/issues/07/05/javascript/default.aspx">JavaScript: Create Advanced Web Applications With Object-Oriented Techniques</a>

The article has lots of examples if you click on the figure links. Here was a useful one on Inheriting from a Prototype:

<code>
Inheriting from a Prototypefunction GreatDane() { }
var rover = new GreatDane();
var spot = new GreatDane();
GreatDane.prototype.getBreed = function() {
return “Great Dane”;
};</code><code>// Works, even though at this point
// rover and spot are already created.
alert(rover.getBreed());
// this hides getBreed() in GreatDane.prototype
spot.getBreed = function() {
return “Little Great Dane”;
};
alert(spot.getBreed());
// but of course, the change to getBreed
// doesn’t propagate back to GreatDane.prototype
// and other objects inheriting from it,
// it only happens in the spot object
alert(rover.getBreed());

</code>]]></content:encoded>
			<wfw:commentRss>http://jasonrowe.com/2007/05/07/javascript-create-advanced-web-applications-with-object-oriented-techniques/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

