Changed around line 1
+ const canvas = document.getElementById('shader-canvas');
+ const gl = canvas.getContext('webgl');
+ const codeInput = document.getElementById('glsl-code');
+
+ let shaderProgram;
+ let vertexShader;
+ let fragmentShader;
+
+ function compileShader(source, type) {
+ const shader = gl.createShader(type);
+ gl.shaderSource(shader, source);
+ gl.compileShader(shader);
+
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
+ console.error('Shader compilation error:', gl.getShaderInfoLog(shader));
+ return null;
+ }
+ return shader;
+ }
+
+ function createProgram(vertexSource, fragmentSource) {
+ vertexShader = compileShader(vertexSource, gl.VERTEX_SHADER);
+ fragmentShader = compileShader(fragmentSource, gl.FRAGMENT_SHADER);
+
+ shaderProgram = gl.createProgram();
+ gl.attachShader(shaderProgram, vertexShader);
+ gl.attachShader(shaderProgram, fragmentShader);
+ gl.linkProgram(shaderProgram);
+
+ if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
+ console.error('Program linking error:', gl.getProgramInfoLog(shaderProgram));
+ }
+
+ return shaderProgram;
+ }
+
+ function render() {
+ if (!shaderProgram) return;
+
+ const vertices = new Float32Array([
+ -1, -1,
+ 1, -1,
+ -1, 1,
+ 1, 1
+ ]);
+
+ const buffer = gl.createBuffer();
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
+ gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
+
+ const positionLocation = gl.getAttribLocation(shaderProgram, 'a_position');
+ gl.enableVertexAttribArray(positionLocation);
+ gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
+
+ gl.useProgram(shaderProgram);
+ gl.viewport(0, 0, canvas.width, canvas.height);
+ gl.clear(gl.COLOR_BUFFER_BIT);
+ gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
+ }
+
+ function updateShader() {
+ const vertexSource = `
+ attribute vec2 a_position;
+ void main() {
+ gl_Position = vec4(a_position, 0, 1);
+ }
+ `;
+
+ const fragmentSource = `
+ precision highp float;
+ uniform float u_time;
+ uniform vec2 u_resolution;
+
+ ${codeInput.value}
+
+ void main() {
+ vec2 uv = gl_FragCoord.xy / u_resolution;
+ gl_FragColor = vec4(mainImage(uv), 1.0);
+ }
+ `;
+
+ createProgram(vertexSource, fragmentSource);
+ render();
+ }
+
+ function resizeCanvas() {
+ canvas.width = canvas.clientWidth;
+ canvas.height = canvas.clientHeight;
+ render();
+ }
+
+ window.addEventListener('resize', resizeCanvas);
+ codeInput.addEventListener('input', updateShader);
+
+ // Initial setup
+ resizeCanvas();
+ updateShader();