Jump to content
Search Community

Scrolltrigger after initial images of sequence is played

Mohit Tilwani
Moderator Tag

Recommended Posts

Mohit Tilwani
Posted

Hey GSAP Team,

I am trying to achieve an animation where on page load, initial sequence of images are played and only after that scroll should be enabled and scroll trigger should take over and play sequence of image on scroll. I am able to achieve the first part of playing image sequence on page load, but stuck on the second part. Can you please nudge me in the right direction 🙏 Thanks. Below is my Vuejs code
 

<script>
import { gsap } from 'gsap'

export default {
  data() {
    return {
      canvas: null,
      context: null,
      images: [],
      images2: [],
      frames: { currentFrame: 0 },
      frames2: { currentFrame: 64 }
    }
  },
  mounted() {
    this.initializeCanvas()
    this.loadImages()
    this.initializeAnimation()
  },
  methods: {
    initializeCanvas() {
      this.canvas = this.$refs.canvas
      this.context = this.canvas.getContext('2d')
    },
    getFrameURL(index) {
      return `http://localhost:5173/demo-${(index)
        .toString()
        .padStart(4, '0')}.png`
    },
    loadImages() {
      for (let i = 0; i < 64; i++) {
        const img = new Image()
        img.src = this.getFrameURL(i)
        this.images.push(img)

        if ( i === 0) this.render()
      }
      for (let i = 64; i < 300; i++) {
        const img = new Image()
        img.src = this.getFrameURL(i)
        this.images2.push(img)
      }
    },
    initializeAnimation() {
      const tl = gsap.timeline()
      tl.to(this.frames, {
        currentFrame: 64 - 1,
        snap: 'currentFrame',
        ease: 'power1.inOut',
        duration: 2,
        onUpdate: this.render
      })
      tl.to(this.canvas,
        {
          y: 400,
          duration: 2,
          onComplete: () => {
            this.canvas.classList.add('canvas-moved')
          }
        })
      tl.to(this.frames2, {
        currentFrame: 299,
        snap: 'currentFrame',
        ease: 'power1.inOut',
        scrollTrigger: {
          trigger: '.canvas-moved',
          start: 'top top', 
          end: '+=300%',
          scrub: 1,
          markers: true
        },
        onUpdate: this.render2
      })
    },
    render() {
      this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)
      const img = this.images[this.frames.currentFrame]
      const scale = Math.min(this.canvas.width / img.width, this.canvas.height / img.height)
      this.context.drawImage(img, 0, 0, img.width * scale, img.height * scale)
    },
    render2() {
      this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)
      const img = this.images2[this.frames2.currentFrame - 64]
      console.log(this.frames2.currentFrame)
      console.log(img)
      const scale = Math.min(this.canvas.width / img.width, this.canvas.height / img.height)
      this.context.drawImage(img, 0, 0, img.width * scale, img.height * scale)
    }
  }
}
</script>

<template>
  <div class="canvas-container">
    <canvas ref="canvas" width="1560" height="1040" />
  </div>
</template>

<style scoped>
.canvas-container {
  position: relative;
  width: 100%;
  height: 100vh;
  overflow: hidden;
  z-index: 10;
}

canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  will-change: opacity;
}
</style>
Posted

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependencies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen.

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Posted

Hi,

 

Besides echoing the need of a minimal demo, you can set the overflow property of the body tag in your CSS to hidden and when the initial images animation sequence is completed, you can use the onComplete callback to change the overflow property to auto and create the ScrollTrigger instances or call ScrollTrigger refresh in order to update the ScrollTrigger instances that could've been created before running that initial image sequence.

 

https://developer.mozilla.org/en-US/docs/Web/CSS/overflow

 

Happy Tweening!

Posted

Hi @Mohit Tilwani welcome to the forum!

 

Your code seems to work perfectly fine, your only issue is that there is nothing to scroll, so changing overflow: hidden to auto does nothing, because the page is already fully scrolled. In the below pen I've just add some extra space to scroll by and it works perfectly. Hope it helps and happy tweening! 

 

See the Pen zYVpVmd by mvaneijgen (@mvaneijgen) on CodePen.

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...