Code
const regl = createREGL({
extensions: ['ANGLE_instanced_arrays'],
optionalExtensions: ['OES_vertex_array_object']
});
if (!regl.hasExtension('OES_vertex_array_object')) {
console.warn('Unable to load OES_vertex_array_object extension. VAOs will be emulated.');
}
const drawLines = reglLines(regl, {
vert: `
precision highp float;
#pragma lines: attribute vec2 xy;
#pragma lines: position = getPosition(xy);
vec4 getPosition(vec2 xy) {
return vec4(xy, 0, 1);
}
#pragma lines: attribute float orientation;
#pragma lines: orientation = getOrientation(orientation)
float getOrientation(float o) { return o; }
#pragma lines: width = getWidth();
uniform float width;
float getWidth() {
return width;
}`,
frag: `
precision lowp float;
void main () {
gl_FragColor = vec4(1);
}`,
uniforms: {
width: (ctx, props) => ctx.pixelRatio * props.width
},
});
const n = 11;
const xy = [...Array(n).keys()]
.map(i => (i / (n - 1) * 2.0 - 1.0) * 0.8)
.map(t => [t, 0.5 * Math.sin(8.0 * t)]);
const vao = drawLines.vao({
vertexAttributes: {
xy: regl.buffer(xy)
},
endpointAttributes: {
orientation: regl.buffer([Array(3).fill(reglLines.CAP_START), Array(3).fill(reglLines.CAP_END)].flat()),
xy: regl.buffer([xy.slice(0, 3), xy.slice(-3).reverse()].flat())
}
});
function draw () {
const lineData = {
width: 30,
join: 'round',
cap: 'round',
vertexCount: xy.length,
endpointCount: 2,
vao
};
regl.poll();
regl.clear({color: [0.2, 0.2, 0.2, 1]});
drawLines(lineData);
}
draw();
window.addEventListener('resize', draw);