JSFiddle - React, Tailwind, and code Playground

by loktar

HTML

<canvas id="canvas"></canvas>

JavaScript

var canvas = document.getElementById("canvas"),
    ctx = canvas.getContext("2d");

canvas.width = canvas.height = 512;

var lights = [],
    blocks = [],
    vector = function(_x,_y){
        this.x = _x;
        this.y = _y;  
    }
    light = function(_position,_radius,_angleSpread, _color){
        this.color = _color;
        this.radius = _radius;
        this.angleSpread = _angleSpread;
        this.position = _position;
        this.angle = Math.random()*180;
    },
    block = function(_position,_width,_height){
       this.position = _position;
       this.width = _width;
       this.height = _height;
       this.visible = false;
    },
        angle = 0;


// FIND DISTANCE ************************
function findDistance(light, block, angle, rLen, start, shortest, closestBlock){
    var y = (block.position.y + block.height/2) - light.position.y,
        x = (block.position.x + block.width/2) - light.position.x,
        dist = Math.sqrt((y * y) + (x * x));

    if(light.radius >= dist)
    {
        var rads = angle * (Math.PI / 180),
            pointPos = new vector(light.position.x, light.position.y);
        
        pointPos.x += Math.cos(rads) * dist;
        pointPos.y += Math.sin(rads) * dist;
        
        if(pointPos.x > block.position.x && pointPos.x < block.position.x + block.width && pointPos.y > block.position.y && pointPos.y < block.position.y + block.height)
        {
            if(start || dist < shortest){
                start = false;
                shortest = dist;
                rLen= dist;
                closestBlock = block;
            }
            
            return {'start' : start, 'shortest' : shortest, 'rLen' : rLen, 'block' : closestBlock};
        }
    }
    return {'start' : start, 'shortest' : shortest, 'rLen' : rLen, 'block' : closestBlock};
}
// **************************************

// SHINE LIGHT**************************
function shineLight(light){
    var curAngle = light.angle - (light.angleSpread/2),
 ...