Draw an image of a triangle using WebGL in a single HTML file. Make the canvas take the full page.
Draw an image of a triangle using WebGL in a single HTML file. Make the canvas take the full page.
Here's a complete HTML file that creates a full-page WebGL canvas with a white triangle on a black background:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | <!DOCTYPE html> <html> <head> <title>WebGL Triangle</title> <style> * { margin: 0; padding: 0; } canvas { display: block; width: 100%; height: 100%; } </style> </head> <body> <canvas id="glCanvas"></canvas> <script> // Initialize WebGL const canvas = document.getElementById('glCanvas'); const gl = canvas.getContext('webgl'); // Set canvas size to full window function resizeCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; gl.viewport(0, 0, canvas.width, canvas.height); } window.addEventListener('resize', resizeCanvas); resizeCanvas(); // Vertex shader const vsSource = ` attribute vec4 aVertexPosition; void main() { gl_Position = aVertexPosition; } `; // Fragment shader const fsSource = ` void main() { gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); // White color } `; // Compile shader function compileShader(gl, source, type) { const shader = gl.createShader(type); gl.shaderSource(shader, source); gl.compileShader(shader); if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { console.error('Shader compile error:', gl.getShaderInfoLog(shader)); gl.deleteShader(shader); return null; } return shader; } // Create shader program const vertexShader = compileShader(gl, vsSource, gl.VERTEX_SHADER); const fragmentShader = compileShader(gl, fsSource, gl.FRAGMENT_SHADER); const shaderProgram = gl.createProgram(); gl.attachShader(shaderProgram, vertexShader); gl.attachShader(shaderProgram, fragmentShader); gl.linkProgram(shaderProgram); if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { console.error('Program link error:', gl.getProgramInfoLog(shaderProgram)); } // Triangle vertices const vertices = new Float32Array([ 0.0, 0.5, 0.0, // Top -0.5, -0.5, 0.0, // Left 0.5, -0.5, 0.0 // Right ]); // Create buffer const vertexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); // Set up vertex attributes const positionLocation = gl.getAttribLocation(shaderProgram, 'aVertexPosition'); gl.enableVertexAttribArray(positionLocation); gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0); // Render gl.useProgram(shaderProgram); gl.clearColor(0.0, 0.0, 0.0, 1.0); // Black background gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLES, 0, 3); </script> </body> </html> |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | <!DOCTYPE html> <html> <head> <title>WebGL Triangle</title> <style> * { margin: 0; padding: 0; } canvas { display: block; width: 100%; height: 100%; } </style> </head> <body> <canvas id="glCanvas"></canvas> <script> // Initialize WebGL const canvas = document.getElementById('glCanvas'); const gl = canvas.getContext('webgl'); // Set canvas size to full window function resizeCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; gl.viewport(0, 0, canvas.width, canvas.height); } window.addEventListener('resize', resizeCanvas); resizeCanvas(); // Vertex shader const vsSource = ` attribute vec4 aVertexPosition; void main() { gl_Position = aVertexPosition; } `; // Fragment shader const fsSource = ` void main() { gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); // White color } `; // Compile shader function compileShader(gl, source, type) { const shader = gl.createShader(type); gl.shaderSource(shader, source); gl.compileShader(shader); if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { console.error('Shader compile error:', gl.getShaderInfoLog(shader)); gl.deleteShader(shader); return null; } return shader; } // Create shader program const vertexShader = compileShader(gl, vsSource, gl.VERTEX_SHADER); const fragmentShader = compileShader(gl, fsSource, gl.FRAGMENT_SHADER); const shaderProgram = gl.createProgram(); gl.attachShader(shaderProgram, vertexShader); gl.attachShader(shaderProgram, fragmentShader); gl.linkProgram(shaderProgram); if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { console.error('Program link error:', gl.getProgramInfoLog(shaderProgram)); } // Triangle vertices const vertices = new Float32Array([ 0.0, 0.5, 0.0, // Top -0.5, -0.5, 0.0, // Left 0.5, -0.5, 0.0 // Right ]); // Create buffer const vertexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); // Set up vertex attributes const positionLocation = gl.getAttribLocation(shaderProgram, 'aVertexPosition'); gl.enableVertexAttribArray(positionLocation); gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0); // Render gl.useProgram(shaderProgram); gl.clearColor(0.0, 0.0, 0.0, 1.0); // Black background gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLES, 0, 3); </script> </body> </html> |
Rendering the webpage gives the following image:
This is a screenshot of a webpage that draws something with webgl. I'm trying to see if it drew what I wanted. Describe what is drawn in this image?
The image shows a white triangle centered on a black background.
Does this answer say that there is a picture of a triangle "The image shows a white triangle centered on a black background."? Answer "Yes it does" or "No it does not".
Yes it does.
Testing if the previous output contains the string es it does
: True
Testing if the previous output contains the string THREE.
: False