npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gl-renderer

v0.14.0

Published

Drawing patterns with glsl shaders on modern browsers.

Downloads

1,297

Readme

gl-renderer

A lightweight webgl renderer.

The underlying Library of glsl-doodle.

Usage

In browser:

<script src="https://unpkg.com/gl-renderer/dist/gl-renderer.js"></script>

With NPM:

npm install gl-renderer

Quick Start

index.frag

#ifdef GL_ES
precision mediump float;
#endif

uniform vec3 color;

void main() {
  gl_FragColor.rgb = color;
  gl_FragColor.a = 1.0;
}

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Demo</title>
  <script src="https://unpkg.com/gl-renderer/dist/gl-renderer.js"></script>
</head>
<body>
  <canvas id="gl-canvas" width="512" height="512"></canvas>
  <script>
  (async function () {
    const glCanvas = document.getElementById('gl-canvas');
    const renderer = new GlRenderer(glCanvas);

    // load fragment shader and createProgram
    const program = await renderer.load('./index.frag');
    renderer.useProgram(program);

    // set color to RED
    renderer.uniforms.color = [1, 0, 0];

    renderer.render();
  }());
  </script>
</body>
</html>

You will see a canvas 512 pixels wide and 512 pixels high in red.

API Reference

constructor(canvas, options = {})

Create renderer options with specified canvas element and options.

The options:

  • autoUpdate: Force renderer to update when uniforms or meshdata changes. Default value is true.
  • vertexPosition: Attribute name of position in vertex shader. Default value is 'a_vertexPosition'.
  • vertexTextureCoord: Attribute name of texture coordinate in vertext shader. Default value is 'a_vertexTextureCoord'.
  • Other webgl context options: See MDN.
  • webgl2: Use webgl2 context. Default value is false.

renderer.createProgram(fragment[, vertex])

Create a program with specified fragment shader and vertex shader.

const fragmentShader = `
#ifdef GL_ES
precision mediump float;
#endif

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

const program = renderer.createProgram(fragmentShader);
render.useProgram(program);

If you don't specified vertex shader, a default vertex shader will be loaded.

attribute vec4 a_vertexPosition;

void main() {
  gl_PointSize = 1.0;
  gl_Position = a_vertexPosition;
}

or

attribute vec4 a_vertexPosition;
attribute vec2 a_vertexTextureCoord;
varying vec2 vTextureCoord;

void main() {
  gl_PointSize = 1.0;
  gl_Position = a_vertexPosition;
  vTextureCoord = a_vertexTextureCoord;
}

It depends whether you use texture samplers in your fragment shader or not.

async renderer.compile(fragment[, vertex])

gl-render allows you to use #pragma include statment to load and include other shaders.

base.glsl

highp float random(vec2 co) {
    highp float a = 12.9898;
    highp float b = 78.233;
    highp float c = 43758.5453;
    highp float dt= dot(co.xy ,vec2(a,b));
    highp float sn= mod(dt,3.14);
    return fract(sin(sn) * c);
}
const fragmentShader = `
#ifdef GL_ES
precision mediump float;
#endif

#pragma include "./base.glsl"

void main() {
  gl_FragColor = random(gl_FragCoord.xy) * vec4(1, 0, 0, 1);
}
`;

const program = await render.compile(fragmentShader);
renderer.useProgram(program);

async renderer.load(fragmentURL[, vertexURL])

Load fragment shader and vertex shader from url.

const program = await renderer.load('./index.glsl');

renderer.useProgram(program);

renderer.useProgram(program[, attributeDescriptor])

Sets the specified Program as part of the current rendering state.

renderer.createTexture(image)

Wrap a image object (or canvas) to a webgl texture.

function loadImage(src) {
  const img = new Image();
  img.crossOrigin = 'anonymous';
  return new Promise((resolve) => {
    img.onload = function () {
      resolve(img);
    };
    img.src = src;
  });
}

(async function () {
  const glCanvas = document.getElementById('gl-canvas');
  const renderer = new GlRenderer(glCanvas);

  const image = await loadImage('http://path.to.image/image.png');
  const texture = renderer.createTexture(image);

  const program = await renderer.load('./index.frag');
  renderer.useProgram(program);
  // bind texture to samplerMyTex
  renderer.uniforms.samplerMyTex = texture;

  renderer.render();
}());

renderer.loadTexture(src)

Load image from source url and create a texture.

(async function () {
  const glCanvas = document.getElementById('gl-canvas');
  const renderer = new GlRenderer(glCanvas);

  const texture = await renderer.loadTexture('http://path.to.image/image.png');

  const program = await renderer.load('./index.frag');
  renderer.useProgram(program);
  // bind texture to samplerMyTex
  renderer.uniforms.samplerMyTex = texture;

  renderer.render();
}());

renderer.setMeshData(meshData)

Set a list of meshData.

const meshData = [mesh0, mesh1, mesh2...];

A mesh object contains the following properties:

  • positions : Required. The vertex positions of the geometry.
  • cells: Required. The indices of the vertexes.
  • textureCoord: The texture coordinates.
  • attributes: The attributes passed into the shaders.
  • uniforms: The changed uniform values.
  • instanceCount: Set instanceCount for webgl2 context to draw instanced arrays.

index.vert

attribute vec3 a_vertexPosition;
attribute vec3 a_color;

varying vec3 vColor;

void main() {
  gl_PointSize = 1.0;
  gl_Position.xyz = a_vertexPosition;
  gl_Position.w = 1.0;
  vColor = a_color;
}

index.frag

#ifdef GL_ES
precision mediump float;
#endif

varying vec3 vColor;

void main() {
  gl_FragColor = vec4(vColor, 1.0);
}
(async function () {
  const glCanvas = document.getElementById('gl-canvas');
  const renderer = new GlRenderer(glCanvas);

  const program = await renderer.load('./index.frag', './index.vert');
  renderer.useProgram(program, {
    a_color: {
      type: 'UNSIGNED_BYTE',
      normalize: true,
    },
  });

  const vertexColors = [
    [255, 0, 0],
    [255, 0, 0],
    [255, 255, 0],
  ];

  renderer.setMeshData([
    {
      positions: [[-1.0, -1.0, 0.0], [-1.0, 1.0, 0.0], [1.0, 1.0, 0.0]],
      cells: [[0, 1, 2]],
      attributes: {
        a_color: vertexColors,
      },
    },
    {
      positions: [[0.5, 0.5, 0], [-0.5, 0.8, 0], [1, -1, 0]],
      cells: [[0, 1, 2]],
      attributes: {
        a_color: vertexColors,
      },
    },
  ]);

  renderer.render();
}());

renderer.render(clearBuffer = true)

Clear and re-draw canvas. If clearBuffer set to true(default is true), renderer will automately clear color buffer before render.

renderer.uniforms

The uniform declarations in the fragment shader will be automatically bind to renderer.uniforms.

index.frag

#ifdef GL_ES
precision mediump float;
#endif

uniform float u_time;

void main() {
  gl_FragColor = 0.5 * (1.0 + sin(0.00314 * u_time)) * vec4(1, 0, 0, 1);
}
(async function () {
  const glCanvas = document.getElementById('gl-canvas');
  const renderer = new GlRenderer(glCanvas);

  const program = await renderer.load('./index.frag');
  renderer.useProgram(program);

  const startTime = Date.now();

  renderer.uniforms.u_time = 0;

  requestAnimationFrame(function update() {
    renderer.uniforms.u_time = Date.now() - startTime;
    requestAnimationFrame(update);
  });

  renderer.render();
}());

renderer.update()

Cause canvas to clear and re-draw in next frame. Change uniforms or set meshData will force renderer update automatically.