# effect/trailrenderer (vanilla.js 0.4.0)

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

트레일 렌더러.

## TrailRenderer extends Component

트레일 렌더러.
- 노드가 지나간 자리를 점으로 기록해 굵기 / 색이 잦아드는 띠로 그린다.
  (유니티 TrailRenderer 의 2D 대응)
- 사용:
    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));

예제: 움직이는 노드 뒤에 빛나는 띠 남기기

```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): 생성.
- `tick(timeDelta)` (method, since 0.3.0): 갱신. (점 기록 + 노화)
  - `timeDelta` number: 지난 시간. (초, 점이 늙고 수명이 지난 점은 빠진다)
- `draw(graphic)` (method, since 0.3.0): 출력. (세그먼트마다 굵기 / 색 보간, 노드 원점 기준 상대 좌표로 그린다)
  - `graphic` Graphic: 그릴 그래픽.
- `clear()` (method, since 0.3.0): 흔적 지우기.
- `calculateDrawnRect(rect)` (method, since 0.4.0): 그리는 사각형 셈하기. (오버라이드: 점을 이은 띠를 모두 품는 사각형, 노드 내용 좌표)
  - 점은 노드 자리를 뺀 자리에 그리므로 지금 노드 자리로 옮기고, 두 굵기 가운데 큰 것의 반과 한 단위 여유만큼 넓힌다. (선 끝과 이음까지)
  - 점이 있으면 tick 마다, 지움(clear)과 굵기를 바꾸면 다시 셈하게 알린다. 점이 둘보다 적으면 그리지 않으므로 크기 0.
  - `rect` Rect: 그리는 사각형을 적을 곳. (노드 내용 영역의 왼쪽 위가 원점)
  - returns boolean: 알면 참.

  예제: 지나간 자리를 품는 사각형

  ```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): 점이 남는 시간 설정. (띠의 길이를 정한다)
  - `pointLifetime` number: 점이 남는 시간. (초)
- `setMinVertexDistance(minVertexDistance)` (method, since 0.3.0): 점 사이 최소 거리 설정.
  - `minVertexDistance` number: 이보다 가까이 움직이면 점을 더하지 않는다. (픽셀, 기본 3)
- `setWidth(startWidth, endWidth)` (method, since 0.3.0): 굵기 설정. (갓 생긴 점의 굵기에서 끝 굵기로 줄어든다)
  - `startWidth` number: 갓 생긴 점의 굵기. (픽셀, 기본 10)
  - `endWidth` number: 수명이 다한 점의 굵기. (픽셀, 기본 0)
- `setColors(startColor, endColor)` (method, since 0.3.0): 색 설정. (갓 생긴 점의 색에서 끝 색으로 바뀐다)
  - `startColor` Color | string: 갓 생긴 점의 색. (복사해 둔다)
  - `endColor` Color | string: 수명이 다한 점의 색. (복사해 둔다)
- `setAdditive(isAdditive)` (method, since 0.3.0): 가산 합성 여부 설정.
  - `isAdditive` boolean: 참이면 "lighter" 로 섞어 그린다. (기본 거짓)
- `setEmitting(isEmitting)` (method, since 0.3.0): 점 만들기 여부 설정.
  - `isEmitting` boolean: 거짓이면 새 점을 만들지 않는다. (남은 띠는 잦아든다)
- `getPointCount()` (method, since 0.3.0): 남은 점 수 반환.
  - returns number: 남은 점 수.
