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

              
                  <main>
        <div id="container" class="container">
            <div class="row justify-content-center align-items-center mb-5">
                <div class="col-12 col-lg-4">
                    <img src="https://via.placeholder.com/300x200">
                    <h2 class="my-3">Hello ceci est un titre</h2>
                    <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quam harum laboriosam, assumenda
                        dolorum,
                        facere modi corrupti beatae, placeat exercitationem cupiditate ab. Assumenda modi odio beatae
                        consequuntur vero deserunt accusamus veritatis?</p>
                </div>
                <div class="col-12 col-lg-8">
                    <img src="https://via.placeholder.com/300x500">
                    <h2 class="my-3">Hello ceci est un titre</h2>
                    <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quam harum laboriosam, assumenda
                        dolorum,
                        facere modi corrupti beatae, placeat exercitationem cupiditate ab. Assumenda modi odio beatae
                        consequuntur vero deserunt accusamus veritatis?</p>
                </div>
            </div>
            <div class="row justify-content-center align-items-center mb-5">
                <div class="col-12 col-lg-8">
                    <img  src="https://via.placeholder.com/300x250">
                    <h2 class="my-3">Hello ceci est un titre</h2>
                    <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quam harum laboriosam, assumenda
                        dolorum,
                        facere modi corrupti beatae, placeat exercitationem cupiditate ab. Assumenda modi odio beatae
                        consequuntur vero deserunt accusamus veritatis?</p>
                </div>
                <div class="col-12 col-lg-4">
                    <img  src="https://via.placeholder.com/300x800">
                    <h2 class="my-3">Hello ceci est un titre</h2>
                    <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quam harum laboriosam, assumenda
                        dolorum,
                        facere modi corrupti beatae, placeat exercitationem cupiditate ab. Assumenda modi odio beatae
                        consequuntur vero deserunt accusamus veritatis?</p>
                </div>
            </div>
            <div class="row justify-content-center align-items-center mb-5">
                <div class="col-12 col-lg-6">
                    <img  src="https://via.placeholder.com/300x100">
                    <h2 class="my-3">Hello ceci est un titre</h2>
                    <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quam harum laboriosam, assumenda
                        dolorum,
                        facere modi corrupti beatae, placeat exercitationem cupiditate ab. Assumenda modi odio beatae
                        consequuntur vero deserunt accusamus veritatis?</p>
                </div>
                <div class="col-12 col-lg-6">
                    <img  src="https://via.placeholder.com/300x450">
                    <h2 class="my-3">Hello ceci est un titre</h2>
                    <p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Quam harum laboriosam, assumenda
                        dolorum,
                        facere modi corrupti beatae, placeat exercitationem cupiditate ab. Assumenda modi odio beatae
                        consequuntur vero deserunt accusamus veritatis?</p>
                </div>

            </div>
        </div>
    </main>
    <div id="canvas"></div>
              
            
!

CSS

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

img {
  width: 100%;
  opacity: 0.5;
}
              
            
!

JS

              
                window.onload = () => {
  
smoothScroll("#container", "main");
  /* Settings */
  let time = 0;
  const container = document.getElementById("canvas");
  const scene = new THREE.Scene();

  let width = container.offsetWidth;
  let height = container.offsetHeight;

  const camera = new THREE.PerspectiveCamera(70, width / height, 100, 2000);
  camera.position.z = 600;
  camera.fov = 2 * Math.atan(height / 2 / 600) * (180 / Math.PI);
  const renderer = new THREE.WebGLRenderer({
    antialias: true,
    alpha: true,
  });

  container.appendChild(renderer.domElement);

  const images = [...document.querySelectorAll("img")];
  const imageInCanvas = images.map((img) => {
    let bounds = img.getBoundingClientRect();
    const geometry = new THREE.PlaneBufferGeometry();

    const material = new THREE.MeshBasicMaterial({
      color: 0xff0000,
    });

    const mesh = new THREE.Mesh(geometry, material);
    mesh.scale.set(bounds.width, bounds.height, 1);
    scene.add(mesh);


    /* GSAP scroll test */

    // gsap.fromTo(
    //   mesh.position,
    //   {
    //     y: () => -img.height / 2 - height / 2,
    //   },
    //   {
    //     y: () => img.height / 2 + height / 2,
    //     ease: "none",
    //     scrollTrigger: {
    //       trigger: img,
    //       from: "top bottom",
    //       to: "bottom top",
    //       scrub: true,
    //       invalidateOnRefresh: true,
    //       markers: true,
    //     },
    //   }
    // );
    
    let tween;
    onRefresh();
    
    function onRefresh() {
      
      tween && tween.progress(0);
      
      let height = container.offsetHeight;
      
      tween = gsap.fromTo(mesh.position, {
        y: () => -img.height / 2 - height / 2,
      }, {
        y: () => img.height / 2 + height / 2,
        ease: "none",
        paused: true
      })
    }
        
    ScrollTrigger.create({
      trigger: img,
      from: "top bottom",
      to: "bottom top",
      onUpdate({ progress }) {
        tween.progress(progress);
        // renderer.render(scene, camera);
      },
      onRefresh({ progress}) {
        onRefresh();
        tween.progress(progress);
      }
    })

    return {
      img: img,
      mesh: mesh,
      top: bounds.top,
      left: bounds.left,
      width: bounds.width,
      height: bounds.height,
    };
  });

  imageInCanvas.forEach((o) => {
    o.mesh.position.x = o.left - width / 2 + o.width / 2;
  });

  /* Resize & animate */
  window.addEventListener("resize", () => resize());

  const resize = function () {
    if (imageInCanvas) {
      let width = container.offsetWidth;
      let height = container.offsetHeight;
      camera.fov = 2 * Math.atan(height / 2 / 600) * (180 / Math.PI);
      renderer.setSize(width, height);
      camera.aspect = width / height;
      camera.updateProjectionMatrix();

      imageInCanvas.forEach((o) => {
        let bounds = o.img.getBoundingClientRect();
        o.left = bounds.left;
        o.width = bounds.width;
        o.height = bounds.height;
        o.mesh.position.x = o.left - width / 2 + o.width / 2;
        o.mesh.scale.set(bounds.width, bounds.height, 1);
      });
    }
  };
  resize();
  
  gsap.ticker.add(() => {
    renderer.render(scene, camera);
  });
  
  // ScrollTrigger.create({
  //   onUpdate() {
  //     renderer.render(scene, camera);
  //   }
  // })
  
  // const animate = function () {
  //   time += 0.05;
  //   window.requestAnimationFrame(animate);
  //   renderer.render(scene, camera);
  // };
  // animate();
  
  


// this is the helper function that sets it all up. Pass in the content <div> and then the wrapping viewport <div> (can be the elements or selector text). It also sets the default "scroller" to the content so you don't have to do that on all your ScrollTriggers.
function smoothScroll(content, viewport, smoothness) {
  content = gsap.utils.toArray(content)[0];
  smoothness = smoothness || 1;

  gsap.set(viewport || content.parentNode, {overflow: "hidden", position: "fixed", height: "100%", width: "100%", top: 0, left: 0, right: 0, bottom: 0});
  gsap.set(content, {overflow: "visible", width: "100%"});
   
   getProp = gsap.getProperty(content)

  let setProp = gsap.quickSetter(content, "y", "px"),
    setScroll = ScrollTrigger.getScrollFunc(window),
    removeScroll = () => content.style.overflow = "visible",
    killScrub = trigger => {
      let scrub = trigger.getTween ? trigger.getTween() : gsap.getTweensOf(trigger.animation)[0]; // getTween() was added in 3.6.2
      scrub && scrub.kill();
      trigger.animation.progress(trigger.progress);
    },
    height, isProxyScrolling;

  function onResize() {
    height = content.clientHeight;
    content.style.overflow = "visible"
    document.body.style.height = height + "px";
  }
  onResize();
  ScrollTrigger.addEventListener("refreshInit", onResize);
  ScrollTrigger.addEventListener("refresh", () => {
    removeScroll();
    requestAnimationFrame(removeScroll);
  })
  ScrollTrigger.defaults({scroller: content});
  ScrollTrigger.prototype.update = p => p; // works around an issue in ScrollTrigger 3.6.1 and earlier (fixed in 3.6.2, so this line could be deleted if you're using 3.6.2 or later)

  ScrollTrigger.scrollerProxy(content, {
    scrollTop(value) {
      if (arguments.length) {
        isProxyScrolling = true; // otherwise, if snapping was applied (or anything that attempted to SET the scroll proxy's scroll position), we'd set the scroll here which would then (on the next tick) update the content tween/ScrollTrigger which would try to smoothly animate to that new value, thus the scrub tween would impede the progress. So we use this flag to respond accordingly in the ScrollTrigger's onUpdate and effectively force the scrub to its end immediately.
        setProp(-value);
        setScroll(value);
        return;
      }
      return -getProp("y");
    },
    getBoundingClientRect() {
      return {top: 0, left: 0, width: window.innerWidth, height: window.innerHeight};
    }
  });

  return ScrollTrigger.create({
    animation: gsap.fromTo(content, {y:0}, {
      y: () => document.documentElement.clientHeight - height,
      ease: "none",
      onUpdate: ScrollTrigger.update
    }),
    scroller: window,
    invalidateOnRefresh: true,
    start: 0,
    end: () => height - document.documentElement.clientHeight,
    scrub: smoothness,
    onUpdate: self => {
      if (isProxyScrolling) {
        killScrub(self);
        isProxyScrolling = false;
      }
    },
    onRefresh: killScrub // when the screen resizes, we just want the animation to immediately go to the appropriate spot rather than animating there, so basically kill the scrub.
  });
}
};


              
            
!
999px

Console