Archive for the 'JavaScript' Category

JavaScript Date Formatting – An Unorthodox Way

September 2nd, 2008

Most date formatting implementations use format strings where format specifiers like “mm”, “mmm”, “HH”, etc. are used for selecting different components of a date. Here’s a different approach that allows you to use more human-readable format specifiers (while being rather verbose). Read the rest of this entry »


Subpixel Scrolltext in JavaScript

August 5th, 2008

Subpixel Scrolltext SnapshotAfter having it in my to-do list (the “what if?” section) for a long time, I’ve finally managed to spend some time on a JavaScript implementation of a very tiny scrolltext; using a 5×5 font with subpixel rendering (aka ClearType). Knowing what subpixel-rendered static letters look like on an LCD, I wanted to see the effect of scrolling them, 1/3 pixel at a time. Note: you need an LCD monitor to see the full effect. Read the rest of this entry »


A Boulder Dash Clone in Only 20 Lines of JavaScript

June 18th, 2008

This was my very first entry for the quasi-regular, friendly 20-line JavaScript competition over at OZONE Asylum, for the month of January 2008. My entry titled “Rockford the Invincible” got the second place among some very impressive entries (I was actually a bit shocked that I got the second place).

Rockford the Invincible

In-game screenshot. Click to play!

Read the rest of this entry »


Rendering N-sided Polygons with DHTML

November 28th, 2006


With this technique, convex polygons with any number of sides can be rendered, with a solid color or a background image as the fill. Click here or the image below to launch the demo (opens in a pop-up window).

DHTML Polygon Rendering Demo

You can probably figure it out on your own by playing with the checkboxes at the lower left corner of the pop-up, but read on to find out how it all works. Read the rest of this entry »


PHP-style Serialization of JavaScript Objects

July 30th, 2006

Heavily-scripted data entry forms that expand on-the-fly as the user enters new data make life easier for the user. However, it can be hell for a developer to devise a method for the representation and submission of data with arbitrary length and depth.

A logical thing to do seems to be to take an object-oriented approach to represent all client-side data as one or multiple JavaScript objects. The objects themselves could be arrays or objects with child objects and so on. Of course the objects need to be serialized in some way before they’re posted back. Now, if you’re using PHP at the back-end, you might make use one of the multitude of JSON implementations out there for posting back the data in JSON format and converting the data back to PHP objects (assuming that you want to end up with PHP objects.) JSON is pretty much native to JavaScript (clever use of the toSource method may cut it in most cases) but you need extra PHP code for deserialization.

If you want to take the opposite approach and use PHP-native serialization, then the following function will convert any complex JavaScript object into PHP-serialized string which can be converted back into PHP objects with a single call to the deserialize PHP function. Read the rest of this entry »


Finding Out Class Names of JavaScript Objects

July 10th, 2006

JavaScript lacks a built-in function for getting the exact class names of object instances. The typeof operator just returns “object” for instances of both Object and Array, as well as user-defined classes. The following example illustrates this:

function MyClassA() {
}

var obj = new Object();
var arr = new Array();
var myobj = new MyClassA();

document.write(typeof(obj) + "<br />");
document.write(typeof(arr) + "<br />");
document.write(typeof(myobj) + "<br />");

The output of the code above, with your browser is:

Read the rest of this entry »