Skip to content

Commit 64d1775

Browse files
committed
Update Timer API
1 parent 509db64 commit 64d1775

File tree

3 files changed

+63
-195
lines changed

3 files changed

+63
-195
lines changed

src/core/EffectComposer.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,8 @@ export class EffectComposer {
592592

593593
if(deltaTime === undefined) {
594594

595-
deltaTime = this.timer.update().getDelta();
595+
this.timer.update();
596+
deltaTime = this.timer.delta;
596597

597598
}
598599

@@ -685,10 +686,10 @@ export class EffectComposer {
685686

686687
reset() {
687688

688-
const autoReset = this.timer.isAutoResetEnabled();
689+
const autoReset = this.timer.autoReset;
689690
this.dispose();
690691
this.autoRenderToScreen = true;
691-
this.timer.setAutoResetEnabled(autoReset);
692+
this.timer.autoReset = autoReset;
692693

693694
}
694695

src/core/Timer.js

Lines changed: 39 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,35 @@ export class Timer {
4242
*
4343
* @type {Number}
4444
* @private
45+
* @see {@link delta}
4546
*/
4647

47-
this.delta = 0;
48+
this._delta = 0;
4849

4950
/**
50-
* The fixed time step in milliseconds.
51-
*
52-
* Default is 16.667 (60 fps).
51+
* The total elapsed time in milliseconds.
5352
*
5453
* @type {Number}
5554
* @private
55+
* @see {@link elapsed}
5656
*/
5757

58-
this.fixedDelta = 1000.0 / 60.0;
58+
this._elapsed = 0;
5959

6060
/**
61-
* The total elapsed time in milliseconds.
61+
* The fixed time step in milliseconds. Default is 16.667 (60 fps).
6262
*
6363
* @type {Number}
6464
* @private
65+
* @see {@link fixedDelta}
6566
*/
6667

67-
this.elapsed = 0;
68+
this._fixedDelta = 1000.0 / 60.0;
6869

6970
/**
7071
* The timescale.
7172
*
7273
* @type {Number}
73-
* @private
7474
*/
7575

7676
this.timescale = 1.0;
@@ -79,65 +79,41 @@ export class Timer {
7979
* Determines whether this timer should use a fixed time step.
8080
*
8181
* @type {Boolean}
82-
* @private
8382
*/
8483

85-
this.fixedDeltaEnabled = false;
84+
this.useFixedDelta = false;
8685

8786
/**
88-
* Indicates whether auto reset is enabled.
89-
*
9087
* @type {Boolean}
9188
* @private
89+
* @see {@link autoReset}
9290
*/
9391

94-
this.autoReset = false;
92+
this._autoReset = false;
9593

9694
}
9795

9896
/**
99-
* Enables or disables the fixed time step.
97+
* Enables or disables auto reset based on page visibility.
10098
*
101-
* @param {Boolean} enabled - Whether the fixed delta time should be used.
102-
* @return {Timer} This timer.
103-
*/
104-
105-
setFixedDeltaEnabled(enabled) {
106-
107-
this.fixedDeltaEnabled = enabled;
108-
return this;
109-
110-
}
111-
112-
/**
113-
* Indicates whether auto reset is enabled.
99+
* If enabled, the timer will be reset when the page becomes visible. This effectively pauses the timer when the page
100+
* is hidden. Has no effect if the API is not supported.
114101
*
115-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API}
116-
* @return {Boolean} Whether the timer will be reset on visibility change.
102+
* @type {Boolean}
103+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API
117104
*/
118105

119-
isAutoResetEnabled(enabled) {
106+
get autoReset() {
120107

121-
return this.autoReset;
108+
return this._autoReset;
122109

123110
}
124111

125-
/**
126-
* Enables or disables auto reset based on page visibility.
127-
*
128-
* If enabled, the timer will be reset when the page becomes visible. This effectively pauses the timer when the page
129-
* is hidden. Has no effect if the API is not supported.
130-
*
131-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API}
132-
* @param {Boolean} enabled - Whether the timer should be reset on visibility change.
133-
* @return {Timer} This timer.
134-
*/
135-
136-
setAutoResetEnabled(enabled) {
112+
set autoReset(value) {
137113

138114
if(typeof document !== "undefined" && document.hidden !== undefined) {
139115

140-
if(enabled) {
116+
if(value) {
141117

142118
document.addEventListener("visibilitychange", this);
143119

@@ -147,141 +123,74 @@ export class Timer {
147123

148124
}
149125

150-
this.autoReset = enabled;
126+
this._autoReset = value;
151127

152128
}
153129

154-
return this;
155-
156130
}
157131

158-
/**
159-
* Returns the delta time.
160-
*
161-
* @return {Number} The delta time in seconds.
162-
*/
132+
get delta() {
163133

164-
getDelta() {
165-
166-
return this.delta * MILLISECONDS_TO_SECONDS;
134+
return this._delta * MILLISECONDS_TO_SECONDS;
167135

168136
}
169137

170-
/**
171-
* Returns the fixed delta time.
172-
*
173-
* @return {Number} The fixed delta time in seconds.
174-
*/
175-
176-
getFixedDelta() {
138+
get fixedDelta() {
177139

178-
return this.fixedDelta * MILLISECONDS_TO_SECONDS;
140+
return this._fixedDelta * MILLISECONDS_TO_SECONDS;
179141

180142
}
181143

182-
/**
183-
* Sets the fixed delta time.
184-
*
185-
* @param {Number} fixedDelta - The delta time in seconds.
186-
* @return {Timer} This timer.
187-
*/
188-
189-
setFixedDelta(fixedDelta) {
190-
191-
this.fixedDelta = fixedDelta * SECONDS_TO_MILLISECONDS;
192-
return this;
193-
194-
}
195-
196-
/**
197-
* Returns the elapsed time.
198-
*
199-
* @return {Number} The elapsed time in seconds.
200-
*/
201-
202-
getElapsed() {
203-
204-
return this.elapsed * MILLISECONDS_TO_SECONDS;
205-
206-
}
207-
208-
/**
209-
* Returns the timescale.
210-
*
211-
* @return {Number} The timescale.
212-
*/
213-
214-
getTimescale() {
144+
set fixedDelta(value) {
215145

216-
return this.timescale;
146+
this._fixedDelta = value * SECONDS_TO_MILLISECONDS;
217147

218148
}
219149

220-
/**
221-
* Sets the timescale.
222-
*
223-
* @param {Number} timescale - The timescale.
224-
* @return {Timer} This timer.
225-
*/
150+
get elapsed() {
226151

227-
setTimescale(timescale) {
228-
229-
this.timescale = timescale;
230-
return this;
152+
return this._elapsed * MILLISECONDS_TO_SECONDS;
231153

232154
}
233155

234156
/**
235157
* Updates this timer.
236158
*
237-
* @param {Number} [timestamp] - The current time in milliseconds.
238-
* @return {Timer} This timer.
159+
* @param {Boolean} [timestamp] - The current time in milliseconds.
239160
*/
240161

241162
update(timestamp) {
242163

243-
if(this.fixedDeltaEnabled) {
164+
if(this.useFixedDelta) {
244165

245-
this.delta = this.fixedDelta;
166+
this._delta = this.fixedDelta;
246167

247168
} else {
248169

249170
this.previousTime = this.currentTime;
250171
this.currentTime = (timestamp !== undefined) ? timestamp : performance.now();
251-
this.delta = this.currentTime - this.previousTime;
172+
this._delta = this.currentTime - this.previousTime;
252173

253174
}
254175

255-
this.delta *= this.timescale;
256-
this.elapsed += this.delta;
257-
258-
return this;
176+
this._delta *= this.timescale;
177+
this._elapsed += this._delta;
259178

260179
}
261180

262181
/**
263182
* Resets this timer.
264-
*
265-
* @return {Timer} This timer.
266183
*/
267184

268185
reset() {
269186

270-
this.delta = 0;
271-
this.elapsed = 0;
187+
this._delta = 0;
188+
this._elapsed = 0;
272189
this.currentTime = performance.now();
273190

274-
return this;
275-
276191
}
277192

278-
/**
279-
* Handles events.
280-
*
281-
* @param {Event} event - The event.
282-
*/
283-
284-
handleEvent(event) {
193+
handleEvent(e) {
285194

286195
if(!document.hidden) {
287196

@@ -292,13 +201,9 @@ export class Timer {
292201

293202
}
294203

295-
/**
296-
* Disposes this timer.
297-
*/
298-
299204
dispose() {
300205

301-
this.setAutoResetEnabled(false);
206+
this.autoReset = false;
302207

303208
}
304209

0 commit comments

Comments
 (0)