browser/devtools/shadereditor/test/doc_simple-canvas.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <!-- Any copyright is dedicated to the Public Domain.
     2      http://creativecommons.org/publicdomain/zero/1.0/ -->
     3 <!doctype html>
     5 <html>
     6   <head>
     7     <meta charset="utf-8"/>
     8     <title>WebGL editor test page</title>
    10     <script id="shader-vs" type="x-shader/x-vertex">
    11       precision lowp float;
    12       attribute vec3 aVertexPosition;
    13       attribute vec3 aVertexColor;
    14       varying vec3 vFragmentColor;
    16       void main(void) {
    17         gl_Position = vec4(aVertexPosition, 1.0);
    18         vFragmentColor = aVertexColor; // I'm special!
    19       }
    20     </script>
    22     <script id="shader-fs" type="x-shader/x-fragment">
    23       precision lowp float;
    24       varying vec3 vFragmentColor;
    26       void main(void) {
    27         gl_FragColor = vec4(vFragmentColor, 1.0); // I'm also special!
    28       }
    29     </script>
    30   </head>
    32   <body>
    33     <canvas width="512" height="512"></canvas>
    35     <script type="text/javascript;version=1.8">
    36       "use strict";
    38       let canvas, gl;
    39       let program;
    40       let squareVerticesPositionBuffer;
    41       let squareVerticesColorBuffer;
    42       let vertexPositionAttribute;
    43       let vertexColorAttribute;
    45       window.onload = function() {
    46         canvas = document.querySelector("canvas");
    47         gl = canvas.getContext("webgl", { preserveDrawingBuffer: true });
    48         gl.clearColor(0.0, 0.0, 0.0, 1.0);
    50         initProgram();
    51         initBuffers();
    52         drawScene();
    53       }
    55       function initProgram() {
    56         let vertexShader = getShader(gl, "shader-vs");
    57         let fragmentShader = getShader(gl, "shader-fs");
    59         program = gl.createProgram();
    60         gl.attachShader(program, vertexShader);
    61         gl.attachShader(program, fragmentShader);
    62         gl.linkProgram(program);
    64         vertexPositionAttribute = gl.getAttribLocation(program, "aVertexPosition");
    65         gl.enableVertexAttribArray(vertexPositionAttribute);
    67         vertexColorAttribute = gl.getAttribLocation(program, "aVertexColor");
    68         gl.enableVertexAttribArray(vertexColorAttribute);
    69       }
    71       function getShader(gl, id) {
    72         let script = document.getElementById(id);
    73         let source = script.textContent;
    74         let shader;
    76         if (script.type == "x-shader/x-fragment") {
    77           shader = gl.createShader(gl.FRAGMENT_SHADER);
    78         } else if (script.type == "x-shader/x-vertex") {
    79           shader = gl.createShader(gl.VERTEX_SHADER);
    80         }
    82         gl.shaderSource(shader, source);
    83         gl.compileShader(shader);
    85         return shader;
    86       }
    88       function initBuffers() {
    89         squareVerticesPositionBuffer = gl.createBuffer();
    90         gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesPositionBuffer);
    91         gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
    92            1.0,  1.0, 0.0,
    93           -1.0,  1.0, 0.0,
    94            1.0, -1.0, 0.0,
    95           -1.0, -1.0, 0.0
    96         ]), gl.STATIC_DRAW);
    98         squareVerticesColorBuffer = gl.createBuffer();
    99         gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesColorBuffer);
   100         gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
   101           1.0, 1.0, 1.0, 1.0,
   102           1.0, 0.0, 0.0, 1.0,
   103           0.0, 1.0, 0.0, 1.0,
   104           0.0, 0.0, 1.0, 1.0
   105         ]), gl.STATIC_DRAW);
   106       }
   108       function drawScene() {
   109         gl.clear(gl.COLOR_BUFFER_BIT);
   111         gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesPositionBuffer);
   112         gl.vertexAttribPointer(vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0);
   114         gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesColorBuffer);
   115         gl.vertexAttribPointer(vertexColorAttribute, 4, gl.FLOAT, false, 0, 0);
   117         gl.useProgram(program);
   118         gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
   120         window.requestAnimationFrame(drawScene);
   121       }
   122     </script>
   123   </body>
   125 </html>

mercurial