-
Notifications
You must be signed in to change notification settings - Fork 0
/
Anim.js
100 lines (81 loc) · 2.77 KB
/
Anim.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
class Anim { // Animation
constructor(value, route, colour) {
this.value = value
this.route = route
this.percent = 0
this.colour = colour
this.start = null
this.x = null
this.y = null
this.goal = null
this.begin = null
this.axis = null
// Emergency measure - if the animation gets stuck or cannot reach goal for whatever reason, are too long has passed it will be skipped
this.timeout = setTimeout(function() {
animCon.animationComplete()
}.bind(this), 200000 / animCon.speed) // if the animation gets stuck, the timeout will make sure it doesnt halt the program
this.nextStep() // start the animation
}
update() { // once a frame
fill(this.colour)
rectMode(CENTER)
textAlign(CENTER, CENTER)
this.begin = [this.start.x, this.start.y]
if (this.axis === "x") { // if moving horizontal
// find out how much percent has been done
let diff = abs(this.goal.x - this.begin[0])
this.percent += 0.7 / diff * (animCon.speed * 20)
if (this.percent > 100) {
this.percent = 100
}
// update the x position to the percent (works even if speed is changed)
this.x = this.begin[0] + (this.goal.x - this.begin[0]) / 100 * this.percent
// display animtion
rect(this.x, this.goal.y, pointW * 0.9, pointH * 0.9)
fill(255)
text(this.value, this.x, this.goal.y)
// if goal reached, do next step
if (round(this.x) === round(this.goal.x)) {
this.percent = 0
this.nextStep()
}
} else {
let diff = abs(this.goal.y - this.begin[1])
this.percent += 0.7 / diff * (animCon.speed * 20)
if (this.percent > 100) {
this.percent = 100
}
this.y = this.begin[1] + (this.goal.y - this.begin[1]) / 100 * this.percent
rect(this.goal.x, this.y, pointW * 0.9, pointH * 0.9)
fill(255)
text(this.value, this.goal.x, this.y)
if (round(this.y) === round(this.goal.y)) {
this.percent = 0
this.nextStep()
}
}
textAlign(LEFT, TOP)
rectMode(CORNER)
}
getAxis() {
if (this.start.x === this.goal.x) {
return "y"
} else {
return "x"
}
}
nextStep() { // runs at every node reached
// check if all steps are done
if (this.route.length === 1) {
clearTimeout(this.timeout)
return animCon.animationComplete()
}
// reset values and start next step
this.start = this.route[0]
this.route.shift()
this.x = this.start.x
this.y = this.start.y
this.goal = this.route[0]
this.axis = this.getAxis()
}
}