<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Thijs van As (@tvanas)

  var _gaq = _gaq || [];
  _gaq.push([‘_setAccount’, ‘UA-22486688-1’]);
  _gaq.push([‘_trackPageview’]);

  (function() {
    var ga = document.createElement(‘script’); ga.type = ‘text/javascript’; ga.async = true;
    ga.src = (‘https:’ == document.location.protocol ? ‘https://ssl’ : ‘http://www’) + ‘.google-analytics.com/ga.js’;
    var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(ga, s);
  })();</description><title>blog.pretopia.net</title><generator>Tumblr (3.0; @tvanas)</generator><link>http://blog.pretopia.net/</link><item><title>SketchIt - A mobile HTML5 doodling app</title><description>&lt;p&gt;Some time ago, while using the &lt;a href="http://itunes.apple.com/us/app/adobe-ideas/id364617858"&gt;Adobe Ideas&lt;/a&gt; drawing/sketching app on my iPad, an idea sprung to mind to create a mobile web-based (HTML5) sketching app. The advantage of having a web-based app is mainly that is platform-independent. That, and the fact that I had a straightforward implementation in mind, made me decide to create one: &lt;a href="http://m.pretopia.net/sketchit"&gt;SketchIt&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Just another little fun project &lt;a href="http://m.pretopia.net/brickit"&gt;after&lt;/a&gt; &lt;a href="http://m.pretopia.net/floodit"&gt;creating&lt;/a&gt; &lt;a href="http://m.pretopia.net/sweepit"&gt;some&lt;/a&gt; HTML5&amp;#160;&lt;a href="http://m.pretopia.net/bounceit"&gt;games&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_lk01chAZo51qer54e.png"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Functional Design&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The core functionality of SketchIt had to be really straight-forward: allow the user to create some sketches, and save them as an image to his device. I defined the following core features of the app:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt; Doodle using touch or mouse gestures (depending on the device) &lt;/li&gt;
&lt;li&gt; Pick a color to doodle with &lt;/li&gt;
&lt;li&gt; Choose the line-width of the pencil&lt;/li&gt;
&lt;li&gt; Save the sketch as an image &lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Starting with these features, SketchIt provides just enough functionality to be useful for people. And I wanted to keep the interface really minimalistic. There were some constraints that I laid down for myself:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt; The app should work on as many devices as possible &lt;/li&gt;
&lt;li&gt; The application should exclusively run on the client (in the browser), no server-side activity other than downloading the application are allowed (so that the application can be ran offline) &lt;/li&gt;
&lt;li&gt; The codebase should be clean&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;&lt;strong&gt;Implementation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As I wanted SketchIt to be web-based and running on the client in the browser, choosing JavaScript + HTML5 for the implementation was the obvious thing to do. These technologies are supported by all major web &lt;/p&gt;
&lt;p&gt;browsers, both on desktop computers as well as mobile devices. Not all browsers have the same grade of support, but that will fade over time (that’s an infrastructural issue).&lt;/p&gt;
&lt;p&gt;I broke down the application logic of SketchIt into 2 modules:&lt;/p&gt;
&lt;ol&gt;&lt;li&gt; A drawing surface that allows a user to doodle on, and save its creation (&lt;a href="http://code.google.com/p/pretopia/source/browse/trunk/js/Sketchpad"&gt;Sketchpad.js&lt;/a&gt;) &lt;/li&gt;
&lt;li&gt; A color picker that allows the user to pick a color that he wants to use (&lt;a href="http://code.google.com/p/pretopia/source/browse/trunk/js/ColorPicker"&gt;ColorPicker.js&lt;/a&gt;) &lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Those 2 modules, wrapped inside a &lt;a href="http://jquerymobile.com/"&gt;jQuery Mobile&lt;/a&gt; interface would become the initial version of the application. There are some other nice mobile UI frameworks, but I really like the simplicity and quality of jQuery Mobile.&lt;/p&gt;
&lt;p&gt;Both &lt;a href="http://code.google.com/p/pretopia/source/browse/trunk/js/Sketchpad"&gt;Sketchpad.js&lt;/a&gt; and &lt;a href="http://code.google.com/p/pretopia/source/browse/trunk/js/ColorPicker"&gt;ColorPicker.js&lt;/a&gt; are open source modules, and can be retrieved from &lt;a href="http://pretopia.googlecode.com"&gt;Google Code&lt;/a&gt;. The source code is annotated with &lt;a href="http://en.wikipedia.org/wiki/JSDoc"&gt;JSDoc&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sketchpad.js&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Sketchpad.js is the module that implements the sketching surface. It is mostly fluff around the HTML5&amp;#160;&lt;a href="https://developer.mozilla.org/en/HTML/Canvas"&gt;Canvas API&lt;/a&gt;. With this module you can instantiate one or more sketching surfaces with a given width and height. Instance methods are provided to set the sketching color, line width, and to export the sketch to an image.&lt;/p&gt;
&lt;p&gt;To export an image, the &lt;em&gt;&lt;a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-canvas-todataurl"&gt;toDataURL&lt;/a&gt;&lt;/em&gt; method of the Canvas object is used. This returns a Base64 image representation of the Canvas, in a given (and supported) file format. Unfortunately, this method is not yet implemented in Android (&amp;lt;= 2.3), so saving a sketch as an image is not yet supported on Android devices. I read somewhere that Android Honeycomb (3.0) for tablets already implements the &lt;em&gt;toDataURL&lt;/em&gt; method on the Canvas.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://jsfiddle.net/tvanas/9HPq8/"&gt;Example of Sketchpad.js usage (jsfiddle.net)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ColorPicker.js&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;ColorPicker.js instantiates a color picker, defined by a number of optional parameters: width, height, lightness and callback functions for choosing a color or mouse-over’ing a color.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://jsfiddle.net/tvanas/RcAwx/"&gt;Example of ColorPicker.js usage (jsfiddle.net)&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="http://media.tumblr.com/tumblr_lk01dnt6HX1qer54e.png"/&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Extra features&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This basic implementation of SketchIt leaves room for many features on different areas:&lt;/p&gt;
&lt;ul&gt;&lt;li&gt; A local library of saved sketches (and resume editing) &lt;/li&gt;
&lt;li&gt; Post sketches to social networks &lt;/li&gt;
&lt;li&gt; Share/collaborate on sketches with friends &lt;/li&gt;
&lt;li&gt; Different pencil types &lt;/li&gt;
&lt;li&gt; &amp;#8230; &lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Anyway, at least for now these are beyond the scope of this little project. You can try SketchIt on &lt;a href="http://m.pretopia.net/sketchit"&gt;http://m.pretopia.net/sketchit&lt;/a&gt;&lt;/p&gt;</description><link>http://blog.pretopia.net/post/4803140039</link><guid>http://blog.pretopia.net/post/4803140039</guid><pubDate>Thu, 21 Apr 2011 13:07:25 +0200</pubDate><category>javascript</category><category>html5</category><category>sketchit</category><category>mobile</category><category>webapp</category></item><item><title>Classes with private properties in JavaScript</title><description>&lt;p&gt;&lt;span&gt;JavaScript, a &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Prototype-based_programming"&gt;prototyped language&lt;/a&gt;&lt;/span&gt;&lt;span&gt;, offers multiple ways to ‘emulate’ a classical class model. The best way to add methods to a constructor (class) function, is by extending its &lt;em&gt;prototype&lt;/em&gt; object. However, all these properties are public, and can thus be accessed by anybody who has access to the object. It is somewhat less obvious to create a class that has private properties (methods or variables that are only accessible from within the class). I created a boilerplate class for the situations when I need this:&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/p&gt;
&lt;pre class="prettyprint"&gt;MyClass = function() {
    // 'that' is used to access 'this' of MyClass from within private
    // methods, because 'this' is then bound to the global object
    var that = this;

    // private variables
    var privateVariable = 'I am private';

    // public variables
    this.publicVariable = 'I am public';


    // private methods
    var privateMethod = function() {
        console.log('Private method entered');
        console.log(privateVariable);
        console.log(that.publicVariable); // note the use of 'that'
    };

    // public methods
    this.publicMethod = function() {
        console.log('Public method entered');
        console.log(privateVariable);
        console.log(this.publicVariable); // note the use of 'this'

        privateMethod();
    };
};&lt;/pre&gt;
&lt;p&gt;&lt;span&gt;When you create an instance of &lt;em&gt;MyClass&lt;/em&gt;, only &lt;em&gt;publicVariable&lt;/em&gt; and &lt;em&gt;publicMethod&lt;/em&gt; will be public properties. This can be confirmed by looping over all properties of an instance:&lt;/span&gt;&lt;/p&gt;
&lt;pre class="prettyprint"&gt;MyInstance = new MyClass();

var property;
// iterate over all public properties (variables and methods)
for (property in MyInstance) {
    // print the property's name + type
    console.log(property + ' : ' + typeof MyInstance[property]);
}

/* console output:   
    publicVariable : string
    publicMethod : function
 */&lt;/pre&gt;
&lt;p&gt;&lt;span&gt;Note that from within a private method, public properties are accessed through &lt;em&gt;that&lt;/em&gt;. This is because of an unfortunate choice in the design of JavaScript which states that within all functions called directly, &lt;em&gt;this&lt;/em&gt; is bound to the global object. This doesn’t happen when a function is called as a method (a property from another object), constructor (a class), or using the standard &lt;em&gt;apply&lt;/em&gt; method of a function.&lt;/span&gt;&lt;br/&gt;&lt;span&gt;&lt;/span&gt;&lt;br/&gt;&lt;span&gt;A disadvantage of above boilerplate code is that for each newly created instance of the class, the public and private properties are copied into memory for the new instance. This doesn’t happen when using prototypal inheritance - but then we are not able to make private properties easily.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;span&gt;There is a workaround for this, by having the constructor function (&lt;em&gt;MyClass&lt;/em&gt;) return an object containing all the public properties. Private properties are accessible through the created closures. This also allows more advanced mechanisms such as super methods. To read more about inheritance in JavaScript, I suggest reading Chapter 5 of &lt;a target="_blank" href="http://oreilly.com/catalog/9780596517748"&gt;JavaScript: The Good Parts&lt;/a&gt; by Douglas Crockford.&lt;/span&gt;&lt;/p&gt;</description><link>http://blog.pretopia.net/post/4388551903</link><guid>http://blog.pretopia.net/post/4388551903</guid><pubDate>Wed, 06 Apr 2011 13:26:00 +0200</pubDate><category>javascript</category><category>class</category><category>boilerplate</category></item><item><title>f1rst p0st</title><description>&lt;p&gt;&lt;pre class="prettyprint"&gt;String.prototype.toString = function() {
    return 'Welcome ' + this;
};

console.log('to my blog!'.toString());
&lt;/pre&gt;&lt;/p&gt;</description><link>http://blog.pretopia.net/post/4049294935</link><guid>http://blog.pretopia.net/post/4049294935</guid><pubDate>Wed, 23 Mar 2011 21:28:00 +0100</pubDate><category>javascript</category></item></channel></rss>

