Published
Edited
Oct 12, 2021
Fork of Pixel
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
if (window.p_inst !== undefined) {
window.p_inst.remove();
}

window.p_inst = new P5(
(p) => {
p.setup = setup(p);
p.draw = draw(p);
},
document.querySelector("#view"),
true
);

return "Succeed";
}
Insert cell
function setup(p) {
return () => {
p.createCanvas(640, 640); // updatePixels not work in WEBGL mode
p.pixelDensity(1);
};
}
Insert cell
function draw(p) {
let rx = 200;
let ry = 200;
const radius = 100;

let deltax = 5;
let deltay = 4;

return () => {
mutable Output = p.frameCount;
rx += deltax;
if (rx > 400) {
deltax = -p.random(5);
}

if (rx < 127) {
deltax = p.random(4);
}

ry += deltay;
if (ry > 400) {
deltay = -p.random(6);
}

if (ry < 127) {
deltay = p.random(6);
}

p.background(255);
p.loadPixels();

for (var y = 0; y < p.height; y++) {
for (var x = 0; x < p.width; x++) {
const distance = (x - rx) * (x - rx) + (y - ry) * (y - ry);

var index = (x + y * p.width) * 4;
if (distance <= radius * radius) {
p.pixels[index + 0] = (2 * x) % 255;
p.pixels[index + 1] = (3 * y) % 255;
p.pixels[index + 2] = (2 * x + y) % 255;
} else {
p.pixels[index + 0] = 255;
p.pixels[index + 1] = 255;
p.pixels[index + 2] = 255;
}
p.pixels[index + 3] = 255; // alpha
}
}

p.updatePixels();
};
}
Insert cell
Insert cell
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