7 border:
2px solid black;
8 content: -webkit-canvas(triangle);
12 <script id=
"vertexShader" type=
"x-shader/x-vertex">
13 attribute vec4 vPosition;
16 gl_Position = vPosition;
20 <script id=
"fragmentShader" type=
"x-shader/x-fragment">
22 gl_FragColor = vec4(
0.8,
0.7,
0,
1.0);
26 <script type=
"application/x-javascript">
27 function getFragmentShader()
29 var shaderNode = document.getElementById(
"fragmentShader"); // fragmentShader has been defined at the top
30 var shaderSource = getShaderSource(shaderNode);
32 var shader = gl.createShader(gl.FRAGMENT_SHADER);
33 gl.shaderSource(shader, shaderSource);
34 gl.compileShader(shader);
39 function getShaderSource(shaderNode)
41 var shaderSource =
"";
42 var node = shaderNode.firstChild;
44 if (node.nodeType ==
3) // Node.TEXT_NODE
45 shaderSource += node.textContent;
46 node = node.nextSibling;
52 function getVertexShader()
54 var shaderNode = document.getElementById(
"vertexShader");
55 var shaderSource = getShaderSource(shaderNode);
57 var shader = gl.createShader(gl.VERTEX_SHADER);
58 gl.shaderSource(shader, shaderSource);
59 gl.compileShader(shader);
66 gl = document.getCSSCanvasContext(
"webgl",
"triangle",
200,
200);
68 var vertexShader = getVertexShader();
69 var fragmentShader = getFragmentShader();
71 var shaderProgram = gl.createProgram();
72 gl.attachShader(shaderProgram, vertexShader);
73 gl.attachShader(shaderProgram, fragmentShader);
74 gl.bindAttribLocation(shaderProgram,
0,
"vPosition"); // vPosition has been defined at the top
75 gl.linkProgram(shaderProgram);
77 gl.useProgram(shaderProgram);
79 var buffer = gl.createBuffer();
80 gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
83 function draw(r, g, b)
85 var vertices = [
0.0,
0.8,
0.0,
88 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
90 gl.vertexAttribPointer(
0,
3, gl.FLOAT, false,
0,
0); // Load the vertex data
91 gl.enableVertexAttribArray(
0);
92 gl.clearColor(r, g, b,
1);
93 gl.clear(gl.COLOR_BUFFER_BIT);
94 gl.drawArrays(gl.TRIANGLES,
0,
3);
97 if (window.testRunner) {
98 testRunner.waitUntilDone();
99 testRunner.overridePreference(
"WebKitWebGLEnabled",
"1");
104 setTimeout(function() {
106 setTimeout(function() {
108 if (window.testRunner)
109 testRunner.notifyDone();