<?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; css sprites</title>
	<atom:link href="http://www.sinceriously.com/tag/css-sprites/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>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>
