# core/tween (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/tween/

Tween.

## Tween extends Object

Tween.
- Once the target node is set with setTarget(node), values whose names are TweenProperty names are written directly to that node every tick.
  If the target node leaves the node tree it was in when the tween first ran (removeChild, etc.), it ends quietly without callbacks.
- If duration is 0 or less, it is treated as finished immediately (progress 1).

Example: Change two values over 0.5 seconds

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

const tween = new Tween({ x: 0, alpha: 1 });
tween.to({ x: 100, alpha: 0 }, 0.5);
tween.easing(Tween.easingFunction.quadratic.easeOut);
tween.setUpdate((values) => {
	console.log(values.x, values.alpha);
});
tween.setComplete(() => {
	console.log("complete");
});
tween.start();
for (let step = 0; step < 5; ++step) {
	tween.tick(0.1);
}
console.log(tween.isFinished());
```

- `constructor(initialValues)` (constructor, since 0.0.8): Constructor.
  - `initialValues` Object: Start value object. (a number or Vector2 per name, kept as a shallow copy)
- `static createNodeTween(node, tweenProperty, startValue, endValue, duration)` (static, since 0.4.0): Creates a tween that animates one node property. (static)
  - Same as new Tween(startValue).to(targetValue, duration).setTarget(node). The value name is the property name.
  - `node` WorldNode: Node to move.
  - `tweenProperty` string: Value of a TweenProperty.
  - `startValue` number | Vector2: Start value.
  - `endValue` number | Vector2: Target value.
  - `duration` number: Duration. (seconds, finishes at once if 0 or less)
  - returns Tween: The created tween. (not running yet, start it with Scene.startTween or start)

  Example: Move a node position in a scene

  ```js
  import { Engine, EngineConfiguration, Scene, WorldNode, Tween, TweenProperty, Vector2 } from "./build/vanilla.js";
  
  class MainScene extends Scene {
  	initialize(engine) {
  		super.initialize(engine);
  		const node = new WorldNode();
  		const root = this.getRoot();
  		root.addChild(node);
  		const tween = Tween.createNodeTween(node, TweenProperty.localPosition, Vector2.create(0, 0), Vector2.create(200, 100), 0.5);
  		tween.easing(Tween.easingFunction.cubic.easeInOut);
  		tween.setComplete(() => {
  			const localPosition = node.getLocalPosition();
  			console.log(localPosition.x, localPosition.y);
  		});
  		this.startTween(tween);
  	}
  }
  
  const engineConfiguration = new EngineConfiguration();
  const engine = new Engine(engineConfiguration);
  engine.run(new MainScene());
  ```

- `static findRootNode(node)` (static, since 0.4.0): Returns the topmost node of the node.
  - `node` Node: Start node.
  - returns Node: The node reached by following parents to the top. (the node itself if it has no parent)
- `tick(timeDelta)` (method, since 0.0.8): Update.
  - `timeDelta` number: Elapsed time. (seconds, advances only when started and not finished)
- `to(properties, duration)` (method, since 0.0.8): Sets the target value.
  - `properties` System.Object: Target value object. (only names that exist in the start values with the same kind change, number to number or Vector2 to Vector2)
  - `duration` number: Duration. (seconds, unchanged if omitted, default 1)
  - returns Tween: This tween. (for chaining)
- `delay(amount)` (method, since 0.0.8): Sets the delay before starting.
  - `amount` number: Wait time before starting. (seconds)
  - returns Tween: This tween. (for chaining)
- `easing(easingFunction)` (method, since 0.0.8): Sets the tween function.
  - `easingFunction` Function: Function that takes the progress (0 ~ 1) and returns the eased progress. (one of Tween.easingFunction, default linear)
  - returns Tween: This tween. (for chaining)
- `setUpdate(callback)` (method, since 0.0.8): Sets the update callback.
  - `callback` Function: Function that receives the current value object on every tick. (not called during the delay)
  - returns Tween: This tween. (for chaining)
- `setComplete(callback)` (method, since 0.0.8): Sets the completion callback.
  - `callback` Function: Function called once when the progress reaches 1. (no arguments, not called when it ends because the target node was detached)
  - returns Tween: This tween. (for chaining)
- `setTarget(node)` (method, since 0.4.0): Sets the target node. (values whose names are TweenProperty names are written to this node)
  - `node` WorldNode | null: Target node. (if null, values are not written to a node and are only passed to the callback)
  - returns Tween: This tween. (for chaining)
- `getTarget()` (method, since 0.4.0): Returns the target node. (null if setTarget was not called)
  - returns WorldNode | null: Target node. (null if none)
- `start()` (method, since 0.0.8): Start.
- `stop()` (method, since 0.0.8): Stop.
- `isFinished()` (method, since 0.0.8): Returns whether it is complete.
  - returns boolean: True if finished. (when stop was called or the progress reached 1)
- `static easingFunction` (static, since 0.0.8): Tween function.

## Functions and constants

- `applyTweenProperty(node, tweenProperty, value)` (method, since 0.4.0): Writes a tween property value to the node. (does nothing if it is not a node property name)
  - `node` WorldNode: Node to write the value to.
  - `tweenProperty` string: Value of a TweenProperty.
  - `value` number | Vector2: Value to write. (a number given to a Vector2 property is written to both axes, a Vector2 given to a number property is not written)

## TweenProperty (enum)

Node property names that a tween writes directly. (when a target node is set with setTarget, values with these names are written to that node)
- Giving a number to a Vector2 property writes it to both axes. (e.g. { localScale: 0 } → setLocalScale(Vector2.create(0, 0)))
- Giving a Vector2 to a number property does not write it.

- `localPosition` = `localPosition`: Local position. (Vector2, setLocalPosition)
- `localScale` = `localScale`: Local scale. (Vector2, setLocalScale)
- `localRotation` = `localRotation`: Local rotation. (number, degrees, setLocalRotation)
- `localOpacity` = `localOpacity`: Local opacity. (number, 0 ~ 1, setLocalOpacity)
- `contentSize` = `contentSize`: Content size. (Vector2, pixels, setContentSize)
