# misc/fixedstepper (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/misc/fixedstepper/

Fixed step.

## FixedStepper extends Object

Fixed step.
- Even if frame intervals are uneven, rules (physics, round flow) always run at the same step (stepSeconds).
- advance(timeDelta, stepFunction) accumulates the received time and calls stepFunction(stepSeconds) each time a full step accumulates.
  The returned value is the remaining fraction (0 ~ 1). Drawing by blending the previous step and the current step by this fraction keeps the steps from jittering against the screen refresh rate.
- Runs at most maximumStepsPerFrame steps per frame. If time still remains after that many, the remaining time is discarded.
  (So a slow device does not get even slower by running the backlog all at once after a stall)
- If stepFunction returns false, this frame's steps stop there and the remaining time stays accumulated. (For holding briefly, as in meetings or cutscenes)

Example: Run uneven frames at a fixed step

```js
import { FixedStepper } from "./build/vanilla.js";

const fixedStepper = new FixedStepper(1 / 60, 5);
let position = 0;
const velocity = 120;
const frameDeltaList = [0.016, 0.033, 0.008, 0.050];
for (const timeDelta of frameDeltaList) {
	const alpha = fixedStepper.advance(timeDelta, (stepSeconds) => {
		position += velocity * stepSeconds;
	});
	console.log(fixedStepper.getLastStepCount(), alpha.toFixed(2), position.toFixed(2));
}
```

- `constructor(stepSeconds = 1 / 60, maximumStepsPerFrame = 5)` (constructor, since 0.4.0): Constructor.
  - `stepSeconds` number: Duration of one step. (Seconds, default 1 / 60)
  - `maximumStepsPerFrame` number: Maximum steps run per frame. (default 5)
- `setStepSeconds(stepSeconds)` (method, since 0.4.0): Sets the duration of one step. (Seconds, must be greater than 0. Otherwise left unchanged)
  - `stepSeconds` number: Time of one step. (seconds)
- `getStepSeconds()` (method, since 0.4.0): Returns the duration of one step. (Seconds)
  - returns number: Time of one step. (seconds)
- `setMaximumStepsPerFrame(maximumStepsPerFrame)` (method, since 0.4.0): Sets the maximum steps run per frame. (1 or more, fractions are discarded)
  - `maximumStepsPerFrame` number: Maximum number of steps run per frame.
- `getMaximumStepsPerFrame()` (method, since 0.4.0): Returns the maximum steps run per frame.
  - returns number: Maximum number of steps run per frame.
- `advance(timeDelta, stepFunction)` (method, since 0.4.0): Advances. (Runs steps for the received time and returns the remaining fraction)
  - `timeDelta` number: Time of this frame. (Seconds, 0 if negative or not a number)
  - `stepFunction` (stepSeconds: number) => (boolean | void): One step. Returning false stops this frame there.
  - returns number: Remaining fraction. (0 ~ 1)

  Example: Blend drawing with the remaining fraction, and hold steps with false

  ```js
  import { FixedStepper } from "./build/vanilla.js";
  
  const fixedStepper = new FixedStepper();
  let previousPosition = 0;
  let currentPosition = 0;
  let isPaused = false;
  const step = (stepSeconds) => {
  	if (isPaused) {
  		return false;
  	}
  	previousPosition = currentPosition;
  	currentPosition += 60 * stepSeconds;
  	return true;
  };
  const alpha = fixedStepper.advance(0.025, step);
  const drawPosition = previousPosition + (currentPosition - previousPosition) * alpha;
  console.log(fixedStepper.getLastStepCount(), drawPosition);
  
  isPaused = true;
  fixedStepper.advance(0.1, step);
  console.log(fixedStepper.getLastStepCount(), fixedStepper.getAlpha());
  ```

- `getAlpha()` (method, since 0.4.0): Remaining fraction. (Accumulated time / one step, 0 ~ 1)
  - returns number: Remaining fraction. (0 ~ 1)
- `getLastStepCount()` (method, since 0.4.0): Number of steps run in the last advance.
  - returns number: Number of steps run in the last advance.
- `reset()` (method, since 0.4.0): Clears the accumulated time. (When starting a new round or releasing a hold)
