# misc/shaker (vanilla.js 0.4.0)

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

Shake generator. (shared by screen / node)

## Shaker extends Object

Shake generator. (shared by screen / node)
- Builds x / y offsets from two sine waves with different phases and lets them fade out with exponential decay.
  (what several games each reimplemented inline as sin combinations + decay formulas)
- Add strength with addShake(strength), and after tick(dt) every frame add getOffset() to
  the camera or node position.

Example: Read an offset that shakes and settles

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

const shaker = new Shaker({ decayRate: 9, frequency: 42 });
shaker.addShake(12);
for (let frame = 0; frame < 30; ++frame) {
	shaker.tick(1 / 60);
	const offset = shaker.getOffset();
	if (frame % 10 === 0) {
		console.log(offset.x.toFixed(2), offset.y.toFixed(2));
	}
}
console.log(shaker.isShaking());
shaker.stop();
console.log(shaker.isShaking());
```

- `constructor(options = {})` (constructor, since 0.3.0): Constructor.
  - `options` object: { decayRate = 9, frequency = 42 }
- `addShake(strength)` (method, since 0.3.0): Adds shake.
  - If already shaking, the stronger one wins.
  - `strength` number: Maximum offset. (pixels)
- `tick(timeDelta)` (method, since 0.3.0): Update.
  - `timeDelta` number: Time since the last frame. (Seconds)
- `getOffset()` (method, since 0.3.0): Returns the current offset.
  - Uses different frequency ratios for the two axes (1 : 1.37) so the path is not monotonous.
  - returns Vector2: x, y offset. (pixels, a zero vector when not shaking)
- `isShaking()` (method, since 0.3.0): Whether it is shaking.
  - returns boolean: True while shaking.
- `stop()` (method, since 0.3.0): Stops immediately.
- `setDecayRate(decayRate)` (method, since 0.3.0): Sets the decay rate. (larger fades faster)
  - `decayRate` number: Decay rate. (exponential decay coefficient per second, default 9)
- `setFrequency(frequency)` (method, since 0.3.0): Sets the oscillation frequency.
  - `frequency` number: Oscillation angular speed. (radians / second, default 42)
