shaderpad 1.0.0-alpha.9 → 1.0.0-beta.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -20
- package/dist/index.d.mts +52 -9
- package/dist/index.d.ts +52 -9
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -57,8 +57,16 @@ shader.play(time => {
|
|
|
57
57
|
shader.updateUniforms({ u_cursorColor: getColor(time) });
|
|
58
58
|
});
|
|
59
59
|
|
|
60
|
-
// Optionally pause the render loop.
|
|
60
|
+
// Optionally pause or reset the render loop.
|
|
61
61
|
// shader.pause();
|
|
62
|
+
// shader.reset();
|
|
63
|
+
|
|
64
|
+
// ShaderPad also attaches a throttled resize observer that you can hook into.
|
|
65
|
+
// It fires when the canvas size changes visually. If you supplied a custom
|
|
66
|
+
// canvas, you may use this to update its `width` and `height` attributes.
|
|
67
|
+
// shader.onResize = (width, height) => {
|
|
68
|
+
// console.log('Canvas resized:', width, height);
|
|
69
|
+
// };
|
|
62
70
|
```
|
|
63
71
|
|
|
64
72
|
See the [`examples/` directory](./examples/) for more.
|
|
@@ -74,30 +82,51 @@ The `canvas` option allows you to pass in an existing canvas element. If not pro
|
|
|
74
82
|
```typescript
|
|
75
83
|
const canvas = document.createElement('canvas');
|
|
76
84
|
const shader = new ShaderPad(fragmentShaderSrc, { canvas });
|
|
85
|
+
shader.onResize = (width, height) => {
|
|
86
|
+
canvas.width = width;
|
|
87
|
+
canvas.height = height;
|
|
88
|
+
};
|
|
77
89
|
```
|
|
78
90
|
|
|
79
|
-
###
|
|
91
|
+
### plugins
|
|
80
92
|
|
|
81
|
-
ShaderPad supports
|
|
93
|
+
ShaderPad supports plugins to add additional functionality.
|
|
94
|
+
|
|
95
|
+
#### history
|
|
96
|
+
|
|
97
|
+
The `history` plugin can capture both the framebuffer output and texture history. The plugin argument controls how many frames to keep for the framebuffer (defaults to `1`).
|
|
82
98
|
|
|
83
99
|
```typescript
|
|
84
|
-
|
|
85
|
-
|
|
100
|
+
import ShaderPad, { history, WithHistory } from 'shaderpad';
|
|
101
|
+
|
|
102
|
+
// Store the last 10 frames of shader output.
|
|
103
|
+
const shaderWithOutputHistory = new ShaderPad(fragmentShaderSrc, { plugins: [history(10)] });
|
|
86
104
|
|
|
87
|
-
//
|
|
88
|
-
const shader = new ShaderPad(fragmentShaderSrc, {
|
|
105
|
+
// Store the last 30 webcam frames.
|
|
106
|
+
const shader = new ShaderPad(fragmentShaderSrc, { plugins: [history()] }) as WithHistory<ShaderPad>;
|
|
107
|
+
shader.initializeTexture('u_webcam', videoElement, 30);
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
#### save
|
|
111
|
+
|
|
112
|
+
The `save` plugin adds a `.save()` method to the shader that saves the current frame to a PNG file.
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import ShaderPad, { save, WithSave } from 'shaderpad';
|
|
116
|
+
const shader = new ShaderPad(fragmentShaderSrc, { plugins: [save()] }) as WithSave<ShaderPad>;
|
|
117
|
+
shader.save('my-frame');
|
|
89
118
|
```
|
|
90
119
|
|
|
91
120
|
## Included uniforms
|
|
92
121
|
|
|
93
|
-
| Uniform | Type | Description
|
|
94
|
-
| -------------- | -------------- |
|
|
95
|
-
| `u_time` | float | The current time in seconds.
|
|
96
|
-
| `u_frame` | int | The current frame number.
|
|
97
|
-
| `u_resolution` | float[2] | The canvas element's dimensions.
|
|
98
|
-
| `u_cursor` | float[
|
|
99
|
-
| `u_click` | float[3] | Click position (x, y) and left click state (z).
|
|
100
|
-
| `u_history` | sampler2DArray | Buffer texture of prior frames.
|
|
122
|
+
| Uniform | Type | Description |
|
|
123
|
+
| -------------- | -------------- | ------------------------------------------------------------------------------- |
|
|
124
|
+
| `u_time` | float | The current time in seconds. |
|
|
125
|
+
| `u_frame` | int | The current frame number. |
|
|
126
|
+
| `u_resolution` | float[2] | The canvas element's dimensions. |
|
|
127
|
+
| `u_cursor` | float[2] | Cursor position (x, y). |
|
|
128
|
+
| `u_click` | float[3] | Click position (x, y) and left click state (z). |
|
|
129
|
+
| `u_history` | sampler2DArray | Buffer texture of prior frames. Only available if the `history` plugin is used. |
|
|
101
130
|
|
|
102
131
|
## Included varyings
|
|
103
132
|
|
|
@@ -120,15 +149,13 @@ npm install
|
|
|
120
149
|
npm run dev
|
|
121
150
|
```
|
|
122
151
|
|
|
123
|
-
This will launch a local server
|
|
124
|
-
|
|
125
|
-
`examples/index.html` hardcodes the `main.ts` example. To view a different example, change `<script type="module" src="/src/main.ts"></script>` to point to your new example.
|
|
152
|
+
This will launch a local server. Open the provided URL (usually `http://localhost:5173`) in your browser to view and interact with the examples. Use the select box to view a different example.
|
|
126
153
|
|
|
127
154
|
### Adding an example
|
|
128
155
|
|
|
129
156
|
- Add a new `.ts` file in `examples/src/`.
|
|
130
|
-
- Follow the structure of an existing example as a template.
|
|
131
|
-
-
|
|
157
|
+
- Follow the structure of an existing example as a template. The example must export an `init` function and a `destroy` function.
|
|
158
|
+
- Add the example to the `demos` array in `examples/src/main.ts`.
|
|
132
159
|
- If your example needs images or other assets, place them in `examples/public/` and reference them with a relative path.
|
|
133
160
|
|
|
134
161
|
## License
|
package/dist/index.d.mts
CHANGED
|
@@ -1,48 +1,91 @@
|
|
|
1
|
+
type WithHistory<T extends ShaderPad> = T & {
|
|
2
|
+
initializeTexture(name: string, source: HTMLImageElement | HTMLVideoElement, historyDepth?: number): void;
|
|
3
|
+
};
|
|
4
|
+
declare function history(framebufferDepth?: number): (shaderPad: ShaderPad, context: PluginContext) => void;
|
|
5
|
+
|
|
6
|
+
declare module '../index' {
|
|
7
|
+
interface ShaderPad {
|
|
8
|
+
save(filename: string): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
declare function save(): (shaderPad: ShaderPad, context: PluginContext) => void;
|
|
12
|
+
type WithSave<T extends ShaderPad> = T & {
|
|
13
|
+
save(filename: string): Promise<void>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
interface Uniform {
|
|
17
|
+
type: 'float' | 'int';
|
|
18
|
+
length: 1 | 2 | 3 | 4;
|
|
19
|
+
location: WebGLUniformLocation;
|
|
20
|
+
}
|
|
21
|
+
interface Texture {
|
|
22
|
+
texture: WebGLTexture;
|
|
23
|
+
unitIndex: number;
|
|
24
|
+
historyDepth?: number;
|
|
25
|
+
writeIndex?: number;
|
|
26
|
+
}
|
|
27
|
+
type TextureSource = HTMLImageElement | HTMLVideoElement;
|
|
28
|
+
interface PluginContext {
|
|
29
|
+
gl: WebGL2RenderingContext;
|
|
30
|
+
uniforms: Map<string, Uniform>;
|
|
31
|
+
textures: Map<string, Texture>;
|
|
32
|
+
get program(): WebGLProgram | null;
|
|
33
|
+
canvas: HTMLCanvasElement;
|
|
34
|
+
reserveTextureUnit: (name: string) => number;
|
|
35
|
+
releaseTextureUnit: (name: string) => void;
|
|
36
|
+
}
|
|
37
|
+
type Plugin = (shaderPad: ShaderPad, context: PluginContext) => void;
|
|
38
|
+
type LifecycleMethod = 'init' | 'step' | 'destroy' | 'updateResolution' | 'reset';
|
|
1
39
|
interface Options {
|
|
2
40
|
canvas?: HTMLCanvasElement | null;
|
|
3
|
-
|
|
41
|
+
plugins?: Plugin[];
|
|
4
42
|
}
|
|
5
43
|
declare class ShaderPad {
|
|
6
44
|
private isInternalCanvas;
|
|
7
45
|
private isTouchDevice;
|
|
8
|
-
private canvas;
|
|
9
46
|
private gl;
|
|
10
47
|
private downloadLink;
|
|
11
48
|
private fragmentShaderSrc;
|
|
12
49
|
private uniforms;
|
|
13
50
|
private textures;
|
|
51
|
+
private textureUnitPool;
|
|
14
52
|
private buffer;
|
|
15
53
|
private program;
|
|
16
54
|
private animationFrameId;
|
|
55
|
+
private resolutionObserver;
|
|
17
56
|
private resizeObserver;
|
|
18
57
|
private resizeTimeout;
|
|
19
58
|
private lastResizeTime;
|
|
20
59
|
private eventListeners;
|
|
21
60
|
private frame;
|
|
61
|
+
private startTime;
|
|
22
62
|
private cursorPosition;
|
|
23
|
-
private scrollX;
|
|
24
|
-
private scrollY;
|
|
25
63
|
private clickPosition;
|
|
26
64
|
private isMouseDown;
|
|
27
|
-
|
|
28
|
-
|
|
65
|
+
canvas: HTMLCanvasElement;
|
|
66
|
+
onResize?: (width: number, height: number) => void;
|
|
67
|
+
private hooks;
|
|
29
68
|
constructor(fragmentShaderSrc: string, options?: Options);
|
|
69
|
+
registerHook(name: LifecycleMethod, fn: Function): void;
|
|
30
70
|
private init;
|
|
31
|
-
private initializeHistoryBuffer;
|
|
32
71
|
private createShader;
|
|
33
72
|
private setupBuffer;
|
|
34
73
|
private throttledHandleResize;
|
|
35
74
|
private handleResize;
|
|
36
75
|
private addEventListeners;
|
|
76
|
+
private updateResolution;
|
|
37
77
|
initializeUniform(name: string, type: 'float' | 'int', value: number | number[]): void;
|
|
38
78
|
updateUniforms(updates: Record<string, number | number[]>): void;
|
|
39
79
|
step(time: number): void;
|
|
40
80
|
play(callback?: (time: number, frame: number) => void): void;
|
|
41
81
|
pause(): void;
|
|
42
|
-
|
|
82
|
+
reset(): void;
|
|
43
83
|
initializeTexture(name: string, source: HTMLImageElement | HTMLVideoElement): void;
|
|
44
84
|
updateTextures(updates: Record<string, HTMLImageElement | HTMLVideoElement>): void;
|
|
85
|
+
save(filename: string): Promise<void>;
|
|
45
86
|
destroy(): void;
|
|
87
|
+
private reserveTextureUnit;
|
|
88
|
+
private releaseTextureUnit;
|
|
46
89
|
}
|
|
47
90
|
|
|
48
|
-
export { ShaderPad as default };
|
|
91
|
+
export { type PluginContext, type TextureSource, type WithHistory, type WithSave, ShaderPad as default, history, save };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,48 +1,91 @@
|
|
|
1
|
+
type WithHistory<T extends ShaderPad> = T & {
|
|
2
|
+
initializeTexture(name: string, source: HTMLImageElement | HTMLVideoElement, historyDepth?: number): void;
|
|
3
|
+
};
|
|
4
|
+
declare function history(framebufferDepth?: number): (shaderPad: ShaderPad, context: PluginContext) => void;
|
|
5
|
+
|
|
6
|
+
declare module '../index' {
|
|
7
|
+
interface ShaderPad {
|
|
8
|
+
save(filename: string): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
declare function save(): (shaderPad: ShaderPad, context: PluginContext) => void;
|
|
12
|
+
type WithSave<T extends ShaderPad> = T & {
|
|
13
|
+
save(filename: string): Promise<void>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
interface Uniform {
|
|
17
|
+
type: 'float' | 'int';
|
|
18
|
+
length: 1 | 2 | 3 | 4;
|
|
19
|
+
location: WebGLUniformLocation;
|
|
20
|
+
}
|
|
21
|
+
interface Texture {
|
|
22
|
+
texture: WebGLTexture;
|
|
23
|
+
unitIndex: number;
|
|
24
|
+
historyDepth?: number;
|
|
25
|
+
writeIndex?: number;
|
|
26
|
+
}
|
|
27
|
+
type TextureSource = HTMLImageElement | HTMLVideoElement;
|
|
28
|
+
interface PluginContext {
|
|
29
|
+
gl: WebGL2RenderingContext;
|
|
30
|
+
uniforms: Map<string, Uniform>;
|
|
31
|
+
textures: Map<string, Texture>;
|
|
32
|
+
get program(): WebGLProgram | null;
|
|
33
|
+
canvas: HTMLCanvasElement;
|
|
34
|
+
reserveTextureUnit: (name: string) => number;
|
|
35
|
+
releaseTextureUnit: (name: string) => void;
|
|
36
|
+
}
|
|
37
|
+
type Plugin = (shaderPad: ShaderPad, context: PluginContext) => void;
|
|
38
|
+
type LifecycleMethod = 'init' | 'step' | 'destroy' | 'updateResolution' | 'reset';
|
|
1
39
|
interface Options {
|
|
2
40
|
canvas?: HTMLCanvasElement | null;
|
|
3
|
-
|
|
41
|
+
plugins?: Plugin[];
|
|
4
42
|
}
|
|
5
43
|
declare class ShaderPad {
|
|
6
44
|
private isInternalCanvas;
|
|
7
45
|
private isTouchDevice;
|
|
8
|
-
private canvas;
|
|
9
46
|
private gl;
|
|
10
47
|
private downloadLink;
|
|
11
48
|
private fragmentShaderSrc;
|
|
12
49
|
private uniforms;
|
|
13
50
|
private textures;
|
|
51
|
+
private textureUnitPool;
|
|
14
52
|
private buffer;
|
|
15
53
|
private program;
|
|
16
54
|
private animationFrameId;
|
|
55
|
+
private resolutionObserver;
|
|
17
56
|
private resizeObserver;
|
|
18
57
|
private resizeTimeout;
|
|
19
58
|
private lastResizeTime;
|
|
20
59
|
private eventListeners;
|
|
21
60
|
private frame;
|
|
61
|
+
private startTime;
|
|
22
62
|
private cursorPosition;
|
|
23
|
-
private scrollX;
|
|
24
|
-
private scrollY;
|
|
25
63
|
private clickPosition;
|
|
26
64
|
private isMouseDown;
|
|
27
|
-
|
|
28
|
-
|
|
65
|
+
canvas: HTMLCanvasElement;
|
|
66
|
+
onResize?: (width: number, height: number) => void;
|
|
67
|
+
private hooks;
|
|
29
68
|
constructor(fragmentShaderSrc: string, options?: Options);
|
|
69
|
+
registerHook(name: LifecycleMethod, fn: Function): void;
|
|
30
70
|
private init;
|
|
31
|
-
private initializeHistoryBuffer;
|
|
32
71
|
private createShader;
|
|
33
72
|
private setupBuffer;
|
|
34
73
|
private throttledHandleResize;
|
|
35
74
|
private handleResize;
|
|
36
75
|
private addEventListeners;
|
|
76
|
+
private updateResolution;
|
|
37
77
|
initializeUniform(name: string, type: 'float' | 'int', value: number | number[]): void;
|
|
38
78
|
updateUniforms(updates: Record<string, number | number[]>): void;
|
|
39
79
|
step(time: number): void;
|
|
40
80
|
play(callback?: (time: number, frame: number) => void): void;
|
|
41
81
|
pause(): void;
|
|
42
|
-
|
|
82
|
+
reset(): void;
|
|
43
83
|
initializeTexture(name: string, source: HTMLImageElement | HTMLVideoElement): void;
|
|
44
84
|
updateTextures(updates: Record<string, HTMLImageElement | HTMLVideoElement>): void;
|
|
85
|
+
save(filename: string): Promise<void>;
|
|
45
86
|
destroy(): void;
|
|
87
|
+
private reserveTextureUnit;
|
|
88
|
+
private releaseTextureUnit;
|
|
46
89
|
}
|
|
47
90
|
|
|
48
|
-
export { ShaderPad as default };
|
|
91
|
+
export { type PluginContext, type TextureSource, type WithHistory, type WithSave, ShaderPad as default, history, save };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var p=Object.defineProperty;var U=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var L=Object.prototype.hasOwnProperty;var P=(s,t)=>{for(var i in t)p(s,i,{get:t[i],enumerable:!0})},I=(s,t,i,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of A(t))!L.call(s,r)&&r!==i&&p(s,r,{get:()=>t[r],enumerable:!(e=U(t,r))||e.enumerable});return s};var S=s=>I(p({},"__esModule",{value:!0}),s);var C={};P(C,{default:()=>F,history:()=>y,save:()=>D});module.exports=S(C);function R(s){let t=s,i=s,e=t.videoWidth||i.naturalWidth||i.width,r=t.videoHeight||i.naturalHeight||i.height;return{width:e,height:r}}function _(s,t,i,e){let r=new Uint8Array(t*i*4);for(let n=0;n<e;++n)s.texSubImage3D(s.TEXTURE_2D_ARRAY,0,0,0,n,t,i,1,s.RGBA,s.UNSIGNED_BYTE,r)}function b(s,t,i,e){let r=s.createTexture();if(!r)throw new Error("Failed to create history texture");return s.activeTexture(s.TEXTURE0),s.bindTexture(s.TEXTURE_2D_ARRAY,r),s.texParameteri(s.TEXTURE_2D_ARRAY,s.TEXTURE_WRAP_S,s.CLAMP_TO_EDGE),s.texParameteri(s.TEXTURE_2D_ARRAY,s.TEXTURE_WRAP_T,s.CLAMP_TO_EDGE),s.texParameteri(s.TEXTURE_2D_ARRAY,s.TEXTURE_MIN_FILTER,s.LINEAR),s.texParameteri(s.TEXTURE_2D_ARRAY,s.TEXTURE_MAG_FILTER,s.LINEAR),s.texStorage3D(s.TEXTURE_2D_ARRAY,1,s.RGBA8,i,e,t),_(s,i,e,t),r}function y(s=1){return function(t,i){let{gl:e,uniforms:r,textures:n,reserveTextureUnit:a}=i,c=t,T=t.initializeTexture.bind(t),w=t.updateTextures.bind(t),u=null;if(s>1){let o=function(){u=b(e,s,e.drawingBufferWidth,e.drawingBufferHeight),r.has("u_history")||t.initializeUniform("u_history","int",0)},l=function(){u&&(e.activeTexture(e.TEXTURE0),e.bindTexture(e.TEXTURE_2D_ARRAY,u),_(e,e.drawingBufferWidth,e.drawingBufferHeight,s))};var W=o,G=l;t.registerHook("init",o),t.registerHook("step",(d,h)=>{if(!u)return;let f=h%s;e.activeTexture(e.TEXTURE0),e.bindTexture(e.TEXTURE_2D_ARRAY,u),e.copyTexSubImage3D(e.TEXTURE_2D_ARRAY,0,0,0,f,0,0,e.drawingBufferWidth,e.drawingBufferHeight)}),t.registerHook("updateResolution",()=>{u&&e.deleteTexture(u),o()}),t.registerHook("reset",l)}t.registerHook("reset",()=>{n.forEach(o=>{o.historyDepth&&o.historyDepth>1&&o.writeIndex!==void 0&&(o.writeIndex=0)})}),c.initializeTexture=function(o,l,d=1){let{width:h,height:f}=R(l);if(!h||!f)throw new Error("Texture source must have valid dimensions");let m=Math.floor(d);if(m<=1)return T(o,l);if(n.has(o))throw new Error(`Texture '${o}' is already initialized.`);let g,E=null;try{if(g=a(o),E=b(e,m,h,f),e.activeTexture(e.TEXTURE0+g),e.bindTexture(e.TEXTURE_2D_ARRAY,E),e.pixelStorei(e.UNPACK_FLIP_Y_WEBGL,!0),e.texParameteri(e.TEXTURE_2D_ARRAY,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D_ARRAY,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D_ARRAY,e.TEXTURE_MIN_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D_ARRAY,e.TEXTURE_MAG_FILTER,e.LINEAR),i.program){let x=e.getUniformLocation(i.program,o);x&&e.uniform1i(x,g)}n.set(o,{texture:E,unitIndex:g,historyDepth:m,writeIndex:0}),c.updateTextures({[o]:l})}catch(x){throw E&&e.deleteTexture(E),g!==void 0&&i.releaseTextureUnit(o),x}},c.updateTextures=function(o){Object.entries(o).forEach(([l,d])=>{let h=n.get(l);if(!h||!h.historyDepth||h.historyDepth<=1)return w({[l]:d});let{width:f,height:m}=R(d),g=h.writeIndex??0;e.activeTexture(e.TEXTURE0+h.unitIndex),e.bindTexture(e.TEXTURE_2D_ARRAY,h.texture),e.texSubImage3D(e.TEXTURE_2D_ARRAY,0,0,0,g,f,m,1,e.RGBA,e.UNSIGNED_BYTE,d),h.writeIndex=(g+1)%h.historyDepth})},t.registerHook("destroy",()=>{u&&e.deleteTexture(u)})}}function D(){return function(s,t){let{gl:i,canvas:e}=t,r=document.createElement("a");s.save=async function(n){if(i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLES,0,6),n&&!`${n}`.toLowerCase().endsWith(".png")&&(n=`${n}.png`),n=n||"export.png","ongesturechange"in window)try{let a=await new Promise(T=>e.toBlob(T,"image/png")),c=new File([a],n,{type:a.type});if(navigator.canShare?.({files:[c]})){await navigator.share({files:[c]});return}}catch(a){console.warn("Web Share API failed:",a)}else r.download=n,r.href=e.toDataURL(),r.click()}}}var M=`#version 300 es
|
|
2
2
|
in vec2 aPosition;
|
|
3
3
|
out vec2 v_uv;
|
|
4
4
|
void main() {
|
|
5
5
|
v_uv = aPosition * 0.5 + 0.5;
|
|
6
6
|
gl_Position = vec4(aPosition, 0.0, 1.0);
|
|
7
7
|
}
|
|
8
|
-
`,v=33.333333333333336,o=class{constructor(t,e={}){this.isInternalCanvas=!1;this.isTouchDevice=!1;this.uniforms=new Map;this.textures=new Map;this.buffer=null;this.program=null;this.resizeTimeout=null;this.lastResizeTime=0;this.eventListeners=new Map;this.frame=0;this.cursorPosition=[.5,.5];this.scrollX=0;this.scrollY=0;this.clickPosition=[.5,.5];this.isMouseDown=!1;this.historyTexture=null;if(this.canvas=e.canvas||document.createElement("canvas"),this.historyLength=e.history||0,e.canvas||(this.isInternalCanvas=!0,document.body.appendChild(this.canvas),this.canvas.style.position="fixed",this.canvas.style.inset="0",this.canvas.style.height="100dvh",this.canvas.style.width="100dvw"),this.gl=this.canvas.getContext("webgl2",{antialias:!1}),!this.gl)throw new Error("WebGL2 not supported. Please use a browser that supports WebGL2.");this.downloadLink=document.createElement("a"),this.fragmentShaderSrc=t,this.animationFrameId=null,this.resizeObserver=new ResizeObserver(()=>this.throttledHandleResize()),this.resizeObserver.observe(this.canvas),this.init(),this.addEventListeners()}init(){let t=T;if(this.program=this.gl.createProgram(),!this.program)throw new Error("Failed to create WebGL program");let e=this.createShader(this.gl.VERTEX_SHADER,t),i=this.createShader(this.gl.FRAGMENT_SHADER,this.fragmentShaderSrc);if(this.gl.attachShader(this.program,e),this.gl.attachShader(this.program,i),this.gl.linkProgram(this.program),this.gl.deleteShader(e),this.gl.deleteShader(i),!this.gl.getProgramParameter(this.program,this.gl.LINK_STATUS))throw console.error("Program link error:",this.gl.getProgramInfoLog(this.program)),this.gl.deleteProgram(this.program),new Error("Failed to link WebGL program");let s=this.gl.getAttribLocation(this.program,"aPosition");this.setupBuffer(s),this.handleResize(),this.gl.useProgram(this.program),this.initializeUniform("u_resolution","float",[this.canvas.width,this.canvas.height]),this.initializeUniform("u_cursor","float",[...this.cursorPosition,this.scrollX,this.scrollY]),this.initializeUniform("u_click","float",[...this.clickPosition,this.isMouseDown?1:0]),this.initializeUniform("u_time","float",0),this.initializeUniform("u_frame","int",0),this.historyLength>0&&this.initializeHistoryBuffer()}initializeHistoryBuffer(){let{gl:t}=this;if(this.historyTexture=t.createTexture(),!this.historyTexture)throw new Error("Failed to create history texture");t.bindTexture(t.TEXTURE_2D_ARRAY,this.historyTexture),t.texParameteri(t.TEXTURE_2D_ARRAY,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D_ARRAY,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D_ARRAY,t.TEXTURE_MIN_FILTER,t.NEAREST),t.texParameteri(t.TEXTURE_2D_ARRAY,t.TEXTURE_MAG_FILTER,t.NEAREST),t.texStorage3D(t.TEXTURE_2D_ARRAY,1,t.RGBA8,this.canvas.width,this.canvas.height,this.historyLength);let e=new Uint8Array(this.canvas.width*this.canvas.height*4);for(let i=0;i<this.historyLength;++i)t.texSubImage3D(t.TEXTURE_2D_ARRAY,0,0,0,i,this.canvas.width,this.canvas.height,1,t.RGBA,t.UNSIGNED_BYTE,e);t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D_ARRAY,this.historyTexture),this.uniforms.has("u_history")||this.initializeUniform("u_history","int",0)}createShader(t,e){let i=this.gl.createShader(t);if(this.gl.shaderSource(i,e),this.gl.compileShader(i),!this.gl.getShaderParameter(i,this.gl.COMPILE_STATUS))throw console.error("Shader compilation failed:",e),console.error(this.gl.getShaderInfoLog(i)),this.gl.deleteShader(i),new Error("Shader compilation failed");return i}setupBuffer(t){let e=new Float32Array([-1,-1,1,-1,-1,1,-1,1,1,-1,1,1]);this.buffer=this.gl.createBuffer(),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.buffer),this.gl.bufferData(this.gl.ARRAY_BUFFER,e,this.gl.STATIC_DRAW),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.gl.enableVertexAttribArray(t),this.gl.vertexAttribPointer(t,2,this.gl.FLOAT,!1,0,0)}throttledHandleResize(){clearTimeout(this.resizeTimeout);let t=performance.now(),e=this.lastResizeTime+v-t;e<=0?(this.lastResizeTime=t,this.handleResize()):this.resizeTimeout=setTimeout(()=>this.throttledHandleResize(),e)}handleResize(){let t=window.devicePixelRatio||1,e=this.canvas.clientWidth*t,i=this.canvas.clientHeight*t,s=getComputedStyle(this.canvas),r=s.width!==`${this.canvas.width}px`&&s.width!=="auto",h=s.height!==`${this.canvas.height}px`&&s.height!=="auto";(!r||!h)&&(this.canvas.style.width=`${this.canvas.clientWidth}px`,this.canvas.style.height=`${this.canvas.clientHeight}px`),(this.canvas.width!==e||this.canvas.height!==i)&&(this.canvas.width=e,this.canvas.height=i,this.gl.viewport(0,0,this.gl.drawingBufferWidth,this.gl.drawingBufferHeight),this.uniforms.has("u_resolution")&&this.updateUniforms({u_resolution:[this.canvas.width,this.canvas.height]}),this.historyLength>0&&this.historyTexture&&(this.gl.deleteTexture(this.historyTexture),this.initializeHistoryBuffer()))}addEventListeners(){let t=(i,s)=>{if(!this.uniforms.has("u_cursor"))return;let r=this.canvas.getBoundingClientRect();this.cursorPosition[0]=(i-r.left)/r.width,this.cursorPosition[1]=1-(s-r.top)/r.height,this.updateUniforms({u_cursor:[this.cursorPosition[0],this.cursorPosition[1],this.scrollX,this.scrollY]})},e=(i,s,r)=>{if(this.uniforms.has("u_click")){if(this.isMouseDown=i,i){let h=this.canvas.getBoundingClientRect(),l=s,c=r;this.clickPosition[0]=(l-h.left)/h.width,this.clickPosition[1]=1-(c-h.top)/h.height}this.updateUniforms({u_click:[this.clickPosition[0],this.clickPosition[1],this.isMouseDown?1:0]})}};this.eventListeners.set("mousemove",i=>{let s=i;this.isTouchDevice||t(s.clientX,s.clientY)}),this.eventListeners.set("mousedown",i=>{let s=i;this.isTouchDevice||s.button===0&&(this.isMouseDown=!0,e(!0,s.clientX,s.clientY))}),this.eventListeners.set("mouseup",i=>{let s=i;this.isTouchDevice||s.button===0&&e(!1)}),this.eventListeners.set("wheel",i=>{let s=i;this.scrollX+=s.deltaX*.01,this.scrollY+=s.deltaY*.01,t(s.clientX,s.clientY)}),this.eventListeners.set("touchmove",i=>{let s=i;s.touches.length>0&&t(s.touches[0].clientX,s.touches[0].clientY)}),this.eventListeners.set("touchstart",i=>{let s=i;this.isTouchDevice=!0,s.touches.length>0&&(e(!0,s.touches[0].clientX,s.touches[0].clientY),t(s.touches[0].clientX,s.touches[0].clientY))}),this.eventListeners.set("touchend",i=>{i.touches.length===0&&e(!1)}),this.eventListeners.forEach((i,s)=>{this.canvas.addEventListener(s,i)})}initializeUniform(t,e,i){if(this.uniforms.has(t))throw new Error(`Uniform '${t}' is already initialized.`);if(e!=="float"&&e!=="int")throw new Error(`Invalid uniform type: ${e}. Expected 'float' or 'int'.`);let s=this.gl.getUniformLocation(this.program,t);if(!s){console.debug(`Uniform ${t} not found in fragment shader. Skipping initialization.`);return}if(Array.isArray(i)||(i=[i]),i.length<1||i.length>4)throw new Error(`Invalid uniform value length: ${i.length}. Expected a length between 1 and 4.`);let r=i.length;this.uniforms.set(t,{type:e,length:r,location:s}),this.updateUniforms({[t]:i})}updateUniforms(t){Object.entries(t).forEach(([e,i])=>{if(!this.uniforms.has(e))throw new Error(`Uniform '${e}' is not initialized.`);let s=this.uniforms.get(e);if(Array.isArray(i)||(i=[i]),i.length!==s.length)throw new Error(`Invalid uniform value length: ${i.length}. Expected ${s.length}.`);this.gl[`uniform${s.length}${s.type.charAt(0)}`](s.location,...i)})}step(t){let e=this.gl;if(this.uniforms.has("u_time")&&this.updateUniforms({u_time:t}),this.uniforms.has("u_frame")&&this.updateUniforms({u_frame:this.frame}),this.historyLength>0){let i=this.frame%this.historyLength;e.bindFramebuffer(e.FRAMEBUFFER,null),e.viewport(0,0,this.canvas.width,this.canvas.height),e.clear(e.COLOR_BUFFER_BIT),e.drawArrays(e.TRIANGLES,0,6),e.activeTexture(e.TEXTURE0),e.bindTexture(e.TEXTURE_2D_ARRAY,this.historyTexture),e.copyTexSubImage3D(e.TEXTURE_2D_ARRAY,0,0,0,i,0,0,this.canvas.width,this.canvas.height)}else e.clear(e.COLOR_BUFFER_BIT),e.drawArrays(e.TRIANGLES,0,6);++this.frame}play(t){let e=i=>{i/=1e3,this.step(i),t&&t(i,this.frame),this.animationFrameId=requestAnimationFrame(e)};this.animationFrameId=requestAnimationFrame(e)}pause(){this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null)}save(t){this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.drawArrays(this.gl.TRIANGLES,0,6);let e=this.canvas.toDataURL();t&&!`${t}`.toLowerCase().endsWith(".png")&&(t=`${t}.png`),this.downloadLink.download=t||"export.png",this.downloadLink.href=e,this.downloadLink.click()}initializeTexture(t,e){if(this.textures.has(t))throw new Error(`Texture '${t}' is already initialized.`);let i=this.gl.createTexture();if(!i)throw new Error("Failed to create texture");let s=this.textures.size+1;this.gl.activeTexture(this.gl.TEXTURE0+s),this.gl.bindTexture(this.gl.TEXTURE_2D,i),this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL,!0),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_S,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_T,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.LINEAR),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.LINEAR),this.textures.set(t,{texture:i,unitIndex:s}),this.updateTextures({[t]:e});let r=this.gl.getUniformLocation(this.program,t);r&&this.gl.uniform1i(r,s)}updateTextures(t){Object.entries(t).forEach(([e,i])=>{let s=this.textures.get(e);if(!s)throw new Error(`Texture '${e}' is not initialized.`);this.gl.activeTexture(this.gl.TEXTURE0+s.unitIndex),this.gl.bindTexture(this.gl.TEXTURE_2D,s.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,i)})}destroy(){this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null),this.resizeObserver.unobserve(this.canvas),this.eventListeners.forEach((t,e)=>{this.canvas.removeEventListener(e,t)}),this.program&&this.gl.deleteProgram(this.program),this.textures.forEach(t=>{this.gl.deleteTexture(t.texture)}),this.historyTexture&&(this.gl.deleteTexture(this.historyTexture),this.historyTexture=null),this.buffer&&(this.gl.deleteBuffer(this.buffer),this.buffer=null),this.isInternalCanvas&&this.canvas.remove()}},R=o;
|
|
8
|
+
`,X=1e3/30,v=class{constructor(t,i={}){this.isInternalCanvas=!1;this.isTouchDevice=!1;this.uniforms=new Map;this.textures=new Map;this.buffer=null;this.program=null;this.resizeTimeout=null;this.lastResizeTime=0;this.eventListeners=new Map;this.frame=0;this.startTime=0;this.cursorPosition=[.5,.5];this.clickPosition=[.5,.5];this.isMouseDown=!1;this.hooks=new Map;if(this.canvas=i.canvas||document.createElement("canvas"),i.canvas||(this.isInternalCanvas=!0,document.body.appendChild(this.canvas),this.canvas.style.position="fixed",this.canvas.style.inset="0",this.canvas.style.height="100dvh",this.canvas.style.width="100dvw"),this.gl=this.canvas.getContext("webgl2",{antialias:!1}),!this.gl)throw new Error("WebGL2 not supported. Please use a browser that supports WebGL2.");if(this.textureUnitPool={free:[],next:1,max:this.gl.getParameter(this.gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS)},this.downloadLink=document.createElement("a"),this.fragmentShaderSrc=t,this.animationFrameId=null,this.resolutionObserver=new MutationObserver(()=>this.updateResolution()),this.resizeObserver=new ResizeObserver(()=>this.throttledHandleResize()),i.plugins){let e={gl:this.gl,uniforms:this.uniforms,textures:this.textures,canvas:this.canvas,reserveTextureUnit:this.reserveTextureUnit.bind(this),releaseTextureUnit:this.releaseTextureUnit.bind(this)};Object.defineProperty(e,"program",{get:()=>this.program,enumerable:!0,configurable:!0}),i.plugins.forEach(r=>r(this,e))}this.init(),this.addEventListeners()}registerHook(t,i){this.hooks.has(t)||this.hooks.set(t,[]),this.hooks.get(t).push(i)}init(){let t=M;if(this.program=this.gl.createProgram(),!this.program)throw new Error("Failed to create WebGL program");let i=this.createShader(this.gl.VERTEX_SHADER,t),e=this.createShader(this.gl.FRAGMENT_SHADER,this.fragmentShaderSrc);if(this.gl.attachShader(this.program,i),this.gl.attachShader(this.program,e),this.gl.linkProgram(this.program),this.gl.deleteShader(i),this.gl.deleteShader(e),!this.gl.getProgramParameter(this.program,this.gl.LINK_STATUS))throw console.error("Program link error:",this.gl.getProgramInfoLog(this.program)),this.gl.deleteProgram(this.program),new Error("Failed to link WebGL program");let r=this.gl.getAttribLocation(this.program,"aPosition");this.setupBuffer(r),this.gl.useProgram(this.program),this.resolutionObserver.observe(this.canvas,{attributes:!0,attributeFilter:["width","height"]}),this.resizeObserver.observe(this.canvas),this.isInternalCanvas||this.updateResolution(),this.initializeUniform("u_cursor","float",this.cursorPosition),this.initializeUniform("u_click","float",[...this.clickPosition,this.isMouseDown?1:0]),this.initializeUniform("u_time","float",0),this.initializeUniform("u_frame","int",0),this.hooks.get("init")?.forEach(n=>n.call(this))}createShader(t,i){let e=this.gl.createShader(t);if(this.gl.shaderSource(e,i),this.gl.compileShader(e),!this.gl.getShaderParameter(e,this.gl.COMPILE_STATUS))throw console.error("Shader compilation failed:",i),console.error(this.gl.getShaderInfoLog(e)),this.gl.deleteShader(e),new Error("Shader compilation failed");return e}setupBuffer(t){let i=new Float32Array([-1,-1,1,-1,-1,1,-1,1,1,-1,1,1]);this.buffer=this.gl.createBuffer(),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.buffer),this.gl.bufferData(this.gl.ARRAY_BUFFER,i,this.gl.STATIC_DRAW),this.gl.viewport(0,0,this.gl.drawingBufferWidth,this.gl.drawingBufferHeight),this.gl.enableVertexAttribArray(t),this.gl.vertexAttribPointer(t,2,this.gl.FLOAT,!1,0,0)}throttledHandleResize(){clearTimeout(this.resizeTimeout);let t=performance.now(),i=this.lastResizeTime+X-t;i<=0?(this.lastResizeTime=t,this.handleResize()):this.resizeTimeout=setTimeout(()=>this.throttledHandleResize(),i)}handleResize(){let t=window.devicePixelRatio||1,i=this.canvas.clientWidth*t,e=this.canvas.clientHeight*t;this.isInternalCanvas&&(this.canvas.width!==i||this.canvas.height!==e)&&(this.canvas.width=i,this.canvas.height=e),this.onResize?.(i,e)}addEventListeners(){let t=(e,r)=>{if(!this.uniforms.has("u_cursor"))return;let n=this.canvas.getBoundingClientRect();this.cursorPosition[0]=(e-n.left)/n.width,this.cursorPosition[1]=1-(r-n.top)/n.height,this.updateUniforms({u_cursor:this.cursorPosition})},i=(e,r,n)=>{if(this.uniforms.has("u_click")){if(this.isMouseDown=e,e){let a=this.canvas.getBoundingClientRect(),c=r,T=n;this.clickPosition[0]=(c-a.left)/a.width,this.clickPosition[1]=1-(T-a.top)/a.height}this.updateUniforms({u_click:[...this.clickPosition,this.isMouseDown?1:0]})}};this.eventListeners.set("mousemove",e=>{let r=e;this.isTouchDevice||t(r.clientX,r.clientY)}),this.eventListeners.set("mousedown",e=>{let r=e;this.isTouchDevice||r.button===0&&(this.isMouseDown=!0,i(!0,r.clientX,r.clientY))}),this.eventListeners.set("mouseup",e=>{let r=e;this.isTouchDevice||r.button===0&&i(!1)}),this.eventListeners.set("touchmove",e=>{let r=e;r.touches.length>0&&t(r.touches[0].clientX,r.touches[0].clientY)}),this.eventListeners.set("touchstart",e=>{let r=e;this.isTouchDevice=!0,r.touches.length>0&&(t(r.touches[0].clientX,r.touches[0].clientY),i(!0,r.touches[0].clientX,r.touches[0].clientY))}),this.eventListeners.set("touchend",e=>{e.touches.length===0&&i(!1)}),this.eventListeners.forEach((e,r)=>{this.canvas.addEventListener(r,e)})}updateResolution(){let t=this.gl.drawingBufferWidth,i=this.gl.drawingBufferHeight;this.gl.viewport(0,0,t,i),this.uniforms.has("u_resolution")?this.updateUniforms({u_resolution:[t,i]}):this.initializeUniform("u_resolution","float",[t,i]),this.hooks.get("updateResolution")?.forEach(e=>e.call(this))}initializeUniform(t,i,e){if(this.uniforms.has(t))throw new Error(`Uniform '${t}' is already initialized.`);if(i!=="float"&&i!=="int")throw new Error(`Invalid uniform type: ${i}. Expected 'float' or 'int'.`);let r=this.gl.getUniformLocation(this.program,t);if(!r){console.debug(`Uniform ${t} not found in fragment shader. Skipping initialization.`);return}if(Array.isArray(e)||(e=[e]),e.length<1||e.length>4)throw new Error(`Invalid uniform value length: ${e.length}. Expected a length between 1 and 4.`);let n=e.length;this.uniforms.set(t,{type:i,length:n,location:r}),this.updateUniforms({[t]:e})}updateUniforms(t){Object.entries(t).forEach(([i,e])=>{if(!this.uniforms.has(i))throw new Error(`Uniform '${i}' is not initialized.`);let r=this.uniforms.get(i);if(Array.isArray(e)||(e=[e]),e.length!==r.length)throw new Error(`Invalid uniform value length: ${e.length}. Expected ${r.length}.`);this.gl[`uniform${r.length}${r.type.charAt(0)}`](r.location,...e)})}step(t){let i=this.gl;this.uniforms.has("u_time")&&this.updateUniforms({u_time:t}),this.uniforms.has("u_frame")&&this.updateUniforms({u_frame:this.frame}),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLES,0,6),this.hooks.get("step")?.forEach(e=>e.call(this,t,this.frame)),++this.frame}play(t){this.pause();let i=e=>{e=(e-this.startTime)/1e3,this.step(e),this.animationFrameId=requestAnimationFrame(i),t&&t(e,this.frame)};this.animationFrameId=requestAnimationFrame(i)}pause(){this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null)}reset(){this.frame=0,this.startTime=performance.now(),this.hooks.get("reset")?.forEach(t=>t.call(this))}initializeTexture(t,i){if(this.textures.has(t))throw new Error(`Texture '${t}' is already initialized.`);let e=this.gl.createTexture();if(!e)throw new Error("Failed to create texture");let r;try{r=this.reserveTextureUnit(t)}catch(a){throw this.gl.deleteTexture(e),a}this.gl.activeTexture(this.gl.TEXTURE0+r),this.gl.bindTexture(this.gl.TEXTURE_2D,e),this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL,!0),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_S,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_T,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.LINEAR),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.LINEAR),this.textures.set(t,{texture:e,unitIndex:r}),this.updateTextures({[t]:i});let n=this.gl.getUniformLocation(this.program,t);n&&this.gl.uniform1i(n,r)}updateTextures(t){Object.entries(t).forEach(([i,e])=>{let r=this.textures.get(i);if(!r)throw new Error(`Texture '${i}' is not initialized.`);this.gl.activeTexture(this.gl.TEXTURE0+r.unitIndex),this.gl.bindTexture(this.gl.TEXTURE_2D,r.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,e)})}async save(t){if(this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.drawArrays(this.gl.TRIANGLES,0,6),t&&!`${t}`.toLowerCase().endsWith(".png")&&(t=`${t}.png`),t=t||"export.png","ongesturechange"in window)try{let i=await new Promise(r=>this.canvas.toBlob(r,"image/png")),e=new File([i],t,{type:i.type});if(navigator.canShare?.({files:[e]})){await navigator.share({files:[e]});return}}catch(i){console.warn("Web Share API failed:",i)}else this.downloadLink.download=t,this.downloadLink.href=this.canvas.toDataURL(),this.downloadLink.click()}destroy(){this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null),this.resolutionObserver.disconnect(),this.resizeObserver.disconnect(),this.eventListeners.forEach((t,i)=>{this.canvas.removeEventListener(i,t)}),this.program&&this.gl.deleteProgram(this.program),this.textures.forEach(t=>{this.gl.deleteTexture(t.texture)}),this.textureUnitPool.free=[],this.textureUnitPool.next=1,this.buffer&&(this.gl.deleteBuffer(this.buffer),this.buffer=null),this.hooks.get("destroy")?.forEach(t=>t.call(this)),this.isInternalCanvas&&this.canvas.remove()}reserveTextureUnit(t){if(!t)throw new Error("Texture unit name must be provided.");let i=this.textures.get(t);if(i)return i.unitIndex;let e;if(this.textureUnitPool.free.length>0)e=this.textureUnitPool.free.pop();else{if(e=this.textureUnitPool.next,e>=this.textureUnitPool.max)throw new Error("Exceeded the available texture units for this device.");this.textureUnitPool.next+=1}return e}releaseTextureUnit(t){let i=this.textures.get(t);if(!i)return;let e=i.unitIndex;e>0&&this.textureUnitPool.free.push(e)}},F=v;0&&(module.exports={history,save});
|
|
9
9
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["const DEFAULT_VERTEX_SHADER_SRC = `#version 300 es\nin vec2 aPosition;\nout vec2 v_uv;\nvoid main() {\n v_uv = aPosition * 0.5 + 0.5;\n gl_Position = vec4(aPosition, 0.0, 1.0);\n}\n`;\nconst RESIZE_THROTTLE_INTERVAL = 1000 / 30;\n\ninterface Uniform {\n\ttype: 'float' | 'int';\n\tlength: 1 | 2 | 3 | 4;\n\tlocation: WebGLUniformLocation;\n}\n\ninterface Texture {\n\ttexture: WebGLTexture;\n\tunitIndex: number;\n}\n\ninterface Options {\n\tcanvas?: HTMLCanvasElement | null;\n\thistory?: number;\n}\n\nclass ShaderPad {\n\tprivate isInternalCanvas = false;\n\tprivate isTouchDevice = false;\n\tprivate canvas: HTMLCanvasElement;\n\tprivate gl: WebGL2RenderingContext;\n\tprivate downloadLink: HTMLAnchorElement;\n\tprivate fragmentShaderSrc: string;\n\tprivate uniforms: Map<string, Uniform> = new Map();\n\tprivate textures: Map<string, Texture> = new Map();\n\tprivate buffer: WebGLBuffer | null = null;\n\tprivate program: WebGLProgram | null = null;\n\tprivate animationFrameId: number | null;\n\tprivate resizeObserver: ResizeObserver;\n\tprivate resizeTimeout: NodeJS.Timeout = null as unknown as NodeJS.Timeout;\n\tprivate lastResizeTime = 0;\n\tprivate eventListeners: Map<string, EventListener> = new Map();\n\tprivate frame = 0;\n\tprivate cursorPosition = [0.5, 0.5];\n\tprivate scrollX = 0;\n\tprivate scrollY = 0;\n\tprivate clickPosition = [0.5, 0.5];\n\tprivate isMouseDown = false;\n\tprivate historyLength: number;\n\tprivate historyTexture: WebGLTexture | null = null;\n\n\tconstructor(fragmentShaderSrc: string, options: Options = {}) {\n\t\tthis.canvas = options.canvas || document.createElement('canvas');\n\t\tthis.historyLength = options.history || 0;\n\t\tif (!options.canvas) {\n\t\t\tthis.isInternalCanvas = true;\n\t\t\tdocument.body.appendChild(this.canvas);\n\t\t\tthis.canvas.style.position = 'fixed';\n\t\t\tthis.canvas.style.inset = '0';\n\t\t\tthis.canvas.style.height = '100dvh';\n\t\t\tthis.canvas.style.width = '100dvw';\n\t\t}\n\n\t\tthis.gl = this.canvas.getContext('webgl2', { antialias: false }) as WebGL2RenderingContext;\n\t\tif (!this.gl) {\n\t\t\tthrow new Error('WebGL2 not supported. Please use a browser that supports WebGL2.');\n\t\t}\n\n\t\tthis.downloadLink = document.createElement('a');\n\t\tthis.fragmentShaderSrc = fragmentShaderSrc;\n\t\tthis.animationFrameId = null;\n\t\tthis.resizeObserver = new ResizeObserver(() => this.throttledHandleResize());\n\t\tthis.resizeObserver.observe(this.canvas);\n\t\tthis.init();\n\t\tthis.addEventListeners();\n\t}\n\n\tprivate init() {\n\t\tconst vertexShaderSrc = DEFAULT_VERTEX_SHADER_SRC;\n\n\t\tthis.program = this.gl.createProgram();\n\t\tif (!this.program) {\n\t\t\tthrow new Error('Failed to create WebGL program');\n\t\t}\n\t\tconst vertexShader = this.createShader(this.gl.VERTEX_SHADER, vertexShaderSrc);\n\t\tconst fragmentShader = this.createShader(this.gl.FRAGMENT_SHADER, this.fragmentShaderSrc);\n\n\t\tthis.gl.attachShader(this.program, vertexShader);\n\t\tthis.gl.attachShader(this.program, fragmentShader);\n\t\tthis.gl.linkProgram(this.program);\n\t\tthis.gl.deleteShader(vertexShader);\n\t\tthis.gl.deleteShader(fragmentShader);\n\n\t\tif (!this.gl.getProgramParameter(this.program, this.gl.LINK_STATUS)) {\n\t\t\tconsole.error('Program link error:', this.gl.getProgramInfoLog(this.program));\n\t\t\tthis.gl.deleteProgram(this.program);\n\t\t\tthrow new Error('Failed to link WebGL program');\n\t\t}\n\n\t\tconst aPosition = this.gl.getAttribLocation(this.program, 'aPosition');\n\t\tthis.setupBuffer(aPosition);\n\t\tthis.handleResize();\n\n\t\tthis.gl.useProgram(this.program);\n\n\t\tthis.initializeUniform('u_resolution', 'float', [this.canvas.width, this.canvas.height]);\n\t\tthis.initializeUniform('u_cursor', 'float', [...this.cursorPosition, this.scrollX, this.scrollY]); // [cursorX, cursorY, scrollX, scrollY]\n\t\tthis.initializeUniform('u_click', 'float', [...this.clickPosition, this.isMouseDown ? 1.0 : 0.0]); // [clickX, clickY, leftClick]\n\t\tthis.initializeUniform('u_time', 'float', 0);\n\t\tthis.initializeUniform('u_frame', 'int', 0);\n\n\t\tif (this.historyLength > 0) {\n\t\t\tthis.initializeHistoryBuffer();\n\t\t}\n\t}\n\n\tprivate initializeHistoryBuffer() {\n\t\tconst { gl } = this;\n\n\t\tthis.historyTexture = gl.createTexture();\n\t\tif (!this.historyTexture) {\n\t\t\tthrow new Error('Failed to create history texture');\n\t\t}\n\n\t\tgl.bindTexture(gl.TEXTURE_2D_ARRAY, this.historyTexture);\n\t\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n\t\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n\t\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.NEAREST);\n\t\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MAG_FILTER, gl.NEAREST);\n\t\tgl.texStorage3D(gl.TEXTURE_2D_ARRAY, 1, gl.RGBA8, this.canvas.width, this.canvas.height, this.historyLength);\n\n\t\t// Make initial frames transparent.\n\t\tconst transparent = new Uint8Array(this.canvas.width * this.canvas.height * 4); // All zeroes.\n\t\tfor (let layer = 0; layer < this.historyLength; ++layer) {\n\t\t\tgl.texSubImage3D(\n\t\t\t\tgl.TEXTURE_2D_ARRAY,\n\t\t\t\t0,\n\t\t\t\t0,\n\t\t\t\t0,\n\t\t\t\tlayer,\n\t\t\t\tthis.canvas.width,\n\t\t\t\tthis.canvas.height,\n\t\t\t\t1,\n\t\t\t\tgl.RGBA,\n\t\t\t\tgl.UNSIGNED_BYTE,\n\t\t\t\ttransparent\n\t\t\t);\n\t\t}\n\n\t\tgl.activeTexture(gl.TEXTURE0);\n\t\tgl.bindTexture(gl.TEXTURE_2D_ARRAY, this.historyTexture);\n\n\t\tif (!this.uniforms.has('u_history')) {\n\t\t\tthis.initializeUniform('u_history', 'int', 0);\n\t\t}\n\t}\n\n\tprivate createShader(type: number, source: string): WebGLShader {\n\t\tconst shader = this.gl.createShader(type)!;\n\t\tthis.gl.shaderSource(shader, source);\n\t\tthis.gl.compileShader(shader);\n\t\tif (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)) {\n\t\t\tconsole.error('Shader compilation failed:', source);\n\t\t\tconsole.error(this.gl.getShaderInfoLog(shader));\n\t\t\tthis.gl.deleteShader(shader);\n\t\t\tthrow new Error('Shader compilation failed');\n\t\t}\n\t\treturn shader;\n\t}\n\n\tprivate setupBuffer(aPosition: number) {\n\t\tconst quadVertices = new Float32Array([-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1]);\n\n\t\tthis.buffer = this.gl.createBuffer();\n\t\tthis.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.buffer);\n\t\tthis.gl.bufferData(this.gl.ARRAY_BUFFER, quadVertices, this.gl.STATIC_DRAW);\n\t\tthis.gl.viewport(0, 0, this.canvas.width, this.canvas.height);\n\t\tthis.gl.enableVertexAttribArray(aPosition);\n\t\tthis.gl.vertexAttribPointer(aPosition, 2, this.gl.FLOAT, false, 0, 0);\n\t}\n\n\tprivate throttledHandleResize() {\n\t\tclearTimeout(this.resizeTimeout);\n\t\tconst now = performance.now();\n\t\tconst timeUntilNextResize = this.lastResizeTime + RESIZE_THROTTLE_INTERVAL - now;\n\t\tif (timeUntilNextResize <= 0) {\n\t\t\tthis.lastResizeTime = now;\n\t\t\tthis.handleResize();\n\t\t} else {\n\t\t\tthis.resizeTimeout = setTimeout(() => this.throttledHandleResize(), timeUntilNextResize);\n\t\t}\n\t}\n\n\t// TODO: This breaks for `position: fixed; inset: 0` canvases.\n\tprivate handleResize() {\n\t\tconst pixelRatio = window.devicePixelRatio || 1;\n\t\tconst width = this.canvas.clientWidth * pixelRatio;\n\t\tconst height = this.canvas.clientHeight * pixelRatio;\n\n\t\tconst computedStyle = getComputedStyle(this.canvas);\n\t\tconst hasExplicitWidth = computedStyle.width !== `${this.canvas.width}px` && computedStyle.width !== 'auto';\n\t\tconst hasExplicitHeight = computedStyle.height !== `${this.canvas.height}px` && computedStyle.height !== 'auto';\n\t\tif (!hasExplicitWidth || !hasExplicitHeight) {\n\t\t\tthis.canvas.style.width = `${this.canvas.clientWidth}px`;\n\t\t\tthis.canvas.style.height = `${this.canvas.clientHeight}px`;\n\t\t}\n\n\t\tif (this.canvas.width !== width || this.canvas.height !== height) {\n\t\t\tthis.canvas.width = width;\n\t\t\tthis.canvas.height = height;\n\t\t\tthis.gl.viewport(0, 0, this.gl.drawingBufferWidth, this.gl.drawingBufferHeight);\n\t\t\tif (this.uniforms.has('u_resolution')) {\n\t\t\t\tthis.updateUniforms({ u_resolution: [this.canvas.width, this.canvas.height] });\n\t\t\t}\n\n\t\t\t// Delete and recreate history buffer, since the canvas size won’t be correct anymore.\n\t\t\tif (this.historyLength > 0 && this.historyTexture) {\n\t\t\t\tthis.gl.deleteTexture(this.historyTexture);\n\t\t\t\tthis.initializeHistoryBuffer();\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate addEventListeners() {\n\t\tconst updateCursor = (x: number, y: number) => {\n\t\t\tif (!this.uniforms.has('u_cursor')) return;\n\t\t\tconst rect = this.canvas.getBoundingClientRect();\n\t\t\tthis.cursorPosition[0] = (x - rect.left) / rect.width;\n\t\t\tthis.cursorPosition[1] = 1 - (y - rect.top) / rect.height; // Flip Y for WebGL\n\t\t\tthis.updateUniforms({\n\t\t\t\tu_cursor: [this.cursorPosition[0], this.cursorPosition[1], this.scrollX, this.scrollY],\n\t\t\t});\n\t\t};\n\n\t\tconst updateClick = (isMouseDown: boolean, x?: number, y?: number) => {\n\t\t\tif (!this.uniforms.has('u_click')) return;\n\t\t\tthis.isMouseDown = isMouseDown;\n\t\t\tif (isMouseDown) {\n\t\t\t\tconst rect = this.canvas.getBoundingClientRect();\n\t\t\t\tconst xVal = x as number;\n\t\t\t\tconst yVal = y as number;\n\t\t\t\tthis.clickPosition[0] = (xVal - rect.left) / rect.width;\n\t\t\t\tthis.clickPosition[1] = 1 - (yVal - rect.top) / rect.height; // Flip Y for WebGL\n\t\t\t}\n\t\t\tthis.updateUniforms({\n\t\t\t\tu_click: [this.clickPosition[0], this.clickPosition[1], this.isMouseDown ? 1.0 : 0.0],\n\t\t\t});\n\t\t};\n\n\t\tthis.eventListeners.set('mousemove', event => {\n\t\t\tconst mouseEvent = event as MouseEvent;\n\t\t\tif (!this.isTouchDevice) {\n\t\t\t\tupdateCursor(mouseEvent.clientX, mouseEvent.clientY);\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('mousedown', event => {\n\t\t\tconst mouseEvent = event as MouseEvent;\n\t\t\tif (!this.isTouchDevice) {\n\t\t\t\tif (mouseEvent.button === 0) {\n\t\t\t\t\tthis.isMouseDown = true;\n\t\t\t\t\tupdateClick(true, mouseEvent.clientX, mouseEvent.clientY);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('mouseup', event => {\n\t\t\tconst mouseEvent = event as MouseEvent;\n\t\t\tif (!this.isTouchDevice) {\n\t\t\t\tif (mouseEvent.button === 0) {\n\t\t\t\t\tupdateClick(false);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('wheel', event => {\n\t\t\tconst wheelEvent = event as WheelEvent;\n\t\t\tthis.scrollX += wheelEvent.deltaX * 0.01;\n\t\t\tthis.scrollY += wheelEvent.deltaY * 0.01;\n\t\t\tupdateCursor(wheelEvent.clientX, wheelEvent.clientY);\n\t\t});\n\n\t\tthis.eventListeners.set('touchmove', event => {\n\t\t\tconst touchEvent = event as TouchEvent;\n\t\t\tif (touchEvent.touches.length > 0) {\n\t\t\t\tupdateCursor(touchEvent.touches[0].clientX, touchEvent.touches[0].clientY);\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('touchstart', event => {\n\t\t\tconst touchEvent = event as TouchEvent;\n\t\t\tthis.isTouchDevice = true;\n\t\t\tif (touchEvent.touches.length > 0) {\n\t\t\t\tupdateClick(true, touchEvent.touches[0].clientX, touchEvent.touches[0].clientY);\n\t\t\t\tupdateCursor(touchEvent.touches[0].clientX, touchEvent.touches[0].clientY);\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('touchend', event => {\n\t\t\tconst touchEvent = event as TouchEvent;\n\t\t\tif (touchEvent.touches.length === 0) {\n\t\t\t\tupdateClick(false);\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.forEach((listener, event) => {\n\t\t\tthis.canvas.addEventListener(event, listener);\n\t\t});\n\t}\n\n\tinitializeUniform(name: string, type: 'float' | 'int', value: number | number[]) {\n\t\tif (this.uniforms.has(name)) {\n\t\t\tthrow new Error(`Uniform '${name}' is already initialized.`);\n\t\t}\n\n\t\tif (type !== 'float' && type !== 'int') {\n\t\t\tthrow new Error(`Invalid uniform type: ${type}. Expected 'float' or 'int'.`);\n\t\t}\n\n\t\tconst location = this.gl.getUniformLocation(this.program!, name);\n\t\tif (!location) {\n\t\t\tconsole.debug(`Uniform ${name} not found in fragment shader. Skipping initialization.`);\n\t\t\treturn;\n\t\t}\n\n\t\tif (!Array.isArray(value)) {\n\t\t\tvalue = [value];\n\t\t}\n\t\tif (value.length < 1 || value.length > 4) {\n\t\t\tthrow new Error(`Invalid uniform value length: ${value.length}. Expected a length between 1 and 4.`);\n\t\t}\n\n\t\tconst length = value.length as 1 | 2 | 3 | 4;\n\t\tthis.uniforms.set(name, { type, length, location });\n\t\tthis.updateUniforms({ [name]: value });\n\t}\n\n\tupdateUniforms(updates: Record<string, number | number[]>) {\n\t\tObject.entries(updates).forEach(([name, value]: [string, number | number[]]) => {\n\t\t\tif (!this.uniforms.has(name)) {\n\t\t\t\tthrow new Error(`Uniform '${name}' is not initialized.`);\n\t\t\t}\n\n\t\t\tconst uniform = this.uniforms.get(name)!;\n\t\t\tif (!Array.isArray(value)) {\n\t\t\t\tvalue = [value];\n\t\t\t}\n\t\t\tif (value.length !== uniform.length) {\n\t\t\t\tthrow new Error(`Invalid uniform value length: ${value.length}. Expected ${uniform.length}.`);\n\t\t\t}\n\t\t\t(this.gl as any)[`uniform${uniform.length}${uniform.type.charAt(0)}`](uniform.location, ...value);\n\t\t});\n\t}\n\n\tstep(time: number) {\n\t\tconst gl = this.gl;\n\n\t\tif (this.uniforms.has('u_time')) {\n\t\t\tthis.updateUniforms({ u_time: time });\n\t\t}\n\t\tif (this.uniforms.has('u_frame')) {\n\t\t\tthis.updateUniforms({ u_frame: this.frame });\n\t\t}\n\n\t\tif (this.historyLength > 0) {\n\t\t\tconst writeIdx = this.frame % this.historyLength;\n\n\t\t\tgl.bindFramebuffer(gl.FRAMEBUFFER, null);\n\t\t\tgl.viewport(0, 0, this.canvas.width, this.canvas.height);\n\t\t\tgl.clear(gl.COLOR_BUFFER_BIT);\n\t\t\tgl.drawArrays(gl.TRIANGLES, 0, 6);\n\n\t\t\tgl.activeTexture(gl.TEXTURE0);\n\t\t\tgl.bindTexture(gl.TEXTURE_2D_ARRAY, this.historyTexture);\n\t\t\tgl.copyTexSubImage3D(gl.TEXTURE_2D_ARRAY, 0, 0, 0, writeIdx, 0, 0, this.canvas.width, this.canvas.height);\n\t\t} else {\n\t\t\tgl.clear(gl.COLOR_BUFFER_BIT);\n\t\t\tgl.drawArrays(gl.TRIANGLES, 0, 6);\n\t\t}\n\n\t\t++this.frame;\n\t}\n\n\tplay(callback?: (time: number, frame: number) => void) {\n\t\tconst loop = (time: number) => {\n\t\t\ttime /= 1000; // Convert from milliseconds to seconds.\n\t\t\tthis.step(time);\n\t\t\tif (callback) callback(time, this.frame);\n\t\t\tthis.animationFrameId = requestAnimationFrame(loop);\n\t\t};\n\t\tthis.animationFrameId = requestAnimationFrame(loop);\n\t}\n\n\tpause() {\n\t\tif (this.animationFrameId) {\n\t\t\tcancelAnimationFrame(this.animationFrameId);\n\t\t\tthis.animationFrameId = null;\n\t\t}\n\t}\n\n\tsave(filename: string) {\n\t\tthis.gl.clear(this.gl.COLOR_BUFFER_BIT);\n\t\tthis.gl.drawArrays(this.gl.TRIANGLES, 0, 6);\n\n\t\tconst image = this.canvas.toDataURL();\n\t\tif (filename && !`${filename}`.toLowerCase().endsWith('.png')) {\n\t\t\tfilename = `${filename}.png`;\n\t\t}\n\t\tthis.downloadLink.download = filename || 'export.png';\n\t\tthis.downloadLink.href = image;\n\t\tthis.downloadLink.click();\n\t}\n\n\tinitializeTexture(name: string, source: HTMLImageElement | HTMLVideoElement) {\n\t\tif (this.textures.has(name)) {\n\t\t\tthrow new Error(`Texture '${name}' is already initialized.`);\n\t\t}\n\n\t\tconst texture = this.gl.createTexture();\n\t\tif (!texture) {\n\t\t\tthrow new Error('Failed to create texture');\n\t\t}\n\t\tconst unitIndex = this.textures.size + 1; // Start from unit 1 to avoid conflict with history texture at unit 0.\n\t\tthis.gl.activeTexture(this.gl.TEXTURE0 + unitIndex);\n\t\tthis.gl.bindTexture(this.gl.TEXTURE_2D, texture);\n\n\t\t// Flip the texture vertically since v_uv is flipped, and set up filters and wrapping.\n\t\tthis.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, true);\n\t\tthis.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, this.gl.CLAMP_TO_EDGE);\n\t\tthis.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, this.gl.CLAMP_TO_EDGE);\n\t\tthis.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR);\n\t\tthis.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR);\n\n\t\tthis.textures.set(name, { texture, unitIndex });\n\t\tthis.updateTextures({ [name]: source });\n\n\t\tconst uSampler = this.gl.getUniformLocation(this.program!, name);\n\t\tif (uSampler) {\n\t\t\tthis.gl.uniform1i(uSampler, unitIndex);\n\t\t}\n\t}\n\n\tupdateTextures(updates: Record<string, HTMLImageElement | HTMLVideoElement>) {\n\t\tObject.entries(updates).forEach(([name, source]) => {\n\t\t\tconst info = this.textures.get(name);\n\t\t\tif (!info) {\n\t\t\t\tthrow new Error(`Texture '${name}' is not initialized.`);\n\t\t\t}\n\t\t\tthis.gl.activeTexture(this.gl.TEXTURE0 + info.unitIndex);\n\t\t\tthis.gl.bindTexture(this.gl.TEXTURE_2D, info.texture);\n\t\t\tthis.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, source);\n\t\t});\n\t}\n\n\tdestroy() {\n\t\tif (this.animationFrameId) {\n\t\t\tcancelAnimationFrame(this.animationFrameId);\n\t\t\tthis.animationFrameId = null;\n\t\t}\n\n\t\tthis.resizeObserver.unobserve(this.canvas);\n\t\tthis.eventListeners.forEach((listener, event) => {\n\t\t\tthis.canvas.removeEventListener(event, listener);\n\t\t});\n\n\t\tif (this.program) {\n\t\t\tthis.gl.deleteProgram(this.program);\n\t\t}\n\n\t\tthis.textures.forEach(texture => {\n\t\t\tthis.gl.deleteTexture(texture.texture);\n\t\t});\n\n\t\tif (this.historyTexture) {\n\t\t\tthis.gl.deleteTexture(this.historyTexture);\n\t\t\tthis.historyTexture = null;\n\t\t}\n\n\t\tif (this.buffer) {\n\t\t\tthis.gl.deleteBuffer(this.buffer);\n\t\t\tthis.buffer = null;\n\t\t}\n\n\t\tif (this.isInternalCanvas) {\n\t\t\tthis.canvas.remove();\n\t\t}\n\t}\n}\n\nexport default ShaderPad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AAG,cAAc,KAAK,GAAG,SAAWe,EAAK,SAAS,EACvD,KAAK,GAAG,YAAY,KAAK,GAAG,WAAYA,EAAK,OAAO,EACpD,KAAK,GAAG,WAAW,KAAK,GAAG,WAAY,EAAG,KAAK,GAAG,KAAM,KAAK,GAAG,KAAM,KAAK,GAAG,cAAevC,CAAM,CACpG,CAAC,CACF,CAEA,SAAU,CACL,KAAK,mBACR,qBAAqB,KAAK,gBAAgB,EAC1C,KAAK,iBAAmB,MAGzB,KAAK,eAAe,UAAU,KAAK,MAAM,EACzC,KAAK,eAAe,QAAQ,CAACuB,EAAUJ,IAAU,CAChD,KAAK,OAAO,oBAAoBA,EAAOI,CAAQ,CAChD,CAAC,EAEG,KAAK,SACR,KAAK,GAAG,cAAc,KAAK,OAAO,EAGnC,KAAK,SAAS,QAAQa,GAAW,CAChC,KAAK,GAAG,cAAcA,EAAQ,OAAO,CACtC,CAAC,EAEG,KAAK,iBACR,KAAK,GAAG,cAAc,KAAK,cAAc,EACzC,KAAK,eAAiB,MAGnB,KAAK,SACR,KAAK,GAAG,aAAa,KAAK,MAAM,EAChC,KAAK,OAAS,MAGX,KAAK,kBACR,KAAK,OAAO,OAAO,CAErB,CACD,EAEOnD,EAAQI","names":["index_exports","__export","index_default","__toCommonJS","DEFAULT_VERTEX_SHADER_SRC","RESIZE_THROTTLE_INTERVAL","ShaderPad","fragmentShaderSrc","options","vertexShaderSrc","vertexShader","fragmentShader","aPosition","gl","transparent","layer","type","source","shader","quadVertices","now","timeUntilNextResize","pixelRatio","width","height","computedStyle","hasExplicitWidth","hasExplicitHeight","updateCursor","x","y","rect","updateClick","isMouseDown","xVal","yVal","event","mouseEvent","wheelEvent","touchEvent","listener","name","value","location","length","updates","uniform","time","writeIdx","callback","loop","filename","image","texture","unitIndex","uSampler","info"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/plugins/history.ts","../src/plugins/save.ts"],"sourcesContent":["const DEFAULT_VERTEX_SHADER_SRC = `#version 300 es\nin vec2 aPosition;\nout vec2 v_uv;\nvoid main() {\n v_uv = aPosition * 0.5 + 0.5;\n gl_Position = vec4(aPosition, 0.0, 1.0);\n}\n`;\nconst RESIZE_THROTTLE_INTERVAL = 1000 / 30;\n\ninterface Uniform {\n\ttype: 'float' | 'int';\n\tlength: 1 | 2 | 3 | 4;\n\tlocation: WebGLUniformLocation;\n}\n\ninterface Texture {\n\ttexture: WebGLTexture;\n\tunitIndex: number;\n\thistoryDepth?: number;\n\twriteIndex?: number;\n}\n\nexport type TextureSource = HTMLImageElement | HTMLVideoElement;\n\nexport interface PluginContext {\n\tgl: WebGL2RenderingContext;\n\tuniforms: Map<string, Uniform>;\n\ttextures: Map<string, Texture>;\n\tget program(): WebGLProgram | null;\n\tcanvas: HTMLCanvasElement;\n\treserveTextureUnit: (name: string) => number;\n\treleaseTextureUnit: (name: string) => void;\n}\n\ntype Plugin = (shaderPad: ShaderPad, context: PluginContext) => void;\n\ntype LifecycleMethod = 'init' | 'step' | 'destroy' | 'updateResolution' | 'reset';\n\ninterface Options {\n\tcanvas?: HTMLCanvasElement | null;\n\tplugins?: Plugin[];\n}\n\ntype TextureUnitPool = {\n\tfree: number[];\n\tnext: number;\n\tmax: number;\n};\n\nclass ShaderPad {\n\tprivate isInternalCanvas = false;\n\tprivate isTouchDevice = false;\n\tprivate gl: WebGL2RenderingContext;\n\tprivate downloadLink: HTMLAnchorElement;\n\tprivate fragmentShaderSrc: string;\n\tprivate uniforms: Map<string, Uniform> = new Map();\n\tprivate textures: Map<string, Texture> = new Map();\n\tprivate textureUnitPool: TextureUnitPool;\n\tprivate buffer: WebGLBuffer | null = null;\n\tprivate program: WebGLProgram | null = null;\n\tprivate animationFrameId: number | null;\n\tprivate resolutionObserver: MutationObserver;\n\tprivate resizeObserver: ResizeObserver;\n\tprivate resizeTimeout: NodeJS.Timeout = null as unknown as NodeJS.Timeout;\n\tprivate lastResizeTime = 0;\n\tprivate eventListeners: Map<string, EventListener> = new Map();\n\tprivate frame = 0;\n\tprivate startTime = 0;\n\tprivate cursorPosition = [0.5, 0.5];\n\tprivate clickPosition = [0.5, 0.5];\n\tprivate isMouseDown = false;\n\tpublic canvas: HTMLCanvasElement;\n\tpublic onResize?: (width: number, height: number) => void;\n\tprivate hooks: Map<LifecycleMethod, Function[]> = new Map();\n\n\tconstructor(fragmentShaderSrc: string, options: Options = {}) {\n\t\tthis.canvas = options.canvas || document.createElement('canvas');\n\t\tif (!options.canvas) {\n\t\t\tthis.isInternalCanvas = true;\n\t\t\tdocument.body.appendChild(this.canvas);\n\t\t\tthis.canvas.style.position = 'fixed';\n\t\t\tthis.canvas.style.inset = '0';\n\t\t\tthis.canvas.style.height = '100dvh';\n\t\t\tthis.canvas.style.width = '100dvw';\n\t\t}\n\n\t\tthis.gl = this.canvas.getContext('webgl2', { antialias: false }) as WebGL2RenderingContext;\n\t\tif (!this.gl) {\n\t\t\tthrow new Error('WebGL2 not supported. Please use a browser that supports WebGL2.');\n\t\t}\n\n\t\tthis.textureUnitPool = {\n\t\t\tfree: [],\n\t\t\tnext: 1, // Start from unit 1 to avoid conflict with history texture at unit 0.\n\t\t\tmax: this.gl.getParameter(this.gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS),\n\t\t};\n\n\t\tthis.downloadLink = document.createElement('a');\n\t\tthis.fragmentShaderSrc = fragmentShaderSrc;\n\t\tthis.animationFrameId = null;\n\t\tthis.resolutionObserver = new MutationObserver(() => this.updateResolution());\n\t\tthis.resizeObserver = new ResizeObserver(() => this.throttledHandleResize());\n\n\t\tif (options.plugins) {\n\t\t\tconst context: PluginContext = {\n\t\t\t\tgl: this.gl,\n\t\t\t\tuniforms: this.uniforms,\n\t\t\t\ttextures: this.textures,\n\t\t\t\tcanvas: this.canvas,\n\t\t\t\treserveTextureUnit: this.reserveTextureUnit.bind(this),\n\t\t\t\treleaseTextureUnit: this.releaseTextureUnit.bind(this),\n\t\t\t} as PluginContext;\n\t\t\t// Define program as a getter so it always returns the current program.\n\t\t\tObject.defineProperty(context, 'program', {\n\t\t\t\tget: () => this.program,\n\t\t\t\tenumerable: true,\n\t\t\t\tconfigurable: true,\n\t\t\t});\n\t\t\toptions.plugins.forEach(plugin => plugin(this, context));\n\t\t}\n\t\tthis.init();\n\t\tthis.addEventListeners();\n\t}\n\n\tregisterHook(name: LifecycleMethod, fn: Function) {\n\t\tif (!this.hooks.has(name)) {\n\t\t\tthis.hooks.set(name, []);\n\t\t}\n\t\tthis.hooks.get(name)!.push(fn);\n\t}\n\n\tprivate init() {\n\t\tconst vertexShaderSrc = DEFAULT_VERTEX_SHADER_SRC;\n\n\t\tthis.program = this.gl.createProgram();\n\t\tif (!this.program) {\n\t\t\tthrow new Error('Failed to create WebGL program');\n\t\t}\n\t\tconst vertexShader = this.createShader(this.gl.VERTEX_SHADER, vertexShaderSrc);\n\t\tconst fragmentShader = this.createShader(this.gl.FRAGMENT_SHADER, this.fragmentShaderSrc);\n\n\t\tthis.gl.attachShader(this.program, vertexShader);\n\t\tthis.gl.attachShader(this.program, fragmentShader);\n\t\tthis.gl.linkProgram(this.program);\n\t\tthis.gl.deleteShader(vertexShader);\n\t\tthis.gl.deleteShader(fragmentShader);\n\n\t\tif (!this.gl.getProgramParameter(this.program, this.gl.LINK_STATUS)) {\n\t\t\tconsole.error('Program link error:', this.gl.getProgramInfoLog(this.program));\n\t\t\tthis.gl.deleteProgram(this.program);\n\t\t\tthrow new Error('Failed to link WebGL program');\n\t\t}\n\n\t\tconst aPosition = this.gl.getAttribLocation(this.program, 'aPosition');\n\t\tthis.setupBuffer(aPosition);\n\n\t\tthis.gl.useProgram(this.program);\n\n\t\tthis.resolutionObserver.observe(this.canvas, { attributes: true, attributeFilter: ['width', 'height'] });\n\t\tthis.resizeObserver.observe(this.canvas);\n\n\t\tif (!this.isInternalCanvas) {\n\t\t\tthis.updateResolution();\n\t\t}\n\t\tthis.initializeUniform('u_cursor', 'float', this.cursorPosition);\n\t\tthis.initializeUniform('u_click', 'float', [...this.clickPosition, this.isMouseDown ? 1.0 : 0.0]);\n\t\tthis.initializeUniform('u_time', 'float', 0);\n\t\tthis.initializeUniform('u_frame', 'int', 0);\n\n\t\tthis.hooks.get('init')?.forEach(hook => hook.call(this));\n\t}\n\n\tprivate createShader(type: number, source: string): WebGLShader {\n\t\tconst shader = this.gl.createShader(type)!;\n\t\tthis.gl.shaderSource(shader, source);\n\t\tthis.gl.compileShader(shader);\n\t\tif (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)) {\n\t\t\tconsole.error('Shader compilation failed:', source);\n\t\t\tconsole.error(this.gl.getShaderInfoLog(shader));\n\t\t\tthis.gl.deleteShader(shader);\n\t\t\tthrow new Error('Shader compilation failed');\n\t\t}\n\t\treturn shader;\n\t}\n\n\tprivate setupBuffer(aPosition: number) {\n\t\tconst quadVertices = new Float32Array([-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1]);\n\n\t\tthis.buffer = this.gl.createBuffer();\n\t\tthis.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.buffer);\n\t\tthis.gl.bufferData(this.gl.ARRAY_BUFFER, quadVertices, this.gl.STATIC_DRAW);\n\t\tthis.gl.viewport(0, 0, this.gl.drawingBufferWidth, this.gl.drawingBufferHeight);\n\t\tthis.gl.enableVertexAttribArray(aPosition);\n\t\tthis.gl.vertexAttribPointer(aPosition, 2, this.gl.FLOAT, false, 0, 0);\n\t}\n\n\tprivate throttledHandleResize() {\n\t\tclearTimeout(this.resizeTimeout);\n\t\tconst now = performance.now();\n\t\tconst timeUntilNextResize = this.lastResizeTime + RESIZE_THROTTLE_INTERVAL - now;\n\t\tif (timeUntilNextResize <= 0) {\n\t\t\tthis.lastResizeTime = now;\n\t\t\tthis.handleResize();\n\t\t} else {\n\t\t\tthis.resizeTimeout = setTimeout(() => this.throttledHandleResize(), timeUntilNextResize);\n\t\t}\n\t}\n\n\tprivate handleResize() {\n\t\tconst pixelRatio = window.devicePixelRatio || 1;\n\t\tconst width = this.canvas.clientWidth * pixelRatio;\n\t\tconst height = this.canvas.clientHeight * pixelRatio;\n\t\tif (this.isInternalCanvas && (this.canvas.width !== width || this.canvas.height !== height)) {\n\t\t\tthis.canvas.width = width;\n\t\t\tthis.canvas.height = height;\n\t\t}\n\t\tthis.onResize?.(width, height);\n\t}\n\n\tprivate addEventListeners() {\n\t\tconst updateCursor = (x: number, y: number) => {\n\t\t\tif (!this.uniforms.has('u_cursor')) return;\n\t\t\tconst rect = this.canvas.getBoundingClientRect();\n\t\t\tthis.cursorPosition[0] = (x - rect.left) / rect.width;\n\t\t\tthis.cursorPosition[1] = 1 - (y - rect.top) / rect.height; // Flip Y for WebGL\n\t\t\tthis.updateUniforms({ u_cursor: this.cursorPosition });\n\t\t};\n\n\t\tconst updateClick = (isMouseDown: boolean, x?: number, y?: number) => {\n\t\t\tif (!this.uniforms.has('u_click')) return;\n\t\t\tthis.isMouseDown = isMouseDown;\n\t\t\tif (isMouseDown) {\n\t\t\t\tconst rect = this.canvas.getBoundingClientRect();\n\t\t\t\tconst xVal = x as number;\n\t\t\t\tconst yVal = y as number;\n\t\t\t\tthis.clickPosition[0] = (xVal - rect.left) / rect.width;\n\t\t\t\tthis.clickPosition[1] = 1 - (yVal - rect.top) / rect.height; // Flip Y for WebGL\n\t\t\t}\n\t\t\tthis.updateUniforms({ u_click: [...this.clickPosition, this.isMouseDown ? 1.0 : 0.0] });\n\t\t};\n\n\t\tthis.eventListeners.set('mousemove', event => {\n\t\t\tconst mouseEvent = event as MouseEvent;\n\t\t\tif (!this.isTouchDevice) {\n\t\t\t\tupdateCursor(mouseEvent.clientX, mouseEvent.clientY);\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('mousedown', event => {\n\t\t\tconst mouseEvent = event as MouseEvent;\n\t\t\tif (!this.isTouchDevice) {\n\t\t\t\tif (mouseEvent.button === 0) {\n\t\t\t\t\tthis.isMouseDown = true;\n\t\t\t\t\tupdateClick(true, mouseEvent.clientX, mouseEvent.clientY);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('mouseup', event => {\n\t\t\tconst mouseEvent = event as MouseEvent;\n\t\t\tif (!this.isTouchDevice) {\n\t\t\t\tif (mouseEvent.button === 0) {\n\t\t\t\t\tupdateClick(false);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('touchmove', event => {\n\t\t\tconst touchEvent = event as TouchEvent;\n\t\t\tif (touchEvent.touches.length > 0) {\n\t\t\t\tupdateCursor(touchEvent.touches[0].clientX, touchEvent.touches[0].clientY);\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('touchstart', event => {\n\t\t\tconst touchEvent = event as TouchEvent;\n\t\t\tthis.isTouchDevice = true;\n\t\t\tif (touchEvent.touches.length > 0) {\n\t\t\t\tupdateCursor(touchEvent.touches[0].clientX, touchEvent.touches[0].clientY);\n\t\t\t\tupdateClick(true, touchEvent.touches[0].clientX, touchEvent.touches[0].clientY);\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('touchend', event => {\n\t\t\tconst touchEvent = event as TouchEvent;\n\t\t\tif (touchEvent.touches.length === 0) {\n\t\t\t\tupdateClick(false);\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.forEach((listener, event) => {\n\t\t\tthis.canvas.addEventListener(event, listener);\n\t\t});\n\t}\n\n\tprivate updateResolution() {\n\t\tconst width = this.gl.drawingBufferWidth;\n\t\tconst height = this.gl.drawingBufferHeight;\n\t\tthis.gl.viewport(0, 0, width, height);\n\t\tif (this.uniforms.has('u_resolution')) {\n\t\t\tthis.updateUniforms({ u_resolution: [width, height] });\n\t\t} else {\n\t\t\tthis.initializeUniform('u_resolution', 'float', [width, height]);\n\t\t}\n\n\t\tthis.hooks.get('updateResolution')?.forEach(hook => hook.call(this));\n\t}\n\n\tinitializeUniform(name: string, type: 'float' | 'int', value: number | number[]) {\n\t\tif (this.uniforms.has(name)) {\n\t\t\tthrow new Error(`Uniform '${name}' is already initialized.`);\n\t\t}\n\n\t\tif (type !== 'float' && type !== 'int') {\n\t\t\tthrow new Error(`Invalid uniform type: ${type}. Expected 'float' or 'int'.`);\n\t\t}\n\n\t\tconst location = this.gl.getUniformLocation(this.program!, name);\n\t\tif (!location) {\n\t\t\tconsole.debug(`Uniform ${name} not found in fragment shader. Skipping initialization.`);\n\t\t\treturn;\n\t\t}\n\n\t\tif (!Array.isArray(value)) {\n\t\t\tvalue = [value];\n\t\t}\n\t\tif (value.length < 1 || value.length > 4) {\n\t\t\tthrow new Error(`Invalid uniform value length: ${value.length}. Expected a length between 1 and 4.`);\n\t\t}\n\n\t\tconst length = value.length as 1 | 2 | 3 | 4;\n\t\tthis.uniforms.set(name, { type, length, location });\n\t\tthis.updateUniforms({ [name]: value });\n\t}\n\n\tupdateUniforms(updates: Record<string, number | number[]>) {\n\t\tObject.entries(updates).forEach(([name, value]: [string, number | number[]]) => {\n\t\t\tif (!this.uniforms.has(name)) {\n\t\t\t\tthrow new Error(`Uniform '${name}' is not initialized.`);\n\t\t\t}\n\n\t\t\tconst uniform = this.uniforms.get(name)!;\n\t\t\tif (!Array.isArray(value)) {\n\t\t\t\tvalue = [value];\n\t\t\t}\n\t\t\tif (value.length !== uniform.length) {\n\t\t\t\tthrow new Error(`Invalid uniform value length: ${value.length}. Expected ${uniform.length}.`);\n\t\t\t}\n\t\t\t(this.gl as any)[`uniform${uniform.length}${uniform.type.charAt(0)}`](uniform.location, ...value);\n\t\t});\n\t}\n\n\tstep(time: number) {\n\t\tconst gl = this.gl;\n\n\t\tif (this.uniforms.has('u_time')) {\n\t\t\tthis.updateUniforms({ u_time: time });\n\t\t}\n\t\tif (this.uniforms.has('u_frame')) {\n\t\t\tthis.updateUniforms({ u_frame: this.frame });\n\t\t}\n\n\t\tgl.clear(gl.COLOR_BUFFER_BIT);\n\t\tgl.drawArrays(gl.TRIANGLES, 0, 6);\n\n\t\tthis.hooks.get('step')?.forEach(hook => hook.call(this, time, this.frame));\n\n\t\t++this.frame;\n\t}\n\n\tplay(callback?: (time: number, frame: number) => void) {\n\t\tthis.pause(); // Prevent double play.\n\t\tconst loop = (time: number) => {\n\t\t\ttime = (time - this.startTime) / 1000; // Convert from milliseconds to seconds.\n\t\t\tthis.step(time);\n\t\t\tthis.animationFrameId = requestAnimationFrame(loop);\n\t\t\tif (callback) callback(time, this.frame);\n\t\t};\n\t\tthis.animationFrameId = requestAnimationFrame(loop);\n\t}\n\n\tpause() {\n\t\tif (this.animationFrameId) {\n\t\t\tcancelAnimationFrame(this.animationFrameId);\n\t\t\tthis.animationFrameId = null;\n\t\t}\n\t}\n\n\treset() {\n\t\tthis.frame = 0;\n\t\tthis.startTime = performance.now();\n\t\tthis.hooks.get('reset')?.forEach(hook => hook.call(this));\n\t}\n\n\tinitializeTexture(name: string, source: HTMLImageElement | HTMLVideoElement) {\n\t\tif (this.textures.has(name)) {\n\t\t\tthrow new Error(`Texture '${name}' is already initialized.`);\n\t\t}\n\n\t\tconst texture = this.gl.createTexture();\n\t\tif (!texture) {\n\t\t\tthrow new Error('Failed to create texture');\n\t\t}\n\t\tlet unitIndex: number;\n\t\ttry {\n\t\t\tunitIndex = this.reserveTextureUnit(name);\n\t\t} catch (error) {\n\t\t\tthis.gl.deleteTexture(texture);\n\t\t\tthrow error;\n\t\t}\n\t\tthis.gl.activeTexture(this.gl.TEXTURE0 + unitIndex);\n\t\tthis.gl.bindTexture(this.gl.TEXTURE_2D, texture);\n\n\t\t// Flip the texture vertically during upload to match WebGL's coordinate system.\n\t\t// WebGL uses bottom-left origin, while images/videos use top-left origin.\n\t\tthis.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, true);\n\t\tthis.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, this.gl.CLAMP_TO_EDGE);\n\t\tthis.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, this.gl.CLAMP_TO_EDGE);\n\t\tthis.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR);\n\t\tthis.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR);\n\n\t\tthis.textures.set(name, { texture, unitIndex });\n\t\tthis.updateTextures({ [name]: source });\n\n\t\tconst uSampler = this.gl.getUniformLocation(this.program!, name);\n\t\tif (uSampler) {\n\t\t\tthis.gl.uniform1i(uSampler, unitIndex);\n\t\t}\n\t}\n\n\tupdateTextures(updates: Record<string, HTMLImageElement | HTMLVideoElement>) {\n\t\tObject.entries(updates).forEach(([name, source]) => {\n\t\t\tconst info = this.textures.get(name);\n\t\t\tif (!info) {\n\t\t\t\tthrow new Error(`Texture '${name}' is not initialized.`);\n\t\t\t}\n\t\t\tthis.gl.activeTexture(this.gl.TEXTURE0 + info.unitIndex);\n\t\t\tthis.gl.bindTexture(this.gl.TEXTURE_2D, info.texture);\n\t\t\tthis.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, source);\n\t\t});\n\t}\n\n\tasync save(filename: string) {\n\t\tthis.gl.clear(this.gl.COLOR_BUFFER_BIT);\n\t\tthis.gl.drawArrays(this.gl.TRIANGLES, 0, 6);\n\n\t\tif (filename && !`${filename}`.toLowerCase().endsWith('.png')) {\n\t\t\tfilename = `${filename}.png`;\n\t\t}\n\t\tfilename = filename || 'export.png';\n\t\tif ('ongesturechange' in window) {\n\t\t\t// Mobile.\n\t\t\ttry {\n\t\t\t\tconst blob: Blob = await new Promise(resolve =>\n\t\t\t\t\tthis.canvas.toBlob(resolve as BlobCallback, 'image/png')\n\t\t\t\t);\n\t\t\t\tconst file = new File([blob], filename, { type: blob.type });\n\n\t\t\t\tif (navigator.canShare?.({ files: [file] })) {\n\t\t\t\t\tawait navigator.share({ files: [file] });\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tconsole.warn('Web Share API failed:', error);\n\t\t\t}\n\t\t} else {\n\t\t\t// Desktop.\n\t\t\tthis.downloadLink.download = filename;\n\t\t\tthis.downloadLink.href = this.canvas.toDataURL();\n\t\t\tthis.downloadLink.click();\n\t\t}\n\t}\n\n\tdestroy() {\n\t\tif (this.animationFrameId) {\n\t\t\tcancelAnimationFrame(this.animationFrameId);\n\t\t\tthis.animationFrameId = null;\n\t\t}\n\n\t\tthis.resolutionObserver.disconnect();\n\t\tthis.resizeObserver.disconnect();\n\t\tthis.eventListeners.forEach((listener, event) => {\n\t\t\tthis.canvas.removeEventListener(event, listener);\n\t\t});\n\n\t\tif (this.program) {\n\t\t\tthis.gl.deleteProgram(this.program);\n\t\t}\n\n\t\tthis.textures.forEach(texture => {\n\t\t\tthis.gl.deleteTexture(texture.texture);\n\t\t});\n\t\tthis.textureUnitPool.free = [];\n\t\tthis.textureUnitPool.next = 1;\n\n\t\tif (this.buffer) {\n\t\t\tthis.gl.deleteBuffer(this.buffer);\n\t\t\tthis.buffer = null;\n\t\t}\n\n\t\tthis.hooks.get('destroy')?.forEach(hook => hook.call(this));\n\n\t\tif (this.isInternalCanvas) {\n\t\t\tthis.canvas.remove();\n\t\t}\n\t}\n\n\t// TODO: Review these two.\n\tprivate reserveTextureUnit(name: string) {\n\t\tif (!name) {\n\t\t\tthrow new Error('Texture unit name must be provided.');\n\t\t}\n\t\tconst existing = this.textures.get(name);\n\t\tif (existing) {\n\t\t\treturn existing.unitIndex;\n\t\t}\n\t\tlet unit: number;\n\t\tif (this.textureUnitPool.free.length > 0) {\n\t\t\tunit = this.textureUnitPool.free.pop()!;\n\t\t} else {\n\t\t\tunit = this.textureUnitPool.next;\n\t\t\tif (unit >= this.textureUnitPool.max) {\n\t\t\t\tthrow new Error('Exceeded the available texture units for this device.');\n\t\t\t}\n\t\t\tthis.textureUnitPool.next += 1;\n\t\t}\n\t\treturn unit;\n\t}\n\n\tprivate releaseTextureUnit(name: string) {\n\t\tconst existing = this.textures.get(name);\n\t\tif (!existing) {\n\t\t\treturn;\n\t\t}\n\t\tconst unit = existing.unitIndex;\n\t\tif (unit > 0) {\n\t\t\tthis.textureUnitPool.free.push(unit);\n\t\t}\n\t}\n}\n\nexport * from './plugins';\nexport default ShaderPad;\n","import ShaderPad, { PluginContext, TextureSource } from '../index';\n\nexport type WithHistory<T extends ShaderPad> = T & {\n\tinitializeTexture(name: string, source: HTMLImageElement | HTMLVideoElement, historyDepth?: number): void;\n};\n\nfunction getSourceDimensions(source: TextureSource) {\n\tconst video = source as HTMLVideoElement;\n\tconst image = source as HTMLImageElement;\n\tconst width = video.videoWidth || image.naturalWidth || image.width;\n\tconst height = video.videoHeight || image.naturalHeight || image.height;\n\treturn { width, height };\n}\n\nfunction clearHistoryTextureLayers(gl: WebGL2RenderingContext, width: number, height: number, depth: number): void {\n\tconst transparent = new Uint8Array(width * height * 4);\n\tfor (let layer = 0; layer < depth; ++layer) {\n\t\tgl.texSubImage3D(gl.TEXTURE_2D_ARRAY, 0, 0, 0, layer, width, height, 1, gl.RGBA, gl.UNSIGNED_BYTE, transparent);\n\t}\n}\n\nfunction createHistoryTexture(gl: WebGL2RenderingContext, depth: number, width: number, height: number): WebGLTexture {\n\tconst texture = gl.createTexture();\n\tif (!texture) {\n\t\tthrow new Error('Failed to create history texture');\n\t}\n\tgl.activeTexture(gl.TEXTURE0);\n\tgl.bindTexture(gl.TEXTURE_2D_ARRAY, texture);\n\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.LINEAR);\n\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MAG_FILTER, gl.LINEAR);\n\tgl.texStorage3D(gl.TEXTURE_2D_ARRAY, 1, gl.RGBA8, width, height, depth);\n\tclearHistoryTextureLayers(gl, width, height, depth);\n\treturn texture;\n}\n\nexport function history(framebufferDepth: number = 1) {\n\treturn function (shaderPad: ShaderPad, context: PluginContext) {\n\t\tconst { gl, uniforms, textures, reserveTextureUnit } = context;\n\t\tconst shaderPadWithHistory = shaderPad as WithHistory<ShaderPad>;\n\n\t\tconst originalInitializeTexture = shaderPad.initializeTexture.bind(shaderPad);\n\t\tconst originalUpdateTextures = shaderPad.updateTextures.bind(shaderPad);\n\n\t\tlet framebufferHistoryTexture: WebGLTexture | null = null;\n\t\tconst shouldTrackFramebuffer = framebufferDepth > 1;\n\t\tif (shouldTrackFramebuffer) {\n\t\t\tfunction initializeFramebufferHistory() {\n\t\t\t\tframebufferHistoryTexture = createHistoryTexture(\n\t\t\t\t\tgl,\n\t\t\t\t\tframebufferDepth,\n\t\t\t\t\tgl.drawingBufferWidth,\n\t\t\t\t\tgl.drawingBufferHeight\n\t\t\t\t);\n\n\t\t\t\tif (!uniforms.has('u_history')) {\n\t\t\t\t\tshaderPad.initializeUniform('u_history', 'int', 0);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction clearFramebufferHistory() {\n\t\t\t\tif (!framebufferHistoryTexture) return;\n\t\t\t\tgl.activeTexture(gl.TEXTURE0);\n\t\t\t\tgl.bindTexture(gl.TEXTURE_2D_ARRAY, framebufferHistoryTexture);\n\t\t\t\tclearHistoryTextureLayers(gl, gl.drawingBufferWidth, gl.drawingBufferHeight, framebufferDepth);\n\t\t\t}\n\n\t\t\tshaderPad.registerHook('init', initializeFramebufferHistory);\n\t\t\tshaderPad.registerHook('step', (_time: number, frame: number) => {\n\t\t\t\tif (!framebufferHistoryTexture) return;\n\t\t\t\tconst writeIdx = frame % framebufferDepth;\n\t\t\t\tgl.activeTexture(gl.TEXTURE0);\n\t\t\t\tgl.bindTexture(gl.TEXTURE_2D_ARRAY, framebufferHistoryTexture);\n\t\t\t\tgl.copyTexSubImage3D(\n\t\t\t\t\tgl.TEXTURE_2D_ARRAY,\n\t\t\t\t\t0,\n\t\t\t\t\t0,\n\t\t\t\t\t0,\n\t\t\t\t\twriteIdx,\n\t\t\t\t\t0,\n\t\t\t\t\t0,\n\t\t\t\t\tgl.drawingBufferWidth,\n\t\t\t\t\tgl.drawingBufferHeight\n\t\t\t\t);\n\t\t\t});\n\t\t\tshaderPad.registerHook('updateResolution', () => {\n\t\t\t\tif (framebufferHistoryTexture) {\n\t\t\t\t\tgl.deleteTexture(framebufferHistoryTexture);\n\t\t\t\t}\n\t\t\t\tinitializeFramebufferHistory();\n\t\t\t});\n\t\t\tshaderPad.registerHook('reset', clearFramebufferHistory);\n\t\t}\n\t\tshaderPad.registerHook('reset', () => {\n\t\t\ttextures.forEach(textureInfo => {\n\t\t\t\tif (textureInfo.historyDepth && textureInfo.historyDepth > 1 && textureInfo.writeIndex !== undefined) {\n\t\t\t\t\ttextureInfo.writeIndex = 0;\n\t\t\t\t}\n\t\t\t});\n\t\t});\n\n\t\tshaderPadWithHistory.initializeTexture = function (\n\t\t\tname: string,\n\t\t\tsource: TextureSource,\n\t\t\thistoryDepth: number = 1\n\t\t) {\n\t\t\tconst { width, height } = getSourceDimensions(source);\n\t\t\tif (!width || !height) {\n\t\t\t\tthrow new Error(`Texture source must have valid dimensions`);\n\t\t\t}\n\n\t\t\tconst depth = Math.floor(historyDepth);\n\t\t\tif (depth <= 1) {\n\t\t\t\treturn originalInitializeTexture(name, source);\n\t\t\t}\n\n\t\t\tif (textures.has(name)) {\n\t\t\t\tthrow new Error(`Texture '${name}' is already initialized.`);\n\t\t\t}\n\n\t\t\tlet unitIndex: number | undefined;\n\t\t\tlet texture: WebGLTexture | null = null;\n\t\t\ttry {\n\t\t\t\tunitIndex = reserveTextureUnit(name);\n\t\t\t\ttexture = createHistoryTexture(gl, depth, width, height);\n\t\t\t\tgl.activeTexture(gl.TEXTURE0 + unitIndex);\n\t\t\t\tgl.bindTexture(gl.TEXTURE_2D_ARRAY, texture);\n\n\t\t\t\t// Flip the texture vertically during upload to match WebGL's coordinate system.\n\t\t\t\t// WebGL uses bottom-left origin, while images/videos use top-left origin.\n\t\t\t\tgl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);\n\t\t\t\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n\t\t\t\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n\t\t\t\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.LINEAR);\n\t\t\t\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MAG_FILTER, gl.LINEAR);\n\n\t\t\t\tif (context.program) {\n\t\t\t\t\tconst uSampler = gl.getUniformLocation(context.program, name);\n\t\t\t\t\tif (uSampler) {\n\t\t\t\t\t\tgl.uniform1i(uSampler, unitIndex);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\ttextures.set(name, {\n\t\t\t\t\ttexture,\n\t\t\t\t\tunitIndex,\n\t\t\t\t\thistoryDepth: depth,\n\t\t\t\t\twriteIndex: 0,\n\t\t\t\t});\n\n\t\t\t\tshaderPadWithHistory.updateTextures({ [name]: source });\n\t\t\t} catch (error) {\n\t\t\t\tif (texture) gl.deleteTexture(texture);\n\t\t\t\tif (unitIndex !== undefined) {\n\t\t\t\t\tcontext.releaseTextureUnit(name);\n\t\t\t\t}\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t};\n\n\t\tshaderPadWithHistory.updateTextures = function (updates) {\n\t\t\tObject.entries(updates).forEach(([name, source]) => {\n\t\t\t\tconst textureInfo = textures.get(name);\n\t\t\t\tif (!textureInfo || !textureInfo.historyDepth || textureInfo.historyDepth <= 1) {\n\t\t\t\t\treturn originalUpdateTextures({ [name]: source });\n\t\t\t\t}\n\n\t\t\t\tconst { width, height } = getSourceDimensions(source);\n\t\t\t\tconst writeIndex = textureInfo.writeIndex ?? 0;\n\t\t\t\tgl.activeTexture(gl.TEXTURE0 + textureInfo.unitIndex);\n\t\t\t\tgl.bindTexture(gl.TEXTURE_2D_ARRAY, textureInfo.texture);\n\t\t\t\tgl.texSubImage3D(\n\t\t\t\t\tgl.TEXTURE_2D_ARRAY,\n\t\t\t\t\t0,\n\t\t\t\t\t0,\n\t\t\t\t\t0,\n\t\t\t\t\twriteIndex,\n\t\t\t\t\twidth,\n\t\t\t\t\theight,\n\t\t\t\t\t1,\n\t\t\t\t\tgl.RGBA,\n\t\t\t\t\tgl.UNSIGNED_BYTE,\n\t\t\t\t\tsource\n\t\t\t\t);\n\t\t\t\ttextureInfo.writeIndex = (writeIndex + 1) % textureInfo.historyDepth;\n\t\t\t});\n\t\t};\n\n\t\tshaderPad.registerHook('destroy', () => {\n\t\t\tif (framebufferHistoryTexture) {\n\t\t\t\tgl.deleteTexture(framebufferHistoryTexture);\n\t\t\t}\n\t\t});\n\t};\n}\n","import ShaderPad, { PluginContext } from '../index';\n\ndeclare module '../index' {\n\tinterface ShaderPad {\n\t\tsave(filename: string): Promise<void>;\n\t}\n}\n\nexport function save() {\n\treturn function (shaderPad: ShaderPad, context: PluginContext) {\n\t\tconst { gl, canvas } = context;\n\t\tconst downloadLink = document.createElement('a');\n\n\t\t(shaderPad as any).save = async function (filename: string) {\n\t\t\tgl.clear(gl.COLOR_BUFFER_BIT);\n\t\t\tgl.drawArrays(gl.TRIANGLES, 0, 6);\n\n\t\t\tif (filename && !`${filename}`.toLowerCase().endsWith('.png')) {\n\t\t\t\tfilename = `${filename}.png`;\n\t\t\t}\n\t\t\tfilename = filename || 'export.png';\n\t\t\tif ('ongesturechange' in window) {\n\t\t\t\t// Mobile.\n\t\t\t\ttry {\n\t\t\t\t\tconst blob: Blob = await new Promise(resolve =>\n\t\t\t\t\t\tcanvas.toBlob(resolve as BlobCallback, 'image/png')\n\t\t\t\t\t);\n\t\t\t\t\tconst file = new File([blob], filename, { type: blob.type });\n\n\t\t\t\t\tif (navigator.canShare?.({ files: [file] })) {\n\t\t\t\t\t\tawait navigator.share({ files: [file] });\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconsole.warn('Web Share API failed:', error);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Desktop.\n\t\t\t\tdownloadLink.download = filename;\n\t\t\t\tdownloadLink.href = canvas.toDataURL();\n\t\t\t\tdownloadLink.click();\n\t\t\t}\n\t\t};\n\t};\n}\n\n// Type helper.\nexport type WithSave<T extends ShaderPad> = T & {\n\tsave(filename: string): Promise<void>;\n};\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,EAAA,YAAAC,EAAA,SAAAC,IAAA,eAAAC,EAAAL,GCMA,SAASM,EAAoBC,EAAuB,CACnD,IAAMC,EAAQD,EACRE,EAAQF,EACRG,EAAQF,EAAM,YAAcC,EAAM,cAAgBA,EAAM,MACxDE,EAASH,EAAM,aAAeC,EAAM,eAAiBA,EAAM,OACjE,MAAO,CAAE,MAAAC,EAAO,OAAAC,CAAO,CACxB,CAEA,SAASC,EAA0BC,EAA4BH,EAAeC,EAAgBG,EAAqB,CAClH,IAAMC,EAAc,IAAI,WAAWL,EAAQC,EAAS,CAAC,EACrD,QAASK,EAAQ,EAAGA,EAAQF,EAAO,EAAEE,EACpCH,EAAG,cAAcA,EAAG,iBAAkB,EAAG,EAAG,EAAGG,EAAON,EAAOC,EAAQ,EAAGE,EAAG,KAAMA,EAAG,cAAeE,CAAW,CAEhH,CAEA,SAASE,EAAqBJ,EAA4BC,EAAeJ,EAAeC,EAA8B,CACrH,IAAMO,EAAUL,EAAG,cAAc,EACjC,GAAI,CAACK,EACJ,MAAM,IAAI,MAAM,kCAAkC,EAEnD,OAAAL,EAAG,cAAcA,EAAG,QAAQ,EAC5BA,EAAG,YAAYA,EAAG,iBAAkBK,CAAO,EAC3CL,EAAG,cAAcA,EAAG,iBAAkBA,EAAG,eAAgBA,EAAG,aAAa,EACzEA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KAAK,gBAAgB,KAAK,OAAS,EACtCA,EAAO,KAAK,gBAAgB,KAAK,IAAI,MAC/B,CAEN,GADAA,EAAO,KAAK,gBAAgB,KACxBA,GAAQ,KAAK,gBAAgB,IAChC,MAAM,IAAI,MAAM,uDAAuD,EAExE,KAAK,gBAAgB,MAAQ,CAC9B,CACA,OAAOA,CACR,CAEQ,mBAAmB/C,EAAc,CACxC,IAAM8C,EAAW,KAAK,SAAS,IAAI9C,CAAI,EACvC,GAAI,CAAC8C,EACJ,OAED,IAAMC,EAAOD,EAAS,UAClBC,EAAO,GACV,KAAK,gBAAgB,KAAK,KAAKA,CAAI,CAErC,CACD,EAGOC,EAAQrD","names":["index_exports","__export","index_default","history","save","__toCommonJS","getSourceDimensions","source","video","image","width","height","clearHistoryTextureLayers","gl","depth","transparent","layer","createHistoryTexture","texture","history","framebufferDepth","shaderPad","context","uniforms","textures","reserveTextureUnit","shaderPadWithHistory","originalInitializeTexture","originalUpdateTextures","framebufferHistoryTexture","initializeFramebufferHistory","clearFramebufferHistory","_time","frame","writeIdx","textureInfo","name","historyDepth","unitIndex","uSampler","error","updates","writeIndex","save","shaderPad","context","gl","canvas","downloadLink","filename","blob","resolve","file","error","DEFAULT_VERTEX_SHADER_SRC","RESIZE_THROTTLE_INTERVAL","ShaderPad","fragmentShaderSrc","options","context","plugin","name","fn","vertexShaderSrc","vertexShader","fragmentShader","aPosition","hook","type","source","shader","quadVertices","now","timeUntilNextResize","pixelRatio","width","height","updateCursor","x","y","rect","updateClick","isMouseDown","xVal","yVal","event","mouseEvent","touchEvent","listener","value","location","length","updates","uniform","time","gl","callback","loop","texture","unitIndex","error","uSampler","info","filename","blob","resolve","file","existing","unit","index_default"]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
var l=`#version 300 es
|
|
1
|
+
function v(s){let t=s,i=s,e=t.videoWidth||i.naturalWidth||i.width,r=t.videoHeight||i.naturalHeight||i.height;return{width:e,height:r}}function b(s,t,i,e){let r=new Uint8Array(t*i*4);for(let n=0;n<e;++n)s.texSubImage3D(s.TEXTURE_2D_ARRAY,0,0,0,n,t,i,1,s.RGBA,s.UNSIGNED_BYTE,r)}function R(s,t,i,e){let r=s.createTexture();if(!r)throw new Error("Failed to create history texture");return s.activeTexture(s.TEXTURE0),s.bindTexture(s.TEXTURE_2D_ARRAY,r),s.texParameteri(s.TEXTURE_2D_ARRAY,s.TEXTURE_WRAP_S,s.CLAMP_TO_EDGE),s.texParameteri(s.TEXTURE_2D_ARRAY,s.TEXTURE_WRAP_T,s.CLAMP_TO_EDGE),s.texParameteri(s.TEXTURE_2D_ARRAY,s.TEXTURE_MIN_FILTER,s.LINEAR),s.texParameteri(s.TEXTURE_2D_ARRAY,s.TEXTURE_MAG_FILTER,s.LINEAR),s.texStorage3D(s.TEXTURE_2D_ARRAY,1,s.RGBA8,i,e,t),b(s,i,e,t),r}function I(s=1){return function(t,i){let{gl:e,uniforms:r,textures:n,reserveTextureUnit:a}=i,c=t,T=t.initializeTexture.bind(t),_=t.updateTextures.bind(t),u=null;if(s>1){let o=function(){u=R(e,s,e.drawingBufferWidth,e.drawingBufferHeight),r.has("u_history")||t.initializeUniform("u_history","int",0)},l=function(){u&&(e.activeTexture(e.TEXTURE0),e.bindTexture(e.TEXTURE_2D_ARRAY,u),b(e,e.drawingBufferWidth,e.drawingBufferHeight,s))};var L=o,P=l;t.registerHook("init",o),t.registerHook("step",(d,h)=>{if(!u)return;let f=h%s;e.activeTexture(e.TEXTURE0),e.bindTexture(e.TEXTURE_2D_ARRAY,u),e.copyTexSubImage3D(e.TEXTURE_2D_ARRAY,0,0,0,f,0,0,e.drawingBufferWidth,e.drawingBufferHeight)}),t.registerHook("updateResolution",()=>{u&&e.deleteTexture(u),o()}),t.registerHook("reset",l)}t.registerHook("reset",()=>{n.forEach(o=>{o.historyDepth&&o.historyDepth>1&&o.writeIndex!==void 0&&(o.writeIndex=0)})}),c.initializeTexture=function(o,l,d=1){let{width:h,height:f}=v(l);if(!h||!f)throw new Error("Texture source must have valid dimensions");let m=Math.floor(d);if(m<=1)return T(o,l);if(n.has(o))throw new Error(`Texture '${o}' is already initialized.`);let g,E=null;try{if(g=a(o),E=R(e,m,h,f),e.activeTexture(e.TEXTURE0+g),e.bindTexture(e.TEXTURE_2D_ARRAY,E),e.pixelStorei(e.UNPACK_FLIP_Y_WEBGL,!0),e.texParameteri(e.TEXTURE_2D_ARRAY,e.TEXTURE_WRAP_S,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D_ARRAY,e.TEXTURE_WRAP_T,e.CLAMP_TO_EDGE),e.texParameteri(e.TEXTURE_2D_ARRAY,e.TEXTURE_MIN_FILTER,e.LINEAR),e.texParameteri(e.TEXTURE_2D_ARRAY,e.TEXTURE_MAG_FILTER,e.LINEAR),i.program){let x=e.getUniformLocation(i.program,o);x&&e.uniform1i(x,g)}n.set(o,{texture:E,unitIndex:g,historyDepth:m,writeIndex:0}),c.updateTextures({[o]:l})}catch(x){throw E&&e.deleteTexture(E),g!==void 0&&i.releaseTextureUnit(o),x}},c.updateTextures=function(o){Object.entries(o).forEach(([l,d])=>{let h=n.get(l);if(!h||!h.historyDepth||h.historyDepth<=1)return _({[l]:d});let{width:f,height:m}=v(d),g=h.writeIndex??0;e.activeTexture(e.TEXTURE0+h.unitIndex),e.bindTexture(e.TEXTURE_2D_ARRAY,h.texture),e.texSubImage3D(e.TEXTURE_2D_ARRAY,0,0,0,g,f,m,1,e.RGBA,e.UNSIGNED_BYTE,d),h.writeIndex=(g+1)%h.historyDepth})},t.registerHook("destroy",()=>{u&&e.deleteTexture(u)})}}function y(){return function(s,t){let{gl:i,canvas:e}=t,r=document.createElement("a");s.save=async function(n){if(i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLES,0,6),n&&!`${n}`.toLowerCase().endsWith(".png")&&(n=`${n}.png`),n=n||"export.png","ongesturechange"in window)try{let a=await new Promise(T=>e.toBlob(T,"image/png")),c=new File([a],n,{type:a.type});if(navigator.canShare?.({files:[c]})){await navigator.share({files:[c]});return}}catch(a){console.warn("Web Share API failed:",a)}else r.download=n,r.href=e.toDataURL(),r.click()}}}var w=`#version 300 es
|
|
2
2
|
in vec2 aPosition;
|
|
3
3
|
out vec2 v_uv;
|
|
4
4
|
void main() {
|
|
5
5
|
v_uv = aPosition * 0.5 + 0.5;
|
|
6
6
|
gl_Position = vec4(aPosition, 0.0, 1.0);
|
|
7
7
|
}
|
|
8
|
-
`,c=33.333333333333336,h=class{constructor(t,i={}){this.isInternalCanvas=!1;this.isTouchDevice=!1;this.uniforms=new Map;this.textures=new Map;this.buffer=null;this.program=null;this.resizeTimeout=null;this.lastResizeTime=0;this.eventListeners=new Map;this.frame=0;this.cursorPosition=[.5,.5];this.scrollX=0;this.scrollY=0;this.clickPosition=[.5,.5];this.isMouseDown=!1;this.historyTexture=null;if(this.canvas=i.canvas||document.createElement("canvas"),this.historyLength=i.history||0,i.canvas||(this.isInternalCanvas=!0,document.body.appendChild(this.canvas),this.canvas.style.position="fixed",this.canvas.style.inset="0",this.canvas.style.height="100dvh",this.canvas.style.width="100dvw"),this.gl=this.canvas.getContext("webgl2",{antialias:!1}),!this.gl)throw new Error("WebGL2 not supported. Please use a browser that supports WebGL2.");this.downloadLink=document.createElement("a"),this.fragmentShaderSrc=t,this.animationFrameId=null,this.resizeObserver=new ResizeObserver(()=>this.throttledHandleResize()),this.resizeObserver.observe(this.canvas),this.init(),this.addEventListeners()}init(){let t=l;if(this.program=this.gl.createProgram(),!this.program)throw new Error("Failed to create WebGL program");let i=this.createShader(this.gl.VERTEX_SHADER,t),e=this.createShader(this.gl.FRAGMENT_SHADER,this.fragmentShaderSrc);if(this.gl.attachShader(this.program,i),this.gl.attachShader(this.program,e),this.gl.linkProgram(this.program),this.gl.deleteShader(i),this.gl.deleteShader(e),!this.gl.getProgramParameter(this.program,this.gl.LINK_STATUS))throw console.error("Program link error:",this.gl.getProgramInfoLog(this.program)),this.gl.deleteProgram(this.program),new Error("Failed to link WebGL program");let s=this.gl.getAttribLocation(this.program,"aPosition");this.setupBuffer(s),this.handleResize(),this.gl.useProgram(this.program),this.initializeUniform("u_resolution","float",[this.canvas.width,this.canvas.height]),this.initializeUniform("u_cursor","float",[...this.cursorPosition,this.scrollX,this.scrollY]),this.initializeUniform("u_click","float",[...this.clickPosition,this.isMouseDown?1:0]),this.initializeUniform("u_time","float",0),this.initializeUniform("u_frame","int",0),this.historyLength>0&&this.initializeHistoryBuffer()}initializeHistoryBuffer(){let{gl:t}=this;if(this.historyTexture=t.createTexture(),!this.historyTexture)throw new Error("Failed to create history texture");t.bindTexture(t.TEXTURE_2D_ARRAY,this.historyTexture),t.texParameteri(t.TEXTURE_2D_ARRAY,t.TEXTURE_WRAP_S,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D_ARRAY,t.TEXTURE_WRAP_T,t.CLAMP_TO_EDGE),t.texParameteri(t.TEXTURE_2D_ARRAY,t.TEXTURE_MIN_FILTER,t.NEAREST),t.texParameteri(t.TEXTURE_2D_ARRAY,t.TEXTURE_MAG_FILTER,t.NEAREST),t.texStorage3D(t.TEXTURE_2D_ARRAY,1,t.RGBA8,this.canvas.width,this.canvas.height,this.historyLength);let i=new Uint8Array(this.canvas.width*this.canvas.height*4);for(let e=0;e<this.historyLength;++e)t.texSubImage3D(t.TEXTURE_2D_ARRAY,0,0,0,e,this.canvas.width,this.canvas.height,1,t.RGBA,t.UNSIGNED_BYTE,i);t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D_ARRAY,this.historyTexture),this.uniforms.has("u_history")||this.initializeUniform("u_history","int",0)}createShader(t,i){let e=this.gl.createShader(t);if(this.gl.shaderSource(e,i),this.gl.compileShader(e),!this.gl.getShaderParameter(e,this.gl.COMPILE_STATUS))throw console.error("Shader compilation failed:",i),console.error(this.gl.getShaderInfoLog(e)),this.gl.deleteShader(e),new Error("Shader compilation failed");return e}setupBuffer(t){let i=new Float32Array([-1,-1,1,-1,-1,1,-1,1,1,-1,1,1]);this.buffer=this.gl.createBuffer(),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.buffer),this.gl.bufferData(this.gl.ARRAY_BUFFER,i,this.gl.STATIC_DRAW),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.gl.enableVertexAttribArray(t),this.gl.vertexAttribPointer(t,2,this.gl.FLOAT,!1,0,0)}throttledHandleResize(){clearTimeout(this.resizeTimeout);let t=performance.now(),i=this.lastResizeTime+c-t;i<=0?(this.lastResizeTime=t,this.handleResize()):this.resizeTimeout=setTimeout(()=>this.throttledHandleResize(),i)}handleResize(){let t=window.devicePixelRatio||1,i=this.canvas.clientWidth*t,e=this.canvas.clientHeight*t,s=getComputedStyle(this.canvas),r=s.width!==`${this.canvas.width}px`&&s.width!=="auto",n=s.height!==`${this.canvas.height}px`&&s.height!=="auto";(!r||!n)&&(this.canvas.style.width=`${this.canvas.clientWidth}px`,this.canvas.style.height=`${this.canvas.clientHeight}px`),(this.canvas.width!==i||this.canvas.height!==e)&&(this.canvas.width=i,this.canvas.height=e,this.gl.viewport(0,0,this.gl.drawingBufferWidth,this.gl.drawingBufferHeight),this.uniforms.has("u_resolution")&&this.updateUniforms({u_resolution:[this.canvas.width,this.canvas.height]}),this.historyLength>0&&this.historyTexture&&(this.gl.deleteTexture(this.historyTexture),this.initializeHistoryBuffer()))}addEventListeners(){let t=(e,s)=>{if(!this.uniforms.has("u_cursor"))return;let r=this.canvas.getBoundingClientRect();this.cursorPosition[0]=(e-r.left)/r.width,this.cursorPosition[1]=1-(s-r.top)/r.height,this.updateUniforms({u_cursor:[this.cursorPosition[0],this.cursorPosition[1],this.scrollX,this.scrollY]})},i=(e,s,r)=>{if(this.uniforms.has("u_click")){if(this.isMouseDown=e,e){let n=this.canvas.getBoundingClientRect(),a=s,o=r;this.clickPosition[0]=(a-n.left)/n.width,this.clickPosition[1]=1-(o-n.top)/n.height}this.updateUniforms({u_click:[this.clickPosition[0],this.clickPosition[1],this.isMouseDown?1:0]})}};this.eventListeners.set("mousemove",e=>{let s=e;this.isTouchDevice||t(s.clientX,s.clientY)}),this.eventListeners.set("mousedown",e=>{let s=e;this.isTouchDevice||s.button===0&&(this.isMouseDown=!0,i(!0,s.clientX,s.clientY))}),this.eventListeners.set("mouseup",e=>{let s=e;this.isTouchDevice||s.button===0&&i(!1)}),this.eventListeners.set("wheel",e=>{let s=e;this.scrollX+=s.deltaX*.01,this.scrollY+=s.deltaY*.01,t(s.clientX,s.clientY)}),this.eventListeners.set("touchmove",e=>{let s=e;s.touches.length>0&&t(s.touches[0].clientX,s.touches[0].clientY)}),this.eventListeners.set("touchstart",e=>{let s=e;this.isTouchDevice=!0,s.touches.length>0&&(i(!0,s.touches[0].clientX,s.touches[0].clientY),t(s.touches[0].clientX,s.touches[0].clientY))}),this.eventListeners.set("touchend",e=>{e.touches.length===0&&i(!1)}),this.eventListeners.forEach((e,s)=>{this.canvas.addEventListener(s,e)})}initializeUniform(t,i,e){if(this.uniforms.has(t))throw new Error(`Uniform '${t}' is already initialized.`);if(i!=="float"&&i!=="int")throw new Error(`Invalid uniform type: ${i}. Expected 'float' or 'int'.`);let s=this.gl.getUniformLocation(this.program,t);if(!s){console.debug(`Uniform ${t} not found in fragment shader. Skipping initialization.`);return}if(Array.isArray(e)||(e=[e]),e.length<1||e.length>4)throw new Error(`Invalid uniform value length: ${e.length}. Expected a length between 1 and 4.`);let r=e.length;this.uniforms.set(t,{type:i,length:r,location:s}),this.updateUniforms({[t]:e})}updateUniforms(t){Object.entries(t).forEach(([i,e])=>{if(!this.uniforms.has(i))throw new Error(`Uniform '${i}' is not initialized.`);let s=this.uniforms.get(i);if(Array.isArray(e)||(e=[e]),e.length!==s.length)throw new Error(`Invalid uniform value length: ${e.length}. Expected ${s.length}.`);this.gl[`uniform${s.length}${s.type.charAt(0)}`](s.location,...e)})}step(t){let i=this.gl;if(this.uniforms.has("u_time")&&this.updateUniforms({u_time:t}),this.uniforms.has("u_frame")&&this.updateUniforms({u_frame:this.frame}),this.historyLength>0){let e=this.frame%this.historyLength;i.bindFramebuffer(i.FRAMEBUFFER,null),i.viewport(0,0,this.canvas.width,this.canvas.height),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLES,0,6),i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D_ARRAY,this.historyTexture),i.copyTexSubImage3D(i.TEXTURE_2D_ARRAY,0,0,0,e,0,0,this.canvas.width,this.canvas.height)}else i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLES,0,6);++this.frame}play(t){let i=e=>{e/=1e3,this.step(e),t&&t(e,this.frame),this.animationFrameId=requestAnimationFrame(i)};this.animationFrameId=requestAnimationFrame(i)}pause(){this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null)}save(t){this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.drawArrays(this.gl.TRIANGLES,0,6);let i=this.canvas.toDataURL();t&&!`${t}`.toLowerCase().endsWith(".png")&&(t=`${t}.png`),this.downloadLink.download=t||"export.png",this.downloadLink.href=i,this.downloadLink.click()}initializeTexture(t,i){if(this.textures.has(t))throw new Error(`Texture '${t}' is already initialized.`);let e=this.gl.createTexture();if(!e)throw new Error("Failed to create texture");let s=this.textures.size+1;this.gl.activeTexture(this.gl.TEXTURE0+s),this.gl.bindTexture(this.gl.TEXTURE_2D,e),this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL,!0),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_S,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_T,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.LINEAR),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.LINEAR),this.textures.set(t,{texture:e,unitIndex:s}),this.updateTextures({[t]:i});let r=this.gl.getUniformLocation(this.program,t);r&&this.gl.uniform1i(r,s)}updateTextures(t){Object.entries(t).forEach(([i,e])=>{let s=this.textures.get(i);if(!s)throw new Error(`Texture '${i}' is not initialized.`);this.gl.activeTexture(this.gl.TEXTURE0+s.unitIndex),this.gl.bindTexture(this.gl.TEXTURE_2D,s.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,e)})}destroy(){this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null),this.resizeObserver.unobserve(this.canvas),this.eventListeners.forEach((t,i)=>{this.canvas.removeEventListener(i,t)}),this.program&&this.gl.deleteProgram(this.program),this.textures.forEach(t=>{this.gl.deleteTexture(t.texture)}),this.historyTexture&&(this.gl.deleteTexture(this.historyTexture),this.historyTexture=null),this.buffer&&(this.gl.deleteBuffer(this.buffer),this.buffer=null),this.isInternalCanvas&&this.canvas.remove()}},g=h;export{g as default};
|
|
8
|
+
`,U=1e3/30,p=class{constructor(t,i={}){this.isInternalCanvas=!1;this.isTouchDevice=!1;this.uniforms=new Map;this.textures=new Map;this.buffer=null;this.program=null;this.resizeTimeout=null;this.lastResizeTime=0;this.eventListeners=new Map;this.frame=0;this.startTime=0;this.cursorPosition=[.5,.5];this.clickPosition=[.5,.5];this.isMouseDown=!1;this.hooks=new Map;if(this.canvas=i.canvas||document.createElement("canvas"),i.canvas||(this.isInternalCanvas=!0,document.body.appendChild(this.canvas),this.canvas.style.position="fixed",this.canvas.style.inset="0",this.canvas.style.height="100dvh",this.canvas.style.width="100dvw"),this.gl=this.canvas.getContext("webgl2",{antialias:!1}),!this.gl)throw new Error("WebGL2 not supported. Please use a browser that supports WebGL2.");if(this.textureUnitPool={free:[],next:1,max:this.gl.getParameter(this.gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS)},this.downloadLink=document.createElement("a"),this.fragmentShaderSrc=t,this.animationFrameId=null,this.resolutionObserver=new MutationObserver(()=>this.updateResolution()),this.resizeObserver=new ResizeObserver(()=>this.throttledHandleResize()),i.plugins){let e={gl:this.gl,uniforms:this.uniforms,textures:this.textures,canvas:this.canvas,reserveTextureUnit:this.reserveTextureUnit.bind(this),releaseTextureUnit:this.releaseTextureUnit.bind(this)};Object.defineProperty(e,"program",{get:()=>this.program,enumerable:!0,configurable:!0}),i.plugins.forEach(r=>r(this,e))}this.init(),this.addEventListeners()}registerHook(t,i){this.hooks.has(t)||this.hooks.set(t,[]),this.hooks.get(t).push(i)}init(){let t=w;if(this.program=this.gl.createProgram(),!this.program)throw new Error("Failed to create WebGL program");let i=this.createShader(this.gl.VERTEX_SHADER,t),e=this.createShader(this.gl.FRAGMENT_SHADER,this.fragmentShaderSrc);if(this.gl.attachShader(this.program,i),this.gl.attachShader(this.program,e),this.gl.linkProgram(this.program),this.gl.deleteShader(i),this.gl.deleteShader(e),!this.gl.getProgramParameter(this.program,this.gl.LINK_STATUS))throw console.error("Program link error:",this.gl.getProgramInfoLog(this.program)),this.gl.deleteProgram(this.program),new Error("Failed to link WebGL program");let r=this.gl.getAttribLocation(this.program,"aPosition");this.setupBuffer(r),this.gl.useProgram(this.program),this.resolutionObserver.observe(this.canvas,{attributes:!0,attributeFilter:["width","height"]}),this.resizeObserver.observe(this.canvas),this.isInternalCanvas||this.updateResolution(),this.initializeUniform("u_cursor","float",this.cursorPosition),this.initializeUniform("u_click","float",[...this.clickPosition,this.isMouseDown?1:0]),this.initializeUniform("u_time","float",0),this.initializeUniform("u_frame","int",0),this.hooks.get("init")?.forEach(n=>n.call(this))}createShader(t,i){let e=this.gl.createShader(t);if(this.gl.shaderSource(e,i),this.gl.compileShader(e),!this.gl.getShaderParameter(e,this.gl.COMPILE_STATUS))throw console.error("Shader compilation failed:",i),console.error(this.gl.getShaderInfoLog(e)),this.gl.deleteShader(e),new Error("Shader compilation failed");return e}setupBuffer(t){let i=new Float32Array([-1,-1,1,-1,-1,1,-1,1,1,-1,1,1]);this.buffer=this.gl.createBuffer(),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,this.buffer),this.gl.bufferData(this.gl.ARRAY_BUFFER,i,this.gl.STATIC_DRAW),this.gl.viewport(0,0,this.gl.drawingBufferWidth,this.gl.drawingBufferHeight),this.gl.enableVertexAttribArray(t),this.gl.vertexAttribPointer(t,2,this.gl.FLOAT,!1,0,0)}throttledHandleResize(){clearTimeout(this.resizeTimeout);let t=performance.now(),i=this.lastResizeTime+U-t;i<=0?(this.lastResizeTime=t,this.handleResize()):this.resizeTimeout=setTimeout(()=>this.throttledHandleResize(),i)}handleResize(){let t=window.devicePixelRatio||1,i=this.canvas.clientWidth*t,e=this.canvas.clientHeight*t;this.isInternalCanvas&&(this.canvas.width!==i||this.canvas.height!==e)&&(this.canvas.width=i,this.canvas.height=e),this.onResize?.(i,e)}addEventListeners(){let t=(e,r)=>{if(!this.uniforms.has("u_cursor"))return;let n=this.canvas.getBoundingClientRect();this.cursorPosition[0]=(e-n.left)/n.width,this.cursorPosition[1]=1-(r-n.top)/n.height,this.updateUniforms({u_cursor:this.cursorPosition})},i=(e,r,n)=>{if(this.uniforms.has("u_click")){if(this.isMouseDown=e,e){let a=this.canvas.getBoundingClientRect(),c=r,T=n;this.clickPosition[0]=(c-a.left)/a.width,this.clickPosition[1]=1-(T-a.top)/a.height}this.updateUniforms({u_click:[...this.clickPosition,this.isMouseDown?1:0]})}};this.eventListeners.set("mousemove",e=>{let r=e;this.isTouchDevice||t(r.clientX,r.clientY)}),this.eventListeners.set("mousedown",e=>{let r=e;this.isTouchDevice||r.button===0&&(this.isMouseDown=!0,i(!0,r.clientX,r.clientY))}),this.eventListeners.set("mouseup",e=>{let r=e;this.isTouchDevice||r.button===0&&i(!1)}),this.eventListeners.set("touchmove",e=>{let r=e;r.touches.length>0&&t(r.touches[0].clientX,r.touches[0].clientY)}),this.eventListeners.set("touchstart",e=>{let r=e;this.isTouchDevice=!0,r.touches.length>0&&(t(r.touches[0].clientX,r.touches[0].clientY),i(!0,r.touches[0].clientX,r.touches[0].clientY))}),this.eventListeners.set("touchend",e=>{e.touches.length===0&&i(!1)}),this.eventListeners.forEach((e,r)=>{this.canvas.addEventListener(r,e)})}updateResolution(){let t=this.gl.drawingBufferWidth,i=this.gl.drawingBufferHeight;this.gl.viewport(0,0,t,i),this.uniforms.has("u_resolution")?this.updateUniforms({u_resolution:[t,i]}):this.initializeUniform("u_resolution","float",[t,i]),this.hooks.get("updateResolution")?.forEach(e=>e.call(this))}initializeUniform(t,i,e){if(this.uniforms.has(t))throw new Error(`Uniform '${t}' is already initialized.`);if(i!=="float"&&i!=="int")throw new Error(`Invalid uniform type: ${i}. Expected 'float' or 'int'.`);let r=this.gl.getUniformLocation(this.program,t);if(!r){console.debug(`Uniform ${t} not found in fragment shader. Skipping initialization.`);return}if(Array.isArray(e)||(e=[e]),e.length<1||e.length>4)throw new Error(`Invalid uniform value length: ${e.length}. Expected a length between 1 and 4.`);let n=e.length;this.uniforms.set(t,{type:i,length:n,location:r}),this.updateUniforms({[t]:e})}updateUniforms(t){Object.entries(t).forEach(([i,e])=>{if(!this.uniforms.has(i))throw new Error(`Uniform '${i}' is not initialized.`);let r=this.uniforms.get(i);if(Array.isArray(e)||(e=[e]),e.length!==r.length)throw new Error(`Invalid uniform value length: ${e.length}. Expected ${r.length}.`);this.gl[`uniform${r.length}${r.type.charAt(0)}`](r.location,...e)})}step(t){let i=this.gl;this.uniforms.has("u_time")&&this.updateUniforms({u_time:t}),this.uniforms.has("u_frame")&&this.updateUniforms({u_frame:this.frame}),i.clear(i.COLOR_BUFFER_BIT),i.drawArrays(i.TRIANGLES,0,6),this.hooks.get("step")?.forEach(e=>e.call(this,t,this.frame)),++this.frame}play(t){this.pause();let i=e=>{e=(e-this.startTime)/1e3,this.step(e),this.animationFrameId=requestAnimationFrame(i),t&&t(e,this.frame)};this.animationFrameId=requestAnimationFrame(i)}pause(){this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null)}reset(){this.frame=0,this.startTime=performance.now(),this.hooks.get("reset")?.forEach(t=>t.call(this))}initializeTexture(t,i){if(this.textures.has(t))throw new Error(`Texture '${t}' is already initialized.`);let e=this.gl.createTexture();if(!e)throw new Error("Failed to create texture");let r;try{r=this.reserveTextureUnit(t)}catch(a){throw this.gl.deleteTexture(e),a}this.gl.activeTexture(this.gl.TEXTURE0+r),this.gl.bindTexture(this.gl.TEXTURE_2D,e),this.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL,!0),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_S,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_WRAP_T,this.gl.CLAMP_TO_EDGE),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MIN_FILTER,this.gl.LINEAR),this.gl.texParameteri(this.gl.TEXTURE_2D,this.gl.TEXTURE_MAG_FILTER,this.gl.LINEAR),this.textures.set(t,{texture:e,unitIndex:r}),this.updateTextures({[t]:i});let n=this.gl.getUniformLocation(this.program,t);n&&this.gl.uniform1i(n,r)}updateTextures(t){Object.entries(t).forEach(([i,e])=>{let r=this.textures.get(i);if(!r)throw new Error(`Texture '${i}' is not initialized.`);this.gl.activeTexture(this.gl.TEXTURE0+r.unitIndex),this.gl.bindTexture(this.gl.TEXTURE_2D,r.texture),this.gl.texImage2D(this.gl.TEXTURE_2D,0,this.gl.RGBA,this.gl.RGBA,this.gl.UNSIGNED_BYTE,e)})}async save(t){if(this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.drawArrays(this.gl.TRIANGLES,0,6),t&&!`${t}`.toLowerCase().endsWith(".png")&&(t=`${t}.png`),t=t||"export.png","ongesturechange"in window)try{let i=await new Promise(r=>this.canvas.toBlob(r,"image/png")),e=new File([i],t,{type:i.type});if(navigator.canShare?.({files:[e]})){await navigator.share({files:[e]});return}}catch(i){console.warn("Web Share API failed:",i)}else this.downloadLink.download=t,this.downloadLink.href=this.canvas.toDataURL(),this.downloadLink.click()}destroy(){this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null),this.resolutionObserver.disconnect(),this.resizeObserver.disconnect(),this.eventListeners.forEach((t,i)=>{this.canvas.removeEventListener(i,t)}),this.program&&this.gl.deleteProgram(this.program),this.textures.forEach(t=>{this.gl.deleteTexture(t.texture)}),this.textureUnitPool.free=[],this.textureUnitPool.next=1,this.buffer&&(this.gl.deleteBuffer(this.buffer),this.buffer=null),this.hooks.get("destroy")?.forEach(t=>t.call(this)),this.isInternalCanvas&&this.canvas.remove()}reserveTextureUnit(t){if(!t)throw new Error("Texture unit name must be provided.");let i=this.textures.get(t);if(i)return i.unitIndex;let e;if(this.textureUnitPool.free.length>0)e=this.textureUnitPool.free.pop();else{if(e=this.textureUnitPool.next,e>=this.textureUnitPool.max)throw new Error("Exceeded the available texture units for this device.");this.textureUnitPool.next+=1}return e}releaseTextureUnit(t){let i=this.textures.get(t);if(!i)return;let e=i.unitIndex;e>0&&this.textureUnitPool.free.push(e)}},C=p;export{C as default,I as history,y as save};
|
|
9
9
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["const DEFAULT_VERTEX_SHADER_SRC = `#version 300 es\nin vec2 aPosition;\nout vec2 v_uv;\nvoid main() {\n v_uv = aPosition * 0.5 + 0.5;\n gl_Position = vec4(aPosition, 0.0, 1.0);\n}\n`;\nconst RESIZE_THROTTLE_INTERVAL = 1000 / 30;\n\ninterface Uniform {\n\ttype: 'float' | 'int';\n\tlength: 1 | 2 | 3 | 4;\n\tlocation: WebGLUniformLocation;\n}\n\ninterface Texture {\n\ttexture: WebGLTexture;\n\tunitIndex: number;\n}\n\ninterface Options {\n\tcanvas?: HTMLCanvasElement | null;\n\thistory?: number;\n}\n\nclass ShaderPad {\n\tprivate isInternalCanvas = false;\n\tprivate isTouchDevice = false;\n\tprivate canvas: HTMLCanvasElement;\n\tprivate gl: WebGL2RenderingContext;\n\tprivate downloadLink: HTMLAnchorElement;\n\tprivate fragmentShaderSrc: string;\n\tprivate uniforms: Map<string, Uniform> = new Map();\n\tprivate textures: Map<string, Texture> = new Map();\n\tprivate buffer: WebGLBuffer | null = null;\n\tprivate program: WebGLProgram | null = null;\n\tprivate animationFrameId: number | null;\n\tprivate resizeObserver: ResizeObserver;\n\tprivate resizeTimeout: NodeJS.Timeout = null as unknown as NodeJS.Timeout;\n\tprivate lastResizeTime = 0;\n\tprivate eventListeners: Map<string, EventListener> = new Map();\n\tprivate frame = 0;\n\tprivate cursorPosition = [0.5, 0.5];\n\tprivate scrollX = 0;\n\tprivate scrollY = 0;\n\tprivate clickPosition = [0.5, 0.5];\n\tprivate isMouseDown = false;\n\tprivate historyLength: number;\n\tprivate historyTexture: WebGLTexture | null = null;\n\n\tconstructor(fragmentShaderSrc: string, options: Options = {}) {\n\t\tthis.canvas = options.canvas || document.createElement('canvas');\n\t\tthis.historyLength = options.history || 0;\n\t\tif (!options.canvas) {\n\t\t\tthis.isInternalCanvas = true;\n\t\t\tdocument.body.appendChild(this.canvas);\n\t\t\tthis.canvas.style.position = 'fixed';\n\t\t\tthis.canvas.style.inset = '0';\n\t\t\tthis.canvas.style.height = '100dvh';\n\t\t\tthis.canvas.style.width = '100dvw';\n\t\t}\n\n\t\tthis.gl = this.canvas.getContext('webgl2', { antialias: false }) as WebGL2RenderingContext;\n\t\tif (!this.gl) {\n\t\t\tthrow new Error('WebGL2 not supported. Please use a browser that supports WebGL2.');\n\t\t}\n\n\t\tthis.downloadLink = document.createElement('a');\n\t\tthis.fragmentShaderSrc = fragmentShaderSrc;\n\t\tthis.animationFrameId = null;\n\t\tthis.resizeObserver = new ResizeObserver(() => this.throttledHandleResize());\n\t\tthis.resizeObserver.observe(this.canvas);\n\t\tthis.init();\n\t\tthis.addEventListeners();\n\t}\n\n\tprivate init() {\n\t\tconst vertexShaderSrc = DEFAULT_VERTEX_SHADER_SRC;\n\n\t\tthis.program = this.gl.createProgram();\n\t\tif (!this.program) {\n\t\t\tthrow new Error('Failed to create WebGL program');\n\t\t}\n\t\tconst vertexShader = this.createShader(this.gl.VERTEX_SHADER, vertexShaderSrc);\n\t\tconst fragmentShader = this.createShader(this.gl.FRAGMENT_SHADER, this.fragmentShaderSrc);\n\n\t\tthis.gl.attachShader(this.program, vertexShader);\n\t\tthis.gl.attachShader(this.program, fragmentShader);\n\t\tthis.gl.linkProgram(this.program);\n\t\tthis.gl.deleteShader(vertexShader);\n\t\tthis.gl.deleteShader(fragmentShader);\n\n\t\tif (!this.gl.getProgramParameter(this.program, this.gl.LINK_STATUS)) {\n\t\t\tconsole.error('Program link error:', this.gl.getProgramInfoLog(this.program));\n\t\t\tthis.gl.deleteProgram(this.program);\n\t\t\tthrow new Error('Failed to link WebGL program');\n\t\t}\n\n\t\tconst aPosition = this.gl.getAttribLocation(this.program, 'aPosition');\n\t\tthis.setupBuffer(aPosition);\n\t\tthis.handleResize();\n\n\t\tthis.gl.useProgram(this.program);\n\n\t\tthis.initializeUniform('u_resolution', 'float', [this.canvas.width, this.canvas.height]);\n\t\tthis.initializeUniform('u_cursor', 'float', [...this.cursorPosition, this.scrollX, this.scrollY]); // [cursorX, cursorY, scrollX, scrollY]\n\t\tthis.initializeUniform('u_click', 'float', [...this.clickPosition, this.isMouseDown ? 1.0 : 0.0]); // [clickX, clickY, leftClick]\n\t\tthis.initializeUniform('u_time', 'float', 0);\n\t\tthis.initializeUniform('u_frame', 'int', 0);\n\n\t\tif (this.historyLength > 0) {\n\t\t\tthis.initializeHistoryBuffer();\n\t\t}\n\t}\n\n\tprivate initializeHistoryBuffer() {\n\t\tconst { gl } = this;\n\n\t\tthis.historyTexture = gl.createTexture();\n\t\tif (!this.historyTexture) {\n\t\t\tthrow new Error('Failed to create history texture');\n\t\t}\n\n\t\tgl.bindTexture(gl.TEXTURE_2D_ARRAY, this.historyTexture);\n\t\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n\t\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n\t\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.NEAREST);\n\t\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MAG_FILTER, gl.NEAREST);\n\t\tgl.texStorage3D(gl.TEXTURE_2D_ARRAY, 1, gl.RGBA8, this.canvas.width, this.canvas.height, this.historyLength);\n\n\t\t// Make initial frames transparent.\n\t\tconst transparent = new Uint8Array(this.canvas.width * this.canvas.height * 4); // All zeroes.\n\t\tfor (let layer = 0; layer < this.historyLength; ++layer) {\n\t\t\tgl.texSubImage3D(\n\t\t\t\tgl.TEXTURE_2D_ARRAY,\n\t\t\t\t0,\n\t\t\t\t0,\n\t\t\t\t0,\n\t\t\t\tlayer,\n\t\t\t\tthis.canvas.width,\n\t\t\t\tthis.canvas.height,\n\t\t\t\t1,\n\t\t\t\tgl.RGBA,\n\t\t\t\tgl.UNSIGNED_BYTE,\n\t\t\t\ttransparent\n\t\t\t);\n\t\t}\n\n\t\tgl.activeTexture(gl.TEXTURE0);\n\t\tgl.bindTexture(gl.TEXTURE_2D_ARRAY, this.historyTexture);\n\n\t\tif (!this.uniforms.has('u_history')) {\n\t\t\tthis.initializeUniform('u_history', 'int', 0);\n\t\t}\n\t}\n\n\tprivate createShader(type: number, source: string): WebGLShader {\n\t\tconst shader = this.gl.createShader(type)!;\n\t\tthis.gl.shaderSource(shader, source);\n\t\tthis.gl.compileShader(shader);\n\t\tif (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)) {\n\t\t\tconsole.error('Shader compilation failed:', source);\n\t\t\tconsole.error(this.gl.getShaderInfoLog(shader));\n\t\t\tthis.gl.deleteShader(shader);\n\t\t\tthrow new Error('Shader compilation failed');\n\t\t}\n\t\treturn shader;\n\t}\n\n\tprivate setupBuffer(aPosition: number) {\n\t\tconst quadVertices = new Float32Array([-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1]);\n\n\t\tthis.buffer = this.gl.createBuffer();\n\t\tthis.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.buffer);\n\t\tthis.gl.bufferData(this.gl.ARRAY_BUFFER, quadVertices, this.gl.STATIC_DRAW);\n\t\tthis.gl.viewport(0, 0, this.canvas.width, this.canvas.height);\n\t\tthis.gl.enableVertexAttribArray(aPosition);\n\t\tthis.gl.vertexAttribPointer(aPosition, 2, this.gl.FLOAT, false, 0, 0);\n\t}\n\n\tprivate throttledHandleResize() {\n\t\tclearTimeout(this.resizeTimeout);\n\t\tconst now = performance.now();\n\t\tconst timeUntilNextResize = this.lastResizeTime + RESIZE_THROTTLE_INTERVAL - now;\n\t\tif (timeUntilNextResize <= 0) {\n\t\t\tthis.lastResizeTime = now;\n\t\t\tthis.handleResize();\n\t\t} else {\n\t\t\tthis.resizeTimeout = setTimeout(() => this.throttledHandleResize(), timeUntilNextResize);\n\t\t}\n\t}\n\n\t// TODO: This breaks for `position: fixed; inset: 0` canvases.\n\tprivate handleResize() {\n\t\tconst pixelRatio = window.devicePixelRatio || 1;\n\t\tconst width = this.canvas.clientWidth * pixelRatio;\n\t\tconst height = this.canvas.clientHeight * pixelRatio;\n\n\t\tconst computedStyle = getComputedStyle(this.canvas);\n\t\tconst hasExplicitWidth = computedStyle.width !== `${this.canvas.width}px` && computedStyle.width !== 'auto';\n\t\tconst hasExplicitHeight = computedStyle.height !== `${this.canvas.height}px` && computedStyle.height !== 'auto';\n\t\tif (!hasExplicitWidth || !hasExplicitHeight) {\n\t\t\tthis.canvas.style.width = `${this.canvas.clientWidth}px`;\n\t\t\tthis.canvas.style.height = `${this.canvas.clientHeight}px`;\n\t\t}\n\n\t\tif (this.canvas.width !== width || this.canvas.height !== height) {\n\t\t\tthis.canvas.width = width;\n\t\t\tthis.canvas.height = height;\n\t\t\tthis.gl.viewport(0, 0, this.gl.drawingBufferWidth, this.gl.drawingBufferHeight);\n\t\t\tif (this.uniforms.has('u_resolution')) {\n\t\t\t\tthis.updateUniforms({ u_resolution: [this.canvas.width, this.canvas.height] });\n\t\t\t}\n\n\t\t\t// Delete and recreate history buffer, since the canvas size won’t be correct anymore.\n\t\t\tif (this.historyLength > 0 && this.historyTexture) {\n\t\t\t\tthis.gl.deleteTexture(this.historyTexture);\n\t\t\t\tthis.initializeHistoryBuffer();\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate addEventListeners() {\n\t\tconst updateCursor = (x: number, y: number) => {\n\t\t\tif (!this.uniforms.has('u_cursor')) return;\n\t\t\tconst rect = this.canvas.getBoundingClientRect();\n\t\t\tthis.cursorPosition[0] = (x - rect.left) / rect.width;\n\t\t\tthis.cursorPosition[1] = 1 - (y - rect.top) / rect.height; // Flip Y for WebGL\n\t\t\tthis.updateUniforms({\n\t\t\t\tu_cursor: [this.cursorPosition[0], this.cursorPosition[1], this.scrollX, this.scrollY],\n\t\t\t});\n\t\t};\n\n\t\tconst updateClick = (isMouseDown: boolean, x?: number, y?: number) => {\n\t\t\tif (!this.uniforms.has('u_click')) return;\n\t\t\tthis.isMouseDown = isMouseDown;\n\t\t\tif (isMouseDown) {\n\t\t\t\tconst rect = this.canvas.getBoundingClientRect();\n\t\t\t\tconst xVal = x as number;\n\t\t\t\tconst yVal = y as number;\n\t\t\t\tthis.clickPosition[0] = (xVal - rect.left) / rect.width;\n\t\t\t\tthis.clickPosition[1] = 1 - (yVal - rect.top) / rect.height; // Flip Y for WebGL\n\t\t\t}\n\t\t\tthis.updateUniforms({\n\t\t\t\tu_click: [this.clickPosition[0], this.clickPosition[1], this.isMouseDown ? 1.0 : 0.0],\n\t\t\t});\n\t\t};\n\n\t\tthis.eventListeners.set('mousemove', event => {\n\t\t\tconst mouseEvent = event as MouseEvent;\n\t\t\tif (!this.isTouchDevice) {\n\t\t\t\tupdateCursor(mouseEvent.clientX, mouseEvent.clientY);\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('mousedown', event => {\n\t\t\tconst mouseEvent = event as MouseEvent;\n\t\t\tif (!this.isTouchDevice) {\n\t\t\t\tif (mouseEvent.button === 0) {\n\t\t\t\t\tthis.isMouseDown = true;\n\t\t\t\t\tupdateClick(true, mouseEvent.clientX, mouseEvent.clientY);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('mouseup', event => {\n\t\t\tconst mouseEvent = event as MouseEvent;\n\t\t\tif (!this.isTouchDevice) {\n\t\t\t\tif (mouseEvent.button === 0) {\n\t\t\t\t\tupdateClick(false);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('wheel', event => {\n\t\t\tconst wheelEvent = event as WheelEvent;\n\t\t\tthis.scrollX += wheelEvent.deltaX * 0.01;\n\t\t\tthis.scrollY += wheelEvent.deltaY * 0.01;\n\t\t\tupdateCursor(wheelEvent.clientX, wheelEvent.clientY);\n\t\t});\n\n\t\tthis.eventListeners.set('touchmove', event => {\n\t\t\tconst touchEvent = event as TouchEvent;\n\t\t\tif (touchEvent.touches.length > 0) {\n\t\t\t\tupdateCursor(touchEvent.touches[0].clientX, touchEvent.touches[0].clientY);\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('touchstart', event => {\n\t\t\tconst touchEvent = event as TouchEvent;\n\t\t\tthis.isTouchDevice = true;\n\t\t\tif (touchEvent.touches.length > 0) {\n\t\t\t\tupdateClick(true, touchEvent.touches[0].clientX, touchEvent.touches[0].clientY);\n\t\t\t\tupdateCursor(touchEvent.touches[0].clientX, touchEvent.touches[0].clientY);\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('touchend', event => {\n\t\t\tconst touchEvent = event as TouchEvent;\n\t\t\tif (touchEvent.touches.length === 0) {\n\t\t\t\tupdateClick(false);\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.forEach((listener, event) => {\n\t\t\tthis.canvas.addEventListener(event, listener);\n\t\t});\n\t}\n\n\tinitializeUniform(name: string, type: 'float' | 'int', value: number | number[]) {\n\t\tif (this.uniforms.has(name)) {\n\t\t\tthrow new Error(`Uniform '${name}' is already initialized.`);\n\t\t}\n\n\t\tif (type !== 'float' && type !== 'int') {\n\t\t\tthrow new Error(`Invalid uniform type: ${type}. Expected 'float' or 'int'.`);\n\t\t}\n\n\t\tconst location = this.gl.getUniformLocation(this.program!, name);\n\t\tif (!location) {\n\t\t\tconsole.debug(`Uniform ${name} not found in fragment shader. Skipping initialization.`);\n\t\t\treturn;\n\t\t}\n\n\t\tif (!Array.isArray(value)) {\n\t\t\tvalue = [value];\n\t\t}\n\t\tif (value.length < 1 || value.length > 4) {\n\t\t\tthrow new Error(`Invalid uniform value length: ${value.length}. Expected a length between 1 and 4.`);\n\t\t}\n\n\t\tconst length = value.length as 1 | 2 | 3 | 4;\n\t\tthis.uniforms.set(name, { type, length, location });\n\t\tthis.updateUniforms({ [name]: value });\n\t}\n\n\tupdateUniforms(updates: Record<string, number | number[]>) {\n\t\tObject.entries(updates).forEach(([name, value]: [string, number | number[]]) => {\n\t\t\tif (!this.uniforms.has(name)) {\n\t\t\t\tthrow new Error(`Uniform '${name}' is not initialized.`);\n\t\t\t}\n\n\t\t\tconst uniform = this.uniforms.get(name)!;\n\t\t\tif (!Array.isArray(value)) {\n\t\t\t\tvalue = [value];\n\t\t\t}\n\t\t\tif (value.length !== uniform.length) {\n\t\t\t\tthrow new Error(`Invalid uniform value length: ${value.length}. Expected ${uniform.length}.`);\n\t\t\t}\n\t\t\t(this.gl as any)[`uniform${uniform.length}${uniform.type.charAt(0)}`](uniform.location, ...value);\n\t\t});\n\t}\n\n\tstep(time: number) {\n\t\tconst gl = this.gl;\n\n\t\tif (this.uniforms.has('u_time')) {\n\t\t\tthis.updateUniforms({ u_time: time });\n\t\t}\n\t\tif (this.uniforms.has('u_frame')) {\n\t\t\tthis.updateUniforms({ u_frame: this.frame });\n\t\t}\n\n\t\tif (this.historyLength > 0) {\n\t\t\tconst writeIdx = this.frame % this.historyLength;\n\n\t\t\tgl.bindFramebuffer(gl.FRAMEBUFFER, null);\n\t\t\tgl.viewport(0, 0, this.canvas.width, this.canvas.height);\n\t\t\tgl.clear(gl.COLOR_BUFFER_BIT);\n\t\t\tgl.drawArrays(gl.TRIANGLES, 0, 6);\n\n\t\t\tgl.activeTexture(gl.TEXTURE0);\n\t\t\tgl.bindTexture(gl.TEXTURE_2D_ARRAY, this.historyTexture);\n\t\t\tgl.copyTexSubImage3D(gl.TEXTURE_2D_ARRAY, 0, 0, 0, writeIdx, 0, 0, this.canvas.width, this.canvas.height);\n\t\t} else {\n\t\t\tgl.clear(gl.COLOR_BUFFER_BIT);\n\t\t\tgl.drawArrays(gl.TRIANGLES, 0, 6);\n\t\t}\n\n\t\t++this.frame;\n\t}\n\n\tplay(callback?: (time: number, frame: number) => void) {\n\t\tconst loop = (time: number) => {\n\t\t\ttime /= 1000; // Convert from milliseconds to seconds.\n\t\t\tthis.step(time);\n\t\t\tif (callback) callback(time, this.frame);\n\t\t\tthis.animationFrameId = requestAnimationFrame(loop);\n\t\t};\n\t\tthis.animationFrameId = requestAnimationFrame(loop);\n\t}\n\n\tpause() {\n\t\tif (this.animationFrameId) {\n\t\t\tcancelAnimationFrame(this.animationFrameId);\n\t\t\tthis.animationFrameId = null;\n\t\t}\n\t}\n\n\tsave(filename: string) {\n\t\tthis.gl.clear(this.gl.COLOR_BUFFER_BIT);\n\t\tthis.gl.drawArrays(this.gl.TRIANGLES, 0, 6);\n\n\t\tconst image = this.canvas.toDataURL();\n\t\tif (filename && !`${filename}`.toLowerCase().endsWith('.png')) {\n\t\t\tfilename = `${filename}.png`;\n\t\t}\n\t\tthis.downloadLink.download = filename || 'export.png';\n\t\tthis.downloadLink.href = image;\n\t\tthis.downloadLink.click();\n\t}\n\n\tinitializeTexture(name: string, source: HTMLImageElement | HTMLVideoElement) {\n\t\tif (this.textures.has(name)) {\n\t\t\tthrow new Error(`Texture '${name}' is already initialized.`);\n\t\t}\n\n\t\tconst texture = this.gl.createTexture();\n\t\tif (!texture) {\n\t\t\tthrow new Error('Failed to create texture');\n\t\t}\n\t\tconst unitIndex = this.textures.size + 1; // Start from unit 1 to avoid conflict with history texture at unit 0.\n\t\tthis.gl.activeTexture(this.gl.TEXTURE0 + unitIndex);\n\t\tthis.gl.bindTexture(this.gl.TEXTURE_2D, texture);\n\n\t\t// Flip the texture vertically since v_uv is flipped, and set up filters and wrapping.\n\t\tthis.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, true);\n\t\tthis.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, this.gl.CLAMP_TO_EDGE);\n\t\tthis.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, this.gl.CLAMP_TO_EDGE);\n\t\tthis.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR);\n\t\tthis.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR);\n\n\t\tthis.textures.set(name, { texture, unitIndex });\n\t\tthis.updateTextures({ [name]: source });\n\n\t\tconst uSampler = this.gl.getUniformLocation(this.program!, name);\n\t\tif (uSampler) {\n\t\t\tthis.gl.uniform1i(uSampler, unitIndex);\n\t\t}\n\t}\n\n\tupdateTextures(updates: Record<string, HTMLImageElement | HTMLVideoElement>) {\n\t\tObject.entries(updates).forEach(([name, source]) => {\n\t\t\tconst info = this.textures.get(name);\n\t\t\tif (!info) {\n\t\t\t\tthrow new Error(`Texture '${name}' is not initialized.`);\n\t\t\t}\n\t\t\tthis.gl.activeTexture(this.gl.TEXTURE0 + info.unitIndex);\n\t\t\tthis.gl.bindTexture(this.gl.TEXTURE_2D, info.texture);\n\t\t\tthis.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, source);\n\t\t});\n\t}\n\n\tdestroy() {\n\t\tif (this.animationFrameId) {\n\t\t\tcancelAnimationFrame(this.animationFrameId);\n\t\t\tthis.animationFrameId = null;\n\t\t}\n\n\t\tthis.resizeObserver.unobserve(this.canvas);\n\t\tthis.eventListeners.forEach((listener, event) => {\n\t\t\tthis.canvas.removeEventListener(event, listener);\n\t\t});\n\n\t\tif (this.program) {\n\t\t\tthis.gl.deleteProgram(this.program);\n\t\t}\n\n\t\tthis.textures.forEach(texture => {\n\t\t\tthis.gl.deleteTexture(texture.texture);\n\t\t});\n\n\t\tif (this.historyTexture) {\n\t\t\tthis.gl.deleteTexture(this.historyTexture);\n\t\t\tthis.historyTexture = null;\n\t\t}\n\n\t\tif (this.buffer) {\n\t\t\tthis.gl.deleteBuffer(this.buffer);\n\t\t\tthis.buffer = null;\n\t\t}\n\n\t\tif (this.isInternalCanvas) {\n\t\t\tthis.canvas.remove();\n\t\t}\n\t}\n}\n\nexport default ShaderPad;\n"],"mappings":"AAAA,IAAMA,EAA4B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQ5BC,EAA2B,mBAkB3BC,EAAN,KAAgB,CAyBf,YAAYC,EAA2BC,EAAmB,CAAC,EAAG,CAxB9D,KAAQ,iBAAmB,GAC3B,KAAQ,cAAgB,GAKxB,KAAQ,SAAiC,IAAI,IAC7C,KAAQ,SAAiC,IAAI,IAC7C,KAAQ,OAA6B,KACrC,KAAQ,QAA+B,KAGvC,KAAQ,cAAgC,KACxC,KAAQ,eAAiB,EACzB,KAAQ,eAA6C,IAAI,IACzD,KAAQ,MAAQ,EAChB,KAAQ,eAAiB,CAAC,GAAK,EAAG,EAClC,KAAQ,QAAU,EAClB,KAAQ,QAAU,EAClB,KAAQ,cAAgB,CAAC,GAAK,EAAG,EACjC,KAAQ,YAAc,GAEtB,KAAQ,eAAsC,KAe7C,GAZA,KAAK,OAASA,EAAQ,QAAU,SAAS,cAAc,QAAQ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AK,MAAM,EAChC,KAAK,OAAS,MAGX,KAAK,kBACR,KAAK,OAAO,OAAO,CAErB,CACD,EAEOI,EAAQnD","names":["DEFAULT_VERTEX_SHADER_SRC","RESIZE_THROTTLE_INTERVAL","ShaderPad","fragmentShaderSrc","options","vertexShaderSrc","vertexShader","fragmentShader","aPosition","gl","transparent","layer","type","source","shader","quadVertices","now","timeUntilNextResize","pixelRatio","width","height","computedStyle","hasExplicitWidth","hasExplicitHeight","updateCursor","x","y","rect","updateClick","isMouseDown","xVal","yVal","event","mouseEvent","wheelEvent","touchEvent","listener","name","value","location","length","updates","uniform","time","writeIdx","callback","loop","filename","image","texture","unitIndex","uSampler","info","index_default"]}
|
|
1
|
+
{"version":3,"sources":["../src/plugins/history.ts","../src/plugins/save.ts","../src/index.ts"],"sourcesContent":["import ShaderPad, { PluginContext, TextureSource } from '../index';\n\nexport type WithHistory<T extends ShaderPad> = T & {\n\tinitializeTexture(name: string, source: HTMLImageElement | HTMLVideoElement, historyDepth?: number): void;\n};\n\nfunction getSourceDimensions(source: TextureSource) {\n\tconst video = source as HTMLVideoElement;\n\tconst image = source as HTMLImageElement;\n\tconst width = video.videoWidth || image.naturalWidth || image.width;\n\tconst height = video.videoHeight || image.naturalHeight || image.height;\n\treturn { width, height };\n}\n\nfunction clearHistoryTextureLayers(gl: WebGL2RenderingContext, width: number, height: number, depth: number): void {\n\tconst transparent = new Uint8Array(width * height * 4);\n\tfor (let layer = 0; layer < depth; ++layer) {\n\t\tgl.texSubImage3D(gl.TEXTURE_2D_ARRAY, 0, 0, 0, layer, width, height, 1, gl.RGBA, gl.UNSIGNED_BYTE, transparent);\n\t}\n}\n\nfunction createHistoryTexture(gl: WebGL2RenderingContext, depth: number, width: number, height: number): WebGLTexture {\n\tconst texture = gl.createTexture();\n\tif (!texture) {\n\t\tthrow new Error('Failed to create history texture');\n\t}\n\tgl.activeTexture(gl.TEXTURE0);\n\tgl.bindTexture(gl.TEXTURE_2D_ARRAY, texture);\n\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.LINEAR);\n\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MAG_FILTER, gl.LINEAR);\n\tgl.texStorage3D(gl.TEXTURE_2D_ARRAY, 1, gl.RGBA8, width, height, depth);\n\tclearHistoryTextureLayers(gl, width, height, depth);\n\treturn texture;\n}\n\nexport function history(framebufferDepth: number = 1) {\n\treturn function (shaderPad: ShaderPad, context: PluginContext) {\n\t\tconst { gl, uniforms, textures, reserveTextureUnit } = context;\n\t\tconst shaderPadWithHistory = shaderPad as WithHistory<ShaderPad>;\n\n\t\tconst originalInitializeTexture = shaderPad.initializeTexture.bind(shaderPad);\n\t\tconst originalUpdateTextures = shaderPad.updateTextures.bind(shaderPad);\n\n\t\tlet framebufferHistoryTexture: WebGLTexture | null = null;\n\t\tconst shouldTrackFramebuffer = framebufferDepth > 1;\n\t\tif (shouldTrackFramebuffer) {\n\t\t\tfunction initializeFramebufferHistory() {\n\t\t\t\tframebufferHistoryTexture = createHistoryTexture(\n\t\t\t\t\tgl,\n\t\t\t\t\tframebufferDepth,\n\t\t\t\t\tgl.drawingBufferWidth,\n\t\t\t\t\tgl.drawingBufferHeight\n\t\t\t\t);\n\n\t\t\t\tif (!uniforms.has('u_history')) {\n\t\t\t\t\tshaderPad.initializeUniform('u_history', 'int', 0);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tfunction clearFramebufferHistory() {\n\t\t\t\tif (!framebufferHistoryTexture) return;\n\t\t\t\tgl.activeTexture(gl.TEXTURE0);\n\t\t\t\tgl.bindTexture(gl.TEXTURE_2D_ARRAY, framebufferHistoryTexture);\n\t\t\t\tclearHistoryTextureLayers(gl, gl.drawingBufferWidth, gl.drawingBufferHeight, framebufferDepth);\n\t\t\t}\n\n\t\t\tshaderPad.registerHook('init', initializeFramebufferHistory);\n\t\t\tshaderPad.registerHook('step', (_time: number, frame: number) => {\n\t\t\t\tif (!framebufferHistoryTexture) return;\n\t\t\t\tconst writeIdx = frame % framebufferDepth;\n\t\t\t\tgl.activeTexture(gl.TEXTURE0);\n\t\t\t\tgl.bindTexture(gl.TEXTURE_2D_ARRAY, framebufferHistoryTexture);\n\t\t\t\tgl.copyTexSubImage3D(\n\t\t\t\t\tgl.TEXTURE_2D_ARRAY,\n\t\t\t\t\t0,\n\t\t\t\t\t0,\n\t\t\t\t\t0,\n\t\t\t\t\twriteIdx,\n\t\t\t\t\t0,\n\t\t\t\t\t0,\n\t\t\t\t\tgl.drawingBufferWidth,\n\t\t\t\t\tgl.drawingBufferHeight\n\t\t\t\t);\n\t\t\t});\n\t\t\tshaderPad.registerHook('updateResolution', () => {\n\t\t\t\tif (framebufferHistoryTexture) {\n\t\t\t\t\tgl.deleteTexture(framebufferHistoryTexture);\n\t\t\t\t}\n\t\t\t\tinitializeFramebufferHistory();\n\t\t\t});\n\t\t\tshaderPad.registerHook('reset', clearFramebufferHistory);\n\t\t}\n\t\tshaderPad.registerHook('reset', () => {\n\t\t\ttextures.forEach(textureInfo => {\n\t\t\t\tif (textureInfo.historyDepth && textureInfo.historyDepth > 1 && textureInfo.writeIndex !== undefined) {\n\t\t\t\t\ttextureInfo.writeIndex = 0;\n\t\t\t\t}\n\t\t\t});\n\t\t});\n\n\t\tshaderPadWithHistory.initializeTexture = function (\n\t\t\tname: string,\n\t\t\tsource: TextureSource,\n\t\t\thistoryDepth: number = 1\n\t\t) {\n\t\t\tconst { width, height } = getSourceDimensions(source);\n\t\t\tif (!width || !height) {\n\t\t\t\tthrow new Error(`Texture source must have valid dimensions`);\n\t\t\t}\n\n\t\t\tconst depth = Math.floor(historyDepth);\n\t\t\tif (depth <= 1) {\n\t\t\t\treturn originalInitializeTexture(name, source);\n\t\t\t}\n\n\t\t\tif (textures.has(name)) {\n\t\t\t\tthrow new Error(`Texture '${name}' is already initialized.`);\n\t\t\t}\n\n\t\t\tlet unitIndex: number | undefined;\n\t\t\tlet texture: WebGLTexture | null = null;\n\t\t\ttry {\n\t\t\t\tunitIndex = reserveTextureUnit(name);\n\t\t\t\ttexture = createHistoryTexture(gl, depth, width, height);\n\t\t\t\tgl.activeTexture(gl.TEXTURE0 + unitIndex);\n\t\t\t\tgl.bindTexture(gl.TEXTURE_2D_ARRAY, texture);\n\n\t\t\t\t// Flip the texture vertically during upload to match WebGL's coordinate system.\n\t\t\t\t// WebGL uses bottom-left origin, while images/videos use top-left origin.\n\t\t\t\tgl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);\n\t\t\t\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n\t\t\t\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n\t\t\t\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MIN_FILTER, gl.LINEAR);\n\t\t\t\tgl.texParameteri(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_MAG_FILTER, gl.LINEAR);\n\n\t\t\t\tif (context.program) {\n\t\t\t\t\tconst uSampler = gl.getUniformLocation(context.program, name);\n\t\t\t\t\tif (uSampler) {\n\t\t\t\t\t\tgl.uniform1i(uSampler, unitIndex);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\ttextures.set(name, {\n\t\t\t\t\ttexture,\n\t\t\t\t\tunitIndex,\n\t\t\t\t\thistoryDepth: depth,\n\t\t\t\t\twriteIndex: 0,\n\t\t\t\t});\n\n\t\t\t\tshaderPadWithHistory.updateTextures({ [name]: source });\n\t\t\t} catch (error) {\n\t\t\t\tif (texture) gl.deleteTexture(texture);\n\t\t\t\tif (unitIndex !== undefined) {\n\t\t\t\t\tcontext.releaseTextureUnit(name);\n\t\t\t\t}\n\t\t\t\tthrow error;\n\t\t\t}\n\t\t};\n\n\t\tshaderPadWithHistory.updateTextures = function (updates) {\n\t\t\tObject.entries(updates).forEach(([name, source]) => {\n\t\t\t\tconst textureInfo = textures.get(name);\n\t\t\t\tif (!textureInfo || !textureInfo.historyDepth || textureInfo.historyDepth <= 1) {\n\t\t\t\t\treturn originalUpdateTextures({ [name]: source });\n\t\t\t\t}\n\n\t\t\t\tconst { width, height } = getSourceDimensions(source);\n\t\t\t\tconst writeIndex = textureInfo.writeIndex ?? 0;\n\t\t\t\tgl.activeTexture(gl.TEXTURE0 + textureInfo.unitIndex);\n\t\t\t\tgl.bindTexture(gl.TEXTURE_2D_ARRAY, textureInfo.texture);\n\t\t\t\tgl.texSubImage3D(\n\t\t\t\t\tgl.TEXTURE_2D_ARRAY,\n\t\t\t\t\t0,\n\t\t\t\t\t0,\n\t\t\t\t\t0,\n\t\t\t\t\twriteIndex,\n\t\t\t\t\twidth,\n\t\t\t\t\theight,\n\t\t\t\t\t1,\n\t\t\t\t\tgl.RGBA,\n\t\t\t\t\tgl.UNSIGNED_BYTE,\n\t\t\t\t\tsource\n\t\t\t\t);\n\t\t\t\ttextureInfo.writeIndex = (writeIndex + 1) % textureInfo.historyDepth;\n\t\t\t});\n\t\t};\n\n\t\tshaderPad.registerHook('destroy', () => {\n\t\t\tif (framebufferHistoryTexture) {\n\t\t\t\tgl.deleteTexture(framebufferHistoryTexture);\n\t\t\t}\n\t\t});\n\t};\n}\n","import ShaderPad, { PluginContext } from '../index';\n\ndeclare module '../index' {\n\tinterface ShaderPad {\n\t\tsave(filename: string): Promise<void>;\n\t}\n}\n\nexport function save() {\n\treturn function (shaderPad: ShaderPad, context: PluginContext) {\n\t\tconst { gl, canvas } = context;\n\t\tconst downloadLink = document.createElement('a');\n\n\t\t(shaderPad as any).save = async function (filename: string) {\n\t\t\tgl.clear(gl.COLOR_BUFFER_BIT);\n\t\t\tgl.drawArrays(gl.TRIANGLES, 0, 6);\n\n\t\t\tif (filename && !`${filename}`.toLowerCase().endsWith('.png')) {\n\t\t\t\tfilename = `${filename}.png`;\n\t\t\t}\n\t\t\tfilename = filename || 'export.png';\n\t\t\tif ('ongesturechange' in window) {\n\t\t\t\t// Mobile.\n\t\t\t\ttry {\n\t\t\t\t\tconst blob: Blob = await new Promise(resolve =>\n\t\t\t\t\t\tcanvas.toBlob(resolve as BlobCallback, 'image/png')\n\t\t\t\t\t);\n\t\t\t\t\tconst file = new File([blob], filename, { type: blob.type });\n\n\t\t\t\t\tif (navigator.canShare?.({ files: [file] })) {\n\t\t\t\t\t\tawait navigator.share({ files: [file] });\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconsole.warn('Web Share API failed:', error);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// Desktop.\n\t\t\t\tdownloadLink.download = filename;\n\t\t\t\tdownloadLink.href = canvas.toDataURL();\n\t\t\t\tdownloadLink.click();\n\t\t\t}\n\t\t};\n\t};\n}\n\n// Type helper.\nexport type WithSave<T extends ShaderPad> = T & {\n\tsave(filename: string): Promise<void>;\n};\n","const DEFAULT_VERTEX_SHADER_SRC = `#version 300 es\nin vec2 aPosition;\nout vec2 v_uv;\nvoid main() {\n v_uv = aPosition * 0.5 + 0.5;\n gl_Position = vec4(aPosition, 0.0, 1.0);\n}\n`;\nconst RESIZE_THROTTLE_INTERVAL = 1000 / 30;\n\ninterface Uniform {\n\ttype: 'float' | 'int';\n\tlength: 1 | 2 | 3 | 4;\n\tlocation: WebGLUniformLocation;\n}\n\ninterface Texture {\n\ttexture: WebGLTexture;\n\tunitIndex: number;\n\thistoryDepth?: number;\n\twriteIndex?: number;\n}\n\nexport type TextureSource = HTMLImageElement | HTMLVideoElement;\n\nexport interface PluginContext {\n\tgl: WebGL2RenderingContext;\n\tuniforms: Map<string, Uniform>;\n\ttextures: Map<string, Texture>;\n\tget program(): WebGLProgram | null;\n\tcanvas: HTMLCanvasElement;\n\treserveTextureUnit: (name: string) => number;\n\treleaseTextureUnit: (name: string) => void;\n}\n\ntype Plugin = (shaderPad: ShaderPad, context: PluginContext) => void;\n\ntype LifecycleMethod = 'init' | 'step' | 'destroy' | 'updateResolution' | 'reset';\n\ninterface Options {\n\tcanvas?: HTMLCanvasElement | null;\n\tplugins?: Plugin[];\n}\n\ntype TextureUnitPool = {\n\tfree: number[];\n\tnext: number;\n\tmax: number;\n};\n\nclass ShaderPad {\n\tprivate isInternalCanvas = false;\n\tprivate isTouchDevice = false;\n\tprivate gl: WebGL2RenderingContext;\n\tprivate downloadLink: HTMLAnchorElement;\n\tprivate fragmentShaderSrc: string;\n\tprivate uniforms: Map<string, Uniform> = new Map();\n\tprivate textures: Map<string, Texture> = new Map();\n\tprivate textureUnitPool: TextureUnitPool;\n\tprivate buffer: WebGLBuffer | null = null;\n\tprivate program: WebGLProgram | null = null;\n\tprivate animationFrameId: number | null;\n\tprivate resolutionObserver: MutationObserver;\n\tprivate resizeObserver: ResizeObserver;\n\tprivate resizeTimeout: NodeJS.Timeout = null as unknown as NodeJS.Timeout;\n\tprivate lastResizeTime = 0;\n\tprivate eventListeners: Map<string, EventListener> = new Map();\n\tprivate frame = 0;\n\tprivate startTime = 0;\n\tprivate cursorPosition = [0.5, 0.5];\n\tprivate clickPosition = [0.5, 0.5];\n\tprivate isMouseDown = false;\n\tpublic canvas: HTMLCanvasElement;\n\tpublic onResize?: (width: number, height: number) => void;\n\tprivate hooks: Map<LifecycleMethod, Function[]> = new Map();\n\n\tconstructor(fragmentShaderSrc: string, options: Options = {}) {\n\t\tthis.canvas = options.canvas || document.createElement('canvas');\n\t\tif (!options.canvas) {\n\t\t\tthis.isInternalCanvas = true;\n\t\t\tdocument.body.appendChild(this.canvas);\n\t\t\tthis.canvas.style.position = 'fixed';\n\t\t\tthis.canvas.style.inset = '0';\n\t\t\tthis.canvas.style.height = '100dvh';\n\t\t\tthis.canvas.style.width = '100dvw';\n\t\t}\n\n\t\tthis.gl = this.canvas.getContext('webgl2', { antialias: false }) as WebGL2RenderingContext;\n\t\tif (!this.gl) {\n\t\t\tthrow new Error('WebGL2 not supported. Please use a browser that supports WebGL2.');\n\t\t}\n\n\t\tthis.textureUnitPool = {\n\t\t\tfree: [],\n\t\t\tnext: 1, // Start from unit 1 to avoid conflict with history texture at unit 0.\n\t\t\tmax: this.gl.getParameter(this.gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS),\n\t\t};\n\n\t\tthis.downloadLink = document.createElement('a');\n\t\tthis.fragmentShaderSrc = fragmentShaderSrc;\n\t\tthis.animationFrameId = null;\n\t\tthis.resolutionObserver = new MutationObserver(() => this.updateResolution());\n\t\tthis.resizeObserver = new ResizeObserver(() => this.throttledHandleResize());\n\n\t\tif (options.plugins) {\n\t\t\tconst context: PluginContext = {\n\t\t\t\tgl: this.gl,\n\t\t\t\tuniforms: this.uniforms,\n\t\t\t\ttextures: this.textures,\n\t\t\t\tcanvas: this.canvas,\n\t\t\t\treserveTextureUnit: this.reserveTextureUnit.bind(this),\n\t\t\t\treleaseTextureUnit: this.releaseTextureUnit.bind(this),\n\t\t\t} as PluginContext;\n\t\t\t// Define program as a getter so it always returns the current program.\n\t\t\tObject.defineProperty(context, 'program', {\n\t\t\t\tget: () => this.program,\n\t\t\t\tenumerable: true,\n\t\t\t\tconfigurable: true,\n\t\t\t});\n\t\t\toptions.plugins.forEach(plugin => plugin(this, context));\n\t\t}\n\t\tthis.init();\n\t\tthis.addEventListeners();\n\t}\n\n\tregisterHook(name: LifecycleMethod, fn: Function) {\n\t\tif (!this.hooks.has(name)) {\n\t\t\tthis.hooks.set(name, []);\n\t\t}\n\t\tthis.hooks.get(name)!.push(fn);\n\t}\n\n\tprivate init() {\n\t\tconst vertexShaderSrc = DEFAULT_VERTEX_SHADER_SRC;\n\n\t\tthis.program = this.gl.createProgram();\n\t\tif (!this.program) {\n\t\t\tthrow new Error('Failed to create WebGL program');\n\t\t}\n\t\tconst vertexShader = this.createShader(this.gl.VERTEX_SHADER, vertexShaderSrc);\n\t\tconst fragmentShader = this.createShader(this.gl.FRAGMENT_SHADER, this.fragmentShaderSrc);\n\n\t\tthis.gl.attachShader(this.program, vertexShader);\n\t\tthis.gl.attachShader(this.program, fragmentShader);\n\t\tthis.gl.linkProgram(this.program);\n\t\tthis.gl.deleteShader(vertexShader);\n\t\tthis.gl.deleteShader(fragmentShader);\n\n\t\tif (!this.gl.getProgramParameter(this.program, this.gl.LINK_STATUS)) {\n\t\t\tconsole.error('Program link error:', this.gl.getProgramInfoLog(this.program));\n\t\t\tthis.gl.deleteProgram(this.program);\n\t\t\tthrow new Error('Failed to link WebGL program');\n\t\t}\n\n\t\tconst aPosition = this.gl.getAttribLocation(this.program, 'aPosition');\n\t\tthis.setupBuffer(aPosition);\n\n\t\tthis.gl.useProgram(this.program);\n\n\t\tthis.resolutionObserver.observe(this.canvas, { attributes: true, attributeFilter: ['width', 'height'] });\n\t\tthis.resizeObserver.observe(this.canvas);\n\n\t\tif (!this.isInternalCanvas) {\n\t\t\tthis.updateResolution();\n\t\t}\n\t\tthis.initializeUniform('u_cursor', 'float', this.cursorPosition);\n\t\tthis.initializeUniform('u_click', 'float', [...this.clickPosition, this.isMouseDown ? 1.0 : 0.0]);\n\t\tthis.initializeUniform('u_time', 'float', 0);\n\t\tthis.initializeUniform('u_frame', 'int', 0);\n\n\t\tthis.hooks.get('init')?.forEach(hook => hook.call(this));\n\t}\n\n\tprivate createShader(type: number, source: string): WebGLShader {\n\t\tconst shader = this.gl.createShader(type)!;\n\t\tthis.gl.shaderSource(shader, source);\n\t\tthis.gl.compileShader(shader);\n\t\tif (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS)) {\n\t\t\tconsole.error('Shader compilation failed:', source);\n\t\t\tconsole.error(this.gl.getShaderInfoLog(shader));\n\t\t\tthis.gl.deleteShader(shader);\n\t\t\tthrow new Error('Shader compilation failed');\n\t\t}\n\t\treturn shader;\n\t}\n\n\tprivate setupBuffer(aPosition: number) {\n\t\tconst quadVertices = new Float32Array([-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1]);\n\n\t\tthis.buffer = this.gl.createBuffer();\n\t\tthis.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.buffer);\n\t\tthis.gl.bufferData(this.gl.ARRAY_BUFFER, quadVertices, this.gl.STATIC_DRAW);\n\t\tthis.gl.viewport(0, 0, this.gl.drawingBufferWidth, this.gl.drawingBufferHeight);\n\t\tthis.gl.enableVertexAttribArray(aPosition);\n\t\tthis.gl.vertexAttribPointer(aPosition, 2, this.gl.FLOAT, false, 0, 0);\n\t}\n\n\tprivate throttledHandleResize() {\n\t\tclearTimeout(this.resizeTimeout);\n\t\tconst now = performance.now();\n\t\tconst timeUntilNextResize = this.lastResizeTime + RESIZE_THROTTLE_INTERVAL - now;\n\t\tif (timeUntilNextResize <= 0) {\n\t\t\tthis.lastResizeTime = now;\n\t\t\tthis.handleResize();\n\t\t} else {\n\t\t\tthis.resizeTimeout = setTimeout(() => this.throttledHandleResize(), timeUntilNextResize);\n\t\t}\n\t}\n\n\tprivate handleResize() {\n\t\tconst pixelRatio = window.devicePixelRatio || 1;\n\t\tconst width = this.canvas.clientWidth * pixelRatio;\n\t\tconst height = this.canvas.clientHeight * pixelRatio;\n\t\tif (this.isInternalCanvas && (this.canvas.width !== width || this.canvas.height !== height)) {\n\t\t\tthis.canvas.width = width;\n\t\t\tthis.canvas.height = height;\n\t\t}\n\t\tthis.onResize?.(width, height);\n\t}\n\n\tprivate addEventListeners() {\n\t\tconst updateCursor = (x: number, y: number) => {\n\t\t\tif (!this.uniforms.has('u_cursor')) return;\n\t\t\tconst rect = this.canvas.getBoundingClientRect();\n\t\t\tthis.cursorPosition[0] = (x - rect.left) / rect.width;\n\t\t\tthis.cursorPosition[1] = 1 - (y - rect.top) / rect.height; // Flip Y for WebGL\n\t\t\tthis.updateUniforms({ u_cursor: this.cursorPosition });\n\t\t};\n\n\t\tconst updateClick = (isMouseDown: boolean, x?: number, y?: number) => {\n\t\t\tif (!this.uniforms.has('u_click')) return;\n\t\t\tthis.isMouseDown = isMouseDown;\n\t\t\tif (isMouseDown) {\n\t\t\t\tconst rect = this.canvas.getBoundingClientRect();\n\t\t\t\tconst xVal = x as number;\n\t\t\t\tconst yVal = y as number;\n\t\t\t\tthis.clickPosition[0] = (xVal - rect.left) / rect.width;\n\t\t\t\tthis.clickPosition[1] = 1 - (yVal - rect.top) / rect.height; // Flip Y for WebGL\n\t\t\t}\n\t\t\tthis.updateUniforms({ u_click: [...this.clickPosition, this.isMouseDown ? 1.0 : 0.0] });\n\t\t};\n\n\t\tthis.eventListeners.set('mousemove', event => {\n\t\t\tconst mouseEvent = event as MouseEvent;\n\t\t\tif (!this.isTouchDevice) {\n\t\t\t\tupdateCursor(mouseEvent.clientX, mouseEvent.clientY);\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('mousedown', event => {\n\t\t\tconst mouseEvent = event as MouseEvent;\n\t\t\tif (!this.isTouchDevice) {\n\t\t\t\tif (mouseEvent.button === 0) {\n\t\t\t\t\tthis.isMouseDown = true;\n\t\t\t\t\tupdateClick(true, mouseEvent.clientX, mouseEvent.clientY);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('mouseup', event => {\n\t\t\tconst mouseEvent = event as MouseEvent;\n\t\t\tif (!this.isTouchDevice) {\n\t\t\t\tif (mouseEvent.button === 0) {\n\t\t\t\t\tupdateClick(false);\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('touchmove', event => {\n\t\t\tconst touchEvent = event as TouchEvent;\n\t\t\tif (touchEvent.touches.length > 0) {\n\t\t\t\tupdateCursor(touchEvent.touches[0].clientX, touchEvent.touches[0].clientY);\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('touchstart', event => {\n\t\t\tconst touchEvent = event as TouchEvent;\n\t\t\tthis.isTouchDevice = true;\n\t\t\tif (touchEvent.touches.length > 0) {\n\t\t\t\tupdateCursor(touchEvent.touches[0].clientX, touchEvent.touches[0].clientY);\n\t\t\t\tupdateClick(true, touchEvent.touches[0].clientX, touchEvent.touches[0].clientY);\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.set('touchend', event => {\n\t\t\tconst touchEvent = event as TouchEvent;\n\t\t\tif (touchEvent.touches.length === 0) {\n\t\t\t\tupdateClick(false);\n\t\t\t}\n\t\t});\n\n\t\tthis.eventListeners.forEach((listener, event) => {\n\t\t\tthis.canvas.addEventListener(event, listener);\n\t\t});\n\t}\n\n\tprivate updateResolution() {\n\t\tconst width = this.gl.drawingBufferWidth;\n\t\tconst height = this.gl.drawingBufferHeight;\n\t\tthis.gl.viewport(0, 0, width, height);\n\t\tif (this.uniforms.has('u_resolution')) {\n\t\t\tthis.updateUniforms({ u_resolution: [width, height] });\n\t\t} else {\n\t\t\tthis.initializeUniform('u_resolution', 'float', [width, height]);\n\t\t}\n\n\t\tthis.hooks.get('updateResolution')?.forEach(hook => hook.call(this));\n\t}\n\n\tinitializeUniform(name: string, type: 'float' | 'int', value: number | number[]) {\n\t\tif (this.uniforms.has(name)) {\n\t\t\tthrow new Error(`Uniform '${name}' is already initialized.`);\n\t\t}\n\n\t\tif (type !== 'float' && type !== 'int') {\n\t\t\tthrow new Error(`Invalid uniform type: ${type}. Expected 'float' or 'int'.`);\n\t\t}\n\n\t\tconst location = this.gl.getUniformLocation(this.program!, name);\n\t\tif (!location) {\n\t\t\tconsole.debug(`Uniform ${name} not found in fragment shader. Skipping initialization.`);\n\t\t\treturn;\n\t\t}\n\n\t\tif (!Array.isArray(value)) {\n\t\t\tvalue = [value];\n\t\t}\n\t\tif (value.length < 1 || value.length > 4) {\n\t\t\tthrow new Error(`Invalid uniform value length: ${value.length}. Expected a length between 1 and 4.`);\n\t\t}\n\n\t\tconst length = value.length as 1 | 2 | 3 | 4;\n\t\tthis.uniforms.set(name, { type, length, location });\n\t\tthis.updateUniforms({ [name]: value });\n\t}\n\n\tupdateUniforms(updates: Record<string, number | number[]>) {\n\t\tObject.entries(updates).forEach(([name, value]: [string, number | number[]]) => {\n\t\t\tif (!this.uniforms.has(name)) {\n\t\t\t\tthrow new Error(`Uniform '${name}' is not initialized.`);\n\t\t\t}\n\n\t\t\tconst uniform = this.uniforms.get(name)!;\n\t\t\tif (!Array.isArray(value)) {\n\t\t\t\tvalue = [value];\n\t\t\t}\n\t\t\tif (value.length !== uniform.length) {\n\t\t\t\tthrow new Error(`Invalid uniform value length: ${value.length}. Expected ${uniform.length}.`);\n\t\t\t}\n\t\t\t(this.gl as any)[`uniform${uniform.length}${uniform.type.charAt(0)}`](uniform.location, ...value);\n\t\t});\n\t}\n\n\tstep(time: number) {\n\t\tconst gl = this.gl;\n\n\t\tif (this.uniforms.has('u_time')) {\n\t\t\tthis.updateUniforms({ u_time: time });\n\t\t}\n\t\tif (this.uniforms.has('u_frame')) {\n\t\t\tthis.updateUniforms({ u_frame: this.frame });\n\t\t}\n\n\t\tgl.clear(gl.COLOR_BUFFER_BIT);\n\t\tgl.drawArrays(gl.TRIANGLES, 0, 6);\n\n\t\tthis.hooks.get('step')?.forEach(hook => hook.call(this, time, this.frame));\n\n\t\t++this.frame;\n\t}\n\n\tplay(callback?: (time: number, frame: number) => void) {\n\t\tthis.pause(); // Prevent double play.\n\t\tconst loop = (time: number) => {\n\t\t\ttime = (time - this.startTime) / 1000; // Convert from milliseconds to seconds.\n\t\t\tthis.step(time);\n\t\t\tthis.animationFrameId = requestAnimationFrame(loop);\n\t\t\tif (callback) callback(time, this.frame);\n\t\t};\n\t\tthis.animationFrameId = requestAnimationFrame(loop);\n\t}\n\n\tpause() {\n\t\tif (this.animationFrameId) {\n\t\t\tcancelAnimationFrame(this.animationFrameId);\n\t\t\tthis.animationFrameId = null;\n\t\t}\n\t}\n\n\treset() {\n\t\tthis.frame = 0;\n\t\tthis.startTime = performance.now();\n\t\tthis.hooks.get('reset')?.forEach(hook => hook.call(this));\n\t}\n\n\tinitializeTexture(name: string, source: HTMLImageElement | HTMLVideoElement) {\n\t\tif (this.textures.has(name)) {\n\t\t\tthrow new Error(`Texture '${name}' is already initialized.`);\n\t\t}\n\n\t\tconst texture = this.gl.createTexture();\n\t\tif (!texture) {\n\t\t\tthrow new Error('Failed to create texture');\n\t\t}\n\t\tlet unitIndex: number;\n\t\ttry {\n\t\t\tunitIndex = this.reserveTextureUnit(name);\n\t\t} catch (error) {\n\t\t\tthis.gl.deleteTexture(texture);\n\t\t\tthrow error;\n\t\t}\n\t\tthis.gl.activeTexture(this.gl.TEXTURE0 + unitIndex);\n\t\tthis.gl.bindTexture(this.gl.TEXTURE_2D, texture);\n\n\t\t// Flip the texture vertically during upload to match WebGL's coordinate system.\n\t\t// WebGL uses bottom-left origin, while images/videos use top-left origin.\n\t\tthis.gl.pixelStorei(this.gl.UNPACK_FLIP_Y_WEBGL, true);\n\t\tthis.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_S, this.gl.CLAMP_TO_EDGE);\n\t\tthis.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_WRAP_T, this.gl.CLAMP_TO_EDGE);\n\t\tthis.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MIN_FILTER, this.gl.LINEAR);\n\t\tthis.gl.texParameteri(this.gl.TEXTURE_2D, this.gl.TEXTURE_MAG_FILTER, this.gl.LINEAR);\n\n\t\tthis.textures.set(name, { texture, unitIndex });\n\t\tthis.updateTextures({ [name]: source });\n\n\t\tconst uSampler = this.gl.getUniformLocation(this.program!, name);\n\t\tif (uSampler) {\n\t\t\tthis.gl.uniform1i(uSampler, unitIndex);\n\t\t}\n\t}\n\n\tupdateTextures(updates: Record<string, HTMLImageElement | HTMLVideoElement>) {\n\t\tObject.entries(updates).forEach(([name, source]) => {\n\t\t\tconst info = this.textures.get(name);\n\t\t\tif (!info) {\n\t\t\t\tthrow new Error(`Texture '${name}' is not initialized.`);\n\t\t\t}\n\t\t\tthis.gl.activeTexture(this.gl.TEXTURE0 + info.unitIndex);\n\t\t\tthis.gl.bindTexture(this.gl.TEXTURE_2D, info.texture);\n\t\t\tthis.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, source);\n\t\t});\n\t}\n\n\tasync save(filename: string) {\n\t\tthis.gl.clear(this.gl.COLOR_BUFFER_BIT);\n\t\tthis.gl.drawArrays(this.gl.TRIANGLES, 0, 6);\n\n\t\tif (filename && !`${filename}`.toLowerCase().endsWith('.png')) {\n\t\t\tfilename = `${filename}.png`;\n\t\t}\n\t\tfilename = filename || 'export.png';\n\t\tif ('ongesturechange' in window) {\n\t\t\t// Mobile.\n\t\t\ttry {\n\t\t\t\tconst blob: Blob = await new Promise(resolve =>\n\t\t\t\t\tthis.canvas.toBlob(resolve as BlobCallback, 'image/png')\n\t\t\t\t);\n\t\t\t\tconst file = new File([blob], filename, { type: blob.type });\n\n\t\t\t\tif (navigator.canShare?.({ files: [file] })) {\n\t\t\t\t\tawait navigator.share({ files: [file] });\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t} catch (error) {\n\t\t\t\tconsole.warn('Web Share API failed:', error);\n\t\t\t}\n\t\t} else {\n\t\t\t// Desktop.\n\t\t\tthis.downloadLink.download = filename;\n\t\t\tthis.downloadLink.href = this.canvas.toDataURL();\n\t\t\tthis.downloadLink.click();\n\t\t}\n\t}\n\n\tdestroy() {\n\t\tif (this.animationFrameId) {\n\t\t\tcancelAnimationFrame(this.animationFrameId);\n\t\t\tthis.animationFrameId = null;\n\t\t}\n\n\t\tthis.resolutionObserver.disconnect();\n\t\tthis.resizeObserver.disconnect();\n\t\tthis.eventListeners.forEach((listener, event) => {\n\t\t\tthis.canvas.removeEventListener(event, listener);\n\t\t});\n\n\t\tif (this.program) {\n\t\t\tthis.gl.deleteProgram(this.program);\n\t\t}\n\n\t\tthis.textures.forEach(texture => {\n\t\t\tthis.gl.deleteTexture(texture.texture);\n\t\t});\n\t\tthis.textureUnitPool.free = [];\n\t\tthis.textureUnitPool.next = 1;\n\n\t\tif (this.buffer) {\n\t\t\tthis.gl.deleteBuffer(this.buffer);\n\t\t\tthis.buffer = null;\n\t\t}\n\n\t\tthis.hooks.get('destroy')?.forEach(hook => hook.call(this));\n\n\t\tif (this.isInternalCanvas) {\n\t\t\tthis.canvas.remove();\n\t\t}\n\t}\n\n\t// TODO: Review these two.\n\tprivate reserveTextureUnit(name: string) {\n\t\tif (!name) {\n\t\t\tthrow new Error('Texture unit name must be provided.');\n\t\t}\n\t\tconst existing = this.textures.get(name);\n\t\tif (existing) {\n\t\t\treturn existing.unitIndex;\n\t\t}\n\t\tlet unit: number;\n\t\tif (this.textureUnitPool.free.length > 0) {\n\t\t\tunit = this.textureUnitPool.free.pop()!;\n\t\t} else {\n\t\t\tunit = this.textureUnitPool.next;\n\t\t\tif (unit >= this.textureUnitPool.max) {\n\t\t\t\tthrow new Error('Exceeded the available texture units for this device.');\n\t\t\t}\n\t\t\tthis.textureUnitPool.next += 1;\n\t\t}\n\t\treturn unit;\n\t}\n\n\tprivate releaseTextureUnit(name: string) {\n\t\tconst existing = this.textures.get(name);\n\t\tif (!existing) {\n\t\t\treturn;\n\t\t}\n\t\tconst unit = existing.unitIndex;\n\t\tif (unit > 0) {\n\t\t\tthis.textureUnitPool.free.push(unit);\n\t\t}\n\t}\n}\n\nexport * from './plugins';\nexport default ShaderPad;\n"],"mappings":"AAMA,SAASA,EAAoBC,EAAuB,CACnD,IAAMC,EAAQD,EACRE,EAAQF,EACRG,EAAQF,EAAM,YAAcC,EAAM,cAAgBA,EAAM,MACxDE,EAASH,EAAM,aAAeC,EAAM,eAAiBA,EAAM,OACjE,MAAO,CAAE,MAAAC,EAAO,OAAAC,CAAO,CACxB,CAEA,SAASC,EAA0BC,EAA4BH,EAAeC,EAAgBG,EAAqB,CAClH,IAAMC,EAAc,IAAI,WAAWL,EAAQC,EAAS,CAAC,EACrD,QAASK,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Q,OAAO,CACtC,CAAC,EACD,KAAK,gBAAgB,KAAO,CAAC,EAC7B,KAAK,gBAAgB,KAAO,EAExB,KAAK,SACR,KAAK,GAAG,aAAa,KAAK,MAAM,EAChC,KAAK,OAAS,MAGf,KAAK,MAAM,IAAI,SAAS,GAAG,QAAQ/B,GAAQA,EAAK,KAAK,IAAI,CAAC,EAEtD,KAAK,kBACR,KAAK,OAAO,OAAO,CAErB,CAGQ,mBAAmBN,EAAc,CACxC,GAAI,CAACA,EACJ,MAAM,IAAI,MAAM,qCAAqC,EAEtD,IAAM8C,EAAW,KAAK,SAAS,IAAI9C,CAAI,EACvC,GAAI8C,EACH,OAAOA,EAAS,UAEjB,IAAIC,EACJ,GAAI,KAAK,gBAAgB,KAAK,OAAS,EACtCA,EAAO,KAAK,gBAAgB,KAAK,IAAI,MAC/B,CAEN,GADAA,EAAO,KAAK,gBAAgB,KACxBA,GAAQ,KAAK,gBAAgB,IAChC,MAAM,IAAI,MAAM,uDAAuD,EAExE,KAAK,gBAAgB,MAAQ,CAC9B,CACA,OAAOA,CACR,CAEQ,mBAAmB/C,EAAc,CACxC,IAAM8C,EAAW,KAAK,SAAS,IAAI9C,CAAI,EACvC,GAAI,CAAC8C,EACJ,OAED,IAAMC,EAAOD,EAAS,UAClBC,EAAO,GACV,KAAK,gBAAgB,KAAK,KAAKA,CAAI,CAErC,CACD,EAGOC,EAAQrD","names":["getSourceDimensions","source","video","image","width","height","clearHistoryTextureLayers","gl","depth","transparent","layer","createHistoryTexture","texture","history","framebufferDepth","shaderPad","context","uniforms","textures","reserveTextureUnit","shaderPadWithHistory","originalInitializeTexture","originalUpdateTextures","framebufferHistoryTexture","initializeFramebufferHistory","clearFramebufferHistory","_time","frame","writeIdx","textureInfo","name","historyDepth","unitIndex","uSampler","error","updates","writeIndex","save","shaderPad","context","gl","canvas","downloadLink","filename","blob","resolve","file","error","DEFAULT_VERTEX_SHADER_SRC","RESIZE_THROTTLE_INTERVAL","ShaderPad","fragmentShaderSrc","options","context","plugin","name","fn","vertexShaderSrc","vertexShader","fragmentShader","aPosition","hook","type","source","shader","quadVertices","now","timeUntilNextResize","pixelRatio","width","height","updateCursor","x","y","rect","updateClick","isMouseDown","xVal","yVal","event","mouseEvent","touchEvent","listener","value","location","length","updates","uniform","time","gl","callback","loop","texture","unitIndex","error","uSampler","info","filename","blob","resolve","file","existing","unit","index_default"]}
|