<?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>Emanuele Feronato &#187; Html</title>
	<atom:link href="http://www.emanueleferonato.com/category/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emanueleferonato.com</link>
	<description>italian geek and PROgrammer</description>
	<lastBuildDate>Mon, 06 Feb 2012 14:28:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Save the Totem: HTML5 game prototype made with Construct2</title>
		<link>http://www.emanueleferonato.com/2011/11/18/save-the-totem-html5-game-prototype-made-with-construct2/</link>
		<comments>http://www.emanueleferonato.com/2011/11/18/save-the-totem-html5-game-prototype-made-with-construct2/#comments</comments>
		<pubDate>Fri, 18 Nov 2011 14:50:12 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4848</guid>
		<description><![CDATA[A complete Totem Destroyer game prototype made with Construct2 dissected to make you see how everything was done.]]></description>
			<content:encoded><![CDATA[<p>As promised, let me show you how to make a game with <a href="http://www.scirra.com/construct2" target = "_blank">Construct2</a>.</p>
<p>First, let&#8217;s introduce the two main actors in the game: <strong>Layouts</strong> and <strong>Event sheets</strong>.</p>
<p><strong>Layouts</strong> are basically the equivalent of Flash <strong>Scenes</strong> and represent the &#8220;stuff&#8221; you place in your game, such as the splash screen, the level selection screen, the game itself, the game over screen, and so on.</p>
<p><strong>Event sheets</strong> contain what happens behind the scenes, such as events and subsequent actions.</p>
<p>While I recommend you to learn the bare bones (how to create a a project, how to import graphics, and so on) in the <a href="http://www.scirra.com/tutorials" target="_blank">official tutorial page</a>, I&#8217;ll show you the logic I used in the making of this little one-level game:</p>
<p><iframe frameborder = "0" src = "/stuff/totem_construct2/index.html" width="100%" height = "430"></iframe></p>
<p>First of all, let&#8217;s look at the project assets:</p>
<p><img src="/wp-content/uploads/2011/11/project.png" /></p>
<p>And now let&#8217;s see the game, layout by layout</p>
<p><strong>Splash Screen</strong></p>
<p>The splash screen just contains the background and a couple of texts: one with the name of the game, and one with the &#8220;click to play&#8221; message.</p>
<p>Once the player clicks anywhere on the screen, the game will begin, so the Event Sheet will be:</p>
<p><img src="/wp-content/uploads/2011/11/sheet01.png" /></p>
<p>You should read it this way: when an <code>On any click</code> event on the <code>Mouse</code> occurs, the <code>System</code> will make the action <code>Go to Game</code>.</p>
<p>That is, when the player clicks, the game begins.</p>
<p><strong>Game</strong></p>
<p>The game itself has just a few more events on the Event sheet. Let&#8217;s see them all:</p>
<p><img src="/wp-content/uploads/2011/11/sheet02.png" /></p>
<p>First, I am declaring a new global variable called <code>destroyed</code>. Now, look at the events:</p>
<p><strong>Event 1</strong>: on the start of the layout (think about <code>ADDED_TO_STAGE</code> if you love AS3) I am setting <code>destroyed</code> variable to <code>zero</code> and set the dynamic text to &#8220;Destroyed: 0/6&#8243;.</p>
<p>Look at the <code>&#038;</code> operator to concatenate strings.</p>
<p><strong>Event 2</strong>: this is a mouse event. When the player clicks with the left button on the small brick, the small brick is destroyed, the global variable <code>destroyed</code> is incremented and the dynamic text is updated.</p>
<p><strong>Event 3</strong>: same thing for the big brick, I just destroy it rather than updating everything for the sake of simplicity of this tutorial.</p>
<p><strong>Event 4</strong>: if the totem collides with the ground, the totem is destroyed, I show a game over message and set <code>destroyed</code> variable to <code>-1</code>.</p>
<p><strong>Event 5</strong>: if the mouse clicks anywhere and <code>destroyed</code> is equal to <code>-1</code> (game over), then restart this layout.</p>
<p><strong>Event 6</strong>: if <code>destroyed</code> is equal to 6 (game completed) show a congratulations message.</p>
<p><strong>Event 7</strong>: if the mouse clicks anywhere and <code>destroyed</code> is equal to <code>6</code> (game completed), then restart from the splash screen.</p>
<p>And that&#8217;s it, in a few minutes with a grand total of eight events this prototype is done. I will try to make a real game in a couple of days, and show you some ways to monetize HTML5 games.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/11/18/save-the-totem-html5-game-prototype-made-with-construct2/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Make HTML5 games with no programming skills (even with Box2D physics) with Construct 2</title>
		<link>http://www.emanueleferonato.com/2011/11/15/make-html5-games-with-no-programming-skills-even-with-box2d-physics-with-construct-2/</link>
		<comments>http://www.emanueleferonato.com/2011/11/15/make-html5-games-with-no-programming-skills-even-with-box2d-physics-with-construct-2/#comments</comments>
		<pubDate>Tue, 15 Nov 2011 17:38:18 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4835</guid>
		<description><![CDATA[A brief look at <a href = "http://www.scirra.com/" target = "_blank" >Construct 2</a>, the engine to create HTML5 games with no programming skills required.]]></description>
			<content:encoded><![CDATA[<p>If you want to be an HTML game developer, but your programming skills aren&#8217;t that good, then <strong>Tom Gullen</strong> and his company (actually, his brother) will save your life with <strong><a href="http://www.scirra.com/construct2" target="_blank">Construct 2</a></strong>.</p>
<p><a href="http://www.scirra.com/construct2" target="_blank"><img src="/wp-content/uploads/2011/11/constructbig.jpg" /></a></p>
<p>Construct 2 is a visual editor that lets you make HTML5 games without writing a single line of code, thanks to its event editor which makes building and creating games visual and easy for beginners and advanced users alike.</p>
<p>Do you remember <a href="http://www.emanueleferonato.com/2009/03/06/full-totem-destroyer-prototype/">Totem Destroyer</a>? Look at the HTML5 version I made in 10 minutes (half of them spent googling for the graphic assets) thanks to Construct 2.</p>
<p><iframe frameborder = "0" src = "/stuff/totem_construct/index.html" width="100%" height = "430"></iframe></p>
<p>Click on the bricks to destroy them, don&#8217;t let the totem fall on the ground or it will get destroyed too.</p>
<p>Look at the physics.</p>
<p>Tomorrow I will publish the second part of the game, as well as the &#8220;build it in 5 minutes tutorial&#8221;.</p>
<p>Meanwhile, here is a brief list of all Construct 2 features:</p>
<p>* <strong>Physics Engine</strong> &#8211; with a couple of clicks make objects behave realistically.</p>
<p>* <strong>Facebook connection</strong> &#8211; publish your games to Facebook. Log in, post hi-scores and see friend&#8217;s scores — even from your own website. Make your games go viral.</p>
<p>* <strong>Make money from your games</strong> &#8211; publish your Construct 2 games to the Chrome Web Store. Bring your games to an audience of millions.</p>
<p>Stay tuned because I&#8217;ll talk about it quite a lot in the next days.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/11/15/make-html5-games-with-no-programming-skills-even-with-box2d-physics-with-construct-2/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Making a game with HTML + jQuery Mobile and publishing it to Apple App Store &#8211; step 2</title>
		<link>http://www.emanueleferonato.com/2011/07/20/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store-step-2/</link>
		<comments>http://www.emanueleferonato.com/2011/07/20/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store-step-2/#comments</comments>
		<pubDate>Wed, 20 Jul 2011 17:47:22 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4473</guid>
		<description><![CDATA[Second step of the iPhone web app creation, this time featuring splash screen, status bar appearance and saving data locally.]]></description>
			<content:encoded><![CDATA[<p>Some days ago I showed you the <a href="http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/">basics of an HTML game optimized for the iPhone</a>.</p>
<p>Now we are going to improve the game with three exciting new features:</p>
<p>* Showing a splash/preloader screen<br />
* Changing iPhone top bar appearance<br />
* Saving data locally</p>
<p>To show a preloader we just need to add a line in the header of the page:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;link rel=&quot;apple-touch-startup-image&quot; href=&quot;splash.png&quot; /&gt;</pre></div></div>

<p>This will work only on iOS 3.0 and later, and <code>splash.png</code> (which you will change with any other path) is the splash image that is displayed while your web application launches, replacing the default screenshot of the web application taken the last time it was used.</p>
<p>Your image must have a size of 320&#215;460 pixels.</p>
<p>In the following picture, on the left the picture I used, on the right how it is shown on the iPhone.</p>
<p><img src="/wp-content/uploads/2011/07/iphone01.png" /></p>
<p>You can also change the status bar with just one line in the header:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;meta name=&quot;apple-mobile-web-app-status-bar-style&quot; content=&quot;black&quot; /&gt;</pre></div></div>

<p>This will work only if your app is launched in full screen mode (refer to <a href="http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/">step 1</a> to see how to launch a web app in full screen mode).</p>
<p>content attribute may have three values:</p>
<p>default: displays the status bar as you are used to see.</p>
<p>black: the status bar will have a black background.</p>
<p>black-translucent: the status bar is black and translucent. In this case, the web content is displayed even below the status bar, partially obscured by the status bar.</p>
<p>This is how my app looks before and after changing the status bar:</p>
<p><img src="/wp-content/uploads/2011/07/iphone02.png" /></p>
<p>As you can see, the status bar is black.</p>
<p>To save data locally, <code>localStorage</code> comes into our help as it acts like a browser local database, or something like a cookie, if you prefer.</p>
<p>That&#8217;s why in this script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</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>
	<span style="color: #003366; font-weight: bold;">var</span> currentNumber <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> score <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>localStorage.<span style="color: #660066;">topScore</span><span style="color: #339933;">==</span>undefined<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		localStorage.<span style="color: #660066;">topScore</span><span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#topscore&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Top score: &quot;</span><span style="color: #339933;">+</span>localStorage.<span style="color: #660066;">topScore</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#number&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>currentNumber<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Click higher or lower&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#higher&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click tap&quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</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>
		<span style="color: #003366; font-weight: bold;">var</span> newNumber <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>newNumber <span style="color: #339933;">&gt;=</span> currentNumber<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			score<span style="color: #339933;">++;</span>	
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>score<span style="color: #339933;">&gt;</span>localStorage.<span style="color: #660066;">topScore</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				localStorage.<span style="color: #660066;">topScore</span><span style="color: #339933;">=</span>score<span style="color: #339933;">;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#topscore&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Top score: &quot;</span><span style="color: #339933;">+</span>localStorage.<span style="color: #660066;">topScore</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Good! score: &quot;</span><span style="color: #339933;">+</span>score<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Bad! score: &quot;</span><span style="color: #339933;">+</span>score<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		currentNumber <span style="color: #339933;">=</span> newNumber<span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#number&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>currentNumber<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#lower&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click tap&quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</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>
		<span style="color: #003366; font-weight: bold;">var</span> newNumber <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>newNumber <span style="color: #339933;">&lt;=</span> currentNumber<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			score<span style="color: #339933;">++;</span>	
			<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>score<span style="color: #339933;">&gt;</span>localStorage.<span style="color: #660066;">topScore</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				localStorage.<span style="color: #660066;">topScore</span><span style="color: #339933;">=</span>score<span style="color: #339933;">;</span>
				$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#topscore&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Top score: &quot;</span><span style="color: #339933;">+</span>localStorage.<span style="color: #660066;">topScore</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Good! score: &quot;</span><span style="color: #339933;">+</span>score<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span>	
		<span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Bad! score: &quot;</span><span style="color: #339933;">+</span>score<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		currentNumber <span style="color: #339933;">=</span> newNumber<span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#number&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>currentNumber<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>At lines <strong>4-7</strong>, <strong>14-17</strong> and <strong>30-33</strong> we are using a variable called <code>topScore</code> saved into <code>localStorage</code> accessing it just like an ordinary variable.</p>
<p>And that&#8217;s enough for this step, no we have everything we need to create a complete game with high scores, which will be shown during next step. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/07/20/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store-step-2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Controlling an iPhone mobile game with swipes using jQuery</title>
		<link>http://www.emanueleferonato.com/2011/07/06/controlling-an-iphone-mobile-game-with-swipes-using-jquery/</link>
		<comments>http://www.emanueleferonato.com/2011/07/06/controlling-an-iphone-mobile-game-with-swipes-using-jquery/#comments</comments>
		<pubDate>Wed, 06 Jul 2011 08:28:35 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4417</guid>
		<description><![CDATA[Learn how to control characters and content in your web applications using  <strong><a href="http://labs.skinkers.com/touchSwipe/" target="_blank">TouchSwipe</a></strong> jQuery plugin.]]></description>
			<content:encoded><![CDATA[<p>Now that I&#8217;m trying to <a href="http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/">create an iPhone game using HTML + jQuery</a>, one of the most unusual things to develop in a web app is the swipe management.</p>
<p>Normally when you design web pages you don&#8217;t need to handle swipes because we expect the final user to interact with it with the mouse or the trackpad.</p>
<p>On mobile phones, people expect to use their fingers to interact with the content, and we have to meet their wishes if we want our app to be successful.</p>
<p>I am about to show you how easy it is: let&#8217;s start with the <a href="http://www.emanueleferonato.com/2010/06/14/creation-of-a-sokoban-game-with-jquery/">jQuery Sokoban prototype</a> I published about a year ago, and change keyboard input with swipe input.</p>
<p>Have a look at the code in the <a href="http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/">original version</a>, then look at this one:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;
	&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot;&gt;
&lt;head&gt;
     &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no&quot;/&gt;
     &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
     &lt;title&gt;jQuery Swipe Sokoban by Emanuele Feronato&lt;/title&gt;
     &lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
     &lt;script type=&quot;text/javascript&quot; src=&quot;touchswipe.js&quot;&gt;&lt;/script&gt; 
     <span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
     $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</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>	
     	<span style="color: #003366; font-weight: bold;">var</span> map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</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: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><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: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">3</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: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span><span style="color: #CC0000;">4</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</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: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	map<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<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: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     	<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>map.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     		<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>j<span style="color: #339933;">&lt;</span>map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     			<span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">:</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class = &quot;wall&quot; style = &quot;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class = &quot;goal&quot; style = &quot;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">:</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id = &quot;c'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">+</span><span style="color: #3366CC;">'_'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot; class = &quot;crate&quot; style = &quot;z-index:1000;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">:</span>
     					map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
     					<span style="color: #003366; font-weight: bold;">var</span> player_pos <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span>j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id =&quot;player&quot; style = &quot;z-index:1000;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">:</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class = &quot;goal&quot; style = &quot;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id = &quot;c'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">+</span><span style="color: #3366CC;">'_'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot; class = &quot;crate&quot; style = &quot;z-index:1000;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     				<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">:</span>
     					map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #CC0000;">2</span><span style="color: #339933;">;</span>
     					<span style="color: #003366; font-weight: bold;">var</span> player_pos <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span>j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div class = &quot;goal&quot; style = &quot;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#sokoban&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div id =&quot;player&quot; style = &quot;z-index:1000;top:'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px;left:'</span><span style="color: #339933;">+</span>j<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #339933;">+</span><span style="color: #3366CC;">'px&quot;&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     			<span style="color: #009900;">&#125;</span>
     		<span style="color: #009900;">&#125;</span>
     	<span style="color: #009900;">&#125;</span>
     	<span style="color: #003366; font-weight: bold;">function</span> possible_move<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">,</span>y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     		<span style="color: #003366; font-weight: bold;">var</span> tile <span style="color: #339933;">=</span> map<span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>y<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>x<span style="color: #009900;">&#93;</span>
     		<span style="color: #003366; font-weight: bold;">var</span> far_tile <span style="color: #339933;">=</span> map<span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>y<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>x<span style="color: #009900;">&#93;</span>
     		<span style="color: #003366; font-weight: bold;">var</span> can_move <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
     		<span style="color: #003366; font-weight: bold;">var</span> solved <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
     		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>tile<span style="color: #339933;">==</span><span style="color: #CC0000;">0</span> <span style="color: #339933;">||</span> tile<span style="color: #339933;">==</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    can_move <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                    solved <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
               <span style="color: #009900;">&#125;</span>
               <span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>tile<span style="color: #339933;">==</span><span style="color: #CC0000;">3</span> <span style="color: #339933;">||</span> tile<span style="color: #339933;">==</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>far_tile<span style="color: #339933;">==</span><span style="color: #CC0000;">0</span> <span style="color: #339933;">||</span> far_tile<span style="color: #339933;">==</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                         map<span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>y<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>x<span style="color: #009900;">&#93;</span><span style="color: #339933;">-=</span><span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
                         map<span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>y<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>x<span style="color: #009900;">&#93;</span><span style="color: #339933;">+=</span><span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
                         $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#c&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>x<span style="color: #009900;">&#41;</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>
                              left<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;+=&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                              top<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;+=&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>y<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span>
                         <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">100</span><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>
                              <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>map.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                                   <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>j<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>j<span style="color: #339933;">&lt;</span>map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span>j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                                        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>map<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                                             solved <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                                             <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                                        <span style="color: #009900;">&#125;</span>
                                   <span style="color: #009900;">&#125;</span>
                              <span style="color: #009900;">&#125;</span>
                              <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>solved<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                                   <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;SOLVED&quot;</span><span style="color: #009900;">&#41;</span>
                              <span style="color: #009900;">&#125;</span>
                         <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;_&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span><span style="color: #CC0000;">2</span><span style="color: #339933;">*</span>x<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                         can_move <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
               <span style="color: #009900;">&#125;</span>	
               <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>can_move<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+=</span>y<span style="color: #339933;">;</span>
                    player_pos<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+=</span>x<span style="color: #339933;">;</span>
     			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#player&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>
     				left<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;+=&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                         top<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;+=&quot;</span><span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>y<span style="color: #339933;">*</span><span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span>
     			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
               <span style="color: #009900;">&#125;</span>
     	<span style="color: #009900;">&#125;</span>
        	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#player&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">swipe</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>swipe<span style="color: #339933;">:</span>swipe<span style="color: #339933;">,</span>threshold<span style="color: #339933;">:</span><span style="color: #CC0000;">50</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        	<span style="color: #003366; font-weight: bold;">function</span> swipe<span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span>direction<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>direction<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     			<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">&quot;left&quot;</span><span style="color: #339933;">:</span>
     				possible_move<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
     				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     			<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">&quot;up&quot;</span><span style="color: #339933;">:</span>
     				possible_move<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
     				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     			<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">&quot;right&quot;</span><span style="color: #339933;">:</span>
     				possible_move<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span>
     				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     			<span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">&quot;down&quot;</span><span style="color: #339933;">:</span>
     				possible_move<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>
     				<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
     		<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
     &lt;style type=&quot;text/css&quot;&gt;
     .wall{
     	background-color:#000000;
     	width:32px;
     	height:32px;
     	position:absolute;
     }
&nbsp;
     .goal{
     	background-color:#ff0000;
     	width:32px;
     	height:32px;
     	position:absolute;	
     }
&nbsp;
     .crate{
     	border:10px solid #00ff00;
     	width:12px;
     	height:12px;
     	position:absolute;	
     }
&nbsp;
     #player{
     	border:10px solid #0000ff;
     	width:12px;
     	height:12px;
     	position:absolute;	
     }
     &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
     &lt;div id = &quot;sokoban&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>At <strong>line 5</strong> we have the <code>viewport</code> meta declaration as explained in <a href="http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/">this post</a> but the core lies at <strong>line 9</strong> when I call <strong><a href="http://labs.skinkers.com/touchSwipe/" target="_blank">TouchSwipe</a></strong>, a jQuery plugin for touch devices.</p>
<p>Everything is managed by this fantastic plugin, since among its features you can find:</p>
<p>* Detects swipes in 4 directions, &#8220;up&#8221;, &#8220;down&#8221;, &#8220;left&#8221; and &#8220;right&#8221;<br />
* Supports single finger or double finger touch events<br />
* Supports click events both on the touchSwipe object and its child objects<br />
* Definable threshold to determin when a gesture is actually a swipe<br />
* Events triggered for swipe &#8220;start&#8221;,&#8221;move&#8221;,&#8221;end&#8221; and &#8220;cancel&#8221;<br />
* End event can be triggered either on touch release, or as soon as threshold is met<br />
* Allows swiping and page scrolling</p>
<p>So WASD key interaction you can find in the <a href="http://www.emanueleferonato.com/2010/06/14/creation-of-a-sokoban-game-with-jquery/">original version</a> has been replaced by swipe interaction at <strong>lines 90-106</strong>, where every swipe longer than 50 pixels is translated into a movement.</p>
<p>To test it, point your mobile browser to <a href="http://www.emanueleferonato.com/stuff/touchban/" target="_blank">this page</a>, put your finger on the hero (the blue square) and move it with a swipe.</p>
<p>That wasy? Yes, that easy. The App store is getting closer and closer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/07/06/controlling-an-iphone-mobile-game-with-swipes-using-jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Making a game with HTML + jQuery Mobile and publishing it to Apple App Store</title>
		<link>http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/</link>
		<comments>http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/#comments</comments>
		<pubDate>Thu, 30 Jun 2011 08:39:55 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=4379</guid>
		<description><![CDATA[The first attempt to create an iPhone game starting from a web page.]]></description>
			<content:encoded><![CDATA[<p>During these days I am playing with HTML to build an iPhone web app and publish it on the <a href="http://www.apple.com/iphone/apps-for-iphone/" target = "_blank">Apple App Store</a>.</p>
<p>It will be a long journey, so let&#8217;s start with something simple: a jQuery version of <a href="http://www.emanueleferonato.com/2009/03/19/magormin-an-old-php-based-game/">MagOrMin &#8211; an old php based game</a>, whose concept have been applied for the creation of <a href="http://www.emanueleferonato.com/2007/12/30/guessnext-complete-flash-game-with-highscores/">GuessNext</a> Flash game.</p>
<p>The first step in making an HTML app for iPhone is making an HTML page.</p>
<p>And this is main, the most basic possible:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&nbsp;
&lt;head&gt;
&nbsp;
	&lt;script src = &quot;jquery.js&quot;&gt;&lt;/script&gt;
	&lt;script src = &quot;game.js&quot;&gt;&lt;/script&gt;
&nbsp;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&nbsp;
&lt;div id = &quot;number&quot;&gt;&lt;/div&gt;
&lt;div id = &quot;higher&quot;&gt;higher&lt;/div&gt;
&lt;div id = &quot;lower&quot;&gt;lower&lt;/div&gt;
&lt;div id = &quot;comment&quot;&gt;&lt;/div&gt;
&nbsp;
&lt;/body&gt;	
&nbsp;
&lt;/html&gt;</pre></td></tr></table></div>

<p>Where jquery.js contains the latest <a href="http://docs.jquery.com/Downloading_jQuery" target = "_blank">jQuery distribution</a> and game.js is this script:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</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>
	<span style="color: #003366; font-weight: bold;">var</span> currentNumber <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> score <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#number&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>currentNumber<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Click higher or lower&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#higher&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</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>
		<span style="color: #003366; font-weight: bold;">var</span> newNumber <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>newNumber <span style="color: #339933;">&gt;=</span> currentNumber<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			score<span style="color: #339933;">++;</span>	
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Good! score: &quot;</span><span style="color: #339933;">+</span>score<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Bad! score: &quot;</span><span style="color: #339933;">+</span>score<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		currentNumber <span style="color: #339933;">=</span> newNumber<span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#number&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>currentNumber<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#lower&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</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>
		<span style="color: #003366; font-weight: bold;">var</span> newNumber <span style="color: #339933;">=</span> Math.<span style="color: #660066;">floor</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>newNumber <span style="color: #339933;">&lt;=</span> currentNumber<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			score<span style="color: #339933;">++;</span>	
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Good! score: &quot;</span><span style="color: #339933;">+</span>score<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		<span style="color: #009900;">&#125;</span>	
		<span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comment&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Bad! score: &quot;</span><span style="color: #339933;">+</span>score<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		currentNumber <span style="color: #339933;">=</span> newNumber<span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#number&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>currentNumber<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Explaining HTML and jQuery is beyond the scope of this post, just notice there are four divs:</p>
<p><strong>number</strong> contains a random integer number from 0 to 9</p>
<p><strong>higher</strong> is a clickable div and when the player clicks on it then he guesses next random number will be higher (or equal) than the current one</p>
<p><strong>lower</strong> follows the same concept as <code>higher</code> but the guess assumes next random number will be lower or equal than the current one</p>
<p><strong>comment</strong> contains in-game comments, such as &#8220;Good!&#8221;, &#8220;Bad!&#8221; and the current score.</p>
<p>At this stage the game has no limit, no game over, no high scores, nothing. But it&#8217;s a good start.</p>
<p>This is what you get if you upload the scripts on a page and browse it with your iPhone:</p>
<p><img src="/wp-content/uploads/2011/06/iphone01.jpg" /></p>
<p>You can even play it, but the text is so tiny!</p>
<p>This happens because unless you tell it otherwise, Safari on the iPhone is going to assume that your page width is 980px.</p>
<p>So we are going to add this line between <code>&lt;head&gt;</code> and <code>&lt;\head&gt;</code> to specify the viewport:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;meta name=&quot;viewport&quot; content=&quot;user-scalable=no, width=device-width&quot; /&gt;</pre></div></div>

<p>This is how the same page looks with the correct <code>viewport</code> meta data:</p>
<p><img src="/wp-content/uploads/2011/06/iphone02.jpg" /></p>
<p>Much better now&#8230; and more playable since the &#8220;buttons&#8221; are bigger.</p>
<p>But it&#8217;s obvious we won&#8217;t go so far without a library designed with mobile devices in mind.</p>
<p>This is when <a href="http://jquerymobile.com/" target = "_blank">jQuery Mobile</a> comes into play. It&#8217;s an unified user interface system across all popular mobile device platforms, built on the rock-solid jQuery and jQuery UI foundation, and that&#8217;s what we need.</p>
<p>Now it&#8217;s time to heavily change our html:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html&gt; 
&nbsp;
&lt;html&gt;
&nbsp;
&lt;head&gt;
&nbsp;
	&lt;meta name=&quot;viewport&quot; content=&quot;user-scalable=no, width=device-width&quot; /&gt;
&nbsp;
	&lt;link rel=&quot;stylesheet&quot; href=&quot;jquery.mobile-1.0b1.min.css&quot; /&gt;
&nbsp;
	&lt;script src = &quot;jquery.js&quot;&gt;&lt;/script&gt;
	&lt;script src=&quot;jquery.mobile-1.0b1.min.js&quot;&gt;&lt;/script&gt;
	&lt;script src = &quot;game.js&quot;&gt;&lt;/script&gt;
&nbsp;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&nbsp;
&lt;div data-role=&quot;page&quot;&gt;
&nbsp;
	&lt;div data-role=&quot;header&quot;&gt;
		&lt;h1&gt;Page Title&lt;/h1&gt;
	&lt;/div&gt;
&nbsp;
	&lt;div data-role=&quot;content&quot;&gt;	
		&lt;div id = &quot;number&quot;&gt;&lt;/div&gt;
		&lt;div id = &quot;higher&quot;&gt;higher&lt;/div&gt;
		&lt;div id = &quot;lower&quot;&gt;lower&lt;/div&gt;
		&lt;div id = &quot;comment&quot;&gt;&lt;/div&gt;		
	&lt;/div&gt;
&nbsp;
	&lt;div data-role=&quot;footer&quot;&gt;
		&lt;h4&gt;Page Footer&lt;/h4&gt;
	&lt;/div&gt;
&lt;/div&gt;
&nbsp;
&lt;/body&gt;
&nbsp;
&lt;/html&gt;</pre></td></tr></table></div>

<p><strong>Line 9</strong>: imports jQuery mobile stylesheet.</p>
<p><strong>Line12</strong>: imports jQuery Mobile library. Both the library and the stylesheet can be downloaded from <a href="http://jquerymobile.com/download/" target = "_blank">this page</a></p>
<p><strong>Line 19</strong>: we are meeting our first <code>data-role</code> attribute. This will tell jQuery Mobile the role of the div in the mobile page. With <code>data-role="page"</code> we tell jQM it&#8217;s the beginning of a mobile page.</p>
<p><strong>Line 21</strong>: <code>data-role="header"</code> tells us we are working wit the header of the page.</p>
<p><strong>Line 25</strong>: <code>data-role="content"</code> tells us we are working wit the content of the page.</p>
<p><strong>Line 32</strong>: <code>data-role="footer"</code> tells us we are working wit the footer of the page.</p>
<p>Once the browser is pointed on the page, jQM automatically triggers all HTML5 <code>data-role</code> attributes to render the page with all necessary widgets. That&#8217;s what we have now:</p>
<p><img src="/wp-content/uploads/2011/06/iphone03.jpg" /></p>
<p>This is not that good yet, as the footer isn&#8217;t at the bottom of the page but just when the page itself ends. To have the footer (and the header) fixed on the bottom (and on the top) of the page, change <strong>lines 21</strong> and <strong>32</strong> respectively with:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>21
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div data-role=&quot;header&quot; data-position=&quot;fixed&quot;&gt;</pre></td></tr></table></div>

<p>and</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>32
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div data-role=&quot;footer&quot; data-position=&quot;fixed&quot;&gt;</pre></td></tr></table></div>

<p>and now your header and footer will be fixed at the very top and at the very bottom of the page, this way:</p>
<p><img src="/wp-content/uploads/2011/06/iphone04.jpg" /></p>
<p>Anyway, we are greedy and we want our page to go in full screen mode, without showing the address bar at the top and the toolbar at the bottom.</p>
<p>This will be made in two steps: first, we have to insert this line between <code>&lt;head&gt;</code> and <code>&lt;\head&gt;</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;meta name=&quot;apple-mobile-web-app-capable&quot; content=&quot;yes&quot; /&gt;</pre></div></div>

<p>Then, we have to launch the web page from Home, that is the iPhone &#8220;desktop&#8221;. Add the page to home: a custom icon will be generated with the thumbnail of the page:</p>
<p><img src="/wp-content/uploads/2011/06/iphone06.jpg" /></p>
<p>and once you call it directly from the icon you will get your full page navigation.</p>
<p><img src="/wp-content/uploads/2011/06/iphone07.jpg" /></p>
<p>The last thing we will see in this first part is how to create two mobile friendly buttons to control &#8220;higher&#8221; and &#8220;lower&#8221; guesses.</p>
<p>Change your content block this way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>25
26
27
28
29
30
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div data-role=&quot;content&quot;&gt;	
	&lt;div id = &quot;number&quot;&gt;&lt;/div&gt;
	&lt;a href=&quot;#&quot; data-role=&quot;button&quot; id = &quot;higher&quot;&gt;Higher&lt;/a&gt;
	&lt;a href=&quot;#&quot; data-role=&quot;button&quot; id = &quot;lower&quot;&gt;Lower&lt;/a&gt;
	&lt;div id = &quot;comment&quot;&gt;&lt;/div&gt;		
&lt;/div&gt;</pre></td></tr></table></div>

<p>Button divs have been changed to anchor texts with <code>data-role="button"</code>. This will make jQuery Mobile render the links as buttons.</p>
<p>This is how the page looks now:</p>
<p><img src="/wp-content/uploads/2011/06/iphone05.jpg" /></p>
<p>But to interact with them we have to change a bit the javascript changing<strong> lines 6</strong> and <strong>18</strong> from respectively</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#higher&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</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></pre></td></tr></table></div>

<p>and</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>18
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#lower&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</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></pre></td></tr></table></div>

<p>to respectively</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#higher&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click tap&quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</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></pre></td></tr></table></div>

<p>and</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>18
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#lower&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click tap&quot;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#40;</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></pre></td></tr></table></div>

<p>And finally you will be able to play again using the new buttons.</p>
<p>This is where this tutorial ends, but there&#8217;s still a lot to do. During next step we&#8217;ll build a preloader, save high scores and enhance the visual appeal of the game.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/06/30/making-a-game-with-html-jquery-mobile-and-publishing-it-to-apple-app-store/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Create awesome HTML5 games with Impact</title>
		<link>http://www.emanueleferonato.com/2011/04/07/create-awesome-html5-games-with-impact/</link>
		<comments>http://www.emanueleferonato.com/2011/04/07/create-awesome-html5-games-with-impact/#comments</comments>
		<pubDate>Thu, 07 Apr 2011 17:09:36 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Game design]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3967</guid>
		<description><![CDATA[Build HTML5 Games in no time with Impact JavaScript Game Engine. Games created with Impact require no browser plugins or any software installations – just a modern browser that supports the Canvas tag.]]></description>
			<content:encoded><![CDATA[<p>Today I want to show you an impressive library you can use to create great HTML5 games: <strong><a target = "_blank" href="http://impactjs.com/">Impact</a></strong>.</p>
<p><a href="http://impactjs.com/" target = "_blank"><img src="/wp-content/uploads/2011/04/impact.jpg" /></a></p>
<p>Impact is a JavaScript Game Engine that allows you to develop HTML5 Games in no time. Games created with Impact require no browser plugins or any software installations – just a modern browser that supports the Canvas tag.</p>
<p>Impact runs on all HTML5 capable browsers: Firefox, Chrome, Safari, Opera and (behold) even Internet Explorer 9. Of course this also includes the iPhone, iPod Touch and iPad.</p>
<p>I got a license from <strong>Dominic Szablewski</strong>, the author, and I&#8217;ll start playing with the engine in a couple of days, releasing some tutorials about making a game with this tool.</p>
<p><a href="http://playbiolab.com/" target = "_blank"><img src="/wp-content/uploads/2011/04/biolab.jpg" /></a></p>
<p>Meanwhile, visit the official site or play <strong><a target = "_blank" href="http://playbiolab.com/">Biolab Disaster</a></strong>, a browser game made with Impact. You&#8217;ll find it has the same look &#038; feel of Flash games made with Flixel or FlashPunk.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2011/04/07/create-awesome-html5-games-with-impact/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>JavaScript analog clock with no images and no CSS</title>
		<link>http://www.emanueleferonato.com/2010/12/11/javascript-analog-clock-with-no-images-and-no-css/</link>
		<comments>http://www.emanueleferonato.com/2010/12/11/javascript-analog-clock-with-no-images-and-no-css/#comments</comments>
		<pubDate>Fri, 10 Dec 2010 23:48:45 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Html]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3588</guid>
		<description><![CDATA[There are a lot JavaScript analog clocks out there, but most of them use images or CSS3 tricks to make them move. I am showing you an analog clock made without any image and without any style thanks to Raphaël JavaScript Library. Raphaël is a JavaScript library that let you your work with vector graphics [...]]]></description>
			<content:encoded><![CDATA[<p>There are a lot JavaScript analog clocks out there, but most of them use images or CSS3 tricks to make them move.</p>
<p>I am showing you an analog clock made without any image and without any style thanks to <strong><a href="http://raphaeljs.com/" target = "_blank">Raphaël JavaScript Library</a></strong>.</p>
<p><strong>Raphaël</strong> is a JavaScript library that let you your work with vector graphics on the web using the SVG W3C Recommendation and VML as a base for creating graphics which works on Firefox 3.0+, Safari 3.0+, Chrome 5.0+, Opera 9.5+ and Internet Explorer 6.0+.</p>
<p>This is what we are going to create:</p>
<p><iframe src = "/stuff/triquiclock/index.html" frameborder= "0" scrolling="no" width = "640" height = "232"></iframe></p>
<p>And as you can see from the source code there isn&#8217;t any image or CSS style as the clock is fully made with vectors.<span id="more-3588"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt; 
&lt;html lang=&quot;en&quot;&gt; 
	&lt;head&gt; 
		&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;&gt; 
		&lt;title&gt;Triqui Clock&lt;/title&gt; 
		&lt;script type=&quot;text/javascript&quot; src=&quot;raphael-min.js&quot;&gt;&lt;/script&gt; 
		<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">function</span> draw_clock<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				canvas <span style="color: #339933;">=</span> Raphael<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;clock_id&quot;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> clock <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">circle</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span><span style="color: #CC0000;">95</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			     clock.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;fill&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;#f5f5f5&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;stroke&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;#444444&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;stroke-width&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;5&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>  
			     <span style="color: #003366; font-weight: bold;">var</span> hour_sign<span style="color: #339933;">;</span>
				<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><span style="color: #CC0000;">12</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #003366; font-weight: bold;">var</span> start_x <span style="color: #339933;">=</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">+</span>Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">80</span><span style="color: #339933;">*</span>Math.<span style="color: #660066;">cos</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">30</span><span style="color: #339933;">*</span>i<span style="color: #339933;">*</span>Math.<span style="color: #660066;">PI</span><span style="color: #339933;">/</span><span style="color: #CC0000;">180</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #003366; font-weight: bold;">var</span> start_y <span style="color: #339933;">=</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">+</span>Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">80</span><span style="color: #339933;">*</span>Math.<span style="color: #660066;">sin</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">30</span><span style="color: #339933;">*</span>i<span style="color: #339933;">*</span>Math.<span style="color: #660066;">PI</span><span style="color: #339933;">/</span><span style="color: #CC0000;">180</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #003366; font-weight: bold;">var</span> end_x <span style="color: #339933;">=</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">+</span>Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">90</span><span style="color: #339933;">*</span>Math.<span style="color: #660066;">cos</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">30</span><span style="color: #339933;">*</span>i<span style="color: #339933;">*</span>Math.<span style="color: #660066;">PI</span><span style="color: #339933;">/</span><span style="color: #CC0000;">180</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #003366; font-weight: bold;">var</span> end_y <span style="color: #339933;">=</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">+</span>Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">90</span><span style="color: #339933;">*</span>Math.<span style="color: #660066;">sin</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">30</span><span style="color: #339933;">*</span>i<span style="color: #339933;">*</span>Math.<span style="color: #660066;">PI</span><span style="color: #339933;">/</span><span style="color: #CC0000;">180</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
					hour_sign <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">path</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;M&quot;</span><span style="color: #339933;">+</span>start_x<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #339933;">+</span>start_y<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;L&quot;</span><span style="color: #339933;">+</span>end_x<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #339933;">+</span>end_y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>    
				hour_hand <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">path</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;M100 100L100 50&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				hour_hand.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>stroke<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#444444&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;stroke-width&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">6</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				minute_hand <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">path</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;M100 100L100 40&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				minute_hand.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>stroke<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#444444&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;stroke-width&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				second_hand <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">path</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;M100 110L100 25&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				second_hand.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>stroke<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#444444&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;stroke-width&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
				<span style="color: #003366; font-weight: bold;">var</span> pin <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">circle</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				pin.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;fill&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;#000000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
				update_clock<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
				setInterval<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;update_clock()&quot;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #003366; font-weight: bold;">function</span> update_clock<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #003366; font-weight: bold;">var</span> now <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> hours <span style="color: #339933;">=</span> now.<span style="color: #660066;">getHours</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> minutes <span style="color: #339933;">=</span> now.<span style="color: #660066;">getMinutes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #003366; font-weight: bold;">var</span> seconds <span style="color: #339933;">=</span> now.<span style="color: #660066;">getSeconds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				hour_hand.<span style="color: #660066;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">30</span><span style="color: #339933;">*</span>hours<span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>minutes<span style="color: #339933;">/</span><span style="color: #CC0000;">2.5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				minute_hand.<span style="color: #660066;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">6</span><span style="color: #339933;">*</span>minutes<span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				second_hand.<span style="color: #660066;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">6</span><span style="color: #339933;">*</span>seconds<span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&nbsp;
	&lt;/head&gt; 
	&lt;body&gt; 
		&lt;div id=&quot;clock_id&quot;&gt;&lt;/div&gt;
		&lt;script&gt;draw_clock()&lt;/script&gt;
	&lt;/body&gt; 
&lt;/html&gt;</pre></td></tr></table></div>

<p>let&#8217;s explore it in detail:</p>
<p><strong>Line 6</strong>: loading <strong>Raphaël</strong> library</p>
<p><strong>Line 10</strong>: creation of a canvas object called <code>canvas</code> on which we will draw the clock. The parameters are the id of the container (<code>clock_id</code>, you can find it at <strong>line 48</strong>), the width and the height of the object, in pixels</p>
<p><strong>Line 11</strong>: drawing a circle called <code>clock</code> with center at 100,100 with a 95 pixels radius.</p>
<p><strong>Line 12</strong>: setting some attributes for <code>clock</code> (the circle): <code>fill":"#f5f5f5"</code> will fill the circle with that color ,<code>"stroke":"#444444"</code> will decide the stroke color and <code>"stroke-width":"5"</code> is the width, in pixels, of the stroke.</p>
<p><strong>Line 13</strong>: creating a variable called <code>hour_sign</code> that we&#8217;ll use to draw the hours.</p>
<p><strong>Lines 14-20</strong>: the <code>for</code> loop goes through all 12 hours representable in a clock, uses trigonometry to find the starting and ending point of each hour sign, then uses path method to create a path element according to the string passed as argument which represents the path in SVG format. For more information about this format, check the official <a href="http://www.w3.org/TR/SVG/paths.html#PathData" target = "_blank">SVG docs</a>. Anyway, it&#8217;s a very easy format since it follows the &#8220;move and line&#8221; theory, just like AS3.</p>
<p>If we look at <strong>line 21</strong>, which draws the hour hand</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>hour_hand <span style="color: #339933;">=</span> canvas.<span style="color: #660066;">path</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;M100 100L100 50&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>it can be translated into AS3 this way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;</span>canvas<span style="color: #000066; font-weight: bold;">.</span>graphichs<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">moveTo</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span>
canvas<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">lineTo</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">50</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>We keep drawing clock parts until <strong>line 29</strong>, using the same methods.</p>
<p>At <strong>line 30</strong> we call <code>update_clock</code> function which will place all hands in the correct position according to your computer&#8217;s time, then at <strong>line 31</strong> we make such function being executed at every second.</p>
<p><code>update_clock</code> just determines current hours, minutes and seconds and updates the hands using <code>rotate</code> method which wants as arguments the amount of degrees to rotate the object and the center of rotation.</p>
<p>The degrees are determined by dividing 360 by the number of possible values (60 for seconds and minutes, 12 for hours) then multiplying such value by the current hours/minutes/seconds.</p>
<p>Hour hand is also rotated a bit more as minutes pass by, just like in real clocks.</p>
<p>Just notice the perfect code for the hour hand would be</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>hour_hand.<span style="color: #660066;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">30</span><span style="color: #339933;">*</span>hours<span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>minutes<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;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>rather than</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>hour_hand.<span style="color: #660066;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">30</span><span style="color: #339933;">*</span>hours<span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>minutes<span style="color: #339933;">/</span><span style="color: #CC0000;">2.5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>but dividing <code>minutes</code> by <code>2.5</code> rather than <code>2</code> will make hour hand rotate slower and will give a better visual effect.</p>
<p>Hope you will like <a href="http://raphaeljs.com/" target = "_blank">Raphaël JavaScript Library</a>, I can show you how to make a little puzzle game with it if I receive a good feedback.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/12/11/javascript-analog-clock-with-no-images-and-no-css/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>Full screen centered background image with CSS and jQuery # 2</title>
		<link>http://www.emanueleferonato.com/2010/11/16/full-screen-centered-background-image-with-css-and-jquery-2/</link>
		<comments>http://www.emanueleferonato.com/2010/11/16/full-screen-centered-background-image-with-css-and-jquery-2/#comments</comments>
		<pubDate>Tue, 16 Nov 2010 21:41:44 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Css]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3515</guid>
		<description><![CDATA[There are a couple of improvements to do to Full screen centered background image with CSS and jQuery to add some functionality such as window resizing management and make it compatible with Opera. First, you should change the CSS to &#60;style type=&#34;text/css&#34;&#62; #triquiback&#123; left: 0; top: 0; position:fixed; overflow:hidden; zIndex: -9999 &#125; #triquibackimg&#123; position:relative &#125; [...]]]></description>
			<content:encoded><![CDATA[<p>There are a couple of improvements to do to <a href="http://www.emanueleferonato.com/2010/11/13/full-screen-centered-background-image-with-css-and-jquery/">Full screen centered background image with CSS and jQuery</a> to add some functionality such as window resizing management and make it compatible with Opera.</p>
<p>First, you should change the CSS to</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">&lt;style type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span><span style="color: #00AA00;">&gt;</span>
     <span style="color: #cc00cc;">#triquiback</span><span style="color: #00AA00;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
          <span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">;</span>
          <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">fixed</span><span style="color: #00AA00;">;</span>
          <span style="color: #000000; font-weight: bold;">overflow</span><span style="color: #00AA00;">:</span><span style="color: #993333;">hidden</span><span style="color: #00AA00;">;</span>
          zIndex<span style="color: #00AA00;">:</span> -<span style="color: #cc66cc;">9999</span>
     <span style="color: #00AA00;">&#125;</span>
     <span style="color: #cc00cc;">#triquibackimg</span><span style="color: #00AA00;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">position</span><span style="color: #00AA00;">:</span><span style="color: #993333;">relative</span>    
     <span style="color: #00AA00;">&#125;</span>      
&lt;/style<span style="color: #00AA00;">&gt;</span></pre></div></div>

<p>Setting <code>triquibackimg</code>&#8216;s position to <code>relative</code> rather than <code>fixed</code> will make it work in Opera too. Then since we want to resize the background image when it loads and when the user resizes the browser, I made a function to handle resizing:<span id="more-3515"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> resize_bg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
     $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#triquibackimg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;left&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #003366; font-weight: bold;">var</span> doc_width <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #003366; font-weight: bold;">var</span> doc_height <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #003366; font-weight: bold;">var</span> image_width <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#triquibackimg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #003366; font-weight: bold;">var</span> image_height <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#triquibackimg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #003366; font-weight: bold;">var</span> image_ratio <span style="color: #339933;">=</span> image_width<span style="color: #339933;">/</span>image_height<span style="color: #339933;">;</span>      
     <span style="color: #003366; font-weight: bold;">var</span> new_width <span style="color: #339933;">=</span> doc_width<span style="color: #339933;">;</span>
     <span style="color: #003366; font-weight: bold;">var</span> new_height <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>new_width<span style="color: #339933;">/</span>image_ratio<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>new_height<span style="color: #339933;">&lt;</span>doc_height<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          new_height <span style="color: #339933;">=</span> doc_height<span style="color: #339933;">;</span>
          new_width <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>new_height<span style="color: #339933;">*</span>image_ratio<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> width_offset <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>new_width<span style="color: #339933;">-</span>doc_width<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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#triquibackimg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;left&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;-&quot;</span><span style="color: #339933;">+</span>width_offset<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
     $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#triquibackimg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span>new_width<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#triquibackimg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span>new_height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I removed all the alerts since they became useless. First, I reset image&#8217;s left position to zero (<strong>line 2</strong>), to align it to top left corner. I am doing this because at <strong>line 14</strong> left position can be changed due to background alignment.</p>
<p>The remaining script is the same you saw at <a href="http://www.emanueleferonato.com/2010/11/13/full-screen-centered-background-image-with-css-and-jquery/">step 1</a>, with the only exception <code>doc_width</code> and <code>doc_height</code> are determined according to window size rather than document size. I found it to be more accurate.</p>
<p>At this time, the entire javascript can be written this way:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</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>
     $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#triquibackimg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</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>
          resize_bg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
     $<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">resize</span><span style="color: #009900;">&#40;</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>
          resize_bg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
     <span style="color: #003366; font-weight: bold;">function</span> resize_bg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#triquibackimg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;left&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> doc_width <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> doc_height <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>window<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> image_width <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#triquibackimg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> image_height <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#triquibackimg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> image_ratio <span style="color: #339933;">=</span> image_width<span style="color: #339933;">/</span>image_height<span style="color: #339933;">;</span>      
          <span style="color: #003366; font-weight: bold;">var</span> new_width <span style="color: #339933;">=</span> doc_width<span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> new_height <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>new_width<span style="color: #339933;">/</span>image_ratio<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>new_height<span style="color: #339933;">&lt;</span>doc_height<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
               new_height <span style="color: #339933;">=</span> doc_height<span style="color: #339933;">;</span>
               new_width <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>new_height<span style="color: #339933;">*</span>image_ratio<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #003366; font-weight: bold;">var</span> width_offset <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>new_width<span style="color: #339933;">-</span>doc_width<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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#triquibackimg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;left&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;-&quot;</span><span style="color: #339933;">+</span>width_offset<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#triquibackimg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span>new_width<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#triquibackimg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span>new_height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, <code>resize_bg</code> function is called both when the background image is loaded and when the window is resized.</p>
<p>You can see the final result <a href="http://www.emanueleferonato.com/stuff/triquibg/" target = "_blank">here</a> and copy the entire source code by looking at the html.</p>
<p>Next steps will be the creation of a standalone jQuery plugin and a WordPress plugin too. Stay tuned.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/11/16/full-screen-centered-background-image-with-css-and-jquery-2/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Full screen centered background image with CSS and jQuery</title>
		<link>http://www.emanueleferonato.com/2010/11/13/full-screen-centered-background-image-with-css-and-jquery/</link>
		<comments>http://www.emanueleferonato.com/2010/11/13/full-screen-centered-background-image-with-css-and-jquery/#comments</comments>
		<pubDate>Sat, 13 Nov 2010 14:18:43 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Css]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=3502</guid>
		<description><![CDATA[Recently a lot of sites feature a full screen background image. Full screen background images can be used to give some sort of liquid effect to fixed layouts, or just to show full screen images in some special occurrences, such as Christmas or the launch of a new service. This is what you&#8217;ll get: demo. [...]]]></description>
			<content:encoded><![CDATA[<p>Recently a lot of sites feature a full screen background image. Full screen background images can be used to give some sort of liquid effect to fixed layouts, or just to show full screen images in some special occurrences, such as Christmas or the launch of a new service.</p>
<p>This is what you&#8217;ll get: <a target = "_blank" href="http://www.emanueleferonato.com/stuff/triquibg/">demo</a>.</p>
<p>Before you continue reading this post, I have to warn you the title is misleading: you won&#8217;t center any background image. You are going to achieve this effect by using an image in div with a a low z-index.</p>
<p>During this example we&#8217;ll use a <a target = "_blank" href="http://www.flickr.com/photos/iawob/5106469437/">this photo</a> by <a target = "_blank" href="http://www.flickr.com/photos/iawob/">horlo</a>.</p>
<p><strong>HTML</strong></p>
<p>As said, it&#8217;s an image inside a div, so let&#8217;s create them:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&nbsp;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;&lt;/head&gt;
&lt;body&gt;
     &lt;div id = &quot;triquiback&quot;&gt;
          &lt;img id = &quot;triquibackimg&quot; src = &quot;bg.jpg&quot; /&gt;
     &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>The div has an id called <code>triquiback</code> while the image has <code>triquibackimg</code>. It should be the first thing you add after <code>body</code> opening.<span id="more-3502"></span></p>
<p><strong>CSS</strong></p>
<p>z-index and positions will be defined with CSS this way:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&nbsp;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
     &lt;style type=&quot;text/css&quot;&gt;
          #triquiback{
               position:fixed;
               left: 0;
               top: 0;
               overflow:hidden;
               zIndex: -9999
          }
          #triquibackimg{
               position:fixed
          }
     &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
     &lt;div id = &quot;triquiback&quot;&gt;
          &lt;img id = &quot;triquibackimg&quot; src = &quot;bg.jpg&quot; /&gt;
     &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>Notice the z-index of #triquiback and its overflow: being an <code>&lt;img /%gt;</code> image and not a background, I had to set the overflow to hidden to prevent unwanted scrollbars. Also, the left and top positions are set to zero to override body margins and paddings.</p>
<p>Both the div and the image have <code>fixed</code> position to allow to easily place them with <code>left</code> and <code>top</code>.</p>
<p><strong>jQuery</strong></p>
<p>jQuery will allow us to resize the image as we want.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&nbsp;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
     &lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
     &lt;title&gt;AUTO RESIZE BACKGROUND IMAGE&lt;/title&gt;
     <span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
     $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</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>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#triquibackimg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</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>
               <span style="color: #003366; font-weight: bold;">var</span> doc_width <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #003366; font-weight: bold;">var</span> doc_height <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Step 1: getting document size<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>Width: &quot;</span><span style="color: #339933;">+</span>doc_width<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px<span style="color: #000099; font-weight: bold;">\n</span>Height = &quot;</span><span style="color: #339933;">+</span>doc_height<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #003366; font-weight: bold;">var</span> image_width <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #003366; font-weight: bold;">var</span> image_height <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Step 2: getting image size<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>Width: &quot;</span><span style="color: #339933;">+</span>image_width<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px<span style="color: #000099; font-weight: bold;">\n</span>Height = &quot;</span><span style="color: #339933;">+</span>image_height<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #003366; font-weight: bold;">var</span> image_ratio <span style="color: #339933;">=</span> image_width<span style="color: #339933;">/</span>image_height<span style="color: #339933;">;</span>
               <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Step 3: getting image width/height ratio: &quot;</span><span style="color: #339933;">+</span>image_ratio<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>       
               <span style="color: #003366; font-weight: bold;">var</span> new_width <span style="color: #339933;">=</span> doc_width<span style="color: #339933;">;</span>
               <span style="color: #003366; font-weight: bold;">var</span> new_height <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>new_width<span style="color: #339933;">/</span>image_ratio<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Step 4: adapting the image to document width, mantaining the ratio<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>Width: &quot;</span><span style="color: #339933;">+</span>new_width<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px<span style="color: #000099; font-weight: bold;">\n</span>Height = &quot;</span><span style="color: #339933;">+</span>new_height<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span>new_width<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span>new_height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>new_height<span style="color: #339933;">&lt;</span>doc_height<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    new_height <span style="color: #339933;">=</span> doc_height<span style="color: #339933;">;</span>
                    new_width <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>new_height<span style="color: #339933;">*</span>image_ratio<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Step 5: the image isn't high enough<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>Adapting the image to document height, mantaining the ratio<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>Width: &quot;</span><span style="color: #339933;">+</span>new_width<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px<span style="color: #000099; font-weight: bold;">\n</span>Height = &quot;</span><span style="color: #339933;">+</span>new_height<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #009900;">&#40;</span>new_width<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">height</span><span style="color: #009900;">&#40;</span>new_height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #003366; font-weight: bold;">var</span> width_offset <span style="color: #339933;">=</span> Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>new_width<span style="color: #339933;">-</span>doc_width<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: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Step 6: moving the image left by &quot;</span><span style="color: #339933;">+</span>width_offset<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px to have it centered&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;left&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;-&quot;</span><span style="color: #339933;">+</span>width_offset<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;px&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
               <span style="color: #009900;">&#125;</span>
               <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Yeah!! Done :)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
     &lt;style type=&quot;text/css&quot;&gt;
          #triquiback{
               left: 0;
               top: 0;
               position:fixed;
               overflow:hidden;
               zIndex: -9999
          }
          #triquibackimg{
               position:fixed
          }
     &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
     &lt;div id = &quot;triquiback&quot;&gt;
          &lt;img id = &quot;triquibackimg&quot; src = &quot;bg.jpg&quot; /&gt;
     &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>At <strong>line 6</strong> I import jQuery library using Google as CDN.</p>
<p>The code is filled with alerts which will guide you through the <a target = "_blank" href="http://www.emanueleferonato.com/stuff/triquibg/">demo</a>, obviously you can remove them from your project.</p>
<p><strong>Line 9</strong>: Function to be executed once the document is ready</p>
<p><strong>Line 10</strong>: Function to be executed once the &#8220;background&#8221; image is loaded</p>
<p><strong>Lines 11-12</strong>: Getting document area with and height</p>
<p><strong>Lines 14-15</strong>: Getting &#8220;background&#8221; image width and height</p>
<p><strong>Line 17</strong>: Determining image ratio as width/height</p>
<p><strong>Line 19</strong>: Setting image width equal to document width</p>
<p><strong>Line 20</strong>: Adjusting image height according to new image width and image ratio</p>
<p><strong>Lines 22-23</strong>: Resizing the image</p>
<p><strong>Line 24</strong>: Checking if the new image height is shorter than document height. In this case we need to resize the image again</p>
<p><strong>Line 25</strong>: Setting image height equal to document height</p>
<p><strong>Line 26</strong>: Adjusting image width according to new image height and image ratio</p>
<p><strong>Lines 28-29</strong>: Resizing the image. At this time, we need to horizontally center the image</p>
<p><strong>Line 30</strong>: Calculating half of the difference between new image width and document width</p>
<p><strong>Line 32</strong>: Adding a style to image to move it by such difference to the right</p>
<p>And that&#8217;s it. You can check the demo <a target = "_blank" href="http://www.emanueleferonato.com/stuff/triquibg/">here</a>. In case of positive feedback, I&#8217;ll show you how to resize image on the fly when the browser is resized, and add an overlay to image.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2010/11/13/full-screen-centered-background-image-with-css-and-jquery/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>JQuery powered lights off effect</title>
		<link>http://www.emanueleferonato.com/2009/10/12/jquery-powered-lights-off-effect/</link>
		<comments>http://www.emanueleferonato.com/2009/10/12/jquery-powered-lights-off-effect/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 14:58:07 +0000</pubDate>
		<dc:creator>Emanuele Feronato</dc:creator>
				<category><![CDATA[Css]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.emanueleferonato.com/?p=1735</guid>
		<description><![CDATA[A couple of days ago a fan of my Facebook page asked me for a script to make the effect you see on this page. Obviously simply reverse-engineering the script wouldn&#8217;t be enough for me, so I decided to create a lights off effect that make any content in a given div remain highlighted while [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago a fan of <a target = "_blank" href="http://www.facebook.com/pages/Emanuele-Feronato/50374255154">my Facebook page</a> asked me for a script to make the effect you see on <a href="http://test.jackread.co.uk/LightsOff/lightsoff.htm" target = "_blank">this page</a>.</p>
<p>Obviously simply reverse-engineering the script wouldn&#8217;t be enough for me, so I decided to create a lights off effect that make <strong>any</strong> content in a given <code>div</code> remain highlighted while the rest of the page fades to black (or to any color).</p>
<p>I used <a target = "_blank" href="http://jquery.com/">JQuery</a> to manage the fade effect because it&#8217;s the best Javascript library available at the moment.</p>
<p>As a developer, I also tried MooTools and scriptaculous but believe me <a target = "_blank" href="http://jquery.com/">JQuery</a> is some steps ahead.</p>
<p>Now let&#8217;s see this script:<span id="more-1735"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;
&lt;html&gt;
     &lt;head&gt;
          &lt;title&gt;Emanuele Feronato's lights off effect&lt;/title&gt;
          &lt;script type=&quot;text/javascript&quot; src=&quot;jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
          &lt;script&gt;
               $(document).ready(function(){
                    $(&quot;#the_lights&quot;).fadeTo(1,0);
                    $(&quot;#turnoff&quot;).click(function () {
                         $(&quot;#the_lights&quot;).css({'display' : 'block'});
                         $(&quot;#the_lights&quot;).fadeTo(&quot;slow&quot;,1);
                    });
                    $(&quot;#soft&quot;).click(function () {
                         document.getElementById(&quot;the_lights&quot;).style.display=&quot;block&quot;;
                         $(&quot;#the_lights&quot;).fadeTo(&quot;slow&quot;,0.8);
                    });
                    $(&quot;#turnon&quot;).click(function () {
                         document.getElementById(&quot;the_lights&quot;).style.display=&quot;block&quot;;
                         $(&quot;#the_lights&quot;).fadeTo(&quot;slow&quot;,0);
                    });
               });
          &lt;/script&gt;
          &lt;style&gt;
               html{
                    width:100%;
                    height:100%;
                    margin:0px;
               }
               #the_lights{
	              background-color:#000;
	              height:100%;
	              width:100%;
	              position:absolute;
	              top:0;
	              left:0;
	              display:none;
               }
               #standout{
                    padding:5px;
                    background-color:white;
                    position:relative;
                    z-index:1000;
               }
          &lt;/style&gt;
     &lt;/head&gt;
&lt;body&gt;
     &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas scelerisque libero euismod nulla porttitor egestas. Nulla fermentum facilisis sagittis. Vestibulum commodo pretium diam, vitae scelerisque lectus bibendum nec. Curabitur a metus est. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed nunc dolor, pretium eget venenatis ut, condimentum at sem. Vivamus rhoncus ullamcorper turpis. Aliquam sit amet risus diam, vel mattis est. Quisque sodales eros id nulla rhoncus posuere. Nullam accumsan lorem quis dolor tempor congue. Vivamus ligula augue, commodo malesuada tempor non, tristique id enim. Sed dapibus sagittis porttitor. Suspendisse faucibus scelerisque eros, ac hendrerit orci scelerisque sit amet.&lt;/p&gt;
     &lt;div id = &quot;standout&quot;&gt;&lt;p&gt;Everything inside this div will remain highlighted&lt;/p&gt;&lt;div id = &quot;turnoff&quot;&gt;Lights off&lt;/div&gt;&lt;div id = &quot;soft&quot;&gt;Soft lights&lt;/div&gt;&lt;div id = &quot;turnon&quot;&gt;Lights on&lt;/div&gt;&lt;/div&gt;
     &lt;p&gt;Vestibulum interdum, odio ut congue imperdiet, justo ligula bibendum nisl, ac placerat orci eros nec tortor. Maecenas rhoncus felis vitae elit consectetur vitae mattis metus tincidunt. Donec nec ipsum quis tellus posuere ultricies. Ut tempus tortor nec sem blandit ut facilisis nisl scelerisque. Nulla venenatis ante sit amet nisi dictum varius. Etiam congue dui eu diam fermentum et tincidunt magna egestas. Sed tincidunt magna et metus molestie malesuada. Nam tempus dignissim porta. Vivamus iaculis, orci hendrerit condimentum viverra, eros nisl euismod arcu, nec pulvinar tellus elit eu nulla. Vestibulum mattis, ligula sit amet euismod commodo, augue lorem fermentum magna, et rhoncus nisl urna nec purus. Suspendisse potenti. Fusce fermentum orci at orci posuere ornare a imperdiet justo. In hac habitasse platea dictumst. Duis adipiscing leo nec risus varius auctor. Cras sed erat massa, quis egestas leo. Vivamus dignissim lacinia leo, ac interdum nisi facilisis eget.&lt;/p&gt;
     &lt;div id=&quot;the_lights&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>The <code>div</code> is the one called <code>standout</code> at <strong>line 48</strong> and it has nothing special but a background color (it can&#8217;t be transparent) and a z-index along with a relative position (<strong>lines 40-42</strong>).</p>
<p>Obviously the z-index should be higher than the rest of the elements.</p>
<p>The other interesting <code>div</code> is <code>the_lights</code> (<strong>line 50</strong>), a black div covering all the page, initially invisible (<strong>lines 30-36</strong>). Playing with its transparency will simulate the lights effect</p>
<p>Now it&#8217;s all a matter of JQuery:</p>
<p><strong>Line 7</strong>: Listening for the document to be ready</p>
<p><strong>Line 8</strong>: Once it&#8217;s ready, the first thing is setting <code>the_lights</code> alpha to zero. I don&#8217;t set it from CSS but use <code>fadeTo</code> because I need to initialize the fading process. The entire lights out effect is just playing with <code>the_lights</code> and some tweening.</p>
<p><strong>Line 9</strong>: Here I am listening for the <code>turnoff</code> div to be clicked</p>
<p><strong>Line 10</strong>: Showing <code>the_lights</code> div&#8230; this is the same thing as<br />
<code>document.getElementById('the_lights').style.display='block'</code></p>
<p><strong>Line 11</strong>: Fading the black div to full opacity.</p>
<p>This will give the &#8220;lights out effect&#8221;</p>
<p>Try the <a href="/stuff/lightsoff/" target = "_blank">example at this page</a> will show you the effect&#8230; click on &#8220;lights on&#8221;, &#8220;lights off&#8221; or &#8220;soft lights&#8221; and see what happens.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.emanueleferonato.com/2009/10/12/jquery-powered-lights-off-effect/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced
Database Caching 10/23 queries in 0.037 seconds using disk: basic

Served from: www.emanueleferonato.com @ 2012-02-08 19:47:06 -->
