@paper-design/shaders 0.0.0-canary.ca6dd66 → 0.0.0-canary.d0d88e97
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 +23 -0
- package/dist/html-in-canvas.js +64 -0
- package/dist/html-in-canvas.js.map +7 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +2 -2
- package/dist/shader-mount.d.ts +13 -8
- package/dist/shader-mount.js +169 -19
- package/dist/shader-mount.js.map +2 -2
- package/dist/shaders/fluted-glass.d.ts +3 -0
- package/dist/shaders/fluted-glass.js +14 -3
- package/dist/shaders/fluted-glass.js.map +2 -2
- package/dist/shaders/halftone-cmyk.d.ts +3 -1
- package/dist/shaders/halftone-cmyk.js +23 -3
- package/dist/shaders/halftone-cmyk.js.map +2 -2
- package/dist/shaders/halftone-dots.d.ts +3 -1
- package/dist/shaders/halftone-dots.js +23 -3
- package/dist/shaders/halftone-dots.js.map +2 -2
- package/dist/shaders/image-dithering.d.ts +4 -2
- package/dist/shaders/image-dithering.js +14 -4
- package/dist/shaders/image-dithering.js.map +2 -2
- package/dist/shaders/lens-distortion.d.ts +2 -0
- package/dist/shaders/lens-distortion.js +11 -4
- package/dist/shaders/lens-distortion.js.map +2 -2
- package/dist/shaders/paper-texture.d.ts +4 -0
- package/dist/shaders/paper-texture.js +22 -9
- package/dist/shaders/paper-texture.js.map +2 -2
- package/dist/shaders/water.d.ts +3 -1
- package/dist/shaders/water.js +22 -5
- package/dist/shaders/water.js.map +2 -2
- package/dist/size-to-content.d.ts +8 -0
- package/dist/size-to-content.js +64 -0
- package/dist/size-to-content.js.map +7 -0
- package/dist/vertex-shader.d.ts +1 -1
- package/dist/vertex-shader.js +3 -0
- package/dist/vertex-shader.js.map +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* https://github.com/WICG/html-in-canvas
|
|
3
|
+
*
|
|
4
|
+
* An HTML uniform has to be in this markup already, `layoutsubtree` is for older Chromium:
|
|
5
|
+
*
|
|
6
|
+
* <div data-paper-shader>
|
|
7
|
+
* <canvas content="drawable" layoutsubtree>
|
|
8
|
+
* <div drawable>…</div>
|
|
9
|
+
* </canvas>
|
|
10
|
+
* </div>
|
|
11
|
+
*/
|
|
12
|
+
export interface DrawableCanvasElement extends HTMLCanvasElement {
|
|
13
|
+
requestPaint(): void;
|
|
14
|
+
updateElementGeometry(element: Element, options?: {
|
|
15
|
+
canvasTransform?: DOMMatrixInit;
|
|
16
|
+
}): void;
|
|
17
|
+
}
|
|
18
|
+
export declare function isHtmlInCanvasSupported(): boolean;
|
|
19
|
+
/** nodeType, since `instanceof` fails across iframes */
|
|
20
|
+
export declare function isHtmlUniform(value: unknown): value is HTMLElement;
|
|
21
|
+
export declare function findDrawableCanvas(values: unknown[], parentElement: Element): DrawableCanvasElement | null;
|
|
22
|
+
/** Captures the element into the bound texture, returns the layout to pass back in next time */
|
|
23
|
+
export declare function drawHtml(gl: WebGL2RenderingContext, canvas: DrawableCanvasElement, element: HTMLElement, lastLayout?: string): string;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
2
|
+
* Paper Shaders *
|
|
3
|
+
* https://github.com/paper-design/shaders *
|
|
4
|
+
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
5
|
+
|
|
6
|
+
function isHtmlInCanvasSupported() {
|
|
7
|
+
return typeof HTMLCanvasElement !== "undefined" && "requestPaint" in HTMLCanvasElement.prototype && typeof WebGL2RenderingContext !== "undefined" && "texElementSubImage2D" in WebGL2RenderingContext.prototype;
|
|
8
|
+
}
|
|
9
|
+
function isHtmlUniform(value) {
|
|
10
|
+
return value?.nodeType === 1 && value.tagName !== "IMG";
|
|
11
|
+
}
|
|
12
|
+
function findDrawableCanvas(values, parentElement) {
|
|
13
|
+
for (const value of values) {
|
|
14
|
+
const canvas = isHtmlUniform(value) ? value.parentElement : null;
|
|
15
|
+
if (canvas?.tagName === "CANVAS" && canvas.parentElement === parentElement) {
|
|
16
|
+
return canvas;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
function drawHtml(gl, canvas, element, lastLayout) {
|
|
22
|
+
const width = Math.max(1, canvas.width);
|
|
23
|
+
const height = Math.max(1, canvas.height);
|
|
24
|
+
const canvasStyle = getComputedStyle(canvas);
|
|
25
|
+
const elementStyle = getComputedStyle(element);
|
|
26
|
+
const canvasCssWidth = parseFloat(canvasStyle.width) || canvas.clientWidth;
|
|
27
|
+
const canvasCssHeight = parseFloat(canvasStyle.height) || canvas.clientHeight;
|
|
28
|
+
const elementCssWidth = parseFloat(elementStyle.width) || element.offsetWidth;
|
|
29
|
+
const elementCssHeight = parseFloat(elementStyle.height) || element.offsetHeight;
|
|
30
|
+
const scaleX = width / Math.max(1, canvasCssWidth);
|
|
31
|
+
const scaleY = height / Math.max(1, canvasCssHeight);
|
|
32
|
+
const left = element.offsetLeft;
|
|
33
|
+
const top = element.offsetTop;
|
|
34
|
+
const visibleLeft = Math.max(0, left);
|
|
35
|
+
const visibleTop = Math.max(0, top);
|
|
36
|
+
const visibleRight = Math.min(canvasCssWidth, left + elementCssWidth);
|
|
37
|
+
const visibleBottom = Math.min(canvasCssHeight, top + elementCssHeight);
|
|
38
|
+
const x0 = Math.round(visibleLeft * scaleX);
|
|
39
|
+
const y0 = Math.round(visibleTop * scaleY);
|
|
40
|
+
const x1 = Math.min(width, Math.round(visibleRight * scaleX));
|
|
41
|
+
const y1 = Math.min(height, Math.round(visibleBottom * scaleY));
|
|
42
|
+
const layout = `${width}x${height} ${x0},${y0},${x1},${y1}`;
|
|
43
|
+
if (layout !== lastLayout) {
|
|
44
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
|
|
45
|
+
}
|
|
46
|
+
if (x1 > x0 && y1 > y0) {
|
|
47
|
+
gl.texElementSubImage2D(gl.TEXTURE_2D, 0, x0, y0, element, {
|
|
48
|
+
sx: visibleLeft - left,
|
|
49
|
+
sy: visibleTop - top,
|
|
50
|
+
swidth: visibleRight - visibleLeft,
|
|
51
|
+
sheight: visibleBottom - visibleTop,
|
|
52
|
+
width: x1 - x0,
|
|
53
|
+
height: y1 - y0
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return layout;
|
|
57
|
+
}
|
|
58
|
+
export {
|
|
59
|
+
drawHtml,
|
|
60
|
+
findDrawableCanvas,
|
|
61
|
+
isHtmlInCanvasSupported,
|
|
62
|
+
isHtmlUniform
|
|
63
|
+
};
|
|
64
|
+
//# sourceMappingURL=html-in-canvas.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/html-in-canvas.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * https://github.com/WICG/html-in-canvas\n *\n * An HTML uniform has to be in this markup already, `layoutsubtree` is for older Chromium:\n *\n * <div data-paper-shader>\n * <canvas content=\"drawable\" layoutsubtree>\n * <div drawable>\u2026</div>\n * </canvas>\n * </div>\n */\n\nexport interface DrawableCanvasElement extends HTMLCanvasElement {\n requestPaint(): void;\n updateElementGeometry(element: Element, options?: { canvasTransform?: DOMMatrixInit }): void;\n}\n\ninterface DrawableCanvasWebGL2Context extends WebGL2RenderingContext {\n texElementSubImage2D(\n target: GLenum,\n level: GLint,\n xoffset: GLint,\n yoffset: GLint,\n element: Element,\n config?: { width?: number; height?: number; sx?: number; sy?: number; swidth?: number; sheight?: number }\n ): void;\n}\n\nexport function isHtmlInCanvasSupported(): boolean {\n return (\n typeof HTMLCanvasElement !== 'undefined' &&\n 'requestPaint' in HTMLCanvasElement.prototype &&\n typeof WebGL2RenderingContext !== 'undefined' &&\n 'texElementSubImage2D' in WebGL2RenderingContext.prototype\n );\n}\n\n/** nodeType, since `instanceof` fails across iframes */\nexport function isHtmlUniform(value: unknown): value is HTMLElement {\n return (value as Node | null | undefined)?.nodeType === 1 && (value as Element).tagName !== 'IMG';\n}\n\nexport function findDrawableCanvas(values: unknown[], parentElement: Element): DrawableCanvasElement | null {\n for (const value of values) {\n const canvas = isHtmlUniform(value) ? value.parentElement : null;\n if (canvas?.tagName === 'CANVAS' && canvas.parentElement === parentElement) {\n return canvas as DrawableCanvasElement;\n }\n }\n return null;\n}\n\n/** Captures the element into the bound texture, returns the layout to pass back in next time */\nexport function drawHtml(\n gl: WebGL2RenderingContext,\n canvas: DrawableCanvasElement,\n element: HTMLElement,\n lastLayout?: string\n): string {\n const width = Math.max(1, canvas.width);\n const height = Math.max(1, canvas.height);\n\n // Offset and client sizes are rounded, bounding rects include ancestor transforms\n const canvasStyle = getComputedStyle(canvas);\n const elementStyle = getComputedStyle(element);\n const canvasCssWidth = parseFloat(canvasStyle.width) || canvas.clientWidth;\n const canvasCssHeight = parseFloat(canvasStyle.height) || canvas.clientHeight;\n const elementCssWidth = parseFloat(elementStyle.width) || element.offsetWidth;\n const elementCssHeight = parseFloat(elementStyle.height) || element.offsetHeight;\n const scaleX = width / Math.max(1, canvasCssWidth);\n const scaleY = height / Math.max(1, canvasCssHeight);\n\n const left = element.offsetLeft;\n const top = element.offsetTop;\n const visibleLeft = Math.max(0, left);\n const visibleTop = Math.max(0, top);\n const visibleRight = Math.min(canvasCssWidth, left + elementCssWidth);\n const visibleBottom = Math.min(canvasCssHeight, top + elementCssHeight);\n const x0 = Math.round(visibleLeft * scaleX);\n const y0 = Math.round(visibleTop * scaleY);\n const x1 = Math.min(width, Math.round(visibleRight * scaleX));\n const y1 = Math.min(height, Math.round(visibleBottom * scaleY));\n\n const layout = `${width}x${height} ${x0},${y0},${x1},${y1}`;\n if (layout !== lastLayout) {\n gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA8, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);\n }\n\n if (x1 > x0 && y1 > y0) {\n (gl as DrawableCanvasWebGL2Context).texElementSubImage2D(gl.TEXTURE_2D, 0, x0, y0, element, {\n sx: visibleLeft - left,\n sy: visibleTop - top,\n swidth: visibleRight - visibleLeft,\n sheight: visibleBottom - visibleTop,\n width: x1 - x0,\n height: y1 - y0,\n });\n }\n\n return layout;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;AA4BO,SAAS,0BAAmC;AACjD,SACE,OAAO,sBAAsB,eAC7B,kBAAkB,kBAAkB,aACpC,OAAO,2BAA2B,eAClC,0BAA0B,uBAAuB;AAErD;AAGO,SAAS,cAAc,OAAsC;AAClE,SAAQ,OAAmC,aAAa,KAAM,MAAkB,YAAY;AAC9F;AAEO,SAAS,mBAAmB,QAAmB,eAAsD;AAC1G,aAAW,SAAS,QAAQ;AAC1B,UAAM,SAAS,cAAc,KAAK,IAAI,MAAM,gBAAgB;AAC5D,QAAI,QAAQ,YAAY,YAAY,OAAO,kBAAkB,eAAe;AAC1E,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AAGO,SAAS,SACd,IACA,QACA,SACA,YACQ;AACR,QAAM,QAAQ,KAAK,IAAI,GAAG,OAAO,KAAK;AACtC,QAAM,SAAS,KAAK,IAAI,GAAG,OAAO,MAAM;AAGxC,QAAM,cAAc,iBAAiB,MAAM;AAC3C,QAAM,eAAe,iBAAiB,OAAO;AAC7C,QAAM,iBAAiB,WAAW,YAAY,KAAK,KAAK,OAAO;AAC/D,QAAM,kBAAkB,WAAW,YAAY,MAAM,KAAK,OAAO;AACjE,QAAM,kBAAkB,WAAW,aAAa,KAAK,KAAK,QAAQ;AAClE,QAAM,mBAAmB,WAAW,aAAa,MAAM,KAAK,QAAQ;AACpE,QAAM,SAAS,QAAQ,KAAK,IAAI,GAAG,cAAc;AACjD,QAAM,SAAS,SAAS,KAAK,IAAI,GAAG,eAAe;AAEnD,QAAM,OAAO,QAAQ;AACrB,QAAM,MAAM,QAAQ;AACpB,QAAM,cAAc,KAAK,IAAI,GAAG,IAAI;AACpC,QAAM,aAAa,KAAK,IAAI,GAAG,GAAG;AAClC,QAAM,eAAe,KAAK,IAAI,gBAAgB,OAAO,eAAe;AACpE,QAAM,gBAAgB,KAAK,IAAI,iBAAiB,MAAM,gBAAgB;AACtE,QAAM,KAAK,KAAK,MAAM,cAAc,MAAM;AAC1C,QAAM,KAAK,KAAK,MAAM,aAAa,MAAM;AACzC,QAAM,KAAK,KAAK,IAAI,OAAO,KAAK,MAAM,eAAe,MAAM,CAAC;AAC5D,QAAM,KAAK,KAAK,IAAI,QAAQ,KAAK,MAAM,gBAAgB,MAAM,CAAC;AAE9D,QAAM,SAAS,GAAG,KAAK,IAAI,MAAM,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE;AACzD,MAAI,WAAW,YAAY;AACzB,OAAG,WAAW,GAAG,YAAY,GAAG,GAAG,OAAO,OAAO,QAAQ,GAAG,GAAG,MAAM,GAAG,eAAe,IAAI;AAAA,EAC7F;AAEA,MAAI,KAAK,MAAM,KAAK,IAAI;AACtB,IAAC,GAAmC,qBAAqB,GAAG,YAAY,GAAG,IAAI,IAAI,SAAS;AAAA,MAC1F,IAAI,cAAc;AAAA,MAClB,IAAI,aAAa;AAAA,MACjB,QAAQ,eAAe;AAAA,MACvB,SAAS,gBAAgB;AAAA,MACzB,OAAO,KAAK;AAAA,MACZ,QAAQ,KAAK;AAAA,IACf,CAAC;AAAA,EACH;AAEA,SAAO;AACT;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/** The core Shader Mount class. Pass it a parent element and a fragment shader to get started. */
|
|
2
|
-
export { ShaderMount, isPaperShaderElement
|
|
2
|
+
export { ShaderMount, isPaperShaderElement } from './shader-mount.js';
|
|
3
|
+
export { isHtmlInCanvasSupported } from './html-in-canvas.js';
|
|
4
|
+
export { observeContentSize, type ShaderSizeToContent } from './size-to-content.js';
|
|
3
5
|
export type { PaperShaderElement, ShaderMotionParams, ShaderMountUniforms, ShaderPreset, ImageShaderPreset, } from './shader-mount.js';
|
|
4
6
|
/** Shader sizing options and uniforms */
|
|
5
7
|
export { defaultObjectSizing, defaultPatternSizing, ShaderFitOptions, type ShaderFit, type ShaderSizingParams, type ShaderSizingUniforms, } from './shader-sizing.js';
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
* https://github.com/paper-design/shaders *
|
|
4
4
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
5
5
|
|
|
6
|
-
import { ShaderMount, isPaperShaderElement
|
|
6
|
+
import { ShaderMount, isPaperShaderElement } from "./shader-mount.js";
|
|
7
|
+
import { isHtmlInCanvasSupported } from "./html-in-canvas.js";
|
|
8
|
+
import { observeContentSize } from "./size-to-content.js";
|
|
7
9
|
import {
|
|
8
10
|
defaultObjectSizing,
|
|
9
11
|
defaultPatternSizing,
|
|
@@ -118,8 +120,6 @@ import { getShaderColorFromString } from "./get-shader-color-from-string.js";
|
|
|
118
120
|
import { getShaderNoiseTexture } from "./get-shader-noise-texture.js";
|
|
119
121
|
import { emptyPixel } from "./empty-pixel.js";
|
|
120
122
|
export {
|
|
121
|
-
DEFAULT_MAX_PIXEL_COUNT,
|
|
122
|
-
DEFAULT_MIN_PIXEL_RATIO,
|
|
123
123
|
DitheringShapes,
|
|
124
124
|
DitheringTypes,
|
|
125
125
|
DotGridShapes,
|
|
@@ -158,6 +158,7 @@ export {
|
|
|
158
158
|
heatmapFragmentShader,
|
|
159
159
|
heatmapMeta,
|
|
160
160
|
imageDitheringFragmentShader,
|
|
161
|
+
isHtmlInCanvasSupported,
|
|
161
162
|
isPaperShaderElement,
|
|
162
163
|
lensDistortionFragmentShader,
|
|
163
164
|
lensDistortionMeta,
|
|
@@ -167,6 +168,7 @@ export {
|
|
|
167
168
|
metaballsFragmentShader,
|
|
168
169
|
metaballsMeta,
|
|
169
170
|
neuroNoiseFragmentShader,
|
|
171
|
+
observeContentSize,
|
|
170
172
|
paperTextureFragmentShader,
|
|
171
173
|
paperTextureMeta,
|
|
172
174
|
perlinNoiseFragmentShader,
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["/** The core Shader Mount class. Pass it a parent element and a fragment shader to get started. */\nexport { ShaderMount, isPaperShaderElement
|
|
5
|
-
"mappings": ";;;;;AACA,SAAS,aAAa,
|
|
4
|
+
"sourcesContent": ["/** The core Shader Mount class. Pass it a parent element and a fragment shader to get started. */\nexport { ShaderMount, isPaperShaderElement } from './shader-mount.js';\nexport { isHtmlInCanvasSupported } from './html-in-canvas.js';\nexport { observeContentSize, type ShaderSizeToContent } from './size-to-content.js';\nexport type {\n PaperShaderElement,\n ShaderMotionParams,\n ShaderMountUniforms,\n ShaderPreset,\n ImageShaderPreset,\n} from './shader-mount.js';\n\n/** Shader sizing options and uniforms */\nexport {\n defaultObjectSizing,\n defaultPatternSizing,\n ShaderFitOptions,\n type ShaderFit,\n type ShaderSizingParams,\n type ShaderSizingUniforms,\n} from './shader-sizing.js';\n\n/** A flowing interplay of color spots, moving along distinct trajectories and transformed by organic distortion */\nexport {\n meshGradientFragmentShader,\n meshGradientMeta,\n type MeshGradientParams,\n type MeshGradientUniforms,\n} from './shaders/mesh-gradient.js';\n\n/** Radial multi-colored gradient shaped with layered noise for a natural, smoky aesthetic */\nexport {\n smokeRingMeta,\n smokeRingFragmentShader,\n type SmokeRingParams,\n type SmokeRingUniforms,\n} from './shaders/smoke-ring.js';\n\n/** A glowing, web-like structure of fluid lines and soft intersections. Great for creating atmospheric, organic-yet-futuristic visuals */\nexport { neuroNoiseFragmentShader, type NeuroNoiseParams, type NeuroNoiseUniforms } from './shaders/neuro-noise.js';\n\n/** Animated multi-color dots pattern with each dot orbiting around its cell center. Supports up to 40 colors and various shape and motion controls. Great for playful, dynamic backgrounds and UI textures */\nexport {\n dotOrbitMeta,\n dotOrbitFragmentShader,\n type DotOrbitParams,\n type DotOrbitUniforms,\n} from './shaders/dot-orbit.js';\n\n/** Static grid pattern made of circles, diamonds, squares or triangles */\nexport {\n dotGridFragmentShader,\n DotGridShapes,\n type DotGridShape,\n type DotGridParams,\n type DotGridUniforms,\n} from './shaders/dot-grid.js';\n\n/** A multi-color gradient mapped into smooth, animated curves, delivering a sleek, futuristic visual */\nexport {\n simplexNoiseMeta,\n simplexNoiseFragmentShader,\n type SimplexNoiseParams,\n type SimplexNoiseUniforms,\n} from './shaders/simplex-noise.js';\n\n/** Up to 20 gooey blobs moving around the center and merging into smooth organic shapes */\nexport {\n metaballsMeta,\n metaballsFragmentShader,\n type MetaballsParams,\n type MetaballsUniforms,\n} from './shaders/metaballs.js';\n\n/** Animated 3D Perlin noise with exposed controls */\nexport { perlinNoiseFragmentShader, type PerlinNoiseParams, type PerlinNoiseUniforms } from './shaders/perlin-noise.js';\n\n/** Anti-aliased animated Voronoi pattern with smooth and customizable edges */\nexport { voronoiMeta, voronoiFragmentShader, type VoronoiParams, type VoronoiUniforms } from './shaders/voronoi.js';\n\n/** Static line pattern configurable into textures ranging from sharp zigzags to smooth flowing waves */\nexport { wavesFragmentShader, type WavesParams, type WavesUniforms } from './shaders/waves.js';\n\n/** Animated color fields warped by noise and swirls, applied over base patterns (checks, stripes, or split edge). Blends up to 10 colors with adjustable distribution, softness, distortion, and swirl. Great for fluid, smoky, or marbled effects */\nexport {\n warpMeta,\n warpFragmentShader,\n WarpPatterns,\n type WarpParams,\n type WarpUniforms,\n type WarpPattern,\n} from './shaders/warp.js';\n\n/** Animated rays of light radiating from the center, blended with up to 5 colors. The rays are adjustable by size, density, brightness and center glow. Great for dramatic backgrounds, logo reveals, and VFX like energy bursts or sun shafts */\nexport { godRaysMeta, godRaysFragmentShader, type GodRaysParams, type GodRaysUniforms } from './shaders/god-rays.js';\n\n/** A single-colored animated spiral that morphs across a wide range of shapes - from crisp, thin-lined geometry to flowing whirlpool forms and wavy, abstract rings */\nexport { spiralFragmentShader, type SpiralParams, type SpiralUniforms } from './shaders/spiral.js';\n\n/** Animated bands of color twisting and bending, producing spirals, arcs, and flowing circular patterns */\nexport { swirlMeta, swirlFragmentShader, type SwirlParams, type SwirlUniforms } from './shaders/swirl.js';\n\n/** Animated 2-color dithering over with multiple pattern sources (noise, warp, dots, waves, ripple, swirl, sphere) */\nexport {\n ditheringFragmentShader,\n DitheringShapes,\n DitheringTypes,\n type DitheringParams,\n type DitheringUniforms,\n type DitheringShape,\n type DitheringType,\n} from './shaders/dithering.js';\n\n/** Multi-color gradients with grainy, noise-textured distortion available in 7 animated abstract forms */\nexport {\n grainGradientFragmentShader,\n grainGradientMeta,\n GrainGradientShapes,\n type GrainGradientParams,\n type GrainGradientUniforms,\n type GrainGradientShape,\n} from './shaders/grain-gradient.js';\n\n/** Luminous trails of color merging into a glowing gradient frame */\nexport {\n pulsingBorderMeta,\n pulsingBorderFragmentShader,\n PulsingBorderAspectRatios,\n type PulsingBorderParams,\n type PulsingBorderUniforms,\n type PulsingBorderAspectRatio,\n} from './shaders/pulsing-border.js';\n\n/** Glowing translucent 3D panels rotating around a central axis */\nexport {\n colorPanelsFragmentShader,\n colorPanelsMeta,\n type ColorPanelsParams,\n type ColorPanelsUniforms,\n} from './shaders/color-panels.js';\n\n/** Multi-point mesh gradients with up to 10 color spots, enhanced by two-direction warping, adjustable blend sharpness, and grain controls. Perfect for elegant wallpapers and atmospheric backdrops */\nexport {\n staticMeshGradientFragmentShader,\n staticMeshGradientMeta,\n type StaticMeshGradientParams,\n type StaticMeshGradientUniforms,\n} from './shaders/static-mesh-gradient.js';\n\n/** Radial gradient with up to 10 blended colors, featuring advanced focal point control, shape distortion, and grain effects */\nexport {\n staticRadialGradientFragmentShader,\n staticRadialGradientMeta,\n type StaticRadialGradientParams,\n type StaticRadialGradientUniforms,\n} from './shaders/static-radial-gradient.js';\n\n/** A static texture built from multiple noise layers, usable for a realistic paper and cardboard surfaces or generating abstract patterns. Can be used as a image filter or as a texture */\nexport {\n paperTextureFragmentShader,\n paperTextureMeta,\n type PaperTextureParams,\n type PaperTextureUniforms,\n} from './shaders/paper-texture.js';\n\n/** Water-like surface distortion with natural caustic realism. Works as an image filter or animated texture without image */\nexport { waterFragmentShader, type WaterParams, type WaterUniforms } from './shaders/water.js';\n\n/** Fluted glass image filter transforms an image into streaked, ribbed distortions, giving a mix of clarity and obscurity */\nexport {\n flutedGlassFragmentShader,\n GlassDistortionShapes,\n GlassGridShapes,\n type GlassDistortionShape,\n type GlassGridShape,\n type FlutedGlassParams,\n type FlutedGlassUniforms,\n} from './shaders/fluted-glass.js';\n\n/** A dithering image filter with support for 4 dithering modes and multiple color palettes (2-color, 3-color, and multicolor options, using either predefined colors or ones sampled directly from the original image) */\nexport {\n imageDitheringFragmentShader,\n type ImageDitheringParams,\n type ImageDitheringUniforms,\n} from './shaders/image-dithering.js';\n\n/** Lens Distortion image filter that separates an image into shifting color layers, recreating the chromatic aberration of a lens, and warps the image geometry with barrel or pincushion curvature */\nexport {\n lensDistortionFragmentShader,\n lensDistortionMeta,\n type LensDistortionParams,\n type LensDistortionUniforms,\n} from './shaders/lens-distortion.js';\n\n/** A glowing gradient of colors flowing through an input image. The effect creates a smoothly animated wave of intensity across the image */\nexport {\n heatmapMeta,\n heatmapFragmentShader,\n toProcessedHeatmap,\n type HeatmapParams,\n type HeatmapUniforms,\n} from './shaders/heatmap.js';\n\n/** Futuristic liquid metal material applied to uploaded logo or one of pre-defined abstract shape */\nexport {\n liquidMetalFragmentShader,\n LiquidMetalShapes,\n toProcessedLiquidMetal,\n type LiquidMetalShape,\n type LiquidMetalParams,\n type LiquidMetalUniforms,\n} from './shaders/liquid-metal.js';\n\n/** A halftone-dot image filter featuring customizable grids, color palettes, and dot styles */\nexport {\n HalftoneDotsTypes,\n HalftoneDotsGrids,\n halftoneDotsFragmentShader,\n type HalftoneDotsParams,\n type HalftoneDotsUniforms,\n type HalftoneDotsType,\n type HalftoneDotsGrid,\n} from './shaders/halftone-dots.js';\n\n/** A halftone CMYK classic algo */\nexport {\n HalftoneCmykTypes,\n halftoneCmykFragmentShader,\n type HalftoneCmykParams,\n type HalftoneCmykUniforms,\n type HalftoneCmykType,\n} from './shaders/halftone-cmyk.js';\n\n/** Animated color fields running around a glassy logo shape */\nexport {\n gemSmokeMeta,\n gemSmokeFragmentShader,\n toProcessedGemSmoke,\n GemSmokeShapes,\n type GemSmokeShape,\n type GemSmokeParams,\n type GemSmokeUniforms,\n} from './shaders/gem-smoke.js';\n\n// ----- Utils ----- //\nexport { getShaderColorFromString } from './get-shader-color-from-string.js';\nexport { getShaderNoiseTexture } from './get-shader-noise-texture.js';\nexport { emptyPixel } from './empty-pixel.js';\n"],
|
|
5
|
+
"mappings": ";;;;;AACA,SAAS,aAAa,4BAA4B;AAClD,SAAS,+BAA+B;AACxC,SAAS,0BAAoD;AAU7D;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAGP,SAAS,gCAAgF;AAGzF;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,OAIK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAGP,SAAS,iCAAmF;AAG5F,SAAS,aAAa,6BAAuE;AAG7F,SAAS,2BAAiE;AAG1E;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AAGP,SAAS,aAAa,6BAAuE;AAG7F,SAAS,4BAAoE;AAG7E,SAAS,WAAW,2BAAiE;AAGrF;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAKK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAGP,SAAS,2BAAiE;AAG1E;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAKK;AAGP;AAAA,EACE;AAAA,OAGK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAGK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAKK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,OAIK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AAGP,SAAS,gCAAgC;AACzC,SAAS,6BAA6B;AACtC,SAAS,kBAAkB;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/shader-mount.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
export declare const DEFAULT_MIN_PIXEL_RATIO: number;
|
|
2
|
-
export declare const DEFAULT_MAX_PIXEL_COUNT: number;
|
|
3
1
|
export declare class ShaderMount {
|
|
4
2
|
parentElement: PaperShaderElement;
|
|
5
3
|
canvasElement: HTMLCanvasElement;
|
|
@@ -34,9 +32,8 @@ export declare class ShaderMount {
|
|
|
34
32
|
private uniformCache;
|
|
35
33
|
private textureUnitMap;
|
|
36
34
|
private ownerDocument;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
private resolveReady;
|
|
35
|
+
private htmlUniforms;
|
|
36
|
+
private isProvidedCanvas;
|
|
40
37
|
constructor(
|
|
41
38
|
/** The div you'd like to mount the shader to. The shader will match its size. */
|
|
42
39
|
parentElement: HTMLElement, fragmentShader: string, uniforms: ShaderMountUniforms, webGlContextAttributes?: WebGLContextAttributes,
|
|
@@ -80,6 +77,8 @@ export declare class ShaderMount {
|
|
|
80
77
|
private setupResizeObserver;
|
|
81
78
|
private setupIntersectionObserver;
|
|
82
79
|
private handleVisualViewportChange;
|
|
80
|
+
/** Resizes to the canvas right away, for size changes the resize observer would only see next frame */
|
|
81
|
+
syncSize: () => void;
|
|
83
82
|
/** Resize handler for when the container div changes size or the max pixel count changes and we want to resize our canvas to match */
|
|
84
83
|
private handleResize;
|
|
85
84
|
private render;
|
|
@@ -87,6 +86,14 @@ export declare class ShaderMount {
|
|
|
87
86
|
/** Creates a texture from an image and sets it into a uniform value */
|
|
88
87
|
private setTextureUniform;
|
|
89
88
|
private createTexture;
|
|
89
|
+
/** Redraws the element, which has to be a child of the shader canvas, into the texture on every canvas paint */
|
|
90
|
+
private setHtmlUniform;
|
|
91
|
+
/** HTML is drawn 1:1, so its aspect ratio is the canvas aspect ratio */
|
|
92
|
+
private setHtmlAspectRatio;
|
|
93
|
+
private updateHtmlAspectRatios;
|
|
94
|
+
/** Captures each HTML uniform into its texture. On resize only HTML that has a snapshot already */
|
|
95
|
+
private captureHtml;
|
|
96
|
+
private handleCanvasPaint;
|
|
90
97
|
/** Utility: recursive equality test for all the uniforms */
|
|
91
98
|
private areUniformValuesEqual;
|
|
92
99
|
/** Sets the provided uniform values into the WebGL program, can be a partial list of uniforms that have changed */
|
|
@@ -115,8 +122,6 @@ export declare class ShaderMount {
|
|
|
115
122
|
/** The parent `<div>` element that has a ShaderMount available on it */
|
|
116
123
|
export interface PaperShaderElement extends HTMLElement {
|
|
117
124
|
paperShaderMount: ShaderMount | undefined;
|
|
118
|
-
/** Set by the React ShaderMount when it starts mounting, resolves with the mount once it's `ready` */
|
|
119
|
-
paperShaderMountReady?: Promise<ShaderMount | null>;
|
|
120
125
|
}
|
|
121
126
|
/** Check if an element is a Paper shader element */
|
|
122
127
|
export declare function isPaperShaderElement(element: HTMLElement): element is PaperShaderElement;
|
|
@@ -127,7 +132,7 @@ export declare function isPaperShaderElement(element: HTMLElement): element is P
|
|
|
127
132
|
* We just skip setting the uniform if it's undefined. This allows the shader mount to still take up space during server rendering
|
|
128
133
|
*/
|
|
129
134
|
export interface ShaderMountUniforms {
|
|
130
|
-
[key: string]: boolean | number | number[] | number[][] | HTMLImageElement | undefined;
|
|
135
|
+
[key: string]: boolean | number | number[] | number[][] | HTMLImageElement | HTMLElement | undefined;
|
|
131
136
|
}
|
|
132
137
|
export interface ShaderMotionParams {
|
|
133
138
|
speed?: number;
|
package/dist/shader-mount.js
CHANGED
|
@@ -4,7 +4,12 @@
|
|
|
4
4
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
|
5
5
|
|
|
6
6
|
import { vertexShaderSource } from "./vertex-shader.js";
|
|
7
|
-
|
|
7
|
+
import {
|
|
8
|
+
drawHtml,
|
|
9
|
+
findDrawableCanvas,
|
|
10
|
+
isHtmlInCanvasSupported,
|
|
11
|
+
isHtmlUniform
|
|
12
|
+
} from "./html-in-canvas.js";
|
|
8
13
|
const DEFAULT_MAX_PIXEL_COUNT = 1920 * 1080 * 4;
|
|
9
14
|
class ShaderMount {
|
|
10
15
|
parentElement;
|
|
@@ -40,14 +45,9 @@ class ShaderMount {
|
|
|
40
45
|
uniformCache = {};
|
|
41
46
|
textureUnitMap = /* @__PURE__ */ new Map();
|
|
42
47
|
ownerDocument;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
};
|
|
47
|
-
constructor(parentElement, fragmentShader, uniforms, webGlContextAttributes, speed = 0, frame = 0, minPixelRatio = DEFAULT_MIN_PIXEL_RATIO, maxPixelCount = DEFAULT_MAX_PIXEL_COUNT, mipmaps = []) {
|
|
48
|
-
this.ready = new Promise((resolve) => {
|
|
49
|
-
this.resolveReady = resolve;
|
|
50
|
-
});
|
|
48
|
+
htmlUniforms = /* @__PURE__ */ new Map();
|
|
49
|
+
isProvidedCanvas = false;
|
|
50
|
+
constructor(parentElement, fragmentShader, uniforms, webGlContextAttributes, speed = 0, frame = 0, minPixelRatio = 2, maxPixelCount = DEFAULT_MAX_PIXEL_COUNT, mipmaps = []) {
|
|
51
51
|
if (parentElement?.nodeType === 1) {
|
|
52
52
|
this.parentElement = parentElement;
|
|
53
53
|
} else {
|
|
@@ -60,9 +60,16 @@ class ShaderMount {
|
|
|
60
60
|
styleElement.setAttribute("data-paper-shader", "");
|
|
61
61
|
this.ownerDocument.head.prepend(styleElement);
|
|
62
62
|
}
|
|
63
|
-
const
|
|
63
|
+
const providedCanvas = findDrawableCanvas(Object.values(uniforms), this.parentElement);
|
|
64
|
+
const canvasElement = providedCanvas ?? this.ownerDocument.createElement("canvas");
|
|
64
65
|
this.canvasElement = canvasElement;
|
|
65
|
-
this.
|
|
66
|
+
this.isProvidedCanvas = providedCanvas !== null;
|
|
67
|
+
if (!providedCanvas) {
|
|
68
|
+
this.parentElement.prepend(canvasElement);
|
|
69
|
+
} else {
|
|
70
|
+
providedCanvas.style.display = "inherit";
|
|
71
|
+
}
|
|
72
|
+
canvasElement.addEventListener("paint", this.handleCanvasPaint);
|
|
66
73
|
this.fragmentShader = fragmentShader;
|
|
67
74
|
this.providedUniforms = uniforms;
|
|
68
75
|
this.mipmaps = mipmaps;
|
|
@@ -88,7 +95,9 @@ class ShaderMount {
|
|
|
88
95
|
visualViewport?.addEventListener("resize", this.handleVisualViewportChange);
|
|
89
96
|
this.setupIntersectionObserver();
|
|
90
97
|
this.setSpeed(speed);
|
|
91
|
-
this.parentElement.
|
|
98
|
+
if (!this.parentElement.hasAttribute("data-paper-shader")) {
|
|
99
|
+
this.parentElement.setAttribute("data-paper-shader", "");
|
|
100
|
+
}
|
|
92
101
|
this.parentElement.paperShaderMount = this;
|
|
93
102
|
this.ownerDocument.addEventListener("visibilitychange", this.handleDocumentVisibilityChange);
|
|
94
103
|
}
|
|
@@ -115,9 +124,10 @@ class ShaderMount {
|
|
|
115
124
|
};
|
|
116
125
|
Object.entries(this.providedUniforms).forEach(([key, value]) => {
|
|
117
126
|
uniformLocations[key] = this.gl.getUniformLocation(this.program, key);
|
|
118
|
-
if (value instanceof HTMLImageElement) {
|
|
127
|
+
if (value instanceof HTMLImageElement || isHtmlUniform(value)) {
|
|
119
128
|
const aspectRatioUniformName = `${key}AspectRatio`;
|
|
120
129
|
uniformLocations[aspectRatioUniformName] = this.gl.getUniformLocation(this.program, aspectRatioUniformName);
|
|
130
|
+
uniformLocations[`${key}IsHtml`] = this.gl.getUniformLocation(this.program, `${key}IsHtml`);
|
|
121
131
|
}
|
|
122
132
|
});
|
|
123
133
|
this.uniformLocations = uniformLocations;
|
|
@@ -150,7 +160,6 @@ class ShaderMount {
|
|
|
150
160
|
this.parentHeight = entry.borderBoxSize[0].blockSize;
|
|
151
161
|
}
|
|
152
162
|
this.handleResize();
|
|
153
|
-
this.resolveReady();
|
|
154
163
|
});
|
|
155
164
|
this.resizeObserver.observe(this.canvasElement);
|
|
156
165
|
};
|
|
@@ -173,6 +182,17 @@ class ShaderMount {
|
|
|
173
182
|
this.resizeObserver?.disconnect();
|
|
174
183
|
this.setupResizeObserver();
|
|
175
184
|
};
|
|
185
|
+
/** Resizes to the canvas right away, for size changes the resize observer would only see next frame */
|
|
186
|
+
syncSize = () => {
|
|
187
|
+
const style = getComputedStyle(this.canvasElement);
|
|
188
|
+
this.parentWidth = parseFloat(style.width) || 0;
|
|
189
|
+
this.parentHeight = parseFloat(style.height) || 0;
|
|
190
|
+
if (this.devicePixelsSupported) {
|
|
191
|
+
this.parentDevicePixelWidth = Math.round(this.parentWidth * window.devicePixelRatio);
|
|
192
|
+
this.parentDevicePixelHeight = Math.round(this.parentHeight * window.devicePixelRatio);
|
|
193
|
+
}
|
|
194
|
+
this.handleResize();
|
|
195
|
+
};
|
|
176
196
|
/** Resize handler for when the container div changes size or the max pixel count changes and we want to resize our canvas to match */
|
|
177
197
|
handleResize = () => {
|
|
178
198
|
let targetPixelWidth = 0;
|
|
@@ -203,7 +223,15 @@ class ShaderMount {
|
|
|
203
223
|
this.canvasElement.height = newHeight;
|
|
204
224
|
this.resolutionChanged = true;
|
|
205
225
|
this.gl.viewport(0, 0, this.gl.canvas.width, this.gl.canvas.height);
|
|
226
|
+
this.updateHtmlAspectRatios();
|
|
227
|
+
this.captureHtml(true);
|
|
228
|
+
if (this.htmlUniforms.size > 0) {
|
|
229
|
+
this.canvasElement.requestPaint();
|
|
230
|
+
}
|
|
206
231
|
this.render(performance.now());
|
|
232
|
+
if (this.htmlUniforms.size > 0 && this.rafId === null) {
|
|
233
|
+
this.requestRender();
|
|
234
|
+
}
|
|
207
235
|
}
|
|
208
236
|
};
|
|
209
237
|
render = (currentTime) => {
|
|
@@ -242,6 +270,7 @@ class ShaderMount {
|
|
|
242
270
|
if (!image.complete || image.naturalWidth === 0) {
|
|
243
271
|
throw new Error(`Paper Shaders: image for uniform ${uniformName} must be fully loaded`);
|
|
244
272
|
}
|
|
273
|
+
this.htmlUniforms.delete(uniformName);
|
|
245
274
|
const { texture, textureUnit } = this.createTexture(uniformName);
|
|
246
275
|
this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, image);
|
|
247
276
|
if (this.mipmaps.includes(uniformName)) {
|
|
@@ -263,6 +292,7 @@ class ShaderMount {
|
|
|
263
292
|
const aspectRatio = image.naturalWidth / image.naturalHeight;
|
|
264
293
|
this.gl.uniform1f(aspectRatioLocation, aspectRatio);
|
|
265
294
|
}
|
|
295
|
+
this.gl.uniform1f(this.uniformLocations[`${uniformName}IsHtml`] ?? null, 0);
|
|
266
296
|
}
|
|
267
297
|
};
|
|
268
298
|
createTexture = (uniformName) => {
|
|
@@ -283,6 +313,65 @@ class ShaderMount {
|
|
|
283
313
|
this.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR);
|
|
284
314
|
return { texture, textureUnit };
|
|
285
315
|
};
|
|
316
|
+
/** Redraws the element, which has to be a child of the shader canvas, into the texture on every canvas paint */
|
|
317
|
+
setHtmlUniform = (uniformName, element) => {
|
|
318
|
+
this.htmlUniforms.delete(uniformName);
|
|
319
|
+
const { texture, textureUnit } = this.createTexture(uniformName);
|
|
320
|
+
if (texture === null) return;
|
|
321
|
+
this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, 1, 1, 0, this.gl.RGBA, this.gl.UNSIGNED_BYTE, null);
|
|
322
|
+
this.textures.set(uniformName, texture);
|
|
323
|
+
this.gl.uniform1i(this.uniformLocations[uniformName] ?? null, textureUnit);
|
|
324
|
+
this.gl.uniform1f(this.uniformLocations[`${uniformName}IsHtml`] ?? null, 1);
|
|
325
|
+
this.setHtmlAspectRatio(uniformName);
|
|
326
|
+
if (!isHtmlInCanvasSupported()) {
|
|
327
|
+
console.warn(`Paper Shaders: HTML for ${uniformName} needs HTML in canvas support`);
|
|
328
|
+
return;
|
|
329
|
+
}
|
|
330
|
+
if (element.parentElement !== this.canvasElement) {
|
|
331
|
+
console.warn(`Paper Shaders: HTML for ${uniformName} has to be a child of the shader canvas`);
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
this.htmlUniforms.set(uniformName, { element });
|
|
335
|
+
this.canvasElement.requestPaint();
|
|
336
|
+
};
|
|
337
|
+
/** HTML is drawn 1:1, so its aspect ratio is the canvas aspect ratio */
|
|
338
|
+
setHtmlAspectRatio = (uniformName) => {
|
|
339
|
+
this.gl.uniform1f(
|
|
340
|
+
this.uniformLocations[`${uniformName}AspectRatio`] ?? null,
|
|
341
|
+
this.gl.canvas.width / this.gl.canvas.height
|
|
342
|
+
);
|
|
343
|
+
};
|
|
344
|
+
updateHtmlAspectRatios = () => {
|
|
345
|
+
for (const [uniformName, value] of Object.entries(this.providedUniforms)) {
|
|
346
|
+
if (isHtmlUniform(value)) {
|
|
347
|
+
this.setHtmlAspectRatio(uniformName);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
/** Captures each HTML uniform into its texture. On resize only HTML that has a snapshot already */
|
|
352
|
+
captureHtml = (isResize) => {
|
|
353
|
+
const canvas = this.canvasElement;
|
|
354
|
+
this.htmlUniforms.forEach((html, uniformName) => {
|
|
355
|
+
if (isResize && html.layout === void 0) return;
|
|
356
|
+
this.gl.activeTexture(this.gl.TEXTURE0 + this.textureUnitMap.get(uniformName));
|
|
357
|
+
this.gl.bindTexture(this.gl.TEXTURE_2D, this.textures.get(uniformName));
|
|
358
|
+
const isFirstCapture = html.layout === void 0;
|
|
359
|
+
try {
|
|
360
|
+
html.layout = drawHtml(this.gl, canvas, html.element, html.layout);
|
|
361
|
+
} catch (error) {
|
|
362
|
+
if (error.name !== "InvalidStateError") throw error;
|
|
363
|
+
html.layout = void 0;
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
if (isFirstCapture) {
|
|
367
|
+
canvas.updateElementGeometry(html.element, { canvasTransform: new DOMMatrix() });
|
|
368
|
+
}
|
|
369
|
+
});
|
|
370
|
+
};
|
|
371
|
+
handleCanvasPaint = () => {
|
|
372
|
+
this.captureHtml(false);
|
|
373
|
+
this.renderUnlessAnimating();
|
|
374
|
+
};
|
|
286
375
|
/** Utility: recursive equality test for all the uniforms */
|
|
287
376
|
areUniformValuesEqual = (a, b) => {
|
|
288
377
|
if (a === b) return true;
|
|
@@ -307,6 +396,8 @@ class ShaderMount {
|
|
|
307
396
|
}
|
|
308
397
|
if (value instanceof HTMLImageElement) {
|
|
309
398
|
this.setTextureUniform(key, value);
|
|
399
|
+
} else if (isHtmlUniform(value)) {
|
|
400
|
+
this.setHtmlUniform(key, value);
|
|
310
401
|
} else if (Array.isArray(value)) {
|
|
311
402
|
let flatArray = null;
|
|
312
403
|
let valueLength = null;
|
|
@@ -387,7 +478,7 @@ class ShaderMount {
|
|
|
387
478
|
this.handleResize();
|
|
388
479
|
};
|
|
389
480
|
/** Set the minimum pixel ratio for the shader */
|
|
390
|
-
setMinPixelRatio = (newMinPixelRatio =
|
|
481
|
+
setMinPixelRatio = (newMinPixelRatio = 2) => {
|
|
391
482
|
this.minPixelRatio = newMinPixelRatio;
|
|
392
483
|
this.handleResize();
|
|
393
484
|
};
|
|
@@ -413,6 +504,8 @@ class ShaderMount {
|
|
|
413
504
|
cancelAnimationFrame(this.rafId);
|
|
414
505
|
this.rafId = null;
|
|
415
506
|
}
|
|
507
|
+
this.canvasElement.removeEventListener("paint", this.handleCanvasPaint);
|
|
508
|
+
this.htmlUniforms.clear();
|
|
416
509
|
if (this.gl && this.program) {
|
|
417
510
|
this.textures.forEach((texture) => {
|
|
418
511
|
this.gl.deleteTexture(texture);
|
|
@@ -426,7 +519,9 @@ class ShaderMount {
|
|
|
426
519
|
this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, null);
|
|
427
520
|
this.gl.getError();
|
|
428
521
|
}
|
|
429
|
-
this.
|
|
522
|
+
if (!this.isProvidedCanvas || !this.canvasElement.isConnected) {
|
|
523
|
+
this.gl.getExtension("WEBGL_lose_context")?.loseContext();
|
|
524
|
+
}
|
|
430
525
|
if (this.resizeObserver) {
|
|
431
526
|
this.resizeObserver.disconnect();
|
|
432
527
|
this.resizeObserver = null;
|
|
@@ -438,7 +533,9 @@ class ShaderMount {
|
|
|
438
533
|
visualViewport?.removeEventListener("resize", this.handleVisualViewportChange);
|
|
439
534
|
this.ownerDocument.removeEventListener("visibilitychange", this.handleDocumentVisibilityChange);
|
|
440
535
|
this.uniformLocations = {};
|
|
441
|
-
this.
|
|
536
|
+
if (!this.isProvidedCanvas) {
|
|
537
|
+
this.canvasElement.remove();
|
|
538
|
+
}
|
|
442
539
|
delete this.parentElement.paperShaderMount;
|
|
443
540
|
};
|
|
444
541
|
}
|
|
@@ -498,6 +595,61 @@ const defaultStyle = `@layer paper-shaders {
|
|
|
498
595
|
border-radius: inherit;
|
|
499
596
|
corner-shape: inherit;
|
|
500
597
|
}
|
|
598
|
+
|
|
599
|
+
& > canvas[content='drawable'],
|
|
600
|
+
& > canvas > [drawable],
|
|
601
|
+
& > [drawable] {
|
|
602
|
+
display: inherit;
|
|
603
|
+
flex-direction: inherit;
|
|
604
|
+
flex-wrap: inherit;
|
|
605
|
+
align-items: inherit;
|
|
606
|
+
align-content: inherit;
|
|
607
|
+
justify-items: inherit;
|
|
608
|
+
justify-content: inherit;
|
|
609
|
+
row-gap: inherit;
|
|
610
|
+
column-gap: inherit;
|
|
611
|
+
grid-template-columns: inherit;
|
|
612
|
+
grid-template-rows: inherit;
|
|
613
|
+
grid-template-areas: inherit;
|
|
614
|
+
grid-auto-flow: inherit;
|
|
615
|
+
grid-auto-columns: inherit;
|
|
616
|
+
grid-auto-rows: inherit;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
& > canvas > [drawable],
|
|
620
|
+
& > [drawable] {
|
|
621
|
+
box-sizing: border-box;
|
|
622
|
+
width: 100%;
|
|
623
|
+
height: 100%;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
& > canvas > [drawable] {
|
|
627
|
+
position: relative;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
& > [drawable] {
|
|
631
|
+
position: absolute;
|
|
632
|
+
top: 0;
|
|
633
|
+
left: 0;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
&:is([data-size-to-content='width'], [data-size-to-content='both']) {
|
|
637
|
+
& > canvas > [drawable],
|
|
638
|
+
& > [drawable] {
|
|
639
|
+
width: max-content;
|
|
640
|
+
min-width: var(--paper-shader-content-min-width, auto);
|
|
641
|
+
max-width: var(--paper-shader-content-max-width, none);
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
&:is([data-size-to-content='height'], [data-size-to-content='both']) {
|
|
646
|
+
& > canvas > [drawable],
|
|
647
|
+
& > [drawable] {
|
|
648
|
+
height: auto;
|
|
649
|
+
min-height: var(--paper-shader-content-min-height, auto);
|
|
650
|
+
max-height: var(--paper-shader-content-max-height, none);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
501
653
|
}
|
|
502
654
|
}`;
|
|
503
655
|
function isPaperShaderElement(element) {
|
|
@@ -529,8 +681,6 @@ function bestGuessBrowserZoom(doc) {
|
|
|
529
681
|
return ratio;
|
|
530
682
|
}
|
|
531
683
|
export {
|
|
532
|
-
DEFAULT_MAX_PIXEL_COUNT,
|
|
533
|
-
DEFAULT_MIN_PIXEL_RATIO,
|
|
534
684
|
ShaderMount,
|
|
535
685
|
isPaperShaderElement
|
|
536
686
|
};
|