Author: esimov

  • A new look for my site

    A new look for my site

    It has been a very long time since my last post, ignoring almost any online social activity and not having too much free time for visual experimentation and self study, but finally find some time to finish the new look of my site. Actually is not only a new look, but a completely reworked and redesigned site from ground up. The previous version was based on WordPress. I liked the flexibility and easy to use feature of WordPress, which in essence could be to reduce to a two-step workflow: search for a theme, install it and you are good to go.

    footer-logo

    BTW this is the logo of my site designed by myself

    I was thinking from time to time, to give some personality to my site, meaning trying to keep myself distant from WP, maybe using only the backend part for content upload, post editing and any other content management activity.

    Searching for WP alternatives I found roots.io and Octopress the two most suitable alternatives for WordPress. Initially i opted for the first one only for the reason that i can preserve the current content without too much overhead. I found this a good alternative to WordPress for it’s minimal and accessible markup and for the fact it can rewrite the conventional wordpress URL’s, meaning it makes the path names cleaner, but on the other hand i thought it would be more challenging but at the same time more satisfying and rewarding to create my own custom Javascript library for building the site. For this scope i used PHP only for calling some web services, pulling out the content from WordPress database, wrapping as a JSON object, then transmitting over to the front end where with some ajax call i can build the whole page.

    I wanted to build the site using cutting edge modern web technologies for this reason many of the features are not working as expected on older browsers. The whole site has been build up around HTML5, Canvas and CSS3 spiced with a lot of Javascript. This is somehow a mini MVC framework, the model part being responsible for obtaining the JSON data from backend, the controller having as main role to communicate with the view and model (triggering the model and passing the results to the view the latter organizing the received data in a visual manner).

    Many of you may ask why i didn’t make it with more fashinable MV* frameworks like Backbone, Angular.js or Ember.js? My answer is: when i started this project i wasn’t very familiar with these technologies, i only heard about them, and once started i wanted to make it till the end. Maybe sometime i will think differently and will give it a try, but till then i’m pretty satisfied with the result. Of course there are some bugs which i have to address, like responsiveness, awkward page rendering when selecting a specific tag and associating the posts with these tags. Feel free to give any feedback be as good or bad.

    As you probably realized sometimes you are redirected to my other domain name (esimov.com). I’m not completely decided either to keep esimov.com or esimov.com as my main blogging platform, the first one having a more ‘business-ish’ feeling, on the other hand the last one suiting more for a personal blogging scope. I will decide later.

  • First impressions about Typescript, the new authoring tool from Microsoft

    First impressions about Typescript, the new authoring tool from Microsoft

    In this blog post i want to share my first impression about Typescript, the new programming language from Microsoft.

    The background

    Coming from Flash/Actionscript, and having done almost all my experiments in AS3, facing the loose typing nature of JavaScript was a bit daunting for me. Over the time I got familiarized myself with all the mystical parts of JavaScript, with it’s prototyping nature, with the strange fact that JS hasn’t a type interface, and you can declare a variable without declaring the type of that variable. In fact all the declared variables can take any type of value, and assigning a different type of value to the variable wont make the compiler to trow an exception (only if we force to do so). This proved to be the weakness of JavaScript and the most acclaimed factor which stand in front of developing highly scalable applications.

    wo

    The first question that may arise is what is Typescript? The answer is: Typescript is a superset of JavaScript which compiles to JavaScript at runtime, all while maintaining your existing code and using the existing libraries. And why Typescript? The short answer: because Typescript is awesome. The long answer will follow.

    One of the main benefits using typescript is code validation at compile time and compilation to native Javascript file at the runtime.

    This differentiate Typescript from other competitors, nominating mainly two contenders: DART ( which is a very heavy assault initiative from Google meant to eliminate all the weakness and flawless of JavaScript, introducing a completely new language, having as target not less than being the web native language), and Coffescript which is a syntactic sugar of Javascript, which compile to Javascript. Typescript somehow break in the middle of the two, because DART is a fundamentally and completely different language than Javascript, with strong typing, traditional class  structures, modules etc. On the other hand Coffescript embraced the new ECMA-6 (Harmony) features like classes,  import modules (only when required) etc, but it’s weakness is the same as of Javascript: the absence of type annotation and a very limited or almost completely absent feedback response and lack of debugging tools. This got managed by TypeScript.

    Getting started

    I won’t go into much details how to get started with Typescript. You can download the source from the official page of the language: http://www.typescriptlang.org/, then if you are a Windows 8 or at least Windows 7 user and preferably (not my case) .NET developer you’ll have a very easy time to get started with coding. There is a plugin for Microsoft Visual Studio 2012 downloadable from the same source. I’ve tried installing on Vista, after too many trials and errors finally got working, unfortunately without code highlighting and code completion. So in the end decided to write a plugin for Sublime Text 2 editor (my editor of choice). Here is the Stackoverflow thread explaining the process to automate the code transcription from TS to JS. The only weakness is – at the time of this writing –  there isn’t a code completion and live time error checking plugin implemented. A good news is that Webstorm 6.0 is already offering support for Typescript. This article summarize in a few words the capabilities of Typescript in Webstorm perspective: http://joeriks.com/2012/11/20/a-first-look-at-the-typescript-support-in-webstorm-6-eap/.

    There is a live playground available for online experimentation with a few samples at disposal: http://www.typescriptlang.org/Playground/

    Things I liked in Typescript

    Static value declaration

    As i said earlier declaring a variable in JS is simply assigning a value to it, the rest of internal operations are up to the JIT compiler. This is the root of many unwanted errors which happens when we unconsciously  messing up different types of values with the originally declared variable. Typescript helps to resolve this issue by allowing to statically declare the variable type. If no type has been specified the type any is inferred.

    var num: number = 20;

    which compiles to JS as:

    var num = 20;

    The only chance in JS to get feedback about the errors is to use test cases by covering all the possible errors you might expect:

    if typeof num !== "number" {
       throw new Error(num, " is not a number");
    }

    On the other hand TS informs instantly about the errors as you type.

    Arrow function expression

    Another great feature is the addition of the so called arrow function expression. These are useful on situations when we need to preserve the scope of the outer execution context. A common practice widespread among JS developers is to bind this to a variable self which we place outside the inner function.
    A function expression using the function keyword introduces a new dynamically bound this, whereas an arrow function expression preserves the this of its enclosing context. These are particularly useful in situation when we need to write callbacks like in the following example.

    function sayHello(message:string, callback:()=>any) {
    	alert(message);
    	setTimeout(callback, 2000);
    }
    
    sayHello("Welcome visitor", function() {
    	alert("thank you");	
    });

    Classes, modules, interfaces

    Probably the greatest addition of Typescript is the possibility to work with classes, modules and interfaces.

    Interfaces

    Let’s start with interfaces. In Typescript interfaces have only a contextual scope, they haven’t any runtime representations of the compiled code. Their role is only to create a syntactic structure, a shell which is particular useful for object properties validation, in other words for documenting and validating the required shape of properties, objects passed as parameters, and objects returned from functions. In TS, interfaces are actually object types. While in Actionscript you have to have a class to implement the interface, in TS a simple object can implement it. Here is how you declare interfaces in TS:

    interface Person {
    	name: string;
    	age?: number;
    }
    
    function print(person: Person) {
    	if (person.age) {
    		alert("Welcome " + person.name + " you are " + person.age + " age");	
    	} else {
    		alert("Welcome " + person.name);
    	}
    	
    }
    
    print({name: "George", age:22});

    Furthermore interfaces have function overloading features which means that you can pass two identical functions on the same interface and let the user decide at the implementation phase which function wants to implement:

    interface Overload {
    	foo(s:string): string;
    	foo(n:number): number;
    }
    
    function process(o:Overload) {
    	o.foo("string");
    	o.foo(23);
    }

    Another very interesting particularity of Typescript is that above the support offered for overloading functions, this is applicable for constructors too. This way we can redefine multiple constructor only by changing the way we implement it.

    var canvas = document.createElement("canvas");
    document.body.appendChild(canvas);
    var ctx = canvas.getContext('2d');
    
    interface ICircle {
    	x: number;
    	y: number;
    	radius: number;
    	color?: string;
    }
    
    class Circle {
    	public x: number;
    	public y: number;
    	public radius: number;
    	public color: string = "#000";
    	
    	constructor () {}		
    	
    	constructor () { 
    		this.x = Math.random() * canvas.width;
    		this.y = Math.random() * canvas.height;
    		this.radius = 40;
    	}		 		
    	
    	constructor (circle: ICircle = {x:Math.random() * canvas.width, y:Math.random() * canvas.height, radius:20}) {
    		this.x = circle.x;
    		this.y = circle.y;
    		this.radius = circle.radius;
    	}
    }
    
    var c = new Circle();
    ctx.fillStyle = c.color;
    ctx.beginPath();
    ctx.arc(c.x, c.y, c.radius, 0, Math.PI * 2, true);
    ctx.closePath();
    ctx.fill();

    Classes

    The most advanced feature of Typescript (from my point of view) is the addition of the more traditional class structure with all the hotness like inheritance, polymorphism, constructor functions etc. In Typescript we are declaring the class in the following way:

    class Point {
    	x: number;
    	y: number;
    	
    	constructor(x: number, y: number) {
    		this.x = x;
    		this.y = y;
    	}
    	
    	sqrt() {
    
    		return Math.sqrt(this.x * this.x + this.y * this.y) 
    	}
    	
    	print(callback?) {		 
    		setTimeout(callback, 3000);
    		
    	}
    }
    
    class Point3D extends Point {	
    	z : number;
    	
    	constructor ( x:number,  y: number, z: number) {
    		super(x, y);
    	}
    	
    	sqrt() {
    		var d = super.sqrt();
    		return Math.sqrt(d * d + this.z * this.z);
    	}
    }
    
    var p = new Point(20, 20);
    p.print(() => { alert (p.sqrt()); });

    … which in Javascript compiles to:

    var __extends = this.__extends || function (d, b) {
        function __() { this.constructor = d; }
        __.prototype = b.prototype;
        d.prototype = new __();
    };
    var Point = (function () {
        function Point(x, y) {
            this.x = x;
            this.y = y;
        }
        Point.prototype.sqrt = function () {
            return Math.sqrt(this.x * this.x + this.y * this.y);
        };
        Point.prototype.print = function (callback) {
            setTimeout(callback, 3000);
        };
        return Point;
    })();
    var Point3D = (function (_super) {
        __extends(Point3D, _super);
        function Point3D(x, y, z) {
            _super.call(this, x, y);
        }
        Point3D.prototype.sqrt = function () {
            var d = _super.prototype.sqrt.call(this);
            return Math.sqrt(d * d + this.z * this.z);
        };
        return Point3D;
    })(Point);
    var p = new Point(20, 20);
    p.print(function () {
        alert(p.sqrt());
    });

    Analyzing the code we observe that it share almost the same principle like any other static type language. We declare the class, it’s variables and functions, which can be static, private and public. By default the declared functions and variables are public, but we can make them private or static, by placing the private or static keyword in front of them. TS supports the constructor functions, which we declare in the following format:

    var1:type;
    var2:type;
    constructor (var1:type, var2:type...varn:type) {
           this.var1 = var1;
           this.var2 = var2;
           .
           .
           .
           this.varn = varn;
    }

    It’s possible to declare the constructor arguments type as public and then we can eliminate to assign the constructor function parameters to constructor’s variables.

    Typescript facilitates the working with classes by introducing another two great features commonly used in OOP, namely the inheritance and class extension. I rewrite the above code sample to show with a few simple changes we can extend the existing code to embrace some advanced polymorphism methods:

    class Point {
    	x: number;
    	y: number;
    	
    	constructor(x: number, y: number) {
    		this.x = x;
    		this.y = y;
    	}
    	
    	sqrt(x:number, y:number):number {		
    		return Math.sqrt(x * x + y * y); 
    	}
    	
    	dist(p:Point):number {
    		var dx: number = this.x - p.x;
    		var dy: number = this.y - p.y;
    		
    		return this.sqrt(dx, dy);
    	}
    	
    	print(callback?) {		 
    		setTimeout(callback, 3000);
    		
    	}
    }
    
    class Point3D extends Point {	
    	z : number;
    	
    	constructor ( x:number,  y: number, z: number) {
    		super(x, y);
    	}
    	
    	sqrt3(z:number) {
    		var s = super.sqrt(this.x, this.y);
    		return Math.sqrt(s * s + this.z * this.z);		
    	}	
    		
    	dist(p:Point3D):number {
    		var d = super.dist(p);		
    		var dz: number = this.z - p.z;										
    		return this.sqrt3(dz);
    	}
    }
    
    var p = new Point3D(19, 20, 20);
    p.print(() => { alert (p.dist(new Point3D(2,2,20))); });

    Modules

    The last topic i want to discuss is about modules. Typescript module implementation is closely aligned with ECMAScript 6 proposal and supports code generation, targeting AMD and CommonJS modules. There are two types of source files Typescript is supporting: implementation source files and declaration source files. The difference between the two is that the first one contains statements and declarations, on the other hand the second contains only declarations. These can be used to declare static type information associated with existing javascript code. By default, a JavaScript output file is generated for each implementation source file in a compilation, but no output is generated from declaration source files.

    The scope of modules is to maintain a clean structure in our code and to prevent the global namespace pollution. If suppose i have to create a global function Main the way i do in Javascript without to override the function at a later stage is to assure that the Main function get initialized only once. This is a common technique widespread among JS developers. In TS we do in the following way:

    module net.esimov.core {
    	export class Main {
    		public name: string;
    		public interest: string;
    		
    		constructor() {
    			this.name = "esimov";
    			this.interest = "web technology";
    		}
    		
    		puts() {
    			console.log(this.name, " like", this.interest);
    		}
    	}
    }

    Importing the declaration files is simple as placing the following statement on the top of our code: /// <reference path="...";/>. Then with a simple import statement we’ll import the modules needed at one specific moment.

    import Core = net.esimov.core;
    var c = new Core.Main();

    Final thoughts

    Typescript is a language that is definitely worth to try it. Unfortunately it has one main disadvantage, namely to benefit from it’s strong typing features we need to declare and construct declaration files for libraries we want to include in our projects. There are predefined declaration files for jQuery, node.js and d3.js, just to mention a few, but hopefully as the community will grow this list will get bigger and bigger.

    Another weak point is the lack of support from IDE’s other than MS Visual Studio, but fortunately Webstorm 6 has in perspective to include full type support for Typescript too.

    What’s next?

    With this introduction done, my intention is to put into real usage all the information and learning accumulated and trying to create some canvas experiments but this time using Typescript. So check out soon.

    Other resources to check

  • Worley noise cellular texturing

    Worley noise cellular texturing

    There are some quite well known procedural content generation algorithms out there: Voronoi, Perlin noise, simplex noise, L-systems, reaction-diffusion systems like Turing pattern, which I’ve presented in my earlier blog post.The specifics of these algorithms are that either generates a large amount of content for a small investment of input data, or one that adds structure to random noise.

    Generally speaking they are categorized by what they generate (map vs sequence generation) and also by the mindset behind their use. This time we’ll discuss the Worley noise pattern, which can be included into the generative map creation category. This means we need a grid system for the representation and visualization of points scattered randomly in 3D or 2D space.

    In it’s visual appearance is almost like a Voronoi diagram, only it’s not as famous like it’s counterpart. Even Wikipedia discuss it very briefly. Worley noise in it’s essence is a noise generation pattern, which means by multiplication/addition of different octaves of the same noise pattern we can produce a variety and completely different outputs. This is the fun part of working with generative patterns.

    Now, after we gain a little theoretical introduction let’s get started with the basics, and try to dissect how can we reproduce those organic shapes and textures found in every living being in nature. (In parentheses being said, in many graphic simulations and parametric applications dealing with textural visualizations this shapes can be found). As a last side note before we go into the technical details, my intention was to implement the algorithm in Javascript mainly because i wished to be accessible by as many users as possible, and at the same time i was curious how far can i stress the limit of the modern web browsers. This is an experiment based 100% on CPU, so WebGL was out of scope, only because i was not playing with this technology until now, but i considering to implement on GPU in a shorter or longer future.

    Feature points

    The basic idea is to spread randomly varying number in space and calculate the distance to these point from every possible location of space. As a complement, Worley suggest to assign to each feature point an unique ID number. This number can be used to generate surface with colored areas or crocodile like textures, cityblock diagrams etc. depending on the algorithm, or coloring function implemented.

    The noise function simply computes a single value for every location in space. We can then use that value in literally thousands of interesting ways, such as perturbing an existing pattern spatially, mapping directly to a density or color or adding multiple scale of noise to make a fractal combination. While there are infinite number of possible functions which provide an infinite number of outputs, noise’s random behavior gives a much more interesting appearance  then simple gradients for example.

    By spreading  feature points randomly through the 3D space we build a scalar function based on the distribution of the points near the sample location. Because an image worth a thousand of words the picture below describes a space with feature points scattered across the plane.

    For a position on the plane (marked with x in the picture above) there is always a closest feature point, a second closest, a third and so on. The algorithm is searching for such feature points, return  their relative distance to the target point, their position and their unique ID number.

    Another key component of the algorithm is the demarcation line which is supposed to be integer in every plane. Each squares (cubes in 3D) are positioned so that their identification numbers can be expressed with some integer values. Let’s suppose we want to obtain the distance to the two feature point closest to the (3.6, 5.7) we start by looking at the integer squares (3, 5) and compare it’s feature points to each other.  It’s obvius that the two feature points in that square aren’t the two closest. One of them is, but the second closest belongs to the integer square (3, 6).

    It’s possible that some feature points are not inside the square where the target point is placed, in this case we extend our analysis to the adjacent squares. We could just test all 26 immediate neighboring cubes, but by checking the closest distance we’ve computed so far (our tentative n-th closest feature distance) we can throw out whole rows of cubes at once by deciding when no point in the neighbor cube could possibly contribute to our list. We don’t know yet what points are in the adjacent cubes marked by “?,” but if we examine a circle of radius F1, we can see that it’s possible that the cubes labeled A, B, and C might contribute a closer feature point, so we have to check them. As resulted from the image below, it’s mostly possible to check 6 facing neighbors of center cube (these are the closest and most likely to have a close feature point), 12 edge cube and 8 corner cubes in 3D. We could start testing more distant cubes, but it’s much more convenient to just use a high enough density to ensure that the central 27 are sufficient.

    The code implementation in Javascript

    For the code implementation i used html5 canvas. I’ve commented the source code, so won’t be too hard to follow for anyone. Because (as i mentioned in the beginning) my WebGL knowledge is quite minimal, for this reason i tried to optimize every single bit of code. For this reason my single option was to use web workers by separating the main core from the computationally heavy and intense pixel manipulation logic. There is a well described article on html5rocks.com about the basic principles of web workers, followed by some very neat examples regarding the implementation of them.

    The main advantage of using web workers is that we can eliminate the bottlenecks caused by computational heavy threads running in the background, separating the implementation interface from the computational logic. This way we can reduce the hangouts caused by intensive calculations which may block the UI or script to handle the user interaction.

    Creating a web worker is quite simple. We define the variables we want to pass to workers, and by calling an event listener using postMessage function we transfer the main tread instructions to the worker (which is running in a separate thread). Because web workers run in isolated threads, the code that they execute needs to be contained in a separate file. But before we do that, the first thing we have to do is create a new Worker object in our main page. The constructor takes the name of the worker script:

    In  a separate script we’ll define wich are the variables we want to listen for. This part is responsible for the heavy weight operations. After the pixel calculations has been done, we pass over the results through the postMessage method to the main thread. This part of code looks like the following:

    As a last option i’ve included the DAT.GUI lightweight graphical interface for a user interaction, where we can select different methods, colors etc. and lastly to offer the possibility for rendering with or without web workers.

    Hopefully you find interesting this experiment, and if you have some remarks, comments, then please use the comments field below, or share through twitter.

  • 404 page

    404 page

    During the setup of my site i decided to create a custom 404 page, inspired by this nice canvas experiment made by Hakim. I wanted the bubbles to be bursted by mouse click and to integrate the representative “404” text message, which i wanted to be reactive to the mouse and wave movement.

    So for that reason i’ve rewrite the code pretty much for my own purposes, plus i wanted to integrate some searching functions into my site. For this reason i was looking for a free php searching engine. I found sphider to be well acclaimed among users and developers, so i gave it a try. I managed to setup the search engine almost instantly, however the visual appearance have to be polished a little bit.

    A challenge for me was to integrate the text into the canvas considering I opted for a big bold character. I’ve tried the canvas native fillText function, but the text edges were very jaggy, so i decided to inject SVG code into the canvas, which looked like this:

    
    var data = "data:image/svg+xml," +
        "<svg xmlns='http://www.w3.org/2000/svg' width='400' height='400'>" +
        "<foreignObject width='100%' height='100%'>" +
            "<div xmlns='http://www.w3.org/1999/xhtml' style='font: bold 160px Myriad Pro, Arial, Helvetica, Arial, sans-serif'>" +
                "<span style='color:rgb(32,124,202); opacity: 0.8; -webkit-text-stroke-color: white; -webkit-text-stroke-width: 2px'>" + letters[pos] + "</span>" +
            "</div>" +
        "</foreignObject>" +
    "</svg>";
    

    As a last element I introduced a twitter feeder, which search for every word i define, and used Modernizr for a fallback for browsers which does not support canvas. Sorry IE.

  • Organic Turing Pattern

    Organic Turing Pattern

    In this post i will present an experiment which i did a few months earlier, but because lately i was pretty occupied with various job related activities and with setting up this new blog (transferring the old content to the new host – plus a lot of other stuff), i have no time to publish this new experiment. More about below, but until then a few words about the background, inspiration and some of the key challenge i have faced during this process.

    This is an experiment inspired by a thoroughly explained article about the concept behind what is generally known as Turing Pattern. The Turing Patterns has become a key attraction in fact thanks to Jonathan McCabe popular paper Cyclic Symmetric Mutli-Scale Turing Patterns in which he explains the process behind the image generation algorithm like that one below, about what Frederik Vanhoutte know as W:blut wrote on his post:

    Quote leftIf you’ve seen any real­ity zoo/wild-life pro­gram you’ll rec­og­nize this. Five min­utes into the show you’re con­fronted with a wounded, mag­nif­i­cent ani­mal, held in cap­tiv­ity so its care­tak­ers can nur­ture and feed it. And inevitably, after three com­mer­cial breaks, they release it, teary-eyed, back into the wild. It’s a piv­otal moment that turns their leop­ard into anyone’s/no one’s leop­ard.Quote right

    That’s the feeling which emanates when you first see the organic like, somehow obscure (some may go even further and say: macabre or creepy) thing which is known as a Turing pattern. At first moment when you are confronted with it, some kind of emotion (be that fear or amazement or just a sight) is resurfaced. Definitely won’t let you neutral.

    This is the emotion which pushed me to try to implement it in Actionscript. (In parentheses be said this is not the best platform for computation intensive experiments.) Felix Woitzel did an amazing remake adaptation of the original pattern in WebGL. The performance with a good GPU is quite impressive. Maybe in the future i will try to port in WebGL. Anyway, there is another nice article by Jason Rampe, where he explains the algorithm of the Turing Pattern.

    So let’s face directly and dissect the core components of the algorithm.

    The basic concept

    The process is based on a series of different “turing” scales. Each scale has different values for activator, inhibitor, radius, amount and levels. We use a two dimensional grid which will be the basic container for activator, inhibitor and variations, all of them going to be of typed arrays (in our case Vectors.<>). At the first phase we will initialize the grid with random points between -1 and 1. In the next step we have to determine the shape of the pattern. For this case w’ll use a formula defined by the following method:

    for (var i:int = 0; i < levels; i++)
    {
    	var radius:int = int(Math.pow(base, i));
    	_radii[i] = radius;
    	_stepSizes[i] = Math.log(radius) * stepScale + stepOffset;
    }

    During each step of simulation we have to execute the following methods:

    1. Populate the grid with pixels at random locations. I’m gonna use a linked list to store the pixels properties: positions and colors. We will loop through all the pixels, the number of pixels will be defined by the grid with x height dimension.
    2. Average each grid cell location over a circular radius specified by activator, then store the results in the activator array, multiplied by the weight.
    3. Average each grid cell location over a circular radius specified by inhibitor, then store the results in the activator array, multiplied by the weight.
    4. In the next step we need to calculate the absolute difference between the activator and inhibitor using the the following formula: variations = variations + abs(activator[x,y] - inhibitor[x,y])

    Once we have all the activator, inhibitor and variations values calculated for each grid cells we proceed with updating the grids as follows:

    1. Find which grid cell has the lowest variation value
    2. Once we found the lowest variation value we update the grid by the following formula:
    for (var l:Number = 0; l < levels - 1; l++)
    {
    	var radius:int = int(_radii[l]);
    	fastBlur(activator, inhibitor, _blurBuffer, WIDTH, HEIGHT, radius);
    
    	//calculates the absolute difference between activator and inhibitor
    
    	for (var i:int = 0; i < n; i++)
    	{
    		_variations[i] = activator[i] - inhibitor[i];
    		if (_variations[i] < 0)
    		{
    			_variations[i] = -_variations[i];
    		}
    	}
    
    	//if first level then set some initial values for bestLevel and bestVariation 
    	if (l == 0)
    	{
    		for (i = 0; i < n; i++)
    		{
    			_bestVariation[i] = _variations[i];
    			_bestLevel[i] = l;
    			_directions[i] = activator[i] > inhibitor[i];
    		}
    		activator = _diffRight;
    		inhibitor = _diffLeft;
    	}
    	else
    	{
    		for (i = 0; i < n; i++)
    		{
    			if (_variations[i] < _bestVariation[i])
    			{
    				_bestVariation[i] = _variations[i];
    				_bestLevel[i] = l;
    				_directions[i] = activator[i] > inhibitor[i];
    
    			}
    		}
    		var swap:Vector. = activator;
    		activator = inhibitor;
    		inhibitor = swap;
    	}
    }

    As a last step we should average the gap between the levels by performing a blur technique for a nicer transition. We blur the image sequentially in two steps: first we blur the image vertically then horizontally.

    The challenge

    The most challenging part was related to the unknown factor about how well gonna handle the AVM2 the intensive CPU calculation. Well as you may have guessed not too well, especially with very large bitmap dimension and a lot of pixels needed to be manipulated, plus the extra performance loss “gained” after the bluring operation.

    For some kind of compensation benefits (in terms of performance) i decided to populate the BitmapData with a chained loop using single linked list, plus typed arrays as generally known the Vector.<> arrays are much faster compared to the traditional array stacks. After a not too impressive performance, i wondered how can i even more optimize the code for a better overcome. I decided to give it a try to Alchemy, and Joa Ebert’s TDSI, a Java based Actionscript compiler, as by that time was commonly known a better choice over AVM. Unfortunately i didn’t get too many support and helpful articles about the implementation and as a consequence my attempt was a failure, which resulted in a quite buggy and ugly output. After too many attempts without the wishful result, i gave up.

    That’s all in big steps. You can grab the source code from here. Feel free to post your remarks.

    ps: Be prepared this experiment will burn your CPU. 🙂

  • Sound visualization

    Sound visualization

    Sound visualization is one thing which I was always fascinated by. I stumbled upon this small Javascript experiment and thought would be nice to reproduce it in Actionscript, adding some spicy extra addons like interaction and sounds reacting shapes. So I decided to implement in Actionscript. Actually it went very well, in fact i only retained the core algorithm. The core algorithm consist in just a few lines:

    private function createSurface(a:Number, z:Number):Point3D
    {
    	var r:Number = W/10;
    	var R:Number = W/3;
    	var b:Number = -10*Math.cos(a*5 + T);
    
    	return new Point3D(W/2 + (R*Math.cos(a) + r*Math.sin(z+2*T))/z + Math.cos(a)*b,
    							   H/2 + (R*Math.sin(a))/z + Math.sin(a)*b, Z);
    }
    
    private function drawQuad(a:Number, da:Number, z:Number, dz:Number):void
    {
    	var v:Array = [createSurface(a,z),
    				   createSurface(a+da,z),
    				   createSurface(a+da, z+dz),
    				   createSurface(a, z+dz)];
    
    	_points = new Vector.();
    	_stroke = new GraphicsStroke();
    	_stroke.thickness = 1;
    	_stroke.fill = new GraphicsSolidFill(0x102020,0.95);
    
    	_path = new GraphicsPath(new Vector.(), new Vector.());
    	_path.moveTo(Point3D(v[0]).x, Point3D(v[0]).y);
    
    	for (var i:Number = 0; i < v.length; i++)
    	{
    		_path.lineTo(Point3D(v[i]).x, Point3D(v[i]).y);
    	}
    	_background = new GraphicsSolidFill(0x663399, 0.95);
    }

    First we create some quads using sine and cosine, multiplying by the radius, which is given as a constant. Then we store these quads on an array, setting their positions by calling the GraphicsPath.lineTo method. These positions are updated each time we loop through on an EnterFrame event listener. The fancy things happens here, where we use a fog variable to apply the shading effect by tweaking some depth illusion.

    And this is how it looks after implementation. By the way this is a good example of how robust and fast are <IGraphicsData> and drawGraphicsData functions combined.

    (Move the mouse to change the color shading intensity)

    After implementing the basic structure I thought – following the suggestion of some of the active members of wonderfl community – it would be nice to add some extra feature like sound visualization. Initially this little effect consists of some sinusoidally  curvy movement modifying the color intensity by the mouse position. You can check the effect by clicking on the image above.

    I was pleasantly surprised by the smoothly frame rate of the effect, so as i mentioned earlier the next step was to implement the algorithm for sound visualization. Thanks to makc3d audio API it was an easy job to obtain some samples which later could be used for the funky disco effect following the rhythm of the sound. This simple API connects to BeatPort portal, and by providing an unique key we can select from a various music genres. I opted for dubstep only because the high variation of beats can produce a very differentiated transition (a next step could be to implement an interface for selecting the different music channel).

    This is another variation.

    Playing with the values we can obtain different visualization effects. For example scaling up the bass bit rate we can obtain a nice upscaled flowing ring or reducing the radius value we should obtain a perfect circular cube like in the example below.

    Definitely a GUI interface would have been a better choice for playing with values, but because i put together this experiment in almost two hours i didn’t take the effort to polish the code. Maybe in a future time.

    (Scroll the mouse to jump to the next track)

    You can grab the source code here!