<?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>indiegamr &#187; js</title>
	<atom:link href="http://indiegamr.com/category/js/feed/" rel="self" type="application/rss+xml" />
	<link>http://indiegamr.com</link>
	<description>apps, indie &#38; beyond</description>
	<lastBuildDate>Sun, 30 Mar 2014 14:51:19 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.38</generator>
	<item>
		<title>SoundJS 0.5.2 Audio Plugin for CocoonJS 1.4.7</title>
		<link>http://indiegamr.com/soundjs-0-5-2-audio-plugin-for-cocoonjs-1-4-7/</link>
		<comments>http://indiegamr.com/soundjs-0-5-2-audio-plugin-for-cocoonjs-1-4-7/#comments</comments>
		<pubDate>Sun, 30 Mar 2014 14:45:36 +0000</pubDate>
		<dc:creator><![CDATA[olsn]]></dc:creator>
				<category><![CDATA[audio]]></category>
		<category><![CDATA[CocoonJS]]></category>
		<category><![CDATA[EaselJS]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mobile]]></category>

		<guid isPermaLink="false">http://indiegamr.com/?p=739</guid>
		<description><![CDATA[I just updated the CocoonJSAudioPlugin.js to work with SoundJS 0.5.2 and CocoonJS 1.4.7 Download: CocoonJSAudioPlugin How to use: Just put the CocoonPlugin as the first entry to register and you can use everything else of the SoundJS-library just as is in &#8230; <a href="http://indiegamr.com/soundjs-0-5-2-audio-plugin-for-cocoonjs-1-4-7/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I just updated the CocoonJSAudioPlugin.js to work with SoundJS 0.5.2 and CocoonJS 1.4.7</p>
<p><strong>Download: <a href='http://indiegamr.com/wp-content/uploads/CocoonJSAudioPlugin_0.5.2.js'>CocoonJSAudioPlugin</a></strong></p>
<p><strong>How to use:</strong> Just put the CocoonPlugin as the first entry to register and you can use everything else of the SoundJS-library just as is in CocoonJS now.</p><pre class="crayon-plain-tag">createjs.Sound.registerPlugins([createjs.CocoonJSAudioPlugin]);</pre><p></p>
<hr />
<strong>Playing the same sound multiple times parallel/overlapping:</strong> You will have to set the maximum number of parallel sounds in the register-method:<br />
<pre class="crayon-plain-tag">//this will allow me to play 3 parallel instances of jump.ogg
  createjs.Sound.registerSound('jump.ogg','jump', 3);</pre></p>
]]></content:encoded>
			<wfw:commentRss>http://indiegamr.com/soundjs-0-5-2-audio-plugin-for-cocoonjs-1-4-7/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Fading Waves Effect with EaselJS</title>
		<link>http://indiegamr.com/fading-line-effect-with-easeljs/</link>
		<comments>http://indiegamr.com/fading-line-effect-with-easeljs/#comments</comments>
		<pubDate>Wed, 15 May 2013 14:24:31 +0000</pubDate>
		<dc:creator><![CDATA[olsn]]></dc:creator>
				<category><![CDATA[EaselJS]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://indiegamr.com/?p=676</guid>
		<description><![CDATA[Today I made a basic JavaScript-version of my flash waves-effect, that I made a couple years back. direct link: http://labs.indiegamr.com/olines/index.html For bluring I used this filter: Link &#8211; but still: For those kind of operations HTML/JS is unfortunately still way &#8230; <a href="http://indiegamr.com/fading-line-effect-with-easeljs/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Today I made a basic JavaScript-version of my <a href="http://activeden.net/item/owaves-wave-effect/full_screen_preview/124004">flash waves-effect</a>, that I made a couple years back.</p>
<p><iframe width="100%" height="250" scrolling="no" src="http://labs.indiegamr.com/olines/index.html"></iframe><br />
direct link: <a href="http://labs.indiegamr.com/olines/index.html">http://labs.indiegamr.com/olines/index.html</a></p>
<p>For bluring I used this filter: <a href="http://www.quasimondo.com/BoxBlurForCanvas/FastBlurDemo.html">Link</a> &#8211; but still: For those kind of operations HTML/JS is unfortunately still way behind flash in terms of performance.</p>
]]></content:encoded>
			<wfw:commentRss>http://indiegamr.com/fading-line-effect-with-easeljs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The state of Audio in HTML5 Games</title>
		<link>http://indiegamr.com/the-state-of-audio-in-html5-games/</link>
		<comments>http://indiegamr.com/the-state-of-audio-in-html5-games/#comments</comments>
		<pubDate>Fri, 26 Apr 2013 10:46:00 +0000</pubDate>
		<dc:creator><![CDATA[olsn]]></dc:creator>
				<category><![CDATA[audio]]></category>
		<category><![CDATA[gaming]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mobile]]></category>

		<guid isPermaLink="false">http://indiegamr.com/?p=649</guid>
		<description><![CDATA[HTML5 Gaming for desktop and mobile is a very trendy topic these days. The performance of canvas applications is getting better and better each day and technologies like CocoonJS or Ejecta even enable a close to native performance for canvas &#8230; <a href="http://indiegamr.com/the-state-of-audio-in-html5-games/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p style="text-align: left;">HTML5 Gaming for desktop and mobile is a very trendy topic these days. The performance of canvas applications is getting better and better each day and technologies like <a href="http://ludei.com/tech/cocoonjs">CocoonJS</a> or <a href="http://impactjs.com/ejecta">Ejecta</a> even enable a close to native performance for canvas applications on mobile devices. &#8211; But what about audio? Is it really that easy to implement sound with your html app?<br />
This article features some interesting points that I collected while researching for my eBook ‘<a href="http://indiegamr.com/zerotoappstore/">From Zero to the Appstore’</a>.</p>
<h2>Audio != Audio</h2>
<p style="text-align: left;">While most browsers and devices already do support audio in general, that doesn’t mean you can just play any audio file in any modern browser. The following table briefly lists the support for Audio-codecs.</p>
<p><span id="more-649"></span></p>
<table style="font-size: 70%;" width="70%" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td valign="top"><strong>Browser</strong></td>
<td valign="top"><strong>Codec Support</strong></td>
</tr>
<tr>
<td valign="top"><strong>Chrome</strong></td>
<td valign="top">Ogg, MP3, WAV</td>
</tr>
<tr>
<td valign="top"><strong>Firefox</strong></td>
<td valign="top">Ogg, WAV</td>
</tr>
<tr>
<td valign="top"><strong>Internet Explorer (9+)</strong></td>
<td valign="top">MP3, AAC</td>
</tr>
<tr>
<td valign="top"><strong>Opera</strong></td>
<td valign="top">Ogg, WAV</td>
</tr>
<tr>
<td valign="top"><strong>Safari</strong></td>
<td valign="top">MP3, AAC, WAV</td>
</tr>
<tr>
<td valign="top"><strong> </strong></td>
<td valign="top"></td>
</tr>
<tr>
<td valign="top"><strong>Android</strong></td>
<td valign="top">Depending on the Device</td>
</tr>
<tr>
<td valign="top"><strong>Blackberry</strong></td>
<td valign="top">MP3, AAC</td>
</tr>
<tr>
<td valign="top"><strong>Mobile Safari (iPhone, iPad, iPod Touch)</strong></td>
<td valign="top">MP3, AAC</td>
</tr>
<tr>
<td valign="top"><strong>Opera Mobile</strong></td>
<td valign="top">Depending on the Device</td>
</tr>
</tbody>
</table>
<p style="text-align: left;">As you can see there is no single codec and format that is supported by all browsers and devices. In addition to this not every device does support the <strong><a href="http://caniuse.com/#feat=audio-api">WebAudio-API</a></strong> and some devices only allow audio-playback upon a user-interaction or only allow playing one sound at a time. So what should you do? Setup an audio file for any codec and then check the browser’s support before loading and playing the file? While this would be currently the only way to do it right, this not only sounds like a lot of work, but also is. What about browsers that don’t support the audio-element at all? – Implementing a flash fallback is no easy task as well.</p>
<hr />
<p><span style="color: #000000; font-weight: bold;">SoundJS</span></p>
<p style="text-align: left;">One possible solution is <strong><a href="http://www.createjs.com/#!/SoundJS">SoundJS</a></strong> – SoundJS is a JavaScript framework that will take care of all the compatibility issues with audio on different devices and even adds the ability of a flash-fallback should the browser support no audio. You would still have to provide different files for each sound to ensure that a soundfile is available for each device depending on the codec support but besides this, playing a sound with SoundJS can be done with 2 lines of JavaScript:</p>
<p></p><pre class="crayon-plain-tag">var src = ‘mySound.m4a|mySound.ogg’;
createjs.Sound.registerSound(src);
//…after the sound was loaded, it can be played by:
createjs.Sound.play(src);</pre><p></p>
<p style="text-align: left;">What I also like about SoundJS is that it features a plugin-system, so you can write your custom playback-plugin for individual needs, for example I used it to write a CocoonJS-plugin for it.</p>
<hr />
<p><span style="color: #000000; font-weight: bold;">howler.js</span></p>
<p style="text-align: left;">Another solution is <strong><a href="https://github.com/goldfire/howler.js/">howler.js</a></strong>, it is also released under the MIT license, so you can use it free in any of you projects it works very similar to SoundJS. One advantage over SoundJS is that it is very lightweight with only 9kb and offers pretty much the same functionality as SoundJS – with one exception though: howler.js features no flash-fallback, but if you are targeting mobile platforms, this should be no problem.</p>
<p></p><pre class="crayon-plain-tag">var sound = new Howl({
  urls: ['mySound.m4a','mySound.ogg']
}).play();</pre><p>Another thing I like about howler is the support of sound-sprites:</p><pre class="crayon-plain-tag">var sound = new Howl({
  urls: ['sounds.mp3', 'sounds.ogg'],
  sprite: {
    blast: [0, 1000],
    laser: [2000, 3000],
    winner: [4000, 7500]
  }
});

// shoot the laser!
sound.play('laser');</pre><p>&nbsp;</p>
<hr />
<p style="text-align: left;"><span style="color: #000000; font-weight: bold;">SoundManager 2</span><br />
A third option is <strong><a href="http://www.schillmania.com/projects/soundmanager2/">SoundManager 2</a></strong>, (<strong><a href="https://github.com/scottschiller/soundmanager2/">github</a></strong>), just like SoundJS it features a flash fallback if HTML5 audio is not supported. SoundManager2 is used by popular site like SoundCloud, last.fm or the Nyan Cat site. The focus of SoundManager2 is more on implementing audio-players on web-sites as it also features UIs for audio playback. Sounds are played like this:</p>
<p></p><pre class="crayon-plain-tag">// create "mySound"...
soundManager.createSound({
  id: 'mySound',
  url: '/path/to/an.mp3'
});

// ...and play it
soundManager.play('mySound');</pre><p>&nbsp;</p>
<hr />
<p><span style="color: #000000; font-weight: bold;">Comparison</span></p>
<p style="text-align: left;">As you can see, all three frameworks have their pros and cons, you should check out their sites and see what features you need for your project. The following table is just a very brief overview on the differences:</p>
<table style="font-size: 70%;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="160"><strong>Feature/Player</strong></td>
<td width="160">
<p align="center"><strong>SoundJS</strong></p>
</td>
<td width="160">
<p align="center"><strong>howler.js</strong></p>
</td>
<td width="160">
<p align="center"><strong>SoundManager 2</strong></p>
</td>
</tr>
<tr>
<td width="160"><strong>Automatic playback of the correct codec/format</strong></td>
<td width="160">
<p align="center">Yes</p>
</td>
<td width="160">
<p align="center">Yes</p>
</td>
<td width="160">
<p align="center">Yes</p>
</td>
</tr>
<tr>
<td width="160"><strong>Flash fallback</strong></td>
<td width="160">
<p align="center">Yes</p>
</td>
<td width="160">
<p align="center">no</p>
</td>
<td width="160">
<p align="center">Yes</p>
</td>
</tr>
<tr>
<td width="160"><strong>Size (no gzip)</strong></td>
<td width="160">
<p align="center">25kb</p>
</td>
<td width="160">
<p align="center">9kb</p>
</td>
<td width="160">
<p align="center">34kb</p>
</td>
</tr>
<tr>
<td width="160"><strong>Sound Sprites</strong></td>
<td width="160">
<p align="center">No (can play with offset)</p>
</td>
<td width="160">
<p align="center">Yes</p>
</td>
<td width="160">
<p align="center">No (can play and stop at offset)</p>
</td>
</tr>
<tr>
<td width="160"><strong>License</strong></td>
<td width="160">
<p align="center">MIT</p>
</td>
<td width="160">
<p align="center">MIT</p>
</td>
<td width="160">
<p align="center">BSD</p>
</td>
</tr>
<tr>
<td width="160"><strong>Best used for*</strong></td>
<td width="160">
<p align="center">All-rounder, probably best used in combination with the CreateJS suite</p>
</td>
<td width="160">
<p align="center">games/mobile apps</p>
</td>
<td width="160">
<p align="center">Websites, mobile sites as an audio player</p>
</td>
</tr>
</tbody>
</table>
<p>*this doesn’t mean that you cannot use it for other cases, it’s just my personal evaluation</p>
<hr />
<p><span style="color: #000000; font-weight: bold;">A last word: Beware of MP3!</span></p>
<p style="text-align: left;">What a lot of people probably don’t know: <span style="text-decoration: underline;"><strong>MP3 is not free!</strong></span><br />
Implementing mp3 into your app/game is no problem, but what you should know is that if your game is ‘distributed’ (technically that includes 1 user playing the game in his browser) more than 5000 times, you have to pay an <a href="http://www.mp3licensing.com/royalty/games.html"><strong>mp3 codec royalty of $2500</strong></a>. But not only because of the licensing I would suggest you to use Ogg Vorbis(.ogg) or AAC(.m4a, MP4), OGG and AAC do have a better sound-quality while encoded with a smaller file-size than their MP3-counterpart.</p>
<p style="text-align: left;">If you want to learn more about the workflow of implementing audio into your html5- (mobile-) app/game and additional information on creating and publishing an HTML5 game, you should checkout my book at: <a href="http://indiegamr.com/zerotoappstore/">http://indiegamr.com/zerotoappstore/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://indiegamr.com/the-state-of-audio-in-html5-games/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Quickfix to enable touch of EaselJS-DisplayObjects in CocoonJS</title>
		<link>http://indiegamr.com/quickfix-to-enable-touch-of-easeljs-displayobjects-in-cocoonjs/</link>
		<comments>http://indiegamr.com/quickfix-to-enable-touch-of-easeljs-displayobjects-in-cocoonjs/#comments</comments>
		<pubDate>Sat, 16 Mar 2013 14:39:27 +0000</pubDate>
		<dc:creator><![CDATA[olsn]]></dc:creator>
				<category><![CDATA[CocoonJS]]></category>
		<category><![CDATA[EaselJS]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://indiegamr.com/?p=594</guid>
		<description><![CDATA[So while on the Stage touch is working for EaselJS in CocoonJS, it is currently not possible to touch individual EaselJS-DisplayObjects in CocoonJS. I wrote a quick fix for that, in case anyone else needs it. Download: easeljs_cocoonjs_touchfix.js (updated version to &#8230; <a href="http://indiegamr.com/quickfix-to-enable-touch-of-easeljs-displayobjects-in-cocoonjs/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>So while on the Stage touch is working for EaselJS in CocoonJS, it is currently not possible to touch individual EaselJS-DisplayObjects in CocoonJS. I wrote a quick fix for that, in case anyone else needs it.</p>
<h2>Download: <a href="http://indiegamr.com/wp-content/uploads/easeljs_cocoonjs_touchfix.js">easeljs_cocoonjs_touchfix.js</a></h2>
<p> (updated version to work with EaselJS 0.7+, thx to Jonny)<br />
<strong>How to use:</strong> Download JS-fix and implement it through a script-tag somewhere after the EaselJS-script-tag and your done!</p>
<p>Hope this helps someone, comment for any issues,cheers!</p>
<p><small>If you use an older version of EaselJS: <a href="http://indiegamr.com/wp-content/uploads/easeljs_cocoonjs_touchfix_pre_0.7.js">easeljs_cocoonjs_touchfix_pre_0.7.js</a> (for pre-EaselJS 0.7)</small><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://indiegamr.com/quickfix-to-enable-touch-of-easeljs-displayobjects-in-cocoonjs/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>EaselJS Pixel Perfect Collision Detection for Bitmaps with Alpha Threshold</title>
		<link>http://indiegamr.com/easeljs-pixel-perfect-collision-detection-for-bitmaps-with-alpha-threshold/</link>
		<comments>http://indiegamr.com/easeljs-pixel-perfect-collision-detection-for-bitmaps-with-alpha-threshold/#comments</comments>
		<pubDate>Thu, 20 Dec 2012 05:24:10 +0000</pubDate>
		<dc:creator><![CDATA[olsn]]></dc:creator>
				<category><![CDATA[EaselJS]]></category>
		<category><![CDATA[gaming]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://indiegamr.com/?p=558</guid>
		<description><![CDATA[I just finished the first version of a pixel perfect collision detection for EaselJS Bitmaps and BitmapAnimations. Get it from Github: https://github.com/olsn/Collision-Detection-for-EaselJS Here is an example (play around with the alpha threshold and toggle the detection mode): [crayon-69d297d053d14992914142/]]]></description>
				<content:encoded><![CDATA[<p>I just finished the first version of a pixel perfect collision detection for EaselJS Bitmaps and BitmapAnimations.<br />
<strong>Get it from Github: <a href="https://github.com/olsn/Collision-Detection-for-EaselJS" target="_blank">https://github.com/olsn/Collision-Detection-for-EaselJS</a></strong></p>
<p>Here is an example (play around with the alpha threshold and toggle the detection mode):<br />
<iframe width="320" height="240" style="width: 100%; height: 300px;" src="http://labs.indiegamr.com/easeljs_collision/"></iframe></p><pre class="crayon-plain-tag">// Rect Collision
var intersection = ndgmr.checkRectCollision(bitmap1,bitmap2);
// intersection is null if no collision, otherwise a {x,y,width,height}-Object is returned

// Pixel Perfect Collision
var collision = ndgmr.checkPixelCollision(bitmap1,bitmap2,alphaThreshold);
// true or false
// alphaThreshold default is 0, set to higher value to ignore collisions with semi transparent pixels</pre><p></p>
]]></content:encoded>
			<wfw:commentRss>http://indiegamr.com/easeljs-pixel-perfect-collision-detection-for-bitmaps-with-alpha-threshold/feed/</wfw:commentRss>
		<slash:comments>59</slash:comments>
		</item>
		<item>
		<title>iOS6 html hardware acceleration changes and how to fix them</title>
		<link>http://indiegamr.com/ios6-html-hardware-acceleration-changes-and-how-to-fix-them/</link>
		<comments>http://indiegamr.com/ios6-html-hardware-acceleration-changes-and-how-to-fix-them/#comments</comments>
		<pubDate>Sun, 30 Sep 2012 13:42:05 +0000</pubDate>
		<dc:creator><![CDATA[olsn]]></dc:creator>
				<category><![CDATA[css]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mobile]]></category>

		<guid isPermaLink="false">http://indiegamr.com/?p=485</guid>
		<description><![CDATA[As probably many developers have already experience this over the past weeks: I had a phonegap application for iOS that contained some visually animated components  (a coverflow or a carousel for example). But after updating to iOS6 this is all &#8230; <a href="http://indiegamr.com/ios6-html-hardware-acceleration-changes-and-how-to-fix-them/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>As probably many developers have already experience this over the past weeks:<br />
I had a phonegap application for iOS that contained some visually animated components  (a coverflow or a carousel for example). But after updating to iOS6 this is all flickring now and it feels horrible because it&#8217;s just stumbling and faltering on the new version of the OS.<br />
There are several reasons for this and I cannot say which were already there in iOS5, however here are the two main reasons and instructions on how to fix that:<span id="more-485"></span></p>
<h2>1. Not all CSS Properties trigger hardware acceleration any more</h2>
<p>Previously there were quite a few properties that triggered a hardware acceleration, for example:</p>
<ul>
<li>-webik-transform: translate3d(x,y,z);</li>
<li>-webkit-transform: preserve-3d;</li>
<li>&#8230;</li>
</ul>
<p>As Apple states in its <a href="http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/_index.html#//apple_ref/doc/uid/TP40012166-CH1-SW19" target="_blank">Developer Changelog for iOS6</a>: &#8216;<em>WebKit no longer always creates hardware-accelerated layers for elements with the <code>-webkit-transform: preserve-3d</code> option.[&#8230;]</em>&#8216; There is a need for new CSS properties to trigger the acceleration, unfortunately I&#8217;m not aware of any official document by Apple listing those properties.</p>
<p><strong>How to fix it:</strong><br />
Here are three properties that have been proven to work (at least in combination, I have not yet tested them solo, but they should):</p>
<ul>
<li>-webkit-transform: translateZ(0);</li>
<li>-webkit-perspective: 1000;</li>
<li>-webkit-backface-visibility: hidden;<a href="http://indiegamr.com/ios6-html-hardware-acceleration-changes-and-how-to-fix-them/ios6-performance-overlapping/" rel="attachment wp-att-489"><br />
</a></li>
</ul>
<p>As this is going to trigger hardware acceleration for the specified elements this might still not completely fix all the issues, therefore please continue reading Nr.2.</p>
<hr />
<h2>2. Overlapping with other Elements</h2>
<p>Since I cannot downgrade to iOS5 I cannot say for sure if this &#8220;issue&#8221; already persisted, however it heavily persists with iOS6:<br />
<a style="font-style: normal; line-height: 24px; text-decoration: underline;" href="http://indiegamr.com/ios6-html-hardware-acceleration-changes-and-how-to-fix-them/ios6-performance-overlapping/" rel="attachment wp-att-489"><br />
</a>If you have an element(Element A) that is hardware accelerated and animated, it will/might cause performance and visual issues when overlapping with other elements(Element B) that are not hardware accelerated, as Element A will trigger Element B to be re-rendered with every change of Element A(at the end of a CSS-transition or when updating CSS properties through JS), which will cause:</p>
<ul>
<li><strong>flickering of Element B, since it renders a lot slower</strong></li>
<li><strong>heavy slowdown of the animation, since it needs more CPU time to render leaving less time for JS execution</strong></li>
</ul>
<p>One thing I noticed: If Element A OVERlaps another element it is not slowing down the animation as heavy as if Element A was UNDERlapping another element. Also it does not really matter if the parent-container of Element A has &#8216;overflow: hidden;&#8217; or &#8216;overflow: visible;&#8217; &#8211; it still slows doen the execution if the overlapping occures outside the bounds of the parent container.</p>
<p><strong>How to fix it:<br />
</strong> a) This can be simply fixed by giving ALL overlapping elements the CSS properties that trigger hardware accerleration. (<strong>see 1.</strong>)<br />
(My first approach to fix this was by alternating the DOM-order to only have OVERlapping elements, however fix a) is way easier and works way better &#8211; for me.)</p>
<p>I have visualized this here:<a href="http://indiegamr.com/ios6-html-hardware-acceleration-changes-and-how-to-fix-them/ios6-performance-overlapping/" rel="attachment wp-att-489"><img class="aligncenter size-full wp-image-489" title="ios6-performance-overlapping" src="http://indiegamr.com/wp-content/uploads/ios6-performance-overlapping.png" alt="" width="768" height="400" /></a></p>
<p>&nbsp;</p>
<p>edit1: 16-10-2012</p>
<hr />
<h2>3. Avoid changes between &#8216;display: none;&#8217; and &#8216;display: something;&#8217;</h2>
<p>Changing the display-state on iOS is like adding or removing an element to and from the DOM and is therefor a very heady calculation depending on how many items you have in your DOM that can easyly cause the UI to freeze for a second or more.</p>
<p><strong>How to fix it:</strong><br />
Instead of using the display-property, use the tranform-property instead, if you want to hide an item you can use something like this: transform:</p><pre class="crayon-plain-tag">translate(100000px,100000px);</pre><p>and if you want to show the element, just set the values to 0:</p><pre class="crayon-plain-tag">translate(0px,0px);</pre><p>You have to set the &#8216;position: absolute;&#8217; though. This method needs a little bit more work than using &#8216;display&#8217;, however the performance of your webapp will thank you greatly for that.</p>
<p>&nbsp;</p>
<p>I hope this helps someone as I was struggeling with this issue for quite a while, thoughts or additions on this article are very welcome!</p>
]]></content:encoded>
			<wfw:commentRss>http://indiegamr.com/ios6-html-hardware-acceleration-changes-and-how-to-fix-them/feed/</wfw:commentRss>
		<slash:comments>41</slash:comments>
		</item>
		<item>
		<title>EaselJS 0.5.0</title>
		<link>http://indiegamr.com/easeljs-0-5-0/</link>
		<comments>http://indiegamr.com/easeljs-0-5-0/#comments</comments>
		<pubDate>Fri, 31 Aug 2012 18:44:50 +0000</pubDate>
		<dc:creator><![CDATA[olsn]]></dc:creator>
				<category><![CDATA[EaselJS]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://indiegamr.com/?p=457</guid>
		<description><![CDATA[I know it&#8217;s been out for a week now, but I guess I can still mention it: EaselJS 0.5.0 is out now: http://www.createjs.com/#!/EaselJS Check Out the Changelog here: https://github.com/CreateJS/EaselJS/blob/master/VERSIONS.txt]]></description>
				<content:encoded><![CDATA[<p>I know it&#8217;s been out for a week now, but I guess I can still mention it:<br />
EaselJS 0.5.0 is out now:<br />
<a href="http://www.createjs.com/#!/EaselJS" target="_blank">http://www.createjs.com/#!/EaselJS</a><br />
Check Out the Changelog here:<br />
<a href="https://github.com/CreateJS/EaselJS/blob/master/VERSIONS.txt" target="_blank">https://github.com/CreateJS/EaselJS/blob/master/VERSIONS.txt</a></p>
]]></content:encoded>
			<wfw:commentRss>http://indiegamr.com/easeljs-0-5-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generate Repeatable Random Numbers (in JS)</title>
		<link>http://indiegamr.com/generate-repeatable-random-numbers-in-js/</link>
		<comments>http://indiegamr.com/generate-repeatable-random-numbers-in-js/#comments</comments>
		<pubDate>Thu, 26 Jul 2012 11:17:37 +0000</pubDate>
		<dc:creator><![CDATA[olsn]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://indiegamr.com/?p=407</guid>
		<description><![CDATA[Quite often when developing a game I need &#8220;random&#8221; numbers, that I can reproduce or that I can steer. Why? &#8211; For example when setting up a level or when resetting a level you might want to have it look &#8230; <a href="http://indiegamr.com/generate-repeatable-random-numbers-in-js/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Quite often when developing a game I need &#8220;random&#8221; numbers, that I can reproduce or that I can steer. Why? &#8211; For example when setting up a level or when resetting a level you might want to have it look random, but still the same every time you load it. &#8211; So how do we do that? There are tons of ways of achieving a so called &#8220;seeded random generated number&#8221; &#8211; if you want any details on the algorithms you can for example check out this <a href="http://www.ict.griffith.edu.au/anthony/info/C/RandomNumbers" target="_blank">link</a>. But basically they all work the same: You initialize the algorithm with a &#8220;seed&#8221; (a number, that you choose to steer the algorithm). And every seed will return its own unique set of &#8220;random&#8221; numbers, so e.g.: Every time I seed the Number 6, I get the same set of generated &#8220;random&#8221; numbers. Every time I seed the Number 10, I get the same set of &#8220;random&#8221; number, that is different from the set &#8216;6&#8217; and so on&#8230;<br />
Because of this fact: seeded random numbers are predictable, which is not bad, but just don&#8217;t use it for encryption purposes ect&#8230; <img src="http://indiegamr.com/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p>
<h2>The algorithm</h2>
<hr/>
So the algorithm I&#8217;m using is the following, it&#8217;s really very simple:<br />
<span style="font-size: 80%">(this is nothing new and also nothing I invented, but I think this might help quite a few people)</span><br />
<pre class="crayon-plain-tag">// the initial seed
Math.seed = 6;

// in order to work 'Math.seed' must NOT be undefined,
// so in any case, you HAVE to provide a Math.seed
Math.seededRandom = function(max, min) {
    max = max || 1;
    min = min || 0;

    Math.seed = (Math.seed * 9301 + 49297) % 233280;
    var rnd = Math.seed / 233280;

    return min + rnd * (max - min);
}</pre><br />
<span id="more-407"></span></p>
<p>You may ask: <strong>Why &#8216;<span style="font-family: Courier">(seed * 9301 + 49297) % 233280</span>&#8216; ?!</strong><br />
The answer is both simple&#038;complicated: The combination of 9301, 49297 and 233280 provide a very even distributed set of &#8220;random&#8221; numbers. Please don&#8217;t ask WHY &#8211; that&#8217;s the complicated part, some very smart people figured out those numbers quite some time ago, and I also cannot tell you how they did it. But as allways: Google is your friend <img src="http://indiegamr.com/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p>
<h2>Benchmark</h2>
<hr/>
I set up a small benchmark on jsfiddle, to test the performance of that algorithm and also how the distribution of the generated sets are: Just hit then Run-button and see the results, for me Math.random() is usually 3-4x faster than Math.seededRandom(), which is for 100.000 numbers still within the range of 10-15ms and therefor pretty fast. Also the distribution of the numbers is very equal as you can see.<br />
<iframe style="width: 100%; height: 270px; border: solid 1px #888;" src="http://jsfiddle.net/ZCkFN/3/embedded/" allowfullscreen="allowfullscreen" frameborder="0"></iframe></p>
<h2>See it in &#8216;action&#8217;</h2>
<hr/>
I quickly implemented this in my <a href="http://indiegamr.com/retro-style-plattform-runner-game-for-mobile-with-easeljs-part-1/" target="_blank">Runner Game Prototype</a>, so no matter how often you reload the page, the level is allways going to look the same and all I did was replacing the &#8220;Math.random()&#8221; with &#8220;Math.seededRandom()&#8221; and of course I added the algorithmto the utilities-file.<br />
<iframe src="http://demos.indiegamr.com/jumpy/seededRandomLevel/?seed=1.57" width="100%" height="250"></iframe><br />
If you want to try out other seeds, just go to: <a href="http://demos.indiegamr.com/jumpy/seededRandomLevel/?seed=1.57" target="_blank">http://demos.indiegamr.com/jumpy/seededRandomLevel/?seed=1.57</a> and change the &#8220;seed&#8221; in the query string.</p>
]]></content:encoded>
			<wfw:commentRss>http://indiegamr.com/generate-repeatable-random-numbers-in-js/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Retro Style Platform Runner Game for mobile with EaselJS (Part 5) – Animations and polishing</title>
		<link>http://indiegamr.com/retro-style-platform-runner-game-for-mobile-with-easeljs-part-5-animations-and-polishing/</link>
		<comments>http://indiegamr.com/retro-style-platform-runner-game-for-mobile-with-easeljs-part-5-animations-and-polishing/#comments</comments>
		<pubDate>Tue, 24 Jul 2012 06:44:47 +0000</pubDate>
		<dc:creator><![CDATA[olsn]]></dc:creator>
				<category><![CDATA[EaselJS]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mobile]]></category>

		<guid isPermaLink="false">http://indiegamr.com/?p=366</guid>
		<description><![CDATA[In this 5th and for the moment last part of my series on creating an html5-canvas-game I&#8217;m going to write about adding animations and other elements to polish up the game a little bit. The result: Background Parallax So we&#8217;re &#8230; <a href="http://indiegamr.com/retro-style-platform-runner-game-for-mobile-with-easeljs-part-5-animations-and-polishing/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>In this 5th and for the moment last part of my series on creating an html5-canvas-game I&#8217;m going to write about adding animations and other elements to polish up the game a little bit.</p>
<p><strong>The result:</strong><br />
<iframe src="http://demos.indiegamr.com/jumpy/part5/index_d.html" width="100%" height="250"></iframe><br />
<span id="more-366"></span></p>
<h2>Background Parallax</h2>
<hr/>
So we&#8217;re going to start off with the background &#8211; at the moment it&#8217;s looking kinda pale and you cannot really see any movement. A background can contain pretty much anything that doesn&#8217;t distract the player from the game but is also not too boring. In this example I will just create a simple grid. Add the following to the jumpy.js:<br />
<pre class="crayon-plain-tag">self.createBgGrid = function(numX, numY) {
	var grid = new Container();
	grid.snapToPixel = true;
	// calculating the distance between
	// the grid-lines
	var gw = w/numX;
	var gh = h/numY;
	// drawing the vertical line
	var verticalLine = new Graphics();
	verticalLine.beginFill(Graphics.getRGB(101, 60, 176));
	verticalLine.drawRect(0,0,gw * 0.02,gh*(numY+2));
	var vs;
	// placing the vertical lines:
	// we're placing 1 more than requested
	// to have seamless scrolling later
	for ( var c = -1; c &lt; numX+1; c++) {
		vs = new Shape(verticalLine);
		vs.snapToPixel = true;
		vs.x = c * gw;
		vs.y = -gh;
		grid.addChild(vs);
	}
	// drawing a horizontal line
	var horizontalLine = new Graphics();
	horizontalLine.beginFill(Graphics.getRGB(101, 60, 176));
	horizontalLine.drawRect(0,0,gw*(numX+1),gh * 0.02);
	var hs;
	// placing the horizontal lines:
	// we're placing 1 more than requested
	// to have seamless scrolling later
	for ( c = -1; c &lt; numY+1; c++ ) {
		hs = new Shape(horizontalLine);
		hs.snapToPixel = true;
		hs.x = 0;
		hs.y = c * gh;
		grid.addChild(hs);
	}

	// return the grid-object
	return grid;
}</pre></p>
<p>And when the game is initialized, don&#8217;t forget to add the grid to the stage:</p><pre class="crayon-plain-tag">// setting up 'constants' for the number of vertical
// and horizontag background lines - play around with those values
var	GRID_HORIZONTAL = 8,
	GRID_VERTICAL = 4;

// and the following will be added to 'self.initializeGame'
background = self.createBgGrid(GRID_HORIZONTAL,GRID_VERTICAL);
stage.addChild(background);</pre><p></p>
<p>To emphasize movement we will make the background move with about 50% of the world-movement creating a <a href="http://en.wikipedia.org/wiki/Parallax" target="_blank">parallax-effect</a>. We accomplish that by adding the following to the &#8216;self.tick&#8217;-method:</p><pre class="crayon-plain-tag">// the background 'moves' about 45% of the speed of the world-object
// and it's position snaps back to zero once it's reached a limit 
background.x = (world.x * .45) % (w/GRID_HORIZONTAL); // horizontal
background.y = (world.y * .45) % (h/GRID_VERTICAL);   // vertical</pre><p></p>
<p><strong>Note:</strong> This is pretty much the most simple way to make a background move seamlessly as the world moves, however if you have a more complex background you will probably have to use either a greater modulo(%) or handle you background-movement individually.</p>
<p>The changes above should already result in something similar to this:<br />
<iframe src="http://demos.indiegamr.com/jumpy/part5/index_a.html" width="100%" height="250"></iframe><br />
Pretty simple &#8211; right? <img src="http://indiegamr.com/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley" /></p>
<h2>More Background Parallax</h2>
<hr/>
To add more motion to the scene we will create just a couple white lines in the background, that are supposed to symbolize distant waterfalls. To create such a waterfall-line, we add the following method to the jumpy.js:<br />
<pre class="crayon-plain-tag">self.createPixelLine = function(width) {
	// adjusting the width to the games scale
	// but not smaller than 1px and round it
	// to only have full pixels to render or
	// anti-aliasing will slow everything down
	width = Math.max(Math.round(width * scale),1);

	// drawing the line
	vl = new Graphics();
	vl.beginFill(Graphics.getRGB(255,255,255));
	vl.drawRect(0,0,width,h);

	lineShape = new Shape(vl);
	lineShape.snapToPixel = true;
	// the thinner the line, the less alpha
	// to make it look like it's further away
	lineShape.alpha = width * .25;
	// if it's further away, make it move slower
	lineShape.speedFactor = 0.3 + lineShape.alpha * 0.3 + Math.random() * 0.2;
	// the range defines when the line will be
	// moved back to the end of the screen
	lineShape.range = w + Math.random() * w * .3;
	// every line needs an offset, so they
	// don't start off at the same position
	lineShape.offsetX = Math.random() * w;
	
	return lineShape;
}</pre></p>
<p>And when we initialize the game, we will just create 4(or how many we want) of those lines:</p><pre class="crayon-plain-tag">// create and add another background-
// /parallax-layer
background_2 = new Container();
stage.addChild(background_2);
for ( var c = 0; c &lt; 4; c++) {
	// create a line/ distant waterfall
	var line = self.createPixelLine((Math.random()*3+1)|0);
	// add it to the scene
	background_2.addChild(line);
	// and push it to the parallaxObjects
	// don't forget to initialize that array
	parallaxObjects.push(line);
}</pre><p></p>
<p>And now we will make those lines move, just we like we made the grid move (it&#8217;s a little more complex this time, but the basic idea behind it, is the same):</p><pre class="crayon-plain-tag">l = parallaxObjects.length;
for ( c = 0; c &lt; l; c++ ) {
	p = parallaxObjects[c];
	// just change the x-coordinate
	// a change in the y-coordinate would not have any
	// result, since it's just a white line
	p.x = ((world.x * p.speedFactor - p.offsetX) % p.range) + p.range;
}</pre><p></p>
<p>And voila &#8211; now we got a pretty decent looking parallax-background already, of course with this way, it&#8217;s pretty simple to use just any graphic instead of white line, just add some vertcal-movement as well, and it should be good to go. The current result after adding all parallax elements looks like this:<br />
<iframe src="http://demos.indiegamr.com/jumpy/part5/index_b.html" width="100%" height="250"></iframe></p>
<h2>Animations</h2>
<hr/>
Now we will bring some more life into the scene by adding some animations, starting with some waterfalls. The key to animations in EaselJS is pretty much the same like most other engines and platforms: SpriteSheets. A SpriteSheet is a graphic that contains all frames of the animation as stills. This is how the SpriteSheets of the waterfall, we are going to use, looks, it contains 3 frames:
<p><a href="http://indiegamr.com/retro-style-platform-runner-game-for-mobile-with-easeljs-part-5-animations-and-polishing/waterfall2/" rel="attachment wp-att-385"><img src="http://indiegamr.com/wp-content/uploads/waterfall2.png" alt="" title="waterfall2" width="24" height="256" class="aligncenter size-full wp-image-385" /></a></p>
<p>The basic way for creating a SpriteSheet-Animation in EaselJS looks like this:</p><pre class="crayon-plain-tag">// defining the spriteSheetData
// this whole object can also be
// stored in a JSON-file, however
// by resizing my assets with my
// custom algorithm this would make
// things more complicated
var waterFallSpriteSheetData = {
	images: [assets[WATERFALL_IMAGE]],
	frames: {
		// dimensions of the animation
		height: 256 * scale,
		width: 8 * scale,
		// number of frames
		count: 3
	},
	animations: {
		// we call this animation 'default'
		default: {
			// it contains all 3 frames
			frames:[0, 1, 2],
			// and it runs with a decent speed
			frequency: 5
		}
	}
}
// ...

// and now we can initialize an animation at any time
var spriteSheet = new SpriteSheet(waterFallSpriteSheetData);
var waterfall = new BitmapAnimation(spriteSheet);
waterfall.snapToPixel = true;
waterfall.gotoAndPlay('default');</pre><p></p>
<p><strong>CAUTION:</strong> EaselJS version 0.4.2 does <strong>NOT</strong> support Canvas-Elements in SpriteSheets, however our scaling-algorithm creates a Canvas-Element, meaning that we have to UPDATE our version of EaselJS &#8211; you can download the &#8220;NEXT-Version&#8221; on <a href="https://github.com/CreateJS/EaselJS" target="_blank">github.com/CreateJS/EaselJS</a> &#8211; However, be aware, that this version uses namespacing, the simplest fix to get our game running with the &#8220;NEXT-Version&#8221; is to add the following into our index.html BEFORE loading EaselJS:</p><pre class="crayon-plain-tag">&lt;script&gt;
	var createjs = window;
&lt;/script&gt;
&lt;!-- now your can load the rest --&gt;
&lt;script src=&quot;js/easeljs-NEXT.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;js/utils.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;js/hero.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;js/jumpy.js&quot;&gt;&lt;/script&gt;</pre><p></p>
<p><strong>Note:</strong> The SpriteSheet-Data CAN also be stored and loaded from a JSON &#8211; but in this case the SpriteSheet-Data contans dynamic values, like the size and the image, that&#8217;s why I prefer to just initialize the whole thing within a JS method. For a more detailed view on all the possible attributes of a SpriteSheet, please refer to: <a href="http://createjs.com/Docs/EaselJS/SpriteSheet.html" target="_blank">http://createjs.com/Docs/EaselJS/SpriteSheet.html</a>.</p>
<p>As my next step I created a new layer in the front to add the waterfalls to and added a method that would add a waterfall to every platform with a probability of 35%. Since this is just placing and moving objects, I&#8217;m not going into detail about what lines of code I added where &#8211; for details please download and view the <a href="http://demos.indiegamr.com/jumpy/part5/part5.zip">sourcecode</a>.<br />
<iframe src="http://demos.indiegamr.com/jumpy/part5/index_c.html" width="100%" height="250"></iframe></p>
<p>As a final step I then added a fly to fly around our hero, it&#8217;s based on a simple 2-frame animation, and its movement is the following:</p><pre class="crayon-plain-tag">// the movement of the fly
// calculate an offset to create a &quot;swirling-like&quot; behaviour
fly.offsetX = ( Math.cos(ticks/10) * 10) * scale;
fly.offsetY = ( Math.sin(ticks/ 7) *  5) * scale;
// smoothly follow the hero by 10% of the distance every frame
fly.x = fly.x + (hero.x - fly.x) * .1 + fly.offsetX;
fly.y = fly.y + (hero.y - fly.y) * .1 + fly.offsetY;</pre><p></p>
<p><iframe src="http://demos.indiegamr.com/jumpy/part5/index_d.html" width="100%" height="250"></iframe></p>
<hr/>
<strong>And as allways: Download the <a href="http://demos.indiegamr.com/jumpy/part5/part5.zip">source</a>.</strong></p>
<hr/>
<strong>Agenda</strong></p>
<ul>
<li><a href="http://indiegamr.com/retro-style-plattform-runner-game-for-mobile-with-easeljs-part-1/">Part 1: User-Input (Keystrokes, MouseClick, Touch)  &amp; Movement</a></li>
<li><a href="http://indiegamr.com/retro-style-plattform-runner-game-for-mobile-with-easeljs-part-2/">Part 2: Collisions between objects</a></li>
<li><a href="http://indiegamr.com/retro-style-platform-runner-game-for-mobile-with-easeljs-part-3-adding-movement-more-collision/">Part 3: Movement&#038;More Collision</a></li>
<li><a href="http://indiegamr.com/retro-style-platform-runner-game-for-mobile-with-easeljs-part-4-adjusting-to-mobile-displays/">Part 4: Adjustments for mobile devices</a></li>
<li><strong>Part 5: Polishing up the game with animations &#038; eye candy</strong></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://indiegamr.com/retro-style-platform-runner-game-for-mobile-with-easeljs-part-5-animations-and-polishing/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Retro Style Platform Runner Game for mobile with EaselJS (Part 4) – adjusting to mobile displays</title>
		<link>http://indiegamr.com/retro-style-platform-runner-game-for-mobile-with-easeljs-part-4-adjusting-to-mobile-displays/</link>
		<comments>http://indiegamr.com/retro-style-platform-runner-game-for-mobile-with-easeljs-part-4-adjusting-to-mobile-displays/#comments</comments>
		<pubDate>Wed, 18 Jul 2012 07:22:22 +0000</pubDate>
		<dc:creator><![CDATA[olsn]]></dc:creator>
				<category><![CDATA[EaselJS]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mobile]]></category>

		<guid isPermaLink="false">http://indiegamr.com/?p=261</guid>
		<description><![CDATA[The 4th part of my series on creating an EaselJS-Game is about adjusting the game to different display-sizes, which is very important when targeting mobile devices. To visualize, what the result is going to look like &#8211; I added the &#8230; <a href="http://indiegamr.com/retro-style-platform-runner-game-for-mobile-with-easeljs-part-4-adjusting-to-mobile-displays/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>The 4th part of my series on creating an EaselJS-Game is about adjusting the game to different display-sizes, which is very important when targeting mobile devices. To visualize, what the result is going to look like &#8211; I added the following two iframes, to demonstrate how the game adjusts to a different screen-/stage-size.</p>
<p><iframe width="100%" height="300" src="http://demos.indiegamr.com/jumpy/part4/index_nearestNeighbor.html"></iframe><br />
<br/>Open this <a href="http://demos.indiegamr.com/jumpy/part4/index_nearestNeighbor.html" target="_blank">http://demos.indiegamr.com/jumpy/part4/index_nearestNeighbor.html</a> in a new browser-window and see how it adjusts to your window-size as you reload the page after resizing the window.<br />
<span id="more-261"></span><br />
As allways: There are many ways to do this, all will pros and cons, I&#8217;m going to explain two (fairly simple) ways to do this.</p>
<h2>Method I &#8211; Linear Scaling: Easy&#038;straight forward</h2>
<hr/>
&#8216;Method I&#8217; basically consists of these two steps:</p>
<ol>
<li>Calculate the minimum-scale from the width&#038;height based on a default-width &#038; default hight</li>
<li>Multiply every (scale-relevant) value of the game with that scale(e.g: dimensions of objects, velocities, ect..)</li>
</ol>
<p>Calculating the scale is just one line: Okay in my case it&#8217;s 3, because I added variables for the default-dimensions:</p><pre class="crayon-plain-tag">/* defined globally */
var BASE_WIDTH = 800,
    BASE_HEIGHT = 400;
// this was new to me here: i tried using 'const' for those values, instead
// of 'var' which was working fine until i tried it with ...guess what: IE!

/* when initializing the _game() */
var w = getWidth(),
    h = getHeight(),
    scale = Math.min(w/BASE_WIDTH,h/BASE_HEIGHT);

// to make the scale accessbile from the 'outsite' through
// Game.scale, we assign it to the Game-object
self.scale = scale;</pre><p></p>
<p>So now that we have our scale-value: We are going to multiply every value, relevant to the scale, with it. As this involves quite a few parts of the code, instead of posting the whole updated source-code I&#8217;m going to list a few examples, and you can download the full source as always at the end of the page.</p><pre class="crayon-plain-tag">// e.g: when setting up the objects or position them
hero.x = 50 * scale;
hero.y = h/2 + 50 * scale;
hero.scaleX = hero.scaleY = scale;

// or when resetting attributes like velocity
Hero.prototype.reset = function() {
    this.velocity = {x:10*Game.scale,y:25*Game.scale};
    this.onGround = false;
    this.doubleJump = false;
};

// or when updating values
this.velocity.y += 1 * Game.scale;</pre><p></p>
<h2>Adjustments</h2>
<hr/>
However &#8211; we are not quite done by just multiplying all the values. Since we now <strong>(can) have decimal values</strong> for the width and height of all objects, we need to watch out for <strong>flaws in the collision detection</strong>: to prevent decimenal numbers from screwing with our collision detection, I added a parameter <span style="font-family:Courier;">&lt;rounded&gt;</span> to the utility-method <span style="font-family:Courier;">&#8216;getBounds()&#8217;</span>:<br />
<pre class="crayon-plain-tag">function getBounds(obj,rounded) {
  /* 
     I left out the calculation of the bounds, this is
     described in Part2
  */
  // and now round everything to get integer values
  if ( rounded ) {
    bounds.x = (bounds.x + (bounds.x &gt; 0 ? .5 : -.5)) | 0;
    bounds.y = (bounds.y + (bounds.y &gt; 0 ? .5 : -.5)) | 0;
    bounds.width = (bounds.width + (bounds.width &gt; 0 ? .5 : -.5)) | 0;
    bounds.height = (bounds.height + (bounds.height &gt; 0 ? .5 : -.5)) | 0;
  }
  return bounds;
}</pre><br />
In case you are wondering, why I&#8217;m not using &#8220;Math.round()&#8221;: Rounding with <strong>bitwise operators</strong> is <strong>7-9x faster</strong> than using Math.round(): <a href="http://jsperf.com/math-round-vs-hack/3" target="_bank">jsperf round() vs. bitwise benchmark</a></p>
<p style="font-size: 80%; padding: 4px; background: #ddd; border: solid 1px #aaa;">You don&#8217;t have to fully understand bitwise operations, but as a basic fact you should know, that with any bitwise operation all decimals will be erased. However, for optimizing code it is <strong>allways helpfull</strong> to know how bitwise <a href="http://en.wikipedia.org/wiki/Bitwise_operation" target="_blank">works</a>!</p>
<p>So all values from the returned bounds-rectangle will be integer values, to prevent the method from not detecting a collision, but then moving the object for example 0.5px too far, thus creating a collision-situation.<br />
Rounding values like the position or the velocity of an object is in 90% of the cases a good thing to do, as it is basically impossible to spot a difference between rounded- and non-rounded pixel-values while it is (on most platforms) <a href="http://seb.ly/2011/02/html5-canvas-sprite-optimisation/" target="_blank">way faster for the canvas to render on pixel instead of subpixel values</a>. So: use integer-values or for EaselJS-Objects: <span style="font-family:Courier;">myObject.snapToPixel = true;</span> &#8211; it is faster and in most cases you won&#8217;t see the difference.</p>
<p>That&#8217;s it with <i>Method I</i> of adjusting a canvas-app to the screen-size, you might think: <i>&#8216;Looks good to me, why should I use anything else?!&#8217;</i> &#8211; here&#8217;s the reason: The following image shows two scaled versions of Method I and Method II &#8211; and since we are building a <strong>retro-/pixel-style</strong> game, guess which method we are going to use <img src="http://indiegamr.com/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p>
<p><a href="http://indiegamr.com/retro-style-platform-runner-game-for-mobile-with-easeljs-part-4-adjusting-to-mobile-displays/scaling/" rel="attachment wp-att-303"><img src="http://indiegamr.com/wp-content/uploads/scaling.jpg" alt="" title="scaling" width="600" height="300" class="aligncenter size-full wp-image-303" /></a></p>
<h2>Method II &#8211; Nearest Neighbor Scaling: keep hard pixel-edges</h2>
<hr/>
As there is (currently?) no reliable way to turn off anti aliasing, (actually there ARE <a href="http://stackoverflow.com/a/10525163/1518765" target="_blank">ways to do this</a>, but they won&#8217;t work across all browsers), we are going to use the following algorithm to scale each of our assets after they are done loading:<br />
<pre class="crayon-plain-tag">function nearestNeighborScale(img, scale)
{
  // to have a good looking scaling
  // we will snap all values to 0.5-steps
  // so 1.4 e.g. becomes 1.5 - you can also
  // set the snapping to 1.0 e.g.
  // however I would recommend to use only 
  // a multiple of 0.5 - but play around
  // with it and see the results
  scale = snapValue(scale,.5);
  if ( scale <= 0 ) scale = 0.5;

  // the size of the "pixels" in the new images
  // will be rounden to integer values, as drawing
  // a rect with 1.5x1.5 would result in half-transparent
  // areas
  var pixelSize = (scale+0.99) | 0;

  // getting the data-array containing all the pixel-data
  // from our source-image
  var src_canvas = document.createElement('canvas');
  src_canvas.width = img.width;
  src_canvas.height = img.height;
  var src_ctx = src_canvas.getContext('2d');
  src_ctx.drawImage(img, 0, 0);
  var src_data = src_ctx.getImageData(0, 0, img.width, img.height).data;
  
  // setting up the new, scaled image
  var dst_canvas = document.createElement('canvas');
  // just to be sure, that no pixel gets lost, when
  // we scale the image down, we add 1 and floor the
  // result
  dst_canvas.width = (img.width * scale+1) | 0;
  dst_canvas.height = (img.height * scale+1) | 0;
  var dst_ctx = dst_canvas.getContext('2d');

  // reading each pixel-data from the source
  // and drawing a scaled version of that pixel
  // to the new canvas
  var offset = 0;
  for (var y = 0; y < img.height; ++y) {
      for (var x = 0; x < img.width; ++x) {
          var r = src_data[offset++];
          var g = src_data[offset++];
          var b = src_data[offset++];
          var a = src_data[offset++] / 255; // the alpha value needs to be divided
          dst_ctx.fillStyle = 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
          dst_ctx.fillRect(x * scale, y * scale, pixelSize, pixelSize);
      }
  }

  return dst_canvas;
}

function snapValue(value,snap)
{
  var roundedSnap = (value/snap + (value > 0 ? .5 : -.5)) | 0;
  return roundedSnap * snap;
}</pre></p>
<p>So when the game gets initialized, we scale any bitmap loaded:</p><pre class="crayon-plain-tag">self.initializeGame = function() {
  assets[HERO_IMAGE] = nearestNeighborScale(assets[HERO_IMAGE], scale);
  assets[PLATFORM_IMAGE] = nearestNeighborScale(assets[PLATFORM_IMAGE], scale);
  // ...
  // see source for the rest of initializing part(no changes there)</pre><p></p>
<p>And then again, like with &#8216;Method I&#8217; we have to adjust every scale-relevant value, like velocities, positions <strong>EXCEPT</strong> for any scaleX or scaleY values, because our nearsetNeighborScale-method did that already. Also, just to be on the save side, we also do the collision-detection adjustments here (see bounds-rounding further up in the article).</p>
<h2>Compare both methods side by side</h2>
<hr/>
<div style="width: 100%;">
(both scales are limited to 0.5)</p>
<div style="width: 45%; float: left; padding: 3px; text-align: right; margin-left: auto;">
</iframe><iframe width="100%" height="150" src="http://demos.indiegamr.com/jumpy/part4/index_linearScale.html"></iframe><br />
<strong>Method I</strong><br />
  <span style="background:#aaffaa;">seamless scaling possible +</span><br />
  <span style="background:#ffaaaa;">image gets distorted on small scale -</span><br />
  <span style="background:#ffaaaa;">image gets blurry on big scale -</span>
</div>
<div style="width: 45%; float: left; border-left: 2px solid #000; padding: 3px;  margin-right: auto;">
</iframe><iframe width="100%" height="150" src="http://demos.indiegamr.com/jumpy/part4/index_nearestNeighbor.html"></iframe><br />
<strong>Method II</strong><br />
  <span style="background:#aaffaa;">+ no distortion, no blurryness</span><br />
  <span style="background:#ffaaaa;">- needs 0.5x values(0.5,1,1.5,2&#8230;)</span><br />
  <span style="background:#ffaaaa;">- only if retro-/pixel-effect is wanted</span>
</div>
<div style="clear:both;"></div>
</div>
<p><br/><br />
<strong>Download the sources: <a href="http://demos.indiegamr.com/jumpy/part4/part4.zip">here</a>.</strong></p>
<p><strong>Agenda</strong></p>
<ul>
<li><a href="http://indiegamr.com/retro-style-plattform-runner-game-for-mobile-with-easeljs-part-1/">Part 1: User-Input (Keystrokes, MouseClick, Touch)  &amp; Movement</a></li>
<li><a href="http://indiegamr.com/retro-style-plattform-runner-game-for-mobile-with-easeljs-part-2/">Part 2: Collisions between objects</a></li>
<li><a href="http://indiegamr.com/retro-style-platform-runner-game-for-mobile-with-easeljs-part-3-adding-movement-more-collision/">Part 3: Movement&#038;More Collision</a></li>
<li><strong>Part 4: Adjustments for mobile devices</strong></li>
<li><a href="http://indiegamr.com/retro-style-platform-runner-game-for-mobile-with-easeljs-part-5-animations-and-polishing/">Part 5: Polishing up the game with animations &#038; eye candy</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://indiegamr.com/retro-style-platform-runner-game-for-mobile-with-easeljs-part-4-adjusting-to-mobile-displays/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
