xxxxxxxxxx
119
let grid = [];
let nextGrid = [];
let cnv;
let gsz = 10;
let sz = 80;
function setup() {
cnv = createCanvas(800, 800);
let cx = (windowWidth - cnv.width) / 2;
let cy = (windowHeight - cnv.height) / 2;
cnv.position(cx, cy);
makeGrid();
//showGrid();
//update();
//showGrid();
frameRate(10)
noStroke();
rectMode(CORNER)
angleMode(DEGREES)
drawingContext.shadowOffsetX = 15;
drawingContext.shadowOffsetY = -15;
drawingContext.shadowBlur = 12;
drawingContext.shadowColor = color(0);
}
function draw(){
background(197)
showGrid();
updateGrid();
if (frameCount%10 ==0){
makeGrid()
let sinoffy = map(sin(frameCount),-1,1,-25,25)
let sinoffx = map(cos(frameCount),-1,1,-25,25)
let shadx= random(-15,15)
let shady= random(-15,15)
drawingContext.shadowOffsetX =sinoffx;
drawingContext.shadowOffsetY = -sinoffy;
}
}
function makeGrid() {
for (let j = 0; j < gsz; j++) {
grid[j] = [];
for (let i = 0; i < gsz; i++) {
let p = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
//let p = [0,1]
grid[j][i] = random(p);
}
}
for (let j = 0; j < gsz; j++) {
nextGrid[j] = [];
for (let i = 0; i < gsz; i++) {
nextGrid[j][i] = grid[j][i];
}
}
}
function updateGrid() {
// if a cell is alive and has BOTH an east west neighbor it dies
// if a cell is dead and has EITHER north south neighbor it comes alive
for (let j = 1; j < gsz - 1; j++) {
for (let i = 1; i < gsz - 1; i++) {
//check east west of alive cell
if (grid[j][i] == 1) {
if (grid[j][i - 1] == 1 && grid[j][i + 1] == 1) {
//dies
nextGrid[j][i] = 0;
} else {
// else it lives
nextGrid[j][i] = 1;
}
} else {
// cell is dead
// check north and south
if (grid[j + 1][i] == 1 || grid[j - 1][i] == 1) {
//comes to life
nextGrid[j][i] = 1;
} else {
// stays dead
nextGrid[j][i] = 0;
}
}
}
}
// copy new grid into old grid
for (let j = 0; j < gsz; j++) {
for (let i = 0; i < gsz; i++) {
grid[j][i] = nextGrid[j][i];
}
}
}
function showGrid() {
for (let j = 0; j < gsz; j++) {
for (let i = 0; i < gsz; i++) {
if (grid[j][i] == 1) {
fill(117,200,174);
} else {
fill(90,61,43);
}
rect(i * sz+(sz/8), j * sz+(sz/3), sz/2, sz/2);
}
}
}
function keyPressed() {
// this will download the first 25 seconds of the animation!
if (key === 'g') {
saveGif('shadow2.gif', 35);
}
if (key === 's') {
saveCanvas('shadow', 'jpg');
}
}