forked from kasznar/wobbly-animation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.js
54 lines (45 loc) · 1.29 KB
/
point.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function Point(x, y) {
this.nextX = x;
this.nextY = y;
this.springs = []
this.x = x;
this.y = y;
this.vx = 0;
this.vy = 0;
this.isAnchor = false;
this.addSpring = function(spring){
this.springs.push(spring)
}
this.clearSprings = function(){
springs.forEach(element => {
element.pop()
});
}
this.calculateNextPosition = function(){
if (this.isAnchor) {
this.vx = 0;
this.vy = 0;
this.nextX = mouseX - width / 2;
this.nextY = mouseY - height / 2;
} else {
let forceX = this.springs.reduce((sum, spring) => sum + spring.forceXOn(this), 0)
let ax = forceX / mass;
this.vx = damping * (this.vx + ax);
this.nextX = this.x + this.vx;
let forceY = this.springs.reduce((sum, spring) => sum + spring.forceYOn(this), 0)
let ay = forceY / mass;
this.vy = damping * (this.vy + ay);
this.nextY = this.y + this.vy;
}
}
this.update = function(){
this.x = this.nextX;
this.y = this.nextY;
}
this.display = function(index) {
push();
noStroke();
ellipse(this.x, this.y, radius * 2, radius * 2);
pop();
};
}