# effect/trailrenderer (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/effect/trailrenderer/

Trail renderer.

## TrailRenderer extends Component

Trail renderer.
- Records the path the node has passed as points and draws it as a band whose width / color fade out.
  (2D counterpart of Unity's TrailRenderer)
- Usage:
    const trail = node.addComponent(TrailRenderer);
    trail.setTime(0.5);
    trail.setWidth(14, 0);
    trail.setColors(new Color(0.4, 0.8, 1, 0.9), new Color(0.4, 0.8, 1, 0));

Example: Leave a glowing trail behind a moving node

```js
import { Engine, EngineConfiguration, Scene, WorldNode, TrailRenderer, Tween, TweenProperty, Color, 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 trailRenderer = node.addComponent(TrailRenderer);
		trailRenderer.setTime(0.5);
		trailRenderer.setWidth(14, 0);
		trailRenderer.setColors(new Color(0.4, 0.8, 1, 0.9), new Color(0.4, 0.8, 1, 0));
		trailRenderer.setAdditive(true);
		const tween = Tween.createNodeTween(node, TweenProperty.localPosition, Vector2.create(40, 180), Vector2.create(600, 180), 0.8);
		tween.setComplete(() => {
			console.log(trailRenderer.getPointCount());
		});
		this.startTween(tween);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.3.0): Constructor.
- `tick(timeDelta)` (method, since 0.3.0): Update. (point recording + aging)
  - `timeDelta` number: Elapsed time. (seconds, points age and those past their lifetime are removed)
- `draw(graphic)` (method, since 0.3.0): Draw. (interpolates width / color per segment, draws in coordinates relative to the node origin)
  - `graphic` Graphic: Graphic to draw with.
- `clear()` (method, since 0.3.0): Clears the trail.
- `calculateDrawnRect(rect)` (method, since 0.4.0): Calculates the drawn rectangle. (Override: a rectangle enclosing the band joining the points, in node content coordinates)
  - Points are drawn relative to the node position, so they are moved by the current node position and grown by half the larger of the two widths plus one unit of margin. (Covering line ends and joins)
  - Recalculation is requested every tick while points exist, on clear, and when the width changes. With fewer than two points nothing is drawn, so the size is 0.
  - `rect` Rect: Where to write the drawn rectangle. (origin at the top-left of the node's content area)
  - returns boolean: True if known.

  Example: A rectangle enclosing the path travelled

  ```js
  import { WorldNode, TrailRenderer, Rect, Vector2 } from "./build/vanilla.js";
  
  const node = new WorldNode();
  const trailRenderer = node.addComponent(TrailRenderer);
  trailRenderer.setWidth(10, 0);
  trailRenderer.tick(0.016);
  node.setLocalPosition(Vector2.create(100, 0));
  trailRenderer.tick(0.016);
  const drawnRect = Rect.zero();
  console.log(trailRenderer.calculateDrawnRect(drawnRect), drawnRect.position.x, drawnRect.size.x);
  ```

- `setTime(pointLifetime)` (method, since 0.3.0): Sets how long points stay. (sets the trail length)
  - `pointLifetime` number: How long points remain. (seconds)
- `setMinVertexDistance(minVertexDistance)` (method, since 0.3.0): Sets the minimum distance between points.
  - `minVertexDistance` number: No point is added while moving closer than this. (pixels, default 3)
- `setWidth(startWidth, endWidth)` (method, since 0.3.0): Sets the width. (shrinks from the width of a fresh point to the end width)
  - `startWidth` number: Width of a fresh point. (pixels, default 10)
  - `endWidth` number: Width of a point at the end of its lifetime. (pixels, default 0)
- `setColors(startColor, endColor)` (method, since 0.3.0): Sets the colors. (changes from the color of a fresh point to the end color)
  - `startColor` Color | string: Color of a fresh point. (kept as a copy)
  - `endColor` Color | string: Color of a point at the end of its lifetime. (kept as a copy)
- `setAdditive(isAdditive)` (method, since 0.3.0): Sets whether to use additive blending.
  - `isAdditive` boolean: If true, draws blended with "lighter". (default false)
- `setEmitting(isEmitting)` (method, since 0.3.0): Sets whether to make points.
  - `isEmitting` boolean: If false, no new points are created. (the remaining band fades out)
- `getPointCount()` (method, since 0.3.0): Returns the remaining point count.
  - returns number: Remaining point count.
