Capítulo 16 de 411

Chapter 16: gsap.getById()

Core Idea

gsap.getById() retrieves a tween or timeline by the id you assigned it when creating it, useful in frameworks where tracking variables is awkward.

Key Concepts

  • Returns: the matching Tween/Timeline, or undefined if no active one has that ID.
  • Garbage collection caveat: GSAP releases completed animations for GC shortly after they finish, so getById() only finds animations that are still active or haven't started — it's not a permanent registry.

Code Examples

gsap.to(obj, { id: "myTween", duration: 1, x: 100 });

let tween = gsap.getById("myTween");
tween.pause();
  • What it demonstrates: recovering a reference to a tween by ID instead of keeping a variable around, and pausing it later.

Key Takeaways

  1. Only works for animations that are still active/pending — for a reference guaranteed to survive completion, store the tween in a variable instead.
  2. Handy in component-heavy codebases where passing tween references between functions/files is inconvenient.

Connects To

  • gsap.getTweensOf(): finds tweens by target instead of by explicit ID.