summaryrefslogtreecommitdiffstats
path: root/devtools/client/shadereditor/test/doc_simple-canvas.html
blob: 2a709ad8ec607d126f298287f2edb8e1ccf6a7cc (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!-- Any copyright is dedicated to the Public Domain.
     http://creativecommons.org/publicdomain/zero/1.0/ -->
<!doctype html>

<html>
  <head>
    <meta charset="utf-8"/>
    <title>WebGL editor test page</title>

    <script id="shader-vs" type="x-shader/x-vertex">
      precision lowp float;
      attribute vec3 aVertexPosition;
      attribute vec3 aVertexColor;
      varying vec3 vFragmentColor;

      void main(void) {
        gl_Position = vec4(aVertexPosition, 1.0);
        vFragmentColor = aVertexColor; // I'm special!
      }
    </script>

    <script id="shader-fs" type="x-shader/x-fragment">
      precision lowp float;
      varying vec3 vFragmentColor;

      void main(void) {
        gl_FragColor = vec4(vFragmentColor, 1.0); // I'm also special!
      }
    </script>
  </head>

  <body>
    <canvas width="512" height="512"></canvas>

    <script type="text/javascript;version=1.8">
      "use strict";

      let canvas, gl;
      let program;
      let squareVerticesPositionBuffer;
      let squareVerticesColorBuffer;
      let vertexPositionAttribute;
      let vertexColorAttribute;

      window.onload = function() {
        canvas = document.querySelector("canvas");
        gl = canvas.getContext("webgl", { preserveDrawingBuffer: true });
        gl.clearColor(0.0, 0.0, 0.0, 1.0);

        initProgram();
        initBuffers();
        drawScene();
      }

      function initProgram() {
        let vertexShader = getShader(gl, "shader-vs");
        let fragmentShader = getShader(gl, "shader-fs");

        program = gl.createProgram();
        gl.attachShader(program, vertexShader);
        gl.attachShader(program, fragmentShader);
        gl.linkProgram(program);

        vertexPositionAttribute = gl.getAttribLocation(program, "aVertexPosition");
        gl.enableVertexAttribArray(vertexPositionAttribute);

        vertexColorAttribute = gl.getAttribLocation(program, "aVertexColor");
        gl.enableVertexAttribArray(vertexColorAttribute);
      }

      function getShader(gl, id) {
        let script = document.getElementById(id);
        let source = script.textContent;
        let shader;

        if (script.type == "x-shader/x-fragment") {
          shader = gl.createShader(gl.FRAGMENT_SHADER);
        } else if (script.type == "x-shader/x-vertex") {
          shader = gl.createShader(gl.VERTEX_SHADER);
        }

        gl.shaderSource(shader, source);
        gl.compileShader(shader);

        return shader;
      }

      function initBuffers() {
        squareVerticesPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesPositionBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
           1.0,  1.0, 0.0,
          -1.0,  1.0, 0.0,
           1.0, -1.0, 0.0,
          -1.0, -1.0, 0.0
        ]), gl.STATIC_DRAW);

        squareVerticesColorBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesColorBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
          1.0, 1.0, 1.0, 1.0,
          1.0, 0.0, 0.0, 1.0,
          0.0, 1.0, 0.0, 1.0,
          0.0, 0.0, 1.0, 1.0
        ]), gl.STATIC_DRAW);
      }

      function drawScene() {
        gl.clear(gl.COLOR_BUFFER_BIT);

        gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesPositionBuffer);
        gl.vertexAttribPointer(vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0);

        gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesColorBuffer);
        gl.vertexAttribPointer(vertexColorAttribute, 4, gl.FLOAT, false, 0, 0);

        gl.useProgram(program);
        gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);

        window.requestAnimationFrame(drawScene);
      }
    </script>
  </body>

</html>