Prime Spiral

One of the first bits of JavaScript I wrote, archived here for posterity.

Organising positive integers into a rectangular spiral, highlighting the primes.

HTML5 canvas disabled?

prime-spiral.js

raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
BSD Zero Clause License

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/

window.addEventListener('load', function () {
    'use strict';
    var canvas, ctx, reqAnimFrame;
    canvas = document.getElementById("primeSpiralCanvas");
    ctx = canvas.getContext("2d");
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    reqAnimFrame =
        window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        function (callback) {
            return setTimeout(callback, 1);
        };

    var primes = [];
    var curNum = 2;
    var xInc = 0;
    var yInc = 1;
    var stepsTaken = 0;
    var steps = 1;
    var currentNum = 2;

    var currentPoint = { x : Math.floor(canvas.width / 2),
        y : Math.floor(canvas.height / 2) };

    var getNextColour = function () {
        var colours = ["red", "darkgray","blue"];
        var i = colours.length - 1;
        return function() {
            i = (i + 1) % colours.length;
            return colours[i];
        };
    }();

    var curColour = getNextColour();

    function resetVars() {
        primes = [];
        curNum = 2;
        xInc = 0;
        yInc = 1;
        stepsTaken = 0;
        steps = 1;
        currentNum = 2;

        currentPoint = { x : Math.floor(canvas.width / 2),
            y : Math.floor(canvas.height / 2) };
    }

    function resizeCanvas() {
        if (canvas.width != canvas.clientWidth ||
                canvas.height != canvas.clientHeight) {
            canvas.width = canvas.clientWidth;
            canvas.height = canvas.clientHeight;
            resetVars();
        }
    }

    function nextPoint() {
        if (stepsTaken >= steps) {
            // rotate
            var oldX = xInc;
            xInc = yInc;
            yInc = oldX;
            xInc *= -1;
            steps += 0.5;
            stepsTaken = 0;
        }
        currentPoint.x += xInc;
        currentPoint.y += yInc;
        stepsTaken += 1;
    }

    function isPrime(myNum) {
        var i;
        for (i = 0; i < primes.length; i++) {
            if(myNum % primes[i] === 0) {
                return false;
            }
        }
        primes.push(myNum);
        return true;
    }

    function plotPoint() {
        if (isPrime(currentNum)) {
            ctx.fillRect(currentPoint.x, currentPoint.y, 1, 1);
        }
        nextPoint();
        currentNum += 1;
    }


    function render() {
        ctx.fillStyle = curColour;
        resizeCanvas();
        var i;
        for (i = 0; i < steps / 2; i++) {
            plotPoint();
        }
        // and again
        if (currentPoint.x < 0 || currentPoint.x >= canvas.width
                || currentPoint.y < 0 || currentPoint.y >= canvas.height) {
            // change colour
            curColour = getNextColour(); 
            resetVars();
        }
        reqAnimFrame(render);
    }

    render();
});