跳到正文
cccskills
免费在 GitHub 公开中文介绍

gsap-utils

帮助编写和审查使用GSAP工具函数的动画代码,完成数值映射、范围限制、随机变化和固定间隔吸附,并支持将滚动进度转换为动画变化及处理多个元素。

原文查看原文说明

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.

AI 评分90分/ 100查看安装方法

适用场景

  • 将滚动进度转换为位移或旋转
  • 将动画数值限制在指定范围内
  • 为位置或颜色添加随机变化
  • 让移动位置吸附到网格或固定步长
  • 选择组件内部的动画目标

中文介绍

功能

gsap-utils帮助编写和审查使用gsap.utils辅助函数的动画代码。包括用clamp将数值限制在指定范围内,用mapRange将数值映射到另一范围,以及用interpolate计算两个值之间的中间值。它还涵盖随机值生成、固定间隔吸附、用于重复效果的数值循环,以及将元素转换为数组。

适用场景

适合将滚动进度转换为移动距离或旋转角度,也适合为多个元素分配不同的值。还可以用来确认如何创建反复使用同一配置的函数,以及如何只选择某个组件内部的元素作为动画目标。

使用示例

  • “请将滚动进度转换为0到360度的旋转。”
  • “请让元素的移动位置按20的步长对齐。”
  • “请只选择这个组件内部的元素,并设置随机移动距离。”

注意事项

适用于使用GSAP的JavaScript代码,gsap.utils本身无需注册。mapRange和normalize处理数值,单位需要另外处理。获取可复用函数时,random的指定方式与其他函数不同。该技能标注的许可证为MIT。

本介绍由 AI(OpenAI Codex)根据公开的 SKILL.md 撰写。准确的规格请查看下方原文。

客观评估

AI 仅根据 SKILL.md 的内容,从 4 个方面进行了评估。星标数和热度不计入评估。

指令清晰度能否看出何时使用、做什么
A良好明确用于编写和审查gsap.utils代码,并列出范围映射等用途。
步骤具体性步骤、输入输出和示例是否具体
A良好通过代码示例说明clamp、mapRange等函数的参数、返回值和复用方式。
安全性发送数据、删除文件、执行命令等是否有说明
A良好主要说明数值和元素辅助函数,没有外部发送或删除指令。
安装难易度所需工具和 API 密钥是否少且有说明
B部分不足以使用GSAP为前提,说明辅助函数无需注册,但未提供安装步骤。

总分按每项 A=25 分、B=15 分、C=5 分相加。 由 AI(OpenAI Codex)于 2026年10月20日 评估。仅供参考,安装前请同时查看原文。

如果您认为评估有误,请申请复核

请写明哪一项有误以及原因。无需登录。我们会对照 SKILL.md 重新评估,并在此处更新结果。

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

包含的文件(3)

  • SKILL.md12.4 KB
  • LICENSE1.0 KB
  • SOURCES.md812 B

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

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

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):

PropertyTypeDescription
baseNumberStarting value. Default 0.
amountNumberTotal 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.
eachNumberAmount 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.
fromNumber | String | ArrayWhere distribution starts: index, or "start", "center", "edges", "random", "end", or ratios like [0.25, 0.75]. Default 0.
gridString | ArrayUse grid position instead of flat index: [rows, columns] (e.g. [5, 10]) or "auto" to detect. Omit for flat array.
axisStringFor grid: limit to one axis ("x" or "y").
easeEaseDistribute 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

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

8-bit-orbit-video-template

免费有简介AI 评分 90分

使用Hyperframes的HTML模板制作复古像素风格的三场景视频,替换标题、标签和配色,同时保留场景转换节奏及可重复的动画时序。

  • 制作像素风格的介绍视频
  • 替换三场景视频中的内容
  • 通过HTML预览场景转换
nexu-io/open-design10.3万2026年10月20日 更新

ad-creative

免费有简介AI 评分 80分

为社交媒体广告和搜索广告撰写标题、说明及正文,并反复调整措辞。可用于准备广告文案备选版本,以及检查现有文案并考虑修改方案。

  • 撰写社交媒体广告标题
  • 准备搜索广告说明
  • 修改广告正文措辞
nexu-io/open-design10.3万2026年10月20日 更新

after-hours-editorial-template

免费有简介AI 评分 80分

以深色背景和衬线字体制作时尚杂志风格的三页动态展示,结合文字逐步显现与章节转场,最终生成内嵌CSS和JavaScript的单个HTML文件。

  • 制作时尚杂志风格的动态介绍
  • 用三页展示简短故事
  • 将现有品牌风格融入深色版面
nexu-io/open-design10.3万2026年10月20日 更新

agent-browser

免费有简介AI 评分 100分

通过浏览器查看页面,执行点击、输入、截图和信息提取。主要用于检查OpenDesign的本地预览,以及核查网页应用的运行情况与质量。

  • 检查OpenDesign本地预览
  • 测试网页应用的点击和输入
  • 截取页面或元素图像
nexu-io/open-design10.3万2026年10月20日 更新

ai-music-album

免费有简介AI 评分 80分

用于AI音乐专辑制作,涵盖概念构思、歌词初稿、曲目排序和导出,适合独立专辑实验或品牌音乐企划;完整制作流程需另行安装上游资源包。

  • 策划独立制作的专辑
  • 围绕专辑主题起草歌词
  • 安排专辑曲目顺序
nexu-io/open-design10.3万2026年10月20日 更新

algorithmic-art

免费有简介AI 评分 80分

使用p5.js,根据计算规则与随机数创作生成艺术。固定随机种子即可重现相同画面,适合制作海报、表现动感的静态图像和艺术构图草案。

  • 根据规则制作海报
  • 创作具有动感的静态图像
  • 试验艺术作品的构图
nexu-io/open-design10.3万2026年10月20日 更新

查看 nexu-io 的全部技能

报告此技能的问题