| name | gsap-utilities |
| description | Use when using GSAP utility functions, math helpers, data transformations, or when working with clamp, mapRange, random, and other GSAP utilities. |
GSAP Utilities
GSAP provides powerful utility functions for data manipulation, math operations, and common animation tasks. These utilities streamline complex logic and improve code readability.
Core Utilities
clamp
import { gsap } from 'gsap'
const clamped = gsap.utils.clamp(0, 100, 150)
const clamped2 = gsap.utils.clamp(0, 100, 50)
const clamp50to100 = gsap.utils.clamp(50, 100)
console.log(clamp50to100(150))
console.log(clamp50to100(25))
mapRange
const mapped = gsap.utils.mapRange(0, 100, 0, 500, 50)
const mapped2 = gsap.utils.mapRange(0, 100, 0, window.innerWidth, 50)
const mapped3 = gsap.utils.mapRange(0, 100, 0, 1, 50)
normalize
const normalized = gsap.utils.normalize(100, 200, 150)
const normalized2 = gsap.utils.normalize(0, 100, 75)
gsap.to('.box', {
x: gsap.utils.mapRange(0, window.innerWidth, 0, 500, scrollProgress)
})
Array Utilities
toArray
const nodeList = document.querySelectorAll('.box')
const array = gsap.utils.toArray(nodeList)
function sum() {
const args = gsap.utils.toArray(arguments)
return args.reduce((a, b) => a + b, 0)
}
random
const num = gsap.utils.random(0, 100)
const stepped = gsap.utils.random(0, 100, 10)
const items = ['red', 'blue', 'green']
const color = gsap.utils.random(items)
gsap.to('.particle', {
x: gsap.utils.random(-200, 200),
y: gsap.utils.random(-200, 200),
backgroundColor: gsap.utils.random(['#ff0000', '#00ff00', '#0000ff']),
duration: 1
})
wrap
const wrapped = gsap.utils.wrap(0, 10, 12)
const wrapped2 = gsap.utils.wrap(0, 10, -1)
const items = ['a', 'b', 'c', 'd']
const item = gsap.utils.wrap(items)
console.log(item(5))
gsap.to('.carousel', {
xPercent: gsap.utils.wrap(0, 100, index * 25),
duration: 0.5
})
wrapYoyo
const yoyo = gsap.utils.wrapYoyo(0, 10)
console.log(yoyo(0))
console.log(yoyo(5))
console.log(yoyo(10))
console.log(yoyo(15))
console.log(yoyo(20))
console.log(yoyo(25))
Transformation Utilities
distribute
const items = document.querySelectorAll('.item')
gsap.to(items, {
x: gsap.utils.distribute(500, items)
duration: 1
})
const grid = gsap.utils.distribute(500, 20, 'columns', 5)
const custom = gsap.utils.distribute(500, items, (i, total) => {
return i * (500 / total)
})
interpolate
const interpolated = gsap.utils.interpolate(0, 100, 0.5)
const color = gsap.utils.interpolate('#ff0000', '#0000ff', 0.5)
const color2 = gsap.utils.interpolate('red', 'blue', 0.7)
const obj = gsap.utils.interpolate(
{ x: 0, y: 0 },
{ x: 100, y: 100 },
0.5
)
const interpolator = gsap.utils.interpolate(0, 100)
console.log(interpolator(0.25))
console.log(interpolator(0.75))
pipe
const process = gsap.utils.pipe(
gsap.utils.clamp(0, 100),
gsap.utils.mapRange(0, 100, 0, 500)
)
const result = process(75)
console.log(result)
const complexPipe = gsap.utils.pipe(
gsap.utils.clamp(0, 100),
(val) => val * 2,
gsap.utils.mapRange(0, 200, 0, 1)
)
const result2 = complexPipe(50)
Unit Utilities
getUnit
const unit = gsap.utils.getUnit('100px')
const unit2 = gsap.utils.getUnit('2.5s')
const unit3 = gsap.utils.getUnit('50%')
const unit4 = gsap.utils.getUnit('rotate(90deg)')
unitize
const withUnit = gsap.utils.unitize(100, 'px')
const withUnit2 = gsap.utils.unitize(50, '%')
const toPx = gsap.utils.unitize('px')
console.log(toPx(100))
gsap.to('.box', {
x: gsap.utils.unitize(scrollY * 2, 'px'),
duration: 0.1
})
removeUnit
const value = gsap.utils.removeUnit('100px')
const value2 = gsap.utils.removeUnit('2.5s')
const value3 = gsap.utils.removeUnit('50%')
Color Utilities
splitColor
const color = gsap.utils.splitColor('red')
const color2 = gsap.utils.splitColor('#ff0000')
const color3 = gsap.utils.splitColor('rgba(255, 0, 0, 0.5)')
checkPrefix
const prefix = gsap.utils.checkPrefix('.box', 'transform')
const prefix2 = gsap.utils.checkPrefix('.box', 'filter')
Selector Utilities
selector
const q = gsap.utils.selector('.container')
const elements = q('.box')
gsap.to(q('.box'), {
x: 100,
duration: 1
})
Math Utilities
snap
const snapped = gsap.utils.snap(20, 47)
const snapped2 = gsap.utils.snap(20, 53)
const snapped3 = gsap.utils.snap([0, 50, 100], 45)
const snapped4 = gsap.utils.snap([0, 50, 100], 30)
const snapped5 = gsap.utils.snap([0, 50, 100], 45, 10)
const snapped6 = gsap.utils.snap([0, 50, 100], 30, 5)
shuffle
const array = [1, 2, 3, 4, 5]
const shuffled = gsap.utils.shuffle(array)
const elements = gsap.utils.toArray('.item')
const shuffledElements = gsap.utils.shuffle(elements)
Practical Examples
Progress-Based Animation
gsap.to('.box', {
x: gsap.utils.mapRange(0, 1, 0, 500, scrollProgress),
opacity: gsap.utils.interpolate(1, 0, scrollProgress),
duration: 1
})
Parallax with Clamp
const parallaxX = gsap.utils.clamp(-50, 50, mouseX - centerX)
gsap.to('.parallax-element', {
x: parallaxX,
duration: 0.5
})
Circular Navigation
let currentIndex = 0
const total = 5
function nextSlide() {
currentIndex = gsap.utils.wrap(0, total, currentIndex + 1)
goToSlide(currentIndex)
}
function prevSlide() {
currentIndex = gsap.utils.wrap(0, total, currentIndex - 1)
goToSlide(currentIndex)
}
Distributed Stagger
const items = document.querySelectorAll('.item')
gsap.to(items, {
x: gsap.utils.distribute(500, items),
duration: 1,
ease: 'power2.out'
})
Color Interpolation
const color = gsap.utils.interpolate('#ff0000', '#0000ff', progress)
gsap.to('.box', {
backgroundColor: color,
duration: 1
})
Value Mapping
const rotation = gsap.utils.mapRange(
0, window.innerWidth,
-30, 30,
mouseX
)
gsap.to('.box', {
rotation: rotation,
duration: 0.5
})
Responsive Values
const scale = gsap.utils.mapRange(
320, 1920,
0.8, 1.2,
window.innerWidth
)
gsap.to('.box', {
scale: scale,
duration: 0.5
})
Utility in Animations
Using in Tween
gsap.to('.box', {
x: gsap.utils.random(-100, 100),
y: gsap.utils.random(-100, 100),
rotation: gsap.utils.random(0, 360),
backgroundColor: gsap.utils.interpolate('red', 'blue', Math.random()),
duration: 1
})
Using in ScrollTrigger
ScrollTrigger.create({
trigger: '.section',
start: 'top center',
end: 'bottom center',
onUpdate: (self) => {
const progress = gsap.utils.normalize(0, 1, self.progress)
const x = gsap.utils.mapRange(0, 1, 0, 500, progress)
gsap.set('.box', { x })
}
})
Using in Stagger
const items = document.querySelectorAll('.item')
gsap.from(items, {
opacity: 0,
x: gsap.utils.distribute(300, items),
stagger: gsap.utils.pipe(
(i, total) => i / total,
(progress) => progress * 0.5
),
duration: 1
})
Performance Tips
Cache Utilities
const clamp100 = gsap.utils.clamp(0, 100)
const mapRange100to500 = gsap.utils.mapRange(0, 100, 0, 500)
gsap.to('.box', {
x: mapRange100to500(clamp100(value)),
duration: 0.5
})
Batch Operations
const values = [10, 20, 30, 40, 50]
const processed = values.map(val =>
gsap.utils.pipe(
gsap.utils.clamp(0, 100),
(v) => v * 2
)(val)
)
Common Mistakes
1. Not Clamping Values
gsap.to('.box', {
x: gsap.utils.mapRange(0, 100, 0, 500, value)
})
gsap.to('.box', {
x: gsap.utils.mapRange(0, 100, 0, 500, gsap.utils.clamp(0, 100, value)),
duration: 0.5
})
2. Random In Loops
gsap.to('.box', {
x: gsap.utils.random(0, 100),
duration: 1
})
const randomX = gsap.utils.random(0, 100)
gsap.to('.box', {
x: randomX,
duration: 1
})
3. Not Using Pipes
const result = gsap.utils.mapRange(0, 100, 0, 500, gsap.utils.clamp(0, 50, value))
const process = gsap.utils.pipe(
gsap.utils.clamp(0, 50),
gsap.utils.mapRange(0, 50, 0, 500)
)
const result = process(value)
Best Practices
- Use clamp for validation - Ensure values stay in range
- Leverage pipe for chaining - Complex transformations become clear
- Cache utility functions - Better performance if reused
- Use mapRange for responsive - Map viewport to properties
- Distribute for staggers - Even spacing across elements
- Interpolate for colors - Smooth color transitions
- Random for variety - Add randomness to animations
- Normalize for progress - Convert to 0-1 range
Quick Reference
| Utility | Description |
|---|
clamp(min, max, value) | Clamp value between min and max |
mapRange(inMin, inMax, outMin, outMax, value) | Map value to new range |
normalize(min, max, value) | Convert to 0-1 range |
toArray(value) | Convert to array |
random(min, max, increment) | Random number or array item |
wrap(min, max, value) | Wrap value in range |
distribute(value, elements) | Distribute value across elements |
interpolate(start, end, progress) | Interpolate between values |
pipe(...functions) | Chain multiple functions |
getUnit(value) | Extract unit from value |
unitize(value, unit) | Add unit to value |
snap(increment, value) | Snap to nearest increment |
shuffle(array) | Shuffle array randomly |
splitColor(color) | Split color to RGBA |
checkPrefix(element, property) | Get vendor prefix |
selector(scope) | Create scoped selector |