Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@plotly/regl
Advanced tools
@plotly/regl is a package that provides a functional abstraction for WebGL, making it easier to create complex visualizations and graphics in the browser. It is particularly useful for creating high-performance, interactive 2D and 3D graphics.
Drawing a Simple Triangle
This code demonstrates how to draw a simple red triangle using @plotly/regl. It sets up the fragment and vertex shaders, defines the triangle's vertices, and renders it in a loop.
const regl = require('@plotly/regl')();
const drawTriangle = regl({
frag: `
precision mediump float;
void main() {
gl_FragColor = vec4(1, 0, 0, 1); // Red color
}
`,
vert: `
precision mediump float;
attribute vec2 position;
void main() {
gl_Position = vec4(position, 0, 1);
}
`,
attributes: {
position: [
[-1, 0],
[0, -1],
[1, 1]
]
},
count: 3
});
regl.frame(() => {
regl.clear({
color: [0, 0, 0, 1],
depth: 1
});
drawTriangle();
});
Creating a 3D Cube
This code demonstrates how to create and render a rotating 3D cube using @plotly/regl. It sets up the shaders, defines the cube's vertices and faces, and uses a perspective projection to render the cube in 3D space.
const regl = require('@plotly/regl')();
const mat4 = require('gl-mat4');
const drawCube = regl({
frag: `
precision mediump float;
void main() {
gl_FragColor = vec4(0, 0, 1, 1); // Blue color
}
`,
vert: `
precision mediump float;
attribute vec3 position;
uniform mat4 model, view, projection;
void main() {
gl_Position = projection * view * model * vec4(position, 1);
}
`,
attributes: {
position: [
[-1, -1, -1], [1, -1, -1], [1, 1, -1], [-1, 1, -1],
[-1, -1, 1], [1, -1, 1], [1, 1, 1], [-1, 1, 1]
]
},
elements: [
[0, 1, 2], [2, 3, 0],
[4, 5, 6], [6, 7, 4],
[0, 1, 5], [5, 4, 0],
[2, 3, 7], [7, 6, 2],
[0, 3, 7], [7, 4, 0],
[1, 2, 6], [6, 5, 1]
],
uniforms: {
model: mat4.identity([]),
view: ({tick}) => mat4.lookAt([], [Math.cos(tick * 0.01) * 5, 2.5, Math.sin(tick * 0.01) * 5], [0, 0, 0], [0, 1, 0]),
projection: ({viewportWidth, viewportHeight}) => mat4.perspective([], Math.PI / 4, viewportWidth / viewportHeight, 0.01, 1000)
}
});
regl.frame(() => {
regl.clear({
color: [0, 0, 0, 1],
depth: 1
});
drawCube();
});
Three.js is a popular JavaScript library for creating 3D graphics in the browser. It provides a higher-level abstraction compared to @plotly/regl, making it easier to create complex 3D scenes with less code. However, it may not offer the same level of performance optimization and control as @plotly/regl.
PixiJS is a 2D rendering engine that uses WebGL for high-performance graphics. It is designed for creating interactive graphics and games, offering a simpler API compared to @plotly/regl. While it excels in 2D graphics, it does not provide the same level of support for 3D graphics as @plotly/regl.
Babylon.js is a powerful, open-source 3D engine that allows you to create stunning 3D experiences in the browser. It provides a comprehensive set of features for 3D rendering, physics, and more. Compared to @plotly/regl, Babylon.js offers a more feature-rich environment but with a higher level of abstraction.
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:
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:
// 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
Check out the gallery. The source code of all the gallery examples can be found here.
regl
has no dependencies, so setting it up is pretty easy. There are 3 basic ways to do this:
To try out regl right away, you can use the live editor in the gallery.
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:
npm i -S regl
For more info on how to use npm, check out the official docs.
If you are using npm, you may also want to try budo
which is a live development server.
By default if you compile regl
with browserify
then all error messages and run time checks are removed. This is done to reduce the size of the final bundle. If you are developing an application, you should run browserify using the --debug
flag in order to enable error messages. This will also generate source maps which make reading the source code of your application easier.
You can also use regl
as a standalone script if you are really stubborn. The most recent versions can be found in the dist/
folder and is also available from npm cdn in both minified and unminified versions.
There are some difference when using regl
in standalone. Because script tags don't assume any sort of module system, the standalone scripts inject a global constructor function which is equivalent to the module.exports
of regl
:
<!DOCTYPE html>
<html>
<head>
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<meta charset=utf-8>
</head>
<body>
</body>
<script language="javascript" src="https://npmcdn.com/regl/dist/regl.js"></script>
<script language="javascript">
var regl = createREGL()
regl.frame(function () {
regl.clear({
color: [0, 0, 0, 1]
})
})
</script>
</html>
regl
regl
just removes shared state from WebGL. You can do anything you could in regular WebGL with little overhead and way less debugging. regl
emphasizes the following values:
regl
has more than 30,000 unit tests and above 95% code coverage. In development mode, regl
performs strong validation and sanity checks on all input data to help you catch errors faster.regl
uses dynamic code generation and partial evaluation to remove almost all overhead.regl
just wraps WebGL. It is not a game engine and doesn't have opinions about scene graphs or vector math libraries. Any feature in WebGL is accessible, including advanced extensions like multiple render targets or instancing.regl
takes interface compatibility and semantic versioning seriously, making it well suited for long lived applications that must be supported for months or years down the road. It also has no dependencies limiting exposure to risky or unplanned updates.While regl
is lower level than many 3D engines, code written in it tends to be highly compact and flexible. A comparison of regl
to various other WebGL libraries across several tasks can be found here.
In order to prevent performance regressions, regl
is continuously
benchmarked. You can run benchmarks locally using npm run bench
or
check them out
online. The
results for the last few days can be found
here.
These measurements were taken using our custom scripts bench-history
and
bench-graph
. You can read more about them in the development guide.
The following is an incomplete list of projects using regl:
If you have a project using regl that isn't on this list that you would like to see added, please send us a pull request!
regl is still under active developement, and anyone willing to contribute is very much welcome to do so. Right now, what we need the most is for people to write examples and demos with the framework. This will allow us to find bugs and deficiencies in the API. We have a list of examples we would like to be implemented here, but you are of course welcome to come up with your own examples. To add an example to our gallery of examples, please send us a pull request!
regl
has extensive API documentation. You can browse the docs online here.
The latest changes in regl
can be found in the CHANGELOG.
For info on how to build and test headless, see the contributing guide here
All code (c) 2016 MIT License
Development supported by the Freeman Lab and the Howard Hughes Medical Institute (@freeman-lab on GitHub)
Many examples use creative commons or public domain artwork for illustrative purposes. These assets are not included in any of the redistributable packages of regl.
audio.js
example is "Bamboo Cactus" by 8bitpeoples. CC BY-ND-NC 1.0 licenseFAQs
regl is a fast functional WebGL framework.
The npm package @plotly/regl receives a total of 130,691 weekly downloads. As such, @plotly/regl popularity was classified as popular.
We found that @plotly/regl demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 11 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.