Smooth Number
The
SmoothNumberclass receives fast number updates, and returns a averaged number when queried.
const smoothVal = new SmoothNumber()
speedNumber.push(stage.mouse.speed())
stage.fps.text = speedNumber
A SmoothNumber combines a average of many values in a list, visually
updated a (Framerate) modulo speed.
Push numbers into the list, to get an average over time allows for nicer-looking updates on a fast changing number, such as the FPS.
Setup
Create a new instance of the SmoothNumber:
const initValue = 40
, width = 20 // how many historical points
, updateRate = 10 // every X frames
, valueFix = 0 // value.toFixed
const smoothVal = new SmoothNumber(initValue, width, updateRate, valueFix)
Then we can push a value to it:
smoothVal.push(currentFPS)
It can be done many (many) times. When required, we read the number:
smoothVal.get()
This secretly returns the smoothVal.value - but checks to ensure the value is fresh.
Easier
To save the hassle of pushing then getting a number, we can do this with one call.
fpsValue = this.smoothVal.pushGet(Math.round(currentFPS)+1)
This adheres to the update-rate, so the number fpsValue will change slower than
directly reading currentFPS.
This is nicer for humans - as most of them can't read text at 60FPS+ apparently?
Meta Data
| title | Smooth Number |
| dependencies | () |
| unused_keys | () |
| unknown_keys | () |
| filepath_exists | True |
| path | smooth-number |
| filepath | smooth-number.js |
| clean_files | () |
-
ClassDeclarationclass
SmoothNumber
extends Noneclass comments:title: Smooth Number --- > The `SmoothNumber` class receives fast number updates, and returns a _averaged_ number when queried. const smoothVal = new SmoothNumber() speedNumber.push(stage.mouse.speed()) stage.fps.text = speedNumber A `SmoothNumber` combines a average of many values in a list, visually updated a (Framerate) modulo speed. Push numbers into the list, to get an average over time allows for nicer-looking updates on a fast changing number, such as the FPS. ## Setup Create a new instance of the `SmoothNumber`: const initValue = 40 , width = 20 // how many historical points , updateRate = 10 // every X frames , valueFix = 0 // value.toFixed const smoothVal = new SmoothNumber(initValue, width, updateRate, valueFix) Then we can push a value to it: smoothVal.push(currentFPS) It can be done many (many) times. When required, we read the number: smoothVal.get() This secretly returns the `smoothVal.value` - but checks to ensure the value is fresh. ### Easier To save the hassle of _pushing_ then _getting_ a number, we can do this with one call. fpsValue = this.smoothVal.pushGet(Math.round(currentFPS)+1) This adheres to the update-rate, so the number `fpsValue` will change slower than directly reading `currentFPS`. This is nicer for humans - as most of them can't read text at 60FPS+ apparently?A SmoothNumber provides a nicer read-back of a number over a period of time. Similar to the FPS counter. Create an instance and push a number when required. Read the _value_ through a few routines. Create: const initValue = 40 width = 20 // how many historical points updateRate = 10 // every X frames valueFix = 0 // value.toFixed const smoothVal = new SmoothNumber(initValue, width, updateRate, valueFix) smoothVal.push(currentFPS) Read: smoothVal.get() Push + Read: b = this.smoothVal.pushGet(Math.round(currentFPS)+1)