← Coding with Art

Galaxy

This spinning galaxy is made only from the code below. Tap the Try buttons to change it, or type in the code yourself. The picture updates as you go.

The code HTML, CSS and JavaScript

The three backgrounds

There are three different "backgrounds" in this code. Here is what each one controls.

1. The purple page behind everything

html, body { margin: 0; background: #421a4a; ... }

#421a4a is the purple. Change that hex code to recolor the whole page.

The Pause button uses the same purple, so change background: #421a4a; in the button line too if you want them to match.

2. The black behind the canvas (the "space")

canvas { ... background: #000; ... }

This is only visible for a split second before the picture draws. The real space color is set by the pixel math (see number 3).

Pick White and watch closely: you will see a quick white flash before the galaxy covers it.

3. The picture's own background (black with stars)

This is the part that really makes the "space." It comes from the pixel formulas:

d[o]     = Math.min(255, 120 * base + star[i]);   // red
d[o + 1] = Math.min(255, 255 * base + star[i]);   // green
d[o + 2] = Math.min(255, 255 * glow + 60 * arms + star[i]);  // blue

Where base is 0 (far from the spiral), all three colors are 0, which is black. To get a colored background instead, add a number to each line:

  • Dark blue space: add + 15 to the blue line
  • Dark purple space: add + 20 to red and + 30 to blue
  • Gray space: add + 25 to all three

Example for dark blue:

d[o + 2] = Math.min(255, 255 * glow + 60 * arms + star[i] + 15);

The rest of the code, in order

  • <style> block: the CSS. Controls the page color, canvas size, rounded corners, and button look.

  • <canvas id="art" width="300" height="300">: the drawing surface. Higher numbers look sharper but run slower.

  • <button id="toggle">: the Pause/Play button.

  • <div id="err">: an empty box that only fills with text if something breaks.

    window.onerror = ...: the error catcher that prints problems into that box.

    Make a mistake changes Math.cos to Math.coss, a word the computer does not know. Watch the error appear under the picture, then fix it.

  • var VIEW = 9;: how much of the math world is visible. Smaller means zoomed in.

  • The first double for loop: runs once. For every pixel it stores the distance from center (rad), the angle (ang), and whether it's a star (star).

    Stars: 0.993 means about 7 pixels in every 1,000 become a star. A smaller number makes more stars.

  • function frame(): runs about 60 times a second. For every pixel it calculates:

    • glow: the bright blob in the center
    • wave: a pattern that swirls around the center
    • arms: the wave sharpened (Math.pow(wave, 5)) and faded with distance (Math.exp(-rr / 4)), which makes the spiral arms
    • base: glow plus arms, which drives the color

    Glow changes the 2 in rr * rr / 2. Arms changes the 2 in 2 * ang[i]. Twist changes the 1.3. Arm shape changes the 5 in Math.pow(wave, 5).

  • time += 0.05: makes the spiral rotate. Bigger number means faster.

  • The last lines: hook up the Pause/Play button.