Pointer, time and resolution
Get the pointer, the time and the canvas's size into a shader, in the units the shader works in.
Most effects start from the same three uniforms. The catch is units: pointer events give CSS pixels from the top left, while gl_FragCoord is in device pixels from the bottom left. Convert once, in JavaScript, and the shader can compare them directly.
import { createContext, createRenderer } from 'wtc-simplegl'
// The pointer, time and resolution as uniforms, all in device pixels with y
// up, the same as gl_FragCoord. The pointer is eased towards where it is, and
// wanders on its own when it's off the canvas.
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 vec2 u_pointer;
uniform float u_time;
out vec4 colour;
void main() {
// Distance from the pointer, as a fraction of the canvas height
float d = length(gl_FragCoord.xy - u_pointer) / u_resolution.y;
float rings = 0.5 + 0.5 * sin(d * 60.0 - u_time * 4.0);
float alpha = smoothstep(0.4, 0.9, rings) * smoothstep(0.45, 0.0, d);
vec3 rgb = mix(vec3(1.0, 0.45, 0.7), vec3(0.29, 0.62, 1.0), smoothstep(0.0, 0.4, d));
colour = vec4(rgb * alpha, alpha);
}`
export default function (container) {
const ac = new AbortController()
const { signal } = ac
const canvas = document.createElement('canvas')
container.appendChild(canvas)
const gl = createContext({
canvas,
signal,
attributes: { alpha: true, premultipliedAlpha: true, antialias: true }
})
const quad = createRenderer(gl, {
vert,
frag,
count: 3,
attributes: {
a_position: { data: new Float32Array([-1, -1, 3, -1, -1, 3]), size: 2 }
}
})
// Pointer events give CSS pixels from the top; the shader wants device
// pixels from the bottom
let target = null
const toDevice = (e) => {
const rect = canvas.getBoundingClientRect()
const scale = gl.canvas.width / rect.width
return [(e.clientX - rect.left) * scale, (rect.bottom - e.clientY) * scale]
}
canvas.addEventListener('pointermove', (e) => (target = toDevice(e)), { signal })
canvas.addEventListener('pointerleave', () => (target = null), { signal })
const pointer = [0, 0]
return {
frame(time, delta) {
const { width, height } = gl.canvas
// Off the canvas, wander in a figure of eight
const [tx, ty] = target ?? [
width * (0.5 + 0.3 * Math.sin(time * 0.7)),
height * (0.5 + 0.25 * Math.sin(time * 1.4))
]
// Ease towards it, at the same speed at any frame rate
const ease = 1 - Math.pow(0.001, delta)
pointer[0] += (tx - pointer[0]) * ease
pointer[1] += (ty - pointer[1]) * ease
quad.bindVAOs()
quad.setUniforms({ u_resolution: [width, height], u_pointer: pointer, u_time: time })
quad.render()
},
dispose() {
ac.abort()
quad.delete()
gl.getExtension('WEBGL_lose_context')?.loseContext()
canvas.remove()
}
}
}The pointer
const toDevice = (e) => {
const rect = canvas.getBoundingClientRect()
const scale = gl.canvas.width / rect.width
return [(e.clientX - rect.left) * scale, (rect.bottom - e.clientY) * scale]
}scale is the canvas's pixel ratio, worked out from the canvas rather than devicePixelRatio, so it's right whatever dpr the context uses. Measuring from rect.bottom flips y.
In the shader, the pointer and gl_FragCoord.xy are then the same kind of thing:
float d = length(gl_FragCoord.xy - u_pointer) / u_resolution.y;Dividing by the height makes distances a fraction of the canvas, so the effect looks the same at any size.
For coordinates from 0 to 1 instead, divide by the rect's size and skip scale, as the image effect does.
Easing
Following the pointer exactly looks mechanical. Ease towards it instead, by the same fraction per second at any frame rate:
const ease = 1 - Math.pow(0.001, delta)
pointer[0] += (target[0] - pointer[0]) * ease
pointer[1] += (target[1] - pointer[1]) * easeThe smaller the number, the faster it catches up.
Listening
Pointer events, rather than mouse events, cover mouse, pen and touch. Add them with the effect's signal, so they're removed with everything else. On touch screens, touch-action: none on the canvas stops dragging from scrolling the page.
Time and resolution
time from loop (or <Example>) is in seconds. [gl.canvas.width, gl.canvas.height] is the resolution in device pixels, which matches gl_FragCoord. Set both every frame; the size changes when the canvas resizes.