Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

              
                <canvas id="canvas"></canvas>
<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>
<script id="fs" type="f">
  precision mediump float;
  
  uniform vec2 u_mouse;
  uniform vec2 u_res;
  uniform float u_time;
  uniform float u_dpi;
  uniform sampler2D u_text;
  uniform sampler2D u_map;
  uniform sampler2D u_blur;

  varying vec2 v_texcoord;
   
  void main(){
    float distAmount = .003;
      
    vec2 uv = v_texcoord; 
    vec2 parallax = u_mouse * 0.05;
    
    float freq = 70.0;
    float amp = 0.004;
    
    vec4 map = texture2D(u_map, uv);

    float dethMap = map.r - .5; // depth map
    float distMap = map.g; // displacement map

    float x = uv.y * freq + u_time * 3.; 
    float y = uv.x * freq + u_time * .3;
    
    float distX = cos(x+y) * amp * cos(y);
    float distY = sin(x-y) * amp * cos(y);

    
    vec2 distPosition = vec2(distX, distY) * distMap;
       
    vec2 turbulance = uv + distPosition + (parallax * dethMap);
    
    vec4 ori_img = texture2D(u_text, turbulance);
    vec4 blur_img = texture2D(u_blur, turbulance);
        
    vec4 color = mix(ori_img, blur_img, length(distPosition) * distAmount);
    
    gl_FragColor = color;
  }  
</script>
              
            
!

CSS

              
                body { margin: 0; }
canvas { width: 100vw; height: 100vh; display: block; }
              
            
!

JS

              
                "use strict";

class MathUtils {
  constructor() {}

  lerp(a, b, n) {
    return n * (b - a) + a;
  }
}

const main = () => {
  const canvas = document.getElementById("canvas");
  const gl = canvas.getContext("webgl");
  const mathUtils = new MathUtils();
  const mouse = { x: 0, y: 0 };
  const lastmouse = [0, 0];
  if (!gl) {
    return;
  }

  twgl.setDefaults({
    textureColor: [0, 0, 0, 0]
  });
  // replaced after loading
  let srcs = {
    text: { width: 1, height: 1 }
  };
  const textures = twgl.createTextures(
    gl,
    {
      text: { src: "https://i.ibb.co/Gf6b39g/fancycrave-165873-unsplash-1.jpg" },
      map: { src: "https://i.ibb.co/N7R9v8p/map3.jpg" },
      blur: { src: "https://i.ibb.co/cy79kN4/blur.jpg" }
    },
    (err, textures, sources) => {
      srcs = sources;
    }
  );

  // 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);

  let uniforms = {
    u_text: textures.text,
    u_map: textures.map,
    u_blur: textures.blur,
    u_res: [gl.canvas.clientWidth, gl.canvas.clientHeight]
  };

  const render = time => {
    time *= 0.001; // seconds

    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 imageAspect = srcs.text.width / srcs.text.height;
    let mat = m3.scaling(imageAspect / canvasAspect, -1);

    // this assumes we want to fill vertically
    let horizontalDrawAspect = imageAspect / canvasAspect;
    let verticalDrawAspect = -1;
    // does it fill horizontally?
    if (horizontalDrawAspect < 1) {
      // no it does not so scale so we fill horizontally and
      // adjust vertical to match
      verticalDrawAspect /= horizontalDrawAspect;
      horizontalDrawAspect = 1;
    }
    mat = m3.scaling(horizontalDrawAspect, verticalDrawAspect);

    uniforms.u_matrix = mat;
    uniforms.u_time = time * 10;
    uniforms.u_mouse = lastmouse;

    // calls gl.activeTexture, gl.bindTexture, gl.uniformXXX
    twgl.setUniforms(programInfo, uniforms);

    // calls gl.drawArrays or gl.drawElements
    twgl.drawBufferInfo(gl, bufferInfo);

    lastmouse[0] = mathUtils.lerp(lastmouse[0], mouse.x, 0.1);
    lastmouse[1] = mathUtils.lerp(lastmouse[1], mouse.y, 0.1);

    requestAnimationFrame(render);
  };
  requestAnimationFrame(render);

  canvas.addEventListener("mousemove", ({ clientX, clientY }) => {
    mouse.x = -clientX / innerWidth;
    mouse.y = -clientY / innerHeight;
  });
};

main();
              
            
!
999px

Console