-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathp5.timer.js
More file actions
138 lines (120 loc) · 4.57 KB
/
p5.timer.js
File metadata and controls
138 lines (120 loc) · 4.57 KB
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
/*******************************************************************************************************************
//
// Class: Timer
// for P5.js
//
// Written by Scott Kildall
// Modified by Alina Xia
//
//------------------------------------------------------------------------------------------------------------------
// - Very simple but incredibly useful timer class
// - Call start() whenever it expires to reset the time
// - Call expired() to check to see if timer is still active
//------------------------------------------------------------------------------------------------------------------
// Constructor: requires a timer duration, this can always be changed with setTimer()
//------------------------------------------------------------------------------------------------------------------
// --> Additions + Mods by Alina
// new Timer(_duration, start) now creates a timer with an option to start immediately or not. default does not start.
// reset() covers the old start() functionality
// pause() pauses the timer, allowing it to be restarted when needed.
// addTime(x) adds x millis to the remaining duration. it does not modify the original duration.
// --> Can also use to subtract time w/ neg number
// endTimer() forcibly ends the timer (will return true when expired() called);
// --> Modifications:
// start() -- now starts ONLY IF timer not currently running
// --> starts if paused by pause() or timer expired
*********************************************************************************************************************/
class Timer {
// Store the duration and start the timer
constructor( _duration, start = false ) {
this.startTime = millis();
this.duration = _duration;
this.paused = false;
this.pauseStartTime = 0; // this will be millis() after the pause()
this.totalPauseTime = 0; // so we can make accurate calculations
}
// MAIN FUNCTIONS
//starts the timer if it was expired or paused
start() {
if( this.expired() ) {
this.startTime = millis(); // restarts counting millis
}
else if( this.paused ) {
// unpauses, add paused amount to total pause tume
this.totalPauseTime += millis() - this.pauseStartTime;
this.paused = false;
}
}
// returns true if the timer is expired, e.g. if millis() is greater than startTime + duration
expired() {
return (this.startTime + this.duration + this.getPauseTime() ) < millis();
}
// restarts timer regardless of status
reset(){
this.startTime = millis();
this.totalPauseTime = 0;
if( this.paused ) {
this.pauseStartTime = this.startTime;
}
else {
this.pauseStartTime = 0;
}
}
// pauses the timer
pause(){
if( this.paused === false ) {
this.pauseStartTime = millis();
this.paused = true;
}
}
// adds x millis to the remaining duration.
addTime(x){
this.duration += x;
}
// set the duration, doesn't start the timer
setTimer(_duration) {
this.duration = _duration;
}
//forces an expired() state by setting remaining duration to zero.
endTimer(){
this.duration = 0;
}
// SIMPLE ACCESSORS
// accessor: returns true/false, indicating paused state
isPaused() {
return this.paused;
}
// returns remaining time in milliseconds, zero if timer is done
getRemainingTime() {
if( this.expired() ) {
return 0;
}
// never return a neg number + account for pause time
let rt = this.startTime + this.duration + this.getPauseTime() - millis();
if( rt < 0 )
rt = 0;
return rt;
}
// returns remaining % of timer, 0.0 through 1.0
getPercentageRemaining() {
if( this.duration === 0 ) {
return 0.0; // avoid div by zero error
}
if( this.expired() ) {
return 0.0;
}
return this.getRemainingTime()/this.duration;
}
// returns elapsed % of timer, 0.0 through 1.0
getPercentageElapsed() {
return 1.0 - this.getPercentageRemaining(); //refactor to remove duplication
}
// we use this to account for all paused actions: return active and past pause times
getPauseTime() {
let pauseTime = this.totalPauseTime;
if( this.paused ) {
pauseTime += (millis() - this.pauseStartTime);
}
return pauseTime;
}
}