chosenBy

Picks one whole effect recipe from a select prop, decided when the shader compiles.

chosenBy<K extends string>(
  prop: PropRef,
  read: (raw: unknown) =,
> K, recipes: Record<K, GatherEffect>): GatherEffect

Import patterns from shaders/std and use patterns.chosenBy.

prop must be a compileTime: true prop. read turns its raw value into one of the keys of recipes; accept both the option string and its transformed value. Every recipe must be a gather effect with the same alpha handling and no setup step, or this throws as soon as it is called.

Parameters

NameTypeNotes
propPropRef
read(raw: unknown) => K
recipesRecord<K, GatherEffect>

Example

effect: chosenBy(p('style'), (raw) => (raw === 'cmyk' || raw === 1 ? 'cmyk' : 'classic'), {classic: dotScreen({angle: p('angle'), frequency: p('frequency')}), cmyk: cmykPress({paper: p('paperColor'), frequency: p('frequency'), misprint: p('misprint'), misprintAngle: p('misprintAngle'), plates})})

Tip. Changing the prop recompiles the shader, which is the point: each style pays only for its own code.

See also

  • dotScreen — A classic halftone filter: the layer inside it redrawn as a grid of dots that grow where it is bright.
  • cmykPress — A four-color print filter: the layer inside it separated into ink plates, each screened into dots at its own angle and laid down on paper in order.

Used by

Halftone