Porting the rays app to processing.js

03 Feb 2013

Over the past few years I have been on a quest to port my old flash web toys to an open platform.
Javascript in conjunction with the HTML canvas element seemed to provide a good alternative.
So I tried to port one of my favorite toys, the rays app, to processing.js. This is the result.

I have mixed feelings about the experience.
On the one hand, it was extremely easy to develop for, as it would be expected from javascript + processing.
Since I had already implemented rays as a java applet with the original processing library, porting to javascript was mainly just changing static to dynamic typing and adjusting some function calls.
I did not manage to get keyboard shortcuts to work, even though they work perfectly in the java version.
This might be some quirk in javascript I am not aware of.

The major issue is, as I was afraid, not enough performance to implement a ‘glow effect’ as I did in flash (press tab to access the options).
The idea in flash was to only draw the ‘dirty’ part of the image to an offscreen buffer, then draw it to the main framebuffer, then blur the offscreen buffer and blend it again into the main framebuffer.
Then clear the offscreen buffer and start again.
Perhaps it is easier to illustrate this with the corresponding actionscript.

Here the drawLines() function draws directly to the offscreen buffer, while drawToScreen() handles copying the buffer twice to the screen.
(This code was written almost 5 years ago, makes me feel old). In processing.js the only option which runs at a fast enough speed is a simple direct draw to the main framebuffer.
I reproduce here the main loop for the curious.

The only interesting thing to comment about this code is how I used the bezier curve to extract a bit more precision out of each time step.
Normally I could just draw a line from the past to the present.
Here instead I draw a bezier curve where the control points are given by euler steps into the future or past, respectively.
This essentially produces an interpolation which uses the first derivative at each endpoint, meaning we get a higher order integration of the path and can use a much bigger time step to integrate the paths.