More fun with Canvas - a JavaScript Starfield!

The canvas element is awesome. JavaScript is fast enough that you can run some pretty computationally intensive stuff (I've seen 3D games, a NES emulator, and much more all done with JS!). This script shouldn't push your CPU to the limit, but it does show how easy it is to create cool effects with just a small amount of code.

The Code

This code bears probably only a small amount of explanation. It sets the origin in the center of the canvas then whenever a star is added it calculates the slope of the line between the star and the origin, and moves it along that line. The width the star increases as its "depth" decreases, and the speed at which it accelerates is increased by 1/10th every iteration. A simple check tells whether the stars are still visible and when they aren't a new star gets created.