xxxxxxxxxx
75
// https://editor.p5js.org/lkalbin/sketches/njIIfxyXX
// Original sketch by https://lindseyalbin.com
let particles = [];
let buffer;
let font;
function preload() {
font = loadFont("Smokum-Regular.ttf");
}
class Particle {
constructor(x, y) {
this.pos = createVector(x, y)
this.origPos = createVector(x, y)
}
draw() {
let target;
let mouseDist = dist(mouseX, mouseY, this.origPos.x, this.origPos.y);
if (mouseDist > 100) {
// If the mouse is too far, the particle should go to the original XY position.
target = this.origPos
} else {
// If the mouse is close enough, the particle should move 100 pixels
// away from the mouse.
let pushVector = createVector(this.origPos.y - mouseX, this.origPos.x - mouseY)
pushVector.setMag(0.5)
target = p5.Vector.add(this.origPos, pushVector)
}
// Move towards the target
let vel = p5.Vector.sub(target, this.pos)
vel.setMag(vel.mag() * cos(frameCount*0.1)*1)
this.pos = this.pos.add(vel)
// noFill();
fill('lightgreen');
textSize(15);
text("GLITCH", this.pos.x, this.pos.y);
}
}
function setup() {
createCanvas(windowWidth, windowHeight);
for (let i = 0; i < 500; i++) {
particles.push (new Particle(random(width), random(height)))
}
for (let x = 0; x < width; x+=100) {
for (let y = 7; y < height; y+=250) {
particles.push(new Particle(x, y))
}
}
}
function draw() {
push();
background(random(2550),random(100),random(255),20);
for (let i = 0; i < particles.length; i++) {
particles[i].draw();
}
}