Canvas Part 2

by gmurray

HTML

<canvas id="canvas"></canvas>
<div>
    <input type="text" id="number" value="80" />
    <input type="button" id="changeNumber" value="Change Number" />
</div>

JavaScript

function ensureQueueFrame() {
    if (!window.queueFrame) {
        if (window.requestAnimationFrame) {
            window.queueFrame = window.requestAnimationFrame;
        } else if (window.webkitRequestAnimationFrame) {
            window.queueFrame = window.webkitRequestAnimationFrame;
        } else if (window.mozRequestAnimationFrame) {
            window.queueFrame = window.mozRequestAnimationFrame;
        } else {
            window.queueFrame = function (callback) {
                window.setTimeout(1000.0 / 60.0, callback);
            };
        }
    }
}

function transform(point3, matrices) {
    var vector = [point3[0], point3[1], point3[2], 1],
        newVector,
        matrix;

    for (var m = matrices.length - 1; m >= 0; m--) {
        newVector = [];
        matrix = matrices[m];
        for (var i = 0; i < 4; i++) {
            newVector[i] = 0;
            for (var j = 0; j < 4; j++) {
                newVector[i] += vector[j] * matrix[j][i];
            }
        }
        vector = newVector;
    }

    return [vector[0], vector[1], vector[2]];
}

function getRotationMatrices(axisPoint, rotationAngle) {
    var len = Math.sqrt(
    axisPoint[0] * axisPoint[0] + axisPoint[1] * axisPoint[1] + axisPoint[2] * axisPoint[2]);
    var a = axisPoint[0] / len;
    var b = axisPoint[1] / len;
    var c = axisPoint[2] / len;

    var d = Math.sqrt(b * b + c * c);

    var matrices = [];

    matrices.push([
        [1, 0, 0, 0],
        [0, c / d, -b / d, 0],
        [0, b / d, c / d, 0],
        [0, 0, 0, 1]
    ]);

    matrices.push([
        [d, 0, -a, 0],
        [0, 1, 0, 0],
        [a, 0, d, 0],
        [0, 0, 0, 1]
    ]);

    matrices.push([
        [Math.cos(rotationAngle), -Math.sin(rotationAngle), 0, 0],
        [Math.sin(rotationAngle), Math.cos(rotationAngle), 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1]
    ]);

    matrices.push([
        [d, 0, a, 0],
        [0, 1, 0, 0],
        [-a, 0, d, 0],
        [0, 0, 0, 1]
    ]);

 ...