<?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>ElbertF &#187; javascript</title>
	<atom:link href="http://elbertf.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://elbertf.com</link>
	<description>Insights and Updates from a Tech Geek</description>
	<lastBuildDate>Sun, 15 Apr 2012 21:46:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Free transform plugin for Raphaël JS</title>
		<link>http://elbertf.com/2011/12/free-transform-plugin-for-raphael-js/</link>
		<comments>http://elbertf.com/2011/12/free-transform-plugin-for-raphael-js/#comments</comments>
		<pubDate>Sun, 04 Dec 2011 04:35:32 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mit]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[raphaël]]></category>
		<category><![CDATA[svg]]></category>
		<category><![CDATA[vector graphics]]></category>
		<category><![CDATA[vml]]></category>

		<guid isPermaLink="false">http://elbertf.com/?p=992</guid>
		<description><![CDATA[After doing some research for work involving client-side image manipulation I decided to dive into Raphaël, a JavaScript library that simplifies working with vector graphics on the&#160;web. One thing I struggled with was applying multiple transformations to an element. When rotating an object the coordinate plane rotates with it which means dragging no longer works [...]]]></description>
			<content:encoded><![CDATA[<p>After doing some research for work involving client-side image manipulation I decided to dive into <a href="http://raphaeljs.com/">Raphaël</a>, a JavaScript library that simplifies working with vector graphics on the&nbsp;web.</p>
<p>One thing I struggled with was applying multiple transformations to an element. When rotating an object the coordinate plane rotates with it which means dragging no longer works as one might expect (e.g. right becomes up). Raphaël&#8217;s creator <a href="http://dmitry.baranovskiy.com/">Dmitri Baranovskiy</a> indicated this is the <a href="https://github.com/DmitryBaranovskiy/raphael/issues/102#issuecomment-2725489">correct&nbsp;behavior</a>.</p>
<p>I worked out a solution and released it as a plugin. It supports snap-to-grid dragging, scaling and rotating and handles sets. Like Raphaël itself, the plugin is licensed under the <a href="http://www.opensource.org/licenses/mit-license.php">MIT&nbsp;license</a>.</p>
<h4>Source&nbsp;code</h4>
<p><a href="https://github.com/ElbertF/Raphael.FreeTransform">Raphaël.FreeTransform on&nbsp;GitHub</a></p>
<h4>Demo</h4>
<p><a href="http://elbertf.com/raphael/free_transform/">elbertf.com/raphael/free_transform/</a></p>
<h3>See&nbsp;also&hellip;</h3>
<p>These are a couple of other plugins I created&nbsp;recently.</p>
<h4><a href="https://github.com/ElbertF/Raphael.JSON">Raphaël.JSON</a></h4>
<p>Serialize Raphaël elements and&nbsp;back.</p>
<h4><a href="https://github.com/ElbertF/Raphael.Export">Raphaël.Export</a></h4>
<p>Cross-browser solution to export Raphaël elements to <a href="http://www.w3.org/Graphics/SVG/">SVG</a>, works in Internet Explorer 6, 7 and 8. Can be used to convert Raphaël drawings to PNG or PDF server-side using <a href="http://www.imagemagick.org/">ImageMagick</a> or&nbsp;<a href="http://xmlgraphics.apache.org/batik/" title="Batik SVG Toolkit">Batik</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2011/12/free-transform-plugin-for-raphael-js/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Resolve scoping issues in jQuery with anonymous functions</title>
		<link>http://elbertf.com/2011/05/resolve-scoping-issues-in-jquery-with-anonymous-functions/</link>
		<comments>http://elbertf.com/2011/05/resolve-scoping-issues-in-jquery-with-anonymous-functions/#comments</comments>
		<pubDate>Sat, 21 May 2011 05:56:37 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://elbertf.com/?p=943</guid>
		<description><![CDATA[I had a bit of a head-scratching moment recently when I was using jQuery to bind event handlers to elements in a loop. Usually I would just use jQuery&#8217;s .each() method but in this specific case that wasn&#8217;t an option. Consider the following (simplified)&#160;code: 1 2 3 4 5 6 7 8 9 10 11 [...]]]></description>
			<content:encoded><![CDATA[<p>I had a bit of a head-scratching moment recently when I was using <a href="http://jquery.com/">jQuery</a> to bind event handlers to elements in a loop. Usually I would just use jQuery&#8217;s <a href="http://api.jquery.com/each/"><code>.each()</code></a> method but in this specific case that wasn&#8217;t an option. Consider the following (simplified)&nbsp;code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>li id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item-1&quot;</span><span style="color: #339933;">&gt;</span>Item <span style="color: #cc66cc;">1</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>li id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item-2&quot;</span><span style="color: #339933;">&gt;</span>Item <span style="color: #cc66cc;">2</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>li id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item-3&quot;</span><span style="color: #339933;">&gt;</span>Item <span style="color: #cc66cc;">3</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> i <span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#item-'</span> <span style="color: #339933;">+</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>click<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			alert<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'You clicked item '</span> <span style="color: #339933;">+</span> i<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>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></pre></td></tr></table></div>

<p>Against my intuition, clicking any of the three <code>li</code> elements would return return &#8220;You clicked item 4&#8221;. I then realized that <code>i</code> was being read after the for-loop had finished. To solve this, wrap the function in another <a href="http://stackoverflow.com/questions/1140089/how-does-an-anonymous-function-in-javascript-work">anonymous function</a> passing <code>i</code> as an&nbsp;argument:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>li id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item-1&quot;</span><span style="color: #339933;">&gt;</span>Item <span style="color: #cc66cc;">1</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>li id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item-2&quot;</span><span style="color: #339933;">&gt;</span>Item <span style="color: #cc66cc;">2</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>li id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;item-3&quot;</span><span style="color: #339933;">&gt;</span>Item <span style="color: #cc66cc;">3</span><span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> i <span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#item-'</span> <span style="color: #339933;">+</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>click<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				alert<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'You clicked item '</span> <span style="color: #339933;">+</span> i<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>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span>i<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: #000000; font-weight: bold;">&lt;/script&gt;</span></pre></td></tr></table></div>

<p>This way the second <code>i</code> lives in the same scope as the <code>alert()</code> function which will now return the correct&nbsp;value.</p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2011/05/resolve-scoping-issues-in-jquery-with-anonymous-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Tetris</title>
		<link>http://elbertf.com/2009/06/javascript-tetris/</link>
		<comments>http://elbertf.com/2009/06/javascript-tetris/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 02:11:08 +0000</pubDate>
		<dc:creator>ElbertF</dc:creator>
				<category><![CDATA[miscellaneous]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[gpl]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[tetris]]></category>

		<guid isPermaLink="false">http://ElbertF.com/?p=246</guid>
		<description><![CDATA[Because I have nothing better to do with my time I wrote a Tetris game in JavaScript. I know there are plenty out there already but it was fun to make and it brought back some memories (I wrote my first Tetris game in DOS when I was 14). Coincidentally, Tetris just turned&#160;25. I released [...]]]></description>
			<content:encoded><![CDATA[<p>Because I have nothing better to do with my time I wrote a <a href="http://elbertf.com/tetris">Tetris game in JavaScript</a>. I know there are plenty out there already but it was fun to make and it brought back some memories (I wrote my first Tetris game in DOS when I was 14). Coincidentally, <a href="http://tetris.com">Tetris</a> just turned&nbsp;25.</p>
<p>I released it under <a href="http://opensource.org/licenses/gpl-3.0.html">GPL</a>, feel free to grab the code from the source and do whatever you like with&nbsp;it.</p>
<p>Play the game at&nbsp;<a href="http://elbertf.com/tetris">elbertf.com/tetris</a></p>
]]></content:encoded>
			<wfw:commentRss>http://elbertf.com/2009/06/javascript-tetris/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

