<!DOCTYPE html>
<html lang="en-us">
<head>
  <meta charset="utf-8">
  <meta name="robots" content="noindex">
  <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
  <title>Trails</title>
  <style>
    html,
    body,
    canvas {
      margin: 0;
      padding: 0;
      width: 100%;
      height: 100%;
      overflow: hidden;
    }
  </style>
</head>
<body>
  <canvas id="canvas"></canvas>
  
  <script id="vertexShader" type="x-shader/x-vertex">
    #version 300 es
    in vec4 vertexPosition;
    void main() { // no-op vertex shader
      gl_Position = vertexPosition;
    }
  </script>
  
  <!--
  
  4 key things are needed to create the trails effect.
  (1) is in the fragment shader. (2), (3), and (4) are in the JavaScript.
  
  -->
  <script id="fragmentShader" type="x-shader/x-fragment">
    #version 300 es
    precision highp float;
    uniform vec2 canvasSize;
    uniform float time;
    out vec4 fragColor;
  
    void main() {
      vec2 coord = (2.*gl_FragCoord.xy - canvasSize)/min(canvasSize.x, canvasSize.y); // [-1,1] in smaller dimension
      float angle = time;
      float radius = 0.8*sin(3.*time);
  
      vec2 center = vec2(radius*cos(angle), radius*sin(angle));
      if (length(coord - center) < 0.1) {
        fragColor = vec4(0, sin(time/1.7)/4.+.3, .5, 1);
      }
      else {
         fragColor = vec4(1, 1, 1, 0.005); // (1) Draw a semi-transparent background
      }
    }
  </script>
  
  <script id="webgl" type="text/javascript">
    const canvas = document.querySelector('canvas');
    const vertexShader = document.querySelector('script[type="x-shader/x-vertex"]');
    const fragmentShader = document.querySelector('script[type="x-shader/x-fragment"]');
  
    const gl = canvas.getContext("webgl2", {
      preserveDrawingBuffer: true, // (2) Don't clear the buffer after every frame
    });
    if (!gl) {
      document.body.innerHTML ='<p>Error: WebGL2 is <a href="https://get.webgl.org/webgl2/">not supported by your browser</a></p>';
      throw "WebGL2 not supported";
    }
  
    function createShader(shaderType, sourceCode) {
      const shader = gl.createShader(shaderType);
      gl.shaderSource(shader, sourceCode);
      gl.compileShader(shader);
      if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) throw gl.getShaderInfoLog(shader);
      return shader;
    }
  
    const program = gl.createProgram();
    gl.attachShader(program, createShader(gl.VERTEX_SHADER, vertexShader.textContent.trim()));
    gl.attachShader(program, createShader(gl.FRAGMENT_SHADER, fragmentShader.textContent.trim()));
    gl.linkProgram(program);
    if (!gl.getProgramParameter(program, gl.LINK_STATUS)) throw gl.getProgramInfoLog(program);
    gl.useProgram(program);
  
    const vertices = [[-1, -1], [1, -1], [-1, 1], [1, 1]];
    gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices.flat()), gl.STATIC_DRAW);
  
    const vertexPosition = gl.getAttribLocation(program, "vertexPosition");
    gl.enableVertexAttribArray(vertexPosition);
    gl.vertexAttribPointer(vertexPosition, 2, gl.FLOAT, false, 0, 0);
  
    const canvasSizeUniform = gl.getUniformLocation(program, 'canvasSize');
    const timeUniform = gl.getUniformLocation(program, 'time');
  
    gl.enable(gl.BLEND); // (3) Enable blend mode
    gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); // (4) Apply the needed blend function
  
    function draw() {
      const width = canvas.clientWidth;
      const height = canvas.clientHeight;
      canvas.width = width;
      canvas.height = height;
      gl.viewport(0, 0, width, height);
      gl.uniform2f(canvasSizeUniform, width, height);
  
      gl.uniform1f(timeUniform, performance.now() / 1000.);
      gl.drawArrays(gl.TRIANGLE_STRIP, 0, vertices.length);
      requestAnimationFrame(draw);
    }
    draw();
  </script>
  
  <style>
    /* This is designed to work with a white background */
    canvas {
      background-color: white;
    }
  </style>

  
</body>
<!--
  © Adam Murray 2022
  https://adammurray.blog/

  Creative Commons License
  Attribution-NonCommercial-ShareAlike 4.0 International
  https://creativecommons.org/licenses/by-nc-sa/4.0/
-->