shaderpad 1.0.0-alpha.0 → 1.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,6 +1,7 @@
1
1
  declare class Shader {
2
2
  private canvas;
3
3
  private gl;
4
+ private downloadLink;
4
5
  private fragmentShaderSrc;
5
6
  private uniforms;
6
7
  private animationFrameId;
@@ -16,6 +17,7 @@ declare class Shader {
16
17
  updateUniforms(updates: Record<string, number | number[]>): void;
17
18
  play(callback?: (time: number) => void): void;
18
19
  pause(): void;
20
+ save(filename: string): void;
19
21
  }
20
22
 
21
23
  export { Shader as default };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  declare class Shader {
2
2
  private canvas;
3
3
  private gl;
4
+ private downloadLink;
4
5
  private fragmentShaderSrc;
5
6
  private uniforms;
6
7
  private animationFrameId;
@@ -16,6 +17,7 @@ declare class Shader {
16
17
  updateUniforms(updates: Record<string, number | number[]>): void;
17
18
  play(callback?: (time: number) => void): void;
18
19
  pause(): void;
20
+ save(filename: string): void;
19
21
  }
20
22
 
21
23
  export { Shader 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 m=Object.prototype.hasOwnProperty;var d=(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))!m.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={};d(w,{default:()=>p});module.exports=u(w);var v=`
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=`
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.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)}},p=o;
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;
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 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.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\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,CASZ,YAAYC,EAA2BC,EAAmC,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EAEA,KAAK,iBAAmB,sBAAsBA,CAAW,CAC1D,CAEA,OAAQ,CACH,KAAK,mBACR,qBAAqB,KAAK,gBAAgB,EAC1C,KAAK,iBAAmB,KAE1B,CACD,EAEOjC,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"]}
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,O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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,EAEOpC,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"]}
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(r,i=null){this.uniforms=new Map;this.program=null;this.canvas=i||document.createElement("canvas"),i||(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.fragmentShaderSrc=r,this.animationFrameId=null,this.resizeObserver=new ResizeObserver(()=>this.resizeCanvas()),this.resizeObserver.observe(this.canvas),this.init(),this.addEventListeners()}init(){let r=h;if(this.program=this.gl.createProgram(),!this.program)throw new Error("Failed to create WebGL program");let i=this.createShader(this.gl.VERTEX_SHADER,r),t=this.createShader(this.gl.FRAGMENT_SHADER,this.fragmentShaderSrc);if(this.gl.attachShader(this.program,i),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(r,i){let t=this.gl.createShader(r);if(this.gl.shaderSource(t,i),this.gl.compileShader(t),!this.gl.getShaderParameter(t,this.gl.COMPILE_STATUS))throw console.error("Shader compilation failed:",i),console.error(this.gl.getShaderInfoLog(t)),this.gl.deleteShader(t),new Error("Shader compilation failed");return t}setupBuffer(r){let i=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,i,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(r),this.gl.vertexAttribPointer(r,2,this.gl.FLOAT,!1,0,0)}resizeCanvas(){let r=window.devicePixelRatio||1,i=this.canvas.clientWidth*r,t=this.canvas.clientHeight*r;(this.canvas.width!==i||this.canvas.height!==t)&&(this.canvas.width=i,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 r=!1,i=(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=>{r||i(t.clientX,t.clientY)}),this.canvas.addEventListener("touchstart",()=>{r=!0}),this.canvas.addEventListener("touchmove",t=>{t.touches.length>0&&i(t.touches[0].clientX,t.touches[0].clientY)})}initializeUniform(r,i,t){if(this.uniforms.has(r))throw new Error(`Uniform '${r}' is already initialized.`);if(!this.program)throw new Error("WebGL program is not initialized");if(i!=="float"&&i!=="int")throw new Error(`Invalid uniform type: ${i}. Expected 'float' or 'int'.`);let e=this.gl.getUniformLocation(this.program,r);if(!e){console.log(`Uniform ${r} 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(r,{type:i,length:s,location:e}),this.updateUniforms({[r]:t})}updateUniforms(r){Object.entries(r).forEach(([i,t])=>{if(!this.uniforms.has(i))throw new Error(`Uniform '${i}' is not initialized.`);let e=this.uniforms.get(i);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(r){let i=t=>{t/=1e3,this.gl.clear(this.gl.COLOR_BUFFER_BIT),this.uniforms.has("uTime")&&this.updateUniforms({uTime:t}),r&&r(t),this.gl.drawArrays(this.gl.TRIANGLES,0,6),this.animationFrameId=requestAnimationFrame(i)};this.animationFrameId=requestAnimationFrame(i)}pause(){this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null)}},g=n;export{g as default};
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};
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 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.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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KAAK,GAAG,MAAM,KAAK,GAAG,gBAAgB,EAElC,KAAK,SAAS,IAAI,OAAO,GAC5B,KAAK,eAAe,CAAE,MAAOA,CAAK,CAAC,EAGhCF,GAAUA,EAASE,CAAI,EAE3B,KAAK,GAAG,WAAW,KAAK,GAAG,UAAW,EAAG,CAAC,EAC1C,KAAK,iBAAmB,sBAAsBD,CAAW,CAC1D,EAEA,KAAK,iBAAmB,sBAAsBA,CAAW,CAC1D,CAEA,OAAQ,CACH,KAAK,mBACR,qBAAqB,KAAK,gBAAgB,EAC1C,KAAK,iBAAmB,KAE1B,CACD,EAEOE,EAAQhC","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","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\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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,CAAW,CAC1D,CAEA,OAAQ,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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shaderpad",
3
- "version": "1.0.0-alpha.0",
3
+ "version": "1.0.0-alpha.2",
4
4
  "description": "A lightweight, dependency-free library to reduce boilerplate when writing fragment shaders.",
5
5
  "keywords": [
6
6
  "shaders",