gsap-utils
它能做什么
这是一套用于动画计算和数据整理的工具集。它可以限制数值范围、把一个数值映射到另一个数值、对数字进行归一化、混合颜色或数值、生成随机数,以及把项目排列成列表。
适用场景
- ✓当你需要某个数值保持在特定范围内时。
- ✓当你要把滚动位置转换为动画中的移动距离时。
- ✓当你需要生成随机数值,或以随机顺序排列项目时。
技能链接
https://github.com/nexu-io/open-design/blob/main/skills/gsap-utils/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“怎样确保对象的 X 坐标不会超过 100 像素?”
每周 5 个新技能,1 分钟读完。
工作原理
这些函数相当于动画中的计算器和整理工具。它们无需注册,直接通过 `gsap.utils` 使用即可。有些函数还可以返回一个新函数,以便多次复用同一套配置。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
gsap.utils
Curated from GreenSock's official GSAP skills: https://github.com/greensock/gsap-skills
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 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
…
常见问题
“省略数值”是什么意思?
就是不提供函数所需的最后一项数据。这时你不会马上得到结果,而是得到一个新函数,之后再调用它并传入缺少的数据。这对于把同一规则应用到多个数值上很有用。
clamp 和 mapRange 有什么区别?
`clamp` 只是限制一个数值不超过最小值和最大值。`mapRange` 则会把一个数值从一个范围转换到一个完全不同的范围。
我怎样用 random 在颜色之间随机选择?
你可以把颜色数组传给 `random` 函数,比如 `gsap.utils.random(['red', 'blue', 'green'])`。每次调用时,它都会随机挑选一种颜色。
详情
作者的原始描述
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.
repo: nexu-io/open-design
热度
- GitHub 星标
- 101k
- 排名
- #1186
- 许可证
- Apache-2.0
- 更新时间
- 2026/10/15
评论
- …