On my search for a JS variant, triggered by NodeJS study

Well while going ahead with my NodeJS study and after looking at the functionality that NodeJS provides, I got a little curious to search for something which might help me develop in JS a little more easily. To my surprise, I found a lot of languages which does that, but I picked 3 as it seemed these are got some history to themselves and backed by big corporate or have a huge community to support.
1. CoffeeScript
2. TypeScript
3. Dart
All these three compile to javascript. While CoffeeScript and TypeScript are nodeJS dependent and installed by “npm”, Dart comes with its own installation and IDE. Personally I liked the first two, at first glance as they are installed with “npm”. But then Dart comes with its own IDE, which is an advantage.
Now a little bit about the face value of the three.
CoffeeScript : A huge community and has proven history of a good player.
TypeScript : Its a microsoft initiative.
Dart : Its a Google initiative.

Looking at all these, I still feel HAXE is the best for JS dev too, may be thats my biased opinion.

Modules in NodeJS

There is this concept of Modules in NodeJS, which is kind of but not exactly the same as Class concept of programming languages like Java and ActionScript. To add a module the syntax is

 
require() // its like import in Java and Actionscript

A detailed implementation of “require()” statement is as below

 
var myModule = require('./myModule.js');

So that’s all to include a module, if the module JS file and your application JS file are in the same folder, for the time being lets do the understanding this way. :)
Now lets see what kind of features or flexibility “modules” gives us as a nodeJS developer.
If we think of modules as the Classes then it will be easier for us to understand.
First of all, everything inside a module is private. Ohh yes, private things in javascript. Thats interesting, is not it!
Lets write a simple class kind of thing in a module, which has a private property and getter-setter method of the same.

stupidVar='Global Scope. Avoid this.';// Never put anything in Global Scope
var nameOfModule='I am private';
exports.getName=function(){
	return nameOfModule;
}
exports.setName=function(newName){
	nameOfModule=newName;
}

Couple of things to note here. First of all every variable that is not declared with “var” is on the Global scope. I think thats true in general for javascript. Apart from this anything else in a module is private, that means the variable “nameOfModule” in our example is only accessible from inside that module itself. If we try to access it from out side the module NodeJS will throw an error. Now to expose methods and properties to outside world, we have to add the methods and properties to an object named “exports”.
Now to consume the module, lets write a “main” class which we can assume as the application entry.

console.log("Hello Modules.");
var modOne=require ('./modOne.js');
console.log(modOne.getName());
modOne.setName("I am set by the setter. Setter is Public.");
console.log(modOne.getName());
//console.log(nameOfModule);// this will throw an ERROR as we are trying to access private var
console.log(stupidVar);

Save both the files in the same directory. Open the commandline and navigate to the directory. To run our application we have to write

node main

Just a note that we do not have to specify “.js” after the “main”, while feeding the file to NodeJS.

The source files for this are here to download.
[reference : net.tutsplus.com ]
Now there are publicly available packages, libraries and modules in our case. To install a module in a nodeJS application, one has to navigate to the folder containing the application through commandline and install the required module. An implementation detail is as below.

npm install module_name

I hope you remember “npm” from previous tutorial. For a quick reminder, “npm” and “node” both the tools are installed by default when we have installed nodeJS. The “npm” is the package manager for nodeJS.

Happy Nodding :)

How I started with NodeJS

To be clear with the following contents, let me tell you that the whole experiment or learning was done in a Windows environment, but then I found that MAC is easier to start with NodeJS.
Installation is straightforward. Just download the binary and get going.
Once installation is done, node and npm command should be available from command-line, if not try restarting and then test the commands. Testing the commands means typing “node” or “npm” in the command line and pressing ENTER. If installed, the result should be something other than “missing command or error message”.

The next commands I went through are

 npm root
npm bin 

why ? To see the details of below explained stuff

npm root // to see where modules go
npm bin // to see where executables go

[ ref : https://npmjs.org/doc/faq.html ]

Next I tried with “-g” switch as below.

 npm root -g
npm bin -g

Why ? Thats because node installs differently for global and local specifications.
[ ref : https://npmjs.org/doc/faq.html
If you install something with the -g flag, then its executables go in npm bin -g and its modules go in npm root -g ]

Next is my favorite first timer stuff, to list all the installed packages,

npm ls

Next to know a little bit about Node itself

 npm view npm author
npm view npm contributors

So thats the start.
Next is the node official doc. [ ref : http://nodeguide.com/beginner.html ]

haXe : Compiling to javascript

After all these tutorials in pure javascript, lets see is there is any other help from any where for our javascript programming. The best thing to happen is haXe. If you have not heard about it yet, haXe is a language on its own! But why I need to learn another language?! Well, there is not much difference in javascript and haXe. As you have already seen, inside javascript also we have different libraries and we need to learn them to use them, lets look at haXe, for now, as another library (which is obviously not). Now, you will be amazed to know that after you know haXe and start writing on haXe, your code can be compiled to whole lot of different languages including C++,PHP and javascript.
First of all one need to download and install haXe. The installation is quite straight forward and the help at its own website is quite nice. I do not think you will get stuck there, but if it is, please drop me a line here or best is ask in their forum.
The hello world application for javascript I am writing here is straight out of haXe documentation. I am just putting it here for I got stuck at the compiling time and want you to get going in haXe without ever stopping anywhere.
I hope by this time, you have installed haXe on your machine. Lets write our first haxe class.

package ;
class Main 
{
	public static function main() 
	{
		trace('Hello');
	}
}

The purpose is to show a message we passed in “trace” function. We will write an HTML file to use the javascript file compiled from the above haXe code. Now save the above in a file with a name say “Main.hx”. Yep, “.hx” is the native HAXE extension. Suppose we are going to save the generated javascript file inside the “js” folder and with name “my.js”. Lets say that our HTML file is named as “HelloWorld.html”. So the folder structure looks as below.

src
   Main.hx
bin
HelloWorld.js
   js
     my.js

So our “Main.hx” is on “src” folder and all our generated code is inside “bin” folder. The generated javascript is inside “js” folder, which is inside “bin” folder. You may be asking, where are the javascript and html files?! Well, we have decide the structure of our application first and now we will move on and create those files in necessary folder. First lets see the HTML file code, which is as below.

	<div id="haxe:trace"></div>
	<script src="js/my.js"></script>

Basically we need a div element with id “haxe:trace”, so that all our “trace()” function calls in our “.hx” file will be written inside this div . We are ready for action now. All we need is to compile our haXe code to javascript.

Open command prompt (terminal in Mac) and move to the src folder, where we have stored our “.hx” file. Now write the code below in the terminal and press enter.

haxe -main Main.hx -js ../bin/js/my.js

Now go inside “bin/js/” folder and you will be amazed to see our “my.js” file is created there. Open the html file in a browser to see the result of it. The “Hello” we typed in trace is now written to the HTML page div tag. Sweet, is not it!
Suppose we want an real alert, a javascript alert, then modify the haxe code as below.

package ;
class Main 
{
	public static function main() 
	{
		js.Lib.alert('Hello World');
	}
}

Next process, I hope you know already :) . Compile it in HAXE compiler as below to generate the JS file with the modified code. This means fire up the command prompt and put the same command as we did before, which is as below.

  haxe -main Main.hx -js ../bin/js/my.js

Now open the same html file in the browser again to see the alert message.
Yeehaa :) we got a nice javascript code generated from haXe.

Here is the source files with comments in it for you to play with.

Happy haXeing :)

HTML5 : Canvas with mouse interaction.

Here we are going to study about the creation of Canvas dynamically and the basic mouse interactivity with the Canvas elements.

The source files for this example is here for you to download and play.

We are going to use a separate sprite sheet and try to make the scene a little more interesting than the last one. I would say to download the source files and see the example for yourself to see what is it doing. Basically there is a scene on the background with 6 tables and a girl doing some experiment on third table. Towards the left top corner of the scene, there is another girl trying to do some magic with its magic wand. If you look at the code, the scene with the girl doing experiment is a straight forward drawing from the sprite sheet as we did that in last tutorial. The girl with the magic wand is drawn in a dynamically created Canvas. The code looks as below.

//creating canvas dynamically
canvasGirl=document.createElement('canvas'); 
canvasGirl.width=100; 
canvasGirl.height=100;
//get the context
contextGirl=canvasGirl.getContext('2d');
//customise canvas
canvasGirl.style.background='transparent';
canvasGirl.style.zIndex=5;
//add the canvas to the document
document.body.appendChild(canvasGirl);

The first thing is to create a new canvas with “createElement(‘canvas’)”. That will make a new canvas. Now we have to get the context to draw something on it. This is done with “getContext(’2d’)” as usual, nothing new here. And lastly, we have to add our newly created Canvas to the document and this is done with “document.body.appendChild(canvasGirl);” code. Thats beautiful. Everything else in the code is just configuring the canvas.
Once the canvas and context are available to us, we can do anything we are able to do to a Canvas. So the girl with the magic wand is drawn to the context of this Canvas element. The code looks as below.

resetContext(canvasGirl,contextGirl);
contextGirl.drawImage(photo, 108*counter+10, 900, 96, 64, 0, 0, 96, 64);

First we are resetting the Canvas, so as to draw new content on each timer. Since the girl with magic wand is actually an animation, we call the code above repeatedly on a timer event. And first we clear the context with our own function and then redraw an image from sprite sheet at the same place on the canvas, so as to make an animation. If you are following along with the previous tutorials then the reseting of context code should not be new to you, else the code looks as as below.

resetContext=function(canvasRef,contextRef){
	contextRef.clearRect(0,0,canvasRef.width,canvasRef.height);
};

All we have, are two layers of drawing and animation drawn from the same sprite sheet.
Lets do some mouse interactivity with these elements. The code looks as below.

//------------ adding mouse interaction --------------
$('#board_one').mouseup(function() {
  	alert('Canvas with Tables');
});
$(canvasGirl).mouseup(function() {
  	alert('Canvas with Girl, doing magic.');
});

The trick here is JQuery’s neat job. Just remember anything wrapped with $() will give a JQuery element of the same HTML element. So our dynamically created Canvas is made an JQuery element by

$(canvasGirl)

Notice that there is no ‘#’ before the name as it is before ‘board_one’, thats for the ‘board_one’ we are referring the HTML element with id ‘board_one’, while the other one we are creating dynamically in javascript and converting it to a JQuery equivalent.
Thats nice and if you have not done yet, please go ahead and click on the board to get an alert showing different message and if you click on the girl with magic wand, that will alert a different message.
This is quite simple and straight forward interactivity, but I think you got the point.
Happy coding :)

And yes, I made it to one month of continuous posting, highly influenced by Keith Peters. Though it was not all javascript and I missed some dates, but all in all, the number of posts in a month is achieved. I am quite happy about my effort and a big thanks to all of you, who read it.

HTML5 : animating on a Canvas with sprite sheet

We have already seen how to animate on Canvas in one of our last tutorials. That was quite straight forward as we were using Canvas drawing straight out of the box. Here we will use our newly learnt tools(loading and drawing bitmap image in canvas) to make an animation.
The source files are here for you to download and play.
The concept is the same though as in re-drawing in the canvas at a regular interval, but instead of drawing with drawing API, we will be drawing image.
First of all lets understand what a sprite sheet is? The sprite sheet is an image! Just an image? Yes, its just an image file. In that image file, we should have almost all the images needed for our purpose. That means, one image file consisting of all the images required in a project. Why its necessary? Good question. The reason being we have just one call to the server and all our image data is loaded. The next reason is to update the image data. Suppose we want to update the graphics all at once, we just have to update one image file.
If you are going through the image file in our source, the image file has 12 different images of a character. All we are going to do is, cycle over each character over a time. That we can do with a “setInterval()” javascript function.
Lets see the javascript code first and then we will go learn it.

$.ready()
{
	//------------- 1. defining the variables and functions----------------
	var resetCanvas,resetContext,
		canvas_1,context_1,
		image_data,photo,loadPhoto,imageLoaded,timer,onTimer,counter,animate;
	//------------- 2. configuring ---------------------------------------
	//get the canvas and context
	canvas_1=$('#board_one').get(0);
	context_1=canvas_1.getContext('2d');
	counter=1;
	//---------------functions----------------------------------
	//resetting the width and height, it automatically resets the canvas
	//clear the board, quick and dirty, its a HACK!!
	resetCanvas = function (canvasRef){
		canvasRef.width=canvasRef.width;
		canvasRef.height=canvasRef.height;
	};
	//clear the board
	resetContext=function(canvasRef,contextRef){
		contextRef.clearRect(0,0,canvasRef.width,canvasRef.height);
	};
	
	imageLoaded=function(evt)
	{
		var originalPic=evt.target; 
		animate();
	};
	
	loadPhoto=function(path)
	{
		photo.src=path;
	};
	
	animate=function(){
		timer=setInterval(onTimer,150);
	};
	
	onTimer=function(){
		resetContext(canvas_1,context_1);
		
		//draw again
		if(counter>=12)
		{
			counter=0;
		}
		context_1.drawImage(photo, 97*counter, 0, 97, 95, 230, 110, 97, 95);
		counter++;
		//clearInterval(timer);//incase we need to stop the animation
	};
	
	//----------------3. using-----------------------------
	image_data=context_1.createImageData(canvas_1.width,canvas_1.height);
	photo=new Image();
	photo.onload=imageLoaded;
	loadPhoto('images/walkSheet.png');
};

In the beginning we have just defined the variables, then populating them with the required values and scripts. Lastly we are calling our methods and using these variables.
In the beginning we get our canvas and its context. Intitialised the “counter” to 1 as we have 12 different positions of the image, we will be needing a counter to move to next position. So once these properties are populated, we have created some functions, which we use. Then moving on to the actions, firstly there is an “image_data” object, I have created, but not used. That is just to show we can create an image data object from the canvas, which is used to pixel manipulation. So the first thingon making our animation is making an Image object with

photo=new Image();

Then we have to provide an event handler for its image load event, that is done with the code below.

photo.onload=imageLoaded;

If you remember we have already created the “imageLoaded” function, which looks as below.

imageLoaded=function(evt)
	{
		var originalPic=evt.target; //just for the shake of understanding
		animate();
	};

Here the first the “originalPic” variable is defined just to know that we can get the image from the event object as “event.target” . So all in all this “imageLoaded” function actually initialises our animation by calling “animate()”.

animate=function(){
		timer=setInterval(onTimer,150);
	};

And “animate()” function actually intialises the timer for animation and calls “onTimer” function repeatedly at each 150 milliseconds.

onTimer=function(){
		resetContext(canvas_1,context_1);
		//draw again
		if(counter>=12)
		{
			counter=0;
		}
		context_1.drawImage(photo, 97*counter, 0, 97, 95, 230, 110, 97, 95);
		counter++;
		//clearInterval(timer);
	};

This is the place, where all the action is happening. First we have to clear our canvas, that is being done on the first line of this function. Next we check, whether the “counter” is more than 12, if it is then reset it to 0. Thats for we have just 12 different images. Now the line which actually draws our animation is

context_1.drawImage(photo, 97*counter, 0, 97, 95, 230, 110, 97, 95);

This is actually drawing part of the sprite sheet to a particular are in the canvas. Its like copying a portion from the sprite sheet and pasting it in the Canvas. On the next timer event (ie; after another 150 milliseconds, it will copy another portion and paste it in the same location.) we take another portion from the sprite sheet and paste it in the same area of canvas. Since we are clearing the canvas and re-drawing it over a period of time, it seems as if the character is animating! The time of 150 milliseconds is just a guess, we can lower it to see the jerky animation or higher it to see the character move faster. The other values as 97,95 are the width and height of each image in pixels, so that would be different for different sprite sheets.
The last line increments the counter by one.
Now we have a nice looking animation in our HTML Canvas.

Things to not here is, I struggled for sometime to see this working, for I was providing wrong parameter values (97,95 seemed perfect). So if your animation is not visible, first see if the parameter values are correct. Even one pixel higher than the actual image height or width will make the whole thing invisible. Though lower values work fine.

Happy coding.

HTML5 : Canvas pixels, part II

Now we know we can create and manipulate pixels in the Html5 Canvas. That opens doors to some of cool effects we can make with this tool. Here we will make an image invert its color.
The thing to remember here is, we have to get the image data from the context after our image is drawn. That means instead of

createImageData(width,height)

we have to use

getImageData(intialX,initialY,width,height)

The function for creating this look as below,

filterImage=function()
	{
		// Get the CanvasPixelArray from the given coordinates and dimensions.
		var imgd = context_one.getImageData(0, 0, width, height);
		var pix = imgd.data;
		// Loop over each pixel and invert the color.
		for (var i = 0, n = pix.length; i < n; i += 4) {
		  pix[i  ] = 255 - pix[i  ]; // red
		  pix[i+1] = 255 - pix[i+1]; // green
		  pix[i+2] = 255 - pix[i+2]; // blue
		  // i+3 is alpha (the fourth element)
		}
		// Draw the ImageData at the given (x,y) coordinates.
		context_one.putImageData(imgd, 0, 0);
	}

first of all, we take the image data object from the context. Then like the previous example, we loop over each pixel and subtract the channel values from the maximum value so as to invert the color of each pixel. And finally we have to put them into context for rendering.
I would suggest the same thing here as, please try this in every possible browser. While the same code does not work for me on Chrome, it did work in latest Opera.
The source code is here for you to download and play.
Hope that helps someone.

HTML5 : Canvas pixels

Once we know we can do an image drawing in a canvas, lets see if we can at all make a new image from a blank canvas. The answer is yes. We have absolutely accessible to each pixel values of the context of the Canvas.

Let me say you here, if you are not getting the result in one browser, try other browsers. I had tried a long time with the same code in Chrome without any result! But it worked out in latest version of Opera !! Not sure whats wrong but if you test it in other browsers it will save you a lot of time.

The code for image creation is as below.

createImage=function()
	{
		// Create an ImageData object.
		var imgd = context_one.createImageData(width,height);
		var pix = imgd.data;
		// Loop over each pixel and set a transparent red.
		for (var i = 0; n = pix.length, i < n; i += 4) {
		  pix[i  ] = 255; // red channel
		  pix[i +1] = 0; // green channel
		  pix[i +2] = 0; // blue channel
		  pix[i+3] = 255; // alpha channel
		}
		// Draw the ImageData object at the given (x,y) coordinates.
		context_one.putImageData(imgd, 0,0);
	}

All we have to do is create an image data with a width and height, that is done as

var imgd = context_one.createImageData(width,height);

The above code, I have taken the width as canvas width and height as canvas height.
Now we will access each element of that image data array. Since its just an array the pixel values of each pixel is stored linearly as "red,green,blue,alpha" and so we have to loop over each 4th element and get the values of all the channels of each pixel. Thats exactly what our for loop is doing. Then we are only populating the red channel to its max, since we want a red fill canvas image :) !
Lastly the image data must be put on the context so as to render the pixels. The following code does that.

context_one.putImageData(imgd, 0,0);

Thats a simple pixel manipulation we did now with our canvas element.
The source code is available here for you to download and play.

HTML5 : Some more libraries

While going good with at least one post each day, I am not sure what to put next for HTML5 Canvas.
I came across some more libraries for HTML5 Canvas.

KineticJS
Doodle.js
Image Manipulation on Canvas
pixastic
CamanJS
tiffus
JSDrawing
PaintbrushJS
Javascript Code Quality tool
JSLint
Javascript programming
Backbone
Underscore
Knockout
Sugar

phpjs
pdf.js
iScroll
zepto.js

node.js
CoffeeScript

We are going good with HTML5 Canvas, I hope there is nothing much left for the basic concepts except one or two, which we will be covering soon. Leave a comment if you feel any tutorial in particular to be included.
Stay tuned and thanks for being here.

HTML5 : multiple Canvas

The interesting thing about Canvas is we can have more than one Canvas and even we can create Canvas dynamically through javascript. Why would we need more than one Canvas !?! There may be many answers to this, but the most important of all is, I think, when we need to update the Canvas. If you remember we have made an animation in our previous tutorial. And diving a little inside of that code will show you that we are redrawing it on each timer event.
The whole idea of animating in Canvas revolves around updating the Canvas on each timer event. But we also know that once something is written/drawn in Canvas
we can not have a reference to it. So in the other hand we have to re-write or re-draw everything again. And this redrawing over and again will take us to a performance problem. Re-drawing the whole canvas with all the elements, again and again is not a good idea and we must avoid it if we can. The best thing to do is to separate elements to different Canvases. And we will never update the Canvas which contains the elements we do not need to be changed. Then only refresh the Canvases whose content needs updating upon timer event. Seems like a plan ? I have done this with the example source code, for your reference.
The source code of the example, contains two canvases and I am updating only the second Canvas as it needs updating.

Point to note here is Canvas refresh does not mean only updating the values, but actually it means erase everything from the Canvas and re-draw everything with the updated value.

Hope that helps to understand the power of Canvas a little better.