123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!-----------------------------------
fragment shader adapted/inspired by
https://www.shadertoy.com/view/ldjGzV
------------------------------------->
            
          
!
            
              const r = createREGL();

navigator.getUserMedia = (
  navigator.getUserMedia ||
  navigator.webkitGetUserMedia ||
  navigator.mozGetUserMedia
);

const init = () => {
  navigator.mediaDevices.getUserMedia({ video: true })
    .then(start)
    .catch(e => console.log(e.name + ": "+ e.message));
}

const initLegacy = () => {
  navigator.getUserMedia({video: true}, start, (e) => e);
}

if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  init();
} else if (navigator.getUserMedia) {
  initLegacy();
}

function start(stream) {
  const cam = document.createElement('video');
  cam.width = 320;
  cam.height = 240;
  cam.autoplay = true;
  cam.srcObject = stream;
  applyCamStyles(cam);
  document.body.appendChild(cam);
  const camTexture = r.texture();

  const noise = new Image();
  noise.crossOrigin = 'Anonymous';
  noise.src = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/476907/noise1.png';
  const noiseTexture = r.texture();
  console.log(noiseTexture);
  noise.onload = function() {
    noiseTexture({
      data: noise,
      wrap: 'repeat',
      height: 256,
      width: 256
    });
  };

  const draw = r({
    frag: `
      precision mediump float;
      uniform float t;
      uniform vec2 resolution;
      uniform float aspect;
      uniform sampler2D cam;
      uniform sampler2D noise;

      float ramp(float y, float start, float end) {
        float inside = step(start, y) - step(end, y);
        float fact = (y - start) / (end - start) * inside;
        return (1.0 - fact) * inside;
      }

      float onOff(float a, float b, float c) {
        return step(c, sin(t + a * cos(t * b)));
      }

      vec2 coverScreen(vec2 fragCoord, vec2 resolution, float aspect) {
        vec2 uv = 0.5 * (2.0 * fragCoord - resolution);
        if (resolution.x / resolution.y > aspect) {
          uv = 0.5 - uv / vec2(resolution.x, resolution.x / aspect);
        } else {
          uv = 0.5 - uv / vec2(resolution.y * aspect, resolution.y);
        }
        return uv;
      }

      vec2 screenDistort(vec2 uv) {
        uv -= vec2(0.5, 0.5);
        uv = uv * 1.2 * (1.0 / 1.2 + 2.0 * uv.x * uv.x * uv.y * uv.y);
        uv += vec2(0.5, 0.5);
        return uv;
      }

      vec2 scanWarp(vec2 uv, float t) {
        float window = 1.0 / (1.0 + 20.0 * (uv.y - mod(t / 4.0, 1.0)) * (uv.y - mod(t / 4.0, 1.0)));
        uv.x = uv.x + sin(uv.y * 10.0 + t) / 50.0 * onOff(4.0, 4.0, 0.3) * (1.0 + cos(t * 80.0)) * window;
        float vShift = 0.4 * onOff(2.0, 3.0, 0.9) * (sin(t) * sin(t * 20.0) + (0.5 + 0.1 * sin(t * 200.0) * cos(t)));
        uv.y = mod(uv.y + vShift, 1.0);
        return uv;
      }

      float vignette(vec2 uv) {
        float vigAmt = 3.0 + 0.3 * sin(t + 5.0 * cos(t * 5.0));
        return (1.0 - vigAmt * (uv.y - 0.5) * (uv.y - 0.5)) * (1.0 - vigAmt * (uv.x - 0.5) * (uv.x - 0.5));
      }

      float crtLines(vec2 uv) {
        return ((12.0 + mod(uv.y * 30.0 + t, 1.0)) / 13.0);
      }

      float getNoise(vec2 p) {
        float s = texture2D(noise, vec2(1.0, 2.0 * cos(t)) * t * 8.0 + p * 1.0).x;
        s *= s;
        return s;
      }

      float getStripes(vec2 uv) {
        float noi = getNoise(uv * vec2(0.5, 1.0) + vec2(1.0, 3.0));
        return ramp(mod(uv.y * 4.0 + t/2.0 + sin(t + sin(t * 0.63)), 1.0), 0.5, 0.6) * noi;
      }

      void main() {
        vec2 uv = coverScreen(gl_FragCoord.xy, resolution, aspect);
        uv = screenDistort(uv);
        uv = scanWarp(uv, t);
        vec4 vid_out = texture2D(cam, uv);

        vid_out.rgb += getStripes(uv);
        vid_out.rgb += getNoise(uv * 3.0) / 3.0;
        vid_out.rgb *= vignette(uv);
        vid_out.rgb *= crtLines(uv);

        gl_FragColor = vid_out;
      }
      `,
    vert: `
      precision mediump float;
      attribute vec2 position;

      void main () {
        gl_Position = vec4(1.0 - 2.0 * position, 0, 1);
      }
    `,
    attributes: {
      position: [-2, 0, 0, -2, 2, 2]
    },
    primitive: 'triangles',
    count: 3,
    context: {
      resolution: function (context) {
        return [
          context.viewportWidth,
          context.viewportHeight
        ];
      }
    },
    uniforms: {
      t: r.context('time'),
      resolution: r.context('resolution'),
      aspect: cam.width / cam.height,
      cam: camTexture,
      noise: noiseTexture,
    }
  });

  r.frame(() => {
    r.clear({color: [0, 0, 0, 1]});
    if (cam.readyState === cam.HAVE_ENOUGH_DATA) {
      camTexture(cam);
    }
    draw();
  });
}

function applyCamStyles(cam) {
  cam.style.position = 'fixed';
  cam.style.left = 0;
  cam.style.bottom = 0;
  cam.style.width = '50vmin';
  cam.style.maxWidth = '150px';
  cam.style.height = 'auto';
  cam.style.transform = 'scale3d(-1, 1, 1)';
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console