Published
Edited
Nov 10, 2019
2 forks
3 stars
Insert cell
Insert cell
draw = {
let exp = init();

function update() {
exp = gUpdate2(exp);
gShow(exp);
return gShow.getCanvas();
}
while (true) {
yield Promises.delay(2, update());
}
}
Insert cell
init = function(){
let grid = new Array(640).fill(0).map(() => new Array(640).fill(0));
for (let i = 0; i < 640; i++) {
for (let j = 0; j < 640; j++) {
//Q: Why 3.1? A: Why not?
grid[i][j] = Math.floor(Math.random() * 3.1);
}
}
return grid;
}
Insert cell
Insert cell
gUpdate1 = gpu.createKernel(function(grid){
var sum = 0;
for (var i = -1; i <= 1; i++) {
var p = (this.thread.y + i + 600) % 600;
for (var j = -1; j <= 1; j++) {
var q = (this.thread.x + j + 600) % 600;
sum += grid[p][q];
}
}
sum /= 9;
sum += 0.05;
sum = Math.sin(sum*3.5);
return sum;
}).setOutput([600, 600]);
Insert cell
Insert cell
gUpdate2 = gpu.createKernel(function(grid){
var sum = 0;
for (var i = -1; i <= 1; i++) {
var p = (this.thread.y + i + 640) % 640;
for (var j = -1; j <= 1; j++) {
var q = (this.thread.x + j + 640) % 640;
sum += grid[p][q];
}
}
sum /= 9;

sum += 0.05;
sum = Math.sin(sum*2.8);
sum = Math.sign(sum)*1.08+(Math.random()*0.1408);
return sum;
}).setOutput([640, 640])
Insert cell
md`
### Another update function: Doom Circuit
`
Insert cell
gUpdate3 = gpu.createKernel(function(grid){
var sum = 0;
for (var i = -1; i <= 1; i++) {
var p = (this.thread.y + i + 640) % 640;
for (var j = -1; j <= 1; j++) {
var q = (this.thread.x + j + 640) % 640;
sum += grid[p][q];
}
}
sum /= 9;
sum -= 0.002;

sum = Math.sin(sum*2.725);
sum = Math.sign(sum)*1.075+(Math.random()*0.1410);

return sum;
}).setOutput([640, 640])
Insert cell
// Color values chosen here to turn the CA a SCARY GREEN!
gShow = gpu.createKernel(function(grid){
this.color(
grid[this.thread.x][grid.thread.y]*0.05,
grid[this.thread.x][grid.thread.y]*0.65,
grid[this.thread.x][grid.thread.y]*0.35,
1
);
}).setOutput([640, 640]).setGraphical(true);
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more