Let’s chat about node.js

Last night I had the pleasure of presenting on node.js and socket.io at the November OKDG meet. It was a lot of fun, and there seemed to be quite a bit of interest in the technology. Well, now I’ve got the slideshow available in PDF form for all to see. :)

Not only that, the chat demo created in the presentation will remain live for all to play with. In addition to what was covered in the presentation, it also has a basic message filtering system to convert URLs to embedded content. Try posting a URL to an image, Youtube video or PDF and you can see it in action. Check it out!

Download as PDF

Peint – Event-based canvas drawing, yay!

I did a bunch of work on Peint today, refactoring it to something that I might actually consider good enough to release. On that note; Peint enters alpha as of now! Get it here. It won’t blow up your computer! …I hope.

No documentation yet, but you can look at the updated sample code at peint.stephenbelanger.com to get an idea of what this thing is capable of. It’s pretty neat. :D

Peint – Painting to canvas made easy!

As I mentioned in my previous post, I have been working on a graphics rendering library for the html5 canvas element. Peint is an attempt to make using canvas easier and more flexible, while introducing event-based programming. You include only the core library and then use Peint.require(‘yourscript’, callback) to asynchronously load extra modules and execute the callback code once the module is ready. Using this method, http requests are made smaller and occur after page load, so execution appears near instant–no waiting for 100+ kb of code to download before the system starts setting itself up. :)

In the previous post, I showed you the viewport module, which allowed me to dynamically resize the canvas to cover the entire viewport and adjust itself automatically whenever the window is resized. This time I’ll be introducing part of the draw2d module. Canvas uses the fillRect() method to fill a rectangular area with a particular color, but it only suppports the standard pixel coordinates from a top left origin. I decide that wasn’t good enough for me.

In the example below, we fill the background with a pleasant blue color, and we draw 2 boxes using a mix of numeric pixel coordinates, css-style ’100px’ coordinates and even percentages. In addition to that, it also demonstrates positioning from a non-top/left origin! The canvas.coords() positioning system dynamically tranforms the input values using their key names and the current dimensions of the canvas, allowing lots of flexibility in element positioning. I have a neat demo showing the fill() and image() methods online here; peint.stephenbelanger.com

// Initialize our canvas screen.
var screen = Peint.init('#screen');

// Load draw2d module for basic 2D drawing tools.
Peint.require('draw2d', function(){
	var canvas = this;

	// Draw our background color.
	canvas.fill(canvas.rgb(0,100,130));

	// Draw some boxes...
	// This one is statically positioned with pixels.
	// 'px' is optional, it will assume that on numbers.
	canvas.fill(
		canvas.rgba(150,200,0,0.8)
		, canvas.coords({
			right:150
			, bottom:150
			, width:'100px'
			, height:'100px'
		})
	);
	// This one is dynamically positioned with percentages.
	canvas.fill(
		canvas.rgba(200,150,0,0.8)
		, canvas.coords({
			left:'2%'
			, top:'5%'
			, width:'15%'
			, height:'50%'
		})
	);
});

On canvas, and how to use it well.

I have been working on a graphics library for html5′s canvas element lately, and I’ve come to the conclusion that it is not particularly easy to use “as-is” in more involved applications. I suppose that should be expected, it being such new technology and all, so I thought I’d share some of my experiences, insights and progress on this endeavor.

First of all, canvas doesn’t play nice with CSS width and height dimensions. Rather than adjusting the usable area like one might expect, it stretches the element while retaining the buffer size of the render area. That means making it “100%” width/height becomes needlessly difficult. That is one of the many things the library I’m working on is meant to handle. Here’s a code sample to show it forcing the canvas dimensions by using a “resize” event in my library.

// Load viewport module.
Peint.require('viewport', function(){
	var viewport = this;

	// Initialize our canvas screen.
	screen = Peint.init('#screen');

	// Create onresize handler.
	var resizer = function(){
		// Get current viewport dimensions.
		var size = viewport.size();

		// Position and resize it.
		screen.position().resize(size);
	};

	// Attach handler to resize and load event of window.
	this.bind('resize', resizer);
	this.bind('load', resizer);
});

What this does, is create a callback function to attach to window.onresize and window.onload (Actually, window.onload doesn’t exist, so it fakes it). Every time this event is called, it repositions the element on the screen and resizes it to match the viewport size. As you’ve probably figured out by the Peint.require() with the callback, this library supports loading external modules with callbacks to run when the code is ready. It’s pretty neat. I’ll share more as I complete the modules.

The Infinite Genies Paradox

There’s not many people out there who haven’t heard the story of Aladdin–it’s a story with quite a bit of history and has got recognition in many forms, including a cartoon rendition created by Disney. It’s a creative and interesting story, but it’s also quite flawed and most people don’t even notice that.

After finding the genie, Aladdin is told he can wish for anything except “more wishes”. This is obviously an attempt to prevent infinite wishes, but a loophole surfaces later in the story that went largely unnoticed. Jafar wishes to be a genie himself, and the original genie grants that wish, thus genies can create other genies.

Herein lies the paradox; a genie offers me 3 wishes–first I wish for something ridiculous like a trillion dollars, then I wish for someone else to become a genie, and spend my last wish to free the original genie. I now have a second genie with no wishes spent. I make another extravagant wish, make a new genie and then free the old one. Wash, rinse repeat–we have our Infinite Genies Paradox.

Were there only two wishes, the infinite loop could still be done if you chose not free the genies after, but that would have fit better with the moral dilemma at the core of the story. So why three wishes, and not two?

Anyone else have any weird observations like this on popular media? Feel free to post whatever comes to mind. It’s always interesting to see what you might not have seen without someone else pointing it out.

Back online! ….mostly…

UPDATE: Everything works now. :D
So I decided to switch from Slicehost to Rackspace Cloud, since my server was overloaded and having all sorts of network issues. Unfortunately things kind of exploded. I managed to get the site back online, but the URL rewriting isn’t working at the moment and I’m at work right now, so I’ll have to look at it later.

JSGame – Game development in Javascript!

I’ve recently started work on my newest Github project; JSGame. It’s a fork of philogb’s V8-GL which, as you can likely assume by the name; is an implementation of OpenGL using Google Chrome’s V8 javascript runtime. At the moment, the only difference in JSGame is a mostly untested implementation of OpenAL. I also plan to add networking support and likely SDL. Once the various libraries I plan to implement have had Javascript interfaces made I’ll begin work on making helper classes for easier access to common actions.

jquery.flash is on github! (and updated)

I’ve recently starting committing changes made to jquery-flash to github. You can view the project page for it here.

I’ve also made a few updates, the most notable being; I moved the checking code to seperate functions called isie(), hasflash() and flashversion(). All of which can be used anywhere, with or without using $().flash(). I also did some minor optimizations to make execution a little speedier.

Node.js – I’m a fanboy already.

About a week ago I discovered a unique new programming technology called node.js. It’s an event-driven javascript system running on Google’s V8 engine and is primarily focused on functioning as a web server. I hesitate to call it a web server however–it’s much more than that!

Using this amazing new system I was able to integrate support for the new WebSockets specification that Google recently added support for in Chrome, and I used this to make a simple real-time chat application with emoticon support and translation of image links to inline images and youtube links to inline videos.

…and the best part? This only took me about an hour!

There are some simple architectural differences that make it vastly more powerful that most any other popular web programming languages. One of my favorite characteristics of it’s unique design is that variables assigned in a global scope can be accessed across all requests…that means regular database querying becomes completely unnecessary! You could create a json object where you store your data, then check it over every once in awhile, and apply any changes to the database. It’d work as a query queue of sorts that would greatly reduce database interaction overhead and it wouldn’t require setting up some other incredibly complex software to proxy the requests!

I’m also quite fond of the branching design. There is a module called ‘http’ for running web servers, but it’s really just routing everything through the tcp module, which essentially does the same thing. The ‘http’ module just simplifies the interface and makes common actions like sending headers more elegant. It’s a clever design really; supply varying levels of complexity in control to match varying levels of complexity in application–not everyone is going to want the ability to create a mail server, so why would they want to have to access tcp directly? Inversely; not everyone is only going to want a web server, so why would you only supply access to http-related code?

Have a look at this sample code I ripped-off from the homepage of node.js;

var sys = require('sys'),
   http = require('http');
http.createServer(function (req, res) {
  setTimeout(function () {
    res.sendHeader(200, {'Content-Type': 'text/plain'});
    res.sendBody('Hello World');
    res.finish();
  }, 2000);
}).listen(8000);
sys.puts('Server running at http://127.0.0.1:8000/');

Isn’t it so simple? You just include a few modules, run http.createServer() with a callback function passing in the request and response objects and tell the server to listen on whatever port you want and whatever domain you want. In the callback you can branch of by breaking apart request.url to find the user’s query and use that to redirect via a switch block or whatever other means you see fit. Best of all, it’s asynchronous, so you don’t need to halt execution for every little thing. It’s even compatible with many common Javascript utility libraries such as Underscore

OiNK admin found not guilty, pigs fly, hell freezes over.

Alan Ellis, creator of OiNK, the most renowned Bit Torrent based music sharing site has been unanimously declared not guilty in his recent court appearance. This is a massive success for freedom and a huge blow to the corruption of the current music industry. It will be most interesting to see what the response is to this in the coming days.

[Source]