# misc/toucheffect (vanilla-engine 0.4.0)

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

Touch effect component.
- A port of the original misc/toucheffect.js. Scatters sky blue light particles at the press point that fade and spread over 0.4 seconds.
- Particles draw a white radial gradient image (64x64, made only once) tinted sky blue (rgb 100, 200, 255) with additive (lighter) blending.
- Usage:
  ```text
    let touch_effect_id = node_tree.create_child(root_id, "TouchEffect");
    node_tree[touch_effect_id].set_pivot(Pivot::TOP_LEFT);
    node_tree[touch_effect_id].add_component(TouchEffect::new());
    // on press: node_tree[touch_effect_id].get_component_mut::<TouchEffect>().unwrap().create_touch_particle(x, y);
  ```
- Differences from the original:
  The original is a node that inherits WorldNode, and the Rust version is a component attached to a node. As in the original, the blend is switched to additive in push_transform
  and restored to the previous blend in pop_transform, so children are drawn additively too. (the component's hooks around the node draw state)
  The gradient is an Image baked with the radial gradient of an offscreen canvas (the engine Canvas2D), like the original.

## TouchParticle (struct) (Clone, Copy, Debug, PartialEq, Default)

Touch particle.
- TouchEffect holds them in a list. A struct holding only values.

Example: Values of one particle

```rust
use vanilla::{TouchParticle, Vector2};

let mut particle = TouchParticle::new();
particle.position = Vector2::create(40.0, 30.0);
particle.radius = 20.0;
assert_eq!(particle.life, particle.max_life);
assert_eq!(particle.velocity, Vector2::zero());
```

- `pub position: Vector2` (field, since 0.3.2): Center position. (node area coordinates, pixels)
  - returns Vector2
- `pub velocity: Vector2` (field, since 0.3.2): Velocity. (pixels/second)
  - returns Vector2
- `pub life: f64` (field, since 0.3.2): Remaining life. (starts at 1 in TouchEffect, decreases by 2.5 per second, removed at 0 or less)
  - returns f64
- `pub max_life: f64` (field, since 0.3.2): Initial life. (opacity when drawn is life / max_life)
  - returns f64
- `pub radius: f64` (field, since 0.3.2): Radius. (pixels)
  - returns f64
- `pub fn new() -> TouchParticle` (constructor, since 0.3.2): Constructor. (stationary, life 1, radius 0)
  - returns TouchParticle: A particle with position and velocity 0, life and initial life 1, and radius 0.

## TouchEffect (struct) (Default, Component, UIDocumentNode)

Touch effect component.
- If create_touch_particle is called on press, tick wears the particles down and draw renders them with additive blending.

Example: Attach to a node and make particles at the press point

```rust
use vanilla::{NodeTree, Pivot, TouchEffect};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let touch_effect_id = node_tree.create_child(root_id, "TouchEffect");
node_tree[touch_effect_id].set_pivot(Pivot::TOP_LEFT);
node_tree[touch_effect_id].add_component(TouchEffect::new());

let touch_effect = node_tree[touch_effect_id].get_component_mut::<TouchEffect>().unwrap();
touch_effect.create_touch_particle(120.0, 80.0);
assert_eq!(touch_effect.get_particle_count(), 1);
touch_effect.update_touch_particles(0.5);
assert_eq!(touch_effect.get_particle_count(), 0);
```

- `pub fn new() -> TouchEffect` (constructor, since 0.3.2): Constructor.
  - returns TouchEffect: A new touch effect with no particles.
- `pub fn create_touch_particle(&mut self, x: f64, y: f64)` (method, since 0.3.2): Creates touch particles. (node area coordinates, ±5 pixel scatter, ±60 pixels/second velocity, radius 10 ~ 35)
  - `x` f64: Press point x. (node area coordinates, pixels)
  - `y` f64: Press point y. (node area coordinates, pixels)
- `pub fn update_touch_particles(&mut self, time_delta: f64)` (method, since 0.3.2): Updates the touch particles. (life decreases by 2.5 per second, removed when used up)
  - `time_delta` f64: Elapsed time. (seconds, the position moves by velocity * time_delta)
- `pub fn draw_touch_particles(&self, graphic: &mut Graphic)` (method, since 0.3.2): Draws the touch particles. (tints the white gradient with the particle color, faded by the remaining life)
  - Does not change the blend. (when drawn as a component, push_transform switches it to additive)
  - `graphic` &mut Graphic: Graphic to draw to. (image tint and global alpha are restored after drawing)
- `pub fn get_particle_count(&self) -> usize` (method, since 0.3.2): Number of live particles. (added in the Rust version)
  - returns usize: Number of particles with life remaining.
