files/en-us/web/api/webglrenderingcontext/bufferdata/index.md
{{APIRef("WebGL")}}{{AvailableInWorkers}}
The WebGLRenderingContext.bufferData() method of the WebGL API initializes and creates the
buffer object's data store.
bufferData(target, size, usage)
bufferData(target, srcData, usage)
target
: A {{domxref("WebGL_API/Types", "GLenum")}} specifying the binding point (target). Possible values:
gl.ARRAY_BUFFER
gl.ELEMENT_ARRAY_BUFFER
When using a {{domxref("WebGL2RenderingContext", "WebGL 2 context", "", 1)}}, the following values are available additionally:
gl.COPY_READ_BUFFER
gl.COPY_WRITE_BUFFER
gl.TRANSFORM_FEEDBACK_BUFFER
gl.UNIFORM_BUFFER
gl.PIXEL_PACK_BUFFER
gl.PIXEL_UNPACK_BUFFER
size
srcData {{optional_inline}}
null, a data store is still created, but the content is uninitialized and undefined.usage
: A {{domxref("WebGL_API/Types", "GLenum")}} specifying the intended usage pattern of the data store for optimization purposes. Possible values:
gl.STATIC_DRAW
gl.DYNAMIC_DRAW
gl.STREAM_DRAW
When using a {{domxref("WebGL2RenderingContext", "WebGL 2 context", "", 1)}}, the following values are available additionally:
gl.STATIC_READ
gl.DYNAMIC_READ
gl.STREAM_READ
gl.STATIC_COPY
gl.DYNAMIC_COPY
gl.STREAM_COPY
None ({{jsxref("undefined")}}).
gl.OUT_OF_MEMORY error is thrown if the context is unable to create
a data store with the given size.gl.INVALID_VALUE error is thrown if size is negative.gl.INVALID_ENUM error is thrown if target or
usage are not one of the allowed enums.const canvas = document.getElementById("canvas");
const gl = canvas.getContext("webgl");
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, 1024, gl.STATIC_DRAW);
To check the current buffer usage and buffer size, use the {{domxref("WebGLRenderingContext.getBufferParameter()")}} method.
gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_SIZE);
gl.getBufferParameter(gl.ARRAY_BUFFER, gl.BUFFER_USAGE);
To calculate size parameter for a typed array.
const dataArray = new Float32Array([1, 2, 3, 4]);
const sizeInBytes = dataArray.length * dataArray.BYTES_PER_ELEMENT;
{{Specifications}}
{{Compat}}