Sunday, February 04, 2007

The Game of Life in JavaScript

I just discovered the canvas tag. <canvas> is a HTML element used for embedding vector graphics within your web pages. It was first introduced by Apple for Dashboard Widgets, and was later implemented in Safari.

It is now supported by most major browsers, except Internet Explorer. Fortunately, Google has a compatibility layer called ExplorerCanvas that brings the canvas tag to IE.

To learn more about <canvas>, Mozilla has a very good tutorial.

Anyhow, for a quick demo of what it can do, check out my interpretation of John Conway's Game of Life, in JavaScript, aptly named Game of Death. I coded it on a Saturday afternoon, slightly hungover, to teach myself JavaScript and familiarize myself with <canvas>.

Here's the source code. Warning: No fancy algorithms within.

1 comment:

  1. Hi Mohit

    I've borrowed (and credited) some of your GoL script for a Monome patch, but note that your business logic is a bit wonky compared to other implementations...

    You may like to create a second cellmap to store next generation values following die and birth methods, then update the cellmap with these values when the nextgens have all been calculated.