Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="page">
  <h1>Unexpected Moiré</h1>
  <p>This moiré interactive background is made with simple WebGL points.</p>
  <p><strong>Move</strong> your mouse ! You can <strong>click</strong> to randomize colors, and <strong>scroll</strong> the page to zoom in this funny effect.</p>

  <h2>About</h2>
  <p>Why <em>unexpected</em> ? Because I didn't want to make this moiré effect, but I think it is funny and strangely beautiful.</p>
  <p>These WebGL points are animated using a water simulation technique taken from this <a href="https://github.com/evanw/webgl-water" target="_blank">great demo</a>
    from <a href="http://madebyevan.com/" target="_blank">Evan Wallace</a>.
  </p>
  <p>Instead using ThreeJS, I thought it was easier to use the tiny <a href="https://github.com/oframe/ogl" target="_blank">oframe/ogl</a> lib.</p>
  
  <p>Take a look at my <a href="https://codepen.io/collection/AGZywR" target="_blank">WebGL</a> collection or <a href="https://codepen.io/soju22" target="_blank">Codepen profile</a> for other experiments.</p>
</div>

<p class="collection">
<a href="https://codepen.io/collection/AGZywR" target="_blank">WebGL Collection</a>
</p>


              
            
!

CSS

              
                body {
  margin: 0;
  font-family: 'Montserrat', sans-serif;
  text-shadow: 1px 1px 1px #fff;;
}

canvas {
  position: fixed;
  top: 0;
  bottom: 0;
  z-index: -1;
}

#page {
  width: 70%;
  max-width: 600px;
  min-height: 4000px;
  margin: 8% auto 0;
  text-align: center;
}

h1, h2, p {
  margin: 2em 0;
}
h1 { font-size: 5em; }
h2 { font-size: 3em; }
p { font-size: 2em; }

a { color: #4040ff; text-decoration: none; }
a:hover { text-decoration: underline; }

@media screen and (max-width: 800px) {
	h1 { font-size: 4em; }
	h2 { font-size: 2em; }
	p { font-size: 1.5em; }
}

              
            
!

JS

              
                function App() {
  const { Renderer, Camera, Geometry, Program, Mesh, Color, Vec2 } = ogl;

  let renderer, gl, camera;
  let width, height, wWidth, wHeight;
  let mouse, mouseOver = false;

  let gridWidth, gridHeight, gridRatio;
  // let gridWWidth, gridWHeight;
  let ripple, points;
  const color1 = new Color([0.149, 0.141, 0.912]);
  const color2 = new Color([1.000, 0.833, 0.224]);
  let cameraZ = 50;

  init();

  function init() {
    renderer = new Renderer({ dpr: 1 });
    gl = renderer.gl;
    document.body.appendChild(gl.canvas);

    camera = new Camera(gl, { fov: 45 });
    camera.position.set(0, 0, cameraZ);

    resize();
    window.addEventListener('resize', resize, false);

    mouse = new Vec2();

    initScene();
    initEventsListener();
    requestAnimationFrame(animate);
  }

  function initScene() {
    gl.clearColor(1, 1, 1, 1);
    ripple = new RippleEffect(renderer);
    // randomizeColors();
    initPointsMesh();
  }

  function initPointsMesh() {
    gridWidth = width;
    gridHeight = height;
    // gridWWidth = gridWidth * wWidth / width;
    // gridWHeight = gridHeight * wHeight / height;

    const ssize = 3; // screen space
    const wsize = ssize * wWidth / width;
    const nx = Math.floor(gridWidth / ssize) + 1;
    const ny = Math.floor(gridHeight / ssize) + 1;
    const numPoints = nx * ny;
    const ox = -wsize * (nx / 2 - 0.5), oy = -wsize * (ny / 2 - 0.5);
    const positions = new Float32Array(numPoints * 3);
    const uvs = new Float32Array(numPoints * 2);
    const sizes = new Float32Array(numPoints);

    let uvx, uvy, uvdx, uvdy;
    gridRatio = gridWidth / gridHeight;
    if (gridRatio >= 1) {
      uvx = 0; uvdx = 1 / nx;
      uvy = (1 - 1 / gridRatio) / 2; uvdy = (1 / ny) / gridRatio;
    } else {
      uvx = (1 - 1 * gridRatio) / 2; uvdx = (1 / nx) * gridRatio;
      uvy = 0; uvdy = 1 / ny;
    }

    for (let i = 0; i < nx; i++) {
      const x = ox + i * wsize;
      for (let j = 0; j < ny; j++) {
        const i1 = i * ny + j, i2 = i1 * 2, i3 = i1 * 3;
        const y = oy + j * wsize;
        positions.set([x, y, 0], i3);
        uvs.set([uvx + i * uvdx, uvy + j * uvdy], i2);
        sizes[i1] = ssize / 2;
      }
    }

    const geometry = new Geometry(gl, {
      position: { size: 3, data: positions },
      uv: { size: 2, data: uvs },
      size: { size: 1, data: sizes }
    });

    if (points) {
      points.geometry = geometry;
    } else {
      const program = new Program(gl, {
        uniforms: {
          hmap: { value: ripple.gpgpu.read.texture },
          color1: { value: color1 },
          color2: { value: color2 }
        },
        vertex: `
          precision highp float;
          const float PI = 3.1415926535897932384626433832795;
          uniform mat4 modelViewMatrix;
          uniform mat4 projectionMatrix;
          uniform sampler2D hmap;
          uniform vec3 color1;
          uniform vec3 color2;
          attribute vec2 uv;
          attribute vec3 position;
          attribute float size;
          varying vec4 vColor;
          void main() {
              vec3 pos = position.xyz;
              vec4 htex = texture2D(hmap, uv);
              pos.z = 10. * htex.r;

              vec3 mixPct = vec3(0.0);
              mixPct.r = smoothstep(0.0, 0.5, htex.r);
              mixPct.g = sin(htex.r * PI);
              mixPct.b = pow(htex.r, 0.5);
              vColor = vec4(mix(color1, color2, mixPct), 1.0);

              gl_PointSize = size;
              gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);
          }
        `,
        fragment: `
          precision highp float;
          varying vec4 vColor;
          void main() {
            gl_FragColor = vColor;
          }
        `
      });
      points = new Mesh(gl, { geometry, program, mode: gl.POINTS });
    }
  }

  function animate(t) {
    requestAnimationFrame(animate);
    camera.position.z += (cameraZ - camera.position.z) * 0.02;

    if (!mouseOver) {
      const time = Date.now() * 0.001;
      const x = Math.cos(time) * 0.2;
      const y = Math.sin(time) * 0.2;
      ripple.addDrop(x, y, 0.05, 0.05);
    }

    ripple.update();
    // ripple.update();
    renderer.render({ scene: points, camera });
  }

  function randomizeColors() {
    color1.set(chroma.random().hex());
    color2.set(chroma.random().hex());
  }

  function initEventsListener() {
    if ('ontouchstart' in window) {
      document.body.addEventListener('touchstart', onMove, false);
      document.body.addEventListener('touchmove', onMove, false);
      document.body.addEventListener('touchend', () => { mouseOver = false; }, false);
    } else {
      document.body.addEventListener('mousemove', onMove, false);
      document.body.addEventListener('mouseleave', () => { mouseOver = false; }, false);
      document.body.addEventListener('mouseup', randomizeColors, false);
      document.addEventListener('scroll', (e) => {
        cameraZ = 50 - getScrollPercentage() * 3;
      });
    }
  }

  function getScrollPercentage() {
    const topPos = document.documentElement.scrollTop;
    const remaining = document.documentElement.scrollHeight - document.documentElement.clientHeight;
    return (topPos / remaining);
  }

  function onMove(e) {
    mouseOver = true;
    if (e.changedTouches && e.changedTouches.length) {
      e.x = e.changedTouches[0].pageX;
      e.y = e.changedTouches[0].pageY;
    }
    if (e.x === undefined) {
      e.x = e.pageX; e.y = e.pageY;
    }
    mouse.set(
      (e.x / gl.renderer.width) * 2 - 1,
      (1.0 - e.y / gl.renderer.height) * 2 - 1
    );

    if (gridRatio >= 1) {
      mouse.y = mouse.y / gridRatio;
    } else {
      mouse.x = mouse.x / gridRatio;
    }

    ripple.addDrop(mouse.x, mouse.y, 0.05, 0.05);
  }

  function resize() {
    width = window.innerWidth; height = window.innerHeight;
    renderer.setSize(width, height);
    camera.perspective({ aspect: width / height });
    const wSize = getWorldSize(camera);
    wWidth = wSize[0]; wHeight = wSize[1];
    if (points) initPointsMesh();
  }

  function getWorldSize(cam) {
    const vFOV = (cam.fov * Math.PI) / 180;
    const height = 2 * Math.tan(vFOV / 2) * Math.abs(cam.position.z);
    const width = height * cam.aspect;
    return [width, height];
  }
}

/**
 * Ripple effect
 */
const RippleEffect = (function () {
  const { Vec2, Program } = ogl;

  function RippleEffect(renderer) {
    this.renderer = renderer;
    this.gl = renderer.gl;
    this.width = 512; this.height = 512;
    this.delta = new Vec2(1 / this.width, 1 / this.height);
    this.gpgpu = new GPGPU(this.renderer.gl, { width: this.width, height: this.height });

    this.initShaders();
  };

  RippleEffect.prototype.initShaders = function () {
    const defaultVertex = `
      attribute vec2 uv;
      attribute vec2 position;
      varying vec2 vUv;
      void main() {
          vUv = uv;
          gl_Position = vec4(position, 0, 1);
      }
    `;

    this.updateProgram = new Program(this.gl, {
      uniforms: {
        tDiffuse: { value: null },
        uDelta: { value: this.delta }
      },
      vertex: defaultVertex,
      fragment: `
        precision highp float;
        uniform sampler2D tDiffuse;
        uniform vec2 uDelta;
        varying vec2 vUv;
        void main() {
          vec4 texel = texture2D(tDiffuse, vUv);

          vec2 dx = vec2(uDelta.x, 0.0);
          vec2 dy = vec2(0.0, uDelta.y);
          float average = (
            texture2D(tDiffuse, vUv - dx).r +
            texture2D(tDiffuse, vUv - dy).r +
            texture2D(tDiffuse, vUv + dx).r +
            texture2D(tDiffuse, vUv + dy).r
          ) * 0.25;

          texel.g += (average - texel.r) * 2.0;
          texel.g *= 0.8;
          texel.r += texel.g;

          gl_FragColor = texel;
        }
      `
    });

    this.dropProgram = new Program(this.gl, {
      uniforms: {
        tDiffuse: { value: null },
        uCenter: { value: new Vec2() },
        uRadius: { value: 0.05 },
        uStrength: { value: 0.05 }
      },
      vertex: defaultVertex,
      fragment: `
        precision highp float;
        const float PI = 3.1415926535897932384626433832795;
        uniform sampler2D tDiffuse;
        uniform vec2 uCenter;
        uniform float uRadius;
        uniform float uStrength;
        varying vec2 vUv;
        void main() {
          vec4 texel = texture2D(tDiffuse, vUv);
          float drop = max(0.0, 1.0 - length(uCenter * 0.5 + 0.5 - vUv) / uRadius);
          drop = 0.5 - cos(drop * PI) * 0.5;
          texel.r += drop * uStrength;
          // texel.r = clamp(texel.r, -2.0, 2.0);
          gl_FragColor = texel;
        }
      `
    });
  };

  RippleEffect.prototype.update = function () {
    this.updateProgram.uniforms.tDiffuse.value = this.gpgpu.read.texture;
    this.gpgpu.renderProgram(this.updateProgram);
  };

  RippleEffect.prototype.addDrop = function (x, y, radius, strength) {
    const us = this.dropProgram.uniforms;
    us.tDiffuse.value = this.gpgpu.read.texture;
    us.uCenter.value.set(x, y);
    us.uRadius.value = radius;
    us.uStrength.value = strength;
    this.gpgpu.renderProgram(this.dropProgram);
  };

  return RippleEffect;
})();

/**
 * GPGPU Helper
 */
const GPGPU = (function () {
  const { RenderTarget, Triangle, Mesh } = ogl;

  function GPGPU(gl, { width, height, type }) {
    this.gl = gl;
    this.width = width; this.height = height;
    this.numVertexes = this.width * this.height;

    const renderTargetOptions = {
      width: this.width,
      height: this.height,
      type: type || gl.HALF_FLOAT || gl.renderer.extensions['OES_texture_half_float'].HALF_FLOAT_OES,
      internalFormat: gl.renderer.isWebgl2 ? (type === gl.FLOAT ? gl.RGBA32F : gl.RGBA16F) : gl.RGBA,
      depth: false,
      unpackAlignment: 1,
    };

    this.read = new RenderTarget(gl, renderTargetOptions);
    this.write = new RenderTarget(gl, renderTargetOptions);
    this.mesh = new Mesh(gl, { geometry: new Triangle(gl) });
  }

  GPGPU.prototype.renderProgram = function(program) {
    this.mesh.program = program;
    this.gl.renderer.render({ scene: this.mesh, target: this.write, clear: false });
    this.swap();
  };

  GPGPU.prototype.swap = function() {
    const temp = this.read;
    this.read = this.write;
    this.write = temp;
  };

  return GPGPU;
})();

App();

              
            
!
999px

Console