4 <style type=
"text/css" media=
"screen">
8 image-rendering: pixelated;
11 image-rendering: auto;
14 <script id=
"vertexShader" type=
"x-shader/x-vertex">
15 attribute vec4 vPosition;
18 gl_Position = vPosition;
22 <script id=
"fragmentShader" type=
"x-shader/x-fragment">
24 gl_FragColor = vec4(
0.0,
0.5,
0.0,
1.0);
28 if (window
.testRunner
) {
29 window
.testRunner
.overridePreference("WebKitWebGLEnabled", "1");
30 window
.testRunner
.dumpAsTextWithPixelResults();
34 var vertices
= [ 0.0, 0.8, 0.0,
37 gl
.bufferData(gl
.ARRAY_BUFFER
, new Float32Array(vertices
), gl
.STATIC_DRAW
);
39 gl
.vertexAttribPointer(0, 3, gl
.FLOAT
, false, 0, 0); // Load the vertex data
40 gl
.enableVertexAttribArray(0);
41 gl
.drawArrays(gl
.TRIANGLES
, 0, 3);
45 function getFragmentShader(gl
) {
46 var shaderNode
= document
.getElementById("fragmentShader"); // fragmentShader has been defined at the top
47 var shaderSource
= getShaderSource(shaderNode
);
49 var shader
= gl
.createShader(gl
.FRAGMENT_SHADER
);
50 gl
.shaderSource(shader
, shaderSource
);
51 gl
.compileShader(shader
);
56 function getShaderSource(shaderNode
) {
57 var shaderSource
= "";
58 var node
= shaderNode
.firstChild
;
60 if (node
.nodeType
== 3) // Node.TEXT_NODE
61 shaderSource
+= node
.textContent
;
62 node
= node
.nextSibling
;
68 function getVertexShader(gl
) {
69 var shaderNode
= document
.getElementById("vertexShader");
70 var shaderSource
= getShaderSource(shaderNode
);
72 var shader
= gl
.createShader(gl
.VERTEX_SHADER
);
73 gl
.shaderSource(shader
, shaderSource
);
74 gl
.compileShader(shader
);
79 function initialize(gl
) {
80 var vertexShader
= getVertexShader(gl
);
81 var fragmentShader
= getFragmentShader(gl
);
83 var shaderProgram
= gl
.createProgram();
84 gl
.attachShader(shaderProgram
, vertexShader
);
85 gl
.attachShader(shaderProgram
, fragmentShader
);
86 gl
.bindAttribLocation(shaderProgram
, 0, "vPosition"); // vPosition has been defined at the top
87 gl
.linkProgram(shaderProgram
);
89 gl
.useProgram(shaderProgram
);
91 var buffer
= gl
.createBuffer();
92 gl
.bindBuffer(gl
.ARRAY_BUFFER
, buffer
);
95 function drawCanvas(canvas
) {
96 var gl
= canvas
.getContext("webgl");
102 drawCanvas(document
.getElementById("canvas0"));
103 drawCanvas(document
.getElementById("canvas1"));
104 drawCanvas(document
.getElementById("canvas2"));
106 document
.getElementById("canvas1").style
.imageRendering
= "auto";
107 document
.getElementById("canvas2").style
.imageRendering
= "pixelated";
111 <body onload=
"drawAll()">
112 <canvas id=
"canvas0" width=
"20" height=
"20"></canvas>
113 <canvas id=
"canvas1" width=
"20" height=
"20"></canvas>
114 <canvas id=
"canvas2" width=
"20" height=
"20"></canvas>