Skip to content

Getting started

Your first shader ​

Fill a canvas with a fragment shader that animates over time.

Most effects start the same way: a context for the canvas, a renderer with your shaders and a triangle that covers the screen, and a uniform or two set every frame.

js
import { createContext, createRenderer } from 'wtc-simplegl'

// A full-screen triangle coloured by a fragment shader. The canvas is
// see-through, so the shader writes premultiplied colour.
const vert = /* glsl */ `#version 300 es
in vec2 a_position;
void main() {
  gl_Position = vec4(a_position, 0.0, 1.0);
}`

const frag = /* glsl */ `#version 300 es
precision highp float;
uniform vec2 u_resolution;
uniform float u_time;
out vec4 colour;
void main() {
  // Centred, with y from -1 to 1
  vec2 p = (gl_FragCoord.xy - 0.5 * u_resolution) / u_resolution.y * 2.0;
  float rings = 0.5 + 0.5 * sin(length(p) * 12.0 - u_time * 2.0);
  float alpha = smoothstep(0.2, 0.8, rings) * smoothstep(1.4, 0.6, length(p));
  vec3 rgb = mix(vec3(0.29, 0.62, 1.0), vec3(1.0, 0.45, 0.7), 0.5 + 0.5 * p.x);
  colour = vec4(rgb * alpha, alpha);
}`

export default function (container) {
  const ac = new AbortController()
  const canvas = document.createElement('canvas')
  container.appendChild(canvas)
  const gl = createContext({
    canvas,
    signal: ac.signal,
    attributes: { alpha: true, premultipliedAlpha: true, antialias: true }
  })

  const quad = createRenderer(gl, {
    vert,
    frag,
    attributes: {
      // One triangle that covers the canvas
      a_position: { data: new Float32Array([-1, -1, 3, -1, -1, 3]), size: 2 }
    },
    count: 3
  })

  return {
    frame(time) {
      quad.bindVAOs()
      quad.setUniforms({
        u_resolution: [gl.canvas.width, gl.canvas.height],
        u_time: time
      })
      quad.render()
    },
    dispose() {
      ac.abort()
      quad.delete()
      gl.getExtension('WEBGL_lose_context')?.loseContext()
      canvas.remove()
    }
  }
}

The context ​

createContext makes a WebGL2 context for a canvas and keeps the canvas's drawing buffer the same size as it is on the page, in device pixels. Size the canvas with CSS, and gl.canvas.width and gl.canvas.height follow:

js
const gl = createContext({ canvas: document.querySelector('canvas') })

With no canvas, it makes one that fills the window. Context and canvas has the options.

The renderer ​

createRenderer compiles the two shaders, links them into a program and sets up the attributes. Here there's one attribute, a_position, with three 2D points: a triangle twice the size of the screen, so it covers it with nothing wasted on a second triangle.

js
const quad = createRenderer(gl, {
  vert,
  frag,
  attributes: {
    a_position: { data: new Float32Array([-1, -1, 3, -1, -1, 3]), size: 2 }
  },
  count: 3
})

Each attribute's name matches an in in the vertex shader. size is the number of components per vertex, and count is the number of vertices to draw.

Drawing a frame ​

Three calls draw it:

js
quad.bindVAOs() // use the program and its vertex array
quad.setUniforms({ u_resolution: [gl.canvas.width, gl.canvas.height], u_time: time })
quad.render() // set the viewport to the canvas and draw

setUniforms looks each uniform up by name and calls the right gl.uniform* for its type, so a vec2 takes an array of two numbers and a float takes a number. Names the shader doesn't use are skipped.

Outside the docs, call this every frame with loop:

js
import { loop } from 'wtc-simplegl'

loop((time) => {
  quad.bindVAOs()
  quad.setUniforms({ u_resolution: [gl.canvas.width, gl.canvas.height], u_time: time })
  quad.render()
})

The fragment shader ​

The shader works out where each pixel is from gl_FragCoord, in device pixels, and u_resolution. Dividing by the height keeps circles round on a wide canvas:

glsl
vec2 p = (gl_FragCoord.xy - 0.5 * u_resolution) / u_resolution.y * 2.0;

This canvas is see-through, so the shader writes premultiplied colour: the RGB multiplied by the alpha. On an opaque canvas, write any colour with an alpha of 1.

Where to go from here ​