@paper-design/shaders-react 0.0.0-canary.23f4c74 → 0.0.0-canary.2d03406
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/dist/html-in-canvas.d.ts +13 -2
- package/dist/html-in-canvas.js +61 -7
- package/dist/html-in-canvas.js.map +3 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.js.map +2 -2
- package/dist/shader-mount.d.ts +9 -0
- package/dist/shader-mount.js +15 -14
- package/dist/shader-mount.js.map +2 -2
- package/package.json +2 -2
package/dist/html-in-canvas.d.ts
CHANGED
|
@@ -1,10 +1,21 @@
|
|
|
1
|
+
/** Props of the element that holds the children. It's what gets drawn, so it takes their layout and the visuals */
|
|
2
|
+
export type ShaderLayoutProps = React.ComponentProps<'div'> & {
|
|
3
|
+
[key: `data-${string}`]: unknown;
|
|
4
|
+
};
|
|
5
|
+
/** Axes on which the shader takes the size of its content instead of its own size styles */
|
|
6
|
+
export type ShaderSizeToContent = 'width' | 'height' | 'both';
|
|
1
7
|
export declare function willDrawHtmlChildren(children: React.ReactNode): boolean;
|
|
2
8
|
/**
|
|
3
9
|
* Draws the children into u_image where the browser supports it, otherwise they're shown on top of the shader.
|
|
4
|
-
* Either way they're held by the same
|
|
10
|
+
* Either way they're held by the same content element, so they lay out the same.
|
|
11
|
+
*
|
|
12
|
+
* The content element fills the shader, except on the axes sized to content: there it takes the size of the children,
|
|
13
|
+
* and the shader follows it. The canvas can't be sized by what's inside it, so the content size is measured and
|
|
14
|
+
* set on the shader element. Resize observer callbacks run before paint, so this settles within the same frame.
|
|
5
15
|
*/
|
|
6
|
-
export declare function useHtmlChildren(acceptsHtmlChildren: boolean, children: React.ReactNode): {
|
|
16
|
+
export declare function useHtmlChildren(acceptsHtmlChildren: boolean, children: React.ReactNode, layoutProps: ShaderLayoutProps | undefined, sizeToContent: ShaderSizeToContent | undefined, shaderRef: React.RefObject<HTMLElement | null>): {
|
|
7
17
|
hasHtmlChildren: boolean;
|
|
8
18
|
addHtmlUniform: <T extends object>(uniforms: T) => T;
|
|
9
19
|
renderedChildren: React.ReactNode;
|
|
20
|
+
sizeStyle: React.CSSProperties | undefined;
|
|
10
21
|
};
|
package/dist/html-in-canvas.js
CHANGED
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
5
5
|
|
|
6
6
|
"use client";
|
|
7
|
-
import { Children, useRef, useSyncExternalStore } from "react";
|
|
7
|
+
import { Children, useLayoutEffect, useRef, useSyncExternalStore } from "react";
|
|
8
8
|
import { isHtmlInCanvasSupported } from "@paper-design/shaders";
|
|
9
|
+
import { useMergeRefs } from "./use-merge-refs.js";
|
|
9
10
|
import { jsx } from "react/jsx-runtime";
|
|
10
11
|
const subscribeToNothing = () => () => {
|
|
11
12
|
};
|
|
@@ -16,20 +17,73 @@ function hasChildren(children) {
|
|
|
16
17
|
function willDrawHtmlChildren(children) {
|
|
17
18
|
return hasChildren(children) && isHtmlInCanvasSupported();
|
|
18
19
|
}
|
|
19
|
-
function useHtmlChildren(acceptsHtmlChildren, children) {
|
|
20
|
-
const
|
|
20
|
+
function useHtmlChildren(acceptsHtmlChildren, children, layoutProps, sizeToContent, shaderRef) {
|
|
21
|
+
const contentRef = useRef(null);
|
|
22
|
+
const mergedContentRef = useMergeRefs([contentRef, layoutProps?.ref]);
|
|
21
23
|
const isSupported = useSyncExternalStore(subscribeToNothing, isHtmlInCanvasSupported, isSupportedOnServer);
|
|
22
24
|
const isWrapped = acceptsHtmlChildren && hasChildren(children);
|
|
23
25
|
const hasHtmlChildren = isWrapped && isSupported;
|
|
24
|
-
const
|
|
26
|
+
const fitsWidth = isWrapped && (sizeToContent === "width" || sizeToContent === "both");
|
|
27
|
+
const fitsHeight = isWrapped && (sizeToContent === "height" || sizeToContent === "both");
|
|
28
|
+
useLayoutEffect(() => {
|
|
29
|
+
const content2 = contentRef.current;
|
|
30
|
+
const shader = shaderRef.current;
|
|
31
|
+
if (!content2 || !shader || !fitsWidth && !fitsHeight) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
let contentWidth = 0;
|
|
35
|
+
let contentHeight = 0;
|
|
36
|
+
const observer = new ResizeObserver((entries) => {
|
|
37
|
+
for (const entry of entries) {
|
|
38
|
+
const size = entry.borderBoxSize[0];
|
|
39
|
+
if (entry.target === content2 && size) {
|
|
40
|
+
contentWidth = size.inlineSize;
|
|
41
|
+
contentHeight = size.blockSize;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const style = getComputedStyle(shader);
|
|
45
|
+
let borderX = 0;
|
|
46
|
+
let borderY = 0;
|
|
47
|
+
if (style.boxSizing === "border-box") {
|
|
48
|
+
borderX = parseFloat(style.borderLeftWidth) + parseFloat(style.borderRightWidth);
|
|
49
|
+
borderY = parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth);
|
|
50
|
+
}
|
|
51
|
+
shader.style.setProperty("--paper-shader-content-width", `${contentWidth + borderX}px`);
|
|
52
|
+
shader.style.setProperty("--paper-shader-content-height", `${contentHeight + borderY}px`);
|
|
53
|
+
});
|
|
54
|
+
observer.observe(content2);
|
|
55
|
+
observer.observe(shader);
|
|
56
|
+
return () => {
|
|
57
|
+
observer.disconnect();
|
|
58
|
+
};
|
|
59
|
+
}, [fitsWidth, fitsHeight, hasHtmlChildren, shaderRef]);
|
|
60
|
+
const addHtmlUniform = (uniforms) => hasHtmlChildren && contentRef.current ? { ...uniforms, u_image: contentRef.current } : uniforms;
|
|
25
61
|
if (!isWrapped) {
|
|
26
|
-
return { hasHtmlChildren, addHtmlUniform, renderedChildren: children };
|
|
62
|
+
return { hasHtmlChildren, addHtmlUniform, renderedChildren: children, sizeStyle: void 0 };
|
|
27
63
|
}
|
|
28
|
-
const
|
|
64
|
+
const contentStyle = { ...layoutProps?.style };
|
|
65
|
+
if (fitsWidth) {
|
|
66
|
+
contentStyle.width = "max-content";
|
|
67
|
+
}
|
|
68
|
+
if (fitsHeight) {
|
|
69
|
+
contentStyle.height = "auto";
|
|
70
|
+
}
|
|
71
|
+
let sizeStyle;
|
|
72
|
+
if (fitsWidth || fitsHeight) {
|
|
73
|
+
sizeStyle = {};
|
|
74
|
+
if (fitsWidth) {
|
|
75
|
+
sizeStyle.width = "var(--paper-shader-content-width)";
|
|
76
|
+
}
|
|
77
|
+
if (fitsHeight) {
|
|
78
|
+
sizeStyle.height = "var(--paper-shader-content-height)";
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const content = /* @__PURE__ */ jsx("div", { ...layoutProps, ref: mergedContentRef, style: contentStyle, ...{ drawable: "" }, children });
|
|
29
82
|
return {
|
|
30
83
|
hasHtmlChildren,
|
|
31
84
|
addHtmlUniform,
|
|
32
|
-
renderedChildren: hasHtmlChildren ? /* @__PURE__ */ jsx("canvas", { ...{ content: "drawable" }, children:
|
|
85
|
+
renderedChildren: hasHtmlChildren ? /* @__PURE__ */ jsx("canvas", { ...{ content: "drawable", layoutsubtree: "" }, children: content }) : content,
|
|
86
|
+
sizeStyle
|
|
33
87
|
};
|
|
34
88
|
}
|
|
35
89
|
export {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/html-in-canvas.tsx"],
|
|
4
|
-
"sourcesContent": ["'use client';\n\nimport { Children, useRef, useSyncExternalStore } from 'react';\nimport { isHtmlInCanvasSupported } from '@paper-design/shaders';\n\nconst subscribeToNothing = () => () => {};\n// Assumed on the server, so that supporting browsers don't re-render the canvas after hydration\nconst isSupportedOnServer = () => true;\n\nfunction hasChildren(children: React.ReactNode): boolean {\n return Children.toArray(children).length > 0;\n}\n\nexport function willDrawHtmlChildren(children: React.ReactNode): boolean {\n return hasChildren(children) && isHtmlInCanvasSupported();\n}\n\n/**\n * Draws the children into u_image where the browser supports it, otherwise they're shown on top of the shader.\n * Either way they're held by the same
|
|
5
|
-
"mappings": ";;;;;;AAEA,SAAS,UAAU,QAAQ,4BAA4B;
|
|
6
|
-
"names": []
|
|
4
|
+
"sourcesContent": ["'use client';\n\nimport { Children, useLayoutEffect, useRef, useSyncExternalStore } from 'react';\nimport { isHtmlInCanvasSupported } from '@paper-design/shaders';\nimport { useMergeRefs } from './use-merge-refs.js';\n\nconst subscribeToNothing = () => () => {};\n// Assumed on the server, so that supporting browsers don't re-render the canvas after hydration\nconst isSupportedOnServer = () => true;\n\n/** Props of the element that holds the children. It's what gets drawn, so it takes their layout and the visuals */\nexport type ShaderLayoutProps = React.ComponentProps<'div'> & { [key: `data-${string}`]: unknown };\n\n/** Axes on which the shader takes the size of its content instead of its own size styles */\nexport type ShaderSizeToContent = 'width' | 'height' | 'both';\n\nfunction hasChildren(children: React.ReactNode): boolean {\n return Children.toArray(children).length > 0;\n}\n\nexport function willDrawHtmlChildren(children: React.ReactNode): boolean {\n return hasChildren(children) && isHtmlInCanvasSupported();\n}\n\n/**\n * Draws the children into u_image where the browser supports it, otherwise they're shown on top of the shader.\n * Either way they're held by the same content element, so they lay out the same.\n *\n * The content element fills the shader, except on the axes sized to content: there it takes the size of the children,\n * and the shader follows it. The canvas can't be sized by what's inside it, so the content size is measured and\n * set on the shader element. Resize observer callbacks run before paint, so this settles within the same frame.\n */\nexport function useHtmlChildren(\n acceptsHtmlChildren: boolean,\n children: React.ReactNode,\n layoutProps: ShaderLayoutProps | undefined,\n sizeToContent: ShaderSizeToContent | undefined,\n shaderRef: React.RefObject<HTMLElement | null>\n): {\n hasHtmlChildren: boolean;\n addHtmlUniform: <T extends object>(uniforms: T) => T;\n renderedChildren: React.ReactNode;\n sizeStyle: React.CSSProperties | undefined;\n} {\n const contentRef = useRef<HTMLDivElement>(null);\n const mergedContentRef = useMergeRefs([contentRef, layoutProps?.ref]);\n const isSupported = useSyncExternalStore(subscribeToNothing, isHtmlInCanvasSupported, isSupportedOnServer);\n const isWrapped = acceptsHtmlChildren && hasChildren(children);\n const hasHtmlChildren = isWrapped && isSupported;\n\n const fitsWidth = isWrapped && (sizeToContent === 'width' || sizeToContent === 'both');\n const fitsHeight = isWrapped && (sizeToContent === 'height' || sizeToContent === 'both');\n\n useLayoutEffect(() => {\n const content = contentRef.current;\n const shader = shaderRef.current;\n if (!content || !shader || (!fitsWidth && !fitsHeight)) {\n return;\n }\n\n let contentWidth = 0;\n let contentHeight = 0;\n\n // The shader is observed too, so that its border changes are picked up\n const observer = new ResizeObserver((entries) => {\n for (const entry of entries) {\n const size = entry.borderBoxSize[0];\n if (entry.target === content && size) {\n contentWidth = size.inlineSize;\n contentHeight = size.blockSize;\n }\n }\n\n // The shader's own border sits around the content\n const style = getComputedStyle(shader);\n let borderX = 0;\n let borderY = 0;\n if (style.boxSizing === 'border-box') {\n borderX = parseFloat(style.borderLeftWidth) + parseFloat(style.borderRightWidth);\n borderY = parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth);\n }\n\n shader.style.setProperty('--paper-shader-content-width', `${contentWidth + borderX}px`);\n shader.style.setProperty('--paper-shader-content-height', `${contentHeight + borderY}px`);\n });\n observer.observe(content);\n observer.observe(shader);\n\n return () => {\n observer.disconnect();\n };\n }, [fitsWidth, fitsHeight, hasHtmlChildren, shaderRef]);\n\n const addHtmlUniform = <T extends object>(uniforms: T): T =>\n hasHtmlChildren && contentRef.current ? { ...uniforms, u_image: contentRef.current } : uniforms;\n\n if (!isWrapped) {\n return { hasHtmlChildren, addHtmlUniform, renderedChildren: children, sizeStyle: undefined };\n }\n\n // Sized to content: the content element takes the size of the children, which can overflow the canvas\n const contentStyle: React.CSSProperties = { ...layoutProps?.style };\n if (fitsWidth) {\n contentStyle.width = 'max-content';\n }\n if (fitsHeight) {\n contentStyle.height = 'auto';\n }\n\n // The shader takes the measured content size\n let sizeStyle: React.CSSProperties | undefined;\n if (fitsWidth || fitsHeight) {\n sizeStyle = {};\n if (fitsWidth) {\n sizeStyle.width = 'var(--paper-shader-content-width)';\n }\n if (fitsHeight) {\n sizeStyle.height = 'var(--paper-shader-content-height)';\n }\n }\n\n const content = (\n <div {...layoutProps} ref={mergedContentRef} style={contentStyle} {...{ drawable: '' }}>\n {children}\n </div>\n );\n\n return {\n hasHtmlChildren,\n addHtmlUniform,\n renderedChildren: hasHtmlChildren ? (\n <canvas {...{ content: 'drawable', layoutsubtree: '' }}>{content}</canvas>\n ) : (\n content\n ),\n sizeStyle,\n };\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;AAEA,SAAS,UAAU,iBAAiB,QAAQ,4BAA4B;AACxE,SAAS,+BAA+B;AACxC,SAAS,oBAAoB;AAsHzB;AApHJ,MAAM,qBAAqB,MAAM,MAAM;AAAC;AAExC,MAAM,sBAAsB,MAAM;AAQlC,SAAS,YAAY,UAAoC;AACvD,SAAO,SAAS,QAAQ,QAAQ,EAAE,SAAS;AAC7C;AAEO,SAAS,qBAAqB,UAAoC;AACvE,SAAO,YAAY,QAAQ,KAAK,wBAAwB;AAC1D;AAUO,SAAS,gBACd,qBACA,UACA,aACA,eACA,WAMA;AACA,QAAM,aAAa,OAAuB,IAAI;AAC9C,QAAM,mBAAmB,aAAa,CAAC,YAAY,aAAa,GAAG,CAAC;AACpE,QAAM,cAAc,qBAAqB,oBAAoB,yBAAyB,mBAAmB;AACzG,QAAM,YAAY,uBAAuB,YAAY,QAAQ;AAC7D,QAAM,kBAAkB,aAAa;AAErC,QAAM,YAAY,cAAc,kBAAkB,WAAW,kBAAkB;AAC/E,QAAM,aAAa,cAAc,kBAAkB,YAAY,kBAAkB;AAEjF,kBAAgB,MAAM;AACpB,UAAMA,WAAU,WAAW;AAC3B,UAAM,SAAS,UAAU;AACzB,QAAI,CAACA,YAAW,CAAC,UAAW,CAAC,aAAa,CAAC,YAAa;AACtD;AAAA,IACF;AAEA,QAAI,eAAe;AACnB,QAAI,gBAAgB;AAGpB,UAAM,WAAW,IAAI,eAAe,CAAC,YAAY;AAC/C,iBAAW,SAAS,SAAS;AAC3B,cAAM,OAAO,MAAM,cAAc,CAAC;AAClC,YAAI,MAAM,WAAWA,YAAW,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BAAkB,kBAChB,oBAAC,YAAQ,GAAG,EAAE,SAAS,YAAY,eAAe,GAAG,GAAI,mBAAQ,IAEjE;AAAA,IAEF;AAAA,EACF;AACF;",
|
|
6
|
+
"names": ["content"]
|
|
7
7
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { ShaderMount } from './shader-mount.js';
|
|
2
2
|
export type { ShaderMountProps, ShaderComponentProps } from './shader-mount.js';
|
|
3
|
+
export type { ShaderLayoutProps, ShaderSizeToContent } from './html-in-canvas.js';
|
|
3
4
|
export { MeshGradient, meshGradientPresets } from './shaders/mesh-gradient.js';
|
|
4
5
|
export type { MeshGradientProps } from './shaders/mesh-gradient.js';
|
|
5
6
|
export type { MeshGradientUniforms, MeshGradientParams } from '@paper-design/shaders';
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["'use client';\n\nexport { ShaderMount } from './shader-mount.js';\nexport type { ShaderMountProps, ShaderComponentProps } from './shader-mount.js';\n\nexport { MeshGradient, meshGradientPresets } from './shaders/mesh-gradient.js';\nexport type { MeshGradientProps } from './shaders/mesh-gradient.js';\nexport type { MeshGradientUniforms, MeshGradientParams } from '@paper-design/shaders';\n\nexport { SmokeRing, smokeRingPresets } from './shaders/smoke-ring.js';\nexport type { SmokeRingProps } from './shaders/smoke-ring.js';\nexport type { SmokeRingUniforms, SmokeRingParams } from '@paper-design/shaders';\n\nexport { NeuroNoise, neuroNoisePresets } from './shaders/neuro-noise.js';\nexport type { NeuroNoiseProps } from './shaders/neuro-noise.js';\nexport type { NeuroNoiseUniforms, NeuroNoiseParams } from '@paper-design/shaders';\n\nexport { DotOrbit, dotOrbitPresets } from './shaders/dot-orbit.js';\nexport type { DotOrbitProps } from './shaders/dot-orbit.js';\nexport type { DotOrbitUniforms, DotOrbitParams } from '@paper-design/shaders';\n\nexport { DotGrid, dotGridPresets } from './shaders/dot-grid.js';\nexport type { DotGridProps } from './shaders/dot-grid.js';\nexport type { DotGridUniforms, DotGridParams } from '@paper-design/shaders';\n\nexport { SimplexNoise, simplexNoisePresets } from './shaders/simplex-noise.js';\nexport type { SimplexNoiseProps } from './shaders/simplex-noise.js';\nexport type { SimplexNoiseUniforms, SimplexNoiseParams } from '@paper-design/shaders';\n\nexport { Metaballs, metaballsPresets } from './shaders/metaballs.js';\nexport type { MetaballsProps } from './shaders/metaballs.js';\nexport type { MetaballsUniforms, MetaballsParams } from '@paper-design/shaders';\n\nexport { Waves, wavesPresets } from './shaders/waves.js';\nexport type { WavesProps } from './shaders/waves.js';\nexport type { WavesUniforms, WavesParams } from '@paper-design/shaders';\n\nexport { PerlinNoise, perlinNoisePresets } from './shaders/perlin-noise.js';\nexport type { PerlinNoiseProps } from './shaders/perlin-noise.js';\nexport type { PerlinNoiseUniforms, PerlinNoiseParams } from '@paper-design/shaders';\n\nexport { Voronoi, voronoiPresets } from './shaders/voronoi.js';\nexport type { VoronoiProps } from './shaders/voronoi.js';\nexport type { VoronoiUniforms, VoronoiParams } from '@paper-design/shaders';\n\nexport { Warp, warpPresets } from './shaders/warp.js';\nexport type { WarpProps } from './shaders/warp.js';\nexport type { WarpUniforms, WarpParams, WarpPattern } from '@paper-design/shaders';\n\nexport { GodRays, godRaysPresets } from './shaders/god-rays.js';\nexport type { GodRaysProps } from './shaders/god-rays.js';\nexport type { GodRaysUniforms, GodRaysParams } from '@paper-design/shaders';\n\nexport { Spiral, spiralPresets } from './shaders/spiral.js';\nexport type { SpiralProps } from './shaders/spiral.js';\nexport type { SpiralUniforms, SpiralParams } from '@paper-design/shaders';\n\nexport { Swirl, swirlPresets } from './shaders/swirl.js';\nexport type { SwirlProps } from './shaders/swirl.js';\nexport type { SwirlUniforms, SwirlParams } from '@paper-design/shaders';\n\nexport { Dithering, ditheringPresets } from './shaders/dithering.js';\nexport type { DitheringProps } from './shaders/dithering.js';\nexport type { DitheringUniforms, DitheringParams } from '@paper-design/shaders';\n\nexport { GrainGradient, grainGradientPresets } from './shaders/grain-gradient.js';\nexport type { GrainGradientProps } from './shaders/grain-gradient.js';\nexport type { GrainGradientUniforms, GrainGradientParams } from '@paper-design/shaders';\n\nexport { PulsingBorder, pulsingBorderPresets } from './shaders/pulsing-border.js';\nexport type { PulsingBorderProps } from './shaders/pulsing-border.js';\nexport type { PulsingBorderUniforms, PulsingBorderParams } from '@paper-design/shaders';\n\nexport { ColorPanels, colorPanelsPresets } from './shaders/color-panels.js';\nexport type { ColorPanelsProps } from './shaders/color-panels.js';\nexport type { ColorPanelsUniforms, ColorPanelsParams } from '@paper-design/shaders';\n\nexport { StaticMeshGradient, staticMeshGradientPresets } from './shaders/static-mesh-gradient.js';\nexport type { StaticMeshGradientProps } from './shaders/static-mesh-gradient.js';\nexport type { StaticMeshGradientUniforms, StaticMeshGradientParams } from '@paper-design/shaders';\n\nexport { StaticRadialGradient, staticRadialGradientPresets } from './shaders/static-radial-gradient.js';\nexport type { StaticRadialGradientProps } from './shaders/static-radial-gradient.js';\nexport type { StaticRadialGradientUniforms, StaticRadialGradientParams } from '@paper-design/shaders';\n\nexport { PaperTexture, paperTexturePresets } from './shaders/paper-texture.js';\nexport type { PaperTextureProps } from './shaders/paper-texture.js';\nexport type { PaperTextureUniforms, PaperTextureParams } from '@paper-design/shaders';\n\nexport { FlutedGlass, flutedGlassPresets } from './shaders/fluted-glass.js';\nexport { type FlutedGlassProps } from './shaders/fluted-glass.js';\nexport { type FlutedGlassUniforms, type FlutedGlassParams } from '@paper-design/shaders';\n\nexport { Water, waterPresets } from './shaders/water.js';\nexport type { WaterProps } from './shaders/water.js';\nexport type { WaterUniforms, WaterParams } from '@paper-design/shaders';\n\nexport { ImageDithering, imageDitheringPresets } from './shaders/image-dithering.js';\nexport type { ImageDitheringProps } from './shaders/image-dithering.js';\nexport type { ImageDitheringUniforms, ImageDitheringParams } from '@paper-design/shaders';\n\nexport { LensDistortion, lensDistortionPresets } from './shaders/lens-distortion.js';\nexport type { LensDistortionProps } from './shaders/lens-distortion.js';\nexport type { LensDistortionUniforms, LensDistortionParams } from '@paper-design/shaders';\n\nexport { Heatmap, heatmapPresets } from './shaders/heatmap.js';\nexport type { HeatmapProps } from './shaders/heatmap.js';\nexport type { HeatmapUniforms, HeatmapParams } from '@paper-design/shaders';\n\nexport { LiquidMetal, liquidMetalPresets } from './shaders/liquid-metal.js';\nexport type { LiquidMetalProps } from './shaders/liquid-metal.js';\nexport type { LiquidMetalUniforms, LiquidMetalParams } from '@paper-design/shaders';\n\nexport { HalftoneDots, halftoneDotsPresets } from './shaders/halftone-dots.js';\nexport type { HalftoneDotsProps } from './shaders/halftone-dots.js';\nexport type { HalftoneDotsUniforms, HalftoneDotsParams } from '@paper-design/shaders';\n\nexport { HalftoneCmyk, halftoneCmykPresets } from './shaders/halftone-cmyk.js';\nexport type { HalftoneCmykProps } from './shaders/halftone-cmyk.js';\nexport type { HalftoneCmykUniforms, HalftoneCmykParams } from '@paper-design/shaders';\n\nexport { GemSmoke, gemSmokePresets } from './shaders/gem-smoke.js';\nexport type { GemSmokeProps } from './shaders/gem-smoke.js';\nexport type { GemSmokeUniforms, GemSmokeParams } from '@paper-design/shaders';\n\nexport { isPaperShaderElement, getShaderColorFromString } from '@paper-design/shaders';\nexport type { PaperShaderElement, ShaderFit, ShaderSizingParams, ShaderSizingUniforms } from '@paper-design/shaders';\n\nexport {\n colorPanelsMeta,\n dotOrbitMeta,\n godRaysMeta,\n grainGradientMeta,\n meshGradientMeta,\n metaballsMeta,\n paperTextureMeta,\n pulsingBorderMeta,\n simplexNoiseMeta,\n smokeRingMeta,\n swirlMeta,\n voronoiMeta,\n warpMeta,\n heatmapMeta,\n lensDistortionMeta,\n staticMeshGradientMeta,\n staticRadialGradientMeta,\n} from '@paper-design/shaders';\n"],
|
|
5
|
-
"mappings": ";;;;;;AAEA,SAAS,mBAAmB;
|
|
4
|
+
"sourcesContent": ["'use client';\n\nexport { ShaderMount } from './shader-mount.js';\nexport type { ShaderMountProps, ShaderComponentProps } from './shader-mount.js';\nexport type { ShaderLayoutProps, ShaderSizeToContent } from './html-in-canvas.js';\n\nexport { MeshGradient, meshGradientPresets } from './shaders/mesh-gradient.js';\nexport type { MeshGradientProps } from './shaders/mesh-gradient.js';\nexport type { MeshGradientUniforms, MeshGradientParams } from '@paper-design/shaders';\n\nexport { SmokeRing, smokeRingPresets } from './shaders/smoke-ring.js';\nexport type { SmokeRingProps } from './shaders/smoke-ring.js';\nexport type { SmokeRingUniforms, SmokeRingParams } from '@paper-design/shaders';\n\nexport { NeuroNoise, neuroNoisePresets } from './shaders/neuro-noise.js';\nexport type { NeuroNoiseProps } from './shaders/neuro-noise.js';\nexport type { NeuroNoiseUniforms, NeuroNoiseParams } from '@paper-design/shaders';\n\nexport { DotOrbit, dotOrbitPresets } from './shaders/dot-orbit.js';\nexport type { DotOrbitProps } from './shaders/dot-orbit.js';\nexport type { DotOrbitUniforms, DotOrbitParams } from '@paper-design/shaders';\n\nexport { DotGrid, dotGridPresets } from './shaders/dot-grid.js';\nexport type { DotGridProps } from './shaders/dot-grid.js';\nexport type { DotGridUniforms, DotGridParams } from '@paper-design/shaders';\n\nexport { SimplexNoise, simplexNoisePresets } from './shaders/simplex-noise.js';\nexport type { SimplexNoiseProps } from './shaders/simplex-noise.js';\nexport type { SimplexNoiseUniforms, SimplexNoiseParams } from '@paper-design/shaders';\n\nexport { Metaballs, metaballsPresets } from './shaders/metaballs.js';\nexport type { MetaballsProps } from './shaders/metaballs.js';\nexport type { MetaballsUniforms, MetaballsParams } from '@paper-design/shaders';\n\nexport { Waves, wavesPresets } from './shaders/waves.js';\nexport type { WavesProps } from './shaders/waves.js';\nexport type { WavesUniforms, WavesParams } from '@paper-design/shaders';\n\nexport { PerlinNoise, perlinNoisePresets } from './shaders/perlin-noise.js';\nexport type { PerlinNoiseProps } from './shaders/perlin-noise.js';\nexport type { PerlinNoiseUniforms, PerlinNoiseParams } from '@paper-design/shaders';\n\nexport { Voronoi, voronoiPresets } from './shaders/voronoi.js';\nexport type { VoronoiProps } from './shaders/voronoi.js';\nexport type { VoronoiUniforms, VoronoiParams } from '@paper-design/shaders';\n\nexport { Warp, warpPresets } from './shaders/warp.js';\nexport type { WarpProps } from './shaders/warp.js';\nexport type { WarpUniforms, WarpParams, WarpPattern } from '@paper-design/shaders';\n\nexport { GodRays, godRaysPresets } from './shaders/god-rays.js';\nexport type { GodRaysProps } from './shaders/god-rays.js';\nexport type { GodRaysUniforms, GodRaysParams } from '@paper-design/shaders';\n\nexport { Spiral, spiralPresets } from './shaders/spiral.js';\nexport type { SpiralProps } from './shaders/spiral.js';\nexport type { SpiralUniforms, SpiralParams } from '@paper-design/shaders';\n\nexport { Swirl, swirlPresets } from './shaders/swirl.js';\nexport type { SwirlProps } from './shaders/swirl.js';\nexport type { SwirlUniforms, SwirlParams } from '@paper-design/shaders';\n\nexport { Dithering, ditheringPresets } from './shaders/dithering.js';\nexport type { DitheringProps } from './shaders/dithering.js';\nexport type { DitheringUniforms, DitheringParams } from '@paper-design/shaders';\n\nexport { GrainGradient, grainGradientPresets } from './shaders/grain-gradient.js';\nexport type { GrainGradientProps } from './shaders/grain-gradient.js';\nexport type { GrainGradientUniforms, GrainGradientParams } from '@paper-design/shaders';\n\nexport { PulsingBorder, pulsingBorderPresets } from './shaders/pulsing-border.js';\nexport type { PulsingBorderProps } from './shaders/pulsing-border.js';\nexport type { PulsingBorderUniforms, PulsingBorderParams } from '@paper-design/shaders';\n\nexport { ColorPanels, colorPanelsPresets } from './shaders/color-panels.js';\nexport type { ColorPanelsProps } from './shaders/color-panels.js';\nexport type { ColorPanelsUniforms, ColorPanelsParams } from '@paper-design/shaders';\n\nexport { StaticMeshGradient, staticMeshGradientPresets } from './shaders/static-mesh-gradient.js';\nexport type { StaticMeshGradientProps } from './shaders/static-mesh-gradient.js';\nexport type { StaticMeshGradientUniforms, StaticMeshGradientParams } from '@paper-design/shaders';\n\nexport { StaticRadialGradient, staticRadialGradientPresets } from './shaders/static-radial-gradient.js';\nexport type { StaticRadialGradientProps } from './shaders/static-radial-gradient.js';\nexport type { StaticRadialGradientUniforms, StaticRadialGradientParams } from '@paper-design/shaders';\n\nexport { PaperTexture, paperTexturePresets } from './shaders/paper-texture.js';\nexport type { PaperTextureProps } from './shaders/paper-texture.js';\nexport type { PaperTextureUniforms, PaperTextureParams } from '@paper-design/shaders';\n\nexport { FlutedGlass, flutedGlassPresets } from './shaders/fluted-glass.js';\nexport { type FlutedGlassProps } from './shaders/fluted-glass.js';\nexport { type FlutedGlassUniforms, type FlutedGlassParams } from '@paper-design/shaders';\n\nexport { Water, waterPresets } from './shaders/water.js';\nexport type { WaterProps } from './shaders/water.js';\nexport type { WaterUniforms, WaterParams } from '@paper-design/shaders';\n\nexport { ImageDithering, imageDitheringPresets } from './shaders/image-dithering.js';\nexport type { ImageDitheringProps } from './shaders/image-dithering.js';\nexport type { ImageDitheringUniforms, ImageDitheringParams } from '@paper-design/shaders';\n\nexport { LensDistortion, lensDistortionPresets } from './shaders/lens-distortion.js';\nexport type { LensDistortionProps } from './shaders/lens-distortion.js';\nexport type { LensDistortionUniforms, LensDistortionParams } from '@paper-design/shaders';\n\nexport { Heatmap, heatmapPresets } from './shaders/heatmap.js';\nexport type { HeatmapProps } from './shaders/heatmap.js';\nexport type { HeatmapUniforms, HeatmapParams } from '@paper-design/shaders';\n\nexport { LiquidMetal, liquidMetalPresets } from './shaders/liquid-metal.js';\nexport type { LiquidMetalProps } from './shaders/liquid-metal.js';\nexport type { LiquidMetalUniforms, LiquidMetalParams } from '@paper-design/shaders';\n\nexport { HalftoneDots, halftoneDotsPresets } from './shaders/halftone-dots.js';\nexport type { HalftoneDotsProps } from './shaders/halftone-dots.js';\nexport type { HalftoneDotsUniforms, HalftoneDotsParams } from '@paper-design/shaders';\n\nexport { HalftoneCmyk, halftoneCmykPresets } from './shaders/halftone-cmyk.js';\nexport type { HalftoneCmykProps } from './shaders/halftone-cmyk.js';\nexport type { HalftoneCmykUniforms, HalftoneCmykParams } from '@paper-design/shaders';\n\nexport { GemSmoke, gemSmokePresets } from './shaders/gem-smoke.js';\nexport type { GemSmokeProps } from './shaders/gem-smoke.js';\nexport type { GemSmokeUniforms, GemSmokeParams } from '@paper-design/shaders';\n\nexport { isPaperShaderElement, getShaderColorFromString } from '@paper-design/shaders';\nexport type { PaperShaderElement, ShaderFit, ShaderSizingParams, ShaderSizingUniforms } from '@paper-design/shaders';\n\nexport {\n colorPanelsMeta,\n dotOrbitMeta,\n godRaysMeta,\n grainGradientMeta,\n meshGradientMeta,\n metaballsMeta,\n paperTextureMeta,\n pulsingBorderMeta,\n simplexNoiseMeta,\n smokeRingMeta,\n swirlMeta,\n voronoiMeta,\n warpMeta,\n heatmapMeta,\n lensDistortionMeta,\n staticMeshGradientMeta,\n staticRadialGradientMeta,\n} from '@paper-design/shaders';\n"],
|
|
5
|
+
"mappings": ";;;;;;AAEA,SAAS,mBAAmB;AAI5B,SAAS,cAAc,2BAA2B;AAIlD,SAAS,WAAW,wBAAwB;AAI5C,SAAS,YAAY,yBAAyB;AAI9C,SAAS,UAAU,uBAAuB;AAI1C,SAAS,SAAS,sBAAsB;AAIxC,SAAS,cAAc,2BAA2B;AAIlD,SAAS,WAAW,wBAAwB;AAI5C,SAAS,OAAO,oBAAoB;AAIpC,SAAS,aAAa,0BAA0B;AAIhD,SAAS,SAAS,sBAAsB;AAIxC,SAAS,MAAM,mBAAmB;AAIlC,SAAS,SAAS,sBAAsB;AAIxC,SAAS,QAAQ,qBAAqB;AAItC,SAAS,OAAO,oBAAoB;AAIpC,SAAS,WAAW,wBAAwB;AAI5C,SAAS,eAAe,4BAA4B;AAIpD,SAAS,eAAe,4BAA4B;AAIpD,SAAS,aAAa,0BAA0B;AAIhD,SAAS,oBAAoB,iCAAiC;AAI9D,SAAS,sBAAsB,mCAAmC;AAIlE,SAAS,cAAc,2BAA2B;AAIlD,SAAS,aAAa,0BAA0B;AAChD,eAAsC;AACtC,eAAiE;AAEjE,SAAS,OAAO,oBAAoB;AAIpC,SAAS,gBAAgB,6BAA6B;AAItD,SAAS,gBAAgB,6BAA6B;AAItD,SAAS,SAAS,sBAAsB;AAIxC,SAAS,aAAa,0BAA0B;AAIhD,SAAS,cAAc,2BAA2B;AAIlD,SAAS,cAAc,2BAA2B;AAIlD,SAAS,UAAU,uBAAuB;AAI1C,SAAS,sBAAsB,gCAAgC;AAG/D;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/shader-mount.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type PaperShaderElement, type ShaderMotionParams } from '@paper-design/shaders';
|
|
2
|
+
import { type ShaderLayoutProps, type ShaderSizeToContent } from './html-in-canvas.js';
|
|
2
3
|
/**
|
|
3
4
|
* React Shader Mount can also accept strings as uniform values, which will assumed to be URLs and loaded as images
|
|
4
5
|
*
|
|
@@ -18,6 +19,10 @@ export interface ShaderMountProps extends Omit<React.ComponentProps<'div'>, 'col
|
|
|
18
19
|
webGlContextAttributes?: WebGLContextAttributes;
|
|
19
20
|
/** Set by shaders that can draw their children into u_image (experimental) */
|
|
20
21
|
acceptsHtmlChildren?: boolean;
|
|
22
|
+
/** Props of the element that holds the children and is drawn with them: layout, padding, background, border */
|
|
23
|
+
layoutProps?: ShaderLayoutProps;
|
|
24
|
+
/** Size the shader to its children on these axes instead of its own width and height styles */
|
|
25
|
+
sizeToContent?: ShaderSizeToContent;
|
|
21
26
|
/** Inline CSS width style */
|
|
22
27
|
width?: string | number;
|
|
23
28
|
/** Inline CSS height style */
|
|
@@ -28,6 +33,10 @@ export interface ShaderComponentProps extends Omit<React.ComponentProps<'div'>,
|
|
|
28
33
|
minPixelRatio?: number;
|
|
29
34
|
maxPixelCount?: number;
|
|
30
35
|
webGlContextAttributes?: WebGLContextAttributes;
|
|
36
|
+
/** Props of the element that holds the children and is drawn with them, for shaders that accept HTML children */
|
|
37
|
+
layoutProps?: ShaderLayoutProps;
|
|
38
|
+
/** Size the shader to its children on these axes, for shaders that accept HTML children */
|
|
39
|
+
sizeToContent?: ShaderSizeToContent;
|
|
31
40
|
/** Inline CSS width style */
|
|
32
41
|
width?: string | number;
|
|
33
42
|
/** Inline CSS height style */
|
package/dist/shader-mount.js
CHANGED
|
@@ -84,6 +84,8 @@ const ShaderMount = forwardRef(
|
|
|
84
84
|
maxPixelCount,
|
|
85
85
|
mipmaps,
|
|
86
86
|
acceptsHtmlChildren = false,
|
|
87
|
+
layoutProps,
|
|
88
|
+
sizeToContent,
|
|
87
89
|
children,
|
|
88
90
|
style,
|
|
89
91
|
...divProps
|
|
@@ -92,7 +94,13 @@ const ShaderMount = forwardRef(
|
|
|
92
94
|
const divRef = useRef(null);
|
|
93
95
|
const shaderMountRef = useRef(null);
|
|
94
96
|
const webGlContextAttributesRef = useRef(webGlContextAttributes);
|
|
95
|
-
const { hasHtmlChildren, addHtmlUniform, renderedChildren } = useHtmlChildren(
|
|
97
|
+
const { hasHtmlChildren, addHtmlUniform, renderedChildren, sizeStyle } = useHtmlChildren(
|
|
98
|
+
acceptsHtmlChildren,
|
|
99
|
+
children,
|
|
100
|
+
layoutProps,
|
|
101
|
+
sizeToContent,
|
|
102
|
+
divRef
|
|
103
|
+
);
|
|
96
104
|
const getUniforms = () => addHtmlUniform(uniformsProp);
|
|
97
105
|
useEffect(() => {
|
|
98
106
|
let isStale = false;
|
|
@@ -146,19 +154,12 @@ const ShaderMount = forwardRef(
|
|
|
146
154
|
shaderMountRef.current?.setFrame(frame);
|
|
147
155
|
}, [frame, isInitialized]);
|
|
148
156
|
const mergedRef = useMergeRefs([divRef, forwardedRef]);
|
|
149
|
-
|
|
150
|
-
"
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
height: typeof height === "string" && isNaN(+height) === false ? +height : height,
|
|
156
|
-
...style
|
|
157
|
-
} : style,
|
|
158
|
-
...divProps,
|
|
159
|
-
children: renderedChildren
|
|
160
|
-
}
|
|
161
|
-
);
|
|
157
|
+
const shaderStyle = width !== void 0 || height !== void 0 ? {
|
|
158
|
+
width: typeof width === "string" && isNaN(+width) === false ? +width : width,
|
|
159
|
+
height: typeof height === "string" && isNaN(+height) === false ? +height : height,
|
|
160
|
+
...style
|
|
161
|
+
} : style;
|
|
162
|
+
return /* @__PURE__ */ jsx("div", { ref: mergedRef, style: sizeStyle ? { ...shaderStyle, ...sizeStyle } : shaderStyle, ...divProps, children: renderedChildren });
|
|
162
163
|
}
|
|
163
164
|
);
|
|
164
165
|
ShaderMount.displayName = "ShaderMount";
|
package/dist/shader-mount.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/shader-mount.tsx"],
|
|
4
|
-
"sourcesContent": ["'use client';\n\nimport { useEffect, useRef, forwardRef, useState } from 'react';\nimport {\n ShaderMount as ShaderMountVanilla,\n emptyPixel,\n type PaperShaderElement,\n type ShaderMotionParams,\n type ShaderMountUniforms,\n} from '@paper-design/shaders';\nimport { useMergeRefs } from './use-merge-refs.js';\nimport { setMinImageSize } from './set-min-image-size.js';\nimport { useHtmlChildren } from './html-in-canvas.js';\n\n/**\n * React Shader Mount can also accept strings as uniform values, which will assumed to be URLs and loaded as images\n *\n * We accept undefined as a convenience for server rendering, when some things may be undefined\n * We just skip setting the uniform if it's undefined. This allows the shader mount to still take up space during server rendering\n */\ninterface ShaderMountUniformsReact {\n [key: string]: string | boolean | number | number[] | number[][] | HTMLImageElement | HTMLElement | undefined;\n}\n\nexport interface ShaderMountProps extends Omit<React.ComponentProps<'div'>, 'color' | 'ref'>, ShaderMotionParams {\n ref?: React.Ref<PaperShaderElement>;\n fragmentShader: string;\n uniforms: ShaderMountUniformsReact;\n mipmaps?: string[];\n minPixelRatio?: number;\n maxPixelCount?: number;\n webGlContextAttributes?: WebGLContextAttributes;\n /** Set by shaders that can draw their children into u_image (experimental) */\n acceptsHtmlChildren?: boolean;\n\n /** Inline CSS width style */\n width?: string | number;\n /** Inline CSS height style */\n height?: string | number;\n}\n\nexport interface ShaderComponentProps extends Omit<React.ComponentProps<'div'>, 'color' | 'ref'> {\n ref?: React.Ref<PaperShaderElement>;\n minPixelRatio?: number;\n maxPixelCount?: number;\n webGlContextAttributes?: WebGLContextAttributes;\n\n /** Inline CSS width style */\n width?: string | number;\n /** Inline CSS height style */\n height?: string | number;\n}\n\n/** Parse the provided uniforms, turning URL strings into loaded images */\nasync function processUniforms(uniformsProp: ShaderMountUniformsReact): Promise<ShaderMountUniforms> {\n const processedUniforms = {} as ShaderMountUniforms;\n const imageLoadPromises: Promise<void>[] = [];\n\n const isValidUrl = (url: string): boolean => {\n try {\n // Handle absolute paths\n if (url.startsWith('/')) return true;\n // Check if it's a valid URL\n new URL(url);\n return true;\n } catch {\n return false;\n }\n };\n\n const isExternalUrl = (url: string): boolean => {\n try {\n if (url.startsWith('/')) return false;\n const urlObject = new URL(url, window.location.origin);\n return urlObject.origin !== window.location.origin;\n } catch {\n return false;\n }\n };\n\n Object.entries(uniformsProp).forEach(([key, value]) => {\n if (typeof value === 'string') {\n // Use a transparent pixel for empty strings\n const url = value || emptyPixel;\n\n // Make sure the provided string is a valid URL or just skip trying to set this uniform entirely\n if (!isValidUrl(url)) {\n console.warn(`Uniform \"${key}\" has invalid URL \"${url}\". Skipping image loading.`);\n return;\n }\n\n const imagePromise = new Promise<void>((resolve, reject) => {\n const img = new Image();\n if (isExternalUrl(url)) {\n img.crossOrigin = 'anonymous';\n }\n img.onload = () => {\n setMinImageSize(img);\n processedUniforms[key] = img;\n resolve();\n };\n img.onerror = () => {\n console.error(`Could not set uniforms. Failed to load image at ${url}`);\n reject();\n };\n img.src = url;\n });\n\n imageLoadPromises.push(imagePromise);\n } else if (value instanceof HTMLImageElement) {\n const imagePromise = value.decode().then(() => {\n setMinImageSize(value);\n processedUniforms[key] = value;\n });\n imageLoadPromises.push(imagePromise);\n } else {\n processedUniforms[key] = value;\n }\n });\n\n await Promise.all(imageLoadPromises);\n return processedUniforms;\n}\n\n/**\n * A React component that mounts a shader and updates its uniforms as the component's props change\n * If you pass a string as a uniform value, it will be assumed to be a URL and attempted to be loaded as an image\n */\nexport const ShaderMount: React.FC<ShaderMountProps> = forwardRef<PaperShaderElement, ShaderMountProps>(\n function ShaderMountImpl(\n {\n fragmentShader,\n uniforms: uniformsProp,\n webGlContextAttributes,\n speed = 0,\n frame = 0,\n width,\n height,\n minPixelRatio,\n maxPixelCount,\n mipmaps,\n acceptsHtmlChildren = false,\n children,\n style,\n ...divProps\n },\n forwardedRef\n ) {\n const [isInitialized, setIsInitialized] = useState(false);\n const divRef = useRef<PaperShaderElement>(null);\n const shaderMountRef: React.RefObject<ShaderMountVanilla | null> = useRef<ShaderMountVanilla>(null);\n const webGlContextAttributesRef = useRef(webGlContextAttributes);\n const { hasHtmlChildren, addHtmlUniform, renderedChildren } = useHtmlChildren(acceptsHtmlChildren
|
|
5
|
-
"mappings": ";;;;;;AAEA,SAAS,WAAW,QAAQ,YAAY,gBAAgB;AACxD;AAAA,EACE,eAAe;AAAA,EACf;AAAA,OAIK;AACP,SAAS,oBAAoB;AAC7B,SAAS,uBAAuB;AAChC,SAAS,
|
|
4
|
+
"sourcesContent": ["'use client';\n\nimport { useEffect, useRef, forwardRef, useState } from 'react';\nimport {\n ShaderMount as ShaderMountVanilla,\n emptyPixel,\n type PaperShaderElement,\n type ShaderMotionParams,\n type ShaderMountUniforms,\n} from '@paper-design/shaders';\nimport { useMergeRefs } from './use-merge-refs.js';\nimport { setMinImageSize } from './set-min-image-size.js';\nimport { useHtmlChildren, type ShaderLayoutProps, type ShaderSizeToContent } from './html-in-canvas.js';\n\n/**\n * React Shader Mount can also accept strings as uniform values, which will assumed to be URLs and loaded as images\n *\n * We accept undefined as a convenience for server rendering, when some things may be undefined\n * We just skip setting the uniform if it's undefined. This allows the shader mount to still take up space during server rendering\n */\ninterface ShaderMountUniformsReact {\n [key: string]: string | boolean | number | number[] | number[][] | HTMLImageElement | HTMLElement | undefined;\n}\n\nexport interface ShaderMountProps extends Omit<React.ComponentProps<'div'>, 'color' | 'ref'>, ShaderMotionParams {\n ref?: React.Ref<PaperShaderElement>;\n fragmentShader: string;\n uniforms: ShaderMountUniformsReact;\n mipmaps?: string[];\n minPixelRatio?: number;\n maxPixelCount?: number;\n webGlContextAttributes?: WebGLContextAttributes;\n /** Set by shaders that can draw their children into u_image (experimental) */\n acceptsHtmlChildren?: boolean;\n /** Props of the element that holds the children and is drawn with them: layout, padding, background, border */\n layoutProps?: ShaderLayoutProps;\n /** Size the shader to its children on these axes instead of its own width and height styles */\n sizeToContent?: ShaderSizeToContent;\n\n /** Inline CSS width style */\n width?: string | number;\n /** Inline CSS height style */\n height?: string | number;\n}\n\nexport interface ShaderComponentProps extends Omit<React.ComponentProps<'div'>, 'color' | 'ref'> {\n ref?: React.Ref<PaperShaderElement>;\n minPixelRatio?: number;\n maxPixelCount?: number;\n webGlContextAttributes?: WebGLContextAttributes;\n /** Props of the element that holds the children and is drawn with them, for shaders that accept HTML children */\n layoutProps?: ShaderLayoutProps;\n /** Size the shader to its children on these axes, for shaders that accept HTML children */\n sizeToContent?: ShaderSizeToContent;\n\n /** Inline CSS width style */\n width?: string | number;\n /** Inline CSS height style */\n height?: string | number;\n}\n\n/** Parse the provided uniforms, turning URL strings into loaded images */\nasync function processUniforms(uniformsProp: ShaderMountUniformsReact): Promise<ShaderMountUniforms> {\n const processedUniforms = {} as ShaderMountUniforms;\n const imageLoadPromises: Promise<void>[] = [];\n\n const isValidUrl = (url: string): boolean => {\n try {\n // Handle absolute paths\n if (url.startsWith('/')) return true;\n // Check if it's a valid URL\n new URL(url);\n return true;\n } catch {\n return false;\n }\n };\n\n const isExternalUrl = (url: string): boolean => {\n try {\n if (url.startsWith('/')) return false;\n const urlObject = new URL(url, window.location.origin);\n return urlObject.origin !== window.location.origin;\n } catch {\n return false;\n }\n };\n\n Object.entries(uniformsProp).forEach(([key, value]) => {\n if (typeof value === 'string') {\n // Use a transparent pixel for empty strings\n const url = value || emptyPixel;\n\n // Make sure the provided string is a valid URL or just skip trying to set this uniform entirely\n if (!isValidUrl(url)) {\n console.warn(`Uniform \"${key}\" has invalid URL \"${url}\". Skipping image loading.`);\n return;\n }\n\n const imagePromise = new Promise<void>((resolve, reject) => {\n const img = new Image();\n if (isExternalUrl(url)) {\n img.crossOrigin = 'anonymous';\n }\n img.onload = () => {\n setMinImageSize(img);\n processedUniforms[key] = img;\n resolve();\n };\n img.onerror = () => {\n console.error(`Could not set uniforms. Failed to load image at ${url}`);\n reject();\n };\n img.src = url;\n });\n\n imageLoadPromises.push(imagePromise);\n } else if (value instanceof HTMLImageElement) {\n const imagePromise = value.decode().then(() => {\n setMinImageSize(value);\n processedUniforms[key] = value;\n });\n imageLoadPromises.push(imagePromise);\n } else {\n processedUniforms[key] = value;\n }\n });\n\n await Promise.all(imageLoadPromises);\n return processedUniforms;\n}\n\n/**\n * A React component that mounts a shader and updates its uniforms as the component's props change\n * If you pass a string as a uniform value, it will be assumed to be a URL and attempted to be loaded as an image\n */\nexport const ShaderMount: React.FC<ShaderMountProps> = forwardRef<PaperShaderElement, ShaderMountProps>(\n function ShaderMountImpl(\n {\n fragmentShader,\n uniforms: uniformsProp,\n webGlContextAttributes,\n speed = 0,\n frame = 0,\n width,\n height,\n minPixelRatio,\n maxPixelCount,\n mipmaps,\n acceptsHtmlChildren = false,\n layoutProps,\n sizeToContent,\n children,\n style,\n ...divProps\n },\n forwardedRef\n ) {\n const [isInitialized, setIsInitialized] = useState(false);\n const divRef = useRef<PaperShaderElement>(null);\n const shaderMountRef: React.RefObject<ShaderMountVanilla | null> = useRef<ShaderMountVanilla>(null);\n const webGlContextAttributesRef = useRef(webGlContextAttributes);\n const { hasHtmlChildren, addHtmlUniform, renderedChildren, sizeStyle } = useHtmlChildren(\n acceptsHtmlChildren,\n children,\n layoutProps,\n sizeToContent,\n divRef\n );\n\n const getUniforms = (): ShaderMountUniformsReact => addHtmlUniform(uniformsProp);\n\n // Initialize the ShaderMountVanilla\n useEffect(() => {\n let isStale = false;\n\n const initShader = async () => {\n const uniforms = await processUniforms(getUniforms());\n\n if (!isStale && divRef.current && !shaderMountRef.current) {\n shaderMountRef.current = new ShaderMountVanilla(\n divRef.current,\n fragmentShader,\n uniforms,\n webGlContextAttributesRef.current,\n speed,\n frame,\n minPixelRatio,\n maxPixelCount,\n mipmaps\n );\n\n setIsInitialized(true);\n }\n };\n\n initShader();\n\n return () => {\n isStale = true;\n shaderMountRef.current?.dispose();\n shaderMountRef.current = null;\n };\n }, [fragmentShader, hasHtmlChildren]);\n\n // Uniforms\n useEffect(() => {\n let isStale = false;\n\n const updateUniforms = async () => {\n const uniforms = await processUniforms(getUniforms());\n\n if (!isStale) {\n // We only use the freshest uniforms otherwise we can get into race conditions\n // if some uniforms (images!) take longer to load in subsequent effect runs.\n shaderMountRef.current?.setUniforms(uniforms);\n }\n };\n\n updateUniforms();\n\n return () => {\n isStale = true;\n };\n }, [uniformsProp, isInitialized]);\n\n // Speed\n useEffect(() => {\n shaderMountRef.current?.setSpeed(speed);\n }, [speed, isInitialized]);\n\n // Max Pixel Count\n useEffect(() => {\n shaderMountRef.current?.setMaxPixelCount(maxPixelCount);\n }, [maxPixelCount, isInitialized]);\n\n // Min Pixel Ratio\n useEffect(() => {\n shaderMountRef.current?.setMinPixelRatio(minPixelRatio);\n }, [minPixelRatio, isInitialized]);\n\n // Frame\n useEffect(() => {\n shaderMountRef.current?.setFrame(frame);\n }, [frame, isInitialized]);\n\n const mergedRef = useMergeRefs([divRef, forwardedRef]) as unknown as React.RefObject<HTMLDivElement>;\n const shaderStyle =\n width !== undefined || height !== undefined\n ? {\n width: typeof width === 'string' && isNaN(+width) === false ? +width : width,\n height: typeof height === 'string' && isNaN(+height) === false ? +height : height,\n ...style,\n }\n : style;\n return (\n <div ref={mergedRef} style={sizeStyle ? { ...shaderStyle, ...sizeStyle } : shaderStyle} {...divProps}>\n {renderedChildren}\n </div>\n );\n }\n);\n\nShaderMount.displayName = 'ShaderMount';\n"],
|
|
5
|
+
"mappings": ";;;;;;AAEA,SAAS,WAAW,QAAQ,YAAY,gBAAgB;AACxD;AAAA,EACE,eAAe;AAAA,EACf;AAAA,OAIK;AACP,SAAS,oBAAoB;AAC7B,SAAS,uBAAuB;AAChC,SAAS,uBAAyE;AAoP5E;AAlMN,eAAe,gBAAgB,cAAsE;AACnG,QAAM,oBAAoB,CAAC;AAC3B,QAAM,oBAAqC,CAAC;AAE5C,QAAM,aAAa,CAAC,QAAyB;AAC3C,QAAI;AAEF,UAAI,IAAI,WAAW,GAAG,EAAG,QAAO;AAEhC,UAAI,IAAI,GAAG;AACX,aAAO;AAAA,IACT,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;AAEA,QAAM,gBAAgB,CAAC,QAAyB;AAC9C,QAAI;AACF,UAAI,IAAI,WAAW,GAAG,EAAG,QAAO;AAChC,YAAM,YAAY,IAAI,IAAI,KAAK,OAAO,SAAS,MAAM;AACrD,aAAO,UAAU,WAAW,OAAO,SAAS;AAAA,IAC9C,QAAQ;AACN,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO,QAAQ,YAAY,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AACrD,QAAI,OAAO,UAAU,UAAU;AAE7B,YAAM,MAAM,SAAS;AAGrB,UAAI,CAAC,WAAW,GAAG,GAAG;AACpB,gBAAQ,KAAK,YAAY,GAAG,sBAAsB,GAAG,4BAA4B;AACjF;AAAA,MACF;AAEA,YAAM,eAAe,IAAI,QAAc,CAAC,SAAS,WAAW;AAC1D,cAAM,MAAM,IAAI,M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O,UAAU,YAAY,MAAM,CAAC,KAAK,MAAM,QAAQ,CAAC,QAAQ;AAAA,MACvE,QAAQ,OAAO,WAAW,YAAY,MAAM,CAAC,MAAM,MAAM,QAAQ,CAAC,SAAS;AAAA,MAC3E,GAAG;AAAA,IACL,IACA;AACN,WACE,oBAAC,SAAI,KAAK,WAAW,OAAO,YAAY,EAAE,GAAG,aAAa,GAAG,UAAU,IAAI,aAAc,GAAG,UACzF,4BACH;AAAA,EAEJ;AACF;AAEA,YAAY,cAAc;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paper-design/shaders-react",
|
|
3
|
-
"version": "0.0.0-canary.
|
|
3
|
+
"version": "0.0.0-canary.2d03406",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"type-check": "tsc --project tsconfig.json"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@paper-design/shaders": "0.0.0-canary.
|
|
29
|
+
"@paper-design/shaders": "0.0.0-canary.2d03406"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/react": "19.1.0"
|