@instantshader/react 0.4.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,8 +1,10 @@
1
+ ![InstantShader](https://raw.githubusercontent.com/ugolbck/instantshader/main/.github/assets/banner.jpg)
2
+
1
3
  # @instantshader/react
2
4
 
3
5
  React bindings for [instantshader](https://www.npmjs.com/package/instantshader), a
4
- zero-dependency animated WebGL gradient engine. Drop a `<Flow>`, `<Beam>` or
5
- `<Bloom>` component into any sized wrapper to mount a live, animated gradient. Built by
6
+ zero-dependency animated WebGL gradient engine. Drop a `<Flow>`, `<Beam>`, `<Bloom>`, `<Halo>`, `<Strata>`, `<Dune>` or
7
+ `<Whorl>` component into any sized wrapper to mount a live, animated gradient. Built by
6
8
  [InstantGradient](https://instantgradient.com/app).
7
9
 
8
10
  ## Install
@@ -35,6 +37,10 @@ One component per shader, each a thin wrapper over the same props:
35
37
  | `<Flow>` | Isotropic swirling currents with defined luminous edges and calm negative space. | `scale`, `curl`, `drift`, `openness`, `grain` |
36
38
  | `<Beam>` | One wide beam of soft light crossing a near-black frame, the palette walking its length. | `scale`, `width`, `glow`, `angle`, `grain` |
37
39
  | `<Bloom>` | A fan of huge soft petals radiating from the bottom edge, palette as concentric scalloped bands. | `scale`, `petals`, `pinch`, `bend`, `sway`, `colorflow`, `grain` |
40
+ | `<Halo>` | An eclipse disc with a bright limb and flowing corona; `x` / `y` place it, and the default is a glowing horizon arc. | `radius`, `x`, `y`, `glow`, `crescent`, `flares`, `grain` |
41
+ | `<Strata>` | Stacked cut-paper layers with soft shadows. | `scale`, `layers`, `warp`, `ridges`, `stretch`, `depth`, `blend`, `angle`, `grain` |
42
+ | `<Dune>` | Overlapping crests, crisp on top and airbrushed below. | `layers`, `swell`, `waves`, `fade`, `soft`, `angle`, `grain` |
43
+ | `<Whorl>` | A spiral of curved blades, positionable with `x` / `y`. | `blades`, `twist`, `depth`, `scale`, `wobble`, `x`, `y`, `grain` |
38
44
 
39
45
  ```tsx
40
46
  import { Bloom } from "@instantshader/react";
package/dist/index.d.ts CHANGED
@@ -1,58 +1,88 @@
1
1
  import { CSSProperties, ReactElement } from "react";
2
- import { ParamDef, ShaderDef, ShaderDef as ShaderDef$1, beam, bloom, flow } from "instantshader";
2
+ import { EffectDef, EffectLayer, EffectLayer as EffectLayer$1, EffectParamDef, ParamDef, ParamValue, ShaderDef, ShaderDef as ShaderDef$1, Source, Source as Source$1, ascii, beam, bloom, dither, dune, flow, halftone, halo, pixelate, strata, whorl } from "instantshader";
3
3
 
4
- //#region src/ShaderCanvas.d.ts
5
- interface ShaderCanvasProps {
6
- shader: ShaderDef$1;
4
+ //#region src/ShaderStack.d.ts
5
+ interface ShaderStackProps {
6
+ source: Source$1;
7
+ /** Effect layers, bottom to top. A new array with the same content does
8
+ * not recompile anything. */
9
+ effects?: EffectLayer$1[];
10
+ /** Palette. Generators draw with it; effects in "palette" color mode map
11
+ * tone through it. */
7
12
  colors: string[];
8
13
  /**
9
- * Merged into the shader's current params (removed/omitted keys are NOT
10
- * reset to their default — they keep whatever value was last applied).
11
- * Setting this prop back to `undefined` does not revert to defaults
12
- * either: the underlying MountHandle is only updated when `params` is
13
- * truthy, so the last applied values simply stick.
14
- */
15
- params?: Record<string, number>;
16
- /**
17
14
  * Setting this prop back to `undefined` does NOT revert playback to the
18
- * default speed — the underlying MountHandle is only updated when `speed`
19
- * is not `undefined`, so whatever speed was last applied keeps playing.
15
+ * default speed — the underlying handle is only updated when `speed` is
16
+ * not `undefined`, so whatever speed was last applied keeps playing.
20
17
  */
21
18
  speed?: number;
22
19
  seed?: number;
23
- /**
24
- * Makes the animation repeat seamlessly every `loopSeconds` animation
25
- * seconds. Unlike `params` and `speed`, setting this back to `undefined`
26
- * DOES take effect — it turns looping off — because "no loop" is a real
27
- * state the underlying handle can be put into.
28
- */
20
+ /** Makes the animation repeat seamlessly every `loopSeconds` animation
21
+ * seconds. Setting it back to `undefined` turns looping off. */
29
22
  loopSeconds?: number;
30
23
  paused?: boolean;
24
+ /** Shown behind contain-fit media and under media alpha. Mount-time only. */
25
+ background?: string;
26
+ /** CSS font-family for effects that draw text (ASCII). Load it first with
27
+ * `document.fonts.load`. Mount-time only. */
28
+ fontFamily?: string;
31
29
  className?: string;
32
30
  style?: CSSProperties;
33
31
  }
34
32
  /**
35
- * Mounts a live InstantShader gradient into a wrapper `<div>`. The canvas
36
- * fills that div at 100%/100% (mountGradient styles it that way), so the
37
- * consumer MUST give the wrapper an explicit size (via `style`, `className`,
38
- * or a sized parent) — this component does not impose one beyond
39
- * `position: relative`.
33
+ * Mounts a live stack into a wrapper `<div>`. The canvas fills that div at
34
+ * 100%/100%, so the consumer MUST give the wrapper an explicit size (via
35
+ * `style`, `className`, or a sized parent) — this component does not impose
36
+ * one beyond `position: relative`.
40
37
  */
41
- declare function ShaderCanvas({
42
- shader,
38
+ declare function ShaderStack({
39
+ source,
40
+ effects,
43
41
  colors,
44
- params,
45
42
  speed,
46
43
  seed,
47
44
  loopSeconds,
48
45
  paused,
46
+ background,
47
+ fontFamily,
49
48
  className,
50
49
  style
50
+ }: ShaderStackProps): ReactElement;
51
+ //#endregion
52
+ //#region src/ShaderCanvas.d.ts
53
+ interface ShaderCanvasProps extends Omit<ShaderStackProps, "source" | "effects" | "background"> {
54
+ shader: ShaderDef$1;
55
+ /**
56
+ * Merged into the shader's current params (removed/omitted keys are NOT
57
+ * reset to their default — they keep whatever value was last applied).
58
+ * Setting this prop back to `undefined` does not revert to defaults
59
+ * either: the underlying handle is only updated when `params` is truthy,
60
+ * so the last applied values simply stick.
61
+ */
62
+ params?: Record<string, number>;
63
+ /** Effect layers drawn over the gradient, bottom to top:
64
+ * `[{ effect: dither, params: { size: 4, colorMode: "palette" } }]`. */
65
+ effects?: EffectLayer$1[];
66
+ }
67
+ /**
68
+ * Mounts a live InstantShader gradient, optionally with effects over it. See
69
+ * ShaderStack for sizing: the consumer MUST give the wrapper an explicit size.
70
+ * Remounts only when `shader` or `seed` changes.
71
+ */
72
+ declare function ShaderCanvas({
73
+ shader,
74
+ params,
75
+ effects,
76
+ ...rest
51
77
  }: ShaderCanvasProps): ReactElement;
52
78
  //#endregion
53
79
  //#region src/shaders.d.ts
54
80
  declare function Flow(props: Omit<ShaderCanvasProps, "shader">): ReactElement;
55
81
  declare function Beam(props: Omit<ShaderCanvasProps, "shader">): ReactElement;
56
82
  declare function Bloom(props: Omit<ShaderCanvasProps, "shader">): ReactElement;
83
+ declare function Halo(props: Omit<ShaderCanvasProps, "shader">): ReactElement;
84
+ declare function Strata(props: Omit<ShaderCanvasProps, "shader">): ReactElement;
85
+ declare function Dune(props: Omit<ShaderCanvasProps, "shader">): ReactElement;
86
+ declare function Whorl(props: Omit<ShaderCanvasProps, "shader">): ReactElement;
57
87
  //#endregion
58
- export { Beam, Bloom, Flow, type ParamDef, ShaderCanvas, type ShaderCanvasProps, type ShaderDef, beam, bloom, flow };
88
+ export { Beam, Bloom, Dune, type EffectDef, type EffectLayer, type EffectParamDef, Flow, Halo, type ParamDef, type ParamValue, ShaderCanvas, type ShaderCanvasProps, type ShaderDef, ShaderStack, type ShaderStackProps, type Source, Strata, Whorl, ascii, beam, bloom, dither, dune, flow, halftone, halo, pixelate, strata, whorl };
package/dist/index.js CHANGED
@@ -1,30 +1,38 @@
1
1
  "use client";
2
2
  import { useEffect, useMemo, useRef } from "react";
3
- import { beam, beam as beam$1, bloom, bloom as bloom$1, flow, flow as flow$1, mountGradient } from "instantshader";
3
+ import { ascii, beam, beam as beam$1, bloom, bloom as bloom$1, dither, dune, dune as dune$1, flow, flow as flow$1, halftone, halo, halo as halo$1, mountStack, pixelate, strata, strata as strata$1, whorl, whorl as whorl$1 } from "instantshader";
4
4
  import { jsx } from "react/jsx-runtime";
5
5
 
6
- //#region src/ShaderCanvas.tsx
6
+ //#region src/ShaderStack.tsx
7
+ function sortedSig(o) {
8
+ if (!o) return "";
9
+ return Object.keys(o).sort().map((k) => `${k}:${String(o[k])}`).join(",");
10
+ }
7
11
  /**
8
- * Mounts a live InstantShader gradient into a wrapper `<div>`. The canvas
9
- * fills that div at 100%/100% (mountGradient styles it that way), so the
10
- * consumer MUST give the wrapper an explicit size (via `style`, `className`,
11
- * or a sized parent) — this component does not impose one beyond
12
- * `position: relative`.
12
+ * Mounts a live stack into a wrapper `<div>`. The canvas fills that div at
13
+ * 100%/100%, so the consumer MUST give the wrapper an explicit size (via
14
+ * `style`, `className`, or a sized parent) — this component does not impose
15
+ * one beyond `position: relative`.
13
16
  */
14
- function ShaderCanvas({ shader, colors, params, speed, seed, loopSeconds, paused, className, style }) {
17
+ function ShaderStack({ source, effects, colors, speed, seed, loopSeconds, paused, background, fontFamily, className, style }) {
15
18
  const containerRef = useRef(null);
16
19
  const handleRef = useRef(null);
17
- const p = useMemo(() => params, [params ? Object.entries(params).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([k, v]) => `${k}:${v}`).join(",") : ""]);
20
+ const sourceIdentity = source.kind === "generator" ? source.shader : source.media;
21
+ const fit = source.kind === "media" ? source.fit : void 0;
22
+ const sourceParams = useMemo(() => source.kind === "generator" ? source.params : void 0, [source.kind === "generator" ? sortedSig(source.params) : ""]);
23
+ const layers = useMemo(() => effects ?? [], [(effects ?? []).map((l) => `${l.effect.id}|${l.enabled ?? true}|${sortedSig(l.params)}`).join(";")]);
18
24
  useEffect(() => {
19
25
  const container = containerRef.current;
20
26
  if (!container) return;
21
- const handle = mountGradient(container, {
22
- shader,
27
+ const handle = mountStack(container, {
28
+ source,
29
+ effects: layers,
23
30
  colors,
24
- params: p,
25
31
  speed,
26
32
  seed,
27
- loopSeconds
33
+ loopSeconds,
34
+ background,
35
+ fontFamily
28
36
  });
29
37
  handleRef.current = handle;
30
38
  if (paused) handle.pause();
@@ -32,13 +40,22 @@ function ShaderCanvas({ shader, colors, params, speed, seed, loopSeconds, paused
32
40
  handle.dispose();
33
41
  handleRef.current = null;
34
42
  };
35
- }, [shader, seed]);
43
+ }, [
44
+ sourceIdentity,
45
+ fit,
46
+ seed,
47
+ background,
48
+ fontFamily
49
+ ]);
36
50
  useEffect(() => {
37
51
  handleRef.current?.setColors(colors);
38
52
  }, [colors]);
39
53
  useEffect(() => {
40
- if (p) handleRef.current?.setParams(p);
41
- }, [p]);
54
+ if (sourceParams) handleRef.current?.setSourceParams(sourceParams);
55
+ }, [sourceParams]);
56
+ useEffect(() => {
57
+ handleRef.current?.setEffects(layers);
58
+ }, [layers]);
42
59
  useEffect(() => {
43
60
  if (speed !== void 0) handleRef.current?.setSpeed(speed);
44
61
  }, [speed]);
@@ -59,6 +76,25 @@ function ShaderCanvas({ shader, colors, params, speed, seed, loopSeconds, paused
59
76
  });
60
77
  }
61
78
 
79
+ //#endregion
80
+ //#region src/ShaderCanvas.tsx
81
+ /**
82
+ * Mounts a live InstantShader gradient, optionally with effects over it. See
83
+ * ShaderStack for sizing: the consumer MUST give the wrapper an explicit size.
84
+ * Remounts only when `shader` or `seed` changes.
85
+ */
86
+ function ShaderCanvas({ shader, params, effects,...rest }) {
87
+ return /* @__PURE__ */ jsx(ShaderStack, {
88
+ source: {
89
+ kind: "generator",
90
+ shader,
91
+ params
92
+ },
93
+ effects,
94
+ ...rest
95
+ });
96
+ }
97
+
62
98
  //#endregion
63
99
  //#region src/shaders.tsx
64
100
  function Flow(props) {
@@ -79,6 +115,30 @@ function Bloom(props) {
79
115
  ...props
80
116
  });
81
117
  }
118
+ function Halo(props) {
119
+ return /* @__PURE__ */ jsx(ShaderCanvas, {
120
+ shader: halo$1,
121
+ ...props
122
+ });
123
+ }
124
+ function Strata(props) {
125
+ return /* @__PURE__ */ jsx(ShaderCanvas, {
126
+ shader: strata$1,
127
+ ...props
128
+ });
129
+ }
130
+ function Dune(props) {
131
+ return /* @__PURE__ */ jsx(ShaderCanvas, {
132
+ shader: dune$1,
133
+ ...props
134
+ });
135
+ }
136
+ function Whorl(props) {
137
+ return /* @__PURE__ */ jsx(ShaderCanvas, {
138
+ shader: whorl$1,
139
+ ...props
140
+ });
141
+ }
82
142
 
83
143
  //#endregion
84
- export { Beam, Bloom, Flow, ShaderCanvas, beam, bloom, flow };
144
+ export { Beam, Bloom, Dune, Flow, Halo, ShaderCanvas, ShaderStack, Strata, Whorl, ascii, beam, bloom, dither, dune, flow, halftone, halo, pixelate, strata, whorl };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instantshader/react",
3
- "version": "0.4.1",
3
+ "version": "0.6.0",
4
4
  "description": "React bindings for InstantShader animated WebGL gradients.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -36,7 +36,7 @@
36
36
  "react"
37
37
  ],
38
38
  "dependencies": {
39
- "instantshader": "0.4.1"
39
+ "instantshader": "0.6.0"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "react": "^18 || ^19",