Skip to content
Run GLSL shaders in a canvas
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
dist
example
src
.gitignore
.npmignore
LICENSE
README.md
package.json
yarn.lock

README.md

shader-canvas

shader-canvas provides a thin, barebones wrapper around the WebGL API for running basic fragment shaders like those found in The Book of Shaders. It is unopinionated, providing no uniforms or attributes by default.

The entire library is tiny (~2kB compressed), so it can be used to add images and patterns on websites with a significantly smaller download than an image, GIF, or video.

Basic example

import {ShaderCanvas} from "shader-canvas";

var shaderCanvas = new ShaderCanvas();
shaderCanvas.setShader(`
  precision mediump float;

  uniform vec2 u_resolution;

  void main() {
    vec2 uv = gl_FragCoord.xy / u_resolution;
    gl_FragColor = vec4(uv.x, 0.0, uv.y, 1.0);
  }
`);
shaderCanvas.setSize(400, 400);
shaderCanvas.setUniform(
  "u_resolution",
  [400 * window.devicePixelRatio, 400 * window.devicePixelRatio],
);
document.body.appendChild(shaderCanvas.domElement);

See example/index.html for more examples.

Development

Install dependencies:

yarn install

Run the typescript watcher:

yarn run watch

Serve the example page:

yarn run serve

Open the example page in your browser:

open http://localhost:8080/example/

Credits

Extracted from atom-glsl-preview.

You can’t perform that action at this time.