+0
Daily
5,600
stars
332
forks
79
watchers
386
prs
328
issues
0
releases
README
👑
regl
Fast functional WebGL
Docs
|
Chat
|
Download
|
Minified
Example
regl simplifies WebGL programming by removing as much shared state as it can get away with. To do this, it replaces the WebGL API with two fundamental abstractions, resources and commands:
A resource is a handle to a GPU resident object, like a texture, FBO or buffer.
A command is a complete representation of the WebGL state required to perform some draw call.
To define a command you specify a mixture of static and dynamic data for the object. Once this is done, regl takes this description and then compiles it into optimized JavaScript code. For example, here is a simple regl program to draw a triangle:
js
// Calling the regl module with no arguments creates a full screen canvas and
// WebGL context, and then uses this context to initialize a new REGL instance
const regl = require('regl')()
// Calling regl() creates a new partially evaluated draw command
const drawTriangle = regl({
// Shaders in regl are just strings. You can use glslify or whatever you want
// to define them. No need to manually create shader objects.
frag:
precision mediump float;
uniform vec4 color;
void main() {
gl_FragColor = color;
},
vert:
precision mediump float;
attribute vec2 position;
void main() {
gl_Position = vec4(position, 0, 1);
},
// Here we define the vertex attributes for the above shader
attributes: {
// regl.buffer creates a new array buffer object
position: regl.buffer([
[-2, -2], // no need to flatten nested arrays, regl automatically
[4, -2], // unrolls them into a typedarray (default Float32)
[4, 4]
])
// regl automatically infers sane defaults for the vertex attribute pointers
},
uniforms: {
// This defines the color of the triangle to be a dynamic variable
color: regl.prop('color')
},
// This tells regl the number of vertices to draw in this command
count: 3
})
// regl.frame() wraps requestAnimationFrame and also handles viewport changes
regl.frame(({time}) = {
// clear contents of the drawing buffer
regl.clear({
color: [0, 0, 0, 0],
depth: 1
})
// draw a triangle using the command defined above
drawTriangle({
color: [
Math.cos(time 0.001),
Math.sin(time 0.0008),
Math.cos(time 0.003),
1
]
})
})
See this example live
More examples
Check out the gallery. The source code of all the gallery examples can be found here.
Setup
regl has no dependencies, so setting it up is pretty easy. There are 2 basic ways to do this:
npm
The easiest way to use regl in a project is via npm. Once you have node set up, you can install and use regl in your project using the following command:
sh
npm i -S regl
For more info on how to use npm, check out the official docs.
If you are using Top issues
#378Investigate WebGL 2.0 support22 comments · 2016-11-13#537Update link to documentation 13 comments · 2019-06-13#398live-coding11 comments · 2017-01-20#386Try-catch inside regl.frame() loop11 comments · 2016-12-16#186Skeletal animation demo11 comments · 2016-08-01#406Use cases/testimonials10 comments · 2017-03-31#143Cross browser testing10 comments · 2016-07-27#455How to change FPS?9 comments · 2018-01-29