- Disables optional features, and raises or lowers the DevicePixelRatio, depending on the current frames per second (FPS).
- Class. Create one with
new.
On every frame, once the breathing window has passed:
| Condition | Action |
|---|---|
FR < 30 & opt_to_disable |
Next optional feature in the list gets disabled |
FR < 30 & NO_opt_to_disable |
DPR reduced by 0.25, to a min of 0.75 |
30 ≤ FR < 60 & threshold disabled & DPR > 1 |
DPR reduced by 0.25 and threshold enabled |
FR ≥ 60 & threshold disabled |
DPR increased by 0.25, to a max of 1.25 × window.devicePixelRatio (min 1) |
FR => framerate,
FPSCounter.avgDPR => Device Pixel Ratio,
settings.dpr
Framerateis the average of 5 samples, each 1 second long.Thresholdenabled means the framerate is in the accepted range. It stops DPR increases and the30 ≤ FR < 60reduction, butFR < 30still acts. Once enabled it stays enabled.- A breathing window is set, checking for
performance_t > 2on each frame. This gives the app 2 seconds to stabilize. It restarts after every action and when the window regains focus.- The two
FR < 30actions also reset the FPS samples to 60.
-
init(omath, settings, graphics, time, oscreen, fps_counter): Initialize component.omath: OMath fromohzi-core.settings: An object with a numericdprthat the controller reads and writes. In the boilerplate,Settingsfromapp/js/Settings.ts.graphics: Graphics fromohzi-core. Each DPR change callsgraphics.on_resize()with the new DPR and the currentOScreenposition and size.time: Time fromohzi-core.oscreen: OScreen fromohzi-core.fps_counter: The FPSCounter singleton. CallFPSCounter.init(Time)first.
Also binds window
focusandblur. -
on_focus_in(): Starts performance check. Bound to windowfocus. (private) -
on_focus_out(): Stops performance check, including FPS counting. Bound to windowblur. (private) -
add_optional_features([...]): Add features that can be disabled if performance is too low. It receives an array of objects that have to implement the methoddisable_optional_feature(). They are disabled one at a time, in array order, and never re-enabled. -
update(): Should be called in anupdatemethod. Also callsfps_counter.update(), so don't call that yourself.
threshold: Whether the threshold is enabled.optional_features: Features added withadd_optional_features().performance_t: Seconds since the last action.
import { Graphics, OMath, OScreen, Time } from 'ohzi-core';
import { FPSCounter, PerformanceController } from 'ohzi-components';
import { Settings } from './Settings';
FPSCounter.init(Time);
const performance_controller = new PerformanceController();
performance_controller.init(OMath, Settings, Graphics, Time, OScreen, FPSCounter);
performance_controller.add_optional_features([
{ disable_optional_feature: () => bloom.disable() },
{ disable_optional_feature: () => particles.hide() }
]);
// In your update loop
performance_controller.update();- In the current
ohzi-core,Graphics.on_resize()setsOScreen.dprbut doesn't callsetPixelRatio(), and three.jssetSize()sizes the canvas with the pixel ratio set atGraphics.init(). So DPR changes don't change the canvas resolution yet.