You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/core/Runner.js
+38-44
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,9 @@
1
1
/**
2
-
* Pre-release beta version.
3
-
*
4
-
* The `Matter.Runner` module is a lightweight, optional utility which provides a game loop.
5
-
* It is intended for development and debugging purposes inside a browser environment.
6
-
* It will continuously update a `Matter.Engine` with a given fixed timestep whilst synchronising updates with the browser frame rate.
2
+
* The `Matter.Runner` module is an optional utility that provides a game loop for running a `Matter.Engine` inside a browser environment.
3
+
* A runner will continuously update a `Matter.Engine` whilst synchronising engine updates with the browser frame rate.
7
4
* This runner favours a smoother user experience over perfect time keeping.
8
-
* To directly step the engine as part of your own alternative game loop implementation, see `Engine.update`.
5
+
* This runner is optional and is used for development and debugging but could be useful as a starting point for implementing some games and experiences.
6
+
* Alternatively see `Engine.update` to step the engine directly inside your own game loop implementation as may be needed inside other environments.
9
7
*
10
8
* See the included usage [examples](https://github.com/liabru/matter-js/tree/master/examples).
11
9
*
@@ -30,7 +28,7 @@ var Common = require('./Common');
30
28
Runner._smoothingUpperBound=0.9;
31
29
32
30
/**
33
-
* Creates a new Runner.
31
+
* Creates a new Runner.
34
32
* See the properties section below for detailed information on what you can pass via the `options` object.
35
33
* @method create
36
34
* @param {} options
@@ -61,12 +59,9 @@ var Common = require('./Common');
61
59
};
62
60
63
61
/**
64
-
* Continuously updates a `Matter.Engine` on every browser frame whilst synchronising updates with the browser frame rate.
65
-
* It is intended for development and debugging purposes inside a browser environment.
66
-
* This runner favours a smoother user experience over perfect time keeping.
67
-
* The number of updates per frame is kept within performance budgets specified by `runner.maxFrameTime` and `runner.maxUpdates`.
68
-
* When device performance is too limited the simulation may appear to slow down compared to real time.
69
-
* As an alternative see `Engine.update` to directly step the engine in your own game loop implementation.
62
+
* Runs a `Matter.Engine` whilst synchronising engine updates with the browser frame rate.
63
+
* See module and properties descriptions for more information on this runner.
64
+
* Alternatively see `Engine.update` to step the engine directly inside your own game loop implementation.
70
65
* @method run
71
66
* @param {runner} runner
72
67
* @param {engine} [engine]
@@ -88,9 +83,9 @@ var Common = require('./Common');
88
83
};
89
84
90
85
/**
91
-
* Used by the game loop inside `Runner.run`.
92
-
*
93
-
* As an alternative see `Engine.update` to directly step the engine in your own game loop implementation.
86
+
* Performs a single runner tick as used inside `Runner.run`.
87
+
* See module and properties descriptions for more information on this runner.
88
+
* Alternatively see `Engine.update` to step the engine directly inside your own game loop implementation.
94
89
* @method tick
95
90
* @param {runner} runner
96
91
* @param {engine} engine
@@ -215,8 +210,7 @@ var Common = require('./Common');
215
210
};
216
211
217
212
/**
218
-
* Ends execution of `Runner.run` on the given `runner`, by canceling the frame loop.
219
-
*
213
+
* Ends execution of `Runner.run` on the given `runner` by canceling the frame loop.
220
214
* Alternatively to temporarily pause the runner, see `runner.enabled`.
221
215
* @method stop
222
216
* @param {runner} runner
@@ -226,7 +220,7 @@ var Common = require('./Common');
226
220
};
227
221
228
222
/**
229
-
* Schedules `callback` on this `runner` for the next animation frame.
223
+
* Schedules the `callback` on this `runner` for the next animation frame.
230
224
* @private
231
225
* @method _onNextFrame
232
226
* @param {runner} runner
@@ -244,7 +238,7 @@ var Common = require('./Common');
244
238
};
245
239
246
240
/**
247
-
* Cancels the last callback scheduled on this `runner` by `Runner._onNextFrame`.
241
+
* Cancels the last callback scheduled by `Runner._onNextFrame` on this `runner`.
248
242
* @private
249
243
* @method _cancelNextFrame
250
244
* @param {runner} runner
@@ -312,8 +306,8 @@ var Common = require('./Common');
312
306
*/
313
307
314
308
/**
315
-
* Fired before each and every update in this browser frame (if any).
316
-
* There may be multiple calls per browser frame (or none), depending on framerate and engine delta.
309
+
* Fired before each and every engine update in this browser frame (if any).
310
+
* There may be multiple engine update calls per browser frame (or none) depending on framerate and timestep delta.
317
311
*
318
312
* @event beforeUpdate
319
313
* @param {} event An event object
@@ -323,8 +317,8 @@ var Common = require('./Common');
323
317
*/
324
318
325
319
/**
326
-
* Fired after each and every update in this browser frame (if any).
327
-
* There may be multiple calls per browser frame (or none), depending on framerate and engine delta.
320
+
* Fired after each and every engine update in this browser frame (if any).
321
+
* There may be multiple engine update calls per browser frame (or none) depending on framerate and timestep delta.
328
322
*
329
323
* @event afterUpdate
330
324
* @param {} event An event object
@@ -340,21 +334,21 @@ var Common = require('./Common');
340
334
*/
341
335
342
336
/**
343
-
* The fixed timestep size used for `Engine.update` calls in milliseconds.
337
+
* The fixed timestep size used for `Engine.update` calls in milliseconds, known as `delta`.
344
338
*
345
-
* This `delta` value is recommended to be `1000 / 60` ms or smaller (i.e. equivalent to at least 60hz).
339
+
* This value is recommended to be `1000 / 60` ms or smaller (i.e. equivalent to at least 60hz).
346
340
*
347
341
* Smaller `delta` values provide higher quality results at the cost of performance.
348
342
*
349
-
* You should avoid changing `delta` during running, otherwise quality may be affected.
343
+
* You should usually avoid changing `delta` during running, otherwise quality may be affected.
350
344
*
351
-
* For smoothest results choose a `delta` using an integer multiple of display FPS, i.e. `1000 / (n * fps)` as this helps distribute an equal number of updates over each display frame.
345
+
* For smoother frame pacing choose a `delta` that is an even multiple of each display FPS you target, i.e. `1000 / (n * fps)` as this helps distribute an equal number of updates over each display frame.
352
346
*
353
-
* For example with a 60 Hz `delta` i.e. `1000 / 60` the runner will on average perform one update per frame on displays running 60 FPS, or one update every two frames on displays running 120 FPS, etc.
347
+
* For example with a 60 Hz `delta` i.e. `1000 / 60` the runner will on average perform one update per frame on displays running 60 FPS and one update every two frames on displays running 120 FPS, etc.
354
348
*
355
-
* Where as e.g. using a 240 Hz `delta` i.e. `1000 / 240` the runner will on average perform four updates per frame on displays running 60 FPS, or two updates per frame on displays running 120 FPS, etc.
349
+
* Where as e.g. using a 240 Hz `delta` i.e. `1000 / 240` the runner will on average perform four updates per frame on displays running 60 FPS and two updates per frame on displays running 120 FPS, etc.
356
350
*
357
-
* Therefore note that `Runner.run` can call multiple engine updates to simulate the time elapsed between frames, but the number of actual updates in any particular frame may be restricted by the runner's performance budgets.
351
+
* Therefore note that `Runner.run` can call multiple engine updates (or none) to simulate the time elapsed between browser frames, as well as the number of actual updates in any particular frame may be restricted to respect the runner's performance budgets.
358
352
*
359
353
* These performance budgets are specified by `runner.maxFrameTime` and `runner.maxUpdates`. See those properties for details.
360
354
*
@@ -364,7 +358,7 @@ var Common = require('./Common');
364
358
*/
365
359
366
360
/**
367
-
* A flag that can be toggled to enable or disable tick calls on this runner, therefore pausing engine updates while the runner loop remains running.
361
+
* A flag that can be toggled to enable or disable tick calls on this runner, therefore pausing engine updates and events while the runner loop remains running.
368
362
*
369
363
* @property enabled
370
364
* @type boolean
@@ -373,7 +367,7 @@ var Common = require('./Common');
373
367
374
368
/**
375
369
* The accumulated time elapsed that has yet to be simulated in milliseconds.
376
-
* This value is clamped within some limits (see `Runner.tick` code).
370
+
* This value is clamped within certain limits (see `Runner.tick` code).
377
371
*
378
372
* @private
379
373
* @property timeBuffer
@@ -383,41 +377,41 @@ var Common = require('./Common');
383
377
384
378
/**
385
379
* The measured time elapsed between the last two browser frames measured in milliseconds.
386
-
* You may use this to estimate the browser FPS (for the current frame) whilst running as `1000 / runner.frameDelta`.
380
+
* This is useful e.g. to estimate the current browser FPS using `1000 / runner.frameDelta`.
387
381
*
388
382
* @readonly
389
383
* @property frameDelta
390
384
* @type number
391
385
*/
392
386
393
387
/**
394
-
* Applies averaging to smooth frame rate measurements and therefore stabilise play rate.
388
+
* Enables averaging to smooth frame rate measurements and therefore stabilise play rate.
395
389
*
396
390
* @property frameDeltaSmoothing
397
391
* @type boolean
398
392
* @default true
399
393
*/
400
394
401
395
/**
402
-
* Rounds measured frame delta to the nearest 1 Hz.
403
-
* Your choice of `runner.delta` should be rounded to the nearest 1 Hz for best results.
404
-
* This option helps smooth frame rate measurements and unify display hardware differences e.g. 59.94Hz vs 60Hz refresh rates.
396
+
* Rounds measured browser frame delta to the nearest 1 Hz.
397
+
* This option can help smooth frame rate measurements and simplify handling hardware timing differences e.g. 59.94Hz and 60Hz displays.
398
+
* For best results you should also round your `runner.delta` equivalent to the nearest 1 Hz.
405
399
*
406
400
* @property frameDeltaSnapping
407
401
* @type boolean
408
402
* @default true
409
403
*/
410
404
411
405
/**
412
-
* A performance budget that limits execution time allowed for this runner per display frame in milliseconds.
406
+
* A performance budget that limits execution time allowed for this runner per browser frame in milliseconds.
413
407
*
414
-
* To calculate the display FPS at which this throttle is applied use `1000 / maxFrameTime`.
408
+
* To calculate the effective browser FPS at which this throttle is applied use `1000 / runner.maxFrameTime`.
415
409
*
416
410
* This performance budget is intended to help maintain browser interactivity and help improve framerate recovery during temporary high CPU usage.
417
411
*
418
-
* This only covers the measured time elapsed executing the functions called in the scope of the runner tick, including `Engine.update` etc. and its related user event callbacks.
412
+
* This budget only covers the measured time elapsed executing the functions called in the scope of the runner tick, including `Engine.update` and its related user event callbacks.
419
413
*
420
-
* You may wish to reduce this budget to allow for any significant additional processing you perform on the same thread outside the scope of this runner, e.g. rendering time.
414
+
* You may also reduce this budget to allow for any significant additional processing you perform on the same thread outside the scope of this runner tick, e.g. rendering time.
421
415
*
422
416
* See also `runner.maxUpdates`.
423
417
*
@@ -427,7 +421,7 @@ var Common = require('./Common');
427
421
*/
428
422
429
423
/**
430
-
* An optional hard limit for maximum engine updates allowed per frame tick in addition to `runner.maxFrameTime`.
424
+
* An optional limit for maximum engine update count allowed per frame tick in addition to `runner.maxFrameTime`.
431
425
*
432
426
* Unless you set a value it is automatically chosen based on `runner.delta` and `runner.maxFrameTime`.
433
427
*
@@ -439,7 +433,7 @@ var Common = require('./Common');
439
433
*/
440
434
441
435
/**
442
-
* The timestamp of the last call to `Runner.tick`, used to measure `frameDelta`.
436
+
* The timestamp of the last call to `Runner.tick` used to measure `frameDelta`.
0 commit comments