gsap-utils

GitHub

提供GSAP工具方法,如clamp、mapRange、normalize等,用于动画中的数学计算、数值映射及数组处理。适用于编写或审查涉及值约束、范围转换和插值的代码。

skills/gsap/gsap-utils/SKILL.md nexu-io/motion-anything

Trigger Scenarios

用户询问 gsap.utils 相关功能 需要实现 clamp, mapRange, normalize, interpolate, random, snap, toArray, wrap, pipe 等辅助工具 在动画中处理数值约束、范围映射或数据归一化

Install

npx skills add nexu-io/motion-anything --skill gsap-utils -g -y
More Options

Non-standard path

npx skills add https://github.com/nexu-io/motion-anything/tree/main/skills/gsap/gsap-utils -g -y

Use without installing

npx skills use nexu-io/motion-anything@gsap-utils

指定 Agent (Claude Code)

npx skills add nexu-io/motion-anything --skill gsap-utils -a claude-code -g -y

安装 repo 全部 skill

npx skills add nexu-io/motion-anything --all -g -y

预览 repo 内 skill

npx skills add nexu-io/motion-anything --list

SKILL.md

Frontmatter
{
    "name": "gsap-utils",
    "license": "MIT",
    "description": "Official GSAP skill for gsap.utils — clamp, mapRange, normalize, interpolate, random, snap, toArray, wrap, pipe. Use when the user asks about gsap.utils, clamp, mapRange, random, snap, toArray, wrap, or helper utilities in GSAP."
}

gsap.utils

When to Use This Skill

Apply when writing or reviewing code that uses gsap.utils for math, array/collection handling, unit parsing, or value mapping in animations (e.g. mapping scroll to a value, randomizing, snapping to a grid, or normalizing inputs).

Related skills: Use with gsap-core, gsap-timeline, and gsap-scrolltrigger when building animations; CustomEase and other easing utilities are in gsap-plugins.

Overview

gsap.utils provides pure helpers; no need to register. Use in tween vars (e.g. function-based values), in ScrollTrigger or Observer callbacks, or in any JS that drives GSAP. All are on gsap.utils (e.g. gsap.utils.clamp()).

Omitting the value: function form. Many utils accept the value to transform as the last argument. If you omit that argument, the util returns a function that accepts the value later. Use the function form when you need to clamp, map, normalize, or snap many values with the same config (e.g. in a mousemove handler or tween callback). Exception: random() — pass true as the last argument to get a reusable function (do not omit the value); see random().

// With value: returns the result
gsap.utils.clamp(0, 100, 150); // 100

// Without value: returns a function you call with the value later
let c = gsap.utils.clamp(0, 100);
c(150);  // 100
c(-10);  // 0

Clamping and Ranges

clamp(min, max, value?)

Constrains a value between min and max. Omit value to get a function: clamp(min, max)(value).

gsap.utils.clamp(0, 100, 150); // 100
gsap.utils.clamp(0, 100, -10); // 0

let clampFn = gsap.utils.clamp(0, 100);
clampFn(150); // 100

mapRange(inMin, inMax, outMin, outMax, value?)

Maps a value from one range to another. Use when converting scroll position, progress (0–1), or input range to an animation range. Omit value to get a function: mapRange(inMin, inMax, outMin, outMax)(value).

gsap.utils.mapRange(0, 100, 0, 500, 50);  // 250
gsap.utils.mapRange(0, 1, 0, 360, 0.5);   // 180 (progress to degrees)

let mapFn = gsap.utils.mapRange(0, 100, 0, 500);
mapFn(50);  // 250

normalize(min, max, value?)

Returns a value normalized to 0–1 for the given range. Inverse of mapping when the target range is 0–1. Omit value to get a function: normalize(min, max)(value).

gsap.utils.normalize(0, 100, 50);   // 0.5
gsap.utils.normalize(100, 300, 200); // 0.5

let normFn = gsap.utils.normalize(0, 100);
normFn(50); // 0.5

interpolate(start, end, progress?)

Interpolates between two values at a given progress (0–1). Handles numbers, colors, and objects with matching keys. Omit progress to get a function: interpolate(start, end)(progress).

gsap.utils.interpolate(0, 100, 0.5);       // 50
gsap.utils.interpolate("#ff0000", "#0000ff", 0.5); // mid color
gsap.utils.interpolate({ x: 0, y: 0 }, { x: 100, y: 50 }, 0.5); // { x: 50, y: 25 }

let lerp = gsap.utils.interpolate(0, 100);
lerp(0.5); // 50

Random and Snap

random(minimum, maximum[, snapIncrement, returnFunction]) / random(array[, returnFunction])

Returns a random number in the range minimummaximum, or a random element from an array. Optional snapIncrement snaps the result to the nearest multiple (e.g. 5 → multiples of 5). To get a reusable function, pass true as the last argument (returnFunction); the returned function takes no args and returns a new random value each time. This is the only util that uses true for the function form instead of omitting the value.

// immediate value: number in range
gsap.utils.random(-100, 100);        // e.g. 42.7
gsap.utils.random(0, 500, 5);        // 0–500, snapped to nearest 5

// reusable function: pass true as last argument
let randomFn = gsap.utils.random(-200, 500, 10, true);
randomFn();  // random value in range, snapped to 10
randomFn();  // another random value

// array: pick one value at random
gsap.utils.random(["red", "blue", "green"]);  // "red", "blue", or "green"
let randomFromArray = gsap.utils.random([0, 100, 200], true);
randomFromArray();  // 0, 100, or 200

String form in tween vars: use "random(-100, 100)", "random(-100, 100, 5)", or "random([0, 100, 200])"; GSAP evaluates it per target.

gsap.to(".box", { x: "random(-100, 100, 5)", duration: 1 });
gsap.to(".item", { backgroundColor: "random([red, blue, green])" });

snap(snapTo, value?)

Snaps a value to the nearest multiple of snapTo, or to the nearest value in an array of allowed values. Omit value to get a function: snap(snapTo)(value) (or snap(snapArray)(value)).

gsap.utils.snap(10, 23);     // 20
gsap.utils.snap(0.25, 0.7);  // 0.75
gsap.utils.snap([0, 100, 200], 150); // 100 or 200 (nearest in array)

let snapFn = gsap.utils.snap(10);
snapFn(23); // 20

Use in tweens for grid or step-based animation:

gsap.to(".x", { x: 200, snap: { x: 20 } });

shuffle(array)

Returns a new array with the same elements in random order. Use for randomizing order (e.g. stagger from "random" with a copy).

gsap.utils.shuffle([1, 2, 3, 4]); // e.g. [3, 1, 4, 2]

distribute(config)

Returns a function that assigns a value to each target based on its position in the array (or in a grid). Used internally for advanced staggers; use it whenever you need values spread across many elements (e.g. scale, opacity, x, delay). The returned function receives (index, target, targets) — either call it manually or pass the result directly into a tween; GSAP will call it per target with index, element, and array.

Config (all optional):

Property Type Description
base Number Starting value. Default 0.
amount Number Total to distribute across all targets (added to base). E.g. amount: 1 with 100 targets → 0.01 between each. Use each instead to set a fixed step per target.
each Number Amount to add between each target (added to base). E.g. each: 1 with 4 targets → 0, 1, 2, 3. Use amount instead to split a total.
from Number | String | Array Where distribution starts: index, or "start", "center", "edges", "random", "end", or ratios like [0.25, 0.75]. Default 0.
grid String | Array Use grid position instead of flat index: [rows, columns] (e.g. [5, 10]) or "auto" to detect. Omit for flat array.
axis String For grid: limit to one axis ("x" or "y").
ease Ease Distribute values along an ease curve (e.g. "power1.inOut"). Default "none".

In a tween: pass the result of distribute(config) as the property value; GSAP calls the function for each target with (index, target, targets).

// Scale: middle elements 0.5, outer edges 3 (amount 2.5 distributed from center)
gsap.to(".class", {
  scale: gsap.utils.distribute({
    base: 0.5,
    amount: 2.5,
    from: "center"
  })
});

Manual use: call the returned function with (index, target, targets) to get the value for that index.

const distributor = gsap.utils.distribute({
  base: 50,
  amount: 100,
  from: "center",
  ease: "power1.inOut"
});
const targets = gsap.utils.toArray(".box");
const valueForIndex2 = distributor(2, targets[2], targets);

See distribute() for more.

Units and Parsing

getUnit(value)

Returns the unit string of a value (e.g. "px", "%", "deg"). Use when normalizing or converting values.

gsap.utils.getUnit("100px");   // "px"
gsap.utils.getUnit("50%");     // "%"
gsap.utils.getUnit(42);        // "" (unitless)

unitize(value, unit)

Appends a unit to a number, or returns the value as-is if it already has a unit. Use when building CSS values or tween end values.

gsap.utils.unitize(100, "px");  // "100px"
gsap.utils.unitize("2rem", "px"); // "2rem" (unchanged)

splitColor(color, returnHSL?)

Converts a color string into an array: [red, green, blue] (0–255), or [red, green, blue, alpha] (4 elements for RGBA when alpha is present or required). Pass true as the second argument (returnHSL) to get [hue, saturation, lightness] or [hue, saturation, lightness, alpha] (HSL/HSLA) instead. Works with "rgb()", "rgba()", "hsl()", "hsla()", hex, and named colors (e.g. "red"). Use when animating color components or building gradients. See splitColor().

gsap.utils.splitColor("red");                    // [255, 0, 0]
gsap.utils.splitColor("#6fb936");                // [111, 185, 54]
gsap.utils.splitColor("rgba(204, 153, 51, 0.5)"); // [204, 153, 51, 0.5] (4 elements)
gsap.utils.splitColor("#6fb936", true);          // [94, 55, 47] (HSL: hue, saturation, lightness)

Arrays and Collections

selector(scope)

Returns a scoped selector function that finds elements only within the given element (or ref). Use in components so selectors like ".box" match only descendants of that component, not the whole document. Accepts a DOM element or a ref (e.g. React ref; handles .current).

const q = gsap.utils.selector(containerRef);
q(".box");        // array of .box elements inside container
gsap.to(q(".circle"), { x: 100 });

toArray(value, scope?)

Converts a value to an array: selector string (scoped to element), NodeList, HTMLCollection, single element, or array. Use when passing mixed inputs to GSAP (e.g. targets) and a true array is needed.

gsap.utils.toArray(".item");           // array of elements
gsap.utils.toArray(".item", container); // scoped to container
gsap.utils.toArray(nodeList);          // [ ... ] from NodeList

pipe(...functions)

Composes functions: pipe(f1, f2, f3)(value) returns f3(f2(f1(value))). Use when applying a chain of transforms (e.g. normalize → mapRange → snap) in a tween or callback.

const fn = gsap.utils.pipe(
  (v) => gsap.utils.normalize(0, 100, v),
  (v) => gsap.utils.snap(0.1, v)
);
fn(50); // normalized then snapped

wrap(min, max, value?)

Wraps a value into the range min–max (inclusive min, exclusive max). Use for infinite scroll or cyclic values. Omit value to get a function: wrap(min, max)(value).

gsap.utils.wrap(0, 360, 370);  // 10
gsap.utils.wrap(0, 360, -10);   // 350

let wrapFn = gsap.utils.wrap(0, 360);
wrapFn(370); // 10

wrapYoyo(min, max, value?)

Wraps value in range with a yoyo (bounces at ends). Use for back-and-forth within a range. Omit value to get a function: wrapYoyo(min, max)(value).

gsap.utils.wrapYoyo(0, 100, 150); // 50 (bounces back)

let wrapY = gsap.utils.wrapYoyo(0, 100);
wrapY(150); // 50

Best practices

  • ✅ Omit the value argument to get a reusable function when the same range/config is used many times (e.g. scroll handler, tween callback): let mapFn = gsap.utils.mapRange(0, 1, 0, 360); mapFn(progress).
  • ✅ Use snap for grid-aligned or step-based values; use toArray when GSAP or your code needs a real array from a selector or NodeList.
  • ✅ Use gsap.utils.selector(scope) in components so selectors are scoped to a container or ref.

Do Not

  • ❌ Assume mapRange / normalize handle units; they work on numbers. Use getUnit / unitize when units matter.
  • ❌ Override or rely on undocumented behavior; stick to the documented API.

Learn More

https://gsap.com/docs/v3/HelperFunctions

Version History

  • b016900 Current 2026-07-11 17:11

Same Skill Collection

recipes/animxyz/xyz-fade-big/SKILL.md
recipes/animxyz/xyz-fade-down/SKILL.md
recipes/animxyz/xyz-fade-left/SKILL.md
recipes/animxyz/xyz-fade-right/SKILL.md
recipes/animxyz/xyz-fade-small/SKILL.md
recipes/animxyz/xyz-fade-up/SKILL.md
recipes/animxyz/xyz-flip-left/SKILL.md
recipes/animxyz/xyz-flip-up/SKILL.md
recipes/animxyz/xyz-rise-big/SKILL.md
recipes/animxyz/xyz-rotate/SKILL.md
recipes/app/bottom-sheet/SKILL.md
recipes/app/button-press/SKILL.md
recipes/app/checkbox-pop/SKILL.md
recipes/app/ripple/SKILL.md
recipes/app/tab-bar-slide/SKILL.md
recipes/app/toast-pop/SKILL.md
recipes/css/anim-backindown/SKILL.md
recipes/css/anim-backinleft/SKILL.md
recipes/css/anim-backinright/SKILL.md
recipes/css/anim-backinup/SKILL.md
recipes/css/anim-backoutdown/SKILL.md
recipes/css/anim-backoutleft/SKILL.md
recipes/css/anim-backoutright/SKILL.md
recipes/css/anim-backoutup/SKILL.md
recipes/css/anim-bounce/SKILL.md
recipes/css/anim-bouncein/SKILL.md
recipes/css/anim-bounceindown/SKILL.md
recipes/css/anim-bounceinleft/SKILL.md
recipes/css/anim-bounceinright/SKILL.md
recipes/css/anim-bounceinup/SKILL.md
recipes/css/anim-bounceout/SKILL.md
recipes/css/anim-bounceoutdown/SKILL.md
recipes/css/anim-bounceoutleft/SKILL.md
recipes/css/anim-bounceoutright/SKILL.md
recipes/css/anim-bounceoutup/SKILL.md
recipes/css/anim-fadein/SKILL.md
recipes/css/anim-fadeinbottomleft/SKILL.md
recipes/css/anim-fadeinbottomright/SKILL.md
recipes/css/anim-fadeindown/SKILL.md
recipes/css/anim-fadeindownbig/SKILL.md
recipes/css/anim-fadeinleft/SKILL.md
recipes/css/anim-fadeinleftbig/SKILL.md
recipes/css/anim-fadeinright/SKILL.md
recipes/css/anim-fadeinrightbig/SKILL.md
recipes/css/anim-fadeintopleft/SKILL.md
recipes/css/anim-fadeintopright/SKILL.md
recipes/css/anim-fadeinup/SKILL.md
recipes/css/anim-fadeinupbig/SKILL.md
recipes/css/anim-fadeout/SKILL.md
recipes/css/anim-fadeoutbottomleft/SKILL.md
recipes/css/anim-fadeoutbottomright/SKILL.md
recipes/css/anim-fadeoutdown/SKILL.md
recipes/css/anim-fadeoutdownbig/SKILL.md
recipes/css/anim-fadeoutleft/SKILL.md
recipes/css/anim-fadeoutleftbig/SKILL.md
recipes/css/anim-fadeoutright/SKILL.md
recipes/css/anim-fadeoutrightbig/SKILL.md
recipes/css/anim-fadeouttopleft/SKILL.md
recipes/css/anim-fadeouttopright/SKILL.md
recipes/css/anim-fadeoutup/SKILL.md
recipes/css/anim-fadeoutupbig/SKILL.md
recipes/css/anim-flash/SKILL.md
recipes/css/anim-flipinx/SKILL.md
recipes/css/anim-flipiny/SKILL.md
recipes/css/anim-headshake/SKILL.md
recipes/css/anim-heartbeat/SKILL.md
recipes/css/anim-hinge/SKILL.md
recipes/css/anim-jackinthebox/SKILL.md
recipes/css/anim-jello/SKILL.md
recipes/css/anim-lightspeedinleft/SKILL.md
recipes/css/anim-lightspeedinright/SKILL.md
recipes/css/anim-pulse/SKILL.md
recipes/css/anim-rollin/SKILL.md
recipes/css/anim-rollout/SKILL.md
recipes/css/anim-rotatein/SKILL.md
recipes/css/anim-rotateindownleft/SKILL.md
recipes/css/anim-rotateindownright/SKILL.md
recipes/css/anim-rotateinupleft/SKILL.md
recipes/css/anim-rotateinupright/SKILL.md
recipes/css/anim-rotateout/SKILL.md
recipes/css/anim-rotateoutdownleft/SKILL.md
recipes/css/anim-rotateoutdownright/SKILL.md
recipes/css/anim-rotateoutupleft/SKILL.md
recipes/css/anim-rotateoutupright/SKILL.md
recipes/css/anim-rubberband/SKILL.md
recipes/css/anim-shake/SKILL.md
recipes/css/anim-shakex/SKILL.md
recipes/css/anim-shakey/SKILL.md
recipes/css/anim-slideindown/SKILL.md
recipes/css/anim-slideinleft/SKILL.md
recipes/css/anim-slideinright/SKILL.md
recipes/css/anim-slideinup/SKILL.md
recipes/css/anim-slideoutdown/SKILL.md
recipes/css/anim-slideoutleft/SKILL.md
recipes/css/anim-slideoutright/SKILL.md
recipes/css/anim-slideoutup/SKILL.md
recipes/css/anim-swing/SKILL.md
recipes/css/anim-tada/SKILL.md
recipes/css/anim-wobble/SKILL.md
recipes/css/anim-zoomin/SKILL.md
recipes/css/anim-zoomindown/SKILL.md
recipes/css/anim-zoominleft/SKILL.md
recipes/css/anim-zoominright/SKILL.md
recipes/css/anim-zoominup/SKILL.md
recipes/css/anim-zoomout/SKILL.md
recipes/css/anim-zoomoutdown/SKILL.md
recipes/css/anim-zoomoutleft/SKILL.md
recipes/css/anim-zoomoutright/SKILL.md
recipes/css/anim-zoomoutup/SKILL.md
recipes/lottie/lottie-fab/SKILL.md
recipes/lottie/lottie-favorite/SKILL.md
recipes/lottie/lottie-pagination/SKILL.md
recipes/lottie/lottie-tab/SKILL.md
recipes/slides/fx-chain-react/SKILL.md
recipes/slides/fx-confetti/SKILL.md
recipes/slides/fx-constellation/SKILL.md
recipes/slides/fx-counter-explosion/SKILL.md
recipes/slides/fx-data-stream/SKILL.md
recipes/slides/fx-firework/SKILL.md
recipes/slides/fx-galaxy-swirl/SKILL.md
recipes/slides/fx-gradient-blob/SKILL.md
recipes/slides/fx-knowledge-graph/SKILL.md
recipes/slides/fx-letter-explode/SKILL.md
recipes/slides/fx-magnetic-field/SKILL.md
recipes/slides/fx-matrix-rain/SKILL.md
recipes/slides/fx-neural-net/SKILL.md
recipes/slides/fx-orbit-ring/SKILL.md
recipes/slides/fx-particle-burst/SKILL.md
recipes/slides/fx-shockwave/SKILL.md
recipes/slides/fx-sparkle-trail/SKILL.md
recipes/slides/fx-starfield/SKILL.md
recipes/slides/fx-typewriter-multi/SKILL.md
recipes/slides/fx-word-cascade/SKILL.md
recipes/web/attention-pulse/SKILL.md
recipes/web/aurora/SKILL.md
recipes/web/balatro/SKILL.md
recipes/web/border-beam/SKILL.md
recipes/web/card-lift-hover/SKILL.md
recipes/web/count-up/SKILL.md
recipes/web/dark-veil/SKILL.md
recipes/web/decrypted-text/SKILL.md
recipes/web/dither/SKILL.md
recipes/web/dot-field/SKILL.md
recipes/web/dot-grid/SKILL.md
recipes/web/elastic-slider/SKILL.md
recipes/web/fade-in-up/SKILL.md
recipes/web/faulty-terminal/SKILL.md
recipes/web/ferrofluid/SKILL.md
recipes/web/galaxy/SKILL.md
recipes/web/glare-hover/SKILL.md
recipes/web/glitch-text/SKILL.md
recipes/web/gradient-blinds/SKILL.md
recipes/web/gradient-text/SKILL.md
recipes/web/grainient/SKILL.md
recipes/web/iridescence/SKILL.md
recipes/web/kinetic-headline/SKILL.md
recipes/web/light-rays/SKILL.md
recipes/web/lightfall/SKILL.md
recipes/web/lightning/SKILL.md
recipes/web/like-burst/SKILL.md
recipes/web/line-waves/SKILL.md
recipes/web/liquid-chrome/SKILL.md
recipes/web/loading-shimmer/SKILL.md
recipes/web/magnetic-button/SKILL.md
recipes/web/particles/SKILL.md
recipes/web/pixel-blast/SKILL.md
recipes/web/pixel-snow/SKILL.md
recipes/web/pixel-transition/SKILL.md
recipes/web/plasma-wave/SKILL.md
recipes/web/plasma/SKILL.md
recipes/web/prismatic-burst/SKILL.md
recipes/web/ripple-grid/SKILL.md
recipes/web/ripple-press/SKILL.md
recipes/web/scroll-reveal/SKILL.md
recipes/web/shimmer-button/SKILL.md
recipes/web/shine-border/SKILL.md
recipes/web/shiny-text/SKILL.md
recipes/web/shuffle-text/SKILL.md
recipes/web/side-rays/SKILL.md
recipes/web/soft-aurora/SKILL.md
recipes/web/splash-cursor/SKILL.md
recipes/web/spotlight-card/SKILL.md
recipes/web/stagger-list/SKILL.md
recipes/web/star-border/SKILL.md
recipes/web/stepper/SKILL.md
recipes/web/strands/SKILL.md
recipes/web/text-scramble/SKILL.md
recipes/web/threads/SKILL.md
recipes/web/tilt-3d/SKILL.md
recipes/web/toggle-spring/SKILL.md
skills/gsap/gsap-frameworks/SKILL.md
skills/gsap/gsap-performance/SKILL.md
skills/gsap/gsap-plugins/SKILL.md
skills/gsap/gsap-react/SKILL.md
skills/gsap/gsap-scrolltrigger/SKILL.md
skills/gsap/gsap-timeline/SKILL.md
skills/web-clone/SKILL.md
skills/gsap/gsap-core/SKILL.md
skills/web-shader-extractor/SKILL.md
skills/web-to-design-md/SKILL.md
recipes/web/blob-cursor/SKILL.md
recipes/web/blur-text/SKILL.md
recipes/web/bounce-cards/SKILL.md
recipes/web/circular-text/SKILL.md
recipes/web/click-spark/SKILL.md
recipes/web/counter/SKILL.md
recipes/web/crosshair/SKILL.md
recipes/web/dock/SKILL.md
recipes/web/electric-border/SKILL.md
recipes/web/fade-content/SKILL.md
recipes/web/falling-text/SKILL.md
recipes/web/glass-icons/SKILL.md
recipes/web/gooey-nav/SKILL.md
recipes/web/gradual-blur/SKILL.md
recipes/web/image-trail/SKILL.md
recipes/web/magnet-lines/SKILL.md
recipes/web/noise/SKILL.md
recipes/web/orb/SKILL.md
recipes/web/pixel-card/SKILL.md
recipes/web/prism/SKILL.md
recipes/web/radar/SKILL.md
recipes/web/rotating-text/SKILL.md
recipes/web/scroll-float/SKILL.md
recipes/web/silk/SKILL.md
recipes/web/target-cursor/SKILL.md
recipes/web/true-focus/SKILL.md
recipes/web/waves/SKILL.md
skills/motion-anything/SKILL.md

Metadata

Files
0
Version
b016900
Hash
1927bcc4
Indexed
2026-07-11 17:11

inicio - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-07-14 15:12
浙ICP备14020137号-1 $mapa de visitantes$