Jump to content
Search Community

Tween start callback/end callback, timeline start callback not getting fired in following code

Mukund Kumar test
Moderator Tag

Go to solution Solved by OSUblake,

Recommended Posts

I am new to gsap and currently exploring gsap timeline. in the following code, Tween start callback/end callback, timeline start callback and parentTimeline start callback not getting fired. but if the duration value is greater than 0, then it works. is there anything that i miss in the following code  in order to work these callback properly ?

 

const parentTimeline = gsap.timeline();
const timeline = gsap.timeline();
const tween = gsap.fromTo("h1", {x: 0}, {x: 300,  duration:0});
timeline.add(tween);
tween.eventCallback("onStart", () => {
     console.log("tween start callback")
 });
timeline.eventCallback("onStart", () => {
  console.log("timeline start callback")
});
tween.eventCallback("onComplete", () => {
     console.log("tween end callback")
 });
parentTimeline.add(timeline, 0);

timeline.eventCallback("onComplete", () => {
  console.log("timeline end callback")
});
parentTimeline.eventCallback("onStart", () => {
  console.log("parentTimeline start callback")
});
parentTimeline.eventCallback("onComplete", () => {
  console.log("parentTimeline end callback")
});

 

See the Pen rNwvRRz by mukundkmr (@mukundkmr) on CodePen

Link to comment
Share on other sites

  • Solution

Welcome to forums @Mukund Kumar

 

Are you trying to do anything in particular? Having callbacks placed at 0 time is a tricky situation. Like onStart from the docs. The time never changes, so it never started because there is no animation. It just get sets.

Quote
onStart A function to call when the animation begins (when its time changes from 0 to some other value which can happen more than once if the tween is restarted multiple times).

 

Your onComplete won't work because the tween has already completed before you add it via eventCallback.

const tween = gsap.fromTo("h1", {x: 0}, {x: 300,  duration:0});

// !!! tween has completed

tween.eventCallback("onComplete", () => {
     console.log("tween end callback")
 });

 

That's why I would recommend putting your callbacks in the vars object like this.

const tween = gsap.fromTo("h1", {x: 0}, {
  x: 300,  
  duration:0,
  onComplete() {
    console.log("tween end callback")
  }
});

// tween end callback

 

0 duration tweens are the same as set, which is what you should use instead if you're just setting something.

https://greensock.com/docs/v3/GSAP/gsap.set()

https://greensock.com/docs/v3/GSAP/Timeline/set()

  • Like 2
  • Thanks 1
Link to comment
Share on other sites

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