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

Auto Save

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>
              
            
!

CSS

              
                html, body{
  height: 100%;
}

body{
  overflow: hidden;
  cursor: pointer;
  background-color: black;
}

.logo{
  color: white;
  font-size: 2em;
  font-weight: 400;
  font-family: "Montserrat", cursive;
  text-align: center;
}

canvas{
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100vh;
  z-index: 1000;
}

              
            
!

JS

              
                const vert = `
  precision mediump float;

  attribute vec3 position;
  attribute vec2 texcoord;

  uniform mat4 uMatrix;
  uniform mat4 uTmatrix;

  uniform float uTime;
  uniform vec2 uRes;
  uniform vec2 uOffset;
  uniform float uPower;

  varying vec2 vTexcoord;

  void main() {
    vec3 pos = position.xzy;

    float dist = distance(uOffset, vec2(pos.x, pos.y));
    float rippleEffect = cos(15.0 * (dist - (uTime / 60.0)));
    float distortionEffect = rippleEffect * uPower;

    pos.x += (distortionEffect / 30.0 * (uOffset.x - pos.x));
    pos.y += distortionEffect / 30.0 * (uOffset.y - pos.y);

    gl_Position = uMatrix * vec4(pos, 1.0);
    vTexcoord = (uTmatrix * vec4(texcoord - vec2(.5), 0, 1)).xy + vec2(.5);
  }  
`

const frag = `
  precision mediump float;

  uniform sampler2D uTexOne;
  uniform sampler2D uTexTwo;
  uniform float uProgress;

  varying vec2 vTexcoord;

  void main() {
    vec2 uv = vTexcoord;
  
    vec4 color = vec4(1.0);

    vec4 texOne = texture2D(uTexOne, uv);
    vec4 texTwo = texture2D(uTexTwo, uv);

    float effect = step(uv.x, uProgress);
    color = mix(texOne, texTwo, effect);

    gl_FragColor = color;
  }
`

function main() {
  const canvas = document.querySelector('#canvas')
  const gl = canvas.getContext('webgl')
  
  const m4 = twgl.m4
  
  const programInfo = twgl.createProgramInfo(gl, [vert, frag])
  const bufferInfo = twgl.primitives.createPlaneBufferInfo(gl, 1, 1, 30, 30)
  
  const images = [
    'https://images.unsplash.com/photo-1492571350019-22de08371fd3?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8NXx8amFwYW58ZW58MHx8MHx8&auto=format&fit=crop&w=600&q=60',
    'https://images.unsplash.com/photo-1512531123205-560f5974e686?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTl8fGNpdHl8ZW58MHx8MHx8&auto=format&fit=crop&w=600&q=60',
    'https://images.unsplash.com/photo-1655192972438-71a670198bfd?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwcm9maWxlLXBhZ2V8Mnx8fGVufDB8fHx8&auto=format&fit=crop&w=600&q=60',
    'https://images.unsplash.com/photo-1654431729108-8fe8a1bbbae6?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwcm9maWxlLXBhZ2V8MjF8fHxlbnwwfHx8fA%3D%3D&auto=format&fit=crop&w=600&q=60'
  ]
  
  const textures = []
  
  let values = {
    total: images.length - 1,
    power: 0,
    progress: 0,
    time: 0,
    offset: [0, 0],
    current: 0,
    next: 1
  }
  
  let isAnimating = false
  
  function createTextures() {
    images.forEach(src => {
      const info = {
        texture: null,
        width: 1,
        heigth: 1        
      }
      
      info.texture = createTexture(info, src)
      
      textures.push(info)
    })
  }
  
  function createTexture(info, src) {
    let texture = gl.createTexture()
    gl.bindTexture(gl.TEXTURE_2D, texture)

    const level = 0
    const internalFormat = gl.RGB
    const width = 1
    const height = 1
    const border = 0
    const srcFormat = gl.RGB
    const srcType = gl.UNSIGNED_BYTE
    const pixel = new Uint8Array([0, 0, 0, 0])

    gl.texImage2D(
      gl.TEXTURE_2D, 
      level, 
      internalFormat, 
      width, 
      height, 
      border, 
      srcFormat, 
      srcType, 
      pixel
    )

    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)

    const image = document.createElement('img')

    image.addEventListener('load', () => {
      gl.bindTexture(gl.TEXTURE_2D, texture)
      gl.texImage2D(
        gl.TEXTURE_2D, 
        level, 
        internalFormat, 
        srcFormat, 
        srcType, 
        image
      )

      info.width = image.width
      info.height = image.height
    })

    image.crossOrigin = ''
    image.src = src
    
    return texture
  }
  
  function render() {
    twgl.resizeCanvasToDisplaySize(gl.canvas)

    gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight)
    gl.clearColor(0, 0, 0, 0)
    gl.clear(gl.COLOR_BUFFER_BIT)

    let matrix = m4.identity()
    let tMatrix = m4.identity()
    
    const texAspect = textures[0].width / textures[0].height
    const imgAspect = gl.canvas.width / gl.canvas.height

    let scaleY = 0
    let scaleX = 0
    
    if (imgAspect < texAspect) {
      scaleY = 1
      scaleX = imgAspect / texAspect
    } else if (imgAspect > texAspect) {
      scaleY = texAspect / imgAspect
      scaleX = 1
    }
     
    m4.scale(tMatrix, [scaleX, scaleY, 1], tMatrix)

    values.time++
    
    m4.ortho(0, gl.canvas.width, gl.canvas.height, 0, -1, 1, matrix)
    m4.translate(matrix, [gl.canvas.width / 2, gl.canvas.height / 2, 1], matrix)
    m4.scale(matrix, [gl.canvas.width / 1.5, gl.canvas.height / 1.5, 1], matrix)

    gl.useProgram(programInfo.program)
    
    twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo)
    
    twgl.setUniforms(programInfo, {
      uMatrix: matrix,
      uTmatrix: tMatrix,
      uTexOne: textures[values.current].texture,
      uTexTwo: textures[values.next].texture,
      uTime: values.time,
      uPower: values.power,
      uProgress: values.progress,
      uRes: [gl.canvas.width, gl.canvas.height],
      uOffset: values.offset
    })
    
    twgl.drawBufferInfo(gl, bufferInfo)
  }
  
  function listeners() {
    canvas.addEventListener('click', onClick)
  }
  
  function changeSlide() {
    values.current = values.next
    values.next = values.next === values.total ? 0 : values.next + 1
  }
  
  function onClick(e) {
    if (isAnimating) return
    isAnimating = true
    
    values.offset = [
      e.clientX / gl.canvas.width * 2 - 1,
      e.clientY / gl.canvas.height * 2 - 1
    ]
    
    const master = new TimelineMax({ 
        onComplete: () => {
          isAnimating = false
          values.progress = 0
          changeSlide()
        }
    })
    
    const sub = new TimelineLite({ paused: true })

    sub
    .to(values, 0.5, { power: 1, ease: Linear.easeNone }, 0)
    .to(values, 1, { power: 0, ease: Linear.easeNone })
    
    master
    .to(sub, 1.5, { progress: 1, ease: Power2.easeInOut })
    .to(values, 1.15, { progress: 1, ease: Expo.easeInOut }, 0) 
  }
  
  function init () {
    createTextures()
    listeners()

    TweenMax.ticker.addEventListener('tick', render)
  }
  
  init()
}

main()



              
            
!
999px

Console