xxxxxxxxxx
42
var chaser;
function setup() {
createCanvas(720, 400);
noStroke();
chaser = new Chaser(width / 2, height / 2, 0.1);
}
function draw() {
background(237, 34, 93);
chaser.chase(mouseX, mouseY);
chaser.present();
}
class Chaser{
constructor(x, y, easing) {
this.ox = x;
this.oy = y;
this.x = x;
this.y = y;
this.easing = easing;
}
chase(targetX, targetY) {
var dx = targetX - this.x;
var dy = targetY - this.y;
var dist = sqrt(dx * dx + dy * dy);
if(dist > 32) {
this.x += dx * this.easing;
this.y += dy * this.easing;
}
else {
this.x = this.ox;
this.y = this.oy;
}
}
present() {
fill(255);
ellipse(this.x, this.y, 64);
}
}