<?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>simon hewitt</title>
	<atom:link href="http://sjhewitt.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://sjhewitt.co.uk</link>
	<description>freelance developer of javascript, python and more!</description>
	<lastBuildDate>Sat, 21 Jul 2012 16:57:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>JavaScript Glitch Effect &#8211; glitch.js</title>
		<link>http://sjhewitt.co.uk/2012/07/javascript-glitch-effect-glitch-js/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=javascript-glitch-effect-glitch-js</link>
		<comments>http://sjhewitt.co.uk/2012/07/javascript-glitch-effect-glitch-js/#comments</comments>
		<pubDate>Sun, 15 Jul 2012 18:20:57 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[glitch]]></category>
		<category><![CDATA[ImageGlitcher]]></category>

		<guid isPermaLink="false">http://blog.sjhewitt.co.uk/?p=102</guid>
		<description><![CDATA[At the moment I&#8217;m working on a small project that has a futuristic computer theme. To make it a bit more visually exciting for the users, I wanted to have a transition between the steps of the application, so I thought it would be cool to have some kind of computer glitch effect followed by [...]]]></description>
			<content:encoded><![CDATA[<p>At the moment I&#8217;m working on a small project that has a futuristic computer theme. To make it a bit more visually exciting for the users, I wanted to have a transition between the steps of the application, so I thought it would be cool to have some kind of computer glitch effect followed by a scan line that comes down the screen to reveal the next page.</p>
<p><img alt="glitch.js" src="http://sjhewitt.github.com/glitch.js/images/example.png" title="glitch.js" class="aligncenter" width="600" height="82" /></p>
<p><img alt="" src="http://sjhewitt.github.com/glitch.js/images/lena-glitch.png" title="Lena Glitched" class="aligncenter" width="600" height="599" /></p>
<p><span id="more-102"></span></p>
<p>tldr: I&#8217;ve created a bit of JavaScript that applies a glitch effect transition to elements within a web page. Check out the <a href="http://www.github.com/sjhewitt/glitch.js/" title="glitch.js on GitHub">source code on GitHub</a>, go to <a href="http://sjhewitt.github.com/glitch.js/examples.html" title="glitch.js Examples" target="_blank">the examples</a> to see it in action, or skip to the <a href="#usage">downloads and usage</a></p>
<h1>ImageGlitcher</h1>
<p>With a bit of googling, I found <a href="http://www.airtightinteractive.com/2011/02/glitch-your-images-with-imageglitcher/">ImageGlitcher</a> by <a href="http://www.airtightinteractive.com">Felix Turner</a>. This takes an image, puts it into an HTML5 canvas, and applies some filters to create the glitched effect. Looking at the source code, you can see that it achieves the effect via the following steps:</p>
<ol>
<li>Offset random slices of the image</li>
<li>Offset one of the color channels</li>
<li>Apply a matrix filter to lighten the image</li>
<li>Overlay black &#8216;scan lines&#8217; to give that crt monitor effect</li>
</ol>
<p>This creates exactly the effect I&#8217;m looking for, but it has 2 problems:</p>
<h2>Only Images are Supported</h2>
<p>In my application, I want to be able to apply the glitch to the actual web page, not just an image&#8230; Easy, you might think, just call <code class="codecolorer javascript default"><span class="javascript">canvas.<span style="color: #660066;">drawWindow</span></span></code> on the with the current window and you can fill the canvas up with the rendered page. Unfortunately not! It seems that there are a load of security issues with this, so it&#8217;s restricted to authorized extensions only. Imagine if a webpage could insert an iframe containing your personal details from Facebook, or your bank, screenshot it, and upload it to a server.</p>
<p>This is where the awesome <a href="http://html2canvas.hertzen.com/">html2canvas</a> project comes in. It&#8217;s a JavaScript library that can render an element into a canvas by inspecting the contents of the DOM. It doesn&#8217;t support all css effects, and can&#8217;t capture the contents of any iframes on the page, but for my application it works perfectly. Plus, the limited rendering may actually add to the glitched effect I&#8217;m creating.</p>
<p>The API is pretty simple, just include the script on the page, and call the html2canvas function with callback (it has to load any images that are within the element so needs to be an asynchronous function.)</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">html2canvas<span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#targetElement&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; onrendered<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>canvas<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// the element has been rendered into the HTMLCanvasElement</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;body&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>canvas<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h2>Rendering is Quite Slow</h2>
<p>The second problem I came across was that the ImageGlitcher implementation seems to be quite slow! It&#8217;s fine for just creating one off images, but I want the effect to work as a transition between pages, so every millisecond of rendering counts. When I tried the ImageGlitcher code on an 840&#215;840 canvas element that I&#8217;m going to apply the transition to, it took about 10 seconds to create the glitched image. This is far too long, so I jumped into the Chrome Profiler tab and started looking for the cause.</p>
<p>It turns out that ImageGlitcher uses a library called BitmapData, a port of the Flash BitmapData class to JavaScript. While this makes applying the required effects quite simple, it does a lot of extra work that isn&#8217;t required. Take this bit of ImageGlitcher code as an example:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">//Add Scan Lines</span><br />
<span style="color: #003366; font-weight: bold;">var</span> line <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Rectangle<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> iw<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> ih<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">%</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; line.<span style="color: #660066;">y</span> <span style="color: #339933;">=</span> i<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; outputBMD.<span style="color: #660066;">fillRect</span><span style="color: #009900;">&#40;</span>line<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>It looks fairly innocuous; just iterate over the height of the image and fill every other line with black. However <code class="codecolorer javascript default"><span class="javascript">BitmapData.<span style="color: #660066;">fillRect</span></span></code> does the following:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">context</span>.<span style="color: #660066;">putImageData</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">imagedata</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> rgb <span style="color: #339933;">=</span> hexToRGB<span style="color: #009900;">&#40;</span>color<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">context</span>.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;rgb(&quot;</span><span style="color: #339933;">+</span>rgb.<span style="color: #660066;">r</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;,&quot;</span><span style="color: #339933;">+</span>rgb.<span style="color: #660066;">g</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;,&quot;</span><span style="color: #339933;">+</span>rgb.<span style="color: #660066;">b</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">context</span>.<span style="color: #660066;">fillRect</span> <span style="color: #009900;">&#40;</span>rect.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> rect.<span style="color: #660066;">y</span><span style="color: #339933;">,</span> rect.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> rect.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">imagedata</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">context</span>.<span style="color: #660066;">getImageData</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</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;">canvas</span>.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">canvas</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>As you can see, it will call <code class="codecolorer javascript default"><span class="javascript">context.<span style="color: #660066;">putImageData</span></span></code>, <code class="codecolorer javascript default"><span class="javascript">hexToRGB</span></code> and <code class="codecolorer javascript default"><span class="javascript">context.<span style="color: #660066;">getImageData</span></span></code> on each iteration of the loop. By removing the BitmapData class and working directly on the canvas context, we go from a few seconds to add the scan lines to under 10 ms!</p>
<h1>Glitching a DOM element</h1>
<p>So, the first step to creating a glitched version of a dom element is creating a canvas containing an image of the dom element we want to glitch using html2canvas:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// render the element onto a canvas</span><br />
html2canvas<span style="color: #009900;">&#40;</span>el<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; onrendered<span style="color: #339933;">:</span> _glitch<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Then we can write a function to actually apply the glitch effect (the <code class="codecolorer javascript default"><span class="javascript">_glitch</span></code> function referenced in the previous snippet.) The full source code is on GitHub, but I&#8217;ll outline the main steps here:</p>
<p>Offset some slices of the image:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// create a temporary canvas to hold the image we're working on</span><br />
<span style="color: #003366; font-weight: bold;">var</span> tempCanvas <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;canvas&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; tempCtx <span style="color: #339933;">=</span> tempCanvas.<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><br />
<br />
<span style="color: #006600; font-style: italic;">// randomly offset slices horizontally</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> numberOfSlices<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// pick a random y coordinate to slice at</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> y <span style="color: #339933;">=</span> getRandInt<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> canvas.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// pick a random height of the slice</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> chunkHeight <span style="color: #339933;">=</span> Math.<span style="color: #660066;">min</span><span style="color: #009900;">&#40;</span>getRandInt<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> canvas.<span style="color: #660066;">h</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> canvas.<span style="color: #660066;">height</span> <span style="color: #339933;">-</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// pick a random horizontal distance to offset the slice</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #339933;">=</span> getRandInt<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> maxOffset<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// calculate the width of the slice given the offset</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> chunkWidth <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">width</span> <span style="color: #339933;">-</span> x<span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// draw the first part of the chunk</span><br />
&nbsp; tempCtx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>canvas<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> chunkWidth<span style="color: #339933;">,</span> chunkHeight<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; x<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> chunkWidth<span style="color: #339933;">,</span> chunkHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// draw the rest</span><br />
&nbsp; tempCtx.<span style="color: #660066;">drawImage</span><span style="color: #009900;">&#40;</span>canvas<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; chunkWidth<span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> chunkHeight<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> y<span style="color: #339933;">,</span> x<span style="color: #339933;">,</span> chunkHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Offset one of the color channels:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// get the ImageData from the original canvas</span><br />
<span style="color: #003366; font-weight: bold;">var</span> srcData <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'2d'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getImageData</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> canvas.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> canvas.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">data</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// get hold of the ImageData for the working image</span><br />
<span style="color: #003366; font-weight: bold;">var</span> targetImageData <span style="color: #339933;">=</span> tempCtx.<span style="color: #660066;">getImageData</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> canvas.<span style="color: #660066;">width</span><span style="color: #339933;">,</span> canvas.<span style="color: #660066;">height</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// and get a local reference to the rgba data array</span><br />
<span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> targetImageData.<span style="color: #660066;">data</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// Copy a random color channel from the original image into</span><br />
<span style="color: #006600; font-style: italic;">// the working canvas, offsetting it by a random amount</span><br />
<span style="color: #006600; font-style: italic;">//</span><br />
<span style="color: #006600; font-style: italic;">// ImageData arrays are a single dimension array that contains</span><br />
<span style="color: #006600; font-style: italic;">// 4 values for each pixel (r,g,b,a).</span><br />
<span style="color: #006600; font-style: italic;">// so, by initializing `i` to a random number between 0 and 2,</span><br />
<span style="color: #006600; font-style: italic;">// and incrementing by 4 on each iteration, we can replace only</span><br />
<span style="color: #006600; font-style: italic;">// a single channel in the image</span><br />
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> getRandInt<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> _len <span style="color: #339933;">=</span> srcData.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> _len<span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; data<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span>channelOffset<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> srcData<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Brighten the image:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// Make the image brighter by doubling the rgb values</span><br />
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> _len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; data<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span><br />
&nbsp; data<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span><br />
&nbsp; data<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">*=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>And, finally, create scan lines by filling alternate lines with black:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">// copy the tweaked ImageData back into the context</span><br />
tempCtx.<span style="color: #660066;">putImageData</span><span style="color: #009900;">&#40;</span>targetImageData<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// add scan lines</span><br />
tempCtx.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;rgb(0,0,0)&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> canvas.<span style="color: #660066;">height</span><span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; tempCtx.<span style="color: #660066;">fillRect</span> <span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> w<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Now that&#8217;s done, <code class="codecolorer javascript default"><span class="javascript">tempCanvas</span></code> contains the glitched images and it&#8217;s pretty trivial to write a transition that overlays the glitched canvas and removes it via some effect. Check out the source if you&#8217;re interested!</p>
<h1 id="usage">Usage and Examples</h1>
<p>You can download the library from the <a href="http://www.github.com/sjhewitt/glitch.js" target="_blank">GitHub Repo</a>, and see <a href="http://sjhewitt.github.com/glitch.js/examples.html">some examples</a> on GitHub Pages.</p>
<p>It requires the <a href="http://html2canvas.hertzen.com/" target="_blank">html2canvas library</a> and, if you want to use the transition effect, a fairly recent jQuery.</p>
<p>To create a glitched version of an element, call the <code class="codecolorer javascript default"><span class="javascript">glitch</span></code> function:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">glitch<span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;currentContent&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; amount<span style="color: #339933;">:</span> <span style="color: #CC0000;">8</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; complete<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>canvas<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// do something with the glitched canvas</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// jQuery plugin version</span><br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#currentContent&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">glitch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; amount<span style="color: #339933;">:</span> <span style="color: #CC0000;">8</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; complete<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>canvas<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// do something with the glitched canvas</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>To replace an element with a glitched version of it, use <code class="codecolorer javascript default"><span class="javascript">glitch.<span style="color: #660066;">replace</span></span></code> or just call <code class="codecolorer text default"><span class="text">$.fn.glitch</span></code> with no arguments:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">glitch.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;currentContent&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// jQuery version</span><br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#currentContent&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">glitch</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>If you want to create a transition between two bits of content, jQuery is required to perform the animation:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#currentContent&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">glitch</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;div&gt;&lt;p&gt;New Content&lt;/p&gt;&lt;/div&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; amount<span style="color: #339933;">:</span> <span style="color: #CC0000;">7</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; effect<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;slide&quot;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; complete<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// do something when the animation is complete</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>A few things to be aware of:</p>
<p>As html2canvas has to iterate over the contents of the element when rendering it to the canvas, it stands to reason that the more complex the DOM, the longer the rendering will take. This is especially true of any images that are attached (including background images,) as they have to be re-loaded asynchronously by the browser. If there are any images from another domain, then they have to be loaded via a proxy to avoid tainting the canvas. So, it&#8217;s wise to remove any unneeded elements from the dom before applying the effect.</p>
<p>html2canvas scrolls the window to the top when rendering the page (see <a href="https://github.com/niklasvh/html2canvas/issues/57" target="_blank">Issue 57</a>) which is fine for my usage, but may not be ideal for you. There are 2 answers for this: store the scroll position before calling glitch, and set it back in the complete handler, or contribute a fix to the html2canvas project (I&#8217;m working on a fix on <a href="https://github.com/sjhewitt/html2canvas/tree/no-scroll" target="_blank">my fork</a>!)</p>
<p>In the project i&#8217;m using this on the effect is triggered via a delegated click event handler (attached to a Backbone.js view,) which seems to create an error in chrome to do with the event listeners that were attached. To get around this, I deferred the call to the glitch function to the next tick.</p>
]]></content:encoded>
			<wfw:commentRss>http://sjhewitt.co.uk/2012/07/javascript-glitch-effect-glitch-js/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Positioning Dialogs in Facebook Canvas Applications</title>
		<link>http://sjhewitt.co.uk/2011/05/positioning-dialogs-in-facebook-canvas-applications/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=positioning-dialogs-in-facebook-canvas-applications</link>
		<comments>http://sjhewitt.co.uk/2011/05/positioning-dialogs-in-facebook-canvas-applications/#comments</comments>
		<pubDate>Mon, 30 May 2011 18:43:26 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Canvas]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://blog.sjhewitt.co.uk/?p=62</guid>
		<description><![CDATA[Facebook have recently (very quietly) added the FB.Canvas.getPageInfo and FB.Canvas.scrollTo methods to their JavaScript SDK. These used to exist in their old SDK, but it&#8217;s taken a year for it to appear in the new one! This is good news to me for the following reason. If you wanted to create a message dialog popup [...]]]></description>
			<content:encoded><![CDATA[<p>Facebook have recently (very quietly) added the <a href="http://developers.facebook.com/docs/reference/javascript/FB.Canvas.getPageInfo/">FB.Canvas.getPageInfo</a> and <a href="http://developers.facebook.com/docs/reference/javascript/FB.Canvas.scrollTo/">FB.Canvas.scrollTo</a> methods to their JavaScript SDK. These used to exist in their old SDK, but it&#8217;s taken a year for it to appear in the new one! This is good news to me for the following reason.<br />
<span id="more-62"></span></p>
<p>If you wanted to create a message dialog popup in an application, you had no guarantee that it would be visible on the screen when it was shown as you have no idea where the user&#8217;s viewport is. Most JavaScript popup libraries try to position the dialog in the centre of the screen by looking at the window&#8217;s scroll position. Unfortunately when creating a Facebook Application you&#8217;re operating inside a cross-domain iframe so that information isn&#8217;t directly available, the best you could do is put it somewhere near where the user clicked.</p>
<p>With the introduction of the getPageInfo method, we can now access it and start positioning things where the user can see them. For example, to position a div at the top of the viewport using jQuery, try this:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> pageInfo <span style="color: #339933;">=</span> FB.<span style="color: #660066;">Canvas</span>.<span style="color: #660066;">getPageInfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #003366; font-weight: bold;">var</span> offset <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>pageInfo.<span style="color: #660066;">scrollTop</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> parseInt<span style="color: #009900;">&#40;</span>pageInfo.<span style="color: #660066;">offsetTop</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#element&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// use Math.max so that the element isn't positioned above the top of the page</span><br />
&nbsp; &nbsp; top<span style="color: #339933;">:</span> Math.<span style="color: #660066;">max</span><span style="color: #009900;">&#40;</span>offset<span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>You might have noticed that I&#8217;ve surrounded the page info properties with the parseInt function. This is because they are stored by the SDK as strings so must be parsed before you can use them in mathematical operations.</p>
<h2><del datetime="2012-07-21T16:52:19+00:00">Bonus: canvas.pageInfoChange Event</del></h2>
<p><del datetime="2012-07-21T16:52:19+00:00">This isn&#8217;t documented on the Facebook Developer website, so may be change without warning, however while poking around the SDK code for pageInfo, I noticed a pageInfoChange event gets dispatched after the pageInfo object is updated. This lets us listen for when the user scrolls the browser window and update the display accordingly, for example by keeping the dialog window in the center of the user&#8217;s screen.</del></p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">FB.<span style="color: #660066;">Event</span>.<span style="color: #660066;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;canvas.pageInfoChange&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#dialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; top<span style="color: #339933;">:</span> Math.<span style="color: #660066;">max</span><span style="color: #009900;">&#40;</span>parseInt<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">scrollTop</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> parseInt<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">offsetTop</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>parseInt<span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">clientHeight</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#dialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">outerHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;fast&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Edit: Since writing this, Facebook have now removed the pageInfoChange event. You can create your own version that polls Facebook for the latest canvas data. See <a href="http://stackoverflow.com/questions/11000073/centered-dialog-on-facebook-canvas-app/11001839#11001839" title="Stack Overflow">this question</a> on StackOverflow for more info.</p>
<h2>Final Note</h2>
<p>Because the page info object is updated via cross-domain ajax calls, it isn&#8217;t always up to date, and is only dispatched a couple of times per second, not at the same rate as the equivalent window.onscroll method would be called. Therefore, you can&#8217;t use it to lock an element to a specific position on the screen (emulating position: fixed,) that&#8217;s why I use jQuery animation to update the position of the box in the above example, as it avoids the dialog jumping around the page.</p>
]]></content:encoded>
			<wfw:commentRss>http://sjhewitt.co.uk/2011/05/positioning-dialogs-in-facebook-canvas-applications/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>RobotLegs and as3signals: Injecting interfaces and named arguments</title>
		<link>http://sjhewitt.co.uk/2010/02/robotlegs-and-as3signals-injecting-interfaces-and-named-arguments/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=robotlegs-and-as3signals-injecting-interfaces-and-named-arguments</link>
		<comments>http://sjhewitt.co.uk/2010/02/robotlegs-and-as3signals-injecting-interfaces-and-named-arguments/#comments</comments>
		<pubDate>Sat, 20 Feb 2010 16:10:27 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[RobotLegs]]></category>
		<category><![CDATA[as3signals]]></category>

		<guid isPermaLink="false">http://blog.sjhewitt.co.uk/?p=40</guid>
		<description><![CDATA[I found out about as3signals yesterday and thought it was such a neat way of dealing with events, I decided to spend the weekend refactoring my current project to take advantage of it. I started by adding some signals to my view components to reduce the number of custom events that I was dispatching, and [...]]]></description>
			<content:encoded><![CDATA[<p>I found out about as3signals yesterday and thought it was such a neat way of dealing with events, I decided to spend the weekend refactoring my current project to take advantage of it.</p>
<p>I started by adding some signals to my view components to reduce the number of custom events that I was dispatching, and I now only use custom events to bubble events from ItemRenderers up to the main components. These events are then caught and a Signal is used to forward it to the Mediator. This was going so well that I decided to start integrating the <a href="http://github.com/joelhooks/signals-extensions-CommandSignal">SignalCommandMap</a> developed by <a href="http://joelhooks.com">Joel Hooks</a> to start using signals in the framework.</p>
<p>After a couple of minor problems upgrading to the latest trunk of RobotLegs, I replaced a few of my custom events and everything was going ok until I came across a problem when trying to use a command that has a ListCollectionView argument. It turns out that ListCollectionView will throw an error if you try to access the constructor property because the case is not handled in the proxy_object::getProperty method, and this is required by the SignalCommandMap to map injection values.</p>
<p><span id="more-40"></span></p>
<p>The SignalCommandMap uses the following method to map injector values and execute the Command:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> routeSignalToCommand<span style="color: #000000;">&#40;</span>signal<span style="color: #000066; font-weight: bold;">:</span>ISignal<span style="color: #000066; font-weight: bold;">,</span> valueObjects<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000066; font-weight: bold;">,</span> commandClass<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=class%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:class.html"><span style="color: #004993;">Class</span></a><span style="color: #000066; font-weight: bold;">,</span> oneshot<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=boolean%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:boolean.html"><span style="color: #004993;">Boolean</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #009900; font-style: italic;">// NOTE: Assumes no duplicated classes in valueObjects,</span><br />
<span style="color: #009900; font-style: italic;">// and none of them are previously mapped.</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=object%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:object.html"><span style="color: #004993;">Object</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">value</span> <span style="color: #0033ff; font-weight: bold;">in</span> valueObjects <span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
injector<span style="color: #000066; font-weight: bold;">.</span>mapValue<span style="color: #000000;">&#40;</span> <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">constructor</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">value</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> command<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=object%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:object.html"><span style="color: #004993;">Object</span></a> = injector<span style="color: #000066; font-weight: bold;">.</span>instantiate<span style="color: #000000;">&#40;</span> commandClass <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">value</span> <span style="color: #0033ff; font-weight: bold;">in</span> valueObjects <span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
injector<span style="color: #000066; font-weight: bold;">.</span>unmap<span style="color: #000000;">&#40;</span> <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">constructor</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
command<span style="color: #000066; font-weight: bold;">.</span>execute<span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> oneshot <span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
unmapSignal<span style="color: #000000;">&#40;</span> signal<span style="color: #000066; font-weight: bold;">,</span> commandClass <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<p>As you can see, value.constructor is used to get the class for the argument. This also causes a problem if you are using an interface as the target class for the injection.</p>
<p>There are 2 improvements that I propose to the map:</p>
<ol>
<li>Use the Signal valueClasses property (if it has been defined on the Signal) as the source of the target classes for injection.<br />
This means that there is no need to call the constructor method to get the class for injection, and you can use interfaces as an injection target.</li>
<li>Add a parameter that allows you to give names to the argument injections.<br />
This would allow you to inject multiple objects of the same type into the Command and use named injections to route them to the right properties.</li>
</ol>
<h2>My Updated SignalCommandMap</h2>
<p>To achieve these two improvements, the ISignalCommandMap interface is updated to include an argumentNames parameter that allows you to pass in injection names for the arguments. You can mix named and unnamed injections by using an empty string as the name for the unnamed arguments.</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #9900cc; font-weight: bold;">package</span> org<span style="color: #000066; font-weight: bold;">.</span>robotlegs<span style="color: #000066; font-weight: bold;">.</span>core<br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #0033ff; font-weight: bold;">import</span> org<span style="color: #000066; font-weight: bold;">.</span>osflash<span style="color: #000066; font-weight: bold;">.</span>signals<span style="color: #000066; font-weight: bold;">.</span>ISignal<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #0033ff; font-weight: bold;">public</span> interface ISignalCommandMap<br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #339966; font-weight: bold;">function</span> mapSignal<span style="color: #000000;">&#40;</span>signal<span style="color: #000066; font-weight: bold;">:</span>ISignal<span style="color: #000066; font-weight: bold;">,</span> commandClass<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=class%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:class.html"><span style="color: #004993;">Class</span></a><span style="color: #000066; font-weight: bold;">,</span> oneShot<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=boolean%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:boolean.html"><span style="color: #004993;">Boolean</span></a> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">,</span> argumentNames<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a> = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> mapSignalClass<span style="color: #000000;">&#40;</span>signalClass<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=class%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:class.html"><span style="color: #004993;">Class</span></a><span style="color: #000066; font-weight: bold;">,</span> commandClass<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=class%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:class.html"><span style="color: #004993;">Class</span></a><span style="color: #000066; font-weight: bold;">,</span> oneShot<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=boolean%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:boolean.html"><span style="color: #004993;">Boolean</span></a> = <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">,</span> argumentNames<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a> = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>ISignal<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> hasSignalCommand<span style="color: #000000;">&#40;</span>signal<span style="color: #000066; font-weight: bold;">:</span>ISignal<span style="color: #000066; font-weight: bold;">,</span> commandClass<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=class%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:class.html"><span style="color: #004993;">Class</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=boolean%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:boolean.html"><span style="color: #004993;">Boolean</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> unmapSignal<span style="color: #000000;">&#40;</span>signal<span style="color: #000066; font-weight: bold;">:</span>ISignal<span style="color: #000066; font-weight: bold;">,</span> commandClass<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=class%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:class.html"><span style="color: #004993;">Class</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> unmapSignalClass<span style="color: #000000;">&#40;</span>signalClass<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=class%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:class.html"><span style="color: #004993;">Class</span></a><span style="color: #000066; font-weight: bold;">,</span> commandClass<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=class%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:class.html"><span style="color: #004993;">Class</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Then the routeSignalToCommand function is updated to include the argumentNames and use some helper functions to map and unmap the signal argument values</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> routeSignalToCommand<span style="color: #000000;">&#40;</span>signal<span style="color: #000066; font-weight: bold;">:</span>ISignal<span style="color: #000066; font-weight: bold;">,</span> valueObjects<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000066; font-weight: bold;">,</span> commandClass<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=class%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:class.html"><span style="color: #004993;">Class</span></a><span style="color: #000066; font-weight: bold;">,</span> oneshot<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=boolean%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:boolean.html"><span style="color: #004993;">Boolean</span></a><span style="color: #000066; font-weight: bold;">,</span> argumentNames<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><br />
<span style="color: #000000;">&#123;</span><br />
mapValues<span style="color: #000000;">&#40;</span> valueObjects<span style="color: #000066; font-weight: bold;">,</span> signal<span style="color: #000066; font-weight: bold;">.</span>valueClasses<span style="color: #000066; font-weight: bold;">,</span> argumentNames <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> command<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=object%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:object.html"><span style="color: #004993;">Object</span></a> = injector<span style="color: #000066; font-weight: bold;">.</span>instantiate<span style="color: #000000;">&#40;</span> commandClass <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
unmapValues<span style="color: #000000;">&#40;</span> valueObjects<span style="color: #000066; font-weight: bold;">,</span> signal<span style="color: #000066; font-weight: bold;">.</span>valueClasses<span style="color: #000066; font-weight: bold;">,</span> argumentNames <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
command<span style="color: #000066; font-weight: bold;">.</span>execute<span style="color: #000000;">&#40;</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> oneshot <span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
unmapSignal<span style="color: #000000;">&#40;</span> signal<span style="color: #000066; font-weight: bold;">,</span> commandClass <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<p>And then the helper functions that do the work of mapping the value objects to the injected properties. Errors are thrown if valueClasses is used and the correct number of arguments isn&#8217;t supplied.</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> mapValues<span style="color: #000000;">&#40;</span>valueObjects<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000066; font-weight: bold;">,</span> valueClasses<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000066; font-weight: bold;">,</span> argumentNames<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=object%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:object.html"><span style="color: #004993;">Object</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>valueClasses <span style="color: #000066; font-weight: bold;">&amp;</span>amp<span style="color: #000066; font-weight: bold;">;&amp;</span>amp<span style="color: #000066; font-weight: bold;">;</span> argumentNames<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>valueObjects<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> <span style="color: #000066; font-weight: bold;">!</span>= valueClasses<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> <span style="color: #000066; font-weight: bold;">||</span> valueObjects<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> <span style="color: #000066; font-weight: bold;">!</span>= argumentNames<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #0033ff; font-weight: bold;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=error%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:error.html"><span style="color: #004993;">Error</span></a><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Unequal numbers of arguments&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span>i=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>i<br />
<span style="color: #000000;">&#123;</span><br />
injector<span style="color: #000066; font-weight: bold;">.</span>mapValue<span style="color: #000000;">&#40;</span> valueClasses<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">,</span> valueObjects<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">,</span> argumentNames<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>valueClasses<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>valueObjects<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> <span style="color: #000066; font-weight: bold;">!</span>= valueClasses<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #0033ff; font-weight: bold;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <a href="http://www.google.com/search?q=error%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:error.html"><span style="color: #004993;">Error</span></a><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Unequal numbers of arguments&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span>i=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>i<br />
<span style="color: #000000;">&#123;</span><br />
injector<span style="color: #000066; font-weight: bold;">.</span>mapValue<span style="color: #000000;">&#40;</span> valueClasses<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">,</span> valueObjects<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #0033ff; font-weight: bold;">else</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">value</span> <span style="color: #0033ff; font-weight: bold;">in</span> valueObjects <span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
injector<span style="color: #000066; font-weight: bold;">.</span>mapValue<span style="color: #000000;">&#40;</span> <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">constructor</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">value</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> unmapValues<span style="color: #000000;">&#40;</span>valueObjects<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000066; font-weight: bold;">,</span> valueClasses<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000066; font-weight: bold;">,</span> argumentNames<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=int%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:int.html"><span style="color: #004993;">int</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=object%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:object.html"><span style="color: #004993;">Object</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>valueClasses <span style="color: #000066; font-weight: bold;">&amp;</span>amp<span style="color: #000066; font-weight: bold;">;&amp;</span>amp<span style="color: #000066; font-weight: bold;">;</span> argumentNames<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span>i=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>i<br />
<span style="color: #000000;">&#123;</span><br />
injector<span style="color: #000066; font-weight: bold;">.</span>unmap<span style="color: #000000;">&#40;</span> valueClasses<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">,</span> argumentNames<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>valueClasses<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span>i=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>i<br />
<span style="color: #000000;">&#123;</span><br />
injector<span style="color: #000066; font-weight: bold;">.</span>unmap<span style="color: #000000;">&#40;</span> valueClasses<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #0033ff; font-weight: bold;">else</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span><span style="color: #000000;">&#40;</span> <span style="color: #004993;">value</span> <span style="color: #0033ff; font-weight: bold;">in</span> valueObjects <span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
injector<span style="color: #000066; font-weight: bold;">.</span>unmap<span style="color: #000000;">&#40;</span> <span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">constructor</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now, for a Signal like this:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> InjectionTestSignal <span style="color: #0033ff; font-weight: bold;">extends</span> Signal<br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> InjectionTestSignal <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html"><span style="color: #004993;">String</span></a><span style="color: #000066; font-weight: bold;">,</span> <a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html"><span style="color: #004993;">String</span></a><span style="color: #000066; font-weight: bold;">,</span> IList<span style="color: #000066; font-weight: bold;">,</span> IList<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<p>And this command:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> InjectionTestCommand <span style="color: #0033ff; font-weight: bold;">extends</span> SignalCommand<br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #000000;">&#91;</span>Inject<span style="color: #000000;">&#40;</span><span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;string1&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> string1<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html"><span style="color: #004993;">String</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #000000;">&#91;</span>Inject<span style="color: #000000;">&#40;</span><span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;string2&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> string2<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=string%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:string.html"><span style="color: #004993;">String</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #000000;">&#91;</span>Inject<span style="color: #000000;">&#40;</span><span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;list1&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> list1<span style="color: #000066; font-weight: bold;">:</span>IList<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #000000;">&#91;</span>Inject<span style="color: #000000;">&#40;</span><span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;list2&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> list2<span style="color: #000066; font-weight: bold;">:</span>IList<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
override <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> execute<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #009900; font-style: italic;">// Do something...</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<p>You can create the mapping as follows:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">signalCommandMap<span style="color: #000066; font-weight: bold;">.</span>mapSignalClass<span style="color: #000000;">&#40;</span>InjectionTestSignal<span style="color: #000066; font-weight: bold;">,</span> InjectionTestCommand<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">false</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;string1&quot;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #990000;">&quot;string2&quot;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #990000;">&quot;list1&quot;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #990000;">&quot;list2&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<p>You can download my <a href="http://blog.sjhewitt.co.uk/wp-content/uploads/2010/02/robotlegs-signals.zip">updated SignalCommandMap here</a> and a very very <a href="http://blog.sjhewitt.co.uk/wp-content/uploads/2010/02/robotlegs-signals-example.zip">simple example here</a></p>
<p>I&#8217;ve also forked CommandSignal on <a href="http://github.com/sjhewitt/signals-extensions-CommandSignal">github</a></p>
<p>I will try to add some tests and better examples when I have a bit more time.</p>
]]></content:encoded>
			<wfw:commentRss>http://sjhewitt.co.uk/2010/02/robotlegs-and-as3signals-injecting-interfaces-and-named-arguments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changing the Background Alpha in Spark ItemRenderers</title>
		<link>http://sjhewitt.co.uk/2010/02/changing-the-background-alpha-in-spark-itemrenderers/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=changing-the-background-alpha-in-spark-itemrenderers</link>
		<comments>http://sjhewitt.co.uk/2010/02/changing-the-background-alpha-in-spark-itemrenderers/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 10:37:14 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Spark]]></category>
		<category><![CDATA[Flex 4]]></category>
		<category><![CDATA[ItemRenderer]]></category>

		<guid isPermaLink="false">http://blog.sjhewitt.co.uk/?p=31</guid>
		<description><![CDATA[The ItemRenderer in the Flex 4 Spark components provides the useful ability to automatically draw the backgrounds of the item renderers, however it is limited to only drawing them with an alpha of 1 or 0. By extending the ItemRenderer class and overriding the mx_internal drawBackground() method, we are able to set the fill alpha [...]]]></description>
			<content:encoded><![CDATA[<p>The ItemRenderer in the Flex 4 Spark components provides the useful ability to automatically draw the backgrounds of the item renderers, however it is limited to only drawing them with an alpha of 1 or 0.</p>
<p>By extending the ItemRenderer class and overriding the mx_internal drawBackground() method, we are able to set the fill alpha to a value retrieved from the &#8220;backgoundAlpha&#8221; style. To do this, I have simply copied the drawBackground method from ItemRenderer and added a call to getStyle(&#8220;backgroundAlpha&#8221;) in the appropriate place.</p>
<p><span id="more-31"></span></p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #9900cc; font-weight: bold;">package</span> uk<span style="color: #000066; font-weight: bold;">.</span>co<span style="color: #000066; font-weight: bold;">.</span>tangentlabs<span style="color: #000066; font-weight: bold;">.</span>hyperbola<span style="color: #000066; font-weight: bold;">.</span>components<br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>core<span style="color: #000066; font-weight: bold;">.</span>mx_internal<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #0033ff; font-weight: bold;">import</span> spark<span style="color: #000066; font-weight: bold;">.</span>components<span style="color: #000066; font-weight: bold;">.</span>ResizeMode<span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #0033ff; font-weight: bold;">import</span> spark<span style="color: #000066; font-weight: bold;">.</span>components<span style="color: #000066; font-weight: bold;">.</span>supportClasses<span style="color: #000066; font-weight: bold;">.</span>ItemRenderer<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
use <span style="color: #004993;">namespace</span> mx_internal<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #000000;">&#91;</span>Style<span style="color: #000000;">&#40;</span><span style="color: #004993;">name</span>=<span style="color: #990000;">&quot;backgroundAlpha&quot;</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">type</span>=<span style="color: #990000;">&quot;Number&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
<br />
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> CustomItemRenderer <span style="color: #0033ff; font-weight: bold;">extends</span> ItemRenderer<br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> CustomItemRenderer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
override mx_internal <span style="color: #339966; font-weight: bold;">function</span> drawBackground<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #009900; font-style: italic;">// if autoDrawBackground is set to true, we always</span><br />
<span style="color: #009900; font-style: italic;">// draw a background and don't need to worry about mouseEnabledWhereTransparent.</span><br />
<span style="color: #009900; font-style: italic;">// However, if it's false, then we should just let super.drawBackground()</span><br />
<span style="color: #009900; font-style: italic;">// do its job.</span><br />
<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">!</span>autoDrawBackground<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000066; font-weight: bold;">.</span>drawBackground<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #0033ff; font-weight: bold;">return</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// TODO (rfrishbe): Would be good to remove this duplicate code with the</span><br />
<span style="color: #009900; font-style: italic;">// super.drawBackground() version</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> w<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000;">&#40;</span>resizeMode == ResizeMode<span style="color: #000066; font-weight: bold;">.</span>SCALE<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">?</span> measuredWidth <span style="color: #000066; font-weight: bold;">:</span> unscaledWidth<span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> h<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #000000;">&#40;</span>resizeMode == ResizeMode<span style="color: #000066; font-weight: bold;">.</span>SCALE<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">?</span> measuredHeight <span style="color: #000066; font-weight: bold;">:</span> unscaledHeight<span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #004993;">isNaN</span><span style="color: #000000;">&#40;</span>w<span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">||</span> <span style="color: #004993;">isNaN</span><span style="color: #000000;">&#40;</span>h<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #0033ff; font-weight: bold;">return</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">clear</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">backgroundColor</span><span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=uint%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:uint.html"><span style="color: #004993;">uint</span></a><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> backgroundAlpha<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=number%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:number.html"><span style="color: #004993;">Number</span></a> = <span style="color: #004993;">getStyle</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;backgroundAlpha&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>selected<span style="color: #000000;">&#41;</span><br />
<span style="color: #004993;">backgroundColor</span> = <span style="color: #004993;">getStyle</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;selectionColor&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #0033ff; font-weight: bold;">else</span> <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>hovered<span style="color: #000000;">&#41;</span><br />
<span style="color: #004993;">backgroundColor</span> = <span style="color: #004993;">getStyle</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;rollOverColor&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #0033ff; font-weight: bold;">else</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> alternatingColors<span style="color: #000066; font-weight: bold;">:</span><a href="http://www.google.com/search?q=array%20inurl:http://livedocs.adobe.com/flex/201/langref/%20inurl:array.html"><span style="color: #004993;">Array</span></a> = <span style="color: #004993;">getStyle</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;alternatingItemColors&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>alternatingColors <span style="color: #000066; font-weight: bold;">&amp;</span>amp<span style="color: #000066; font-weight: bold;">;&amp;</span>amp<span style="color: #000066; font-weight: bold;">;</span> alternatingColors<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> <span style="color: #000066; font-weight: bold;">&amp;</span>gt<span style="color: #000066; font-weight: bold;">;</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #009900; font-style: italic;">// translate these colors into uints</span><br />
styleManager<span style="color: #000066; font-weight: bold;">.</span>getColorNames<span style="color: #000000;">&#40;</span>alternatingColors<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #004993;">backgroundColor</span> = alternatingColors<span style="color: #000000;">&#91;</span>itemIndex <span style="color: #000066; font-weight: bold;">%</span> alternatingColors<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #0033ff; font-weight: bold;">else</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #009900; font-style: italic;">// don't draw background if it is the contentBackgroundColor. The</span><br />
<span style="color: #009900; font-style: italic;">// list skin handles the background drawing for us.</span><br />
backgroundAlpha = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">backgroundColor</span><span style="color: #000066; font-weight: bold;">,</span> backgroundAlpha<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>showsCaret<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">getStyle</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;selectionColor&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">drawRect</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0.5</span><span style="color: #000066; font-weight: bold;">,</span> w<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span> h<span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #0033ff; font-weight: bold;">else</span><br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineStyle</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">drawRect</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> w<span style="color: #000066; font-weight: bold;">,</span> h<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<br />
<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Note that this will always use the same backgroundAlpha for all states, but could be extended to add &#8220;backgoundAlphaOver&#8221; and &#8220;backgroundAlphaSelected&#8221; styles that will be used in the appropriate states.</p>
]]></content:encoded>
			<wfw:commentRss>http://sjhewitt.co.uk/2010/02/changing-the-background-alpha-in-spark-itemrenderers/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Flex 4 Autocomplete TextInput</title>
		<link>http://sjhewitt.co.uk/2010/02/flex-4-autocomplete-textinput/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=flex-4-autocomplete-textinput</link>
		<comments>http://sjhewitt.co.uk/2010/02/flex-4-autocomplete-textinput/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 15:54:16 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Spark]]></category>

		<guid isPermaLink="false">http://blog.sjhewitt.co.uk/?p=28</guid>
		<description><![CDATA[Ivan Tenger over at flashcommander has posted a very useful AutoComplete component using the Spark component architecture. Click here to have a look.]]></description>
			<content:encoded><![CDATA[<p><span style="padding: 0px; margin: 0px;">Ivan Tenger over at <a href="http://flashcommander.org/">flashcommander</a> has posted a very useful AutoComplete component using the Spark component architecture.</span></p>
<p><span style="padding: 0px; margin: 0px;"><a href="http://flashcommander.org/flex/flex-4-autocomplete">Click here</a> to have a look.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://sjhewitt.co.uk/2010/02/flex-4-autocomplete-textinput/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Autocomplete broken in Flash Builder Beta 2 with nightly builds</title>
		<link>http://sjhewitt.co.uk/2010/01/autocomplete-broken-in-flash-builder-beta-2-with-nightly-builds/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=autocomplete-broken-in-flash-builder-beta-2-with-nightly-builds</link>
		<comments>http://sjhewitt.co.uk/2010/01/autocomplete-broken-in-flash-builder-beta-2-with-nightly-builds/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 12:33:51 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Flash Builder]]></category>

		<guid isPermaLink="false">http://sjhewitt.co.uk/?p=24</guid>
		<description><![CDATA[I&#8217;m using a nightly build (currently 13493) of the Flex SDK to build my current due to some bug fixes that my project needed (there was a problem with the [Managed] decorator not working). However, the new builds broke the autocomplete functionality in Flash Builder. I was able put up with it for a week. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m using a nightly build (currently 13493) of the Flex SDK to build my current due to some bug fixes that my project needed (there was a problem with the [Managed] decorator not working). However, the new builds broke the autocomplete functionality in Flash Builder.</p>
<p>I was able put up with it for a week. The lack of autocomplete was manageable, it was the fact that it also broke the import organisation that was infuriating. For example, I have a custom event class that extends flash.events.Event. I had to manually insert the &#8216;import flash.events.Event&#8217; line into the file, but every time I used autocomplete for a valid class, Flash Builder would delete the import because it couldn&#8217;t find the Event class in any library.<span id="more-24"></span></p>
<p>I spent quite a while googling to find an answer to the problem but came up empty handed. So I started having a root around the sdk, comparing it with the build shipped with Beta 2. In new builds, the flex-config.xml file contains reference to the targetPlayerMinorVersion variable . It seems that Flash Builder is unable to handle this, so could not resolve the playerglobal.swc library containing all of the base classes.</p>
<p>After finding this, one quick search led me to <a href="http://forums.adobe.com/thread/559312" target="_blank">a post</a> on the Adobe forums that contained a description of the problem and the answer:</p>
<ol>
<li>Rename the folder &lt;nightly sdk location&gt;/frameworks/libs/player/10.0 to &#8220;10&#8243;</li>
<li>Edit the file &lt;nightly sdk location&gt;/frameworks/flex-config.xml and remove &#8220;.{targetPlayerMinorVersion}&#8221; and save</li>
</ol>
<p>I hope this helps anyone having the same problem!</p>
]]></content:encoded>
			<wfw:commentRss>http://sjhewitt.co.uk/2010/01/autocomplete-broken-in-flash-builder-beta-2-with-nightly-builds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Papervision Lines3D: addNewLine and removeAllLines</title>
		<link>http://sjhewitt.co.uk/2009/12/papervision-lines3d-addnewline-and-removealllines/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=papervision-lines3d-addnewline-and-removealllines</link>
		<comments>http://sjhewitt.co.uk/2009/12/papervision-lines3d-addnewline-and-removealllines/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 16:04:58 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Papervision]]></category>

		<guid isPermaLink="false">http://blog.sjhewitt.co.uk/?p=7</guid>
		<description><![CDATA[While working with the Lines3D class in Papervision, I noticed that the memory usage was growing while my application was running. After a bit of hair pulling, debugging and profiling, I found that a lot of Vertex3D objects were being created and not garbage collected. After the initial creation of the scene, it seemed that [...]]]></description>
			<content:encoded><![CDATA[<p>While working with the <strong>Lines3D </strong>class in Papervision, I noticed that the memory usage was growing while my application was running. After a bit of hair pulling, debugging and profiling, I found that a lot of <strong>Vertex3D</strong> objects were being created and not garbage collected.</p>
<p><span id="more-7"></span>After the initial creation of the scene, it seemed that the only place where new <strong>Vertex3Ds</strong> were being created was in these lines:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">lines<span style="color: #000066; font-weight: bold;">.</span>removeAllLines<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
lines<span style="color: #000066; font-weight: bold;">.</span>addNewLine<span style="color: #000000;">&#40;</span>o<span style="color: #000066; font-weight: bold;">.</span>extra<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">size</span><span style="color: #000066; font-weight: bold;">,</span><br />
x1<span style="color: #000066; font-weight: bold;">,</span> y1<span style="color: #000066; font-weight: bold;">,</span> z1<span style="color: #000066; font-weight: bold;">,</span><br />
x2<span style="color: #000066; font-weight: bold;">,</span> y2<span style="color: #000066; font-weight: bold;">,</span> z2<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
<p>The leak was caused because the addNewLine command creates a new Line object using the material of the Lines3D object. The <em>removeAllLines() </em>command removes all references to Line from the array, however there are a number of current objects still referenced by the <strong>Line </strong>instances. If you wanted to make sure the line instances are garbage collected, then you would need to null the <em>instance </em>and <em>material</em> properties on each <strong>Line</strong> instance (it wouldn&#8217;t hurt to null the <em>v0</em>, <em>v1</em> and <em>renderCommand</em> properties too)</p>
<p>I believe it is correct that <em>removeAllLines</em> should not clear the material attribute as it is possible that you are going to use the removed lines for something else and you don&#8217;t want to lose reference to the material. However, I do think that <em>removeLine</em> (which is called by <em>removeAllLines</em>) should clear the instance property!</p>
<p>As I know that each <strong>Lines3D </strong>instance in my graph has only one line, and I&#8217;m only updating the endpoints of the line, my solution was to simply update the vertex at each end of the line, as follows:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #6699cc; font-weight: bold;">var</span> v0<span style="color: #000066; font-weight: bold;">:</span>Vertex3D = o<span style="color: #000066; font-weight: bold;">.</span>lines<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>v0<span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #6699cc; font-weight: bold;">var</span> v1<span style="color: #000066; font-weight: bold;">:</span>Vertex3D = o<span style="color: #000066; font-weight: bold;">.</span>lines<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">.</span>v1<span style="color: #000066; font-weight: bold;">;</span><br />
v0<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = x1<span style="color: #000066; font-weight: bold;">;</span><br />
v0<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = y1<span style="color: #000066; font-weight: bold;">;</span><br />
v0<span style="color: #000066; font-weight: bold;">.</span>z = z1<span style="color: #000066; font-weight: bold;">;</span><br />
v1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = x2<span style="color: #000066; font-weight: bold;">;</span><br />
v1<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = y2<span style="color: #000066; font-weight: bold;">;</span><br />
v1<span style="color: #000066; font-weight: bold;">.</span>z = z2<span style="color: #000066; font-weight: bold;">;</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://sjhewitt.co.uk/2009/12/papervision-lines3d-addnewline-and-removealllines/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Welcome</title>
		<link>http://sjhewitt.co.uk/2009/12/welcome/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=welcome</link>
		<comments>http://sjhewitt.co.uk/2009/12/welcome/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 14:05:26 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.sjhewitt.co.uk/?p=3</guid>
		<description><![CDATA[Welcome to my blog, here I will be documenting the things I find out while developing various projects using Flex and a few other languages. The main reason is that I will soon be starting a new project and will be experimenting with a great collection of APIs and Frameworks, and I need somewhere to [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to my blog, here I will be documenting the things I find out while developing various projects using Flex and a few other languages.</p>
<p>The main reason is that I will soon be starting a new project and will be experimenting with a great collection of APIs and Frameworks, and I need somewhere to vent when things don&#8217;t work:</p>
<ul>
<li>Adobe Flex 4 (with the Spark components)</li>
<li>Adobe Catalyst (or maybe teaching the designers how to use it)</li>
<li><a href="http://www.robotlegs.org/">Robotlegs</a> &#8211; this sounds like a great framework and should bring some much needed organisation to the project</li>
<li><a href="http://code.google.com/p/asaxb/">ASAXB</a> &#8211; for some unfortunately necessary XML deserialisation</li>
<li><a href="http://blog.papervision3d.org/">Papervision3D</a> &#8211; because it &#8216;looks whizzy&#8217; according to the Project Manager. Hopefully I can come up with something that will actually add to the application and UX</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://sjhewitt.co.uk/2009/12/welcome/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
