xxxxxxxxxx
53
// include the package as ts.
const ts = TotalSerialism;
// assign sub-libraries to the ts namespace
// using globalThis as namespace can result in name clashes
Object.assign(ts, ts.Stochastic, ts.Algorithmic, ts.Generative);
let posX, posY, size;
let cols = 200;
let ca = new ts.Automaton();
let gens = [];
let rules = [22, 120, 89, 30, 82, 41, 60, 45, 106, 110, 105]
function setup() {
createCanvas(windowWidth, windowHeight);
posX = ts.spread(cols, width);
posY = ts.spread(cols, height);
size = width/cols;
// feed Automaton with random initial values
ca.feed(ts.coin(cols));
// set the rule, see:
// https://en.wikipedia.org/wiki/Elementary_cellular_automaton
ca.rule(120);
}
function draw() {
background(0);
noStroke();
if (frameCount % 240 == 0){
// pick a new rule every 4 seconds
ca.rule(ts.choose(1, rules)[0]);
}
// generate the next generations of Cellular Automaton
gens.unshift(ca.next());
// cut of the last gen to fit in the frame
gens = gens.slice(0, cols);
// draw all the squares
for (let g in gens){
for (let i=0; i<cols; i++){
// if the cell is alive, draw it
if (gens[g][i] === 1){
rect(posX[i], posY[g], size);
}
}
}
}