Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<meta name="description" content="Blinkenlights" />
<html>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script src="http://blinken.eecs.umich.edu/client.js"></script>
<body></body>
</html>
 
(new Blinken({title: "Overtaken", 
                author: "odious_toad"})).run(function () {
    // configuration! numConcurrent must be at least 1
    var numConcurrent = 5;
    var numSpacesBetweenStarts = 5;
    // whether or not to randomly generate brightness
    // looks better without
    var brightness = false;
    function RGBA(r, g, b, a) {
        this.r = r;
        this.g = g;
        this.b = b;
        this.a = a;
    }
    
    function getRandomColor() {
        return new RGBA(Math.random(), Math.random(), Math.random(), Math.random());
    }
    
    // between 2 and -2
    function getGrowth() {
        var tmp = Math.random();
        var abs = (tmp < 0.33) ? 2 : ((tmp > 0.44) ? 1 : 0);
        return (Math.random() < 0.66) ? abs : abs * -1;
    }
    
    function setExpandingColor(rgba, maxI, lights) {
        var halfLength = lights.length / 2;
        for (var i = 0; i < maxI && i < halfLength; i++) {
            var up = halfLength+i;
            var down = halfLength-1-i;
            lights[up].rgb(rgba.r, rgba.g, rgba.b);
            lights[down].rgb(rgba.r, rgba.g, rgba.b);
            lights[up].a = brightness ? Math.random() : rgba.a;
            lights[down].a = brightness ? Math.random() : rgba.a;
        }   
    }
    
    // initialization
    var currentDistance = [];
    var going = [];
    var currentColors = [];
    for (var i = 0; i < numConcurrent; i++) {
        currentDistance.push(0);
        going.push(false);
        currentColors.push(getRandomColor());
    }
    // set the first color to start
    going[0] = true;
    // make an extra space for the background color
    currentColors.push(getRandomColor());
    // make first background color black (background color lives at the end)        
    currentColors[numConcurrent].a = 0;
    
    // update lights one frame
    return function (lights) {
        // set background color 
        setExpandingColor(currentColors[numConcurrent], lights.length / 2, lights);
        
        // set other colors
        for (var i = 0; i < numConcurrent; i++) {
            if (i > 0 && currentDistance[i-1] > numSpacesBetweenStarts) {
                going[i] = true;
            }
            if (going[i]) {
                setExpandingColor(currentColors[i], currentDistance[i], lights);
        currentDistance[i] = Math.abs(currentDistance[i] + getGrowth());
            }
        }
    // the first one can be overtaken, and if it has we should move on.
    var moveNext = false;
    for (var i = 0; i < numConcurrent; i++) {
        if (currentDistance[i] >= lights.length / 2) {
            moveNext = true;
        }
    }
    
    // move everything for the next iteration
    if (moveNext) {
        // promote the first color to background
        currentColors[numConcurrent] = currentColors[0];
        // move existing colors over
        for (var i = 0; i < numConcurrent-1; i++) {
            currentColors[i] = currentColors[i+1];
            currentDistance[i] = currentDistance[i+1];
            going[i] = going[i+1];
        }
        // pick a new color and reset variables
        currentColors[numConcurrent-1] = getRandomColor();
        currentDistance[numConcurrent-1] = 0;
        going[numConcurrent-1] = false; going[0] = true;
        }
         return 100;
    };
});
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers