-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractiveMotion.js
More file actions
37 lines (32 loc) · 816 Bytes
/
Copy pathinteractiveMotion.js
File metadata and controls
37 lines (32 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
let mover;
function setup() {
createCanvas(1640, 1200);
mover = new InteractiveMover();
background(255);
}
function draw() {
mover.show();
mover.update();
}
class InteractiveMover {
constructor() {
this.position = createVector(width / 2, height / 2);
this.velocity = createVector(0, 0);
this.topSpeed = 2;
}
show() {
stroke(0);
fill(127);
circle(this.position.x, this.position.y, 48);
}
update() {
let mouse = createVector(mouseX, mouseY);
let dir = p5.Vector.sub(mouse, this.position);
dir.normalize();
dir.mult(0.2);
this.acceleration = dir;
this.velocity.add(this.acceleration);
this.velocity.limit(this.topSpeed);
this.position.add(this.velocity);
}
}