Create and/or return WebGL context for the canvas element, possibly based on options. If container is not defined, document.body is used.
Option
Meaning
canvas
A canvas element to obtain context for.
container
An element to create canvas in and return context for it.
width
If specified, will set the canvas width.
height
If specified, will set the canvas height.
pixelRatio
Multiplier for width and height.
attributes
Attributes object. Available attributes: alpha, depth, stencil, antialias, premultipliedAlpha, preserveDrawingBuffer and failIfMajorPerformanceCaveat.
const getContext = require('gl-util/context')// create canvas element in the document.body and retrieve context for itlet gl = getContext({ attributes: { antialias: true }})
prog = program(gl, prog|vert?, frag?)
Set active program or create a new program from vertex and fragment sources. Programs are cached for the context by source. The WebGLProgram instance is returned.
const program = require('gl-util/program')// create and set programlet prog = program(gl, ` precision mediump float; attribute vec2 position; void main() { gl_Position = vec4(position * 2. - 1., 0, 1); }`, ` precision mediump float; uniform sampler2D image; uniform vec2 shape; uniform float x; void main () { gl_FragColor = texture2D(image, gl_FragCoord.xy / shape); }`)// set active programprogram(gl, prog)
Get/set uniform or multiple uniforms. Returns an object with uniform parameters: {name, location, data, type}. Uniforms are stored per-program instance.
Data passed to texture. Can be array, typed array, image, canvas or string denoting the URL of image to load.
index
Texture unit number, if undefined - calculated automatically.
filter
Sets texture scaling for both min and mag. Can be defined as two separate properties minFilter and magFilter. By default gl.LINEAR.
wrap
Defines texture tiling vertically and horizontally. Can be defined precisely as wrapS and wrapT. By default gl.CLAMP_TO_EDGE, can be gl.MIRRORED_REPEAT or gl..
There are regl, stack.gl and many other WegGL components or frameworks, so why gl-util?
WebGL frameworks API is usually difficult to remember, not much better than pure WebGL, although regl does a great job. gl-util is like functions from any WebGL tutorial - tiny, handy and already familiar.
gl-util does not supersede WebGL API - that allows for debugging pure WebGL at any moment.
gl-util is tiny - if one needs minimalistic WebGL setup it may be better to opt for a couple of functions than massive stack.gl components or regl (70kb+).
regl API may be cumbersome for organizing components
License
(c) 2018 Dmitry Yv. MIT License
so
dy
df.creative@gmail.com
,
1
]);
u.uniform(prog, 'color', [1, .2, 0, 1.]);
gl.drawArrays(gl.TRIANGLES, 0, 3);
type
gl.UNSIGNED_BYTE, can be gl.FLOAT with proper extension enabled
level
0, mipmap level.
gl.STATIC_DRAW
gl.DYNAMIC_DRAW
gl.STREAM_DRAW
normalized
false
If fixed-point data values should be normalized or are to converted to fixed point values when accessed.
index
0
Attribute unit number, detected automatically if omitted.