Public
Edited
May 9, 2023
Insert cell
Insert cell
Insert cell
function draw(p8g){
const { background, ellipse, fill, noSmooth, noStroke, stroke, strokeWeight, rectMode, CENTER, CORNER, point} = p8g;
const {sin, cos, tan, abs, random} = Math;
// setup code
if( window.setup == true){
window.setup = false // gets reset when cancelAnimationFrame() is called in createCanvas()
window.tick = 0
}

// noStroke()
noSmooth()
background(0,0,70,5) // '#00004605'
for (let iteration = 2e4; iteration--;) {

// Calculate the value of p using trigonometric functions and time
let angle = tan(tan(iteration + sin(window.tick / 1e9)) * iteration)

// Calculate the values of x, y, and z using trigonometric functions and p
let z = sin(iteration) / 40
let x = v + sin(angle ) / z
let y = v + cos(angle ) / z

let scaled = 0.35
let circleSize = (angle % 85 / 8 + 1) * scaled;
strokeWeight(scaled)
ellipse(x, y, circleSize, circleSize );
}
window.tick += 0.05
}

Insert cell
window.setup
Insert cell
w = width
Insert cell
h = w
Insert cell
v = w/2
Insert cell
Insert cell
// Set tick to 0 initially for animation
window.tick = 0
Insert cell
{
boom
window.setup = true
}
Insert cell
// setup and draw are both passed a `p8g` object.
// This means you can use destructuring assignment
// in the parameters to avoid having to write
// `p8g.fill`, `p8g.rect`, etc.
//
// Note that unlike P5, `fill`, `stroke` and other
// settings are not kept between draw calls,
// so initializing those once in setup won't work!
// The reason for this is that p8g does not have
// its own `setup` function, just the `draw` loop.
function setup(p8g) {
// initiate background colour
p8g.background(0x00, 0x00, 0x36); // 000046
}
Insert cell
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.

createCanvas = {
const p8g = await import('https://cdn.skypack.dev/p8g.js@0.8.3?min');

let canvas = null;
let _draw = () => {};
let _setup = () => {};
let request = null;
let state = null;
return async function createCanvas(width, height, draw = _draw, setup = _setup, opts = {}) {

const {
css
} = opts
// cancel previous loop
if (request) cancelAnimationFrame(request);

if(typeof _setup !== "function") throw new Error("setup is not a function!");
if(typeof _draw !== "function") throw new Error("draw is not a function!")

_setup = setup;
_draw = draw;

if (!canvas) {
canvas = await html`${p8g.createCanvas(width, height)}`;
}
if (typeof css === "string") canvas.style = css;

state = setup(p8g);

// Draw Loop
request = requestAnimationFrame(function tick() {
draw(p8g, state);
request = requestAnimationFrame(tick);
});

invalidation.then(() => cancelAnimationFrame(request));

return canvas;
}
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more