forked from ItsSupercar/alttpr-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.js
145 lines (107 loc) · 3.39 KB
/
timer.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
startValue = moment.duration(0);
digits = { 7: 0, 6: 0, 5: 0, 4: 0, 3: 0, 2: 0, 1: 0 }
function checkTime() {
displayTime = startValue.clone().add(moment().diff(startTime));
var milliseconds = displayTime.milliseconds();
var seconds = displayTime.seconds();
var minutes = displayTime.minutes();
var hours = displayTime.hours();
if (milliseconds >= 100) {
scrollDigit(2, milliseconds.toString()[0]);
scrollDigit(1, milliseconds.toString()[1]);
} else if (milliseconds >= 10) {
scrollDigit(2, 0);
scrollDigit(1, milliseconds.toString()[0]);
} else {
scrollDigit(2, 0);
scrollDigit(1, 0);
}
if (seconds >= 10) {
scrollDigit(4, seconds.toString()[0]);
scrollDigit(3, seconds.toString()[1]);
} else {
scrollDigit(4, 0);
scrollDigit(3, seconds);
}
if (minutes >= 10) {
scrollDigit(6, minutes.toString()[0]);
scrollDigit(5, minutes.toString()[1]);
} else {
scrollDigit(6, 0);
scrollDigit(5, minutes);
}
if (hours <= 9) {
scrollDigit(7, hours);
}
fadeDigits();
}
function scrollDigit(digit, num) {
var elem = "#digit" + digit + " .digits";
var speed = (digit <= 2) ? 0.05 : 0.5;
var ease = (digit <= 2) ? Linear.easeNone : Back.easeInOut.config(1.4);
if (digits[digit] !== num && !TweenMax.isTweening(elem)) {
if (num == 0) {
pos = (digit == 6 || digit == 4) ? -600 : -1000;
TweenMax.to(elem, speed, {
y: pos, ease: ease, onComplete: function () {
TweenMax.set("#digit" + digit + " .digits", { y: 0 });
}
});
} else {
TweenMax.to(elem, speed, { y: (-100 * num), ease: ease });
}
}
digits[digit] = num;
}
function fadeDigits() {
var blank = true;
var blankCount = 0;
var speed = 0.5;
for (i = 7; i >= 4; i--) {
if (digits[i] !== 0) { blank = false; }
TweenMax.to("#digit" + i, speed, { opacity: (blank ? 0 : 1) });
TweenMax.to("#colon" + i, speed, { opacity: (blank ? 0 : 1) });
if (blank) { blankCount++; }
}
if (typeof(prevCount) == 'undefined' || prevCount !== blankCount) {
xOffset = blankCount == 4 ?
-86.5 :
blankCount == 3 ?
-66.5 :
blankCount == 2 ?
-35.5 :
blankCount == 1 ?
-16.5 :
14
;
if (!TweenMax.isTweening("#timerWrap")) {
TweenMax.to("#timerWrap .timerDigit,.colon", speed, { x: xOffset, ease: Back.easeOut.config(3) });
}
}
prevCount = blankCount;
}
timerPos = 0;
function startTimer() {
startTime = moment();
timerUpdate = setInterval(function () { checkTime(); }, 10);
}
function pauseTimer() {
startValue = displayTime;
if (typeof (timerUpdate) !== 'undefined') {
clearInterval(timerUpdate);
}
}
function resetTimer() {
var speed = 0.5;
pauseTimer();
startValue = moment.duration(0);
running = false;
digits = { 7: 0, 6: 0, 5: 0, 4: 0, 3: 0, 2: 0, 1: 0 };
TweenMax.to(".digits", speed, { y: 0, ease: Back.easeInOut.config(1.4) });
fadeDigits();
}
running = false;
function toggleTimer() {
running = !running;
if (running) { startTimer(); } else { pauseTimer(); }
}