css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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.

            
              <div class="loading">
  <div class="loading__progress">
    <div class="loading__progress__text">0%</div>
    <div class="loading__progress__bar"></div>
  </div>
</div>

<div id="webgl-container"></div>

<h1 class="title">
  <a class="title__text" href="https://www.facebook.com/Pili-1478488365788729/" target="_blank">Pili's Inktober</a>
</h1>

<div class="counter">
  <span class="counter__current">1</span>
  <span class="counter__total">/ 35</span>
</div>

<div class="credits">
  <h1 class="credits__title">fm_ph</h1>
  <span class="credits__links">
    <a class="credits__links-el" href="https://www.facebook.com/Pili-1478488365788729/" target="_blank">Pili</a>
    <a class="credits__links-el" href="https://twitter.com/FabMotte" target="_blank">Fabien Motte</a>
    <a class="credits__links-el" href="https://twitter.com/Pat_Hg" target="_blank">Patrick Heng</a>
  </span>
</div>

<div class="indications">
  Use left / right keys or click for navigate through the comic
</div>

<!-- Shaders -->
<script id="simulation_fragment_shader" type="x-shader/x-fragment">
  uniform float uTime;
  uniform sampler2D uTextureA;
  uniform sampler2D uTextureB;
  uniform float uProgress;

  varying vec2 vUv;

  vec3 mod289(vec3 x) {
    return x - floor(x * (1.0 / 289.0)) * 289.0;
  }

  vec4 mod289(vec4 x) {
    return x - floor(x * (1.0 / 289.0)) * 289.0;
  }

  vec4 permute(vec4 x) {
    return mod289(((x*34.0)+1.0)*x);
  }

  vec4 taylorInvSqrt(vec4 r) {
    return 1.79284291400159 - 0.85373472095314 * r;
  }

  vec3 fade(vec3 t) {
    return t*t*t*(t*(t*6.0-15.0)+10.0);
  }

  float cnoise3(vec3 P) {
    vec3 Pi0 = floor(P); // Integer part for indexing
    vec3 Pi1 = Pi0 + vec3(1.0); // Integer part + 1
    Pi0 = mod289(Pi0);
    Pi1 = mod289(Pi1);
    vec3 Pf0 = fract(P); // Fractional part for interpolation
    vec3 Pf1 = Pf0 - vec3(1.0); // Fractional part - 1.0
    vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
    vec4 iy = vec4(Pi0.yy, Pi1.yy);
    vec4 iz0 = Pi0.zzzz;
    vec4 iz1 = Pi1.zzzz;

    vec4 ixy = permute(permute(ix) + iy);
    vec4 ixy0 = permute(ixy + iz0);
    vec4 ixy1 = permute(ixy + iz1);

    vec4 gx0 = ixy0 * (1.0 / 7.0);
    vec4 gy0 = fract(floor(gx0) * (1.0 / 7.0)) - 0.5;
    gx0 = fract(gx0);
    vec4 gz0 = vec4(0.5) - abs(gx0) - abs(gy0);
    vec4 sz0 = step(gz0, vec4(0.0));
    gx0 -= sz0 * (step(0.0, gx0) - 0.5);
    gy0 -= sz0 * (step(0.0, gy0) - 0.5);

    vec4 gx1 = ixy1 * (1.0 / 7.0);
    vec4 gy1 = fract(floor(gx1) * (1.0 / 7.0)) - 0.5;
    gx1 = fract(gx1);
    vec4 gz1 = vec4(0.5) - abs(gx1) - abs(gy1);
    vec4 sz1 = step(gz1, vec4(0.0));
    gx1 -= sz1 * (step(0.0, gx1) - 0.5);
    gy1 -= sz1 * (step(0.0, gy1) - 0.5);

    vec3 g000 = vec3(gx0.x,gy0.x,gz0.x);
    vec3 g100 = vec3(gx0.y,gy0.y,gz0.y);
    vec3 g010 = vec3(gx0.z,gy0.z,gz0.z);
    vec3 g110 = vec3(gx0.w,gy0.w,gz0.w);
    vec3 g001 = vec3(gx1.x,gy1.x,gz1.x);
    vec3 g101 = vec3(gx1.y,gy1.y,gz1.y);
    vec3 g011 = vec3(gx1.z,gy1.z,gz1.z);
    vec3 g111 = vec3(gx1.w,gy1.w,gz1.w);

    vec4 norm0 = taylorInvSqrt(vec4(dot(g000, g000), dot(g010, g010), dot(g100, g100), dot(g110, g110)));
    g000 *= norm0.x;
    g010 *= norm0.y;
    g100 *= norm0.z;
    g110 *= norm0.w;
    vec4 norm1 = taylorInvSqrt(vec4(dot(g001, g001), dot(g011, g011), dot(g101, g101), dot(g111, g111)));
    g001 *= norm1.x;
    g011 *= norm1.y;
    g101 *= norm1.z;
    g111 *= norm1.w;

    float n000 = dot(g000, Pf0);
    float n100 = dot(g100, vec3(Pf1.x, Pf0.yz));
    float n010 = dot(g010, vec3(Pf0.x, Pf1.y, Pf0.z));
    float n110 = dot(g110, vec3(Pf1.xy, Pf0.z));
    float n001 = dot(g001, vec3(Pf0.xy, Pf1.z));
    float n101 = dot(g101, vec3(Pf1.x, Pf0.y, Pf1.z));
    float n011 = dot(g011, vec3(Pf0.x, Pf1.yz));
    float n111 = dot(g111, Pf1);

    vec3 fade_xyz = fade(Pf0);
    vec4 n_z = mix(vec4(n000, n100, n010, n110), vec4(n001, n101, n011, n111), fade_xyz.z);
    vec2 n_yz = mix(n_z.xy, n_z.zw, fade_xyz.y);
    float n_xyz = mix(n_yz.x, n_yz.y, fade_xyz.x);
    return 2.2 * n_xyz;
  }

  void main() {

    //origin
    vec3 origin = texture2D( uTextureA, vUv ).xyz;

    //destination
    vec3 destination = texture2D( uTextureB, vUv ).xyz;

    //lerp
    vec3 pos = mix( origin, destination, uProgress );

    float noise = cnoise3( 0.2 * pos + vec3( uTime ) );
    float displacement = 7. * noise;

    pos.x += displacement / 10.;
    pos.y += displacement;
    pos.z += displacement / 10.;

    gl_FragColor = vec4( pos, 1.0 );
  }

</script>

<script id="simulation_vertex_shader" type="x-shader/x-vertex">
  varying vec2 vUv;
  void main() {
      vUv = vec2(uv.x, uv.y);
      gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  }
</script>

<script id="render_fragment_shader" type="x-shader/x-fragment">
  void main() {
    gl_FragColor = vec4( vec3( .0 ), 0.9 );
  }
</script>

<script id="render_vertex_shader" type="x-shader/x-vertex">
  uniform sampler2D uPositions;
  uniform float uThreshold;

  uniform float pointSize;

  void main() {

    vec3 pos = texture2D( uPositions, position.xy ).xyz;

    if(pos.y > uThreshold) {
        gl_Position = projectionMatrix * modelViewMatrix * vec4( pos.x, 2000., pos.z, 1.0 );
    } else {
        gl_Position = projectionMatrix * modelViewMatrix * vec4( pos, 1.0 );
    }

    gl_PointSize = pointSize;
  }

</script>
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Fira+Mono');

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
  font-family: 'Fira Mono', helvetica, arial, sans-serif;
  font-weight: 400;
  font-size: 62.5%;
}

#webgl-container {
  width: 100%;
  height: 100%;
}

.loading {
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: white;
  opacity: 1;
  -webkit-transition: opacity 1.2s ease-out;
  -o-transition: opacity 1.2s ease-out;
  transition: opacity 1.2s ease-out;
  pointer-events: none;
  z-index: 5;
}

.loading__progress {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  -webkit-transition: opacity 0.25s ease-out;
  -o-transition: opacity 0.25s ease-out;
  transition: opacity 0.25s ease-out;
  opacity: 0;
}

.loading__progress__text {
  font-size: 1.6rem;
  color: black;
  margin-bottom: 1rem;
}

.loading__progress__bar {
  display: inline-block;
  width: 10rem;
  height: 0.2rem;
  background-color: black;
  transform: scale(0, 0);
  transform-origin: center center;
  -webkit-transition: transform 0.25s ease-out;
  -o-transition: transform 0.25s ease-out;
  transition: transform 0.25s ease-out;
}

.title {
  position: absolute;
  left: 50%;
  top: 20px;
  height: 2rem;
  color: black;
  transform: translate(-50%, 0);
  font-size: 1.6rem;
  text-transform: uppercase;
  overflow: hidden;
  user-select: none;
}

.title__text {
  display: inline-block;
  color: black;
  text-decoration: none;
  transition: color .25s ease-out;
  
  &:hover {
    color: #AAA;
  }
}

.counter {
  position: absolute;
  bottom: 60px;
  left: 50%;
  transform: translate(-50%, 0);
  font-size: 1.4rem;
}

.oui,
.credits,
.counter,
.indications {
  user-select: none;
}

.credits {
  position: absolute;
  right: 20px;
  bottom: 20px;
  color: black;
  text-align: right;
}

.credits__title {
  font-size: 1.5rem;
  margin-bottom: 0.5absoluterem;
}

.credits__links-el {
  position: relative;
  font-size: 1rem;
  color: black;
  text-decoration: none;
  font-weight: 200;
  transition: color .25s ease-out;
  margin-right: 2rem;

  &:before {
    position: absolute;
    content: '';
    display: block;
    left: 0;
    bottom: -0.2rem;
    width : 100%;
    height: 0.2rem;
    background: #AAA;
    opacity: 0;
    transform: translate3d(0, 0.3rem, 0);
    -webkit-transition: transform 0.25s ease-out, opacity 0.25s ease-out;
    -o-transition: transform 0.25s ease-out, opacity 0.25s ease-out;
    transition: transform 0.25s ease-out, opacity 0.25s ease-out;
  }

  &:hover:before {
    opacity: 1;
    transform: none;
  }

  &:after {
    position: absolute;
    content: '|';
    top: 0.3rem;
    left: 100%;
    margin-left: 0.9rem;
    opacity: 0.8;
  }

  &:last-child {
    margin-right: 0rem;
  }

  &:last-child:after {
    content: '';
  }

  &:hover {
    font-size: 1rem;
    color: #AAA;
    text-decoration: none;
    font-weight: 200;
  }
}

.indications {
  position: absolute;
  left: 50%;
  bottom: 18px;
  color: black;
  transform: translate(-50%, 0);
  font-size: 1.1rem;
  line-height: 1.6rem;
  text-align: center;
}

.cursor-previous {
  cursor: w-resize;
}

.cursor-next {
  cursor: e-resize;
}
            
          
!
            
              const assets = []

const ASSET_PATH = 'https://lab.hengpatrick.fr/codevember-assets/fm_ph/04_maud_drawings/assets/img/'

const config = {
  ELEVATION: 50,
  RESOLUTION: new THREE.Vector2(500, 500),
  POINT_SIZE: 1 * window.devicePixelRatio
}

const postProcessingConfig = {
  active: false,
  passes: []
}

class FBOAnimation {

  constructor ({ renderer, loader, imgNb }) {
    this.renderer = renderer
    this.resolution = config.RESOLUTION
    this.fbo = new helpers.FBO()
    this.loader = loader

    this.dataTextures = []

    this.imgs = []
    this.imgNb = imgNb

    for (let i = 0; i < this.imgNb; i++) {
      const img = this.loader.get('draw_' + i)
      this.imgs.push(img)
      this.createFromImage(img, config.ELEVATION)
    }

    this.initFBOMorph({
      dataTextureA: this.dataTextures[0],
      dataTextureB: this.dataTextures[0],
      pointSize: config.POINT_SIZE
    })

    this.tl = new TimelineMax({ paused: true })
  }

  createFromImage (image, elevation) {
    image.crossOrigin = "Anonymous"
    const imageData = utils.getImage(image, image.width, image.height, elevation)

    const dataTexture = new THREE.DataTexture(imageData, image.width, image.height, THREE.RGBFormat, THREE.FloatType)

    dataTexture.needsUpdate = true

    this.dataTextures.push(dataTexture)

    return dataTexture
  }

  initFBOMorph ({ dataTextureA, dataTextureB, pointSize = 1 }) {
    this.simulationShader = new THREE.ShaderMaterial({

      uniforms: {
        uTime: { type: 'f', value: 0.0 },
        uTextureA: { type: 't', value: dataTextureA },
        uTextureB: { type: 't', value: dataTextureB },
        uProgress: { type: 'f', value: 0 }
      },
      vertexShader: document.getElementById( 'simulation_vertex_shader' ).textContent,
      fragmentShader: document.getElementById( 'simulation_fragment_shader' ).textContent
    })

    this.renderShader = new THREE.ShaderMaterial({
      uniforms: {
        uPositions: { type: 't', value: null },
        uThreshold: { type: 'f', value: config.ELEVATION * 0.7 },
        pointSize: { type: 'f', value: pointSize }
      },
      vertexShader: document.getElementById( 'render_vertex_shader' ).textContent,
      fragmentShader: document.getElementById( 'render_fragment_shader' ).textContent
    })

    this.fbo.init(this.resolution.x, this.resolution.y, this.renderer, this.simulationShader, this.renderShader)

    this.points = this.fbo.particles
    this.points.frustumCulled = true
  }

  setDrawing (index) {
    this.tl.pause()
    this.tl.clear()

    this.simulationShader.uniforms.uTextureA.value = this.simulationShader.uniforms.uTextureB.value
    this.simulationShader.uniforms.uTextureB.value = this.dataTextures[index]

    this.tl
      .fromTo(this.simulationShader.uniforms.uProgress, 4, {
        value: 0
      }, {
        value: 1,
        ease: Expo.easeOut
      }, 0)

    this.tl.play(0)
  }

  dispose () {
    this.fbo.dispose()
    this.simulationShader.dispose()
    this.renderShader.dispose()

    for (let i = 0; i < this.dataTextures.length; i++) {
      this.dataTextures[ i ].dispose()
    }
  }

  update (time) {
    this.simulationShader.uniforms.uTime.value = time
    this.fbo.update()
  }
}

class Renderer extends THREE.WebGLRenderer {

  constructor (options = { antialias: true, alpha: true }) {
    super(options)

    this.setSize(window.innerWidth, window.innerHeight)
    this.setPixelRatio(window.devicePixelRatio)
    this.setClearColor(0xfefefe, 1.0)

    helpers.Window.add(::this.resize)
  }

  resize (width, height) {
    this.setSize(width, height)
  }
}

class Camera extends THREE.PerspectiveCamera {

  /**
   * constructor method
   */
  constructor (fov, aspect, near, far) {
    super(fov, aspect, near, far)

    this.resize = this.resize.bind(this)
    helpers.Window.add(this.resize)
  }

  update (delta) {}

  resize (width, height) {
    this.aspect = width / height
    this.updateProjectionMatrix()
  }
}

class Scene extends THREE.Scene {

  constructor (Renderer, Camera, Loader) {
    super()

    this.renderer = Renderer
    this.camera = Camera
    this.loader = Loader

    this.postProcessing = new helpers.PostProcessing(this, this.camera, this.renderer, postProcessingConfig)
    this.clock = new helpers.Clock()

    this.fboPointsRotationTarget = new THREE.Vector3()
    this.baseFBORotationX = Math.PI / 2

    this.slideIndex = 0
    this.imgNb = 35

    this.currentCounter = document.querySelector('.counter__current')

    this.bind()
    this.createScene()
    this.enterAnimation()
  }

  bind () {
    this.renderer.domElement.addEventListener('click', ::this.handleClick, false)
    document.addEventListener('mousemove', ::this.handleMouseMove, false)
    document.addEventListener('keydown', ::this.handleKeyDown, false)
  }

  createScene() {
    this.fboAnimation = new FBOAnimation({
      renderer: this.renderer,
      loader: this.loader,
      imgNb: this.imgNb
    })

    this.fboAnimation.points.rotation.x = this.baseFBORotationX

    this.fboPointsRotationTarget.x = - this.baseFBORotationX
    this.fboPointsRotationTarget.z = 0

    this.fboAnimation.points.scale.multiplyScalar(0.32)
    this.add(this.fboAnimation.points)

    this.render = this.render.bind(this)
    this.raf = requestAnimationFrame(this.render)
  }

  enterAnimation() {
    this.tl = new TimelineMax()
    const title = document.querySelector('.title__text')
    const counter = document.querySelector('.counter')
    const indications = document.querySelector('.indications')

    this.tl
      .from(this.camera.position, 5, { z: this.camera.position.z + 2000, ease: Expo.easeOut }, 0)
      .from(title, 1.5, { y: 20, ease: Expo.easeOut }, 2)
      .from(counter, 1.5, { y: '100%', opacity: 0, ease: Expo.easeOut }, 2.6)
      .from(indications, 1.5, { opacity: 0, ease: Expo.easeOut }, 3)
  }

  previousDraw() {
    if( this.slideIndex <= 0) return
    this.slideIndex--
    this.updateSlideIndex()
  }

  nextDraw() {
    if(this.slideIndex >= this.imgNb - 1) return
    this.slideIndex++
    this.updateSlideIndex()
  }

  updateSlideIndex() {
    this.fboAnimation.setDrawing(this.slideIndex)
    this.currentCounter.innerHTML = this.slideIndex + 1
  }

  handleKeyDown(event) {
    if(event.keyCode == 39){
      this.nextDraw()
    } else if(event.keyCode == 37) {
      this.previousDraw()
    }
  }

  handleClick(event) {
    if (event.clientX < window.innerWidth / 2) {
      this.previousDraw()
    } else {
      this.nextDraw()
    }
  }

  handleMouseMove(event) {
    this.fboPointsRotationTarget.x = -this.baseFBORotationX - utils.map( event.clientY / window.innerHeight, 0, 1, -Math.PI/20, Math.PI/20 )
    this.fboPointsRotationTarget.z = utils.map( event.clientX / window.innerWidth, 0, 1, -Math.PI/20, Math.PI/20 )

    if (event.clientX < window.innerWidth / 2) {

      document.body.classList.remove('cursor-next')
      if( this.slideIndex > 0 ) {
        document.body.classList.add('cursor-previous')
      } else {
        document.body.classList.remove('cursor-previous')
      }
    } else {
      document.body.classList.remove('cursor-previous')
      if( this.slideIndex < this.imgNb - 1 ) {
        document.body.classList.add('cursor-next')
      } else {
        document.body.classList.remove('cursor-next')
      }
    }
  }

  render () {
    this.fboAnimation.points.rotation.x -= ( this.fboPointsRotationTarget.x + this.fboAnimation.points.rotation.x ) * 0.1
    this.fboAnimation.points.rotation.z -= ( this.fboPointsRotationTarget.z + this.fboAnimation.points.rotation.z ) * 0.1

    this.fboAnimation.update(this.clock.time)

    this.postProcessing.update()

    this.camera.update(this.clock.delta)

    requestAnimationFrame(this.render)
  };
}

class App {

  /**
   * begin method
   */
  static begin () {
    const credits = document.querySelector('.credits')
    const loadingEl = document.querySelector('.loading')
    const progress = document.querySelector('.loading__progress')
    const progressTextEl = document.querySelector('.loading__progress__text')
    const progressBarEl = document.querySelector('.loading__progress__bar')

    credits.style.opacity = 1
    progress.style.opacity = 1

    // Renderer
    const renderer = new Renderer()
    const container = document.getElementById('webgl-container')
    container.appendChild(renderer.domElement)

    // Camera
    const camera = new Camera(75, window.innerWidth / window.innerHeight, 0.1, 10000)
    camera.position.z = 300

    const drawNb = 35

    for (let i = 0; i < drawNb; i++) {
      assets.push({ id: 'draw_' + i, url: ASSET_PATH + i + '.jpg', type: 'jpg',crossOrigin: 'anonymous' })
    }

    // Scene
    if (assets.length) {
      const loader = helpers.assetsLoader({ assets, crossOrigin: 'anonymous' })

      loader.on('progress', progress => {
        progressTextEl.innerHTML = `${Math.floor(progress * 100)}%`
        progressBarEl.style.transform = `scale(${progress}, ${progress})`
      })

      loader.on('complete', assets => {
        const scene = new Scene(renderer, camera, loader)
        loadingEl.style.opacity = 0
      })
      loader.start()
    } else {
      loadingEl.style.display = 'none'
      const scene = new Scene(renderer, camera)
    }
  }
}

let the = App.begin()


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

Console