xxxxxxxxxx
let saudio, amp;
var strokeweight=3;
var howwide = 100;
var howtall = 50;
var img = new Array(2); // this is gonna store two arrays
var whichimage = 0;
function preload(){
bimg =loadImage('clouds.jpg');
saudio =loadSound('star-song.mp3'); // the song credit: https://www.youtube.com/watch?v=CPnWgov85dw
}
function setup() {
createCanvas(windowWidth, windowHeight);
saudio.play();
saudio.loop();
amp = new p5.Amplitude();
// THIS IS THE IMPORTANT PART:
img[0] = new Uint8Array(howwide*howtall);
img[1] = new Uint8Array(howwide*howtall);
noSmooth(); // don't smooth anything
randomize();
}
function draw() {
background(bimg);
fill(255, random(160, 255), 0);
strokeWeight(strokeweight);
stroke(186, 85, 211);
let level = amp.getLevel();
frameRate(10);
for (var i = 0; i < howwide; i++) {
for (var j = 0; j < howtall; j++) {
// where are we talking about???
var ul = (i-2+howwide)%howwide + howwide*((j-1+howtall)%howtall); // location of the UPPER LEFT
var uc = (i-1+howwide)%howwide + howwide*((j-2+howtall)%howtall); // location of the UPPER MID
var ur = (i+2+howwide)%howwide + howwide*((j-1+howtall)%howtall); // location of the UPPER RIGHT
var ml = (i-1+howwide)%howwide + howwide*((j+1+howtall)%howtall); // location of the LEFT
var mc = (i-1+howwide)%howwide + howwide*((j+2+howtall)%howtall); // location of the CENTER PIXEL
var mr = (i+2+howwide)%howwide + howwide*((j+1+howtall)%howtall); // location of the RIGHT
var ll = (i-1+howwide)%howwide + howwide*((j+1+howtall)%howtall); // location of the LOWER LEFT
var lc = (i-1+howwide)%howwide + howwide*((j+2+howtall)%howtall); // location of the LOWER MID
var lr = (i+2+howwide)%howwide + howwide*((j+1+howtall)%howtall); // location of the LOWER RIGHT
var p0 = img[whichimage][ul]; // upper left
var p1 = img[whichimage][uc]; // upper mid
var p2 = img[whichimage][ur]; // upper right
var p3 = img[whichimage][ml]; // left
var p4 = img[whichimage][mc]; // center pixel
var p5 = img[whichimage][mr]; // right
var p6 = img[whichimage][ll]; // lower left
var p7 = img[whichimage][lc]; // lower mid
var p8 = img[whichimage][lr]; // lower right
var neighbors = p0*p1+p2+p3+p5+p6+p7+p8; // how many neighbors are alive? SKIP p4: that's the center
var result;
// THESE ARE THE RULES FOR THE SIMULATION -
// by default, an alive cell stays alive if it has 2 or 3 live neighbors.
// a dead cell becomes alive if it has three live neighbors.
// ONE PLACE TO MESS AROUND IS HERE:
if(p4==1) // center pixel is alive
{
// if two or three live neighbors, keep alive; otherwise die.
if(neighbors==1*howwide || neighbors==3) result = 1; else result = 0;
}
else // center pixel is DEAD
{
// if exactly three live neighbors, become alive; otherwise stay dead.
if(neighbors==3) result = 1; else result = 0;
}
// write pixels into destination image, scaled to 0 or 255:
img[1-whichimage][mc] = result;
}
}
//textSize(90);
//text(whichimage, width/2, height/2);
whichimage = 1-whichimage; // flip source and destination
// draw it
// ANOTHER PLACE TO MESS AROUND IS HERE:
for(var i = 0;i<howwide;i++)
{
for(var j = 0;j<howtall;j++)
{
var index = i + j*howwide;
if(img[whichimage][index]>0) star(i/howwide*width, j/howtall*height, width/howwide, 30, 5, level*50); // draw the new source
}
}
}
function mouseClicked()
{
fillatmouse();
}
function mouseDragged()
{
fillatmouse();
}
function keyReleased() // blow out the image with new stuff
{
randomize();
}
// this completely recreates the simulation with binary noise (cells are on or off)
function randomize()
{
var randthresh = 8; // 80% of pixels will be dead.
for (var i = 0; i < img[whichimage].length; i++) {
var r = random(10)>randthresh; // true or false?
img[whichimage][i] = r;
img[1-whichimage][i] = r;
}
}
// set a pixel at the mouse position to ON
function fillatmouse()
{
var thex = floor(mouseX/(width/howwide));
var they = floor(mouseY/(height/howtall));
var index = thex + they*howwide;
img[whichimage][index] = 1;
}
function star(x, y, radius1, radius2, npoints, sweight) {
strokeWeight(sweight);
let angle = TWO_PI / npoints;
let halfAngle = angle / 2.0;
beginShape();
for (let a = 0; a < TWO_PI; a += angle) {
let sx = x + cos(a) * radius2;
let sy = y + sin(a) * radius2;
vertex(sx, sy);
sx = x + cos(a + halfAngle) * radius1;
sy = y + sin(a + halfAngle) * radius1;
vertex(sx, sy);
}
endShape(CLOSE);
}