const MAX_PARTICLE_COUNT = 70;
const MAX_TRAIL_COUNT = 30;
var colorScheme = ["#E69F66", "#DF843A", "#D8690F", "#B1560D", "#8A430A"];
theShader = new p5.Shader(this.renderer, vertShader, fragShader);
let canvas = createCanvas(
min(windowWidth, windowHeight),
min(windowWidth, windowHeight),
canvas.canvas.oncontextmenu = () => false;
shaderTexture = createGraphics(width, height, WEBGL);
shaderTexture.noStroke();
trail.push([mouseX, mouseY]);
if (mouseIsPressed && mouseButton == CENTER) {
for (let i = 0; i < removeCount; i++) {
if (mouseIsPressed || trail.length > MAX_TRAIL_COUNT) {
if (trail.length > 1 && particles.length < MAX_PARTICLE_COUNT) {
let mouse = new p5.Vector(mouseX, mouseY);
mouse.sub(pmouseX, pmouseY);
particles.push(new Particle(pmouseX, pmouseY, mouse.x, mouse.y));
translate(-width / 2, -height / 2);
for (let i = particles.length - 1; i > -1; i--) {
if (particles[i].vel.mag() < 0.1) {
shaderTexture.shader(theShader);
let data = serializeSketch();
theShader.setUniform("resolution", [width, height]);
theShader.setUniform("trailCount", trail.length);
theShader.setUniform("trail", data.trails);
theShader.setUniform("particleCount", particles.length);
theShader.setUniform("particles", data.particles);
theShader.setUniform("colors", data.colors);
shaderTexture.rect(0, 0, width, height);
rect(0, 0, width, height);
for (let i = 0; i < particles.length; i++) {
point(particles[i].pos.x, particles[i].pos.y);
for (let i = 0; i < trail.length; i++) {
point(trail[i][0], trail[i][1]);
function mousePressed() {
if (mouseButton == RIGHT) {
function serializeSketch() {
data = {"trails": [], "particles": [], "colors": []};
for (let i = 0; i < trail.length; i++) {
map(trail[i][0], 0, width, 0.0, 1.0),
map(trail[i][1], 0, height, 1.0, 0.0));
for (let i = 0; i < particles.length; i++) {
map(particles[i].pos.x, 0, width, 0.0, 1.0),
map(particles[i].pos.y, 0, height, 1.0, 0.0),
particles[i].mass * particles[i].vel.mag() / 100)
let itsColor = colorScheme[particles[i].colorIndex];
data.colors.push(red(itsColor), green(itsColor), blue(itsColor));