forked from kasznar/wobbly-animation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspring.js
60 lines (50 loc) · 1.44 KB
/
spring.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
55
56
57
58
59
60
function Spring(pointA, pointB, length, stiffness, offsetX, offsetY) {
this.pointA = pointA;
this.pointB = pointB;
this.length = length;
this.stiffness = stiffness;
this.offsetX = offsetX;
this.offsetY = offsetY;
this.pointA.addSpring(this);
this.pointB.addSpring(this);
this.pop = function () {
pointA.springs = pointA.springs.filter(value => value !== this)
pointB.springs = pointB.springs.filter(value => value !== this)
}
this.forceXOn = function (point) {
if (point === pointA) {
return this.forceAX();
}
if (point === pointB) {
return this.forceBX();
}
return 0;
}
this.forceYOn = function (point) {
if (point === pointA) {
return this.forceAY();
}
if (point === pointB) {
return this.forceBY();
}
return 0;
}
this.forceBX = function () {
return (this.pointA.x - this.pointB.x - this.offsetX) * this.stiffness;
}
this.forceBY = function () {
return (this.pointA.y - this.pointB.y - this.offsetY) * this.stiffness;
}
this.forceAX = function () {
return -this.forceBX();
}
this.forceAY = function () {
return -this.forceBY();
}
this.display = function () {
push();
stroke(255);
line(this.pointA.x, this.pointA.y, this.pointB.x, this.pointB.y);
pop();
};
}