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

              
                <body class="bg-gray-900 text-white">
  <!-- additional section -->
  <section class="h-screen bg-blue-600 flex flex-col items-center justify-center"><strong class="text-4xl">A</strong>scroll down</section>
  <!-- section -->
  <section class="overflow-hidden bg-green-900">
    <div data-gsap="section-home" class="h-screen relative flex flex-col items-center justify-center">
      <!-- canvas one -->
      <div data-gsap-canvas-wrapper="one" class="absolute z-1">
        <canvas data-gsap="sequenceOne"></canvas>
      </div>
      <!-- text in the middle of sequence -->
      <div data-gsap-item="textOne" class="absolute z-1 opacity-0 invisible text-4xl text-center uppercase">Show some text <br>in the middle of the sequence!</div>
      <!-- canvas two -->
      <div data-gsap-canvas-wrapper="two" class="absolute z-1 opacity-0 invisible">
        <canvas data-gsap="sequenceTwo"></canvas>
      </div>
      <!-- end -->
    </div>
  </section>
  <!-- additional section -->
  <section class="h-screen bg-purple-600 flex items-center justify-center"><strong class="text-4xl">C</strong></section>
  <!-- end -->
</body>
              
            
!

CSS

              
                
              
            
!

JS

              
                gsap.registerPlugin(ScrollTrigger);

// selector
let section = document.querySelector('[data-gsap="section-home"]');

//
let canvasWrapperOne = document.querySelector(
  '[data-gsap-canvas-wrapper="one"]'
);
let canvasWrapperTwo = document.querySelector(
  '[data-gsap-canvas-wrapper="two"]'
);

//
let canvasOne = document.querySelector('[data-gsap="sequenceOne"]');
let canvasTwo = document.querySelector('[data-gsap="sequenceTwo"]');

//
let contextOne = canvasOne.getContext("2d");
let contextTwo = canvasTwo.getContext("2d");

//
canvasOne.width = 1000;
canvasOne.height = 1000;
canvasTwo.width = 1000;
canvasTwo.height = 1000;

//
let frameCountOne = 26;
let frameCountTwo = 25;

//
const currentFrameOne = (index) =>
  `https://kubik101.com.au/clients/protect/sequences/1000px/001/${(index + 1)
    .toString()
    .padStart(4, "0")}.webp`;
const currentFrameTwo = (index) =>
  `https://kubik101.com.au/clients/protect/sequences/1000px/002/${(index + 1)
    .toString()
    .padStart(4, "0")}.webp`;

//
let imagesOne = [];
let imagesTwo = [];

//
let sequenceOne = {
  frame: 0
};
let sequenceTwo = {
  frame: 0
};

//
for (let i = 0; i < frameCountOne; i++) {
  let img = new Image();
  img.src = currentFrameOne(i);
  imagesOne.push(img);
}
for (let ii = 0; ii < frameCountTwo; ii++) {
  let img = new Image();
  img.src = currentFrameTwo(ii);
  imagesTwo.push(img);
}

// gsap timeline ******************************

let tl = gsap.timeline({
  onUpdate: render,
  defaults: {
    ease: "none"
  },
  scrollTrigger: {
    trigger: section,
    pin: true,
    start: "top top",
    end: "+=500%",
    scrub: 0,
    anticipatePin: 1
  }
});

// label
tl.addLabel("startCavasOne");

// sequence 1
tl.to(
  sequenceOne,
  {
    frame: frameCountOne - 1,
    snap: "frame",
    duration: 2
  },
  "startCavasOne"
);
// sequence 1 wrapper
tl.to(
  canvasWrapperOne,
  {
    startAt: {
      opacity: 1
    },
    opacity: 1,
    autoAlpha: 1,
    duration: 2
  },
  "startCavasOne"
);

// hold for 1 second
tl.add(function () {}, "+=1");

// text transition in
tl.to(
  '[data-gsap-item="textOne"]',
  {
    startAt: {
      y: () => "+=" + 100,
      opacity: 0
    },
    y: 0,
    opacity: 1,
    autoAlpha: 1,
    duration: 1
  },
  ">"
);

// hold for 1 second
tl.add(function () {}, "+=1");

// text transition out
tl.to(
  '[data-gsap-item="textOne"]',
  {
    startAt: {
      y: 0,
      opacity: 1
    },

    y: () => "-=" + 100,
    opacity: 0,
    autoAlpha: 1,
    duration: 1
  },
  ">"
);

// label
tl.addLabel("startCavasTwo");

// hide sequence 1
tl.to(
  canvasWrapperOne,
  {
    opacity: 0,
    autoAlpha: 1,
    duration: 0
  },
  "startCavasTwo"
);

// sequence 2
tl.to(
  sequenceTwo,
  {
    frame: frameCountTwo - 1,
    snap: "frame",
    duration: 2
  },
  "startCavasTwo"
);
// sequence 2 wrapper
tl.to(
  canvasWrapperTwo,
  {
    startAt: {
      opacity: 1
    },
    opacity: 1,
    autoAlpha: 1,
    duration: 2
  },
  "startCavasTwo"
);

// hold for 1 second
tl.add(function () {}, "+=1");

// end timeline elements ******************************

//
imagesOne[0].onload = render;
imagesTwo[0].onload = render;

//
function render() {
  // one
  contextOne.clearRect(0, 0, canvasOne.width, canvasOne.height);
  contextOne.drawImage(imagesOne[sequenceOne.frame], 0, 0);
  // two
  contextTwo.clearRect(0, 0, canvasTwo.width, canvasTwo.height);
  contextTwo.drawImage(imagesTwo[sequenceTwo.frame], 0, 0);
  //
}

              
            
!
999px

Console