# ui/popupmotion (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/ui/popupmotion/

Popup open / close transitioner.

## PopupMotion extends Object

Popup open / close transitioner.
- Cycles through four states closed → opening → open → closing → closed and
  computes the scale (open: 0.45 → 1.18 overshoot → 1, close: 1 → 1.08 → 0.08) and
  the dimming ratio (dimRatio). (an effect several games copy-pasted)
- Independent of the rendering method. After tick(dt) every frame, read getScale() / getDimRatio() and
  apply them to node scale / backdrop alpha. With setInstantEnabled(true), switches immediately without the effect.

Example: Applying it to the panel scale and the dim alpha

```js
import { PopupMotion, PopupMotionState, WorldNode, Paint, Color, Vector2 } from "./build/vanilla.js";

const panelNode = new WorldNode();
const dimNode = new WorldNode();
const dimPaint = dimNode.addComponent(Paint);
const popupMotion = new PopupMotion({ openDuration: 0.3, closeDuration: 0.2 });
popupMotion.setOpenedEvent(() => {
	console.log("opened");
});
popupMotion.setClosedEvent(() => {
	console.log("closed");
});
const applyMotion = () => {
	const scale = popupMotion.getScale();
	const dimRatio = popupMotion.getDimRatio();
	panelNode.setLocalScale(Vector2.create(scale, scale));
	dimPaint.setColor(new Color(0, 0, 0, 0.5 * dimRatio));
};
popupMotion.open();
for (let frameIndex = 0; frameIndex < 30; ++frameIndex) {
	popupMotion.tick(1 / 60);
	applyMotion();
}
const state = popupMotion.getState();
console.log(state === PopupMotionState.open);
popupMotion.close();
for (let frameIndex = 0; frameIndex < 30; ++frameIndex) {
	popupMotion.tick(1 / 60);
	applyMotion();
}
```

- `constructor(options = {})` (constructor, since 0.3.0): Constructor.
  - `options` object: { openDuration = 0.32, closeDuration = 0.22 }
- `open()` (method, since 0.3.0): Open.
- `close()` (method, since 0.3.0): Close.
- `tick(timeDelta)` (method, since 0.3.0): Update.
  - `timeDelta` number: Time since the last frame. (Seconds)
- `getScale()` (method, since 0.3.0): Returns the current scale.
  - returns number: Scale to apply to the panel. (0 when closed, 1 when open, a bounce curve while opening or closing)
- `getDimRatio()` (method, since 0.3.0): Returns the dimming ratio. (0 ~ 1)
  - returns number: Dim ratio. (0 ~ 1, linear in the open and close time)
- `isInteractive()` (method, since 0.3.0): Whether interactable. (true only after fully open)
  - returns boolean: True if in the open state.
- `getState()` (method, since 0.3.0): State query.
  - returns string: State. (a PopupMotionState value)
- `isVisible()` (method, since 0.3.0): Returns whether it is visible. (true unless closed)
  - returns boolean: True if not in the closed state.
- `setInstantEnabled(isInstantEnabled)` (method, since 0.3.0): Sets whether to switch instantly. (UI tone without effects)
  - `isInstantEnabled` boolean: If true, open and close switch to open and closed right away and also call the events on the spot.
- `isInstantEnabled()` (method, since 0.4.0): Whether it opens and closes immediately without motion.
  - returns boolean: True if it opens and closes immediately.
- `setOpenedEvent(openedEvent)` (method, since 0.3.0): Sets the callback.
  - `openedEvent` Function: Function to call when fully opened. (no arguments)
- `setClosedEvent(closedEvent)` (method, since 0.3.0): Sets the fully closed event.
  - `closedEvent` Function: Function to call when fully closed. (no arguments)

## PopupMotionState (enum)

Popup open / close transitioner.
- Cycles through four states closed → opening → open → closing → closed and
  computes the scale (open: 0.45 → 1.18 overshoot → 1, close: 1 → 1.08 → 0.08) and
  the dimming ratio (dimRatio). (an effect several games copy-pasted)
- Independent of the rendering method. After tick(dt) every frame, read getScale() / getDimRatio() and
  apply them to node scale / backdrop alpha. With setInstantEnabled(true), switches immediately without the effect.

- `closed` = `closed`: Closed. (scale 0, dim 0)
- `opening` = `opening`: Opening. (scale swells from 0.45 to 1.18 and settles at 1)
- `open` = `open`: Open. (scale 1, dim 1, the only state that accepts interaction)
- `closing` = `closing`: Closing. (scale swells to 1.08 and shrinks to 0.08)
