Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gl

Package Overview
Dependencies
Maintainers
6
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gl

Creates a WebGL context without a window

  • 2.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
20K
decreased by-10.63%
Maintainers
6
Weekly downloads
 
Created
Source

gl

gl lets you create a WebGL context in node.js without making a window or loading a full browser environment.

It aspires to fully conform to the WebGL 1.0.3 specification.

Example

//Create context
var width   = 64
var height  = 64
var gl = require('gl')(width, height, { preserveDrawingBuffer: true })

//Clear screen to red
gl.clearColor(1, 0, 0, 1)
gl.clear(gl.COLOR_BUFFER_BIT)

//Write output as a PPM formatted image
var pixels = new Uint8Array(width * height * 4)
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
process.stdout.write(['P3\n# gl.ppm\n', width, " ", height, '\n255\n'].join(''))
for(var i=0; i<pixels.length; i+=4) {
  for(var j=0; j<3; ++j) {
    process.stdout.write(pixels[i+j] + ' ')
  }
}

Install

Because gl uses native code, it is a bit more involved to set up than a typical JavaScript npm module. Before you can use it, you will need to ensure that your system has the correct dependencies installed.

System dependencies

For general information on building native modules, see the node-gyp documentation. System specific build instructions are as follows:

Mac OS X
  • Python 2.7
  • XCode
Linux
  • Python 2.7
  • A GNU C++ environment (available via the build-essential package on apt)
  • Working and up to date OpenGL drivers
  • GLEW
Windows
  • Python 2.7
  • Microsoft Visual Studio

npm

Once your system is set up, installing the headless-gl module is pretty easy to do with npm. Just run the following command:

npm i gl

And you are good to go!

API

Context creation

var gl = require('gl')(width, height[, options])

Creates a new WebGLRenderingContext with the given parameters.

  • width is the width of the drawing buffer
  • height is the height of the drawing buffer
  • options is an optional object whose properties are the context attributes for the WebGLRendering context

Returns A new WebGLRenderingContext object

Extra methods

In addition to all of the usual WebGL methods, headless-gl adds the following two methods to each WebGL context in order to support some functionality which would not otherwise be exposed at the WebGL level.

gl.resize(width, height)

Resizes the drawing buffer of a WebGL rendering context

  • width is the new width of the drawing buffer for the context
  • height is the new height of the drawing buffer for the context

Note In the DOM, this method would implemented by resizing the canvas, which is done by modifying the width/height properties.

gl.destroy()

Destroys the WebGL context immediately, reclaiming all resources

Note For long running jobs, garbage collection of contexts is often not fast enough. To prevent the system from becoming overloaded with unused contexts, you can force the system to reclaim a WebGL context immediately by calling .destroy().

More information

Improvements from version 1.0.0

The previous version of gl (aka headless-gl) was pretty much a terrible hack. Thanks to the support of @mapbox and @google's ANGLE project, gl is now actually kind of good! The following things are now way better in version >=2.0.0:

  • Vastly improved conformance
  • Khronos ARB test suite integration via gl-conformance
  • Works on node 0.12
  • Windows and Linux support
  • No default context
  • Added .destroy() and .resize() methods

Why use this thing instead of node-webgl?

It depends on what you are trying to do. node-webgl is good if you are making a graphical application like a game, and allows for access to some features which are not part of ordinary WebGL. On the other hand, because headless-gl does not create any windows, it is suitable for running in a server environment. This means that you can use it to generate figures using OpenGL or perform GPGPU computations using shaders. Also, unlike node-webgl, headless-gl attempts to correctly implement the full WebGL standard making it more reliable.

Why use this thing instead of nw.js/electron/atom shell/Chromium?

nw.js is good if you need a full DOM implementation. On the other hand, because it is a larger dependency it can be more difficult to set up and configure. headless-gl is lighter weight and more modular in the sense that it just implements WebGL and nothing else.

How are <image> and <video> elements implemented?

They aren't for now. If you want to upload data to a texture, you will need to unpack the pixels into a Uint8Array and stick on the GPU yourself.

What extensions are supported?

None at the moment

How is the development environment set up?

  1. Clone this repo
  2. Init the ANGLE git submodule
  3. Run npm install
  4. To do a partial rebuild, cd into the build directory and run make
  5. To do a full rebuild, run npm build
  6. To run all the test cases, run npm test

This should work on most environments, but hasn't been tested thoroughly with windows.

License

See LICENSES

Keywords

FAQs

Package last updated on 04 Sep 2015

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc