<canvas id="canvas"></canvas>
<!-- vertex shader -->
<script id="vs" type="f">
attribute vec2 position;
attribute vec2 texcoord;
uniform mat3 u_matrix;
varying vec2 v_texcoord;
void main() {
gl_Position = vec4(u_matrix * vec3(position, 1), 1);
v_texcoord = texcoord;
}
</script>
<!-- fragment shader -->
<script id="fs" type="f">
precision mediump float;
uniform vec2 u_mouse;
uniform vec2 u_res;
varying vec2 v_texcoord;
void main() {
vec2 position = v_texcoord;
vec3 lastcolor = vec3(0.0, 0.0, 0.0);
float color = 0.009 / distance(position, u_mouse);
vec3 o = vec3(0);
o += lastcolor * 0.5;
o += vec3(color) * vec3(1.0, 0.2, 1.0);
o += (length(position - u_mouse) * 0.01) / (length((position - (vec2(.5) - (u_mouse - vec2(0.25)))))) * vec3(1.0, 0.9, 1.0);
o += (u_mouse.x * 0.002) / (length((position - u_mouse) * vec2(1.62, 13.0))) * vec3(02.2, 5.4, u_mouse.y * 30.8);
gl_FragColor = vec4( o, 1.0 );
}
</script>
body {
margin: 0;
}
canvas {
width: 100%;
height: 100%;
display: block;
position: absolute;
top: 0;
left: 0;
z-index: 1;
}
"use strict";
function main() {
// Get A WebGL context
/** @type {HTMLCanvasElement} */
const canvas = document.getElementById("canvas");
const gl = canvas.getContext("webgl");
if (!gl) {
return;
}
// compile shaders, link program, lookup location
const programInfo = twgl.createProgramInfo(gl, ["vs", "fs"]);
// calls gl.createBuffer, gl.bindBuffer, gl.bufferData for a quad
const bufferInfo = twgl.primitives.createXYQuadBufferInfo(gl);
const mouse = [0, 0];
canvas.addEventListener('mousemove', (event) => {
mouse[0] = (event.clientX / gl.canvas.clientWidth);
mouse[1] = (event.clientY / gl.canvas.clientHeight);
});
canvas.addEventListener('mouseout', (event) => {
mouse[0] = 0.5;
mouse[1] = 0.5;
});
canvas.addEventListener('touchmove', (event) => {
mouse[0] = (event.touches[0].clientX / gl.canvas.clientWidth );
mouse[1] = (event.touches[0].clientY / gl.canvas.clientHeight);
});
canvas.addEventListener('touchend', (event) => {
mouse[0] = 0.5;
mouse[1] = 0.5;
});
var nMouse = [0, 0];
var oMouse = [0, 0];
requestAnimationFrame(render);
function render() {
twgl.resizeCanvasToDisplaySize(gl.canvas);
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.useProgram(programInfo.program);
// calls gl.bindBuffer, gl.enableVertexAttribArray, gl.vertexAttribPointer
twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);
const canvasAspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
const mat = m3.scaling(canvasAspect / canvasAspect, -1);
nMouse[0] += (mouse[0] - nMouse[0]) * 0.05;
nMouse[1] += (mouse[1] - nMouse[1]) * 0.05;
// calls gl.activeTexture, gl.bindTexture, gl.uniformXXX
twgl.setUniforms(programInfo, {
u_matrix: mat,
u_mouse: nMouse,
u_res: [gl.canvas.clientWidth, gl.canvas.clientHeight],
});
// calls gl.drawArrays or gl.drawElements
twgl.drawBufferInfo(gl, bufferInfo);
requestAnimationFrame(render);
}
}
main();
This Pen doesn't use any external CSS resources.