Public
Edited
Oct 18, 2022
1 fork
Insert cell
Insert cell
// p8g can currently only initialize a WebGL2 canvas once.
// That means it can easily conflict with Observable's reactivity.
// To avoid that, use `createCanvas` to create a p8g canvas ONCE
// and don't use any reactivity in this cell
createCanvas(w, h)
Insert cell
function draw(){

const {
background,
noStroke,
fill,
ellipse
} = p8g;

const {sin, cos, PI, pow, abs} = Math;
const TAU = PI * 2

t[0]+=.01;
background(0);
for(let n=1;n<55;n++){
for(let i=0;i<S;i++){
let p=i/S;
let z=noise(n,0);
let b=noise(n+sin(TAU*(p-t[0]+z),cos(TAU*(p-t[0]+z)),p))*pow(sin(TAU*p),2);
noStroke();
fill(255,30);
ellipse(w*p+300*pow(b,2),9*n+150*b,4,6);
}
}
}
Insert cell
S = 1200
Insert cell
w = width
Insert cell
h=600
Insert cell
t=[0]
Insert cell
Insert cell
import {noise} from "@hellonearthis/noise-like-p5js"

Insert cell
Insert cell
Insert cell
async function createCanvas(width, height) {
return html`${p8g.createCanvas(width, height)}`;
}
Insert cell
// The normal p8g approach would be to assign a draw function to
// the p8g object, but because it's imported through skypack
// at runtime the p8g module is read-only. So instead we use
// this workaround. As a bonus, we can write the draw() function
// as a named cell.
p8g_readonly_workaround = {
await p8g;
// Draw Loop
let request = requestAnimationFrame(function tick() {
draw();
request = requestAnimationFrame(tick);
});

invalidation.then(() => cancelAnimationFrame(request));
}
Insert cell
p8g = await import('https://cdn.skypack.dev/p8g.js@0.8.1?min');
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