# misc/toucheffect (vanilla.js 0.4.0)

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

Radial gradient texture for particles. (Baked in white and colored by the shader tint)
- WebGL2 has no gradient API, so it is baked only once onto an offscreen 2D canvas.

## TouchParticle extends Object

Touch particle.

Example: Advance one particle's life and position by one step

```js
import { TouchParticle, Vector2 } from "./build/vanilla.js";

const touchParticle = new TouchParticle();
touchParticle.position = Vector2.create(100, 100);
touchParticle.velocity = Vector2.create(30, -60);
touchParticle.radius = 20;
const timeDelta = 0.1;
touchParticle.life -= timeDelta * 2.5;
const step = touchParticle.velocity.multiply(timeDelta);
touchParticle.position = touchParticle.position.add(step);
const opacity = touchParticle.life / touchParticle.maxLife;
console.log(opacity, touchParticle.position.x, touchParticle.position.y);
```

- `position` (field, since 0.0.8): Position. (pixels, relative to the TouchEffect node)
  - returns Vector2
- `velocity` (field, since 0.0.8): Velocity. (pixels / second)
  - returns Vector2
- `life` (field, since 0.0.8): Remaining life. (Starts at 1, decreases by 2.5 per second, removed at 0 or below)
  - returns number
- `maxLife` (field, since 0.0.8): Initial life. (Opacity is life / maxLife)
  - returns number
- `radius` (field, since 0.0.8): Radius. (pixels)
  - returns number
- `constructor()` (constructor, since 0.0.8): Constructor.

## TouchEffect extends WorldNode

Touch effect.

Example: Scatter glowing particles where pressed

```js
import { Engine, EngineConfiguration, Scene, TouchEffect } from "./build/vanilla.js";

const touchEffect = new TouchEffect();

class MainScene extends Scene {
	initialize(engine) {
		super.initialize(engine);
		const root = this.getRoot();
		root.addChild(touchEffect);
		for (let index = 0; index < 8; ++index) {
			touchEffect.createTouchParticle(320, 180);
		}
	}

	touchPress(viewInputPosition) {
		super.touchPress(viewInputPosition);
		for (let index = 0; index < 8; ++index) {
			touchEffect.createTouchParticle(viewInputPosition.x, viewInputPosition.y);
		}
	}
}

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

- `constructor()` (constructor, since 0.0.8): Constructor.
- `tick(timeDelta)` (method, since 0.0.8): Update.
  - `timeDelta` number: Time since the last frame. (Seconds)
- `pushTransform(graphic)` (method, since 0.0.11): Begins the draw state.
  - `graphic` Graphic: Graphic to draw with.
- `draw(graphic)` (method, since 0.0.8): Draw.
  - `graphic` Graphic: Graphic to draw with.
- `popTransform(graphic)` (method, since 0.0.11): Ends the draw state.
  - `graphic` Graphic: Graphic to draw with.
- `createTouchParticle(x, y)` (method, since 0.0.8): Creates touch particles.
  - `x` number: X to emit particles at. (Coordinates relative to this node, jittered within 10 pixels)
  - `y` number: Y to emit particles at. (Coordinates relative to this node, jittered within 10 pixels)
- `updateTouchParticles(timeDelta)` (method, since 0.0.8): Updates touch particles.
  - `timeDelta` number: Time since the last frame. (Seconds)
- `drawTouchParticles(graphic)` (method, since 0.0.8): Renders touch particles.
  - `graphic` Graphic: Graphic to draw with.
