The code you wrote really does nothing but define some points. To do this, in WebGL, you could do this as follows:
var colors = []; var verts = []; var theta=0 for(var radius=60.0; radius>1.0; radius-=0.3) { colors.push(radius/60.0, 0.3, 1-(radius/60.0)); verts.push(200+radius*Math.cos(theta),200+radius*Math.sin(theta)); theta+=0.1; } var numPoints = colors.length / 3;
This will create 2 JavaScript arrays. Then you will need to put them in WebGLBuffers
var colorBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW); var vertBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vertBuffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(verts), gl.STATIC_DRAW);
After that, although you need to write a shader and configure it. Shaders are a huge topic. For your specific data, I assume that these shaders will do
Vertex shader
uniform mat4 u_matrix; attribute vec4 a_vertex; attribute vec4 a_color; varying vec4 v_color; void main() {
Fragment shader
precision mediump float; varying vec4 v_color; void main() { gl_FragColor = v_color; }
Then you need to initialize the shaders and parameters. I assume that I put shaders in script tags with the identifiers "vshader" and "fshader" and used this template code to load them.
var program = createProgramFromScriptTags(gl, "vshader", "fshader"); gl.useProgram(program); // look up the locations for the inputs to our shaders. var u_matLoc = gl.getUniformLocation(program, "u_matrix"); var colorLoc = gl.getAttribLocation(program, "a_color"); var vertLoc = gl.getAttribLocation(program, "a_vertex"); // Set the matrix to some that makes 1 unit 1 pixel. gl.uniformMatrix4fv(u_matLoc, false, [ 2 / width, 0, 0, 0, 0, 2 / height, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ]); // Tell the shader how to get data out of the buffers. gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); gl.vertexAttribPointer(colorLoc, 3, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(colorLoc); gl.bindBuffer(gl.ARRAY_BUFFER, vertBuffer); gl.vertexAttribPointer(vertLoc, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(vertLoc);
and finally draw the points
gl.drawArrays(gl.POINTS, 0, numPoints);
Here is a fragment
var canvas = document.getElementById("c"); var gl = canvas.getContext("webgl"); if (!gl) { alert("no WebGL");
canvas { border: 1px solid black; }
<script src="https://twgljs.org/dist/3.x/twgl.min.js"></script> <script id="vshader" type="whatever"> uniform mat4 u_matrix; attribute vec4 a_vertex; attribute vec4 a_color; varying vec4 v_color; void main() { </script> <script id="fshader" type="whatever"> precision mediump float; varying vec4 v_color; void main() { gl_FragColor = v_color; } </script> <canvas id="c" width="400" height="400"></canvas>
You may find these WebGL guides useful .