UNPKG

16.9 kBTypeScriptView Raw
1declare namespace gsap.utils {
2
3 interface DistributeConfig {
4 amount?: number;
5 axis?: "x" | "y";
6 base?: number;
7 each?: number;
8 ease?: string | EaseFunction;
9 from?: "start" | "center" | "end" | "edges" | "random" | number | [number, number];
10 grid?: "auto" | [number, number];
11 }
12
13 interface SnapNumberConfig {
14 increment?: number;
15 values?: number[];
16 radius: number;
17 }
18
19 interface SnapPoint2DConfig {
20 values: Point2D[];
21 radius: number;
22 }
23
24 type toArrayValue = string | object | Element | null;
25
26 /**
27 * Prefixes the provided CSS property if necessary. Returns null if the property isn't supported at all.
28 *
29 * ```js
30 * // The following may return "filter", "WebkitFilter", or "MozFilter" depending on the browser
31 * const filterProperty = gsap.utils.checkPrefix("filter");
32 * ```
33 *
34 * @param {string} property
35 * @returns {string | null} The appropriately prefixed property
36 * @memberof gsap.utils
37 */
38 function checkPrefix(property: string): string;
39
40 /**
41 * Clamps a number between a given minimum and maximum.
42 *
43 * ```js
44 * gsap.utils.clamp(0, 100, 105); // returns 100
45 *
46 * const clamper = gsap.utils.clamp(0, 100); // no value = reusable function
47 * console.log(clamper(105)); // returns 100
48 * ```
49 *
50 * @param {number} minimum
51 * @param {number} maximum
52 * @param {number} [valueToClamp]
53 * @returns {number | Function} The clamped number or function to clamp to given range
54 * @memberof gsap.utils
55 */
56 function clamp(minimum: number, maximum: number, valueToClamp: number): number;
57 function clamp(minimum: number, maximum: number): (valueToClamp: number) => number;
58
59 /**
60 * Returns a function to distribute an array of values based on the inputs that you give it.
61 *
62 * ```js
63 * gsap.utils.distribute({
64 * base: 50,
65 * amount: 100,
66 * from: "center",
67 * grid: "auto",
68 * axis: "y",
69 * ease: "power1.inOut"
70 * });
71 * ```
72 *
73 * @param {DistributeConfig} config
74 * @returns {FunctionBasedValue<number>} The clamped number or function to clamp to given range
75 * @memberof gsap.utils
76 */
77 function distribute(config: DistributeConfig): FunctionBasedValue<number>;
78
79 /**
80 * Returns unit of a given string where the number comes first, then the unit.
81 *
82 * ```js
83 * gsap.utils.getUnit("50%"); // "%"
84 * ```
85 *
86 * @param {string} value
87 * @returns {string} The unit
88 * @memberof gsap.utils
89 */
90 function getUnit(value: string): string;
91
92 /**
93 * Linearly interpolates between any two values of a similar type.
94 *
95 * ```js
96 * gsap.utils.interpolate(0, 500, 0.5); // 250
97 *
98 * const interp = gsap.utils.interpolate(0, 100); // no value = reusable function
99 * console.log( interp(0.5) ); // 50
100 * ```
101 *
102 * @param {T} startValue
103 * @param {T} endValue
104 * @param {number} [number]
105 * @returns {T | Function<number>} The interpolated value or interpolate function
106 * @memberof gsap.utils
107 */
108 function interpolate<T>(startValue: T, endValue: T, progress: number): T;
109 function interpolate<T>(startValue: T, endValue: T): (progress: number) => T;
110 /**
111 * Linearly interpolates between any two values of a similar type.
112 *
113 * ```js
114 * gsap.utils.interpolate([100, 50, 500], 0.5); // 50
115 *
116 * c interp = gsap.utils.interpolate([100, 50, 500]); // no value = reusable function
117 * console.log( interp(0.5) ); // 50
118 * ```
119 *
120 * @param {T[]} array
121 * @param {number} progress
122 * @returns {T | Function} The interpolated value or interpolate function
123 * @memberof gsap.utils
124 */
125 function interpolate<T>(array: T[], progress: number): T;
126 function interpolate<T>(array: T[]): (progress: number) => T;
127
128 /**
129 * Maps a number's relative placement within one range to the equivalent position in another range.
130 *
131 * ```js
132 * gsap.utils.mapRange(-10, 10, 100, 200, 0); // 150
133 *
134 * const mapper = gsap.utils.mapRange(0, 100, 0, 250); // no value = reusable function
135 * console.log( mapper(50) ); // 250
136 * ```
137 *
138 * @param {number} inMin
139 * @param {number} inMax
140 * @param {number} outMin
141 * @param {number} outMax
142 * @param {number} [value]
143 * @returns {number | Function} The mapped value or map function
144 * @memberof gsap.utils
145 */
146 function mapRange(inMin: number, inMax: number, outMin: number, outMax: number, value: number): number;
147 function mapRange(inMin: number, inMax: number, outMin: number, outMax: number): (value: number) => number;
148
149 /**
150 * Maps a value within a provided range to the corresponding position in the range between 0 and 1.
151 *
152 * ```js
153 * gsap.utils.normalize(-10, 10, 0); // 0.5
154 *
155 * const clamper = gsap.utils.normalize(0, 100); // no value = reusable function
156 * console.log( clamper(50) ); // 0.5
157 * ```
158 *
159 * @param {number} inMin
160 * @param {number} inMax
161 * @param {number} [value]
162 * @returns {number | Function} The normalized value or normalizer function
163 * @memberof gsap.utils
164 */
165 function normalize(inMin: number, inMax: number, value: number): number;
166 function normalize(inMin: number, inMax: number): (value: number) => number;
167
168 /**
169 * Strings together multiple function calls, passing the result from one to the next.
170 * You can pass in as many function references as you'd like!
171 *
172 * ```js
173 * const transfrom = gsap.utils.pipe(func1, func2, func3); // reusable function
174 * const output = transform(input);
175 * ```
176 *
177 * @param {Function} ab
178 * @param {Function} bc
179 * @param {Function} [cd]
180 * @returns {Function} The function that pipes values from function to function given
181 * @memberof gsap.utils
182 */
183 function pipe<A extends Array<unknown>, B>(
184 ab: (...a: A) => B
185 ): (...a: A) => B;
186 function pipe<A extends Array<unknown>, B, C>(
187 ab: (...a: A) => B,
188 bc: (b: B) => C
189 ): (...a: A) => C
190 function pipe<A extends Array<unknown>, B, C, D>(
191 ab: (...a: A) => B,
192 bc: (b: B) => C,
193 cd: (c: C) => D
194 ): (...a: A) => D;
195 function pipe<A extends Array<unknown>, B, C, D, E>(
196 ab: (...a: A) => B,
197 bc: (b: B) => C,
198 cd: (c: C) => D,
199 de: (d: D) => E
200 ): (...a: A) => E;
201 function pipe<A extends Array<unknown>, B, C, D, E, F>(
202 ab: (...a: A) => B,
203 bc: (b: B) => C,
204 cd: (c: C) => D,
205 de: (d: D) => E,
206 ef: (e: E) => F
207 ): (...a: A) => F;
208 function pipe<A extends Array<unknown>, B, C, D, E, F, G>(
209 ab: (...a: A) => B,
210 bc: (b: B) => C,
211 cd: (c: C) => D,
212 de: (d: D) => E,
213 ef: (e: E) => F,
214 fg: (f: F) => G
215 ): (...a: A) => G;
216 function pipe<A extends Array<unknown>, B, C, D, E, F, G, H>(
217 ab: (...a: A) => B,
218 bc: (b: B) => C,
219 cd: (c: C) => D,
220 de: (d: D) => E,
221 ef: (e: E) => F,
222 fg: (f: F) => G,
223 gh: (g: G) => H
224 ): (...a: A) => H;
225 function pipe<A extends Array<unknown>, B, C, D, E, F, G, H, I>(
226 ab: (...a: A) => B,
227 bc: (b: B) => C,
228 cd: (c: C) => D,
229 de: (d: D) => E,
230 ef: (e: E) => F,
231 fg: (f: F) => G,
232 gh: (g: G) => H,
233 hi: (h: H) => I
234 ): (...a: A) => I;
235 function pipe<A extends Array<unknown>, B, C, D, E, F, G, H, I, J>(
236 ab: (...a: A) => B,
237 bc: (b: B) => C,
238 cd: (c: C) => D,
239 de: (d: D) => E,
240 ef: (e: E) => F,
241 fg: (f: F) => G,
242 gh: (g: G) => H,
243 hi: (h: H) => I,
244 ij: (i: I) => J
245 ): (...a: A) => J;
246
247 /**
248 * Get a random number within a range, optionally rounding to an increment you provide.
249 *
250 * ```js
251 * gsap.utils.random(-100, 100);
252 * gsap.utils.random(0, 500, 5); // snapped to the nearest value of 5
253 *
254 * const random = gsap.utils.random(-200, 500, 10, true); // reusable function
255 * console.log( random() );
256 * ```
257 *
258 * @param {number} minValue
259 * @param {number} maxValue
260 * @param {number} [snapIncrement]
261 * @param {boolean} [returnFunction]
262 * @returns {number | Function} The random number or random number generator function
263 * @memberof gsap.utils
264 */
265 function random(minValue: number, maxValue: number, snapIncrement?: number): number;
266 function random<T extends boolean>(minValue: number, maxValue: number, returnFunction?: T): T extends true ? () => number : number;
267 function random<T extends boolean>(minValue: number, maxValue: number, snapIncrement: number, returnFunction?: T): T extends true ? () => number : number;
268 /**
269 * Get a random random element in an array.
270 *
271 * ```js
272 * gsap.utils.random(["red", "blue", "green"]); //"red", "blue", or "green"
273 *
274 * const random = gsap.utils.random([0, 100, 200], true);
275 * console.log( random() ); // 0, 100, or 200 (randomly selected)
276 * ```
277 *
278 * @param {T[]} array
279 * @param {boolean} [returnFunction]
280 * @returns {number | Function} The random number or random number generator function
281 * @memberof gsap.utils
282 */
283 function random<T>(array: T[]): T;
284 function random<T, U extends boolean>(array: T[], returnFunction?: U): U extends true ? () => T : T;
285
286 /**
287 * Takes an array and randomly shuffles it, returning the same (but shuffled) array.
288 *
289 * ```js
290 * gsap.utils.shuffle(array);
291 * ```
292 *
293 * @param {T[]} array
294 * @returns {T[]} The same shuffled array
295 * @memberof gsap.utils
296 */
297 function shuffle<T>(array: T[]): T[];
298
299 /**
300 * Snaps a value to the nearest increment of the number provided.
301 * Or snaps to a value in the given array.
302 * Or snaps to a value within the given radius (if an object is provided).
303 * Or returns a function that does the above (if the second value is not provided).
304 *
305 * ```js
306 * gsap.utils.snap(10, 23.5); // 20
307 * gsap.utils.snap([100, 50, 500], 65); // 50
308 * gsap.utils.snap({values:[0, 100, 300], radius:20}, 30.5); // 30.5
309 * gsap.utils.snap({increment:500, radius:150}, 310); // 310
310 *
311 * const snap = gsap.utils.snap(5); // no value = reusable function
312 * console.log( snap(0.5) ); // 0
313 * ```
314 *
315 * @param {SnapNumberConfig} snapConfig
316 * @param {number} [valueToSnap]
317 * @returns {number | Function} The snapped number or snap function
318 * @memberof gsap.utils
319 */
320 function snap(snapConfig: number | number[] | SnapNumberConfig, valueToSnap: number): number;
321 function snap(snapConfig: number | number[] | SnapNumberConfig): (valueToSnap: number) => number;
322 /**
323 * Snaps a value if within the given radius of a points (objects with "x" and "y" properties).
324 * Or returns a function that does the above (if the second value is not provided).
325 *
326 * ```js
327 *
328 * gsap.utils.snap({values:[0, 100, 300], radius:20}, 85); // 100
329 *
330 * const snap = gsap.utils.snap({values:[{x:0, y:0}, {x:10, y:10}, {x:20, y:20}], radius:5}); // no value = reusable function
331 * console.log( snap({x:8, y:8}) ); // {x:10, y:10}
332 * ```
333 *
334 * @param {SnapPoint2DConfig} snapConfig
335 * @param {number} [valueToSnap]
336 * @returns {Point2D | Function} The snapped number or snap function
337 * @memberof gsap.utils
338 */
339 function snap(snapConfig: SnapPoint2DConfig, valueToSnap: Point2D): Point2D;
340 function snap(snapConfig: SnapPoint2DConfig): (valueToSnap: Point2D) => Point2D;
341
342 /**
343 * Converts a string-based color value into an array consisting of RGB(A) or HSL values.
344 *
345 * ```js
346 * gsap.utils.splitColor("red"); // [255, 0, 0]
347 * gsap.utils.splitColor("rgba(204, 153, 51, 0.5)"); // [204, 153, 51, 0.5]
348 *
349 * gsap.utils.splitColor("#6fb936", true); // [94, 55, 47] - HSL value
350 * ```
351 *
352 * @param {string} color
353 * @param {boolean} [hsl]
354 * @returns {[number, number, number] | [number, number, number, number]} The converted color array
355 * @memberof gsap.utils
356 */
357 function splitColor(color: string, hsl?: boolean): [number, number, number] | [number, number, number, number];
358
359 /**
360 * Converts almost any array-like object into a flat Array.
361 *
362 * ```js
363 * var targets = gsap.utils.toArray(".class");
364 * var targets = gsap.utils.toArray(myElement);
365 * var targets = gsap.utils.toArray($(".class"));
366 * var targets = gsap.utils.toArray(document.querySelectorAll(".class1"), ".class2", rawElement);
367 * ```
368 *
369 * @param {toArrayValue} value
370 * @param {boolean} [leaveStrings]
371 * @returns {T[]} The converted array
372 * @memberof gsap.utils
373 */
374 function toArray<T>(value: toArrayValue, leaveStrings?: boolean): T[];
375 function toArray<T>(value1: toArrayValue, value2: toArrayValue, leaveStrings?: boolean): T[];
376 function toArray<T>(value1: toArrayValue, value2: toArrayValue, value3: toArrayValue, leaveStrings?: boolean): T[];
377 function toArray<T>(value1: toArrayValue, value2: toArrayValue, value3: toArrayValue, value4: toArrayValue, leaveStrings?: boolean): T[];
378 function toArray<T>(value1: toArrayValue, value2: toArrayValue, value3: toArrayValue, value4: toArrayValue, value5: toArrayValue, leaveStrings?: boolean): T[];
379
380 /**
381 * Ensures that a specific unit gets applied.
382 *
383 * ```js
384 * const clamp = gsap.utils.unitize( gsap.utils.clamp(0, 100), "px");
385 * clamp(132); // "100px"
386 *
387 * gsap.to(".class", {
388 * x: 1000,
389 * modifiers: {
390 * x: gsap.utils.unitize( gsap.utils.wrap(0, window.innerWidth), "px")
391 * }
392 * });
393 * ```
394 *
395 * @param {Function} fn
396 * @param {string} [unit]
397 * @returns {string} The value with unit added
398 * @memberof gsap.utils
399 */
400 function unitize<T extends Array<unknown>>(fn: (...args: T) => unknown, unit?: string): (...args: T) => string;
401
402 /**
403 * Returns the next number in a range after the given index, jumping to the start after the end has been reached.
404 *
405 * ```js
406 * let color = gsap.utils.wrap(["red", "green", "yellow"], 5); // "yellow"
407 *
408 * let wrapper = gsap.utils.wrap(["red", "green", "yellow"]); // no value = reusable function
409 * let color = wrapper(5) // "yellow"
410 * ```
411 *
412 * @param {number} value1
413 * @param {number} value2
414 * @param {number} [index]
415 * @returns {string} The wrapped value or wrap function
416 * @memberof gsap.utils
417 */
418 function wrap(value1: number, value2: number, index: number): number;
419 function wrap(value1: number, value2: number): (index: number) => number;
420 /**
421 * Returns the next item in an array after the given index, jumping to the start after the end has been reached.
422 *
423 * ```js
424 * let color = gsap.utils.wrap(["red", "green", "yellow"], 5); // "yellow"
425 *
426 * let wrapper = gsap.utils.wrap(["red", "green", "yellow"]); // no value = reusable function
427 * let color = wrapper(5) // "yellow"
428 * ```
429 *
430 * @param {T[]} values
431 * @param {number} [index]
432 * @returns {string} The wrapper value or wrap function
433 * @memberof gsap.utils
434 */
435 function wrap<T>(values: T[], index: number): T;
436 function wrap<T>(values: T[]): (index: number) => T;
437
438 /**
439 * Returns the next number in a range after the given index, wrapping backwards towards the start after the end has been reached.
440 *
441 * ```js
442 * let color = gsap.utils.wrap(["red", "green", "yellow"], 5); // "yellow"
443 *
444 * let wrapper = gsap.utils.wrap(["red", "green", "yellow"]); // no value = reusable function
445 * let color = wrapper(5) // "yellow"
446 * ```
447 *
448 * @param {number} value1
449 * @param {number} value2
450 * @param {number} [index]
451 * @returns {string} The wrapped value or wrap function
452 * @memberof gsap.utils
453 */
454 function wrapYoyo(value1: number, value2: number, index: number): number;
455 function wrapYoyo(value1: number, value2: number): (index: number) => number;
456 /**
457 * Returns the next item in an array after the given index, wrapping backwards towards the start after the end has been reached.
458 *
459 * ```js
460 * let color = gsap.utils.wrap(["red", "green", "yellow"], 5); // "yellow"
461 *
462 * let wrapper = gsap.utils.wrap(["red", "green", "yellow"]); // no value = reusable function
463 * let color = wrapper(5) // "yellow"
464 * ```
465 *
466 * @param {T[]} values
467 * @param {number} [index]
468 * @returns {string} The wrapper value or wrap function
469 * @memberof gsap.utils
470 */
471 function wrapYoyo<T>(values: T[], index: number): T;
472 function wrapYoyo<T>(values: T[]): (index: number) => T;
473}
474
475declare module "gsap/gsap-core" {
476 export const clamp: typeof gsap.utils.clamp;
477 export const distribute: typeof gsap.utils.distribute;
478 export const getUnit: typeof gsap.utils.getUnit;
479 export const interpolate: typeof gsap.utils.interpolate;
480 export const mapRange: typeof gsap.utils.mapRange;
481 export const normalize: typeof gsap.utils.normalize;
482 export const pipe: typeof gsap.utils.pipe;
483 export const random: typeof gsap.utils.random;
484 export const shuffle: typeof gsap.utils.shuffle;
485 export const snap: typeof gsap.utils.snap;
486 export const splitColor: typeof gsap.utils.splitColor;
487 export const toArray: typeof gsap.utils.toArray;
488 export const unitize: typeof gsap.utils.unitize;
489 export const wrap: typeof gsap.utils.wrap;
490 export const wrapYoyo: typeof gsap.utils.wrapYoyo;
491}
492
\No newline at end of file