Chuyển đến nội dung
Skillpedia
Lập trình chung101k · #1186

gsap-utils

Skill này làm gì

Bộ công cụ này giúp thực hiện tính toán và sắp xếp dữ liệu cho hoạt ảnh. Nó cho phép giới hạn giá trị, ánh xạ giá trị này sang giá trị khác, chuẩn hóa số, pha trộn màu hoặc giá trị, tạo số ngẫu nhiên và sắp xếp các phần tử trong danh sách.

Khi nào nên dùng

  • ✓Khi bạn cần một giá trị nằm trong giới hạn nhất định.
  • ✓Khi chuyển vị trí cuộn thành chuyển động hoạt ảnh.
  • ✓Khi tạo giá trị ngẫu nhiên hoặc sắp xếp các phần tử theo thứ tự ngẫu nhiên.

Liên kết skill

https://github.com/nexu-io/open-design/blob/main/skills/gsap-utils/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/nexu-io/open-design/blob/main/skills/gsap-utils/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Làm sao để đảm bảo vị trí X của một đối tượng không vượt quá 100 pixel?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Các hàm này hoạt động như máy tính và công cụ sắp xếp cho hoạt ảnh. Chúng không cần đăng ký; bạn có thể dùng trực tiếp với `gsap.utils`. Một số hàm có thể trả về một hàm khác để tái sử dụng cùng cấu hình nhiều lần.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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

…

Câu hỏi thường gặp

“Bỏ qua giá trị” nghĩa là gì?

Nghĩa là bạn không cung cấp phần dữ liệu cuối cùng cho hàm. Thay vì nhận kết quả ngay, bạn nhận một hàm mới có thể gọi sau với dữ liệu còn thiếu. Điều này hữu ích để áp dụng cùng một quy tắc cho nhiều giá trị.

Khác nhau giữa `clamp` và `mapRange` là gì?

`clamp` chỉ đơn giản ngăn một giá trị vượt quá mức tối thiểu và tối đa. `mapRange` chuyển một giá trị từ thang đo này sang một thang đo hoàn toàn khác.

Làm sao để dùng `random` để chọn giữa các màu?

Bạn có thể truyền một mảng màu vào hàm `random`, ví dụ `gsap.utils.random(['red', 'blue', 'green'])`. Nó sẽ chọn một màu ngẫu nhiên mỗi lần được gọi.

Chi tiết

Mô tả gốc của tác giả

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

Độ phổ biến

Sao GitHub
101k
Vị trí xếp hạng
#1186
Giấy phép
Apache-2.0
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự