<?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>Fraser Hobbs</title>
	<atom:link href="http://fraserhobbs.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://fraserhobbs.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 29 Nov 2010 23:41:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Flash/JavaScript Communication</title>
		<link>http://fraserhobbs.com/2010/11/flashjavascriptbridge/</link>
		<comments>http://fraserhobbs.com/2010/11/flashjavascriptbridge/#comments</comments>
		<pubDate>Mon, 29 Nov 2010 23:41:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Browser]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Utility]]></category>

		<guid isPermaLink="false">http://fraserhobbs.com/?p=70</guid>
		<description><![CDATA[I recently worked on a video player in Flash for a predominantly HTML/JavaScript site, which involved having to do a lot of communication between ActionScript and JavaScript. This turned out to not be as straight forward as I had hoped, so I created a set of JavaScript and ActionScript code to wrap up the nasty [...]]]></description>
			<content:encoded><![CDATA[<p>I recently worked on a video player in Flash for a predominantly HTML/JavaScript site, which involved having to do a lot of communication between ActionScript and JavaScript. This turned out to not be as straight forward as I had hoped, so I created a set of JavaScript and ActionScript code to wrap up the nasty bits and make it as simple as possible.</p>
<p><span id="more-70"></span></p>
<p>The aim was to create a cross browser solution that makes communication between ActionScript and JavaScript with both having similar APIs to one another.</p>
<h3>Issues with Flash/Javascript communications</h3>
<ul>
<li>In Safari Flash has to make a successful call to JavaScript before JavaScript can call Flash.</li>
<li>In browsers except IE when the Flash HTMLElement Object on the page is display:none it is removed and when re-displayed starts from scratch.</li>
<li>In IE Flash is inaccessible from JavaScript when display:none.</li>
<li>You may have multiple Flash instances on one page.</li>
</ul>
<h3>How to use it</h3>
<h4>Javascript</h4>
<h5>Connect</h5>
<p>Connect from JavaScript. When both JavaScript and ActionScript have connected successfully they can communicate.</p>
<pre>FlashJavascriptBridge.connect(id,flashObject,target);</pre>
<ul>
<li>id &#8211; unique id shared between ActionScript and JavaScript (we recommend the Flash objects ID) [String, Required]</li>
<li>flashObject &#8211; DOMs Flash object [Object, Required]</li>
<li>target &#8211; a target property to include when dispatching events [Object, Optional]</li>
</ul>
<h5>Add Event Listener</h5>
<p>Add an event listener to listen for an event dispatched by ActionScript.</p>
<pre>FlashJavascriptBridge.addEventListener(id,eventName,eventHandler);</pre>
<ul>
<li>id &#8211; unique id same as value passed into connect method [String, Required]</li>
<li>target -unique string given to the event you are listening for [String, Required]</li>
<li>eventHandler &#8211; function to call if event triggered [Function, Required]</li>
</ul>
<h5>Dispatch Event</h5>
<p>Dispatch an event from JavaScript to ActionScript.</p>
<pre>FlashJavascriptBridge.dispatchEvent(id,event);</pre>
<ul>
<li>id &#8211; unique id same as value passed into connect method [String, Required]</li>
<li>event &#8211; a FlashJavascriptBridgeEvent [FlashJavascriptBridgeEvent, Required]</li>
</ul>
<h5>FlashJavascriptBridgeEvent</h5>
<p>Event object sent to and received from ActionScript.</p>
<pre>new FlashJavascriptBridgeEvent(type,data,target);</pre>
<ul>
<li>type &#8211; a unique string denoting the type of event [String, Required]</li>
<li>data &#8211; any data sent with key value pairs [Object, Optional]</li>
<li>target &#8211; a target property set in the connect method [Object, Not used for event dispatching]</li>
</ul>
<h4>ActionScript</h4>
<h5>Connect</h5>
<p>Connect from ActionScript.</p>
<pre>FlashJavascriptBridge.connect(id, failCallback);</pre>
<ul>
<li>id &#8211; unique id passed from JavaScript as a flashVar [String, Required]</li>
<li>failCallback &#8211; callback for if connection fails i.e. ExternalInterface is unavailable. [Function, Required]</li>
</ul>
<h5>Add Event Listener</h5>
<p>Add an event listener to listen for an event dispatched by JavaScript.</p>
<pre>FlashJavascriptBridge.addEventListener(type,eventHandler);</pre>
<ul>
<li>type &#8211; unique string given to the event you are listening for [String, Required]</li>
<li>eventHandler &#8211; function to call if event triggered [Function, Required]</li>
</ul>
<h5>Dispatch Event</h5>
<p>Dispatch an event from ActionScript to ActionScript.</p>
<pre>FlashJavascriptBridge.dispatchEvent(event);</pre>
<ul>
<li>event &#8211; a FlashJavascriptBridgeEvent [FlashJavascriptBridgeEvent, Required]</li>
</ul>
<h5>FlashJavascriptBridgeEvent</h5>
<pre>new FlashJavascriptBridgeEvent(type, data);</pre>
<ul>
<li>type &#8211; a unique string denoting the type of event [String, Required]</li>
<li>data &#8211; any data sent with key value pairs [Object, Optional]</li>
</ul>
<h3>Example</h3>
<h4>JavaScript</h4>
<pre>
var vars = {id: "123", swf:"example.swf", flashvars:{bridgeID:"123"}};
$obj.flash(vars);

FlashJavascriptBridge.connect("123",$(obj).children()[0],obj);
FlashJavascriptBridge.addEventListener("123","fromASExample",fromASExampleHandler);

FlashJavascriptBridge.dispatchEvent("123",
new FlashJavascriptBridgeEvent("fromJSExample", {message:"Hello"}));

function fromASExampleHandler(event)
{
	log(event.type); //fromASExample
	log(event.data.message); //hello
};
</pre>
<h4>ActionScript</h4>
<pre>
FlashJavascriptBridge.connect(loaderInfo.parameters.bridgeID, flashJavascriptBridgeFail);
FlashJavascriptBridge.addEventListener("fromJSExample",fromJSExampleHandler);

FlashJavascriptBridge.dispatchEvent(
new FlashJavascriptBridgeEvent("fromASExample", {message:"hello"}));

function fromJSExampleHandler(event:FlashJavascriptBridgeEvent):void
{
	trace(event.type); //fromJSExample
	trace(event.data.message); //hello
}
</pre>
<h3>Get the source</h3>
<p>This is part of my jQuery video plugin on Github.</p>
<div class="source"><a href="https://github.com/LeanMeanFightingMachine/HTML5FlashVideo" target="_blank">Visit GitHub</a></div>
]]></content:encoded>
			<wfw:commentRss>http://fraserhobbs.com/2010/11/flashjavascriptbridge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flash, Dynamic font embedding</title>
		<link>http://fraserhobbs.com/2010/07/flash-dynamic-font-embedding/</link>
		<comments>http://fraserhobbs.com/2010/07/flash-dynamic-font-embedding/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 16:04:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Utility]]></category>
		<category><![CDATA[font]]></category>

		<guid isPermaLink="false">http://fraserhobbs.com/?p=44</guid>
		<description><![CDATA[It&#8217;s now easy to dynamically embed fonts loaded from a swf using Denis Kolyako&#8217;s getDefinitionNames class. This combined with external styleSheets is a great solution for multilingual projects which require different fonts to be used for different languages. To get it working you first need to create a fonts swf. Create font symbols for the [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s now easy to dynamically embed fonts loaded from a swf using Denis Kolyako&#8217;s <a href="http://etcs.ru/pre/getDefinitionNamesSource/">getDefinitionNames class</a>. This combined with external styleSheets is a great solution for multilingual projects which require different fonts to be used for different languages.</p>
<p><span id="more-44"></span></p>
<p>To get it working you first need to create a fonts swf. Create font symbols for the fonts you require in an fla, ensuring you tick &#8216;Export for actionscript&#8217; and &#8216;Export in frame 1&#8242; in the symbols properties window. Publish as a swf.</p>
<p>Now you can use something like the following code in your application to load the fonts swf and embed all the available fonts in the swf.</p>
<pre>
import flash.text.TextFieldAutoSize;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextField;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.net.URLRequest;
import ru.etcs.utils.getDefinitionNames;
import flash.text.Font;

var fontLoader:Loader = new Loader();
fontLoader.contentLoaderInfo.addEventListener(Event.INIT, fontLoadedHandler);
fontLoader.load(new URLRequest("fonts/fonts.swf"));

function fontLoadedHandler(event : Event) : void
{
	var loaderInfo:LoaderInfo = event.target as LoaderInfo;

	var def:Array = getDefinitionNames(loaderInfo, false, true);

	for(var i : int = 0; i < def.length; i++)
	{
		var c : Class = Class(loaderInfo.applicationDomain.getDefinition(def[i]));

		if(new c() is Font)
		{
			Font.registerFont(c);
		}
	}

	var textFormat:TextFormat = new TextFormat();
	textFormat.font = "Compacta BT";
	textFormat.size = 40;

	var textField:TextField = new TextField();
	textField.defaultTextFormat = textFormat;
	textField.embedFonts = true;
	textField.autoSize = TextFieldAutoSize.LEFT;
	textField.antiAliasType = AntiAliasType.ADVANCED;
	textField.text = "embedded font text";
	addChild(textField);
}
</pre>
<p>You can either use TextFormat or Styles once the font is registered, I used TextFormat to keep the example nice and quick.</p>
]]></content:encoded>
			<wfw:commentRss>http://fraserhobbs.com/2010/07/flash-dynamic-font-embedding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML5, Percentage fill image</title>
		<link>http://fraserhobbs.com/2010/07/html5-percentage-fill-image/</link>
		<comments>http://fraserhobbs.com/2010/07/html5-percentage-fill-image/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 22:01:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Browser]]></category>
		<category><![CDATA[Experiment]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://fraserhobbs.com/?p=31</guid>
		<description><![CDATA[Being predominately an Actionscript developer I&#8217;m not one of the people getting over excited by HTML5 and the hype surrounding it, especially considering its suggested timeline. But I won&#8217;t get onto that already exhausted subject. I think it&#8217;s important to understand the capabilities of technologies to make the right calls on which is appropriate for [...]]]></description>
			<content:encoded><![CDATA[<p>Being predominately an Actionscript developer I&#8217;m not one of the people getting over excited by HTML5 and the hype surrounding it, especially considering its <a href="http://blogs.techrepublic.com.com/programming-and-development/?p=718">suggested timeline</a>. But I won&#8217;t get onto that already exhausted subject.</p>
<p>I think it&#8217;s important to understand the capabilities of technologies to make the right calls on which is appropriate for each project. So I&#8217;ve started having a bit of a tinker with HTML5 and really gone back to basics! To have a play with the canvas I messed around with doing a coloured percentage fill on an image.</p>
<p><span id="more-31"></span></p>
<p>I came up with 2 solutions; <a href="/examples/html5/percentage-fill-image/pixels.html" class="popup-link" name="Colouring pixels example">colouring the pixels</a> of part of the image and combining a <a href="/examples/html5/percentage-fill-image/cropped.html" class="popup-link" name="Cropped example">cropped</a> coloured version of the image and the original.</p>
<h3>Colouring pixels</h3>
<pre>
window.addEventListener('load', function () {

//get canvas
var elem = document.getElementById('percent');

//if canvas doesn't exist or canvas methods not supported abort
if (!elem || !elem.getContext) {
 	return;
}

//get 2D context
var context = elem.getContext('2d');

//if context doesn't exist or context methods not supported abort
if (!context || !context.drawImage || !context.putImageData || !context.getImageData) {
 	return;
}

//load image
var b = new Image();
b.addEventListener('load', function() {

	//get number out of canvas and strip out non-numerals
	var p = elem.innerHTML;
	p = p.match( /\d+/g ).join('');
	var h = parseInt((p / 100) * b.height);

	//draw base image
	context.drawImage(b, 0, 0, b.width, b.height);

	//get pixels from point where percent starts
	var iData = context.getImageData( 0, b.height - h, b.width, h);
	var pixels = iData.data;

	//for every pixel that isn't white draw a pink pixel
	for (var i = 0; n = pixels.length, i < n; i += 4)
	{
		if(pixels[i] < 255 &#038;&#038; pixels[i+1] < 255 &#038;&#038; pixels[i+2] < 255)
		{
			pixels[i] = 255;
			pixels[i+1] = 0;
			pixels[i+2] = 64;
		}
	}

	//add the new data to the context bottom aligned
	context.putImageData(iData, 0, b.height - h);

}, false);
b.src = 'bird.png';

}, false);
</pre>
<h3>Cropped</h3>
<pre>
window.addEventListener('load', function () {

//get canvas
var elem = document.getElementById('percent');

//if canvas doesn't exist or canvas methods not supported abort
if (!elem || !elem.getContext) {
	return;
}

//get 2D context
var context = elem.getContext('2d');

//if context doesn't exist or context methods not supported abort
if (!context || !context.drawImage) {
	return;
}

//load base image
var b = new Image();
b.addEventListener('load', function() {
	//draw base image
	context.drawImage(b, 0, 0, b.width, b.height);
}, false);
b.src = 'bird.png';

//load pink image
var f = new Image();
f.addEventListener('load', function() {

	//get number out of canvas and strip out non-numerals
	var p = elem.innerHTML;
	p = p.match( /\d+/g ).join('');
	var h = parseInt((p / 100) * f.height);

	//draw highlight image from point where percent starts
	context.drawImage( f, 0, f.height - h, f.width, h, 0, f.height - h, f.width, h );
}, false);
f.src = 'bird-pink.png';

}, false);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://fraserhobbs.com/2010/07/html5-percentage-fill-image/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flash, Facebook Graph API in tabs</title>
		<link>http://fraserhobbs.com/2010/07/flash-facebook-graph-api-in-tabs/</link>
		<comments>http://fraserhobbs.com/2010/07/flash-facebook-graph-api-in-tabs/#comments</comments>
		<pubDate>Sun, 04 Jul 2010 20:15:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Facebook]]></category>

		<guid isPermaLink="false">http://fraserhobbs.com/?p=15</guid>
		<description><![CDATA[I am by no means an expert at developing for Facebook, and to be honest neither would I want to be. What little I have done on the Facebook platform, I&#8217;ve found myself getting frustrated with the confusing, out-of-date and often half-baked documentation. At last it seems like Facebook might be getting their acts together [...]]]></description>
			<content:encoded><![CDATA[<p>I am by no means an expert at developing for Facebook, and to be honest neither would I want to be. What little I have done on the Facebook platform, I&#8217;ve found myself getting frustrated with the confusing, out-of-date and often half-baked documentation.</p>
<p>At last it seems like Facebook might be getting their acts together and the <a href="http://developers.facebook.com/docs/api">Graph API</a> looks impressive and simple to use.</p>
<p><span id="more-15"></span></p>
<p>I&#8217;ve been working on a project where I had to get a users basic information within flash in a Tab and I decided to use the Graph API.</p>
<h3>Authentication</h3>
<p>The Graph API requires an OAuth Access token, but by default in a tab you are provided with a session key which is passed along with the rest of the Facebook variables as a flashVar. But fear not, Facebook provide a <a href="http://developers.facebook.com/docs/guides/upgrade">service</a> to convert a session key into a OAuth Access Token, which in AS3 looks like this:</p>
<pre>
var variables : URLVariables = new URLVariables();
variables["type"] = "client_cred";
variables["client_id"] = __appID;
variables["client_secret"] = __secret;
variables["sessions"] = __sessionKey;

var urlRequest : URLRequest = new URLRequest(EXCHANGE_SESSIONS_URI);
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = variables;

var urlLoader : URLLoader = new URLLoader();
urlLoader.load(urlRequest);
</pre>
<p>Once you&#8217;ve successfully got your OAuth Access Token you can start using the API.</p>
<h3>Fetch data</h3>
<pre>
var uri:String = REQUEST_URI + __id + (__connection.length > 0 ? "/" + __connection : "");

var variables : URLVariables = new URLVariables();
if(_accessToken) variables["access_token"] = _accessToken;

if(__filters)
{
	for (var i : String in __filters)
	{
		variables[i] = __filters[i];
	}
}

var urlRequest : URLRequest = new URLRequest(uri);
urlRequest.method = URLRequestMethod.GET;
urlRequest.data = variables;

var urlLoader : URLLoader = new URLLoader();
urlLoader.load(urlRequest);
</pre>
<h3>Load picture</h3>
<pre>
var uri:String = REQUEST_URI + __id + "/picture" + "?access_token=" + _accessToken +
(__size.length > 0 ? "&#038;type=" + __size : "");

var loader:Loader = new Loader();
loader.load(new URLRequest(uri));
</pre>
<h3> Search </h3>
<pre>
var uri:String = REQUEST_URI + "/search";

var variables : URLVariables = new URLVariables();
if(_accessToken) variables["access_token"] = _accessToken;
variables["q"] = __q;
variables["type"] = __type;

var urlRequest : URLRequest = new URLRequest(uri);
urlRequest.method = URLRequestMethod.GET;
urlRequest.data = variables;

var urlLoader : URLLoader = new URLLoader();
urlLoader.load(urlRequest);
</pre>
<p>I&#8217;ve created a utility class to make using the Facebook Graph API easier.</p>
<p><a href="http://code.google.com/p/facebook-graph-api-as3/">View project on Google code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fraserhobbs.com/2010/07/flash-facebook-graph-api-in-tabs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bitwise Shifting</title>
		<link>http://fraserhobbs.com/2010/05/bitwise-shifting/</link>
		<comments>http://fraserhobbs.com/2010/05/bitwise-shifting/#comments</comments>
		<pubDate>Tue, 18 May 2010 19:34:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://fraserhobbs.com/?p=9</guid>
		<description><![CDATA[Bitwise shifting can be useful in AS3 as it can give some quite significant performance gains. It isn’t actually as complicated as you think and the name explains exactly what’s going on. If we take the number 7 represented as 111 in binary and we move that value right and so removing the last 1 [...]]]></description>
			<content:encoded><![CDATA[<p>Bitwise shifting can be useful in AS3 as it can give some quite significant performance gains. It isn’t actually as complicated as you think and the name explains exactly what’s going on.</p>
<p><span id="more-9"></span></p>
<p>If we take the number 7 represented as 111 in binary and we move that value right and so removing the last 1 we get 11 which is binary for 3. Results are always floored because we are dealing in binary and so whole numbers.</p>
<p>Here are a few examples of it can be used:</p>
<h3>Divide by 2 and floor</h3>
<pre>
num >> 1
</pre>
<h3>Multiply by 2 and floor</h3>
<pre>
num << 1
</pre>
<h3>Split up 24bit RGB Colour</h3>
<pre>
var colour:uint = 0x336699;
var red:uint = color >> 16;
var green:uint = color >> 8 &#038; 0xFF;
var blue:uint = color &#038; 0xFF;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://fraserhobbs.com/2010/05/bitwise-shifting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

