# effect/heathazeeffect (vanilla.js 0.4.0)

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

Heat haze effect component.

## HeatHazeEffect extends Component

Heat haze effect component.
- Takes the canvas content drawn before the attached node, over the node's area, and applies a shimmering effect.
- Therefore the effect is visible only when attached to the node drawn last (topmost) in the scene. Child nodes are drawn over the effect.
- A component attached to a node, like ParticleSystem and TrailRenderer. The area uses the node's content size (contentSize) and pivot.
  If the node's content size is empty when attached, it is set to 300 x 300.
- Usage:
    const heatHazeEffect = node.addComponent(HeatHazeEffect);
    heatHazeEffect.setAmplitude(4);
- In the WebGL Graphic, it flushes the batch first, copies the screen bounding box of the node area (drawing buffer pixels) into a texture with copyTexSubImage2D,
  and redraws it shifted along a sine curve, one quad per line, as vertex color quads (drawColoredQuads). (same method as the Rust vanilla-engine)
- If the area goes off screen, the screen edge pixels are stretched. (CLAMP_TO_EDGE)
- The copied texture is a GL resource, so call destroy() when done with it.

Example: Put heat haze over a rectangle drawn earlier

```js
import { Engine, EngineConfiguration, Scene, WorldNode, HeatHazeEffect, Colors, Rect, Vector2 } from "./build/vanilla.js";

class MainScene extends Scene {
	initialize(engine) {
		super.initialize(engine);
		const hazeNode = new WorldNode();
		hazeNode.setLocalPosition(Vector2.create(320, 180));
		hazeNode.setContentSize(Vector2.create(300, 240));
		const root = this.getRoot();
		root.addChild(hazeNode);
		const heatHazeEffect = hazeNode.addComponent(HeatHazeEffect);
		heatHazeEffect.setAmplitude(4);
		heatHazeEffect.setFrequency(0.08);
		heatHazeEffect.setSpeed(3);
	}

	preDraw(graphic) {
		super.preDraw(graphic);
		graphic.setFillColor(Colors.white);
		graphic.drawRect(Rect.create(220, 100, 200, 160));
	}
}

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

- `constructor()` (constructor, since 0.0.8): Constructor.
- `attach(node)` (method, since 0.4.0): Attached to a node. (300 x 300 if the node's content size is empty)
  - `node` ComponentNode: Node being attached to.
- `tick(timeDelta)` (method, since 0.0.8): Update.
  - `timeDelta` number: Elapsed time. (seconds, multiplied by the speed and added to the wave clock)
- `draw(graphic)` (method, since 0.0.8): Draw.
  - `graphic` Graphic: Graphic to draw with.
- `ensureCaptureTexture(graphic, width, height)` (method, since 0.4.0): Prepares the texture to copy into. (reallocated when the size changes, RGB8 like the screen)
  - `graphic` Graphic: Graphic to make the texture with.
  - `width` number: Width. (drawing buffer pixels)
  - `height` number: Height. (drawing buffer pixels)
  - returns WebGLTexture: Texture to capture into.
- `destroyCaptureTexture()` (method, since 0.4.0): Deletes the copied texture.
- `destroy()` (method, since 0.4.0): Destroys. (deletes the copied texture)
- `setSpeed(value)` (method, since 0.0.8): Sets the speed.
  - `value` number: Speed of the wave clock. (radians per second, default 2)
- `getSpeed()` (method, since 0.0.8): Returns the speed.
  - returns number: Wave speed. (radians per second)
- `setAmplitude(value)` (method, since 0.0.8): Sets the intensity.
  - `value` number: Horizontal shift amount. (node local pixels, default 3)
- `getAmplitude()` (method, since 0.0.8): Returns the strength.
  - returns number: Wave amplitude. (node local pixels)
- `setFrequency(value)` (method, since 0.0.8): Sets the frequency.
  - `value` number: Frequency multiplied by the row position. (radians per node local pixel, default 0.05)
- `getFrequency()` (method, since 0.0.8): Returns the frequency.
  - returns number: Frequency multiplied by the row position. (radians per node local pixel)
- `getTimer()` (method, since 0.4.0): Returns the wobble clock. (advances by timeDelta * speed every tick)
  - returns number: Wave clock. (radians)
- `calculateDrawnRect(rect)` (method, since 0.4.0): Writes the drawn rectangle to rect. (Override: each row of the content area is shifted left and right by the wobble amplitude, so it spills by that much on both sides)
  - `rect` Rect: Where to write the drawn rectangle. (origin at the top-left of the node's content area)
  - returns boolean: True if attached to a node.

  Example: Seeing where the heat haze draws

  ```js
  import { WorldNode, HeatHazeEffect, Rect, Vector2 } from "./build/vanilla.js";
  
  const node = new WorldNode();
  node.setContentSize(Vector2.create(100, 40));
  const heatHazeEffect = node.addComponent(HeatHazeEffect);
  heatHazeEffect.setAmplitude(4);
  const drawnRect = Rect.zero();
  heatHazeEffect.calculateDrawnRect(drawnRect);
  console.log(drawnRect.position.x, drawnRect.size.x);
  ```

- `calculateRowOffset(localY)` (method, since 0.4.0): Horizontal wobble of a line. (sin(clock + line position * frequency) * amplitude)
  - `localY` number: Top position of the row. (node local pixels)
  - returns number: Horizontal shift distance. (node local pixels)
