Skip to content

Instantly share code, notes, and snippets.

@jaju
Created March 6, 2025 16:43
Show Gist options
  • Save jaju/bd0574dffb8d2cb4ad3ff770ba657021 to your computer and use it in GitHub Desktop.
Save jaju/bd0574dffb8d2cb4ad3ff770ba657021 to your computer and use it in GitHub Desktop.
p5js code created with Qwen QwQ-32B using llama.cpp
// Prompt: "Create an amazing animation using p5js"
let particles = [];
let center;
let centerDriftSpeed = 0.05;
function setup() {
createCanvas(windowWidth, windowHeight);
center = createVector(width/2, height/2);
colorMode(HSB, 255); // Use HSB color mode
}
function draw() {
background(0, 5); // Transparent black with alpha 5
blendMode(ADD); // Additive blending for bright trails
// Update center position
if (mouseIsPressed) {
center.x = mouseX;
center.y = mouseY;
} else {
center.x = lerp(center.x, width/2, centerDriftSpeed);
center.y = lerp(center.y, height/2, centerDriftSpeed);
}
// Spawn new particles every 5 frames
if (frameCount % 5 === 0) {
particles.push(new Particle());
}
// Update and draw particles
for (let i = particles.length-1; i >= 0; i--) {
particles[i].update(center);
particles[i].display();
if (particles[i].isDead()) {
particles.splice(i, 1); // Remove dead particles
}
}
}
class Particle {
constructor() {
// Create random spawn angle and radius near current center
let angle = random(TWO_PI);
let radius = random(0, 100);
this.pos = createVector(
center.x + radius * cos(angle),
center.y + radius * sin(angle)
);
// Initial velocity in random direction
this.vel = p5.Vector.fromAngle(angle, random(0.5, 2.0));
// Color based on angle for rainbow effect
this.hue = map(angle, 0, TWO_PI, 0, 255);
this.sat = 255; // Full saturation
this.bri = 255; // Full brightness
this.size = random(1, 3);
this.lifespan = 255; // Max lifespan
}
update(center) {
// Vector pointing towards the current center
let dir = p5.Vector.sub(center, this.pos);
let distance = dir.mag();
// Normalize direction vector
dir.normalize();
// Radial acceleration towards center (gravity effect)
let radialAcc = dir.mult(0.01);
// Tangential acceleration for rotation
let tangent = dir.copy().rotate(HALF_PI); // Perpendicular to radial
tangent.normalize();
let tangentAcc = tangent.mult(0.05); // Adjust strength here
// Apply forces to velocity
this.vel.add(radialAcc);
this.vel.add(tangentAcc);
// Update position
this.pos.add(this.vel);
// Decrease lifespan and size over time
this.lifespan -= 3;
this.size *= 0.98;
}
display() {
noStroke();
// Use HSB color with alpha from lifespan
fill(this.hue, this.sat, this.bri, this.lifespan);
ellipse(this.pos.x, this.pos.y, this.size, this.size);
}
isDead() {
return (
this.lifespan < 0 ||
this.pos.dist(center) > 300 ||
this.size < 0.5
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment