Smooth Number

<script src="./point_src/smooth-number"></script>

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?

Meta Data
title Smooth Number
dependencies ()
unused_keys ()
unknown_keys ()
filepath_exists True
path smooth-number
filepath smooth-number.js
clean_files ()

Method List

comment.html|safe
comment.html|safe
  • method_kind

    method_name

    (
    param_name = param.default_value ,
    )
    from class_name
    comment.html|safe
    comment.html|safe