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.clamp()).// 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
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).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).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).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
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
"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) (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
gsap.to(".x", { x: 200, snap: { x: 20 } });
gsap.utils.shuffle([1, 2, 3, 4]); // e.g. [3, 1, 4, 2]
(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.| 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". |
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"
})
});
(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);
"px", "%", "deg"). Use when normalizing or converting values.gsap.utils.getUnit("100px"); // "px"
gsap.utils.getUnit("50%"); // "%"
gsap.utils.getUnit(42); // "" (unitless)
gsap.utils.unitize(100, "px"); // "100px"
gsap.utils.unitize("2rem", "px"); // "2rem" (unchanged)
"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)
".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 });
gsap.utils.toArray(".item"); // array of elements
gsap.utils.toArray(".item", container); // scoped to container
gsap.utils.toArray(nodeList); // [ ... ] from NodeList
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).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).gsap.utils.wrapYoyo(0, 100, 150); // 50 (bounces back)
let wrapY = gsap.utils.wrapYoyo(0, 100);
wrapY(150); // 50
let mapFn = gsap.utils.mapRange(0, 1, 0, 360); mapFn(progress).