3.2.1 • Published 7 years ago

gl-component v3.2.1

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

gl-component unstable

Class for canvas2d/webgl components. Straightens out setting up canvas, obtaining context, animation loop, resizing, viewport, textures, attributes, multipass rendering, node/browser compatibility. Basically, allows to focus on vis logic as much as possible.

Usage

$ npm install gl-component

const createComponent = require('gl-component');
let c = createComponent({
	//2d or webgl
	context: 'webgl',

	frag: `
		precision mediump float;

		uniform vec4 viewport;
		uniform sampler2D picture;

		void main () {
			vec2 coord = (gl_FragCoord.xy - viewport.xy) / viewport.zw;

			gl_FragColor = texture2D(picture, coord);
		}
	`,

	//vert is a-big-triangle if undefined

	textures: {picture: './texture.gif'}
});

That creates an image in webgl viewport. See in action.

API

Component = require('gl-component')

Component constructor, can be used in functional style let c = Component(opts) or object-oriented style let c = new Component(opts).

component = Component(options|draw)

Create instance based on options or draw method. Options are transfered to the instance.

Available options:

// place canvas into it, by default document.body
container: document.body,

// '2d', 'webgl' or existing context
context: 'webgl',

//context options
canvas: document.createElement('canvas'),
antialias: true,
premultipliedAlpha: false,
alpha: false,

//default canvas size
width: null,
height: null,

//color to clear
background: null,

//autofit on resize
fit: true,

//autolaunch rendering loop, otherwise invoke draw calls manually
autostart: true,

//enable floating point textures (webgl)
float: true,

//viewport box or function returning viewport box, in terms of 2d canvas
viewport: function (w, h) {
	//left, top, width, height
	return [0, 0, w, h - 20];
},

//fragment shader code (webgl)
frag: `
	uniform vec4 viewport; //available through gl-component

	void main () {
		gl_FragColor = vec4(0,0,0,0)
	}
`,

//vertex shader code (webgl)
vert: `
	uniform vec4 viewport;
	attribute vec2 position; //available through gl-component
	void main () {
		gl_Position = vec4(position, 0, 1);
	}
`,

//draw method is called by render once per frame, put canvas2d/webgl draw calls here
draw: function (context, viewport, arg) {
},

//initial data/options for shader
textures: {}, attributes: {}, uniforms: {},

component.render()

Plans draw call on the next animation frame.

component.bind()

Attaches component program, viewport and attributes.

component.draw()

Runs drawing routine. Make sure bind is called before calling draw.

component.clear()

Clear canvas viewport, the opposite of draw.

component.update()

Updates background and viewport.

component.texture(name?, options)

Set texture data/options. See gl-util/texture

component.uniform(name, options)

Set uniform. See gl-util/uniform

component.attribute(name, options)

Set attribute data/options. See gl-util/attribute

component.start()

Start animation loop — will be calling draw each animation frame.

component.stop()

Stop animation loop.

component.on('draw', (gl, viewport, data) => {})

Fired by render before draw call.

component.on('render', () => {})

Fired first in every render call, in case of autostart - once per frame.

component.on('resize', () => {})

Fired with every resize call (after being resized).

component.*

Instance contains all context attributes obtained with gl.getContextAttributes.

What gl-component is not

  • It is not a webgl wrapper, it does not supersede webgl API. The main purpose is making launch and init of webgl easy in DOM, allowing to focus on vis. It does not try to serve as a main entry point nor replace native API, this.gl always provides direct access to drawing context, so you can and are expected to manipulate it.
  • It is not designed for custom rendering, like multipass rendering, texture swapping, stencil/depth/multitexture control, computational shaders etc. The main purpose is a simple way to put webgl rendering process to canvas. Although, customization can be easily done with hooks/redefining methods, but everything you would have to do manually, here is to say creating framebuffers, renderbuffers etc.
  • It does not manage multiple programs, gl-component is bound to a single program. Best practice for managing multiple programs is creating a separate gl-component with shared context.

Built with gl-component

See also

Credits

For API insight to gl-vis, in particular gl-plot3d; to gl-now, canvas-loop, regl. For routines to get-canvas-context, canvas-fit, raf-loop.

Contribute

Try building your own component based on gl-component, for inspiration see code of released components.

site of the moment

3.2.1

7 years ago

3.2.0

7 years ago

3.1.0

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.4.1

7 years ago

2.4.0

7 years ago

2.3.9

7 years ago

2.3.8

7 years ago

2.3.7

7 years ago

2.3.6

7 years ago

2.3.5

8 years ago

2.3.4

8 years ago

2.3.3

8 years ago

2.3.2

8 years ago

2.3.1

8 years ago

2.3.0

8 years ago

2.2.0

8 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.0

8 years ago

1.7.0

8 years ago

1.6.0

8 years ago

1.5.3

8 years ago

1.5.2

8 years ago

1.5.1

8 years ago

1.5.0

8 years ago

1.4.20

8 years ago

1.4.19

8 years ago

1.4.18

8 years ago

1.4.17

8 years ago

1.4.16

8 years ago

1.4.15

8 years ago

1.4.14

8 years ago

1.4.13

8 years ago

1.4.12

8 years ago

1.4.11

8 years ago

1.4.10

8 years ago

1.4.9

8 years ago

1.4.8

8 years ago

1.4.7

8 years ago

1.4.6

8 years ago

1.4.5

8 years ago

1.4.4

8 years ago

1.4.3

8 years ago

1.4.2

8 years ago

1.4.1

8 years ago

1.4.0

8 years ago

1.3.1

8 years ago

1.3.0

8 years ago

1.2.6

8 years ago

1.2.5

8 years ago

1.2.4

8 years ago

1.2.3

8 years ago

1.2.2

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.7

8 years ago

1.1.6

8 years ago

1.1.5

8 years ago

1.1.4

8 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.11

8 years ago

1.0.10

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago