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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

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.

HTML

            
              <div id="container"></div>
<h1>Shader "parallax"</h1>

<script id="vertexShader" type="x-shader/x-vertex">
  void main()	{
    gl_Position = vec4(position, 1.0);
  }
</script>

<script id="fragmentShader" type="x-shader/x-fragment">
// FORKED FROM @patriciogv
uniform vec2 resolution;
uniform vec2 pointer;
uniform float time;

float random (in vec2 _st) { 
    return fract(sin(dot(_st.xy,
                         vec2(12.9898,78.233)))* 
        43758.5453123);
}

// Based on Morgan McGuire @morgan3d
// https://www.shadertoy.com/view/4dS3Wd
float noise (in vec2 _st) {
    vec2 i = floor(_st);
    vec2 f = fract(_st);

    // Four corners in 2D of a tile
    float a = random(i);
    float b = random(i + vec2(1.0, 0.0));
    float c = random(i + vec2(0.0, 1.0));
    float d = random(i + vec2(1.0, 1.0));

    vec2 u = f * f * (3.0 - 2.0 * f);

    return mix(a, b, u.x) + 
            (c - a)* u.y * (1.0 - u.x) + 
            (d - b) * u.x * u.y;
}

#define NUM_OCTAVES 5

float fbm (in vec2 _st) {
    float v = 0.2;
    float a = 0.5;
    vec2 shift = vec2(-pointer.x, pointer.y);
    for (int i = 0; i < NUM_OCTAVES; ++i) {
        v += a * noise(_st);
        _st = _st * 2.0 + shift;
        a *= 0.5;
    }
    return v;
}

void main() {
    vec2 st = gl_FragCoord.xy / resolution.xy * 3.0;
    vec3 color = vec3(0.0);

    vec2 r = vec2(0.);
    r.x = fbm(st + 1.0 + vec2(1.7, 9.2) + 0.15 * time);
    r.y = fbm(st + 1.0 + vec2(8.3, 2.8) + 0.126 * time);

    float f = fbm(st+r);
    
    vec2 pointer2 = pointer / 2.0;

    color = mix(color,
                vec3(0.574,0.631,0.990),
                clamp(length(pointer2 + 0.25),0.0,1.0));

    gl_FragColor = vec4((f*f*f+.6*f*f+.5*f) * color, 1.);
}
</script>
            
          
!

CSS

            
              hmtl, body { margin: 0; line-height: 0; overflow: hidden; }
#container { overflow: hidden; }

body {
  -webkit-font-smoothing: antialiased;
  font-smoothing: antialiased;
}

h1 {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #586198;
  line-height: 1;
  font-size: 5em;
  mix-blend-mode: lighten;
  font-family: sans-serif;
  font-weight: 500;
  
  @media (min-width: 960px) {
    font-size: 10em;
  }
}
            
          
!

JS

            
              
class Experiment {
  constructor() {
    this.container = null;
    this.renderer = null;
    this.camera = null;
    this.scene = null;
    this.uniforms = {};
    this.width = 0;
    this.height = 0;
  }

  init() {
    this.container = document.getElementById('container');
    this.camera = new THREE.Camera();
    this.camera.position.z = 2;
    this.scene = new THREE.Scene();
    this.renderer = new THREE.WebGLRenderer();
    this.renderer.setPixelRatio(window.devicePixelRatio);
    this.container.appendChild(this.renderer.domElement);
    this.width = window.innerWidth;
    this.height = window.innerHeight;
  
    this.hasDeviceMotion = window.DeviceMotionEvent;

    this.uniforms = {
      time: {
        value: 1.0
      },
      resolution: {
        value: new THREE.Vector2()
      },
      pointer: {
        value: new THREE.Vector2(0.5, 0.5)
      }
    };

    this.createShaderViewport();
    this.addEventListeners();
    this.onResize();
    this.animate();
  }

  createShaderViewport() {
    const geometry = new THREE.PlaneBufferGeometry(2, 2);
    const material = new THREE.ShaderMaterial({
      uniforms: this.uniforms,
      vertexShader: document.getElementById('vertexShader').textContent,
      fragmentShader: document.getElementById('fragmentShader').textContent
    });
    const mesh = new THREE.Mesh(geometry, material);
    this.scene.add(mesh);
  }

  addEventListeners() {
    window.addEventListener('resize', this.onResize.bind(this), false);
    
    // if (this.hasDeviceMotion) {
    //   window.addEventListener('devicemotion', this.onDeviceMotion.bind(this), false);
    // } else {
    //   window.addEventListener('pointermove', this.onPointerMove.bind(this), false); 
    // }
    
    window.addEventListener('mousemove', this.onPointerMove.bind(this), false);
    window.addEventListener('touchmove', this.onPointerMove.bind(this), false);
  }

  onResize() {
    this.width = window.innerWidth;
    this.height = window.innerHeight;
    this.renderer.setSize(this.width, this.height);
    this.uniforms.resolution.value.x = this.renderer.domElement.width;
    this.uniforms.resolution.value.y = this.renderer.domElement.height;
  }
  
  onDeviceMotion(event) {
    this.uniforms.pointer.value.x = event.accelaration.x;
    this.uniforms.pointer.value.y = event.accelaration.y;
  }

  onPointerMove(event) {
    event.preventDefault();
    const x = event.x || event.pageX || event.touches[0].pageX;
    const y = event.y || event.pageY || event.touches[0].pageY;
    this.uniforms.pointer.value.x = x / this.width;
    this.uniforms.pointer.value.y = y / this.height;
  }

  animate() {
    requestAnimationFrame(this.animate.bind(this));
    this.render();
  }

  render() {
    this.uniforms.time.value += 0.01;
    this.renderer.render(this.scene, this.camera);
  }
}

const experiment = new Experiment();
experiment.init();

            
          
!
999px

Console