<?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>BadMagicNumber &#187; javascript</title>
	<atom:link href="http://nicklothian.com/blog/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://nicklothian.com/blog</link>
	<description>My Blog, Take 4</description>
	<lastBuildDate>Wed, 18 Jan 2012 13:55:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using Flash to shim a webcam to Canvas</title>
		<link>http://nicklothian.com/blog/2009/12/15/using-flash-to-shim-a-webcam-to-canvas/</link>
		<comments>http://nicklothian.com/blog/2009/12/15/using-flash-to-shim-a-webcam-to-canvas/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 00:09:58 +0000</pubDate>
		<dc:creator>Nick Lothian</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://nicklothian.com/blog/?p=441</guid>
		<description><![CDATA[A couple of weeks ago I posted my work on developing virtual keypad using HTML5 video. That worked surprisingly well, but had some unfortunate requirements in that HTML5 doesn&#8217;t really support access to webcams. Flash Does. I&#8217;ve developed a Flash shim, which gets access to your webcam, and copies frames to data urls which can [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago I posted my work on developing <a href="http://nicklothian.com/blog/2009/11/18/augmented-reality-virtual-keyboard-using-html-5-video/">virtual keypad using HTML5 video</a>. That worked surprisingly well, but had some unfortunate requirements in that HTML5 doesn&#8217;t really support access to webcams.</p>
<p>Flash Does.</p>
<p>I&#8217;ve developed a Flash shim, which gets access to your webcam, and copies frames to data urls which can be used in HTML DOM images.</p>
<p>This works well, within some limits:</p>
<ul>
<li>The performance of the Flash-&gt;Javascript interface is adequate for normal use, but drops off as the data being copied across increases in size. This means that you need to have a postage stamp sized (80&#215;60) video in flash to get adequate performance in HTML (~25 fps).</li>
<li>Chrome (and Safari?) leaks memory when new data URLs are created. (See <a href="http://code.google.com/p/chromium/issues/detail?id=25047">bug</a>). Apart from that, this technique works on both Chrome &amp; Firefox, but my virtual keypad is Firefox only ATM</li>
<li>I don&#8217;t really know Flash. I downloaded the Flex SDK a few days ago and started hacking, so I&#8217;m pretty sure <a href="http://demo.nicklothian.com/flashcamshim/cam.mxml">my Flash code</a> sucks.</li>
</ul>
<p><a href="http://demo.nicklothian.com/flashcamshim/">Here&#8217;s a demo</a>. Take a look at the <a href="http://nicklothian.com/blog/2009/11/18/augmented-reality-virtual-keyboard-using-html-5-video/">previous post</a> to see how you are supposed to use it. I started work on making a proper Javascript API for it, but yeah&#8230; The code is on GitHub, though: <a href="http://github.com/nlothian/FlashCamShim">http://github.com/nlothian/FlashCamShim</a></p>
]]></content:encoded>
			<wfw:commentRss>http://nicklothian.com/blog/2009/12/15/using-flash-to-shim-a-webcam-to-canvas/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cross domain, cross window Javascript communication</title>
		<link>http://nicklothian.com/blog/2009/08/05/cross-domain-cross-window-javascript-communication/</link>
		<comments>http://nicklothian.com/blog/2009/08/05/cross-domain-cross-window-javascript-communication/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 02:24:34 +0000</pubDate>
		<dc:creator>Nick Lothian</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://nicklothian.com/blog/?p=379</guid>
		<description><![CDATA[The scenario: A typical meeting, they want to pop up our search screen, select an item and have it post back via Javascript the selected item. &#8220;Sure, no problem&#8221; I say. &#8220;Can you do cross domain Javascript like that&#8221; their dev asks, and I think for a second about all the IFrame proxy hacks I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>The scenario: A typical meeting, they want to pop up our search screen, select an item and have it post back via Javascript the selected item. &#8220;Sure, no problem&#8221; I say. &#8220;Can you do cross domain Javascript like that&#8221; their dev asks, and I think for a second about all the IFrame proxy hacks I&#8217;ve seen and say &#8220;yeah, no problems&#8221;.</p>
<p>Doh.</p>
<p>So I get back to work, and talk to Ben-out-local-Javascript-god, who points me at the location-fragment for communication hack (see, for example <a href="http://tagneto.blogspot.com/2006/06/cross-domain-frame-communication-with.html">Cross Domain Frame Communication with Fragment Identifiers (for Comet?)</a>) and away I go.</p>
<p>I quickly discover that all the documentation on that is for use with frames, not windows, and windows work slightly different. For example, the parent window can&#8217;t poll the child for it&#8217;s location.hash, nor can the child set the parent&#8217;s location.hash. </p>
<p>Doh Doh.</p>
<p>Anyway to cut a long story short and to document it for anyone else, you can make it work. The child window needs to contain a iframe from the parent domain, then proxy communication though that. Modern non IE browsers can use <a href="http://ejohn.org/blog/cross-window-messaging/">Window.postMessage</a> to do the same thing (Actually, IE8 supports Window.postMessage, too, but I couldn&#8217;t get it to work properly).</p>
<p>The core of the IE specific code looks like this. The child window uses this object:</p>
<p><code></p>
<pre>
/*
  XDCommsChild - Cross Domain Communication, Child Object.

   Create this in the child window with the address of the xdomaincomms.html file on the same domain as the parent window
*/
var XDCommsChild = function(xdomaincommsAddress) {
    this.xdomaincommsAddress = xdomaincommsAddress;

    this.isIe = isInternetExplorer();

    if (isIe) {
        // need hidden frame for communication. IE8 is supposed to support postMessage, but I can't get it to work properly
        document.writeln("<iframe style='display:none' name='xdcomms' src='" + this.xdomaincommsAddress + "' width='1px' height='1px'></iframe>")
    }

    this.postBack = function(data) {
        if (isIe) {
            // this method tested against IE6,7 &#038; 8
            window.open(this.xdomaincommsAddress + '#data=' + data, 'xdcomms'); // MUST use window.open. frame.src or frame.location both fail
        } else {
            // for everything else - tested against Firefox 3, Chrome, Safari
            window.opener.postMessage(data, '*'); // should really restrict the domain data can come from here
        }
    }

    this.postBackAndCloseWindow = function(data){
        this.postBack(data);
        setTimeout("window.close()", 200); // need to use a timeout to make sure the javascript in the frame has executed if we are using that communication model
    }
}
</pre>
<p></code></p>
<p>Then the included proxy frame (deployed on the parent domain) looks like this:</p>
<p><code> </p>
<pre>
           var hash = null;

            function sendData() {
                hash = location.hash;
                if (hash != null &#038;&#038; hash.length > 1) {
                    window.parent.opener.location.hash=hash;
                    location.hash = '#';
                }
            }

            setInterval("sendData()", 50);
</pre>
<p></code></p>
<p>The the parent window uses this object:</p>
<p><code></p>
<pre>
/*
  XDCommsParent - Cross Domain Communication, Parent Object.

   Create this in the parent window with a callback function that will recieve data.
*/
var XDCommsParent = function(dataRecievedCallback) {
    this.dataRecievedCallback = dataRecievedCallback;

    this.isIe = isInternetExplorer();

    this.receiveMessage = function(event) {
        dataRecievedCallback(event.data);
    }

    if (this.isIe) {
        window.setInterval("pollForData()", 200);
    } else {
        window.addEventListener("message", this.receiveMessage, false);
    }

}
</pre>
<p></code></p>
<p>The final piece of the solution is calling it from the child window. I do this:</p>
<p><code></p>
<pre>
&lt;button onclick="javascript:xDCommsChild.postBackAndCloseWindow(document.getElementById('thevalue').value);"&gt;Send to Parent Window&lt;/button&gt;
</pre>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://nicklothian.com/blog/2009/08/05/cross-domain-cross-window-javascript-communication/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The future of web development looks a lot like&#8230;.</title>
		<link>http://nicklothian.com/blog/2007/06/25/the-future-of-web-development-looks-a-lot-like/</link>
		<comments>http://nicklothian.com/blog/2007/06/25/the-future-of-web-development-looks-a-lot-like/#comments</comments>
		<pubDate>Tue, 26 Jun 2007 03:46:50 +0000</pubDate>
		<dc:creator>Nick Lothian</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://nicklothian.com/blog/2007/06/25/the-future-of-web-development-looks-a-lot-like/</guid>
		<description><![CDATA[The future of web development looks a lot like this (Rails on Javascript on the JVM). I&#8217;ve been spending a lot of time learning Javascript, and I&#8217;ve made no secret of the fact that I think it&#8217;s a better option for web development than Ruby (mainly because it means you only need to learn one [...]]]></description>
			<content:encoded><![CDATA[<p>The future of web development looks a lot like <a href="http://www.iunknown.com/2007/06/steve-yegge-por.html">this</a> (Rails on Javascript on the JVM).</p>
<p><span id="more-287"></span></p>
<p>I&#8217;ve been spending a lot of time learning Javascript, and I&#8217;ve made no secret of the fact that I think it&#8217;s a better option for web development than Ruby (mainly because it means you only need to learn one language for both client and serverside programming) .</p>
<p>BTW, if you don&#8217;t already read <a href="http://steve-yegge.blogspot.com/">Steve Yegge</a>, go and catch up now. I think his <a href="http://steve-yegge.blogspot.com/2007/06/rich-programmer-food.html">Javascript 2 to bytecode compiler</a> (I assume that what it is) is a great idea, too.</p>
<p>Also, <a href="http://jsc.sourceforge.net/">JSC</a> looks interesting.</p>
]]></content:encoded>
			<wfw:commentRss>http://nicklothian.com/blog/2007/06/25/the-future-of-web-development-looks-a-lot-like/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DWR Callback closures inside loops</title>
		<link>http://nicklothian.com/blog/2007/05/18/dwr-callback-closures-inside-loops/</link>
		<comments>http://nicklothian.com/blog/2007/05/18/dwr-callback-closures-inside-loops/#comments</comments>
		<pubDate>Fri, 18 May 2007 11:26:36 +0000</pubDate>
		<dc:creator>Nick Lothian</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://nicklothian.com/blog/2007/05/18/dwr-callback-closures-inside-loops/</guid>
		<description><![CDATA[I&#8217;ve been doing a fair bit of Javascript lately. Like anytime I learn a new language I look back at code I wrote a week ago and thing OMG&#8230; what was I thinking!? One useful thing I learnt today was how to use the DWR closure callback pattern inside a loop. My original code looked [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing a fair bit of Javascript lately. Like anytime I learn a new language I look back at code I wrote a week ago and thing <em>OMG&#8230; what was I thinking!?</em></p>
<p>One useful thing I learnt today was how to use the <a href="http://getahead.org/dwr/browser/extradata">DWR closure callback pattern</a> inside a loop.</p>
<p>My original code looked like this:</p>
<pre><tt>
var taggedLinks = $$(".taggedlink");

for(var i=0; i &lt; taggedlinks.length; i++) {
	RatingApi.getRating(taggedLinks[i].href, {
		callback: function(data) {
			displayRating(data, taggedLinks[i]);
		}
	});
}
</tt></pre>
<p>Where <em>RatingApi.getRating(..)</em> is a DWR AJAX call. It&#8217;s fairly clear what the problem is: the <em>displayRating</em> function is called when <em>RatingApi.getRating</em> returns. Obviously the value of <em>i</em> will be not what was expected.</p>
<p>The solution looks like this (thanks Ben!):</p>
<pre><tt>
var taggedLinks = $$(".taggedlink");

for(var i=0; i &lt; taggedlinks.length; i++) {
	var remoteCall = {
		index : i,
		callback: function(data) {
			displayRating(data, taggedLinks[this.index]);
		}
	};	

	RatingApi.getRating(taggedLinks[i].href, remoteCall);
}
</tt></pre>
]]></content:encoded>
			<wfw:commentRss>http://nicklothian.com/blog/2007/05/18/dwr-callback-closures-inside-loops/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

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

