shaderpad 1.0.0-alpha.2 → 1.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -67,6 +67,7 @@ See the [`examples/` directory](./examples/) for more.
67
67
  | ------------- | -------- | ----------------------------------- |
68
68
  | `uUv` | float[2] | The UV coordinates of the fragment. |
69
69
  | `uTime` | float | The current time in seconds. |
70
+ | `uFrame` | int | The current frame number. |
70
71
  | `uResolution` | float[2] | The canvas element’s dimensions. |
71
72
  | `uCursor` | float[2] | The current mouse/cursor position. |
72
73
 
package/dist/index.d.mts CHANGED
@@ -1,12 +1,18 @@
1
- declare class Shader {
1
+ declare class ShaderPad {
2
+ private isInternalCanvas;
3
+ private isTouchDevice;
2
4
  private canvas;
3
5
  private gl;
4
6
  private downloadLink;
5
7
  private fragmentShaderSrc;
6
8
  private uniforms;
9
+ private textures;
10
+ private buffer;
11
+ private program;
7
12
  private animationFrameId;
8
13
  private resizeObserver;
9
- private program;
14
+ private eventListeners;
15
+ private frame;
10
16
  constructor(fragmentShaderSrc: string, canvas?: HTMLCanvasElement | null);
11
17
  private init;
12
18
  private createShader;
@@ -15,9 +21,13 @@ declare class Shader {
15
21
  private addEventListeners;
16
22
  initializeUniform(name: string, type: 'float' | 'int', value: number | number[]): void;
17
23
  updateUniforms(updates: Record<string, number | number[]>): void;
18
- play(callback?: (time: number) => void): void;
24
+ step(time: number): void;
25
+ play(callback?: (time: number, frame: number) => void): void;
19
26
  pause(): void;
20
27
  save(filename: string): void;
28
+ initializeTexture(name: string, source: HTMLImageElement | HTMLVideoElement): void;
29
+ updateTextures(updates: Record<string, HTMLImageElement | HTMLVideoElement>): void;
30
+ destroy(): void;
21
31
  }
22
32
 
23
- export { Shader as default };
33
+ export { ShaderPad as default };
package/dist/index.d.ts CHANGED
@@ -1,12 +1,18 @@
1
- declare class Shader {
1
+ declare class ShaderPad {
2
+ private isInternalCanvas;
3
+ private isTouchDevice;
2
4
  private canvas;
3
5
  private gl;
4
6
  private downloadLink;
5
7
  private fragmentShaderSrc;
6
8
  private uniforms;
9
+ private textures;
10
+ private buffer;
11
+ private program;
7
12
  private animationFrameId;
8
13
  private resizeObserver;
9
- private program;
14
+ private eventListeners;
15
+ private frame;
10
16
  constructor(fragmentShaderSrc: string, canvas?: HTMLCanvasElement | null);
11
17
  private init;
12
18
  private createShader;
@@ -15,9 +21,13 @@ declare class Shader {
15
21
  private addEventListeners;
16
22
  initializeUniform(name: string, type: 'float' | 'int', value: number | number[]): void;
17
23
  updateUniforms(updates: Record<string, number | number[]>): void;
18
- play(callback?: (time: number) => void): void;
24
+ step(time: number): void;
25
+ play(callback?: (time: number, frame: number) => void): void;
19
26
  pause(): void;
20
27
  save(filename: string): void;
28
+ initializeTexture(name: string, source: HTMLImageElement | HTMLVideoElement): void;
29
+ updateTextures(updates: Record<string, HTMLImageElement | HTMLVideoElement>): void;
30
+ destroy(): void;
21
31
  }
22
32
 
23
- export { Shader as default };
33
+ export { ShaderPad as default };
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
- "use strict";var a=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var m=(s,i)=>{for(var r in i)a(s,r,{get:i[r],enumerable:!0})},f=(s,i,r,t)=>{if(i&&typeof i=="object"||typeof i=="function")for(let e of c(i))!d.call(s,e)&&e!==r&&a(s,e,{get:()=>i[e],enumerable:!(t=g(i,e))||t.enumerable});return s};var u=s=>f(a({},"__esModule",{value:!0}),s);var w={};m(w,{default:()=>p});module.exports=u(w);var v=`
1
+ "use strict";var h=Object.defineProperty;var l=Object.getOwnPropertyDescriptor;var g=Object.getOwnPropertyNames;var c=Object.prototype.hasOwnProperty;var u=(s,t)=>{for(var e in t)h(s,e,{get:t[e],enumerable:!0})},d=(s,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of g(t))!c.call(s,r)&&r!==e&&h(s,r,{get:()=>t[r],enumerable:!(i=l(t,r))||i.enumerable});return s};var m=s=>d(h({},"__esModule",{value:!0}),s);var p={};u(p,{default:()=>v});module.exports=m(p);var f=`
2
2
  attribute vec2 aPosition;
3
3
  varying vec2 vUv;
4
4
  void main() {
5
5
  vUv = aPosition * 0.5 + 0.5;
6
6
  gl_Position = vec4(aPosition, 0.0, 1.0);
7
7
  }
8
- `,o=class{constructor(i,r=null){this.uniforms=new Map;this.program=null;this.canvas=r||document.createElement("canvas"),r||(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("webgl"),this.downloadLink=document.createElement("a"),this.fragmentShaderSrc=i,this.animationFrameId=null,this.resizeObserver=new ResizeObserver(()=>this.resizeCanvas()),this.resizeObserver.observe(this.canvas),this.init(),this.addEventListeners()}init(){let i=v;if(this.program=this.gl.createProgram(),!this.program)throw new Error("Failed to create WebGL program");let r=this.createShader(this.gl.VERTEX_SHADER,i),t=this.createShader(this.gl.FRAGMENT_SHADER,this.fragmentShaderSrc);if(this.gl.attachShader(this.program,r),this.gl.attachShader(this.program,t),this.gl.linkProgram(this.program),this.gl.getProgramParameter(this.program,this.gl.LINK_STATUS)||(console.error("Program link error:",this.gl.getProgramInfoLog(this.program)),this.gl.deleteProgram(this.program),this.program=null),!this.program)throw new Error("Failed to link WebGL program");let e=this.gl.getAttribLocation(this.program,"aPosition");this.setupBuffer(e),this.resizeCanvas(),this.gl.useProgram(this.program),this.initializeUniform("uResolution","float",[this.canvas.width,this.canvas.height]),this.initializeUniform("uCursor","float",[.5,.5]),this.initializeUniform("uTime","float",0)}createShader(i,r){let t=this.gl.createShader(i);if(this.gl.shaderSource(t,r),this.gl.compileShader(t),!this.gl.getShaderParameter(t,this.gl.COMPILE_STATUS))throw console.error("Shader compilation failed:",r),console.error(this.gl.getShaderInfoLog(t)),this.gl.deleteShader(t),new Error("Shader compilation failed");return t}setupBuffer(i){let r=new Float32Array([-1,-1,1,-1,-1,1,-1,1,1,-1,1,1]),t=this.gl.createBuffer();this.gl.bindBuffer(this.gl.ARRAY_BUFFER,t),this.gl.bufferData(this.gl.ARRAY_BUFFER,r,this.gl.STATIC_DRAW),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,t),this.gl.enableVertexAttribArray(i),this.gl.vertexAttribPointer(i,2,this.gl.FLOAT,!1,0,0)}resizeCanvas(){let i=window.devicePixelRatio||1,r=this.canvas.clientWidth*i,t=this.canvas.clientHeight*i;(this.canvas.width!==r||this.canvas.height!==t)&&(this.canvas.width=r,this.canvas.height=t,this.gl.viewport(0,0,this.gl.drawingBufferWidth,this.gl.drawingBufferHeight),this.uniforms.has("uResolution")&&this.updateUniforms({uResolution:[this.canvas.width,this.canvas.height]}))}addEventListeners(){let i=!1,r=(t,e)=>{if(!this.uniforms.has("uCursor"))return;let n=this.canvas.getBoundingClientRect(),h=(t-n.left)/n.width,l=1-(e-n.top)/n.height;this.updateUniforms({uCursor:[h,l]})};this.canvas.addEventListener("mousemove",t=>{i||r(t.clientX,t.clientY)}),this.canvas.addEventListener("touchstart",()=>{i=!0}),this.canvas.addEventListener("touchmove",t=>{t.touches.length>0&&r(t.touches[0].clientX,t.touches[0].clientY)})}initializeUniform(i,r,t){if(this.uniforms.has(i))throw new Error(`Uniform '${i}' is already initialized.`);if(!this.program)throw new Error("WebGL program is not initialized");if(r!=="float"&&r!=="int")throw new Error(`Invalid uniform type: ${r}. Expected 'float' or 'int'.`);let e=this.gl.getUniformLocation(this.program,i);if(!e){console.log(`Uniform ${i} not found in fragment shader. Skipping initialization.`);return}if(Array.isArray(t)||(t=[t]),t.length<1||t.length>4)throw new Error(`Invalid uniform value length: ${t.length}. Expected a length between 1 and 4.`);let n=t.length;this.uniforms.set(i,{type:r,length:n,location:e}),this.updateUniforms({[i]:t})}updateUniforms(i){Object.entries(i).forEach(([r,t])=>{if(!this.uniforms.has(r))throw new Error(`Uniform '${r}' is not initialized.`);let e=this.uniforms.get(r);if(Array.isArray(t)||(t=[t]),t.length!==e.length)throw new Error(`Invalid uniform value length: ${t.length}. Expected ${e.length}.`);this.gl[`uniform${e.length}${e.type.charAt(0)}`](e.location,...t)})}play(i){let r=t=>{t/=1e3,this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.uniforms.has("uTime")&&this.updateUniforms({uTime:t}),i&&i(t),this.gl.drawArrays(this.gl.TRIANGLES,0,6),this.animationFrameId=requestAnimationFrame(r)};this.animationFrameId=requestAnimationFrame(r)}pause(){this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null)}save(i){this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.drawArrays(this.gl.TRIANGLES,0,6);let r=this.canvas.toDataURL();i&&!`${i}`.toLowerCase().endsWith(".png")&&(i=`${i}.png`),this.downloadLink.download=i||"export.png",this.downloadLink.href=r,this.downloadLink.click()}},p=o;
8
+ `,o=class{constructor(t,e=null){this.isInternalCanvas=!1;this.isTouchDevice=!1;this.uniforms=new Map;this.textures=new Map;this.buffer=null;this.program=null;this.eventListeners=new Map;this.frame=0;this.canvas=e||document.createElement("canvas"),e||(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("webgl"),this.downloadLink=document.createElement("a"),this.fragmentShaderSrc=t,this.animationFrameId=null,this.resizeObserver=new ResizeObserver(()=>this.resizeCanvas()),this.resizeObserver.observe(this.canvas),this.init(),this.addEventListeners()}init(){let t=f;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 r=this.gl.getAttribLocation(this.program,"aPosition");this.setupBuffer(r),this.resizeCanvas(),this.gl.useProgram(this.program),this.initializeUniform("uResolution","float",[this.canvas.width,this.canvas.height]),this.initializeUniform("uCursor","float",[.5,.5]),this.initializeUniform("uTime","float",0),this.initializeUniform("uFrame","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)}resizeCanvas(){let t=window.devicePixelRatio||1,e=this.canvas.clientWidth*t,i=this.canvas.clientHeight*t,r=getComputedStyle(this.canvas),n=r.width!==`${this.canvas.width}px`&&r.width!=="auto",a=r.height!==`${this.canvas.height}px`&&r.height!=="auto";(!n||!a)&&(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("uResolution")&&this.updateUniforms({uResolution:[this.canvas.width,this.canvas.height]}))}addEventListeners(){let t=(e,i)=>{if(!this.uniforms.has("uCursor"))return;let r=this.canvas.getBoundingClientRect(),n=(e-r.left)/r.width,a=1-(i-r.top)/r.height;this.updateUniforms({uCursor:[n,a]})};this.eventListeners.set("mousemove",e=>{let i=e;this.isTouchDevice||t(i.clientX,i.clientY)}),this.eventListeners.set("touchmove",e=>{let i=e;i.touches.length>0&&t(i.touches[0].clientX,i.touches[0].clientY)}),this.eventListeners.set("touchstart",()=>{this.isTouchDevice=!0}),this.eventListeners.forEach((e,i)=>{this.canvas.addEventListener(i,e)})}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 r=this.gl.getUniformLocation(this.program,t);if(!r){console.log(`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 n=i.length;this.uniforms.set(t,{type:e,length:n,location:r}),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 r=this.uniforms.get(e);if(Array.isArray(i)||(i=[i]),i.length!==r.length)throw new Error(`Invalid uniform value length: ${i.length}. Expected ${r.length}.`);this.gl[`uniform${r.length}${r.type.charAt(0)}`](r.location,...i)})}step(t){t/=1e3,this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.uniforms.has("uTime")&&this.updateUniforms({uTime:t}),this.uniforms.has("uFrame")&&this.updateUniforms({uFrame:this.frame}),++this.frame,this.gl.drawArrays(this.gl.TRIANGLES,0,6)}play(t){let e=i=>{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 r=this.textures.size;this.gl.activeTexture(this.gl.TEXTURE0+r),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:r}),this.updateTextures({[t]:e});let n=this.gl.getUniformLocation(this.program,t);n&&this.gl.uniform1i(n,r)}updateTextures(t){Object.entries(t).forEach(([e,i])=>{let r=this.textures.get(e);if(!r)throw new Error(`Texture '${e}' 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,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.buffer&&(this.gl.deleteBuffer(this.buffer),this.buffer=null),this.isInternalCanvas&&this.canvas.remove()}},v=o;
9
9
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["const defaultVertexShaderSrc = `\nattribute vec2 aPosition;\nvarying vec2 vUv;\nvoid main() {\n vUv = aPosition * 0.5 + 0.5;\n gl_Position = vec4(aPosition, 0.0, 1.0);\n}\n`;\n\ninterface Uniform {\n\ttype: 'float' | 'int';\n\tlength: 1 | 2 | 3 | 4;\n\tlocation: WebGLUniformLocation;\n}\n\nclass Shader {\n\tprivate canvas: HTMLCanvasElement;\n\tprivate gl: WebGLRenderingContext;\n\tprivate downloadLink: HTMLAnchorElement;\n\tprivate fragmentShaderSrc: string;\n\tprivate uniforms: Map<string, Uniform> = new Map();\n\tprivate animationFrameId: number | null;\n\tprivate resizeObserver: ResizeObserver;\n\tprivate program: WebGLProgram | null = null;\n\n\tconstructor(fragmentShaderSrc: string, canvas: HTMLCanvasElement | null = null) {\n\t\tthis.canvas = canvas || document.createElement('canvas');\n\t\tif (!canvas) {\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\t\tthis.gl = this.canvas.getContext('webgl')!;\n\t\tthis.downloadLink = document.createElement('a');\n\t\tthis.fragmentShaderSrc = fragmentShaderSrc;\n\t\tthis.animationFrameId = null;\n\t\tthis.resizeObserver = new ResizeObserver(() => this.resizeCanvas());\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 = defaultVertexShaderSrc;\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\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\tthis.program = null;\n\t\t}\n\n\t\tif (!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.resizeCanvas();\n\n\t\tthis.gl.useProgram(this.program);\n\n\t\tthis.initializeUniform('uResolution', 'float', [this.canvas.width, this.canvas.height]);\n\t\tthis.initializeUniform('uCursor', 'float', [0.5, 0.5]);\n\t\tthis.initializeUniform('uTime', 'float', 0);\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\tconst buffer = this.gl.createBuffer();\n\t\tthis.gl.bindBuffer(this.gl.ARRAY_BUFFER, buffer);\n\t\tthis.gl.bufferData(this.gl.ARRAY_BUFFER, quadVertices, this.gl.STATIC_DRAW);\n\n\t\tthis.gl.viewport(0, 0, this.canvas.width, this.canvas.height);\n\t\tthis.gl.bindBuffer(this.gl.ARRAY_BUFFER, buffer);\n\t\tthis.gl.enableVertexAttribArray(aPosition);\n\t\tthis.gl.vertexAttribPointer(aPosition, 2, this.gl.FLOAT, false, 0, 0);\n\t}\n\n\tprivate resizeCanvas() {\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\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('uResolution')) {\n\t\t\t\tthis.updateUniforms({ uResolution: [this.canvas.width, this.canvas.height] });\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate addEventListeners() {\n\t\tlet isTouchDevice = false;\n\n\t\tconst updateCursor = (x: number, y: number) => {\n\t\t\tif (!this.uniforms.has('uCursor')) return;\n\t\t\tconst rect = this.canvas.getBoundingClientRect();\n\t\t\tconst cursorX = (x - rect.left) / rect.width;\n\t\t\tconst cursorY = 1 - (y - rect.top) / rect.height; // Flip Y for WebGL\n\t\t\tthis.updateUniforms({ uCursor: [cursorX, cursorY] });\n\t\t};\n\n\t\tthis.canvas.addEventListener('mousemove', event => {\n\t\t\tif (!isTouchDevice) {\n\t\t\t\tupdateCursor(event.clientX, event.clientY);\n\t\t\t}\n\t\t});\n\n\t\tthis.canvas.addEventListener('touchstart', () => {\n\t\t\tisTouchDevice = true;\n\t\t});\n\n\t\tthis.canvas.addEventListener('touchmove', event => {\n\t\t\tif (event.touches.length > 0) {\n\t\t\t\tupdateCursor(event.touches[0].clientX, event.touches[0].clientY);\n\t\t\t}\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 (!this.program) {\n\t\t\tthrow new Error('WebGL program is not 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.log(`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\tplay(callback?: (time: number) => void) {\n\t\tconst renderFrame = (time: number) => {\n\t\t\ttime /= 1000; // Convert from milliseconds to seconds.\n\n\t\t\tthis.gl.clear(this.gl.COLOR_BUFFER_BIT);\n\n\t\t\tif (this.uniforms.has('uTime')) {\n\t\t\t\tthis.updateUniforms({ uTime: time });\n\t\t\t}\n\n\t\t\tif (callback) callback(time);\n\n\t\t\tthis.gl.drawArrays(this.gl.TRIANGLES, 0, 6);\n\t\t\tthis.animationFrameId = requestAnimationFrame(renderFrame);\n\t\t};\n\n\t\tthis.animationFrameId = requestAnimationFrame(renderFrame);\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\nexport default Shader;\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAMI,EAAyB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAezBC,EAAN,KAAa,CAUZ,YAAYC,EAA2BC,EAAmC,KAAM,CALhF,KAAQ,SAAiC,IAAI,IAG7C,KAAQ,QAA+B,KAGtC,KAAK,OAASA,GAAU,SAAS,cAAc,QAAQ,EAClDA,IACJ,SAAS,KAAK,YAAY,KAAK,MAAM,EACrC,KAAK,OAAO,MAAM,SAAW,QAC7B,KAAK,OAAO,MAAM,MAAQ,IAC1B,KAAK,OAAO,MAAM,OAAS,SAC3B,KAAK,OAAO,MAAM,MAAQ,UAE3B,KAAK,GAAK,KAAK,OAAO,WAAW,OAAO,EACxC,KAAK,aAAe,SAAS,cAAc,GAAG,EAC9C,KAAK,kBAAoBD,EACzB,KAAK,iBAAmB,KACxB,KAAK,eAAiB,IAAI,eAAe,IAAM,KAAK,aAAa,CAAC,EAClE,KAAK,eAAe,QAAQ,KAAK,MAAM,EACvC,KAAK,KAAK,EACV,K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AEOpC,EAAQG","names":["index_exports","__export","index_default","__toCommonJS","defaultVertexShaderSrc","Shader","fragmentShaderSrc","canvas","vertexShaderSrc","vertexShader","fragmentShader","aPosition","type","source","shader","quadVertices","buffer","pixelRatio","width","height","isTouchDevice","updateCursor","x","y","rect","cursorX","cursorY","event","name","value","location","length","updates","uniform","callback","renderFrame","time","filename","image"]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["const defaultVertexShaderSrc = `\nattribute vec2 aPosition;\nvarying vec2 vUv;\nvoid main() {\n vUv = aPosition * 0.5 + 0.5;\n gl_Position = vec4(aPosition, 0.0, 1.0);\n}\n`;\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\nclass ShaderPad {\n\tprivate isInternalCanvas = false;\n\tprivate isTouchDevice = false;\n\tprivate canvas: HTMLCanvasElement;\n\tprivate gl: WebGLRenderingContext;\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 eventListeners: Map<string, EventListener> = new Map();\n\tprivate frame = 0;\n\n\tconstructor(fragmentShaderSrc: string, canvas: HTMLCanvasElement | null = null) {\n\t\tthis.canvas = canvas || document.createElement('canvas');\n\t\tif (!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\t\tthis.gl = this.canvas.getContext('webgl')!;\n\t\tthis.downloadLink = document.createElement('a');\n\t\tthis.fragmentShaderSrc = fragmentShaderSrc;\n\t\tthis.animationFrameId = null;\n\t\tthis.resizeObserver = new ResizeObserver(() => this.resizeCanvas());\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 = defaultVertexShaderSrc;\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.resizeCanvas();\n\n\t\tthis.gl.useProgram(this.program);\n\n\t\tthis.initializeUniform('uResolution', 'float', [this.canvas.width, this.canvas.height]);\n\t\tthis.initializeUniform('uCursor', 'float', [0.5, 0.5]);\n\t\tthis.initializeUniform('uTime', 'float', 0);\n\t\tthis.initializeUniform('uFrame', 'int', 0);\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\t// TODO: This breaks for `position: fixed; inset: 0` canvases.\n\tprivate resizeCanvas() {\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('uResolution')) {\n\t\t\t\tthis.updateUniforms({ uResolution: [this.canvas.width, this.canvas.height] });\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('uCursor')) return;\n\t\t\tconst rect = this.canvas.getBoundingClientRect();\n\t\t\tconst cursorX = (x - rect.left) / rect.width;\n\t\t\tconst cursorY = 1 - (y - rect.top) / rect.height; // Flip Y for WebGL\n\t\t\tthis.updateUniforms({ uCursor: [cursorX, cursorY] });\n\t\t};\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\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\t\tthis.eventListeners.set('touchstart', () => {\n\t\t\tthis.isTouchDevice = true;\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.log(`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\ttime /= 1000; // Convert from milliseconds to seconds.\n\n\t\tthis.gl.clear(this.gl.COLOR_BUFFER_BIT);\n\n\t\tif (this.uniforms.has('uTime')) {\n\t\t\tthis.updateUniforms({ uTime: time });\n\t\t}\n\n\t\tif (this.uniforms.has('uFrame')) {\n\t\t\tthis.updateUniforms({ uFrame: this.frame });\n\t\t}\n\n\t\t++this.frame;\n\t\tthis.gl.drawArrays(this.gl.TRIANGLES, 0, 6);\n\t}\n\n\tplay(callback?: (time: number, frame: number) => void) {\n\t\tconst loop = (time: number) => {\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;\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 vUv 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.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":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAMI,EAAyB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBzBC,EAAN,KAAgB,CAgBf,YAAYC,EAA2BC,EAAmC,KAAM,CAfhF,KAAQ,iBAAmB,GAC3B,KAAQ,cAAgB,GAKxB,KAAQ,SAAiC,IAAI,IAC7C,KAAQ,SAAiC,IAAI,IAC7C,KAAQ,OAA6B,KACrC,KAAQ,QAA+B,KAGvC,KAAQ,eAA6C,IAAI,IACzD,KAAQ,MAAQ,EAGf,KAAK,OAASA,GAAU,SAAS,cAAc,QAAQ,EAClDA,IACJ,KAAK,iBAAmB,GACxB,SAAS,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,eAAe,QAAQ,CAACkB,EAAUH,IAAU,CAChD,KAAK,OAAO,oBAAoBA,EAAOG,CAAQ,CAChD,CAAC,EAEG,KAAK,SACR,KAAK,GAAG,cAAc,KAAK,OAAO,EAGnC,KAAK,SAAS,QAAQY,GAAW,CAChC,KAAK,GAAG,cAAcA,EAAQ,OAAO,CACtC,CAAC,EAEG,KAAK,SACR,KAAK,GAAG,aAAa,KAAK,MAAM,EAChC,KAAK,OAAS,MAGX,KAAK,kBACR,KAAK,OAAO,OAAO,CAErB,CACD,EAEOzC,EAAQG","names":["index_exports","__export","index_default","__toCommonJS","defaultVertexShaderSrc","ShaderPad","fragmentShaderSrc","canvas","vertexShaderSrc","vertexShader","fragmentShader","aPosition","type","source","shader","quadVertices","pixelRatio","width","height","computedStyle","hasExplicitWidth","hasExplicitHeight","updateCursor","x","y","rect","cursorX","cursorY","event","mouseEvent","touchEvent","listener","name","value","location","length","updates","uniform","time","callback","loop","filename","image","texture","unitIndex","uSampler","info"]}
package/dist/index.mjs CHANGED
@@ -5,5 +5,5 @@ void main() {
5
5
  vUv = aPosition * 0.5 + 0.5;
6
6
  gl_Position = vec4(aPosition, 0.0, 1.0);
7
7
  }
8
- `,n=class{constructor(i,r=null){this.uniforms=new Map;this.program=null;this.canvas=r||document.createElement("canvas"),r||(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("webgl"),this.downloadLink=document.createElement("a"),this.fragmentShaderSrc=i,this.animationFrameId=null,this.resizeObserver=new ResizeObserver(()=>this.resizeCanvas()),this.resizeObserver.observe(this.canvas),this.init(),this.addEventListeners()}init(){let i=h;if(this.program=this.gl.createProgram(),!this.program)throw new Error("Failed to create WebGL program");let r=this.createShader(this.gl.VERTEX_SHADER,i),t=this.createShader(this.gl.FRAGMENT_SHADER,this.fragmentShaderSrc);if(this.gl.attachShader(this.program,r),this.gl.attachShader(this.program,t),this.gl.linkProgram(this.program),this.gl.getProgramParameter(this.program,this.gl.LINK_STATUS)||(console.error("Program link error:",this.gl.getProgramInfoLog(this.program)),this.gl.deleteProgram(this.program),this.program=null),!this.program)throw new Error("Failed to link WebGL program");let e=this.gl.getAttribLocation(this.program,"aPosition");this.setupBuffer(e),this.resizeCanvas(),this.gl.useProgram(this.program),this.initializeUniform("uResolution","float",[this.canvas.width,this.canvas.height]),this.initializeUniform("uCursor","float",[.5,.5]),this.initializeUniform("uTime","float",0)}createShader(i,r){let t=this.gl.createShader(i);if(this.gl.shaderSource(t,r),this.gl.compileShader(t),!this.gl.getShaderParameter(t,this.gl.COMPILE_STATUS))throw console.error("Shader compilation failed:",r),console.error(this.gl.getShaderInfoLog(t)),this.gl.deleteShader(t),new Error("Shader compilation failed");return t}setupBuffer(i){let r=new Float32Array([-1,-1,1,-1,-1,1,-1,1,1,-1,1,1]),t=this.gl.createBuffer();this.gl.bindBuffer(this.gl.ARRAY_BUFFER,t),this.gl.bufferData(this.gl.ARRAY_BUFFER,r,this.gl.STATIC_DRAW),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.gl.bindBuffer(this.gl.ARRAY_BUFFER,t),this.gl.enableVertexAttribArray(i),this.gl.vertexAttribPointer(i,2,this.gl.FLOAT,!1,0,0)}resizeCanvas(){let i=window.devicePixelRatio||1,r=this.canvas.clientWidth*i,t=this.canvas.clientHeight*i;(this.canvas.width!==r||this.canvas.height!==t)&&(this.canvas.width=r,this.canvas.height=t,this.gl.viewport(0,0,this.gl.drawingBufferWidth,this.gl.drawingBufferHeight),this.uniforms.has("uResolution")&&this.updateUniforms({uResolution:[this.canvas.width,this.canvas.height]}))}addEventListeners(){let i=!1,r=(t,e)=>{if(!this.uniforms.has("uCursor"))return;let s=this.canvas.getBoundingClientRect(),a=(t-s.left)/s.width,o=1-(e-s.top)/s.height;this.updateUniforms({uCursor:[a,o]})};this.canvas.addEventListener("mousemove",t=>{i||r(t.clientX,t.clientY)}),this.canvas.addEventListener("touchstart",()=>{i=!0}),this.canvas.addEventListener("touchmove",t=>{t.touches.length>0&&r(t.touches[0].clientX,t.touches[0].clientY)})}initializeUniform(i,r,t){if(this.uniforms.has(i))throw new Error(`Uniform '${i}' is already initialized.`);if(!this.program)throw new Error("WebGL program is not initialized");if(r!=="float"&&r!=="int")throw new Error(`Invalid uniform type: ${r}. Expected 'float' or 'int'.`);let e=this.gl.getUniformLocation(this.program,i);if(!e){console.log(`Uniform ${i} not found in fragment shader. Skipping initialization.`);return}if(Array.isArray(t)||(t=[t]),t.length<1||t.length>4)throw new Error(`Invalid uniform value length: ${t.length}. Expected a length between 1 and 4.`);let s=t.length;this.uniforms.set(i,{type:r,length:s,location:e}),this.updateUniforms({[i]:t})}updateUniforms(i){Object.entries(i).forEach(([r,t])=>{if(!this.uniforms.has(r))throw new Error(`Uniform '${r}' is not initialized.`);let e=this.uniforms.get(r);if(Array.isArray(t)||(t=[t]),t.length!==e.length)throw new Error(`Invalid uniform value length: ${t.length}. Expected ${e.length}.`);this.gl[`uniform${e.length}${e.type.charAt(0)}`](e.location,...t)})}play(i){let r=t=>{t/=1e3,this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.uniforms.has("uTime")&&this.updateUniforms({uTime:t}),i&&i(t),this.gl.drawArrays(this.gl.TRIANGLES,0,6),this.animationFrameId=requestAnimationFrame(r)};this.animationFrameId=requestAnimationFrame(r)}pause(){this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null)}save(i){this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.drawArrays(this.gl.TRIANGLES,0,6);let r=this.canvas.toDataURL();i&&!`${i}`.toLowerCase().endsWith(".png")&&(i=`${i}.png`),this.downloadLink.download=i||"export.png",this.downloadLink.href=r,this.downloadLink.click()}},g=n;export{g as default};
8
+ `,a=class{constructor(i,e=null){this.isInternalCanvas=!1;this.isTouchDevice=!1;this.uniforms=new Map;this.textures=new Map;this.buffer=null;this.program=null;this.eventListeners=new Map;this.frame=0;this.canvas=e||document.createElement("canvas"),e||(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("webgl"),this.downloadLink=document.createElement("a"),this.fragmentShaderSrc=i,this.animationFrameId=null,this.resizeObserver=new ResizeObserver(()=>this.resizeCanvas()),this.resizeObserver.observe(this.canvas),this.init(),this.addEventListeners()}init(){let i=h;if(this.program=this.gl.createProgram(),!this.program)throw new Error("Failed to create WebGL program");let e=this.createShader(this.gl.VERTEX_SHADER,i),t=this.createShader(this.gl.FRAGMENT_SHADER,this.fragmentShaderSrc);if(this.gl.attachShader(this.program,e),this.gl.attachShader(this.program,t),this.gl.linkProgram(this.program),this.gl.deleteShader(e),this.gl.deleteShader(t),!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.resizeCanvas(),this.gl.useProgram(this.program),this.initializeUniform("uResolution","float",[this.canvas.width,this.canvas.height]),this.initializeUniform("uCursor","float",[.5,.5]),this.initializeUniform("uTime","float",0),this.initializeUniform("uFrame","int",0)}createShader(i,e){let t=this.gl.createShader(i);if(this.gl.shaderSource(t,e),this.gl.compileShader(t),!this.gl.getShaderParameter(t,this.gl.COMPILE_STATUS))throw console.error("Shader compilation failed:",e),console.error(this.gl.getShaderInfoLog(t)),this.gl.deleteShader(t),new Error("Shader compilation failed");return t}setupBuffer(i){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(i),this.gl.vertexAttribPointer(i,2,this.gl.FLOAT,!1,0,0)}resizeCanvas(){let i=window.devicePixelRatio||1,e=this.canvas.clientWidth*i,t=this.canvas.clientHeight*i,r=getComputedStyle(this.canvas),s=r.width!==`${this.canvas.width}px`&&r.width!=="auto",n=r.height!==`${this.canvas.height}px`&&r.height!=="auto";(!s||!n)&&(this.canvas.style.width=`${this.canvas.clientWidth}px`,this.canvas.style.height=`${this.canvas.clientHeight}px`),(this.canvas.width!==e||this.canvas.height!==t)&&(this.canvas.width=e,this.canvas.height=t,this.gl.viewport(0,0,this.gl.drawingBufferWidth,this.gl.drawingBufferHeight),this.uniforms.has("uResolution")&&this.updateUniforms({uResolution:[this.canvas.width,this.canvas.height]}))}addEventListeners(){let i=(e,t)=>{if(!this.uniforms.has("uCursor"))return;let r=this.canvas.getBoundingClientRect(),s=(e-r.left)/r.width,n=1-(t-r.top)/r.height;this.updateUniforms({uCursor:[s,n]})};this.eventListeners.set("mousemove",e=>{let t=e;this.isTouchDevice||i(t.clientX,t.clientY)}),this.eventListeners.set("touchmove",e=>{let t=e;t.touches.length>0&&i(t.touches[0].clientX,t.touches[0].clientY)}),this.eventListeners.set("touchstart",()=>{this.isTouchDevice=!0}),this.eventListeners.forEach((e,t)=>{this.canvas.addEventListener(t,e)})}initializeUniform(i,e,t){if(this.uniforms.has(i))throw new Error(`Uniform '${i}' is already initialized.`);if(e!=="float"&&e!=="int")throw new Error(`Invalid uniform type: ${e}. Expected 'float' or 'int'.`);let r=this.gl.getUniformLocation(this.program,i);if(!r){console.log(`Uniform ${i} not found in fragment shader. Skipping initialization.`);return}if(Array.isArray(t)||(t=[t]),t.length<1||t.length>4)throw new Error(`Invalid uniform value length: ${t.length}. Expected a length between 1 and 4.`);let s=t.length;this.uniforms.set(i,{type:e,length:s,location:r}),this.updateUniforms({[i]:t})}updateUniforms(i){Object.entries(i).forEach(([e,t])=>{if(!this.uniforms.has(e))throw new Error(`Uniform '${e}' is not initialized.`);let r=this.uniforms.get(e);if(Array.isArray(t)||(t=[t]),t.length!==r.length)throw new Error(`Invalid uniform value length: ${t.length}. Expected ${r.length}.`);this.gl[`uniform${r.length}${r.type.charAt(0)}`](r.location,...t)})}step(i){i/=1e3,this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.uniforms.has("uTime")&&this.updateUniforms({uTime:i}),this.uniforms.has("uFrame")&&this.updateUniforms({uFrame:this.frame}),++this.frame,this.gl.drawArrays(this.gl.TRIANGLES,0,6)}play(i){let e=t=>{this.step(t),i&&i(t,this.frame),this.animationFrameId=requestAnimationFrame(e)};this.animationFrameId=requestAnimationFrame(e)}pause(){this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null)}save(i){this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.gl.drawArrays(this.gl.TRIANGLES,0,6);let e=this.canvas.toDataURL();i&&!`${i}`.toLowerCase().endsWith(".png")&&(i=`${i}.png`),this.downloadLink.download=i||"export.png",this.downloadLink.href=e,this.downloadLink.click()}initializeTexture(i,e){if(this.textures.has(i))throw new Error(`Texture '${i}' is already initialized.`);let t=this.gl.createTexture();if(!t)throw new Error("Failed to create texture");let r=this.textures.size;this.gl.activeTexture(this.gl.TEXTURE0+r),this.gl.bindTexture(this.gl.TEXTURE_2D,t),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(i,{texture:t,unitIndex:r}),this.updateTextures({[i]:e});let s=this.gl.getUniformLocation(this.program,i);s&&this.gl.uniform1i(s,r)}updateTextures(i){Object.entries(i).forEach(([e,t])=>{let r=this.textures.get(e);if(!r)throw new Error(`Texture '${e}' 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,t)})}destroy(){this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null),this.resizeObserver.unobserve(this.canvas),this.eventListeners.forEach((i,e)=>{this.canvas.removeEventListener(e,i)}),this.program&&this.gl.deleteProgram(this.program),this.textures.forEach(i=>{this.gl.deleteTexture(i.texture)}),this.buffer&&(this.gl.deleteBuffer(this.buffer),this.buffer=null),this.isInternalCanvas&&this.canvas.remove()}},l=a;export{l as default};
9
9
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["const defaultVertexShaderSrc = `\nattribute vec2 aPosition;\nvarying vec2 vUv;\nvoid main() {\n vUv = aPosition * 0.5 + 0.5;\n gl_Position = vec4(aPosition, 0.0, 1.0);\n}\n`;\n\ninterface Uniform {\n\ttype: 'float' | 'int';\n\tlength: 1 | 2 | 3 | 4;\n\tlocation: WebGLUniformLocation;\n}\n\nclass Shader {\n\tprivate canvas: HTMLCanvasElement;\n\tprivate gl: WebGLRenderingContext;\n\tprivate downloadLink: HTMLAnchorElement;\n\tprivate fragmentShaderSrc: string;\n\tprivate uniforms: Map<string, Uniform> = new Map();\n\tprivate animationFrameId: number | null;\n\tprivate resizeObserver: ResizeObserver;\n\tprivate program: WebGLProgram | null = null;\n\n\tconstructor(fragmentShaderSrc: string, canvas: HTMLCanvasElement | null = null) {\n\t\tthis.canvas = canvas || document.createElement('canvas');\n\t\tif (!canvas) {\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\t\tthis.gl = this.canvas.getContext('webgl')!;\n\t\tthis.downloadLink = document.createElement('a');\n\t\tthis.fragmentShaderSrc = fragmentShaderSrc;\n\t\tthis.animationFrameId = null;\n\t\tthis.resizeObserver = new ResizeObserver(() => this.resizeCanvas());\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 = defaultVertexShaderSrc;\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\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\tthis.program = null;\n\t\t}\n\n\t\tif (!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.resizeCanvas();\n\n\t\tthis.gl.useProgram(this.program);\n\n\t\tthis.initializeUniform('uResolution', 'float', [this.canvas.width, this.canvas.height]);\n\t\tthis.initializeUniform('uCursor', 'float', [0.5, 0.5]);\n\t\tthis.initializeUniform('uTime', 'float', 0);\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\tconst buffer = this.gl.createBuffer();\n\t\tthis.gl.bindBuffer(this.gl.ARRAY_BUFFER, buffer);\n\t\tthis.gl.bufferData(this.gl.ARRAY_BUFFER, quadVertices, this.gl.STATIC_DRAW);\n\n\t\tthis.gl.viewport(0, 0, this.canvas.width, this.canvas.height);\n\t\tthis.gl.bindBuffer(this.gl.ARRAY_BUFFER, buffer);\n\t\tthis.gl.enableVertexAttribArray(aPosition);\n\t\tthis.gl.vertexAttribPointer(aPosition, 2, this.gl.FLOAT, false, 0, 0);\n\t}\n\n\tprivate resizeCanvas() {\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\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('uResolution')) {\n\t\t\t\tthis.updateUniforms({ uResolution: [this.canvas.width, this.canvas.height] });\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate addEventListeners() {\n\t\tlet isTouchDevice = false;\n\n\t\tconst updateCursor = (x: number, y: number) => {\n\t\t\tif (!this.uniforms.has('uCursor')) return;\n\t\t\tconst rect = this.canvas.getBoundingClientRect();\n\t\t\tconst cursorX = (x - rect.left) / rect.width;\n\t\t\tconst cursorY = 1 - (y - rect.top) / rect.height; // Flip Y for WebGL\n\t\t\tthis.updateUniforms({ uCursor: [cursorX, cursorY] });\n\t\t};\n\n\t\tthis.canvas.addEventListener('mousemove', event => {\n\t\t\tif (!isTouchDevice) {\n\t\t\t\tupdateCursor(event.clientX, event.clientY);\n\t\t\t}\n\t\t});\n\n\t\tthis.canvas.addEventListener('touchstart', () => {\n\t\t\tisTouchDevice = true;\n\t\t});\n\n\t\tthis.canvas.addEventListener('touchmove', event => {\n\t\t\tif (event.touches.length > 0) {\n\t\t\t\tupdateCursor(event.touches[0].clientX, event.touches[0].clientY);\n\t\t\t}\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 (!this.program) {\n\t\t\tthrow new Error('WebGL program is not 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.log(`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\tplay(callback?: (time: number) => void) {\n\t\tconst renderFrame = (time: number) => {\n\t\t\ttime /= 1000; // Convert from milliseconds to seconds.\n\n\t\t\tthis.gl.clear(this.gl.COLOR_BUFFER_BIT);\n\n\t\t\tif (this.uniforms.has('uTime')) {\n\t\t\t\tthis.updateUniforms({ uTime: time });\n\t\t\t}\n\n\t\t\tif (callback) callback(time);\n\n\t\t\tthis.gl.drawArrays(this.gl.TRIANGLES, 0, 6);\n\t\t\tthis.animationFrameId = requestAnimationFrame(renderFrame);\n\t\t};\n\n\t\tthis.animationFrameId = requestAnimationFrame(renderFrame);\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\nexport default Shader;\n"],"mappings":"AAAA,IAAMA,EAAyB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAezBC,EAAN,KAAa,CAUZ,YAAYC,EAA2BC,EAAmC,KAAM,CALhF,KAAQ,SAAiC,IAAI,IAG7C,KAAQ,QAA+B,KAGtC,KAAK,OAASA,GAAU,SAAS,cA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AAQ,CACH,KAAK,mBACR,qBAAqB,KAAK,gBAAgB,EAC1C,KAAK,iBAAmB,KAE1B,CAEA,KAAKE,EAAkB,CACtB,KAAK,GAAG,MAAM,KAAK,GAAG,gBAAgB,EACtC,KAAK,GAAG,WAAW,KAAK,GAAG,UAAW,EAAG,CAAC,EAE1C,IAAMC,EAAQ,KAAK,OAAO,UAAU,EAChCD,GAAY,CAAC,GAAGA,CAAQ,GAAG,YAAY,EAAE,SAAS,MAAM,IAC3DA,EAAW,GAAGA,CAAQ,QAEvB,KAAK,aAAa,SAAWA,GAAY,aACzC,KAAK,aAAa,KAAOC,EACzB,KAAK,aAAa,MAAM,CACzB,CACD,EAEOC,EAAQlC","names":["defaultVertexShaderSrc","Shader","fragmentShaderSrc","canvas","vertexShaderSrc","vertexShader","fragmentShader","aPosition","type","source","shader","quadVertices","buffer","pixelRatio","width","height","isTouchDevice","updateCursor","x","y","rect","cursorX","cursorY","event","name","value","location","length","updates","uniform","callback","renderFrame","time","filename","image","index_default"]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["const defaultVertexShaderSrc = `\nattribute vec2 aPosition;\nvarying vec2 vUv;\nvoid main() {\n vUv = aPosition * 0.5 + 0.5;\n gl_Position = vec4(aPosition, 0.0, 1.0);\n}\n`;\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\nclass ShaderPad {\n\tprivate isInternalCanvas = false;\n\tprivate isTouchDevice = false;\n\tprivate canvas: HTMLCanvasElement;\n\tprivate gl: WebGLRenderingContext;\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 eventListeners: Map<string, EventListener> = new Map();\n\tprivate frame = 0;\n\n\tconstructor(fragmentShaderSrc: string, canvas: HTMLCanvasElement | null = null) {\n\t\tthis.canvas = canvas || document.createElement('canvas');\n\t\tif (!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\t\tthis.gl = this.canvas.getContext('webgl')!;\n\t\tthis.downloadLink = document.createElement('a');\n\t\tthis.fragmentShaderSrc = fragmentShaderSrc;\n\t\tthis.animationFrameId = null;\n\t\tthis.resizeObserver = new ResizeObserver(() => this.resizeCanvas());\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 = defaultVertexShaderSrc;\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.resizeCanvas();\n\n\t\tthis.gl.useProgram(this.program);\n\n\t\tthis.initializeUniform('uResolution', 'float', [this.canvas.width, this.canvas.height]);\n\t\tthis.initializeUniform('uCursor', 'float', [0.5, 0.5]);\n\t\tthis.initializeUniform('uTime', 'float', 0);\n\t\tthis.initializeUniform('uFrame', 'int', 0);\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\t// TODO: This breaks for `position: fixed; inset: 0` canvases.\n\tprivate resizeCanvas() {\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('uResolution')) {\n\t\t\t\tthis.updateUniforms({ uResolution: [this.canvas.width, this.canvas.height] });\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('uCursor')) return;\n\t\t\tconst rect = this.canvas.getBoundingClientRect();\n\t\t\tconst cursorX = (x - rect.left) / rect.width;\n\t\t\tconst cursorY = 1 - (y - rect.top) / rect.height; // Flip Y for WebGL\n\t\t\tthis.updateUniforms({ uCursor: [cursorX, cursorY] });\n\t\t};\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\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\t\tthis.eventListeners.set('touchstart', () => {\n\t\t\tthis.isTouchDevice = true;\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.log(`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\ttime /= 1000; // Convert from milliseconds to seconds.\n\n\t\tthis.gl.clear(this.gl.COLOR_BUFFER_BIT);\n\n\t\tif (this.uniforms.has('uTime')) {\n\t\t\tthis.updateUniforms({ uTime: time });\n\t\t}\n\n\t\tif (this.uniforms.has('uFrame')) {\n\t\t\tthis.updateUniforms({ uFrame: this.frame });\n\t\t}\n\n\t\t++this.frame;\n\t\tthis.gl.drawArrays(this.gl.TRIANGLES, 0, 6);\n\t}\n\n\tplay(callback?: (time: number, frame: number) => void) {\n\t\tconst loop = (time: number) => {\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;\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 vUv 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.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,EAAyB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,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AK,GAAG,SAAWc,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,cAAejC,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,CAACkB,EAAUH,IAAU,CAChD,KAAK,OAAO,oBAAoBA,EAAOG,CAAQ,CAChD,CAAC,EAEG,KAAK,SACR,KAAK,GAAG,cAAc,KAAK,OAAO,EAGnC,KAAK,SAAS,QAAQY,GAAW,CAChC,KAAK,GAAG,cAAcA,EAAQ,OAAO,CACtC,CAAC,EAEG,KAAK,SACR,KAAK,GAAG,aAAa,KAAK,MAAM,EAChC,KAAK,OAAS,MAGX,KAAK,kBACR,KAAK,OAAO,OAAO,CAErB,CACD,EAEOI,EAAQ1C","names":["defaultVertexShaderSrc","ShaderPad","fragmentShaderSrc","canvas","vertexShaderSrc","vertexShader","fragmentShader","aPosition","type","source","shader","quadVertices","pixelRatio","width","height","computedStyle","hasExplicitWidth","hasExplicitHeight","updateCursor","x","y","rect","cursorX","cursorY","event","mouseEvent","touchEvent","listener","name","value","location","length","updates","uniform","time","callback","loop","filename","image","texture","unitIndex","uSampler","info","index_default"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shaderpad",
3
- "version": "1.0.0-alpha.2",
3
+ "version": "1.0.0-alpha.4",
4
4
  "description": "A lightweight, dependency-free library to reduce boilerplate when writing fragment shaders.",
5
5
  "keywords": [
6
6
  "shaders",