<?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>Jens Arps &#187; fun</title>
	<atom:link href="http://jensarps.de/tag/fun/feed/" rel="self" type="application/rss+xml" />
	<link>http://jensarps.de</link>
	<description></description>
	<lastBuildDate>Tue, 01 May 2012 12:00:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Porting a 3D RPG to WebGL, Part 1</title>
		<link>http://jensarps.de/2011/11/07/porting-a-3d-rpg-to-webgl-part-1/</link>
		<comments>http://jensarps.de/2011/11/07/porting-a-3d-rpg-to-webgl-part-1/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 17:53:33 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[Experiments in Web]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[WebGL]]></category>

		<guid isPermaLink="false">http://jensarps.de/?p=366</guid>
		<description><![CDATA[It all began half a year ago when I sat down with a friend and fellow crew member, Stephan, and we agreed that Gothic was indeed one the best RPGs of all times (seriously, if you don&#8217;t know it and are into RPGs, you should get it). We also soon agreed that it would be [...]]]></description>
			<content:encoded><![CDATA[<p>It all began half a year ago when I sat down with a friend and fellow crew member, <a href="http://twitter.com/#%21/evilhackerdude" target="_blank">Stephan</a>, and we agreed that Gothic was indeed one the best RPGs of all times (seriously, if you don&#8217;t know it and are into RPGs, you should get it). We also soon agreed that it would be awesome to experience it&#8217;s amazing atmosphere in a browser, using WebGL. Stephan shopped two shiny new copies of Gothic and we started hacking away and gave the project the very fitting title RavenJS&#8230;</p>
<p>Update: Stephan recommended to put the video on top,  so here it goes: Some impressions from RavenJS, enjoy (fullscreen and headphones recommended)!</p>
<p><iframe src="http://www.youtube.com/embed/OWy96m8lo-U" frameborder="0" width="456" height="262"></iframe></p>
<p><span id="more-366"></span></p>
<h1>The Beginning</h1>
<p>When we started, I had little to no knowledge about working with 3D and OpenGL – I&#8217;m a JavaScript guy, after all. When I first fired up Blender and tried to work with it I was pretty close to dropping the whole idea again. Same happened when I saw how shader code looked like. But I so much wanted this – and the existing wrappers like <a href="http://www.glge.org/" target="_blank">GLGE</a> or <a href="https://github.com/mrdoob/three.js" target="_blank">three.js</a> enable us to still work with WebGL even if we don&#8217;t have the background knowledge.</p>
<p>So I decided to go with GLGE as I had already played around it with it&#8217;s examples before and because it already had nice collada support, which I decided to have as format for meshes and animations.<br />
The first step was to extract some assets from the original game, collect mesh and texture data and get it into Blender. Luckily, Gothic has a highly active modding community, and this community is supported by the original game developers. So there were a couple of tools that could be used to aid in this, and it didn&#8217;t take long and we had a sword rotating in a browser.</p>
<p>But if it was possible to load a sword into GLGE, then shouldn&#8217;t it also be technically possible to load parts of the world into it? Yes, it was. It took some time, and it&#8217;s still far from perfect, but it worked. The next step was to create a first person camera that would react to mouse and key events – and had collision detection. Real collision detection, a height map would get us nowhere, as the levels had bridges and indoor locations, and I needed moving objects, too. Luckily, GLGE had some demos using raycasting to achieve this that I could use as start and modify until it worked as needed.</p>
<p>To recreate the dark intense atmosphere I played around with the right ambient lighting, fog types and added a sky box. Well, I would have loved to have a real sky dome, but, alas, I have no idea how to do this… Finally, I added objects, both static and animated.</p>
<h1>In action</h1>
<p>I can&#8217;t and won&#8217;t put online a live demo, as I obviously have no rights to do so. The copyright of the meshes and textures used belongs to the original developers, Piranha Bytes, and the RavenJS project is in now way endorsed, encouraged or supported by nor affiliated with them. But I can show you some impressions of what it looks like so far (fullscreen and headphones recommended):</p>
<p>Update: Video is now on top <img src='http://jensarps.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>(The song in this video is called &#8220;A special thing&#8221; by &#8220;amanyth&#8221; and published under a Creative Commons license.)</p>
<h1>Issues</h1>
<p>Besides the mere amount of free time that is needed and lacking, there are currently four main issues that are a bit of a show-stopper:</p>
<p>Animations caused a big deal of problems, and it&#8217;s still not prossible to run more than one animation sequence &#8211; running multiple animations will screw up bone position. While the animations still run fine in Blender, something goes wrong during the export. I&#8217;m still to find out if this can be overcome by using an alternate format for export.</p>
<p>Another thing that is still unsolved is &#8220;double-faced textures&#8221; that have transparent parts (look at the trees in the video). Those transparent parts become plain black when rendered in the browser. I&#8217;ve read up on this, and it seems that this scenario is indeed not trivial… From what I understood so far, in our case there&#8217;s two shapes &#8220;glued&#8221; to each other and each one has its own material, but I still have no idea what exactly goes wrong.</p>
<p>A real biggie, performance-wise, is that there is no logic yet that handles display of objects that are not visible. In the original game e.g., doors and the likes are &#8220;closed&#8221; by vertices with solid (non-transparent) textures and everything that&#8217;s behind it is taken out from the whole rendering/object interaction/whatever process. Only when you get really close you can see through these and see the intereour of the adjectant room. If these vertices get actually removed or if you &#8220;portal&#8221; through them I don&#8217;t know.</p>
<p>Mouse lock. Of course. No further explanation needed, we&#8217;re all looking forward to seeing this in the browser.</p>
<h2>Future</h2>
<p>I always loved 3D games. And the fact that it&#8217;s possible to render 3D worlds in a browser – with acceptable performance – is just stunning. The majority of work is still ahead, I&#8217;m well aware of that, but I *so much* want this to happen… So I just hope that some of the major problems can be solved and this project can go on!</p>
]]></content:encoded>
			<wfw:commentRss>http://jensarps.de/2011/11/07/porting-a-3d-rpg-to-webgl-part-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Promises with dojo &#8211; a lightweight alternative to dojo.Deferred</title>
		<link>http://jensarps.de/2010/02/03/promises-with-dojo-a-lightweight-alternative-to-dojo-deferred/</link>
		<comments>http://jensarps.de/2010/02/03/promises-with-dojo-a-lightweight-alternative-to-dojo-deferred/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 23:27:16 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[Dojo Love]]></category>
		<category><![CDATA[Experiments in Web]]></category>
		<category><![CDATA[CommonJS]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[dojox]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://jensarps.de/?p=190</guid>
		<description><![CDATA[Update: Micheil Smith had a cool idea to improve this and posted it in the comments. I have updated the test page and the code examples to reflect his idea. Thanks Micheil! Update II: Ben Hockey proposed to do the whole thing without using the pub/sub system. I for one think its a good idea, [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Update:</strong> <a href="http://twitter.com/Miksago" target="_blank">Micheil Smith</a> had a cool idea to improve this and posted it in the comments. I have updated the test page and the code examples to reflect his idea. Thanks Micheil!</em></p>
<p><em><strong>Update II:</strong> <a rel="nofollow" href="#comment-489">Ben Hockey</a> proposed to do the whole thing without using the pub/sub system. I for one think its a good idea, but in case you want the topic version, I&#8217;ll leave the test page and add <a href="http://jensarps.de/tests/dojo_tests/dojo-release-1.4.0-src/test_Promise_notopic.html" target="_blank">another one</a> with the topic-less version. Thanks Ben!</em></p>
<p><em><strong>Update III</strong>: Dojo 1.5 introduces robust Promises with dojo.deferred as Krys Zyp explains in <a href="http://www.sitepen.com/blog/2010/05/03/robust-promises-with-dojo-deferred-1-5/" target="_blank">this SitePen post</a>.<br />
</em></p>
<p>&#8212;</p>
<p>One of the things/ideas/concepts that I really like about CommonJS is Promises.</p>
<p>Promises? A brief explanation from the <a href="http://wiki.commonjs.org/wiki/Promises" target="_blank">CommonJS API</a>:</p>
<blockquote><p>Promises provide a well-defined interface for interacting with an object  that represents the result of an action that is performed  asynchronously, and may or may not be finished at any given point in  time.</p></blockquote>
<p>To hear more about Promises, I highly recommend <a href="http://www.michaelharrison.ws/weblog/?p=198" target="_blank">these</a> <a href="http://www.sitepen.com/blog/2010/01/19/commonjsjsgi-the-emerging-javascript-application-server-platform/" target="_blank">two</a> posts.<br />
<span id="more-190"></span><br />
That concept is not entirely new. The dojo toolkit has <a href="http://docs.dojocampus.org/dojo/Deferred" target="_blank">dojo.Deferred</a>, which does a similar job. You create a Deferred, add (multiple) callbacks and error handlers to it, and when it fires off, all your handlers get called. But dojo.Deferred is pretty heavyweight. It&#8217;s loads of code. And it works with <code>.addCallback()</code> and <code>.addErrback()</code> methods – not with <code>.then()</code> methods, which provide maximum readability.</p>
<p><strong>The goal</strong></p>
<p>What I want to have is the follwing code running (with dojo):</p>
<pre>asyncComputeTheAnswerToEverything().
    then(addTwo).
    then(printResult, onError);</pre>
<p><em>(This is the example code from the proposed CommonJS Promise API)</em></p>
<p>And I want the execution to continue right after <code>doSomethingAsnyc()</code> – the async call should still be non-blocking. The <code>.then()</code> methods should recieve the results of the previous part of the chain, but still be non-blocking. Luckily, dojo provides a tool that allows us to achieve this: the publish/subscribe system.</p>
<p><strong>Let&#8217;s do it</strong></p>
<p>When we have unique topics for each promise made, we can subscribe the <code>.then()</code> method to the current topic in the chain, and publish the next topic when the data handlers are done computing. Within the <code>then()</code> method we need to check if the data we recieve is an error or not, to determine wich handler we need to call. The spec also demands a progress handler – if we wanted to implement that, we needed to add another topic, but I left that out.</p>
<p>We only need to kick off  the whole thing when we create the first promise, then the rest takes care for itself. Including the changes proposed by Micheil, what you have to do is pretty trivial and you don&#8217;t have to care about unique topics or the pub/sub system at all. The <code>asyncComputeTheAnswerToEverything()</code> function mentioned above would look like the following:</p>
<pre>var asyncComputeTheAnswerToEverything = function(){
	var promise = new dojox.Promise();
	window.setTimeout(function(){ // some async operation
		var data = 42;
		promise.emit(data);
	},2000);
	return promise;
};</pre>
<p>You create a Promise and store a reference to it. You return the promise and use the reference to it to kick off the chain when the data is there. Everything else runs on it&#8217;s own now.<br />
Go to the <a href="http://jensarps.de/tests/dojo_tests/dojo-release-1.4.0-src/test_Promise_notopic.html" target="_blank">test page</a> or theand check the source &#8211; it contains the Promise code and the example functions. Open the console and run <code>testSpec();</code> to start off the example code from the CommonJS API proposal.</p>
<p><strong>Usage</strong></p>
<p>As a namespace to put the Promise into I chose dojox – but choose whatever floats your boat. You can just drop the promise code somewhere into your code, or, if want to <code>dojo.require("dojox.Promise")</code> it, create a file named <code>Promise.js</code> that contains the code and place a <code>dojo.provide("dojox.Promise");</code> as the first line in it. Save the file in the dojox directory and you&#8217;re done. (Well, to be honest, this violates the dojox naming convention, but if its just in your project, it&#8217;s ok and it will work.)</p>
<p>Any questions left? Feel free to leave a comment, @ me at twitter or drop me a line.</p>
<p><strong>References / Files / More<br />
</strong></p>
<ul>
<li><a href="http://wiki.commonjs.org/wiki/Promises" target="_blank">CommonJS Promise API Proposal</a></li>
<li><a href="http://www.sitepen.com/blog/2010/01/19/commonjsjsgi-the-emerging-javascript-application-server-platform/" target="_blank">Post by Kris Zyp @ SitePen blog about CommonJS</a></li>
<li><a href="http://www.michaelharrison.ws/weblog/?p=198" target="_blank">Post on www.michaelharrison.ws about promises and more</a></li>
<li><a href="http://jensarps.de/tests/dojo_tests/dojo-release-1.4.0-src/test_Promise.html" target="_blank">the test page (using topics and pub/sub)</a></li>
<li><a href="http://jensarps.de/tests/dojo_tests/dojo-release-1.4.0-src/test_Promise_notopic.html">the test page (w/o topics, recommended)</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://jensarps.de/2010/02/03/promises-with-dojo-a-lightweight-alternative-to-dojo-deferred/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Working with the Curve &#8211; Advanced Animation with Dojo</title>
		<link>http://jensarps.de/2010/01/22/working-with-the-curve-advanced-animation-with-dojo/</link>
		<comments>http://jensarps.de/2010/01/22/working-with-the-curve-advanced-animation-with-dojo/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 21:03:16 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[Dojo Love]]></category>
		<category><![CDATA[Experiments in Web]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[dojox]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://jensarps.de/?p=170</guid>
		<description><![CDATA[If you use animations, you probably use them to animate CSS properties. But this post is about some real bareknuckle animation – using the dojo toolkit. We&#8217;ll have a look into dojo.Animation and talk about the curve, the line, easing and rate, and we&#8217;ll check out (the somehow undocumented) dojox.fx._core – and see how to [...]]]></description>
			<content:encoded><![CDATA[<p>If you use animations, you probably use them to animate CSS properties. But this post is about some <em>real bareknuckle animation</em> – using the dojo toolkit. We&#8217;ll have a look into dojo.Animation and talk about the curve, the line, easing and rate, and we&#8217;ll check out (the somehow undocumented) dojox.fx._core – and see how to work with multidimensional lines.</p>
<p><span id="more-170"></span></p>
<p><strong>The Curve</strong></p>
<p>Every dojo.Animation needs a curve. If you call <code>animateProperty</code> or it&#8217;s shorthand <code>anim</code>, you don&#8217;t have to provide the curve for yourself, dojo will do it for you – if you build an animation using <code>new dojo.Animation()</code>, you have to provide your own curve. You can think of the curve as of a line that the animation walks on. The line is already present before the animation starts to walk it, and it can always take a break and pause in the middle and eventually continue walking where it had stopped. To create a curve, you need to call <code>new dojo._Line(start, end)</code>. In return, you get an object with three properties: <code>end</code>, <code>start</code> and a <code>getValue()</code> method.</p>
<pre>var anim = new dojo.Animation({
	curve: new dojo._Line(0,100)
});</pre>
<p><strong>Easing</strong></p>
<p>The curve will then be modified by a easing function. By default, this is the linear <code>dojo._defaultEasing</code>. Easing breathes a bit of life into the curve, you can say. Other properties of dojo.Animation that affect the curve are <code>rate</code> and <code>duration</code>. <code>rate</code> is the timespan between two frames. To calculate the targeted fps, use <code>(1000/rate)</code>. By default, dojo aims at 50 fps.</p>
<pre>var anim = new dojo.Animation({
	curve: new dojo._Line(0,100),
	easing: dojo.fx.easing.quadInOut
});</pre>
<p><strong>Waking Life</strong></p>
<p>To get some numbers out of <code>dojo.Animation</code>, let&#8217;s let it go wild and <code>play()</code>. When using <code>animateProperty</code> or the likes, you just fire and forget. In our case, we need to get the numbers. To achieve this, we connect to our animation&#8217;s <code>onAnimate</code> event. This is fired on every frame of the animation. When your connected function is called, you get – who would have guessed – the current value of the curve. Now you have a function called at a given rate, for a given period of time and you get get a numeric value that is already calculated for you. Decent!</p>
<pre>dojo.connect(anim,'onAnimate',function(curveValue){
	// do something
});</pre>
<p>To see some curves in action, go to <a href="http://jensarps.de/tests/dojo_tests/animation.html" target="_blank">this test page</a>, open the console and type <code>play(1);</code> and <code>play(2);</code> and check the source code to see what&#8217;s happening.</p>
<p><strong>Multidimensional Curves</strong></p>
<p><code>dojo._Line</code> generates only one-dimensional curves. But, there is <code>dojox.fx._core</code>, which contains an (old) version of <code>dojox.fx._Line</code>, which is capable of creating multi-dimensional curves. To use it, you have to explicitely <code>dojo.require("dojox.fx._core")</code>. It&#8217;s a bit odd, but the _core file contains only the _Line, and it is not loaded when requiring &#8220;dojox.fx&#8221;. Well, whatever, you can pass arrays of numbers into <code>dojox.fx._Line</code>:</p>
<pre>new dojox.fx._Line([0,2],[100,6]);</pre>
<p>Now you have a multi-dimensional curve. Or, if you could say, you have two curves in one. You can (technically) pass an unlimited amount of numbers into <code>dojox.fx._Line</code>. When you hand this curve to a <code>dojo.Animation</code>, it will animate all dimensions of the curve. And it will modify all dimensions of the curve according to easing, rate and duration. If you connect a function to <code>onAnimate</code>, it will now recieve an array of numbers.</p>
<pre>var anim = new dojo.Animation({
	curve: new dojox.fx._Line([0,2],[100,6]),
	easing: dojo.fx.easing.quadInOut
});</pre>
<p>Head over to the <a href="http://jensarps.de/tests/dojo_tests/animation.html">test page</a> again, and type <code>play(3);</code> into the console. Now, the width of the bars is also animated – by the same animation function that calculates the height of the bars.</p>
<p><strong>What now?</strong></p>
<p>If your mind is still not blown away with ideas about how to use this, let me give you a start:</p>
<ul>
<li>animate position, width and height of a browser window and use it as a screensaver for some unknowing, yet thankful visitor…</li>
<li>take some CSS 2D and 3D transisitions/transforms and animate away…</li>
<li>canvas! No need to say another word…</li>
<li>Still too banal? Go WebGL! Grab some 3DContext and animate an evil ball of jelly jumping around!</li>
</ul>
<p>In short: dojo&#8217;s animation system allows you to not only animate CSS properties, but gives you a good tool for  animations in general. You get the curves on the one hand, but you also get the &#8220;player&#8221; for these curves, including play/pause/stop/gotoPercent methods. And dojo.fx.easing has a lot of easing functions in stock.</p>
]]></content:encoded>
			<wfw:commentRss>http://jensarps.de/2010/01/22/working-with-the-curve-advanced-animation-with-dojo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More fun with strings: dojo.string.contains()</title>
		<link>http://jensarps.de/2009/11/29/more-fun-with-strings-dojo-string-contains/</link>
		<comments>http://jensarps.de/2009/11/29/more-fun-with-strings-dojo-string-contains/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 19:27:25 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[Dojo Love]]></category>
		<category><![CDATA[Experiments in Web]]></category>
		<category><![CDATA[Goodies to go]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://jensarps.de/?p=114</guid>
		<description><![CDATA[In the series &#8220;convenience wrappers for small tasks that increase code readability&#8221;, today contains() is starring. Having a contains() method could also serve another purpose: to maybe prevent people from using match() to find out if a string contains a given substring (what is still proposed in some JS tutorials out there…). So, I want [...]]]></description>
			<content:encoded><![CDATA[<p>In the series &#8220;convenience wrappers for small tasks that increase code readability&#8221;, today contains() is starring. Having a contains() method could also serve another purpose: to maybe prevent people from using match() to find out if a string contains a given substring (what is still proposed in some JS tutorials out there…). So, I want the contains() method to also have a switch to work case-insensitive.</p>
<p>Besides indexOf(), there are some other ways to achieve this, so – let&#8217;s have a competition and find out who&#8217;s the fastest!<br />
<span id="more-114"></span></p>
<h3>The Contestants</h3>
<p><strong>replace and length</strong></p>
<p>One possibility is to replace the searched string with an empty string, read the length property of the modified string and compare it to the length of the haystack. If it&#8217;s different, the haystack contains the needle. Reading the length property is extra work, but comparing two integers is faster than comparing two strings, so maybe it&#8217;s faster in general.</p>
<p><strong>replace</strong></p>
<p>Again, replace the searched string with an empty string. Then compare the the modified string with the haystack. If they are different, the haystack contains the needle.</p>
<p><strong>split</strong></p>
<p>Take the haystack and try to split it using the needle as the seperator. If the result&#8217;s length is greater than one, the haystack contains the needle.</p>
<p><strong>indexOf</strong></p>
<p>Find the first occurance of needle in haystack; if the result is something else than -1, the haystack contains the needle.</p>
<h3>Results</h3>
<p>For testing, I did 10,000 iterations and retrieved the execution time in ms. The methods were tested in the order presented above.</p>
<p>On Chromium (Mac build, Version 4.0.203.0 here), replace + length is slightly faster than replace, and both are faster than split. indexOf is by far the fastest.</p>
<p>1) true: 7 / false: 3.5<br />
2) true: 7.5 / false: 3.5<br />
3) true: 10 / false: 8<br />
4) true: 2.5 / false: 3</p>
<p>Safari 4 has nearly the same results as Chromium, but the numbers tend to differ a lot from test to test.</p>
<p>1) true: 4-18, avg 10 / false: 2.5<br />
2) true: 4-19, avg 10 / false: 3<br />
3) true: 6-22. avg 13 / false: 6-23 avg. 16<br />
4) true: 2 / false: 3</p>
<p>On Firefox 3.5, split a bit faster than the two replace methods, but indexOf is again by far the fastest.</p>
<p>1) true: 13 / false 10<br />
2) true: 13 / false 10<br />
3) true: 10 / false 9<br />
4) true: 1.5 / false: 2</p>
<p>On IE 8 (run in a VM), both replace versions perform almost the same, and faster than split. Again, indexOf is fastest. Only 2 &#8211; 3 times faster than the replace methods, but still the fastest.</p>
<p>1) true: 35 / false: 25<br />
2) true: 30 / false: 25<br />
3) true: 60 / false: 50<br />
4) true: 15 / false: 15</p>
<p>You can run the tests yourself, if you are interested, the test page is <a href="http://jensarps.de/tests/dojo_tests/test_Contains.html" target="_blank">here</a>.</p>
<h3>Summary</h3>
<p>The results are pretty obvious: indexOf() outperforms the other contestants. Which is not really a surprise, considering that whatever Javascript does during indexOf() –  it has also to do the same before being able to do a split() or replace(). So, the proposed way for a contains() method is the following:</p>
<pre>dojo.string.contains = function(/* string */ needle, /* string */ haystack, /* bool */ caseInsensitive) {
    if(caseInsensitive) {
        needle = needle.toLowerCase();
        haystack = haystack.toLowerCase();
    }
    return haystack.indexOf(needle) !== -1;
}</pre>
<p>If you want to use contains() in your code, just copy the above lines somewhere in your code. Again, don&#8217;t forget to dojo.require(&#8220;dojo.string&#8221;) before.</p>
<p>Or, if you want to have beginsWith(), endsWith() and contains() all-in-one, use this: <a href="http://jensarps.de/tests/dojo_tests/dojo.string.addons.js" target="_blank">dojo.string.addons.js</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jensarps.de/2009/11/29/more-fun-with-strings-dojo-string-contains/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Keyboard accessibility for dojox.layout.FloatingPane</title>
		<link>http://jensarps.de/2009/11/02/keyboard-accessibility-for-dojox-layout-floatingpane/</link>
		<comments>http://jensarps.de/2009/11/02/keyboard-accessibility-for-dojox-layout-floatingpane/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 19:26:58 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[Dojo Love]]></category>
		<category><![CDATA[Experiments in Web]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[dojox]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[widget]]></category>

		<guid isPermaLink="false">http://jensarps.de/?p=94</guid>
		<description><![CDATA[Well, I don&#8217;t really see a use for FloatingPane, except as a windowing system for projects like LucidDesktop, but I got the impression that many people just love the FloatingPane. However, it caught my attention these days. One of its major drawbacks – and the one that keeps it pretty far from becoming a joyful [...]]]></description>
			<content:encoded><![CDATA[<p>Well, I don&#8217;t really see a use for FloatingPane, except as a windowing system for projects like <a href="http://www.lucid-desktop.org/" target="_blank">LucidDesktop</a>, but I got the impression that many people just love the FloatingPane. However, it caught my attention these days.</p>
<p>One of its major drawbacks – and the one that keeps it pretty far from becoming a joyful member of the dijit namespace – is the lack of keyboard accessibility. It has plenty of other issues and could use some refactoring, but this one is outstanding. Hm, adding keyboard access shouldn&#8217;t be too hard I thought and went for it…<br />
<span id="more-94"></span><br />
<strong>1. Moving and resizing</strong></p>
<p>To enable keybord movement, there are some prequesites: First, the pane needs to be able to recieve focus and move itself to front via the keyboard. Second, the pane needs a lot of visual markers. And it needs a certain state in which the keybord movement can actually be activated.</p>
<p>So, the idea is the following: If you tab through the panes, the focus is set to the pane&#8217;s title node. If you click into a pane or into its child widgets, you need to shift-tab upwards until the title node recieves focus. In my minimal styling, the title node text is in <em>italics</em> when it has focus. You can then press space to &#8220;activate&#8221; keyboard movement. An overlay is shown to indicate that the &#8220;moveMode&#8221; is active. Then you can use the arrow keys to move, and shift-arrow to resize the pane. To exit this mode, press space or escape or take the focus off the title node by clicking somewhere else or tab away.</p>
<p>In short, the first step included:</p>
<ul>
<li>Adding visual hints to indicate which FloatingPane is on top (via box-shadow and a bold title node text)</li>
<li>Adding a visual hint to indicate when the focus is on the title node and &#8220;moveMode&#8221; can be activated</li>
<li>Enabling the pane to get on top of other panes by tabbing to it</li>
<li>Adding key handlers to start and end &#8220;moveMode&#8221;</li>
<li>Adding typematic key listerners to realize movement</li>
</ul>
<p>Oh, did I mention that dojo&#8217;s typematic is awesome? Nope? Well, it&#8217;s awesome! Go try it out for yourself!</p>
<p><strong>2. Minimize, maximize, restore and the dock</strong></p>
<p>The FloatingPane also has docking abilities – so the dock and it&#8217;s items had be accessible, too. The dock items have their own widget class, so this had to be modified. The changes are minor: adding a tabIndex and a keyhandler. And, of course, adding a visual hint to indicate that the dock item has focus (that is, the label is bold). When the dock item has focus, press space to restore the corresponding pane.</p>
<p>Next, the pane needed extra key handling: I chose &#8220;+&#8221; to maximize, &#8220;-&#8221; to minimize (or restore, if pane is maximized) and &#8220;x&#8221; to close the pane.</p>
<p><strong>3. Baking it all together</strong></p>
<p>As it isn&#8217;t that desireable to have two widgets doing the same in the dojox namespace, I merged the changes with FloatingPane. But, then again, as I did some other minor changes to FloatingPane (but including changes to the CSS classNames to have a consistent use of baseClass, and inheriting from dijit.layout.ContentPane instead of dojox.layout.ContentPane), I named the new widget &#8220;WindowPane&#8221;.</p>
<p><strong>What&#8217;s left?</strong></p>
<p>Short: A ton. The widget needs to be refactored, it has a couple of bugs. The code I added is undocumented, the CSS incomplete. Well, I&#8217;ll be short on time for a week or two, but I&#8217;ll surely get back to this. Consider this a work in progress. But, hey, maybe having keybord accessibility will give further develepmont of this widget a little push.</p>
<p><strong>You want to use it?</strong></p>
<p>Cool, there are two options:</p>
<p>- You can use dojox.layout.Window to gain just movement/resize abilities. Then bake Window.js and the extra CSS from the test page into your build, replace calls to FloatingPane with calls to Window, and you&#8217;re done.<br />
- Or, if you don&#8217;t use FloatingPane yet or want the complete functionality, grab the WindowPane.js file, the WindowPane.css file and the WindowPane.html template and add them to your dojox. This is probably the best way to do.</p>
<p>See below for files.</p>
<p><strong>Keybord summary</strong></p>
<p>Use Tab to navigate from pane to pane, and press space when the title node has focus (title text is itailc) to activate keyboard interaction.</p>
<p>Arrow keys: move pane around<br />
Shift + arrow keys: resize pane<br />
plus: maximize pane<br />
minus: minimize pane or restore pane (if maximized)<br />
x: close pane</p>
<p>Press space again or escape to deactivate keyboard interaction. Or just tab or click away.</p>
<p><strong>Files:</strong></p>
<p>Step one (moving and resizing):</p>
<ul>
<li><a href="http://jensarps.de/tests/dojo_tests/dojo-release-1.4.0-src/dojox/layout/Window.js" target="_blank">/dojox/layout/Window.js</a></li>
<li><a href="http://jensarps.de/tests/dojo_tests/dojo-release-1.4.0-src/dojox/layout/tests/test_Window.html" target="_blank">test page</a></li>
</ul>
<p>Or: Step three (full)</p>
<ul>
<li><a href="http://jensarps.de/tests/dojo_tests/dojo-release-1.4.0-src/dojox/layout/WindowPane.js" target="_blank">/dojox/layout/WindowPane.js</a></li>
<li><a href="http://jensarps.de/tests/dojo_tests/dojo-release-1.4.0-src/dojox/layout/resources/WindowPane.html" target="_blank">/dojox/layout/resources/WindowPane.html</a></li>
<li><a href="http://jensarps.de/tests/dojo_tests/dojo-release-1.4.0-src/dojox/layout/resources/WindowPane.css" target="_blank">/dojox/layout/resources/WindowPane.css</a></li>
<li><a href="http://jensarps.de/tests/dojo_tests/dojo-release-1.4.0-src/dojox/layout/tests/test_WindowPane.html" target="_blank">test page</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://jensarps.de/2009/11/02/keyboard-accessibility-for-dojox-layout-floatingpane/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>dojo.string.beginsWith()</title>
		<link>http://jensarps.de/2009/10/27/dojo-string-beginswith/</link>
		<comments>http://jensarps.de/2009/10/27/dojo-string-beginswith/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 20:36:18 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[Dojo Love]]></category>
		<category><![CDATA[Goodies to go]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://jensarps.de/?p=78</guid>
		<description><![CDATA[Most cases where you find String.substr() in the wild are to check if a given string begins with a certain other string. Be it checking for a prefix or sorting out zipcodes that begin with certain numbers. And because code readability is a good thing (really, it is important), it would be nice to have [...]]]></description>
			<content:encoded><![CDATA[<p>Most cases where you find String.substr() in the wild are to check if a given string begins with a certain other string. Be it checking for a prefix or sorting out zipcodes that begin with certain numbers. And because code readability is a good thing (really, it <em>is</em> important), it would be nice to have a String.beginsWith() method. Or, because of dojo love, a dojo.string.beginsWith() method.</p>
<p>Consider the following code:</p>
<pre>var nearbyZipcodes = dojo.filter(givenZipcodes,function(zipcode){
    return dojo.string.beginsWith(zipcode,'12');
});</pre>
<p><span id="more-78"></span><br />
No need to tell you what this does, right? So, let&#8217;s do this then! The only thing left is to think about performance: Is there a difference between String.substr() and String.substring()? And can we get faster than the two?</p>
<p>If we have very short needles to look for in our haystack, one could consider the following approach:</p>
<pre>dojo.string.beginsWith = function(/* string */ needle, /* string */ haystack) {
    var i,
        len = needle.length;
    if(needle.length &gt; haystack.length) {
        return false;
    }
    for(i = 0; i &lt; len; i++) {
        if(needle.charAt(i) !== haystack.charAt(i)) {
            return false;
        }
    }
    return true;
}</pre>
<p>For very short needles, or when we expect close to no hits, this might be faster. So I set up a test page and let the different methods run against each other. The results clearly spoke against the char iteration method: On Firefox, iteration was <em>always</em> slower, even when the iteration method could return false after the first character. And when it had to iterate more often, times went up (I somehow had in mind Tracemonkey was perfect for simple iterations – but in this case it won&#8217;t help). Only on Safari the iteration method could compete with native substr() / substring() – but was never significantly faster. So, we&#8217;ll stick to the native methods (there was no real difference between the two).</p>
<p>More convenience?</p>
<p>Depending on your datasource, you might want to trim the input. No problem, as we use dojo, we can use it&#8217;s super fast trim and end up with the following:</p>
<pre>dojo.string.beginsWith = function(/* string */ needle, /* string */ haystack, /* bool */ trimBefore) {
    if(trimBefore) {
        needle = dojo.string.trim(needle)
    }
    if(needle.length &gt; haystack.length) {
        return false;
    }
    return haystack.substr(0,needle.length) === needle;
}</pre>
<p>So simple, so sweet.</p>
<p>You can run the tests for yourself, the page is located here: <a href="http://jensarps.de/tests/dojo_tests/test_beginsWith.html" target="_blank">test_beginsWith.html</a></p>
<p>If you want to use beginsWith in your code, just put the lines above it anywhere in your code – but don&#8217;t forget to dojo.require(&#8216;dojo.string&#8217;) before.</p>
]]></content:encoded>
			<wfw:commentRss>http://jensarps.de/2009/10/27/dojo-string-beginswith/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>3D Canvas Walker revisited</title>
		<link>http://jensarps.de/2009/08/27/3d-canvas-walker-revisited/</link>
		<comments>http://jensarps.de/2009/08/27/3d-canvas-walker-revisited/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 20:01:26 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[Experiments in Web]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[js]]></category>

		<guid isPermaLink="false">http://jensarps.de/?p=12</guid>
		<description><![CDATA[One of the things that fascinated me most about computers since I was a teen, was their ability to simulate a three-dimensional space where one could move around in. That&#8217;s why I was so intrigued when I read Jacob Seidelin&#8217;s post about “Creating pseudo 3D Games“. I had to play around with it, as I [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_22" class="wp-caption alignleft" style="width: 160px"><a href="http://jensarps.de/wp-content/uploads/2009/08/walker-screenshot.jpg"><img class="size-thumbnail wp-image-22 " title="walker-screenshot" src="http://jensarps.de/wp-content/uploads/2009/08/walker-screenshot.jpg" alt="Screenshot" width="150" /></a><p class="wp-caption-text">Screenshot</p></div>
<p>One of the things that fascinated me most about computers since I was a teen, was their ability to simulate a three-dimensional space where one could move around in.</p>
<p>That&#8217;s why I was so intrigued when I read Jacob Seidelin&#8217;s post about “<a href="http://dev.opera.com/articles/view/creating-pseudo-3d-games-with-html-5-can-1/">Creating pseudo 3D Games</a>“. I had to play around with it, as I wanted to have mouse control in it, but performance was so bad that it was impossible. So I laid aside that idea. Eventually, he <a href="http://dev.opera.com/articles/view/3d-games-with-canvas-and-raycasting-part/">continued this project</a> and used it for something pretty awesome called “<a href="http://blog.nihilogic.dk/2008/07/wolfenflickr-3d-unlikely-mashup.html">WolfenFlickr</a>&#8220;, where you can walk around and look at Flickr photos, attached to walls. As I said, pretty awesome.</p>
<p>Anyway, what I wanted was mouse navigation and strafe keys, as you have it in shooters – this kind of moving around always gives me the feeling of being “free“ in a way. Experiencing that freedom in a web page (w/o flash, of course) was still on my mind.</p>
<p><span id="more-12"></span></p>
<p>And the I stumbled upon Ben Joffe&#8217;s <a href="http://www.benjoffe.com/code/demos/canvascape/textures">textured Canvascape 3D-Walker</a>. The performance of the canvas element drawing the image slices was better than letting the browser render single elements for each slice – much better. In fact, the idea of mouse control immeadiately popped up again and I had to give it a try. And – it worked! Of course it has some drawbacks (that is, when the cursor leaves the browser window, there&#8217;s no more control…), but it gave me that feeling of freedom while moving around!</p>
<p>Fooling around with the canvas element was fun and tought me about it&#8217;s image methods. And I hope to find more time to take it further…</p>
<p>Of course there&#8217;s an aweful lot of work ahead if one would want this to be pretty (it&#8217;s all bricks now, no interaction, perspective issues, no smooth movement on strafe, and so on…) and performance is still an issue with high-res textures, but it is a lot of fun moving around.</p>
<p>Here&#8217;s what I added to Ben Joffe&#8217;s Walker:</p>
<ul>
<li>mouse control</li>
<li>strafe movement</li>
<li>walk and crouch</li>
<li>high-res textures</li>
</ul>
<p>I dropped IE-Support for this and disabled the mini map for performance reasons.</p>
<p>You&#8217;ll need <a href="http://www.google.com/chrome" target="_blank">Chrome</a>/<a href="http://code.google.com/intl/de-DE/chromium/" target="_blank">Chromium</a> to see it in action in high details mode, but it&#8217;s definitely worth it!</p>
<p><a href="http://jensarps.de/tests/canvas/textures.html" target="_blank">Go give it try!</a> Move the mouse cursor into the center of your browser window, press “M“ and enjoy!</p>
<p>PS: Chromium builds for Mac can be found <a href="http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/">here</a>.</p>
<p>Note: The modifications are all just minor, all credits for this go out to <a href="http://www.benjoffe.com/">Ben Joffe</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://jensarps.de/2009/08/27/3d-canvas-walker-revisited/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

