Most of my understanding how to do this came from a couple of tutorials:

This was an assignment I did while I was in grad school for one of my courses. I've ported the C++/OpenGL code to work on an HTML5 canvas object. You need to be using Firefox, Chrome, Opera, or Safari to view this. The project is a complete working version of my assignment, down to a bug causing a single pixel line to not render correctly.Pending Former Instructor Permission to Post.

Instructions: Click the black canvas 3 times. For every third click, a triangle will be drawn with the linear gradient.

Random Canvas