Capítulo 963 de 988

Chapter 963: Freeze portions of a sequence

Core Idea

With the following snippet, two portions of a sequence are frozen and resumed afterwards from the point they were paused.

Key Concepts

  • Freeze portions of a sequence: With the following snippet, two portions of a sequence are frozen and resumed afterwards from the point they were paused..

Code Examples

export const Counter: React.FC = () => {
  return (
    <div className="flex h-full justify-center items-center text-6xl">
      {useCurrentFrame()}
    </div>
  );
};

const FREEZES = [
  {
    frame: 0,
    durationInFrames: 25,
  },
  {
    frame: 30,
    durationInFrames: 50,
  },
];

const getFreezes = () => {
  let summedUpFreezes = 0;
  const freezeFrames = [];
  for (const freeze of FREEZES) {
    freezeFrames.push({
      start: summedUpFreezes + freeze.frame,
      durationInFrames: freeze.durationInFrames,
      from: summedUpFreezes,
      frame: freeze.frame,
    });
    summedUpFreezes += freeze.durationInFrames;
  }
  return freezeFrames;
};

export const FreezePortion: React.FC = () => {
  const freezes = useMemo(() => {
    return getFreezes();
  }, []);
  const frame = useCurrentFrame();

  const nextFreeze = freezes.find(
    (freeze) => frame < freeze.start + freeze.durationInFrames,
  );
  const activeFreeze = freezes.find(
    (freeze) =>
      frame >= freeze.start && frame < freeze.start + freeze.durationInFrames,
  );

  const from = useMemo(() => {
    if (activeFreeze) {
      return 0;
    }

    if (nextFreeze) {
      return nextFreeze.from;
    }

    return (
      freezes[freezes.length - 1].from +
      freezes[freezes.length - 1].durationInFrames
    );
  }, [activeFreeze, freezes, nextFreeze]);

  return (
    <Freeze
      frame={activeFreez
// ...(truncated)
  • What it demonstrates: Usage pattern for Freeze portions of a sequence from the official docs.

Key Takeaways

  1. Understand freeze portions of a sequence when working with Freeze portions of a sequence.

Connects To

  • 4 0 Alpha: related page in the Miscellaneous (Core) section.
  • ai: related page in the Miscellaneous (Core) section.
  • Animated Emoji: related page in the Miscellaneous (Core) section.