tunnel sized

tunnel ellipse with sizing

by rob Davis

HTML

<div id="container">
    <div>
        <canvas id="starfield" />
    </div>
    <div>
        <canvas id="tunnel" />
    </div>
</div>

CSS

#container { background-color:yellow;
        width:500px;
    height:500px;
    position:relative;
}
#tunnel    {position:absolute; top:0px; left:0px; }
#starfield {position:absolute; top:0px; left:0px; }

JavaScript

drawStarfield('starfield');
drawTunnel('tunnel');

function drawTunnel(elementId) {
    var canvas = document.getElementById(elementId);
    var context = canvas.getContext('2d');
    var width=400;
    var height=400;
    setCanvasSize(canvas, width, height);
    for (var i=1;i<30;i++) {
        drawTunnelSection(context, 200, 200, 1.0, 14*i);
    }
}

function drawTunnelSection(context, x, y, z, size) {
    var gradient = context.createRadialGradient(1, 1, 0, 1, 1, 1);
    gradient.addColorStop(0.95,'rgba(255,255,0,0.0)');
    gradient.addColorStop(1.0,'rgba(0,255, 255, '+z+')');
    drawEllipse(context, x, y, size, size, gradient);
}


function drawEllipse(context, cx, cy, rx, ry, fill){
    context.save();
    context.beginPath();
    context.translate(cx-rx, cy-ry);
    context.scale(rx, ry);
    var ellipseFill;
    if (typeof(fill)==='function') {
        ellipseFill=fill(context);
    } else {
        ellipseFill=fill;
    }
    context.arc(1, 1, 1, 0, 2 * Math.PI, false);
    context.fillStyle = ellipseFill;
    context.fill();
    context.closePath();
    context.restore();
}




function drawStarfield(elementId) {
    var canvas = document.getElementById(elementId);
    var context = canvas.getContext('2d');
    var width=400;
    var height=400;
    var maxStars=1000;
    
    setCanvasSize(canvas, width, height);
    clearCanvas(canvas, context, "#000");
    addStars(context, canvas.width, canvas.height, maxStars);
}
function setCanvasSize(canvas, width, height) {
    canvas.setAttribute('width', width);
    canvas.setAttribute('height', height);
}

function clearCanvas(canvas, context, style) {
    context.fillStyle = style;
    context.fillRect(0,0,canvas.width, canvas.height);
}
function addStars(context, width, height, maxStars) {
    for (var i=0;i<maxStars;i++) {
        drawDot(context, getRandomRange(0,width), getRandomRange(0,height), getRandomRange(0,2.5),...