gsap-utils by greensock/gsap-skills
npx skills add https://github.com/greensock/gsap-skills --skill gsap-utils当编写或审查使用 gsap.utils 进行数学运算、数组/集合处理、单位解析或动画中值映射的代码时应用此技能(例如,将滚动位置映射到某个值、随机化、对齐到网格或标准化输入)。
相关技能: 在构建动画时与 gsap-core、gsap-timeline 和 gsap-scrolltrigger 一起使用;CustomEase 和其他缓动工具位于 gsap-plugins 中。
gsap.utils 提供纯辅助函数;无需注册。可在补间变量(例如基于函数的值)、ScrollTrigger 或 Observer 回调中,或任何驱动 GSAP 的 JS 代码中使用。所有函数都位于 gsap.utils 上(例如 gsap.utils.clamp())。
省略值:函数形式。 许多工具函数将待转换的值作为最后一个参数。如果省略该参数,该工具函数将返回一个函数,该函数稍后接受该值。当需要使用相同配置对多个值进行钳制、映射、标准化或对齐时(例如在 mousemove 处理程序或补间回调中),请使用函数形式。例外:random() —— 将 true 作为最后一个参数传递以获得可重用的函数(不要省略值);参见 random()。
// 带值:返回结果
gsap.utils.clamp(0, 100, 150); // 100
// 不带值:返回一个稍后可以调用并传入值的函数
let c = gsap.utils.clamp(0, 100);
c(150); // 100
c(-10); // 0
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
将值限制在最小值和最大值之间。省略 value 以获得一个函数: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
将值从一个范围映射到另一个范围。在将滚动位置、进度(0–1)或输入范围转换为动画范围时使用。省略 value 以获得一个函数: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 (进度转换为角度)
let mapFn = gsap.utils.mapRange(0, 100, 0, 500);
mapFn(50); // 250
返回给定范围内归一化到 0–1 的值。当目标范围是 0–1 时,是映射的逆运算。省略 value 以获得一个函数: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
在给定进度(0–1)下对两个值进行插值。处理数字、颜色和具有匹配键的对象。省略 progress 以获得一个函数:interpolate(start, end)(progress)。
gsap.utils.interpolate(0, 100, 0.5); // 50
gsap.utils.interpolate("#ff0000", "#0000ff", 0.5); // 中间颜色
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
返回 minimum –maximum 范围内的随机数,或 array 中的随机元素。可选的 snapIncrement 将结果对齐到最近的倍数(例如 5 → 5 的倍数)。要获得可重用的函数,请将 true 作为最后一个参数(returnFunction)传递;返回的函数不接受参数,每次调用都返回一个新的随机值。这是唯一一个使用 true 而不是省略值来获得函数形式的工具函数。
// 立即返回值:范围内的数字
gsap.utils.random(-100, 100); // 例如 42.7
gsap.utils.random(0, 500, 5); // 0–500,对齐到最近的 5
// 可重用函数:将 true 作为最后一个参数传递
let randomFn = gsap.utils.random(-200, 500, 10, true);
randomFn(); // 范围内的随机值,对齐到 10
randomFn(); // 另一个随机值
// 数组:随机选取一个值
gsap.utils.random(["red", "blue", "green"]); // "red"、"blue" 或 "green"
let randomFromArray = gsap.utils.random([0, 100, 200], true);
randomFromArray(); // 0、100 或 200
补间变量中的字符串形式: 使用 "random(-100, 100)"、"random(-100, 100, 5)" 或 "random([0, 100, 200])";GSAP 会为每个目标计算它。
gsap.to(".box", { x: "random(-100, 100, 5)", duration: 1 });
gsap.to(".item", { backgroundColor: "random([red, blue, green])" });
将值对齐到 snapTo 的最近倍数,或对齐到允许值数组中的最近值。省略 value 以获得一个函数:snap(snapTo)(value)(或 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 或 200(数组中的最近值)
let snapFn = gsap.utils.snap(10);
snapFn(23); // 20
在补间中用于网格或基于步长的动画:
gsap.to(".x", { x: 200, snap: { x: 20 } });
返回一个新数组,其中包含相同元素但顺序随机。用于随机化顺序(例如,使用副本进行"随机"交错)。
gsap.utils.shuffle([1, 2, 3, 4]); // 例如 [3, 1, 4, 2]
返回一个函数,该函数根据目标在数组(或网格)中的位置为其分配一个值。内部用于高级交错;每当需要将值分布到多个元素时(例如缩放、不透明度、x 坐标、延迟)使用它。返回的函数接收 (index, target, targets) —— 可以手动调用它,或者将结果直接传递给补间;GSAP 会为每个目标调用它,并传入索引、元素和数组。
配置(全部可选):
| 属性 | 类型 | 描述 |
|---|---|---|
base | 数字 | 起始值。默认 0。 |
amount | 数字 | 在所有目标之间分配的总量(加到 base 上)。例如,amount: 1 且 100 个目标 → 每个之间相差 0.01。改用 each 来设置每个目标的固定步长。 |
each | 数字 | 每个目标之间增加的量(加到 base 上)。例如,each: 1 且 4 个目标 → 0, 1, 2, 3。改用 amount 来分割总量。 |
from | 数字 | 字符串 |
grid | 字符串 | 数组 |
axis | 字符串 | 对于网格:限制在一个轴("x" 或 "y")。 |
ease | Ease | 沿缓动曲线分布值(例如 "power1.inOut")。默认 "none"。 |
在补间中: 将 distribute(config) 的结果作为属性值传递;GSAP 会为每个目标调用该函数,并传入 (index, target, targets)。
// 缩放:中间元素 0.5,外边缘 3(总量 2.5 从中心开始分布)
gsap.to(".class", {
scale: gsap.utils.distribute({
base: 0.5,
amount: 2.5,
from: "center"
})
});
手动使用: 使用 (index, target, targets) 调用返回的函数,以获取该索引的值。
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);
更多信息请参见 distribute()。
返回值的单位字符串(例如 "px"、"%"、"deg")。在标准化或转换值时使用。
gsap.utils.getUnit("100px"); // "px"
gsap.utils.getUnit("50%"); // "%"
gsap.utils.getUnit(42); // "" (无单位)
将单位附加到数字上,如果值已有单位则按原样返回。在构建 CSS 值或补间结束值时使用。
gsap.utils.unitize(100, "px"); // "100px"
gsap.utils.unitize("2rem", "px"); // "2rem" (不变)
将颜色字符串转换为数组:[red, green, blue] (0–255),或 [red, green, blue, alpha](当存在或需要 alpha 通道时为 4 个元素的 RGBA)。将 true 作为第二个参数(returnHSL)传递以获得 [hue, saturation, lightness] 或 [hue, saturation, lightness, alpha](HSL/HSLA)。适用于 "rgb()"、"rgba()"、"hsl()"、"hsla()"、十六进制和命名颜色(例如 "red")。在动画化颜色分量或构建渐变时使用。参见 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 个元素)
gsap.utils.splitColor("#6fb936", true); // [94, 55, 47] (HSL: 色调、饱和度、亮度)
返回一个作用域选择器函数,该函数仅在给定元素(或引用)内查找元素。在组件中使用,以便像 ".box" 这样的选择器仅匹配该组件的后代元素,而不是整个文档。接受 DOM 元素或引用(例如 React ref;处理 .current)。
const q = gsap.utils.selector(containerRef);
q(".box"); // 容器内的 .box 元素数组
gsap.to(q(".circle"), { x: 100 });
将值转换为数组:选择器字符串(作用域限定到元素)、NodeList、HTMLCollection、单个元素或数组。在将混合输入传递给 GSAP(例如目标)且需要真正的数组时使用。
gsap.utils.toArray(".item"); // 元素数组
gsap.utils.toArray(".item", container); // 作用域限定到容器
gsap.utils.toArray(nodeList); // 来自 NodeList 的 [ ... ]
组合函数:pipe(f1, f2, f3)(value) 返回 f3(f2(f1(value)))。在补间或回调中应用一系列转换时使用(例如 标准化 → 映射范围 → 对齐)。
const fn = gsap.utils.pipe(
(v) => gsap.utils.normalize(0, 100, v),
(v) => gsap.utils.snap(0.1, v)
);
fn(50); // 标准化然后对齐
将值环绕到 min–max 范围内(包含 min,不包含 max)。用于无限滚动或循环值。省略 value 以获得一个函数: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
以 yoyo 方式将值环绕在范围内(在两端反弹)。用于在范围内来回移动。省略 value 以获得一个函数:wrapYoyo(min, max)(value)。
gsap.utils.wrapYoyo(0, 100, 150); // 50 (反弹回来)
let wrapY = gsap.utils.wrapYoyo(0, 100);
wrapY(150); // 50
let mapFn = gsap.utils.mapRange(0, 1, 0, 360); mapFn(progress)。每周安装量
983
仓库
GitHub 星标数
1.5K
首次出现
5 天前
安全审计
安装于
codex974
opencode973
cursor972
kimi-cli971
gemini-cli971
github-copilot971
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.
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
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
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
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
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
Returns a random number in the range minimum –maximum , 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])" });
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 } });
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]
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. |
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.
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)
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)
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)
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 });
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
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
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
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
let mapFn = gsap.utils.mapRange(0, 1, 0, 360); mapFn(progress).https://gsap.com/docs/v3/HelperFunctions
Weekly Installs
983
Repository
GitHub Stars
1.5K
First Seen
5 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex974
opencode973
cursor972
kimi-cli971
gemini-cli971
github-copilot971
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装
Grimoire CLI 使用指南:区块链法术编写、验证与执行全流程
940 周安装
Grimoire Uniswap 技能:查询 Uniswap 元数据与生成代币/资金池快照的 CLI 工具
940 周安装
Grimoire Aave 技能:查询 Aave V3 元数据和储备快照的 CLI 工具
941 周安装
Railway CLI 部署指南:使用 railway up 命令快速部署代码到 Railway 平台
942 周安装
n8n Python 代码节点使用指南:在自动化工作流中编写 Python 脚本
943 周安装
Flutter Platform Views 实现指南:Android/iOS/macOS原生视图与Web嵌入教程
943 周安装
from | Number | String |
grid | String | 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". |