# effect/screeneffect (vanilla.js 0.4.0)

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

Screen effect. (scene post-processing chain)

## ScreenEffect extends Object

Screen effect. (scene post-processing chain)
- The scene draws to the screen as is, and end() captures the screen (or the region set with setRegion), applies the enabled effects in order, and puts it back in the same place.
- The region is in canvas pixel coordinates (top-left origin), and if a rounded corner radius is given, the original is kept outside it.
- Usage:
    // in the scene's load
    this.screenEffect = new ScreenEffect();
    this.screenEffect.setEnabled(ScreenEffectType.vignette, true);
    // at the start of preDraw / the end of postDraw
    this.screenEffect.begin(graphic);  ...  this.screenEffect.end(graphic);
    // in tick
    this.screenEffect.tick(timeDelta);

Example: Apply a vignette and chromatic aberration to a scene region

```js
import { Engine, EngineConfiguration, Scene, ScreenEffect, ScreenEffectType, Color, Colors, Rect } from "./build/vanilla.js";

class MainScene extends Scene {
	initialize(engine) {
		super.initialize(engine);
		const graphic = engine.getGraphic();
		this.screenEffect = new ScreenEffect();
		this.screenEffect.setRegion(40, 20, 560, 320, 24);
		this.screenEffect.setEnabled(ScreenEffectType.vignette, true);
		this.screenEffect.setColor(ScreenEffectType.vignette, new Color(0.1, 0, 0.2, 1));
		this.screenEffect.setEnabled(ScreenEffectType.chromatic, true);
		this.screenEffect.setStrength(ScreenEffectType.chromatic, 0.5);
	}

	tick(timeDelta) {
		super.tick(timeDelta);
		this.screenEffect.tick(timeDelta);
	}

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

	postDraw(graphic) {
		super.postDraw(graphic);
		this.screenEffect.end(graphic);
	}
}

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

- `constructor()` (constructor, since 0.3.0): Create.
  - The graphic is received in begin and end. (the WebGL2 context is taken the first time it draws, same as the Rust edition ScreenEffect::new())
- `resetAll()` (method, since 0.3.0): Resets everything to defaults. (off, strength 1, default parameters / colors, so previous settings don't linger when switching modes)
- `setRegion(x, y, width, height, cornerRadius = 0)` (method, since 0.3.0): Sets the region to apply to. (canvas pixels, top-left origin, null means the whole screen)
  - `x` number: Region left. (canvas pixels)
  - `y` number: Region top. (canvas pixels)
  - `width` number: Region width. (canvas pixels)
  - `height` number: Region height. (canvas pixels)
  - `cornerRadius` number: Rounded corner radius (pixels). 0 means a rectangle.
- `clearRegion()` (method, since 0.3.0): Clears the region to apply to. (whole screen)
- `getRegion()` (method, since 0.3.0): Returns the region to apply to.
  - returns object | null: Applied region. ({ x, y, width, height, cornerRadius }, null for the whole screen)
- `tick(timeDelta)` (method, since 0.3.0): Update. (time for animated effects)
  - `timeDelta` number: Elapsed time. (seconds, added to the effect time)
- `begin(graphic)` (method, since 0.3.0): Begin. (at the start of the scene's preDraw, the scene draws to the screen as is. end does work only when an effect is enabled)
  - `graphic` Graphic: Graphic of the scene.
- `end(graphic)` (method, since 0.3.0): Applies the effects. (at the end of the scene's postDraw, captures the screen region, applies the effects and puts it back in the same place)
  - `graphic` Graphic: Graphic of the scene.
- `hasActiveEffect()` (method, since 0.3.0): Whether any effect is enabled. (strength 0 counts as off)
  - returns boolean: True if an effect is enabled with a strength above 0.
- `ensureTargets(width, height)` (method, since 0.3.0): Prepares the render targets. (recreated when the size changes)
  - `width` number: Width. (drawing buffer pixels)
  - `height` number: Height. (drawing buffer pixels)
- `resolvePass(passName)` (method, since 0.3.0): Returns a pass. (compiles it if missing. A failure is also cached and reported only once, and steps with a failed pass are skipped)
  - `passName` string: Pass name. (an effect name or an internal step name such as copy or bloomBright)
  - returns FullscreenPass | null: Pass. (null if building the shader failed)
- `setEnabled(effectType, isEnabled)` (method, since 0.3.0): Turns an effect on / off.
  - `effectType` string: ScreenEffectType value. (ignored if unknown)
  - `isEnabled` boolean: True to enable.
- `isEnabled(effectType)` (method, since 0.3.0): Returns whether an effect is on.
  - `effectType` string: ScreenEffectType value.
  - returns boolean: True if enabled. (false if unknown)
- `setStrength(effectType, strength)` (method, since 0.3.0): Sets the strength. (0 is the same as off)
  - `effectType` string: ScreenEffectType value. (ignored if unknown)
  - `strength` number: Strength. (default 1, treated as off if 0 or less)
- `getStrength(effectType)` (method, since 0.3.0): Returns the strength.
  - `effectType` string: ScreenEffectType value.
  - returns number: Strength. (0 if unknown)
- `setParameters(effectType, x, y = 0, z = 0, w = 0)` (method, since 0.3.0): Sets the parameters. (meaning differs per effect, see the ScreenEffectType comments)
  - `effectType` string: ScreenEffectType value. (ignored if unknown)
  - `x` number: First parameter.
  - `y` number: Second parameter. (default 0)
  - `z` number: Third parameter. (default 0)
  - `w` number: Fourth parameter. (default 0)
- `setParameter(effectType, parameterName, value)` (method, since 0.3.0): Sets a single parameter. (names in SCREEN_EFFECT_PARAMETER_NAMES)
  - `effectType` string: ScreenEffectType value. (ignored if unknown)
  - `parameterName` string: Parameter name. (one of SCREEN_EFFECT_PARAMETER_NAMES, ignored if the name does not exist)
  - `value` number: Value.
- `getParameters(effectType)` (method, since 0.3.0): Returns the parameters.
  - `effectType` string: ScreenEffectType value.
  - returns number[]: Parameters. ([x, y, z, w], the array itself, not a copy, four zeros if unknown)
- `setColor(effectType, color)` (method, since 0.3.0): Sets the color.
  - `effectType` string: ScreenEffectType value. (ignored if unknown)
  - `color` Color | string: Effect color. (kept as a copy, ignored if null)
- `getColor(effectType)` (method, since 0.3.0): Returns the color.
  - `effectType` string: ScreenEffectType value.
  - returns Color: Effect color. (white if unknown)
- `disableAll()` (method, since 0.3.0): Turns everything off.
- `setTimelineProperty(propertyName, value)` (method, since 0.3.0): Applies a timeline property. ("vignette" = strength (0 turns it off), "shockwave.radius" = parameter, "fade.color" = color)
  - `propertyName` string: Property name. ("effect" is the strength, "effect.color" the color, "effect.enabled" the enabled state, "effect.parameter name" a parameter, ignored if the effect is unknown)
  - `value` *: Value. (strength and parameters are converted to numbers, colors accept only HEX strings)

  Example: Change a shockwave and a fade by timeline property names

  ```js
  import { Engine, EngineConfiguration, ScreenEffect, ScreenEffectType } from "./build/vanilla.js";
  
  const engineConfiguration = new EngineConfiguration();
  const engine = new Engine(engineConfiguration);
  const graphic = engine.getGraphic();
  const screenEffect = new ScreenEffect();
  screenEffect.setTimelineProperty("shockwave", 1);
  screenEffect.setTimelineProperty("shockwave.radius", 0.4);
  screenEffect.setTimelineProperty("fade.color", "#ff8800");
  screenEffect.setParameter(ScreenEffectType.shockwave, "width", 0.2);
  console.log(screenEffect.isEnabled(ScreenEffectType.shockwave), screenEffect.getParameters(ScreenEffectType.shockwave));
  ```

- `getTime()` (method, since 0.3.0): Returns the time.
  - returns number: Effect time. (seconds)
- `getSceneTexture()` (method, since 0.3.0): Returns the scene texture. (after end, the original of the region captured in the last frame)
  - returns WebGLTexture | null: Captured region texture. (null if none yet)
- `static getEffectOrder()` (static, since 0.3.0): Returns the apply order. (static)
  - returns string[]: Application order of effect names. (a new array)
- `destroy()` (method, since 0.3.0): Destroy. (frees WebGL resources: render targets and pass programs)

## Functions and constants

- `SCREEN_EFFECT_PARAMETER_NAMES` (field, since 0.3.0): Parameter names per effect. (accessed as the timeline property "effect.name")

## ScreenEffectType (enum)

Screen effect types.

- `lensDistortion` = `lensDistortion`: --- Shape distortion ---
- `bulge` = `bulge`: Bulge / pinch. (parameters = [center x, center y, strength, radius])
- `swirl` = `swirl`: Swirl. (parameters = [center x, center y, turn count, radius])
- `kaleidoscope` = `kaleidoscope`: Kaleidoscope. (parameters = [segment count])
- `mirror` = `mirror`: Symmetry. (parameters = [0 horizontal / 1 vertical])
- `ripple` = `ripple`: Water ripple. (parameters = [center x, center y, frequency, speed])
- `heatHaze` = `heatHaze`: Heat haze. (parameters = [noise scale, speed])
- `jitter` = `jitter`: Screen shake. (parameters = [amplitude pixels, changes per second])
- `doubleVision` = `doubleVision`: Double vision. (parameters = [spacing, sway speed])
- `pixelate` = `pixelate`: Mosaic. (strength 0 → 1, parameters = [largest block pixels])
- `shockwave` = `shockwave`: Shockwave ring distortion. (parameters = [center x, center y, radius 0 ~ 1.5, width], color = ring light)
- `wave` = `wave`: Screen wave. (parameters = [frequency, speed])
- `motionBlur` = `motionBlur`: --- Blur ---
- `zoomBlur` = `zoomBlur`: Radial blur streaking toward the center. (parameters = [center x, center y, length])
- `blur` = `blur`: Gaussian blur. (parameters = [radius pixels])
- `gaussianBlur` = `gaussianBlur`: Standard-deviation Gaussian blur. (parameters = [standard deviation in pixels], same meaning as CSS filter: blur(), separable linear sampling, up to 3 sigma)
- `tiltShift` = `tiltShift`: Tilt-shift. (parameters = [focus y, focus width, radius pixels])
- `godRays` = `godRays`: Light shafts. (parameters = [center x, center y, threshold, decay], color = light color)
- `bloom` = `bloom`: Bloom of bright areas. (parameters = [threshold, strength scale, radius pixels])
- `anamorphic` = `anamorphic`: Horizontal lens flare. (parameters = [threshold, strength scale, width pixels], color = flare color)
- `glitch` = `glitch`: --- Glitch ---
- `vhs` = `vhs`: VHS tape. (shake + color bleeding + noise lines, parameters = [shake, noise])
- `chromatic` = `chromatic`: Chromatic aberration. (parameters = [scale])
- `whiteBalance` = `whiteBalance`: --- Color ---
- `liftGammaGain` = `liftGammaGain`: Lift / gamma / gain. (parameters = [lift, gamma, gain])
- `colorGrade` = `colorGrade`: Contrast / saturation / brightness + tint. (parameters = [contrast, saturation, brightness], color = tint)
- `hueShift` = `hueShift`: Hue rotation. (parameters = [turn count, multiplied by strength])
- `splitToning` = `splitToning`: Split toning. (parameters.xyz = shadow color, color = highlight color, w = balance)
- `tonemap` = `tonemap`: ACES tone mapping. (parameters = [exposure])
- `gradientMap` = `gradientMap`: Maps brightness between two colors. (parameters.xyz = dark color, color = bright color)
- `sepia` = `sepia`: Sepia.
- `grayscale` = `grayscale`: Grayscale.
- `posterize` = `posterize`: Posterize. (parameters = [step count])
- `dither` = `dither`: Ordered dithering. (parameters = [step count, pixel size])
- `halftone` = `halftone`: Halftone dots. (parameters = [grid pixels])
- `sharpen` = `sharpen`: Sharpen.
- `edgeDetect` = `edgeDetect`: Edge detection. (color = line color)
- `oldFilm` = `oldFilm`: Old film. (sepia + grain + scratches + flicker)
- `nightVision` = `nightVision`: Night vision. (color = tint)
- `invert` = `invert`: Color inversion.
- `scanline` = `scanline`: --- Pattern / mask ---
- `grain` = `grain`: Film grain.
- `rain` = `rain`: Rain streaks. (parameters = [line count, density, speed], color = streak color)
- `fog` = `fog`: Vertical fog. (parameters = [start y, end y], color = fog color)
- `vignette` = `vignette`: Vignette. (parameters = [start distance, end distance], color = edge color)
- `spotlight` = `spotlight`: Darkening outside a spotlight. (parameters = [center x, center y, radius, softness], color = outside color)
- `speedLines` = `speedLines`: Focus lines. (parameters = [center x, center y, density, speed], color = line color)
- `letterbox` = `letterbox`: Black bars at top and bottom / left and right. (parameters = [vertical bar ratio, horizontal bar ratio], color = bar color)
- `pixelDissolve` = `pixelDissolve`: --- Transitions (covered by color as strength goes 0 → 1) ---
- `wipe` = `wipe`: Directional wipe. (parameters = [direction x, direction y, softness])
- `irisWipe` = `irisWipe`: Circular iris. (parameters = [center x, center y, softness])
- `diamondWipe` = `diamondWipe`: Diamond wipe. (parameters = [softness])
- `clockWipe` = `clockWipe`: Clock wipe. (parameters = [softness])
- `blinds` = `blinds`: Blinds. (parameters = [line count, softness])
- `checkerWipe` = `checkerWipe`: Checkerboard. (parameters = [cell count])
- `noiseFade` = `noiseFade`: Noise fade. (parameters = [noise scale, softness])
- `burn` = `burn`: As if burning. (parameters = [noise scale, band width], color = cover color, the band is orange)
- `tvOff` = `tvOff`: CRT turn-off. (squashes vertically into a bright line)
- `fade` = `fade`: Fade to a solid color. (color = target color)
