xxxxxxxxxx
let font;
let particles = []
function preload() {
font = loadFont('Montserrat-VariableFont_wght.ttf');
}
function setup() {
createCanvas(windowWidth, windowHeight);
background(200);
// Get the point array.
let points = font.textToPoints('p5*js', width/3, height/2, 125, { sampleFactor: 0.5 });
// Draw a dot at each point.
for (let p of points) {
particles.push(new Particle(p.x, p.y));
}
}
function draw(){
background(220);
for (let i = 0; i < particles.length; i++) {
particles[i].draw();
}
}
class Particle{
constructor(x, y) {
// this.pos = createVector(width/2, height/2);
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 > 150){
target = this.origPos;
} else{
let pushVector = createVector (this.origPos.x-mouseX, this.origPos.y-mouseY);
pushVector.setMag(400);
target = p5.Vector.add(this.origPos, pushVector);
}
let vel = p5.Vector.sub(target, this.pos);
vel.setMag(vel.mag() * 0.05);
this.pos = this.pos.add(vel);
// text("before", this.pos.x, this.pos.y)
textAlign(CENTER,CENTER);
vel.setMag(vel.mag() * 0.05);
this.pos = this.pos.add(vel);
circle(this.pos.x, this.pos.y,20)
}
}