LOADING...

Preview

Pen ID
Unlock Campus Themeforest adv

 

Code
CSS
body {
  background: #080808; 
}

canvas {
}
JS
 /**
 * @author Benedikt Wolters
 */
     // shim layer with setTimeout fallback
    window.requestAnimFrame = (function(){
      return  window.requestAnimationFrame       || 
              window.webkitRequestAnimationFrame || 
              window.mozRequestAnimationFrame    || 
              window.oRequestAnimationFrame      || 
              window.msRequestAnimationFrame     || 
              function( callback ){
                window.setTimeout(callback, 1000 / 60);
              };
    })();
window.requestInterval = function(fn, delay) {
    if( !window.requestAnimationFrame       && 
        !window.webkitRequestAnimationFrame && 
        !window.mozRequestAnimationFrame    && 
        !window.oRequestAnimationFrame      && 
        !window.msRequestAnimationFrame)
            return window.setInterval(fn, delay);

    var start = new Date().getTime(),
    handle = new Object();

    function loop() {
        var current = new Date().getTime(),
        delta = current - start;

        if(delta >= delay) {
            fn.call();
            start = new Date().getTime();
        }

        handle.value = requestAnimFrame(loop);
    };

    handle.value = requestAnimFrame(loop);
    return handle;
}

window.clearRequestInterval = function(handle) {
    window.cancelAnimationFrame ? window.cancelAnimationFrame(handle.value) :
    window.webkitCancelRequestAnimationFrame ? window.webkitCancelRequestAnimationFrame(handle.value)   :
    window.mozCancelRequestAnimationFrame ? window.mozCancelRequestAnimationFrame(handle.value) :
    window.oCancelRequestAnimationFrame ? window.oCancelRequestAnimationFrame(handle.value) :
    window.msCancelRequestAnimationFrame ? msCancelRequestAnimationFrame(handle.value) :
    clearInterval(handle);
};

$(document).ready(
            function () {
                var lastTime = 0;
                var GameOfLife = {};
               
                GameOfLife.Dead = 0;
                GameOfLife.Alive = 1;


                GameOfLife.glider = new Array();
                //Glider..
                GameOfLife.glider[0] = new Array();
                GameOfLife.glider[0][0] = GameOfLife.Dead;
                GameOfLife.glider[0][1] = GameOfLife.Alive;
                GameOfLife.glider[0][2] = GameOfLife.Dead;

                GameOfLife.glider[1] = new Array();
                GameOfLife.glider[1][0] = GameOfLife.Dead;
                GameOfLife.glider[1][1] = GameOfLife.Dead;
                GameOfLife.glider[1][2] = GameOfLife.Alive;

                GameOfLife.glider[2] = new Array();
                GameOfLife.glider[2][0] = GameOfLife.Alive;
                GameOfLife.glider[2][1] = GameOfLife.Alive;
                GameOfLife.glider[2][2] = GameOfLife.Alive;
                //LWSS
                GameOfLife.LWSShip = new Array();
                GameOfLife.LWSShip[0]= new Array();
                GameOfLife.LWSShip[0][0] = GameOfLife.Dead;
                GameOfLife.LWSShip[0][1] = GameOfLife.Alive;
                GameOfLife.LWSShip[0][2] = GameOfLife.Alive;
                GameOfLife.LWSShip[0][3] = GameOfLife.Alive;
                
                GameOfLife.LWSShip[1]= new Array();
                GameOfLife.LWSShip[1][0] = GameOfLife.Alive;
                GameOfLife.LWSShip[1][1] = GameOfLife.Dead;
                GameOfLife.LWSShip[1][2] = GameOfLife.Dead;
                GameOfLife.LWSShip[1][3] = GameOfLife.Alive;
              
                GameOfLife.LWSShip[2]= new Array();
                GameOfLife.LWSShip[2][0] = GameOfLife.Dead;
                GameOfLife.LWSShip[2][1] = GameOfLife.Dead;
                GameOfLife.LWSShip[2][2] = GameOfLife.Dead;
                GameOfLife.LWSShip[2][3] = GameOfLife.Alive;              
                GameOfLife.LWSShip[3]= new Array();
                GameOfLife.LWSShip[3][0] = GameOfLife.Dead;
                GameOfLife.LWSShip[3][1] = GameOfLife.Dead;
                GameOfLife.LWSShip[3][2] = GameOfLife.Dead;
                GameOfLife.LWSShip[3][3] = GameOfLife.Alive;                
                GameOfLife.LWSShip[4]= new Array();
                GameOfLife.LWSShip[4][0] = GameOfLife.Alive;
                GameOfLife.LWSShip[4][1] = GameOfLife.Dead;
                GameOfLife.LWSShip[4][2] = GameOfLife.Alive;
                GameOfLife.LWSShip[4][3] = GameOfLife.Dead;                


                var Config = {};
                Config.CellSize = 4;
                Config.Height = 300;
                Config.Width = 1000;
                Config.GridColor = "#000";
                Config.LiveColor = "red";
                Config.DeadColor = "#000"
                Config.RowCount = Config.Height / Config.CellSize;
                Config.ColCount = Config.Width / Config.CellSize;

                var c = document.getElementById('c'),
                    ctx = c.getContext('2d'),
                    grid;


                GameOfLife.placeFigure = function (fig, x, y) {

                    for (i = 0; i < fig.length; i++) {
                        for (j = 0; j < fig.length; j++) {
                            grid[x + i][y + j] = fig[i][j];
                        }
                    }
                }

                GameOfLife.invertFigure = function(fig){
                    var newfig = fig;
                    for (var x = 0; x < fig.length/2; ++x){
                        for (var y = 0; y < fig[0].length; ++y){
                            var tmp = fig[x][y];
                            newfig[x][y] = fig[fig.length-1-x][y];
                            newfig[fig.length-1-x][y] = tmp;
                        }
                    }
                    return fig;
                }

                GameOfLife.transposeFigure = function (fig) {
                    var newfig = new Array();
                    for (i = 0; i < fig[0].length; i++) {
                        newfig[i] = new Array();
                        for (j = 0; j < fig.length; j++) {
                            newfig[i][j] = fig[j][i];
                        }
                    }
                    return newfig;
                }

                initRandom = function(){
                    //num = Config.
                    //TODO
                }

                init = function () {
                    grid = initGrid();
                    //initRandom();


                    for(var i=Math.round(Config.ColCount-Config.ColCount*0.8); i  3) {
                                    newGrid[i][j] = GameOfLife.Dead;
                                    currentDiff[changes] = {
                                        x: i,
                                        y: j
                                    };
                                    changes++;
                                }
                            } else {
                                if (neighbors == 3) {
                                    newGrid[i][j] = GameOfLife.Alive;
                                    currentDiff[changes] = {
                                        x: i,
                                        y: j
                                    };
                                    changes++;
                                }
                            }
                        }
                    }
                    grid = newGrid;
                }

                var initGrid = function () {
                    var grid = new Array(Math.round(Config.Width / Config.CellSize) + 1);
                    for (i = 0; i < grid.length; i++) {
                        grid[i] = new Array(Math.round(Config.Height / Config.CellSize) + 1);
                        for (j = 0; j < grid[i].length; j++) {
                            grid[i][j] = GameOfLife.Dead; //Init all Dead.
                        }
                    }
                    return grid;
                }

                var renderGrid = function () {
                    for (var i = 0; i * Config.CellSize < Config.Width; i++) {
                        for (var j = 0; j * Config.CellSize < Config.Height; j++) {
                            if (getPointCell(i, j) == GameOfLife.Alive) {
                                ctx.fillStyle = Config.LiveColor;
                            } else {
                                ctx.fillStyle = Config.DeadColor;
                            }

                            ctx.fillRect(i * Config.CellSize, j * Config.CellSize, Config.CellSize - 1, Config.CellSize - 1);
                        }
                    }
                };

                var renderDiff = function () {
                    for (var i = 0; i < currentDiff.length; i++) {

                        if (getPointCell(currentDiff[i].x, currentDiff[i].y) == GameOfLife.Alive) {
                            ctx.fillStyle = Config.LiveColor;
                        } else {
                            ctx.fillStyle = Config.DeadColor;
                        }

                        ctx.fillRect(currentDiff[i].x * Config.CellSize, currentDiff[i].y * Config.CellSize, Config.CellSize - 1, Config.CellSize - 1);
                    }
                };

                var render = function () {
                    renderDiff();
                };

                function animloop() {
                    //requestInterval(animloop, 4);
                    if (!paintStarted) {
                        newState();
                    }
                    render();

                    //
                    //sleep(1000);
                    //newState();

                }

               init()
            });
Term
Sat, 01/13/2018 - 07:27

Related Codes

Pen ID
Pen ID
Pen ID
Square Adv