Your browser is very old. You might enjoy surfing the web more if you used something newer like:

Google Chrome

Even Firefox would be OK.

If you're being forced at gunpoint to use Internet Explorer, you should at least upgrade it. Version 8 is tolerable and 9 will be OK when it comes out.

Get on your bike. No excuses

Don’t worry about how your bike looks.

Don’t worry about your clothes.

Don’t worry about the weather. If it’s cold, you’ll warm up again. If it’s wet, you’ll get dry again.

Just get on your bike and ride it. Go as fast as you can. Or as fast as you want to.

You don’t need headphones.

Listen to the sounds of the world. The wind whistling in your ears. The sound of your heart pounding in your chest.

If you see someone else on a bike, wave to them.

Don’t worry about mountain bike, road bike, fixie, used, new, fancy brand, department store bike.

People should wave to other people on bikes.

We are brothers and sisters of the wheel and should acknowledge each other as such.

Pixel level drawing with the <canvas> element

Today we’re going to learn how to manipulate the HTML <canvas> element at the pixel level.

To start, we need a canvas element:

<canvas id=”canvas” width=”640″ height=”480>”
<p>Your browser doesn’t support canvas.</p>
</canvas>

Next get a handle to the canvas element and its 2d context.

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

Next, we create a ImageData element on the canvas that covers the entire canvas. This is we’re actually going to be drawing our pixels on.

var imgd = ctx.createImageData(canvas.width, canvas.height);

The ImageData element is basically just a big array. Each block of 4 array elements corresponds to a single pixel.

The array indices in each pixel block control the color and alpha transparency as follows:

  • i = Red value
  • i+1 = Green value
  • i+2 = Blue value
  • i+3 = Alpha

And here’s a simple function for drawing a single pixel given an x,y value and r,g,b:

var setPixel = function(imageData, point, r, g, b, a) {
    a = typeof a == 'undefined' ? 255 : a;
    i = (point.x + point.y * imageData.width) * 4;

    imageData.data[i  ] = r;
    imageData.data[i+1] = g;
    imageData.data[i+2] = b;
    imageData.data[i+3] = a;
};

Let’s say we wanted to draw a red pixel at 100,100. Given the ImageData element we declared above, we’d call setPixel like this:

setPixel(imgd, {x:100,y:100}, 255, 0, 0);

Notice we’re not passing a value for the alpha transparency. The setPixel defaults the alpha to fully opaque if a value is specified.

A more full-featured setPixel function can be found here. This version accepts HTML hex codes for colors and extends the Object prototype so we call it directly on the ImageData element like this:

imgd.setPixel({x:100,y:100}, ‘#f00’);

Here’s a sample that draws a Sierpinski triangle using the chaos game

New Year’s Resolutions

2011 was an interesting year with lots of highs and lows.

But rather than rehashing last year, I’ve decided to go on record with my New Year’s Resolutions in the hopes that a permanent record will help me stay honest:

  • Starting tomorrow, no alcoholic beverages until I’m under 200lbs.
  • Write at least one blog post or poem (I reserve the right to keep the poems to myself) per week.
  • Ride a century. The ideal would be 100-miles-to-nowhere in Cherokee.
  • Play more D&D with the kids.
  • Do something cool with my Freeduino
  • Get a app published to the Android Market.
So here’s to being the people we always wanted to be next year and here’s to not beating ourselves up too much if we can’t pull it off.