<?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>Sinceriously &#187; javascript</title>
	<atom:link href="http://www.sinceriously.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sinceriously.com</link>
	<description>Designment and Development</description>
	<lastBuildDate>Sun, 18 Jul 2010 01:36:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Canvas Demos</title>
		<link>http://www.sinceriously.com/2009/03/canvas-demos/</link>
		<comments>http://www.sinceriously.com/2009/03/canvas-demos/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 20:44:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[css canvas]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web app]]></category>

		<guid isPermaLink="false">http://www.sinceriously.com/?p=236</guid>
		<description><![CDATA[In the third update to my crossword web app tutorial, I discussed CSS Canvas. I mentioned how potentially powerful the canvas can be, especially as an alternative to Flash, but I wasn&#8217;t able to provide concrete examples since I hadn&#8217;t personally done anything exceptionally extraordinary with it yet. Well now I have the proof! A [...]]]></description>
			<content:encoded><![CDATA[<p>In the third update to my crossword web app tutorial, I discussed CSS Canvas. I mentioned how potentially powerful the canvas can be, especially as an alternative to Flash, but I wasn&#8217;t able to provide concrete examples since I hadn&#8217;t personally done anything exceptionally extraordinary with it yet.</p>
<p>Well now I have the proof! A couple of weeks ago I was followed on Twitter by <a href="http://twitter.com/canvasdemos" onclick="window.open(this.href); return false;">@canvasdemos</a>. The <a href="http://www.canvasdemos.com/" onclick="window.open(this.href); return false;">Canvas Demos</a> blog dedicates its time to tracking down and showcasing demos and tutorials for the HTML canvas element. Some of the examples they&#8217;ve uncovered are truly amazing and include games, 3D tests, music visualizers, etc. If you&#8217;re at all intrigued by the power of CSS Canvas, and are wondering how you can use it on your own site (or Web app), <a href="http://www.canvasdemos.com/" onclick="window.open(this.href); return false;">don&#8217;t hesitate to give their site a look!</a>.</p>
<p>The image above is from Jacob Seidelin&#8217;s music visualizer. The demo creates an image of Thom Yorke to the music of Radiohead&#8217;s Idioteque. <a href="http://www2.nihilogic.dk/labs/canvas_music_visualization/" onclick="window.open(this.href); return false;">It&#8217;s worth checking out.</a></p>
<p><strong>&mdash; Ryan</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://js-kit.com/rss/www.sinceriously.com/p=236</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Crossword Web App, Part 6</title>
		<link>http://www.sinceriously.com/2009/03/crossword-web-app-part-6/</link>
		<comments>http://www.sinceriously.com/2009/03/crossword-web-app-part-6/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 20:31:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Game Designment]]></category>
		<category><![CDATA[crossword]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web app]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.sinceriously.com/?p=217</guid>
		<description><![CDATA[Although it&#8217;s easy to get Javascript to read from an XML file, pulling out the appropriate data can sometimes be tricky. Here are some tips to help you tame the XML beast. Part 6: Taming XML Let&#8217;s look at a typical XMLHttpRequest again. var url = 'xml/070814.xml';var xmlhttp = new XMLHttpRequest();&#160;if (xmlhttp != null) {xmlhttp.onreadystatechange [...]]]></description>
			<content:encoded><![CDATA[<p>Although it&#8217;s easy to get Javascript to read from an XML file, pulling out the appropriate data can sometimes be tricky. Here are some tips to help you tame the XML beast.</p>
<h1>Part 6: Taming XML</h1>
<p></p>
<p>Let&#8217;s look at a typical <code>XMLHttpRequest</code> again.</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout">var url = 'xml/070814.xml';</code><code class="code-breakout">var xmlhttp = new XMLHttpRequest();</code><code class="code-breakout">&nbsp;</code><code class="code-breakout">if (xmlhttp != null) {</code><code class="code-breakout" style="margin-left:18px;">xmlhttp.onreadystatechange = stateChange;</code><code class="code-breakout" style="margin-left:18px;">xmlhttp.open('GET', url, true);</code><code class="code-breakout" style="margin-left:18px;">xmlhttp.send(null);</code><code class="code-breakout">} else {</code><code class="code-breakout" style="margin-left:18px;">alert('Your browser does not support XMLHTTP.'); }</code><code class="code-breakout">}</code><code class="code-breakout">&nbsp;</code><code class="code-breakout">function stateChange() {</code><code class="code-breakout" style="margin-left:18px;">if (xmlhttp.readyState == 4) {</code><code class="code-breakout" style="margin-left:36px;">if (xmlhttp.status == 200) {</code><code class="code-breakout" style="margin-left:54px;">/*</code><code class="code-breakout" style="margin-left:54px;">A status of 200 means that the XML file was</code><code class="code-breakout" style="margin-left:54px;">loaded and parsed without error. At this point</code><code class="code-breakout" style="margin-left:54px;">you'll want to sort through all of the data so</code><code class="code-breakout" style="margin-left:54px;">that it can be used by your program. I'll give</code><code class="code-breakout" style="margin-left:54px;">you some tips on how to do this in the next</code><code class="code-breakout" style="margin-left:54px;">update.</code><code class="code-breakout" style="margin-left:54px;">*/</code><code class="code-breakout" style="margin-left:36px;">} else {</code><code class="code-breakout" style="margin-left:54px;">alert('Problem retrieving XML data.');</code><code class="code-breakout" style="margin-left:36px;">}</code><code class="code-breakout" style="margin-left:18px;">}</code><code class="code-breakout">}</code></div>
<p><span>Figure 1. The basic structure of an XMLHttpRequest</span></div>
<p></p>
<p><span id="more-217"></span></p>
<p>If the request succeeds without error, then we can use the <code>xmlhttp</code> object to access the contents of the loaded XML file. To understand how to work with an <code>XMLHttpRequest</code> object, let&#8217;s relate it to something a little more familiar to Web coders: the Javascript <code>Document</code> object.</p>
<p><code>document.getElementsByName('across')</code> will return an array containing all of the elements on your Web page whose <code>name</code> <em>attribute</em> is &#8220;across.&#8221; For example, <code>&lt;div name="across"&gt;</code>.</p>
<p><code>xmlhttp.responseXML.getElementsByTagName('across')</code> will return an array containing all of the &#8220;across&#8221; XML nodes. For example, <code>&lt;across&gt;Data&lt;/across&gt;</code>.</p>
<p>Nodes can have both <em>attributes</em> and <em>values</em>. Review Figure 2.</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</code><code class="code-breakout">&lt;editor&gt;Timothy Parker&lt;/editor&gt;</code><code class="code-breakout">&lt;across&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a1 a="POET" c="Burns or Byron" n="1" cn="1" /&gt;</code><code class="code-breakout">&lt;/across&gt;</code></div>
<p><span>Figure 2. <strong>c</strong> is an attribute of the <strong>a1</strong> node.<br />&#8220;Timothy Parker&#8221; is the value of the <strong>editor</strong> node.</span></div>
<p></p>
<p><code>getElementsByTagName</code> will always return an array. In the example above there is only one <code>a1</code> node, therefore there&#8217;s no reason to keep an <em>array</em> of <code>a1</code> nodes. Instead, using the code below, we can save only the first returned node (since that&#8217;s all that exists).</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout">var a1Node = xmlhttp.responseXML.getElementsByTagName('a1')[0];</code></div>
<p><span>Figure 3. Save a reference to the <strong>a1</strong> XML node in a variable named <strong>a1Node</strong>.</span></div>
<p></p>
<p>Now that we&#8217;ve captured the <code>a1</code> node specifically, we can access its <code>c</code> <em>attribute</em> using this simple code: <code>var c = a1.getAttribute('c');</code>.</p>
<p>Accessing node <em>attributes</em> is easy, but accessing node <em>values</em> is a little more confusing. Figure 4 demonstrates how to access the <em>value</em> of the <code>editor</code> node.</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout">var editor = xmlhttp.responseXML.getElementsByTagName('editor')[0];</code><code class="code-breakout">var editorValue = editor.firstChild.nodeValue;</code></div>
<p><span>Figure 4. Accessing the value of the <strong>editor</strong> node.</span></div>
<p></p>
<p>The <code>firstChild</code> method returns the first child of a node, duh. But the <code>editor</code> node does not appear to have any children. Well, it apparently (or not so apparently) does. When attempting to access the <em>value</em> of a node, imagine the value text as being a node itself. Access the <code>nodeValue</code> of the &#8220;text node&#8221; and you&#8217;ll get the result you&#8217;re looking for. Or, in more simple terms, always use <code>.firstChild.nodeValue</code> to access the value of a node.</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</code><code class="code-breakout">&lt;across&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a1 a="POET" c="Burns or Byron" n="1" cn="1" /&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a2 a="BLAB" c="Give everything away" n="6" cn="5" /&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a3 a="TALKS" c="Gives everything away" n="11" cn="9" /&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a4 a="ALSO" c="Too" n="16" cn="14" /&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a5 a="YOHO" c="Exclamation by Captain Jack Sparrow" n="21" cn="15" /&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a6 a="ELIOT" c="'A Cooking Egg' writer" n="26" cn="16" /&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a7 a="LIAR" c="Ananias, for one" n="31" cn="17" /&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a8 a="PREY" c="Target in the wild" n="36" cn="18" /&gt;</code><code class="code-breakout" style="margin-left:18px;">&lt;a9 a="ALONE" c="In isolation" n="41" cn="19" /&gt;</code><code class="code-breakout">&lt;/across&gt;</code></div>
<p><span>Figure 5. Another XML example.</span></div>
<p></p>
<p>In the above example, all of the <code>across</code> child nodes are named differently, so it would be cumbersome to <code>getElementsByTagName</code> each one separately. Instead, take advantage of the <code>childNodes</code> collection to return an array of the nodes.</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout"> var acrossNodes = xmlhttp.responseXML.getElementsByTagName('across')[0].childNodes;</code></div>
<p><span>Figure 6. Save all of the <strong>across</strong> child nodes to an array.</span></div>
<p></p>
<p>There is more to learn about <code>XMLHttpRequest</code>, but by simply knowing how to use the <code>getElementsByTagName</code>, <code>getAttribute</code>, <code>firstChild</code> and <code>nodeValue</code> methods and the <code>childNodes</code> collection, you&#8217;ll have an easy time retrieving all types of data from XML documents.</p>
<p>Before I close this lengthy post, there is one more thing I&#8217;d like to mention with regards to XML files. It appears that you can not use Javascript to access local XML files using XMLHttpRequest, you can only access files found on a domain. I am completely perplexed as to why this is and if anyone  has knowledge of the reason, or knows a workaround, I&#8217;d love to hear from you.</p>
<p><strong>&mdash; Ryan</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://js-kit.com/rss/www.sinceriously.com/p=217</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Crossword Web App, Part 5</title>
		<link>http://www.sinceriously.com/2009/03/crossword-web-app-part-5/</link>
		<comments>http://www.sinceriously.com/2009/03/crossword-web-app-part-5/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 21:33:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Game Designment]]></category>
		<category><![CDATA[crossword]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web app]]></category>

		<guid isPermaLink="false">http://www.sinceriously.com/?p=196</guid>
		<description><![CDATA[When I first started developing this crossword tutorial, I wanted the end game to access USA Today’s crossword XML feed directly and offer a new game each day. Sadly, their feed is secured. Although I can track it down, view its source in a Web browser, save to a local file and upload to my [...]]]></description>
			<content:encoded><![CDATA[<p>When I first started developing this crossword tutorial, I wanted the end game to access USA Today’s crossword XML feed directly and offer a new game each day. Sadly, their feed is secured. Although I can track it down, view its source in a Web browser, save to a local file and upload to my server, I cannot get my code to access the file directly from their server. Keep that in mind when we reach the end of this tutorial. You’ll be able to play one completed game, but don’t get too attached. There’s not much replayability.</p>
<h1>Part 5: Importing From XML</h1>
<p></p>
<p>Most programs, including games, require data. More often than not this data must be updated regularly so as to sustain the usefulness of the application. This type of data is usually stored externally (out of the application&#8217;s main code) since it makes very little sense to require the application&#8217;s user to download a new version of the program every day in order to receive the latest and greatest data. Therefore, with respect to our game, it makes a great deal of sense to store the data for each crossword puzzle in an external XML file. This way each puzzle can easily be loaded individually by referencing a different XML file.</p>
<p><span id="more-196"></span></p>
<p>To import data from an XML file, we use the XMLHttpRequest Javascript object as follows.</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout">var url = 'xml/070814.xml';</code><code class="code-breakout">var xmlhttp = new XMLHttpRequest(); </code><code class="code-breakout">&nbsp;</code><code class="code-breakout">if (xmlhttp != null) {</code><code class="code-breakout" style="margin-left:18px;">xmlhttp.onreadystatechange = stateChange;</code><code class="code-breakout" style="margin-left:18px;">xmlhttp.open('GET', url, true);</code><code class="code-breakout" style="margin-left:18px;">xmlhttp.send(null);</code><code class="code-breakout">} else {</code><code class="code-breakout" style="margin-left:18px;">alert('Your browser does not support XMLHTTP.'); }</code><code class="code-breakout">}</code><code class="code-breakout">&nbsp;</code><code class="code-breakout">function stateChange() {</code><code class="code-breakout" style="margin-left:18px;">if (xmlhttp.readyState == 4) {</code><code class="code-breakout" style="margin-left:36px;">if (xmlhttp.status == 200) {</code><code class="code-breakout" style="margin-left:54px;">/*</code><code class="code-breakout" style="margin-left:54px;">A status of 200 means that the XML file was</code><code class="code-breakout" style="margin-left:54px;">loaded and parsed without error. At this point</code><code class="code-breakout" style="margin-left:54px;">you'll want to sort through all of the data so</code><code class="code-breakout" style="margin-left:54px;">that it can be used by your program. I'll give</code><code class="code-breakout" style="margin-left:54px;">you some tips on how to do this in the next</code><code class="code-breakout" style="margin-left:54px;">update.</code><code class="code-breakout" style="margin-left:54px;">*/</code><code class="code-breakout" style="margin-left:36px;">} else {</code><code class="code-breakout" style="margin-left:54px;">alert('Problem retrieving XML data.');</code><code class="code-breakout" style="margin-left:36px;">}</code><code class="code-breakout" style="margin-left:18px;">}</code><code class="code-breakout">}</code></div>
<p><span>Figure 1. Using XMLHttpRequest to load and read an XML file</span></div>
<p></p>
<p>We&#8217;ll talk more about reading from XML files in the next update.</p>
<p><strong>&mdash;Ryan</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://js-kit.com/rss/www.sinceriously.com/p=196</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Crossword Web App, Part 4</title>
		<link>http://www.sinceriously.com/2009/03/crossword-web-app-part-4/</link>
		<comments>http://www.sinceriously.com/2009/03/crossword-web-app-part-4/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 21:35:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Game Designment]]></category>
		<category><![CDATA[crossword]]></category>
		<category><![CDATA[css sprites]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[preload images]]></category>
		<category><![CDATA[uri kitchen]]></category>
		<category><![CDATA[web app]]></category>

		<guid isPermaLink="false">http://www.sinceriously.com/?p=169</guid>
		<description><![CDATA[Unlike desktop applications, or native iPhone apps, whose code is pre-compiled with assets prepped and readily available in nearby bundles, Web apps are burdened with the task of downloading their assets (HTML, CSS, images, scripts, etc) in real time, as a user is attempting to interact with the app. In this context I&#8217;m referring to [...]]]></description>
			<content:encoded><![CDATA[<p><span>Unlike desktop applications, or native iPhone apps, whose code is pre-compiled with assets prepped and readily available in nearby bundles, Web apps are burdened with the task of downloading their assets (HTML, CSS, images, scripts, etc) in real time, as a user is attempting to interact with the app.</span> <span class="note">In this context I&#8217;m referring to a Web site as an &#8220;app&#8221; since so many of today&#8217;s Web 2.0 sites are robust enough to be considered applications. Also, Apple has often referred to iPhone Web sites as Web apps.</span> <span>HTML, CSS and scripts are generally low weight files, a couple of kilobytes a piece (some are larger, but most aren&#8217;t) and can be downloaded quickly. Images, on the other hand, can be very large&mdash;upwards of 100K in some instances. Downloading large images can be time consuming, especially on mobile devices, therefore you can improve your application&#8217;s user experience by downloading images ahead of time.</span></p>
<p>This part of the tutorial is actually not going to explain proper procedures for preloading images. There are a number of methods available, all of which can be found by doing a simple <a href="http://www.google.com/search?client=safari&#038;rls=en-us&#038;q=preload+images&#038;ie=UTF-8&#038;oe=UTF-8" onclick="window.open(this.href); return false;">Google search for &#8220;preload images&#8221;</a>. This article will instead concentrate on ways to reduce the necessity of preloading altogether by means of self-contained data and CSS Sprites.</p>
<p>I&#8217;ll start off by briefing explaining the importance of CSS Sprites (since Part 4 below will explain how to use self-contained data). <a href="http://www.alistapart.com/articles/sprites" onclick="window.open(this.href); return false;">An in-depth discussion of CSS Sprites can be read on A List Apart.</a> In a nutshell, a sprite map is a collection of smaller images mapped onto one large image. By combining images, you will reduce server requests and speed up download times. CSS Sprites especially come in handy when dealing with hovers.</p>
<p><span id="more-169"></span></p>
<p>Without sprites, displaying a different image for the hover state of an element requires making an extra call to the server for the new asset. This results in another download (at the time of mouseover), which can take time and create jerky mouseover experiences (especially in Internet Explorer). If a sprite map is used instead, the sprite can be setup to contain images for both the default state as well as the hover state. The sprite is downloaded as the page loads (if the element which will use the sprite is initially embedded on the page), and voil&aacute;, the hover state is downloaded with it and ready for use.</p>
<p>I&#8217;ve used sprites a couple of times on this site. To demonstrate how easy they are to use, let&#8217;s take a look at the &#8220;press&#8221; button at the bottom of the page.</p>
<div class="illustration"><img src="http://www.sinceriously.com/wp-content/themes/FREEmium/post-img/sprite_example.gif" alt="Example sprite image" width="286" height="106" /><span>Figure 1. Example sprite image</span><br />&nbsp;</div>
<p></p>
<p>The sprite is illustrated above. <span class="note">The actual image is a PNG and contains elements which would be hard to decipher on a light grey background. The turquoise box was added to make things more viewable.</span> Using an anchor tag as a button, we can crop out the backgrounds we need for the default and hover states using the following CSS.</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout">#press { background:url("img/press.png"); width:50px; height:50px; display:block; text-indent:-200em; overflow:hidden; }</code><code class="code-breakout">#press:hover { background-position:0 -50px; }</code></div>
<p><span>Figure 2. &#8220;Press&#8221; Button CSS</span></div>
<p></p>
<p><em>Background, width and height styles:</em> Because we are cropping the button to a width and height of 50 pixels, it is not necessary to specify a background position for the default state. Why? The default background appears at the top of our sprite. So as long as we assign a width and height that are the exact dimensions of the default background, the remainder of the background will extended past the boundaries of the button and will not be seen.</p>
<p><em>Display style:</em> By default, anchor tags display inline, so in order to represent the anchor as a button (and specify things like width and height), we need to force the anchor to display as a block element.</p>
<p><em>Text-indent and overflow styles:</em> If, in your markup, you include text within the anchor tag (for example: <code>&lt;a id="press" href="javascript:void(0)" onclick="dropBlock()"&gt;Press&lt;/a&gt;</code>), you can hide the text by assigning a negative text indent and hiding overflow.</p>
<p><em>The hover state:</em> Now, all we need to do in order to fire up the hover state of the button is offset the top background position of the background enough so that the hover image is used instead of the default image. In this case, we need to offset by 50 pixels. <span class="note">When a sprite map only includes two image states, the offset can be either positive or negative. Both will achieve the same result. When dealing with sprites with three or more states, I recommend strictly using negative values. You&#8217;ll understand why as you experiment more with sprites.</span></p>
<p>And that&#8217;s CSS Sprites in a nutshell. Now on to the actual Part 4 update where I&#8217;ll discuss self-contained data.</p>
<h1>Part 4: &#8216;Preloading&#8217; Commonly Used Game Assets</h1>
<p></p>
<p>If you&#8217;ve ever used Dreamweaver, you&#8217;re probably familiar with the code that&#8217;s used to preload images:</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout previous-code">function simplePreload() {</code><code class="code-breakout previous-code" style="margin-left:18px;">var args = simplePreload.arguments;</code><code class="code-breakout previous-code" style="margin-left:18px;">document.imageArray = new Array(args.length);</code><code class="code-breakout previous-code" style="margin-left:18px;">for (var i = 0; i < args.length; i++) {</code></code><code class="code-breakout" style="margin-left:36px;">document.imageArray[i] = new Image;</code><code class="code-breakout previous-code" style="margin-left:36px;">document.imageArray[i].src = args[i];</code><code class="code-breakout previous-code" style="margin-left:18px;">}</code><code class="code-breakout previous-code">}</code></div>
<p><span>Figure 3. Dreamweaver preload script</span></div>
<p></p>
<p>Look at the highlighted line. In Javascript you can create Image objects. By doing so, you can store images in the DOM&#8217;s (Document Object Model) memory and quickly access them later. But preloading images in this fashion still requires an HTTP call out to the image so that it can be downloaded. When designing games for the iPhone, we want to limit the number of HTTP requests as much as possible. There&#8217;s actually a way to embed some of your images in your Javascript file so that there&#8217;s no need to download them.</p>
<p>If you&#8217;ve ever tried to open a GIF or JPEG in a text editor you&#8217;ll notice that the guts of the image are a bunch of letters, numbers and symbols. There&#8217;s a free program online, called the URI Kitchen that will read an image and print out the letter/number/symbol/gremlin guts for you.</p>
<p>To use the program, go to <a href="http://software.hixie.ch/utilities/cgi/data/data" onclick="window.open(this.href); return false;">URI Kitchen</a>, upload a file and then copy the resulting data from the address bar. Here&#8217;s the data I get when feeding the program a graphic of the number 5 (one of the numbers used to mark the crossword grid):</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout">data:image/gif;base64,R0lGODlhAwAFAIAAAP%2F%2F%2F39%2FfyH5B AAAAAAALAAAAAADAAUAAAIFjANgqVsAOw%3D%3D</code></div>
<p><span>Figure 4. URI Kitchen image data</span></div>
<p></p>
<p>Pretty, huh? Now, take that data, and use it as follows:</p>
<div class="illustration">
<div class="code-wrap"><code class="code-breakout">var five = new Image();</code><code class="code-breakout">five.src = 'data:image/gif;base64,R0lGODlhAwAFAIAAAP%2F%2F%2F39%2FfyH5B AAAAAAALAAAAAADAAUAAAIFjANgqVsAOw%3D%3D'; </code></div>
<p><span>Figure 5. Using self-contained data to generate an image</span></div>
<p></p>
<p>I can now access <code>five</code> anytime I want to load that image in my game, and since it&#8217;s embedded in the code there&#8217;s no need to run off to the Internet to fetch it. I can draw the image to the canvas (at coordinate 100, 50) like this: <code>ctx.drawImage(questionMark, 100, 50)</code>.</p>
<p>Now, you won&#8217;t want to use this approach for every image in your game. You&#8217;ll still have to elegantly handle load times for large images. But this approach works great for smaller, commonly used images. What are the most commonly used assets in a crossword puzzle? The numbers used to label the grid, and the letters that will spell out each word. And those are the exact images that we&#8217;ll &#8220;preload&#8221; into Javascript using this method.</p>
<p>And this has officially been the longest blog post I&#8217;ve ever written. I hope you&#8217;ve enjoyed it!</p>
<p><strong>&mdash;Ryan</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://js-kit.com/rss/www.sinceriously.com/p=169</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
