WebGL error - "glDrawArrays: attempt to access out of range vertices in attribute 0"
<h2>Edit:</h2>

<p>After grueling reviewal of the code I happened to make the right alteration to have this triangle render.  In the code posted here, the vertices and colors Arrays are declared before <code>initBuffers</code>.  In my actual code, they are declared somewhat after.  Because javascript (in sublime text) was auto-completing these variables no matter where I defined them (like methods in a java class, say..) and because chrome was throwing no errors, I assumed this could not be the problem.  Yet simply shifting these array initializations from after <code>initShaders</code>, <code>initBuffers</code> to before those calls was the crux of my problem.  I'm sorry for posting a fundamental unanswerable question but hopefully this will serve as a helpful warning to future people that declaration time matters with regards to webGL buffers.  More rigorous checking of shader compilation/linking may yield better information here.</p>

<p>///////////////////</p>

<p>I'm taking some WebGL code I found in a tutorial and trying to refactor it to use code-specified color values for each vertex.  Unfortunately, after working this code it, I receive the aforementioned "out of range" error.</p>

<p>node - any not seen declarations are global vars  </p>

<p>I have the following setup code, initializing buffers:</p>

<p>The vertex information is specified like so:</p>

<pre><code>var vertices = [
        0.0,  1.0,
        -1.0, -1.0,
         1.0, -1.0
];


var colors = [

    1, 0, 0,
    1, 0, 0,
    1, 0, 0

];




var gl;

var shaderProgram;


var triangleVertexPositionBuffer;
var triangleVertexColorBuffer;


var canvas = document.getElementById("ABID");
gl = canvas.getContext("experimental-webgl");
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;



function initBuffers() {

    triangleVertexPositionBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
    triangleVertexPositionBuffer.itemSize = 2;



    triangleVertexColorBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexColorBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
    triangleVertexColorBuffer.itemSize = 3;


}


initBuffers();
</code></pre>

<p>Then creating my shaders/program where I try to get attribute locations and set them on the global variable, 'shaderProgram':</p>

<pre><code>function initShaders() {

    var fragmentShader = getShader(gl, "shader-fs");
    var vertexShader = getShader(gl, "shader-vs");

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

    if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
        alert("Could not initialise shaders");
    }

    gl.useProgram(shaderProgram);

    shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
    shaderProgram.vertexColorAttrib = gl.getAttribLocation(shaderProgram, "aVertexColor");

    gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
    gl.enableVertexAttribArray(shaderProgram.vertexColorAttrib);


}
</code></pre>

<p>It depends on a getShader function which works perfectly.  The shaders are as follows:</p>

<pre><code>&lt;script id="shader-fs" type="x-shader/x-fragment"&gt;
    precision mediump float;

    varying vec3 color;

    void main(void) {
        gl_FragColor = vec4(1,0,0, 1);
    }
&lt;/script&gt;

&lt;script id="shader-vs" type="x-shader/x-vertex"&gt;
    attribute vec2 aVertexPosition;
    attribute vec3 aVertexColor;



    varying vec3 color;

    void main(void) {
        gl_Position = vec4(aVertexPosition, 1.0,1.0);
        color = aVertexColor;
    }
&lt;/script&gt;
</code></pre>

<p>And finally, the error occurs somehow when drawing these 3 vertices:</p>

<pre><code>draw();

function draw() {

    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
    gl.clearColor(0, 0, 0, 1);



    gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
    gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);


    gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexColorBuffer);
    gl.vertexAttribPointer(shaderProgram.vertexColorAttrib, triangleVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);


    gl.drawArrays(gl.TRIANGLES, 0, 3);


}
</code></pre>

<p>I'm sure there is some offset, or size or magic number typed incorrectly but I've been over this many times and cannot spot the error.  Being an openGL beginner and a lack of debugging insights compounds the frustrations.  Any help would be greatly appreciated.</p>