<?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; js</title>
	<atom:link href="http://jensarps.de/tag/js/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>EmbedJS 0.2 and AMD</title>
		<link>http://jensarps.de/2011/10/17/embedjs-0-2-and-amd/</link>
		<comments>http://jensarps.de/2011/10/17/embedjs-0-2-and-amd/#comments</comments>
		<pubDate>Mon, 17 Oct 2011 14:44:36 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[EmbedJS]]></category>
		<category><![CDATA[Framework Peace]]></category>
		<category><![CDATA[AMD]]></category>
		<category><![CDATA[CommonJS]]></category>
		<category><![CDATA[embedjs]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[modules]]></category>
		<category><![CDATA[uxebu]]></category>

		<guid isPermaLink="false">http://jensarps.de/?p=352</guid>
		<description><![CDATA[[Note: This is a cross-post. I also published this on the uxebu blog.]During JSConf.eu, we released EmbedJS 0.2. That release was mainly about a fundamental architectural change: the move to the AMD pattern for our features. Let me explain why we decided to do this, why we think this is a big step forward and [...]]]></description>
			<content:encoded><![CDATA[<p>[<em>Note: This is a cross-post. I also published this on the <a href="http://uxebu.com/blog/2011/10/17/embedjs-0-2-and-amd/" target="_blank">uxebu blog</a>.</em>]</p><p>During JSConf.eu, we released EmbedJS 0.2. That release was mainly about a fundamental architectural change: the move to the AMD pattern for our features. Let me explain why we decided to do this, why we think this is a big step forward and what this means for the user.</p>
<p><span id="more-352"></span></p>
<h2>Modules</h2>
<p>AMD stands for “Asynchronous Module Definition”. So, what are modules? I’ll quote the definition from the <a href="http://wiki.commonjs.org/wiki/Modules/1.1" target="_blank">CommonJS Module specification</a>, as it describes it very well:</p>
<blockquote><p>This specification addresses how modules should be written in order to be interoperable among a class of module systems that can be both client and server side, secure or insecure, implemented today or supported by future systems with syntax extensions. These modules are offered privacy of their top scope, facility for importing singleton objects from other modules, and exporting their own API.</p></blockquote>
<p>So, modules are isolated parts of code that live in their own scope and are kind of self-contained. Well, that is pretty much how we see the individual Features in EmbedJS – we made our feature implementations as small as possible and reduced dependencies as much as possible. Many of EmbedJS’ features are also browser-independent, so why not make it possible to use some of the tools in a non-browser environment?</p>
<p>(If you want to learn more on modules and why we need them, I recommend <a href="http://twitter.com/briancavalier" target="_blank">@briancavalier</a>‘s <a href="http://briancavalier.com/presentations/pgh-js-amd-10-2011/" target="_blank">presentation on modules</a>.)</p>
<h2>Features in EmbedJS</h2>
<p>So far, I didn’t really explain what I mean with “Features”, so let me do another quote, this time from the <a href="http://uxebu.github.com/embedjs/" target="_blank">Project Page</a>:</p>
<blockquote><p>EmbedJS uses the concept of features: Functionalities are split up into features, as fine-grained as possible, and each feature might have multiple implementations.</p></blockquote>
<p>Our Features are kinda small and contain only few methods, sometimes even just one. The asyc-promise feature, for example, will get you Promises, and the transport-jsonp feature will get you a method for pull in data via jsonp – and that’s it.  If multiple methods are contained within one feature, this is because they are too closely related to each other to split them up, like the html-class feature, which contains the <code><span class="text">addClass</span></code>, <code><span class="text">removeClass</span></code>, <code><span class="text">toggleClass</span></code> and <code><span class="text">hasClass</span></code> methods.</p>
<h2>AMD vs CommonJS Modules 1.1</h2>
<p>Now that we knew modules were the way to go, there was another, quite important question to solve: There are two common patterns for modules, so which one do we want to apply? One pattern is described in the above mentioned CommonJS Wiki, the other pattern is described in the <a href="https://github.com/amdjs/amdjs-api/wiki/AMD" target="_blank">AMD wiki</a>.</p>
<p>Well, there have been <a href="http://tagneto.blogspot.com/2011/04/on-inventing-js-module-formats-and.html" target="_blank">some</a> <a href="http://unscriptable.com/index.php/2011/09/30/amd-versus-cjs-whats-the-best-format/" target="_blank">blog</a> <a href="http://blog.millermedeiros.com/2011/09/amd-is-better-for-the-web-than-commonjs-modules/" target="_blank">posts</a> lately that compare the pros and cons of each of them, so I don’t want to repeat all of this – but I’m hiving a similar tendency towards AMD. Besides this, there was another big reason to go with AMD: Remember, EmbedJS is based on the <a href="http://dojotoolkit.org/" target="_blank">Dojo Toolkit</a>, and it’s a <a href="http://dojofoundation.org/" target="_blank">Dojo Foundation Project</a> – and we hope that the efforts that went into making EmbedJS might flow back into the Dojo Toolkit one day. And as the Dojo Toolkit uses the AMD approach, the decision was quickly made to do so as well.</p>
<h2>Implications for EmbedJS</h2>
<p>There are several huge benefits for EmbedJS in using the AMD approach. The most noteworthy surely is the ease-of-use for developers. In your code, when you find you want an EmbedJS method, you <a href="http://uxebu.github.com/embedjs/docs/" target="_blank">look up the feature name</a> that provides this method, add it to the require statement and there you go. Man, easy.</p>
<p>A second important thing is that EmbedJS now plays really nice with AMD based projects. In fact, you could see EmbedJS now as a collection of small useful modules, that can be easily integrated – and also easily extended.</p>
<p>The third major benefit is that we can now get rid of all the custom tools we built around EmbedJS – you can load features with any AMD loader (though we use and recommend RequireJS) and build it with any AMD optimizer/builder (we recommend r.js).</p>
<p>Let’s have an extra word on</p>
<p><strong>Building</strong></p>
<p>It was always possible with EmbedJS to create a custom build that would contain only the features you wanted/needed in your project. But, you had to use our build tool that we created. And learning using Yet-Another-Build-Tool™ is always… well, let’s just say it would be cool to avoid it (and, not to forget, so is maintaining it). We did a lot to improve that, we created a GUI that allowed you to just “click together” the features you wanted, and we worked on a dynamic loader that would ease development. But still, the situation was far from ideal, and people just ended up going for our ready-made “kitchensink” builds that contained <em>all</em> of the features.</p>
<p>Now, we still provide those ready-made builds that contain only the implementations for a given platform (and are stripped of require/define calls), but I really hope that using the AMD approach and using an AMD optimizer to deploy will become the more adopted style.</p>
<h2>Q: Cross-target? A: The Feature Plugin</h2>
<p>One goal of EmbedJS is to allow for easy cross-device development. We got rid of run-time branching but instead went for an approach that held the different implementations for a given feature in separate files, so that one only has to deploy the actually needed code to a given device – and not all the alternate implementations, that sit useless in the device’s memory and never get used. We needed to achieve that with “in-house” means provided by the AMD approach. Lucky us, there’s the concept of <a href="https://github.com/amdjs/amdjs-api/wiki/Loader-Plugins" target="_blank">loader-plugins</a>, and so I created a <a href="https://github.com/jensarps/AMD-feature" target="_blank">feature-plugin for AMD loaders</a>. The idea is simple yet effective: You create an implementation map that maps a feature name to an implementation, and if there are multiple implementations, you supply tests that tells the plugin which feature to use.</p>
<p>When you’re ready to deploy, you have two choices: I f you really don’t know on what target your code is going to end up, you can use the dynamic implementation map containing the tests for the optimizer, and you’ll get a build that contains all of the available implementations. Then, during runtime, the feature plugin will run the tests and load the specific implementation. But, if you <em>do</em> know what target your code will run on, you provide a specific implementation map that doesn’t contain tests but directly maps to the according implementations, and you will only have those in your built file.</p>
<h2>Synopsis</h2>
<p>AMD is exactly what we needed to make EmbedJS more what we want it to be – an easy to use JavaScript toolbox that is especially handy for cross-target scenarios such as cross-platform mobile development. So, please don’t hesitate and check it out on Github, read the project page and check out the examples!</p>
<p><strong>Resources / Further Reading</strong></p>
<ul>
<li><a href="https://github.com/uxebu/embedjs" target="_blank">EmbedJS on GitHub</a></li>
<li><a href="http://uxebu.github.com/embedjs/" target="_blank">EmbedJS Project Page</a></li>
<li><a href="http://uxebu.github.com/embedjs/docs/" target="_blank">EmbedJS API Docs</a></li>
<li><a href="https://github.com/jensarps/AMD-feature" target="_blank">AMD-Feature plugin</a></li>
<li><a href="https://github.com/amdjs/amdjs-api/wiki" target="_blank">AMD Wiki</a></li>
<li><a href="http://wiki.commonjs.org/wiki/CommonJS" target="_blank">CommonJS Wiki</a></li>
</ul>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://jensarps.de/2011/10/17/embedjs-0-2-and-amd/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using IndexedDB on Firefox</title>
		<link>http://jensarps.de/2011/03/30/using-indexeddb-on-firefox/</link>
		<comments>http://jensarps.de/2011/03/30/using-indexeddb-on-firefox/#comments</comments>
		<pubDate>Wed, 30 Mar 2011 14:20:26 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[Storage]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[persistence]]></category>

		<guid isPermaLink="false">http://jensarps.de/?p=289</guid>
		<description><![CDATA[[Note: This is a cross-post. I also published this on the uxebu blog.]As Firefox 4 is now stable, chances are that the async IDB (IndexedDB) API is not going to change anymore. But IDB is not localStorage – it can be a major pain to work with. So, here is a guide on how to use [...]]]></description>
			<content:encoded><![CDATA[<p>[<em>Note: This is a cross-post. I also published this on the <a href="http://uxebu.com/blog/2011/03/30/using-indexeddb-on-firefox/" target="_blank">uxebu blog</a>.</em>]</p><p>As Firefox 4 is now stable, chances are that the async IDB (IndexedDB) API is not going to change anymore. But IDB is not localStorage – it can be a major pain to work with. So, here is a guide on how to use IDB in Firefox as a key-value store.</p>
<p><span id="more-289"></span><br />
<em><strong>TL;DR</strong></em></p>
<p>There is example code available at the end of the post. If you don‘t want to read this lengthy post and/or prefer to jump right in the middle of the action, grab the example implementation and play around with it.</p>
<p>Yes, it is. But I still argue that in WebApp context a key-value store will cover about 90% of use cases. So I won‘t cover in this post how to create fancy key ranges or how to create multiple indices. But I will cover the five basic methods that you will need to work with any store: get, set, remove, getAll and clear – this will enable you to work with IDB and give you an understanding of how it works. You‘re then of course heartly encouraged to explore it and it‘s features further!</p>
<p>As long as the sync API is not there, all operations are async. That means that you will make requests. All the time. These requests will have an onerror and an onsuccess property, where you can put your callbacks/handlers in. If your request was successful, you will get an event in your callback. That event contains a lot of useful and useless information, but what you will want to look at in most cases is event.target.result – this is where your result lives in. In most cases, you will have to start a transaction before; inside of the transaction, you can access the objectStore and make your request. However, there are different types of transactions, but you will get the details below.</p>
<p>Yep, it‘s not that you can just start away and store your data, there‘s some work to do ahead…</p>
<h2>Open the Database</h2>
<p>Easy one:</p>
<p><code class="codecolorer text mac-classic"><span class="text">event.target.result</span></code> then contains a reference to the database. Store it, you will need it.</p>
<h2>Creating/Opening the ObjectStore</h2>
<p>That‘s a little tougher. Some operations, such as creating and deleting object stores, require the database to be in a „mutation transaction state“. Wow. Well, there‘s only one way to put the db into this state, and that‘s via a <code class="codecolorer text mac-classic"><span class="text">setVersion</span></code> request. If you are opening the db for the first time ever, you will need to do this anyway. You can, of course, change the version of the database anytime, which might be a useful thing if you, e.g., change the structure of how your objectStore saves the data. To check what version the database in the user‘s browser has, you can check the <code class="codecolorer text mac-classic"><span class="text">version</span></code> property in the reference to the database anytime.</p>
<p>Inside of the callback function, you can now check if your store already exists, or if you need to create it. The reference to your db contains an object called <code class="codecolorer text mac-classic"><span class="text">objectStoreNames</span></code>, which is a list of existing objectStores. As this is a DOMStringList, it features the handy <code class="codecolorer text mac-classic"><span class="text">contains()</span></code> method:</p>
<p>If the store is not there, you need to create it via the <code class="codecolorer text mac-classic"><span class="text">createObjectStore()</span></code> method. It takes two arguments: the name of the store, and an object with two properties: <code class="codecolorer text mac-classic"><span class="text">keyPath</span></code> and <code class="codecolorer text mac-classic"><span class="text">autoIncrement</span></code>. keyPath is… well… the path to the key. Think of it as the one column that is the primary key in your data table (you all did some MySQL at some time, I bet). You can do really fancy things with the keyPath in objectStores, but it‘s perfectly fine to just use something like „id“ as the keyPath. If you want to store an object and want to provide the keyPath, all you need to do is let the object you want to store have a property with the name of the keyPath, and a unique value. Uh, it‘s not that complicated, examples will follow. AutoIncrement is, well, autoIncrement. You have the option to let the objectStore take care for the uniqueness of it‘s data‘s keyPath properties, or do it yourself.</p>
<p>You immediately get a reference to the newly created objectStore, so save it.</p>
<p>If the store is already there, you need to open it. Um, well, you don‘t really have to open it, but you might want to obtain a reference to it, just to make sure that it really exists. To do this, you need to start a transaction via the <code class="codecolorer text mac-classic"><span class="text">transaction()</span></code> method. It accepts three arguments: an array of store names (the ones which you want to work with), an integer denoting whether you want to read and/or write during that transaction, and a timeout. There are constants for read/write you can use for better readability in your code, you can find them at <code class="codecolorer text mac-classic"><span class="text">IDBTransaction.READ_ONLY</span></code> and <code class="codecolorer text mac-classic"><span class="text">IDBTransaction.READ_WRITE</span></code>. The timeout parameter is optional. That method returns a transaction object that contains a method called <code class="codecolorer text mac-classic"><span class="text">objectStore</span></code>, which you can use to access your store. This is the „default“ way transactions work; the mutation transaction above is an eception to this, where you need to be in the callback of the transaction to modify the database.</p>
<p>In our case, we can start an „empty“ transaction:</p>
<p>There you have your reference to the store.</p>
<p>Now that the store is ready to use, it‘s time to store some data in it. To do so, you need to start a transaction, access the object store, and call the store‘s <code class="codecolorer text mac-classic"><span class="text">put</span></code> method:</p>
<p>That‘s all. This type of transaction is what you also use for other data manipulation tasks, for example to retrieve data:</p>
<p>You will need to provide the key of the stored entry to get it; as you need to do when you want to remove it:</p>
<p>If you want to get all data objects from the store:</p>
<p>And, finally, if you want to clear (i.e., delete all data objects) the store:</p>
<p>It isn‘t. Yeah, well, it is, as you can store objects and don‘t have to stringify them before. But basically, to get the real advantages of IDB, you will need to dive deeper into it. Features like keyRanges remove the need to do map/reduce action when you search for items in your store. But now you have an idea of how the whole thing works. If you want to go further, browse in the spec or the MDC docs and try things out!</p>
<p><strong>Example Code</strong></p>
<p>There is an example page over here: <a href="http://static.uxebu.com/~jens/indexeddb/ff-idb.html" target="_blank">http://static.uxebu.com/~jens/indexeddb/ff-idb.html</a>.</p>
<p>It uses an ObjectStore wrapper I created using dojo that covers the basic methods mentioned above, the JavaScript is here: <a href="http://static.uxebu.com/~jens/indexeddb/idb-ff.js" target="_blank">http://static.uxebu.com/~jens/indexeddb/idb-ff.js</a>.</p>
<p>When you open the page, open the console and you will see some messages there. Check out the source to see what‘s going on; it‘s basically a round trip through all the basic methods.</p>
<p><strong>Further Reading</strong></p>
<ul>
<li>The spec: <a href="http://www.w3.org/TR/IndexedDB/" target="_blank">http://www.w3.org/TR/IndexedDB/</a></li>
<li>The MDC article: <a href="https://developer.mozilla.org/en/IndexedDB" target="_blank">https://developer.mozilla.org/en/IndexedDB</a></li>
</ul>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://jensarps.de/2011/03/30/using-indexeddb-on-firefox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Explaining EmbedJS</title>
		<link>http://jensarps.de/2011/01/31/explaining-embedjs/</link>
		<comments>http://jensarps.de/2011/01/31/explaining-embedjs/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 15:12:31 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[EmbedJS]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[embedjs]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[uxebu]]></category>

		<guid isPermaLink="false">http://jensarps.de/?p=290</guid>
		<description><![CDATA[[Note: This is a cross-post. I also published this on the uxebu blog.]Last week, we tagged the current state of EmbedJS 0.1. This is a large step for us, and something we have been waiting for and wanting to do for a long time. And with doing so, the need arises to answer a lot [...]]]></description>
			<content:encoded><![CDATA[<p>[<em>Note: This is a cross-post. I also published this on the <a href="http://uxebu.com/blog/2011/01/31/explaining-embedjs/" target="_blank">uxebu blog</a>.</em>]</p><p>Last week, we tagged the current state of EmbedJS 0.1. This is a large step for us, and something we have been waiting for and wanting to do for a long time. And with doing so, the need arises to answer a lot of questions – and we better start sooner than later. So, here it is, the first part of „Explaining EmbedJS“.</p>
<p><span id="more-290"></span></p>
<h2>What is it?</h2>
<p>It‘s a JavaScript Toolkit, especially aimed at embedded devices. Embedded devices, that is phones, tablet devices, TVs, car dashboards and the like. Devices that are capable of running JavaScript. We are sure that this list is going to grow over time – and will hopefully one day also include devices like your fridge. EmbedJS is based on the Dojo Toolkit.</p>
<h2>Why?</h2>
<p>Because the toolkits and frameworks that exist focus on desktop browsers and therefore have a different approach – an approach we didn‘t find that suitable for embedded/mobile development. And the JS frameworks that target mobile devices are flawed, too: either they target only a specific device or platform, or they provide far more than we do need and want.</p>
<p>What we wanted was a toolkit that offered a unified API across different platforms, but with the best implementation that was available for each platform. And we didn‘t want to ship code that implemented a certain feature for, say, an old Blackberry device to an iPhone. Plus, we wanted that API to be small – enough to assist us in our daily work, but not that bloated that we would ship code to a phone that we wouldn‘t need in most of our projects.</p>
<h2>The Dojo Toolkit</h2>
<p>We at uxebu have a strong affinity to the <a href="http://dojotoolkit.org/" target="_blank">Dojo Toolkit</a> – most of us are committers – for a good reason: we believe that it has done many things right, and that it has an excellent code quality. Thus, finding a starting point for our venture to what was later to be EmbedJS was pretty easy: It was the very core of the Dojo Toolkit, dojo._base.</p>
<p>We examined the code and split it into its smallest parts (features, how we call them in EmbedJS). After having the features all nicely separated, we could look for the best implementation for each of the platforms we wanted to support, and put it into a separate file. Then we optimized our code, tweaking here and there, but also using the code from dojo._base as it was, because it already was the best implementation.</p>
<p>And we needed to find means to put all these tiny parts together again, respecting any given dependecies, to create a file that then contained all the feature‘s implementations for a given platform – so the EmbedJS Tools were born, including our highly flexible build system.</p>
<h2>API</h2>
<p>EmbedJS‘ API is a small subset on Dojo‘s API. It provides around 70 methods, from the fields language enhancement (like Array manipulation or JSON), OO (like classes and inheritance), transport (like JSONP or XHR), event system (like connecting to DOM events or methods), DOM manipulation (like style or node creation) and misc things like Promises or pub/sub.</p>
<p>We kept the method signatures intact so that you can import your existing knowledge from working with the Dojo Toolkit to EmbedJS. There are only a few minor changes where we</p>
<p>a) found a new name highly useful, e.g <code class="codecolorer text mac-classic"><span class="text">dojo.io.script.get()</span></code> is <code class="codecolorer text mac-classic"><span class="text">embed.jsonp()</span></code></p>
<p>b) we needed to strip given functionality for the sake of code size.</p>
<p>All methods in EmbedJS are available under the embed namespace as well as under the dojo namespace. I.e. to call the connect method you can use <code class="codecolorer text mac-classic"><span class="text">dojo.connect()</span></code> and <code class="codecolorer text mac-classic"><span class="text">embed.connect()</span></code>.</p>
<p>The full API documentations is <a href="http://embedjs.org/apidocs/dools/app/apidoc/embedjs/" target="_blank">here</a>, though it‘s undergoing some updates right now.</p>
<h2>Summary</h2>
<p>I didn‘t want to dig too deep in the first article on EmbedJS, but I guess you got the point: EmbedJS is a highly optimized Dojo core, and offers you a specialized, optimal build for a given platform. To explain how the build system works, how we manage dependencies, how we separate our features and our implementations and how one can create a project-oriented custom build will need another blog post – but if you are too curious, you are invited to take a look at <a href="https://github.com/uxebu/embedjs" target="_blank">the source code at github</a>; a lot is self-explenatory there. A further source of information is the <a href="http://uxebu.github.com/embedjs/" target="_blank">EmbedJS project page</a>, as well as the <a href="https://github.com/uxebu/embedjs-tools" target="_blank">EmbedJS Tools source</a>.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://jensarps.de/2011/01/31/explaining-embedjs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>localStorage Performance Test Results</title>
		<link>http://jensarps.de/2010/09/07/localstorage-performance-test-results/</link>
		<comments>http://jensarps.de/2010/09/07/localstorage-performance-test-results/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 11:17:50 +0000</pubDate>
		<dc:creator>Jens Arps</dc:creator>
				<category><![CDATA[Experiments in Web]]></category>
		<category><![CDATA[Storage]]></category>
		<category><![CDATA[client-side storage]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[localStorage]]></category>
		<category><![CDATA[persistence]]></category>

		<guid isPermaLink="false">http://jensarps.de/?p=260</guid>
		<description><![CDATA[It&#8217;s been some time since I last updated this blog, mostly because there&#8217;s plenty going on these days. However, there&#8217;s something I&#8217;ve been wanting to publish for quite some time now: The results of the localStorage performance tests I ran several weeks ago. As I am currently working on performance tests for Mozilla&#8217;s IndexedDB implementation, [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been some time since I last updated this blog, mostly because there&#8217;s plenty going on these days. However, there&#8217;s something I&#8217;ve been wanting to publish for quite some time now: The results of the localStorage performance tests I ran several weeks ago. As I am currently working on performance tests for Mozilla&#8217;s IndexedDB implementation, which is available in latest Minefield releases, I got reminded that there are still other results to publish – so, here we go:</p>
<p><span id="more-260"></span></p>
<p>Among other tests, the main thing I wanted to know is how long does it take to read or write 1000 entries from/to localStorage. The keys were of the form &#8220;key000&#8243;, the values &#8220;val000&#8243;, with increasing numbers. So the tests were using minimal data – only 6  bytes – but the results are still useful to get an idea of how it goes – and how the browsers perform compared to each other.</p>
<p><strong>Desktop</strong></p>
<p>All test were run on a Mac Mini. Opera and IE were tested on a virtual machine, so there might be an additional performance penalty based on virtualization. However, here are the numbers (average time needed to run the respective method 1000 times):</p>
<p>Safari 4:<br />
- getItem: 1.4ms<br />
- setItem: 1.7ms<br />
- key: 0.8ms</p>
<p>Chrome:<br />
- getItem: 240ms<br />
- setIem: 280ms<br />
- key: 230ms</p>
<p>Firefox:<br />
- getItem: 77ms<br />
- setItem: 2100ms<br />
- key: 25ms</p>
<p>Opera:<br />
- getItem: 19ms<br />
- setItem: 20ms<br />
- key: 18ms</p>
<p>IE8:<br />
- getItem: 5ms<br />
- setItem: 72ms<br />
- key: 3.6ms</p>
<p><strong>Mobile</strong></p>
<p>As we can see, Safari is lightening fast. That made me want to run the tests on a mobile phone that has a modern Safari browser. I took an HTC Desire, wich comes with Android 2.1, and thus has a Safari capable of localStorage. Here are the results:</p>
<p>HTC Desire:<br />
- getItem: 16.4ms<br />
- setItem: 19.5ms<br />
- key: 8.7ms</p>
<p>This is pretty amazing, considering that it is a phone. It still beats most of the browseres on the desktop.</p>
<p>If I find that test page again (uh, yeah, it&#8217;s lost somehow), I&#8217;ll run the tests again with newer browser releases and publish the link here, so you can run the tests yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://jensarps.de/2010/09/07/localstorage-performance-test-results/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Encrypted client-side storage with dojo</title>
		<link>http://jensarps.de/2010/04/15/encrypted-client-side-storage-with-dojo/</link>
		<comments>http://jensarps.de/2010/04/15/encrypted-client-side-storage-with-dojo/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 21:48:54 +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[Storage]]></category>
		<category><![CDATA[client-side storage]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[dojox]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[localStorage]]></category>

		<guid isPermaLink="false">http://jensarps.de/?p=248</guid>
		<description><![CDATA[A couple of days ago, Nicholas Zakas wrote an article about secure client side storage. I think the scenario he mentioned (working from a cyber cafe) is not unsafe by nature, and could be well handled by an application. Nonetheless, client side storage such as localStorage still is subject to DNS spoofing attacks (which is [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago, <a href="http://www.twitter.com/slicknet/" target="_blank">Nicholas Zakas</a> wrote <a href="http://www.nczonline.net/blog/2010/04/13/towards-more-secure-client-side-data-storage/" target="_blank">an article</a> about secure client side storage. I think the scenario he mentioned (working from a cyber cafe) is not unsafe by nature, and could be well handled by an application. Nonetheless, client side storage such as <code>localStorage</code> still is subject to DNS spoofing attacks (which is the main security issue, I think). To handle this, one needs to encrypt the keys and values in the store.</p>
<p>So here you go: <code>dojox.storage.encrypted</code>, a <a href="http://en.wikipedia.org/wiki/Blowfish_%28cipher%29" target="_blank">Blowfish</a> encrypted storage. It sits on top of <code>dojox.storage</code>, and you get all the dojo storage manager goodness, mainly the automatic selection of the best storage provider available. It exposes the complete API that <code>dojox.storage</code> does. If an attacker gains access to the storage area, he can still nuke the storage, but the data found within will be useless.<br />
<span id="more-248"></span><br />
I&#8217;m not completely happy with it&#8217;s architecture, and I didn&#8217;t have the time yet to fully test it with all providers, but it works pretty well with <code>localStorage</code> and the performance is not too bad. If you have a spare minute and are interested, feel free to to test it yourself (ah I know, you don&#8217;t have the time yourself, but I&#8217;d be thankful!)…</p>
<h2>Usage</h2>
<p>Just use it as you would use dojox.storage, except that you need to set your passphrase before you start working with the storage:</p>
<pre>dojo.require("dojox.storage.encrypted");
var sto = dojox.storage.encrypted;
sto.setPassphrase("my super secret passphrase");
sto.put('key','value');
var value = sto.get('key');</pre>
<p>It uses/requires <code>dojox.encoding.crypto.Blowfish</code> but that&#8217;s been in dojo for ages, so no worries.</p>
<p>To use it, just put the <code>encrypted.js</code> file in your dojox/storage directory.</p>
<h2>Files</h2>
<p><a href="http://jensarps.de/tests/dojo_tests/dojo-release-1.4.0-src/dojox/storage/encrypted.js" target="_blank">the code (encrypted.js)</a><br />
<a href="http://jensarps.de/tests/dojo_tests/dojo-release-1.4.0-src/dojox/storage/tests/test_enc_storage.html">the test page</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jensarps.de/2010/04/15/encrypted-client-side-storage-with-dojo/feed/</wfw:commentRss>
		<slash:comments>0</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>persistent local storage with dojo</title>
		<link>http://jensarps.de/2010/01/04/persistent-local-storage-with-dojo/</link>
		<comments>http://jensarps.de/2010/01/04/persistent-local-storage-with-dojo/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 21:46:40 +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[Storage]]></category>
		<category><![CDATA[client-side storage]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[dojox]]></category>
		<category><![CDATA[js]]></category>
		<category><![CDATA[local]]></category>
		<category><![CDATA[localStorage]]></category>
		<category><![CDATA[persistence]]></category>

		<guid isPermaLink="false">http://jensarps.de/?p=140</guid>
		<description><![CDATA[Last year, Brian Leroux&#8216;s Lawnchair caught my interest – it is an easy and fast way to access local persistant local storage. Uh, persistant local storage? Ahm, yeah, in case you don&#8217;t know, that means a client-side storage, be it in a browser, or in an Air app. It&#8217;s not very popular, but that concept [...]]]></description>
			<content:encoded><![CDATA[<p>Last year, <a href="http://twitter.com/brianleroux" target="_blank">Brian Leroux</a>&#8216;s <a href="http://brianleroux.github.com/lawnchair/" target="_blank">Lawnchair</a> caught my interest – it is an easy and fast way to access local persistant local storage.</p>
<p><strong>Uh, persistant local storage?</strong></p>
<p>Ahm, yeah, in case you don&#8217;t know, that means a client-side storage, be it in a browser, or in an Air app. It&#8217;s not very popular, but that concept has been around for a long time. So why local? There are two major reasons for it: First, we need this for apps and tools that work offline – and apps and tools that work online but need an offline backup and sync later. <a href="http://twitter.com/kriszyp" target="_blank">Kris Zyp</a> wrote a <a href="http://www.sitepen.com/blog/2008/09/23/effortless-offline-with-offlinerest/" target="_blank">post about the JsonRestStore and OfflineRest</a> back in 2008, he goes a little into detail there. Secondly, we need it for apps and tools that rely on persistence for other reasons, no matter if online or offline – like it happended to me. When I ran into Lawnchair, I had the idea to build a tool that was monitoring CSS selector usage on websites and apps (you know how you sometimes lose control about CSS selectors in webapps…). To achieve this, I needed to store data locally, and persistent.<br />
<span id="more-140"></span><br />
<strong>What dojo offers<br />
</strong></p>
<p>As I was using dojo&#8217;s excellent CSSRuleStore to find out what selectors where used, I wanted to switch from Lawnchair to a dojo solution. Dojo has dojox.storage (the above mentioned OfflineRest uses dojox.storage), which provides wrappers for the following backends: Gears, Flash, globalStorage and Adobe Air (globalStorage is Firefox&#8217;s early implementation of the early WhatWG storage draft). Hm. Not that much. Gears and Flash are 3rd party solutions, globalStorage is a Firefox-only-thingy (and old, though it&#8217;s still supported in current Versions) and Air is non-browser. That means: No IE without plugins. No Safari when Flash is not available.</p>
<p><strong>What dojo does not offer (but should)</strong></p>
<p>Guaranteed, reliable support for *any* browser. Without depending on 3rd party plugins. As of now, to have this, you&#8217;d need to use Lawnchair or <a href="http://github.com/lloyd/persist-js" target="_blank">persist,js</a>, which is pretty complete. Speaking of it, let me quote from it&#8217;s readme (it&#8217;s a good read on local persistent storage in general):</p>
<blockquote><p>The most notable attempt at addressing this problem [storing client-side persistent data] is probably Dojo Storage.  Unfortunately, Dojo Storage does not support Internet Explorer without Flash, and it does not support Safari or other WebKit-based<br />
browsers at all (at least, not without Flash).  Also, Dojo Storage is<br />
not standalone; it requires a several other Dojo components in order to<br />
operate.</p></blockquote>
<p>Whoa, even other JS tools mention the lack of support in dojo…</p>
<p><strong>New providers</strong></p>
<p>Attached to this post are wrappers for the following mechanisms:</p>
<ul>
<li>localStorage: this is the current spec, and it is supported by IE8, Firefox 3, Safari 4, Opera 10, Chrome 5.</li>
<li>userData behavior: this is Microsofts client-side storage for IE &lt; 8.</li>
<li>cookie: worst case fallback solution…</li>
</ul>
<p>With these three, there&#8217;s an almost 100% coverage of browser situations (see storage overview). There are still no-go scenarios, of course, such as IE7 with userData disabled and cookies disabled, but there&#8217;s no way to address them all.</p>
<p><strong>Notes on BehaviorStorageProvider</strong></p>
<p>This one uses the <a href="http://msdn.microsoft.com/en-us/library/ms531424%28VS.85%29.aspx" target="_blank">userData behavior</a>, present in IE from version 5 up. It works with DOM elements that have a special behavior assigned. You can then use  set/get/removeAttribute() methods to store data. It has some limitations though – of course…</p>
<ul>
<li>Maximum size varies from 64k to 1M – depending on the zone the site is in. So, you can&#8217;t rely on more than 64k.</li>
<li>UserData can be switched off in security settings.</li>
<li>I couldn&#8217;t find a way to read out stored keys if you don&#8217;t know them. Maybe this is some security thingy or just a bug – but it means that some methods in the provider don&#8217;t work. (that is, getNamespaces, getKeys and clear). To work around this, we have to take some of the precious storage space and store the keys and namespaces seperately.</li>
</ul>
<p><strong>Usage</strong></p>
<p>Copy the the providers into your <code>/dojox/storage</code> directory and add the needed <code>dojo.require</code>s to <code>/dojox/storage/_common.js</code>:</p>
<pre>dojo.require("dojox.storage.LocalStorageProvider");
dojo.require("dojox.storage.GearsStorageProvider");
dojo.require("dojox.storage.BehaviorStorageProvider");
//&gt;&gt;excludeStart("offlineProfileExclude", kwArgs.dojoxStorageBuildOption == "offline");
dojo.require("dojox.storage.WhatWGStorageProvider");
dojo.require("dojox.storage.FlashStorageProvider");
//&gt;&gt;excludeEnd("offlineProfileExclude");
dojo.require("dojox.storage.CookieStorageProvider");</pre>
<p><strong>What&#8217;s left?</strong></p>
<p>Well, it would be pretty cool to have a dojo.data API compliant store that uses dojox.storage as backend, so that you could write your apps using dojo.data calls and have a reliable offline backup. I started working on that, but then came Christmas, parents, vacation, new year and a lot of beer. But hey, it&#8217;s 2010 now, and there&#8217;s plenty of time left!</p>
<p><strong>Storage Overview</strong><br />
<!-- .overviewTable td {font-size: 10px; padding:2px 5px;} --></p>
<table class="overviewTable" border="0">
<tbody>
<tr>
<td>IE 6 / IE7</td>
<td>BehaviorStorageProvider<br />
FlashStorageProvider<br />
GearsStorageProvider<br />
CookieStorageProvider</td>
</tr>
<tr>
<td>IE 8</td>
<td>LocalStorageProvider<br />
GearsStorageProvider<br />
FlashStorageProvider<br />
CookieStorageProvider</td>
</tr>
<tr>
<td>Safari 3</td>
<td>FlashStorageProvider<br />
CookieStorageProvider</td>
</tr>
<tr>
<td>Safari 4</td>
<td>LocalStorageProvider<br />
FlashStorageProvider<br />
CookieStorageProvider</td>
</tr>
<tr>
<td>Chrome 5</td>
<td>LocalStorageProvider<br />
FlashStorageProvider<br />
CookieStorageProvider</td>
</tr>
<tr>
<td>Firefox 2</td>
<td>WhatWGStorageProvider (= globalStorage)<br />
GearsStorageProvider<br />
FlashStorageProvider<br />
CookieStorageProvider</td>
</tr>
<tr>
<td>Firefox 3</td>
<td>LocalStorageProvider<br />
GearsStorageProvider<br />
FlashStorageProvider<br />
CookieStorageProvider</td>
</tr>
<tr>
<td>Opera 10</td>
<td>LocalStorageProvider<br />
FlashStorageProvider (?)<br />
CookieStorageProvider</td>
</tr>
</tbody>
</table>
<p><strong>Files</strong></p>
<p><a href="http://jensarps.de/tests/dojo_tests/dojo-release-1.4.0-src/dojox/storage/LocalStorageProvider.js" target="_blank">LocalStorageProvider.js</a><br />
<a href="http://jensarps.de/tests/dojo_tests/dojo-release-1.4.0-src/dojox/storage/BehaviorStorageProvider.js" target="_blank">BehaviorStorageProvider.js</a><br />
<a href="http://jensarps.de/tests/dojo_tests/dojo-release-1.4.0-src/dojox/storage/CookieStorageProvider.js" target="_blank">CookieStorageProvider.js</a><br />
<a href="http://jensarps.de/tests/dojo_tests/dojo-release-1.4.0-src/dojox/storage/tests/test_storage.html" target="_blank">Storage Test Page</a></p>
]]></content:encoded>
			<wfw:commentRss>http://jensarps.de/2010/01/04/persistent-local-storage-with-dojo/feed/</wfw:commentRss>
		<slash:comments>8</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>
	</channel>
</rss>

