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

              
                <canvas id="webgl" width="500" height="1758"></canvas>

<script id="vertexShader" type="x-shader/x-vertex">
  attribute vec4 a_position;
  
  uniform mat4 u_modelViewMatrix;
  uniform mat4 u_projectionMatrix;
  
  void main() {
    gl_Position = a_position;
  }
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
  precision highp float;
  
  uniform vec2 u_resolution;
  uniform vec2 u_mouse;
  uniform float u_time;
  uniform sampler2D u_noise;
  uniform sampler2D u_bricks;
  
  vec2 getScreenSpace() {
    vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);
    
    return uv;
  }
  
  vec3 randcol(float i) {
    i = fract(i/4.);
    if(i < .25) {
      return vec3(0,0,.5);
    } else if(i < .5) {
      return vec3(0,.5,.5);
    } else if(i < .75) {
      return vec3(0,0,.2);
    } else if(i < 1.) {
      return vec3(0,.3,.6);
    }
  }
  
  vec2 hash12(float i) {
    return texture2D(u_noise, vec2(i/255.)).xy;
  }
  
  #define PI 3.14159265359

  void main() {
    vec2 uv = getScreenSpace();
    
    uv *= 15. + sin(u_time*.5) * 10.;
    uv.y += sin(u_time*.3)*10.;
    
    vec2 guv = vec2(uv.x, fract(uv.y));
    float id = floor(uv.y);
    
    vec3 col = vec3(0);
    
    for(float i = -2.; i < 1.; i++) {
      vec2 suv = guv + vec2(0., i);
      vec2 suv1 = guv + vec2(0., i-1.);
      
      float sid = id - i;
      float sid1 = id - i - 1.;
      
      vec2 hash = hash12(sid);
      vec2 hash1 = hash12(sid1);
      
      vec2 p = suv - vec2(u_time*hash1.x, 0);
      
      suv.x += u_time*10.*(hash.x*hash.x);
      suv1.x += u_time*10.*(hash1.x*hash1.x);
      
      float wave = sin(suv.x + sid) + cos(suv.x * hash.y) * (hash.y*10.);
      float wave1 = sin(suv1.x + sid1) + cos(suv1.x * hash1.y) * (hash1.y*10.);
      
      p = vec2(length(p*.02), ((p.y) + wave * .1))*.25;
      
      float sfield = (suv.y + wave * .1);
      float sfield1 = (suv.y + wave1 * .1);
      
      float mask = smoothstep(.05, .0, sfield);
      
      const vec3 eps = vec3(.001, .001, 0);
      vec4 tex = texture2D(u_bricks, p);
      tex += texture2D(u_bricks, p + eps.xy);
      tex += texture2D(u_bricks, p - eps.xy);
      tex += texture2D(u_bricks, p + eps.xy * vec2(1, -1));
      tex += texture2D(u_bricks, p - eps.xy * vec2(1, -1));
      tex *= .25;
      
      col = mix(col, (randcol(sid)+.2) * tex.gbr * (suv.y * .5 + .3 + smoothstep(-1.5, -.3, sfield1)), mask);
    }

    gl_FragColor = vec4(col,1.0);
  }
  
</script>
              
            
!

CSS

              
                body {
  margin:0;
}

canvas {
  position: fixed;
}
              
            
!

JS

              
                console.clear();

const twodWebGL = new WTCGL(
  document.querySelector('canvas#webgl'), 
  document.querySelector('script#vertexShader').textContent, 
  document.querySelector('script#fragmentShader').textContent,
  window.innerWidth,
  window.innerHeight,
  window.devicePixelRatio
);
twodWebGL.startTime = -100 + Math.random() * 50;

window.addEventListener('resize', () => {
  twodWebGL.resize(window.innerWidth, window.innerHeight);
});





// track mouse move
let mousepos = [0,0];
const u_mousepos = twodWebGL.addUniform('mouse', WTCGL.TYPE_V2, mousepos);
window.addEventListener('pointermove', (e) => {
  let ratio = window.innerHeight / window.innerWidth;
  if(window.innerHeight > window.innerWidth) {
    mousepos[0] = (e.pageX - window.innerWidth / 2) / window.innerWidth;
    mousepos[1] = (e.pageY - window.innerHeight / 2) / window.innerHeight * -1 * ratio;
  } else {
    mousepos[0] = (e.pageX - window.innerWidth / 2) / window.innerWidth / ratio;
    mousepos[1] = (e.pageY - window.innerHeight / 2) / window.innerHeight * -1;
  }
  twodWebGL.addUniform('mouse', WTCGL.TYPE_V2, mousepos);
});









// Load all our textures. We only initiate the instance once all images are loaded.
const textures = [
  {
    name: 'noise',
    url: 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/982762/noise.png',
    type: WTCGL.IMAGETYPE_TILE,
    img: null
  },
  {
    name: 'bricks',
    url: 'https://assets.codepen.io/982762/Brick-2377.jpg',
    type: WTCGL.IMAGETYPE_TILE,
    img: null
  }
];
const loadImage = function (imageObject) {
  let img = document.createElement('img');
  img.crossOrigin="anonymous";
  
  return new Promise((resolve, reject) => {
    img.addEventListener('load', (e) => {
      imageObject.img = img;
      resolve(imageObject);
    });
    img.addEventListener('error', (e) => {
      reject(e);
    });
    img.src = imageObject.url
  });
}
const loadTextures = function(textures) {
  return new Promise((resolve, reject) => {
    const loadTexture = (pointer) => {
      if(pointer >= textures.length || pointer > 10) {
        resolve(textures);
        return;
      };
      const imageObject = textures[pointer];

      const p = loadImage(imageObject);
      p.then(
        (result) => {
          twodWebGL.addTexture(result.name, result.type, result.img);
        },
        (error) => {
          console.log('error', error)
        }).finally((e) => {
          loadTexture(pointer+1);
      });
    }
    loadTexture(0);
  });
  
}

loadTextures(textures).then(
  (result) => {
    twodWebGL.initTextures();
    // twodWebGL.render();
    twodWebGL.running = true;
  },
  (error) => {
    console.log('error');
  }
);
              
            
!
999px

Console