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 float u_xscale;

  vec2 diagonalhash2(vec2 p)
  {
    return fract(vec2(sin((p.x + p.y) * 15.543) * 73964.686, sin((p.x + p.y) * 55.8543)*28560.986));
  }
  
  vec3 getColour(float d) {
    vec3 c = vec3(0.15);
    if( d <= .25 ) {
      // #F44336
      c = vec3(0.9568627450980393, 0.2627450980392157, 0.21176470588235294);
      // c = vec3( 0.843, 0.149, 0.239 );
    } else if( d <= .5 ) {
      // #1E88E5
      c = vec3(0.11764705882352941, 0.5333333333333333, 0.8980392156862745);
      // c = vec3( 0.18, 0.161, 0.306 );
    } else if( d <= .75 ) {
      // #FDD835
      c = vec3(0.9921568627450981, 0.8470588235294118, 0.20784313725490197);
      // c = vec3( 0.773, 0.847, 0.427 );
    }
    
    return c;
  }
  
  vec3 pattern(vec2 uv, vec2 m) {
    
    vec2 grid = floor(uv);
    vec2 subuv = fract(uv);
    
    vec2 rand = diagonalhash2(grid);
    vec2 rand2 = diagonalhash2(grid + 100.);
    float shade = 0.;
    float df;
    
    // float s = sin(u_time);
    // float c = cos(u_time);
    // subuv *= mat2(c, -s, s, c);
    
    if( rand.x <= .25 ) {
      df = subuv.x - subuv.y; // tl
    } else if( rand.x <= .5 ) {
      df = 1. - subuv.y - subuv.x;
    } else if( rand.x <= .75 ) {
      df = subuv.y - subuv.x;
    } else if( rand.x <= 1. ) {
      // subuv += .1;
      df = subuv.y - 1. + subuv.x;
    }
    
    shade = sin(df * floor(20. * rand.x * rand.y));
    shade += sin(df * rand.x * rand.y);
    float aa = rand.x * rand.y * .001;
    shade = smoothstep(.0, aa, shade);
    
    float mouseMask = smoothstep(.1, .6, length(m));
    vec3 c1 = mix(getColour(rand.x), getColour(rand2.x), mouseMask);
    vec3 c2 = mix(getColour(rand.y), getColour(rand2.y), mouseMask);
    
    return mix(c1, c2, shade );
  }

  void main() {
    
    vec2 uv = gl_FragCoord.xy+gl_FragCoord.xy-floor(u_resolution.xy);

    uv = vec2(log(length(uv)/u_resolution.y)-2.3*u_time, atan(uv.y,uv.x)+u_time*.5 );
    //uv *= 4.;
    // float s = sin(1.);
    // float c = cos(1.);
    // uv = 1.911*uv*mat2(c,-1,1,1);
    uv = 1.911*uv*mat2(1.,-1,.9,1);
    // uv += vec2(u_time, 0.).yx;
    
    gl_FragColor = texture2D(u_noise, uv + diagonalhash2(uv + u_time)) * .2;
    
    vec2 m = u_mouse - (gl_FragCoord.xy - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);;
    
    
    vec3 colour = pattern(uv, m);

    gl_FragColor += vec4(colour,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,
  2
);
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
  }
];
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