One quiet night I was remembering how much fun I had plotting graphics on my TRS-80 Color Computer and lamenting the lack of any decent GUI platforms on modern computers. Specifically, there was one trig function plotter from my CoCo's manual that I thought would be fun to port.
The book and code are long gone, but the output looked a little like Fig. 1. But how should we reproduce it now?
First we pick an overall
size for the square canvas. Halve it on each axis
to give us an
Then we fetch the
canvas element itself and create a 2D
it, setting its dimensions while we're at it.
Inside the loop, we plot each value of
t (θ) over the range [0, 2π) by
fractional increments. We compute
r using the trig function
cos. We then
translate the polar coordinates
(r, t) to a Cartesian point
(x, y) and
plot it relative to our
The canvas routines like
fillRect will automatically perform subpixel
antialiasing ("dithering") for fractional coordinates, which can consume
to the nearest integer. This isn't crucial in this routine, but may be
; ; canvas.height = canvas.width = size; ; for ; t < 2 * Math.PI; t += 0007
So here is a minimal implementation. Feel free to play around with the code. In the next section, we'll look at how to make it feel more authentically TRS-80.
for loop. But graphics
plotting on the CoCo was not only imperative and synchronous, but a lot
slower! How could I reproduce the satisfyingly tortoise-like plot speed of
the Motorola 6809E (approximated in Fig. 2) with modern
In most other languages this would be a simple
that one thread has to keep the entire browser page running!
So we need to break our larger task ("Plot values from 0 to 2π") into
discrete steps ("Plot one point at regular intervals"). The body of the
function is essentially the same as the body of the
for loop above:
- calculate the
(x, y)coordinates for
- plot the new coordinates
- set a timeout to plot the next
Steps (1) and (2) — the body of our
for loop above — will
form the body of the new
graphLoop callback function, along with a
setTimeout delay just long enough to create that authentic sub-MHz CPU
The next invocation of the loop won't know what value of
t to use, so
we'll have to pass that along with. And since the
doesn't send parameters, we have to create a 0-arity lambda that does
There are also a few quirks of the Klipse widget that I had to work
around. Every code change causes the whole block to be re-evaluated, so
graphLoop calls to exit cleanly. Here are the highlights:
fis defined as a
varto make it visible across re-evaluations of the code.
fis an arrow function, which creates a unique object every evaluation.
- On entry to
graphLoop, we check if
fhas been recreated the code. If so, return without drawing or setting a new time-out.
- When the full graph period (
[0, 2π)) is complete, clear the graph and start over at 0.
Here is the final code currently driving Fig. 2.
; ; ; ; ; ; canvas.height = canvas.width = size; canvas.onclick =paused = !paused; ; 0, 0, size, size; f, 0;
We spent the previous section refactoring our
for loop so we could slow the
plotting down. In this exercise, we're not going to plot trig functions one
point at a time. We're going to plot about a hundred functions per second,
end-to-end. Let's play with Canvas for a while.
; ; ; ; ; canvas.height = canvas.width = size; // let the user stop/start the animation ; canvas.onclick =; ; 0;
You'll notice lots of
consts in the code, because almost none of these variables
will change. E.g., there should be only one
canvas element and
only one caption.
The most of the logic is only required to start/stop the animation by clicking
on the canvas. The rest of the code is a loop that does nothing but calculate
the polar coordinates
(r, θ), convert them to Cartesian
(x, y), and plot
them on the canvas.
Note: A previous revision of this post used ClojureScript to render Fig. 3.