Instant Download of Premium Web Templates
2009 December | Java-ScriptGate.com - Part 2

Archive for December, 2009

JS and App

December 22, 2009

JavaScript Particle System

After yesterday’s Processing.js article I discovered this post about creating a particle system with JavaScript and a canvas: Parcycle: A Particle System with HTML5 canvas.

You can view the effect in action in this demo: Parcycle

I found the use of globalCompositeOperation very interesting in this code. It allowed the author to make particles appear lighter when they overlap by using a drawing mode rather than detecting the colour on the canvas.

Excel Training Aberdeen - F1plus offers face to face excel training on Microsoft software applications. Training can be given in our office in Aberdeen city centre or in your own premises.

JS and App

December 21, 2009

Processing.js

Processing.js is a JavaScript/HTML canvas port of Processing. Processing provides a toolkit for programming images, animation and even games. It’s hosted in Java and provides its own mini-IDE. Processing.js can interpret Processing projects, but runs them in a browser that supports canvas.

If you’re familiar with Processing already you should be able to run some of your projects in Processing.js. Just create a simple HTML page with a canvas and include processing.js. The examples/ folder in the source distribution demonstrates how to do this using a script called init.js — this finds the canvas on the page and instantiates a processing object:

Processing(canvas, scripts[i].text);

Processing.js allows you to embed Processing scripts into HTML like this:

<script type="application/processing">
</script>

Calling Processing from JavaScript

If you’re new to Processing but have a lot of JavaScript you want to reuse, you can safely call Processing from your JavaScript code. I’ve prepared an example project, fire.js which demonstrates one way of mixing JavaScript and Processing. You can pass the current Processing environment to a JavaScript class like this:

void draw() {
  fire.update(this);
}

Keeping some level of separation between JavaScript code and “pure” Processing seems like a good way to avoid confusion if you ever port to another Processing platform.

Processing Basics

Every Processing project has setup() and draw() functions. In this case I’ve instantiated a Fire object and I’m passing this which is the current Processing object. Then I can access Processing’s functions from within my JavaScript class.

The Processing reference gives example code for all of the Processing functions. There’s a lot of drawing primitives like rect() and ellipse() — playing around with these is a good way to learn Processing.

Related Projects

ruby-processing is a Ruby port. It differs to Processing.js because it provides a Ruby-style version of the Processing API. Even CamelCase method names have been changed to underscores.

Although Processing.js and Processing are awesome projects, if you just want to draw vector graphics you’d be better off with something like Raphael. Processing is more suited to animated visualisations or interactive projects.

Frameworks

December 17, 2009

Making Every Specified Link Send Via Ajax Using MooTools

Tags: , ,

This is a relatively simple concept and is nothing to elaborate but I wanted to share a small piece of code that will take every link with the class of “ajax” and access it using AJAX instead of actually going to that page. This using the same Request instance which will keep it optimized and manageable. MooTools is going to make this nice and easy on us…

Usage

JavaScript

var ajax_request = new Request(
{
	onSuccess: function(responseText, responseXML)
	{
		$('message').set('html', responseText);
	}
});

$$('.ajax').each(function(item){
	var url = item.get('href');
	item.addEvent('click', function(event){
		ajax_request.options.url = url;
		ajax_request.send();
		return false;
	});
});

HTML

Sent Via AJAX
Also Sent Via AJAX
I am filled with the results of our AJAX requests.

Its incredibly simple so no demo for this. The important concept here is that we are using the same request and pulling the href attribute base on a class selection. So changing something from AJAX to regular is as easy as adding or removing the class “ajax”. Pretty cool eh?

JS and App

December 16, 2009

Node.js on IRC

Node.js makes writing IRC-related programs straightforward and super-efficient, thanks to its evented networking. Therefore it isn’t surprising that a lot of projects have appeared already.

I’ve been looking around for reusable JavaScript IRC libraries, and discovered the following projects.

IRC Daemon Demo

This IRC daemon demo presented at jsconf.eu/2009 demonstrates how to build a server. It has simple prototypal classes for channels and users, and parses IRC commands using a regex and a switch. It also uses node’s TCP library.

nodelog

nodelog is an IRC logger. This would be a good place to start if you’d like to build a JavaScript IRC bot. The client code is clear and reusable. The log.js file demonstrates how to use the irc.js library.

irccat-nodejs

irccat-nodejs is based on irccat which allows you to send messages to IRC using HTTP or a TCP socket. The client code is again fairly simple.

egret

egret is a command-line IRC client. The base code uses node’s TCP sockets and a hash to interpret IRC commands into English.

nodejs.irc

nodejs.irc is an IRC bot. It’s not finished yet, but the code is separated out and easy to follow.

aoibot

aoibot is similar to nodejs.irc, but it looks like the finished product will be less generic. Again, it’s unfinished, so I expect the author still has a lot of ideas and work to do.