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

webglazy

v3.1.3

Published

Responsive + configurable WebGL canvas replacement

Downloads

175

Readme

WebGLazy

Responsive + configurable WebGL canvas replacement

example

How to Use

Browser

  1. Download ./dist/WebGLazy.min.js and add it to your project's files
  2. Add <script src='WebGLazy.min.js'></script> to your page's <head>

Node

  1. npm install webglazy --save
  2. import WebGLazy from 'webglazy';

Use

Add new WebGLazy(); somewhere in your code after your game's canvas has been initialized. If you're not sure where/when that happens, alternatively add a new tag that calls the same code after a short delay (e.g. <script>setTimeout(function(){new WebGLazy();}, 1000);</script>).

Configuration

WebGLazy behaviour can be configured by passing an options object into the constructor; e.g.:

new WebGLazy({
    background: 'white',
    scaleMode: WebGLazy.SCALE_MODES.COVER,
    source: document.getElementById('myGameCanvas')
});
  • source : Element to treat as a source for output; default: see sources
  • sources : If source isn't provided, finds the first tag from this list of possible tags; default: ['canvas', 'video', 'img']
  • hideSource : If true, extra CSS is used to hide everything except the output canvas; default: true
  • background : Background CSS applied to HTML, BODY, and canvasContainer element; default: 'black'
  • scaleMultiplier : Multiplier applied to size of source canvas; default: 1
  • scaleMode : Defines the scaling behaviour of the output canvas; see Scale Modes for possible settings; default: WebGLazy.SCALE_MODES.FIT
  • allowDownscaling : Allow scaling the output canvas smaller than the original size * scaleMultiplier (only applies when scaleMode is FIT or COVER); default: false
  • autoInit : Call this.init in constructor; default: true
  • timestep : Target duration between frames (in milliseconds); default: 1 / 60 * 1000, i.e. 60fps
  • pixelate : If true, uses GL_NEAREST and image-rendering: pixelated; default: true
  • disableFeedbackTexture : Disables a second texture, which contains a copy of the WebGL output; default: false
  • disableMouseEvents : if true, MouseEvents triggered on the output canvas will not be dispatched on the source element; default: false
  • vertex : Vertex shader source; default: a functional pass-through
  • fragment : Frament shader source; default: a functional pass-through

Scale Modes

Scale modes define how the output canvas is scaled in relation to the screen size. Available scale modes are:

  • WebGLazy.SCALE_MODES.FIT : scale output canvas to fit screen (i.e. largest possible size with all content visible)
  • WebGLazy.SCALE_MODES.COVER : scale output canvas to cover screen (i.e. smallest possible size with no background visible)
  • WebGLazy.SCALE_MODES.MULTIPLES : scale up in multiples of original size (e.g. if the source's original size was 256x256 and the screen size is 1920x1080, the output canvas will be 1024x1024). This mode is particularly useful for upscaling pixel-art without artifacts.
  • WebGLazy.SCALE_MODES.NONE : output canvas size doesn't scale with screen

handy-dandy scaleMode diagram

Post-processing

Since WebGLazy renders to a WebGL canvas, simple post-processing shader support is made trivially easy! Use the vertex and fragment options to override the default shaders.

Available uniforms:

uniform sampler2D tex0;  // source
uniform sampler2D tex1;  // output from last frame
uniform vec2 resolution; // size of output (uv coordinates = gl_FragCoord.xy / resolution)
uniform float time;      // milliseconds since initialization (equal to performance.now())

Example vertex shader:

// pass-through vertex shader
attribute vec4 position;
void main(){
	gl_Position = position;
}

Example fragment shader:

// uv-wave fragment shader
precision mediump float;
uniform sampler2D tex0;
uniform sampler2D tex1;
uniform float time;
uniform vec2 resolution;

void main(){
	vec2 coord = gl_FragCoord.xy;
	vec2 uv = coord.xy / resolution.xy;
	uv.x += sin(uv.y * 10.0 + time / 200.0) / 60.0;
	uv.y += cos(uv.x * 10.0 + time / 200.0) / 60.0;
	vec3 col = texture2D(tex0,uv).rgb;
	gl_FragColor = vec4(col, 1.0);
}

Limitations

  • Behaviour with multiple instances of WebGLazy on a single page is undefined
  • If WebGL is not supported, WebGLazy will fallback to 2D canvas rendering, which does not support shaders