1+ /**
2+ * Easing function type (0..1 -> 0..1).
3+ *
4+ * イージング関数の型 (0..1 -> 0..1)。
5+ *
6+ * @example
7+ * ```ts
8+ * const ease: Easing = (t) => t * t
9+ * ```
10+ */
111export type Easing = ( t : number ) => number
212
13+ /**
14+ * Clamps a value to the given range.
15+ *
16+ * 値を指定した範囲に収めます。
17+ *
18+ * @example
19+ * ```ts
20+ * const v = clamp(1.2, 0, 1)
21+ * ```
22+ */
323export const clamp = ( value : number , min : number , max : number ) =>
424 Math . min ( max , Math . max ( min , value ) )
525
26+ /**
27+ * Linear interpolation between two numbers.
28+ *
29+ * 2 つの数値を線形補間します。
30+ *
31+ * @example
32+ * ```ts
33+ * const v = lerp(0, 10, 0.5)
34+ * ```
35+ */
636export const lerp = ( a : number , b : number , t : number ) => a + ( b - a ) * t
737
38+ /**
39+ * Creates a cubic-bezier easing function.
40+ *
41+ * cubic-bezier のイージング関数を生成します。
42+ *
43+ * @example
44+ * ```ts
45+ * const ease = cubicBezier(0.42, 0, 0.58, 1)
46+ * ```
47+ */
848export const cubicBezier = ( x1 : number , y1 : number , x2 : number , y2 : number ) : Easing => {
949 if ( x1 === y1 && x2 === y2 ) {
1050 return ( t ) => clamp ( t , 0 , 1 )
@@ -87,29 +127,189 @@ export const cubicBezier = (x1: number, y1: number, x2: number, y2: number): Eas
87127 }
88128}
89129
130+ /**
131+ * Preset cubic-bezier easing (CSS "ease").
132+ *
133+ * 既定の cubic-bezier(CSS の "ease")。
134+ *
135+ * @example
136+ * ```ts
137+ * const ease = BEZIER_EASE
138+ * ```
139+ */
90140export const BEZIER_EASE = cubicBezier ( 0.25 , 0.1 , 0.25 , 1 )
141+ /**
142+ * Preset cubic-bezier easing for ease-in.
143+ *
144+ * ease-in 用のプリセット。
145+ *
146+ * @example
147+ * ```ts
148+ * const ease = BEZIER_EASE_IN
149+ * ```
150+ */
91151export const BEZIER_EASE_IN = cubicBezier ( 0.42 , 0 , 1 , 1 )
152+ /**
153+ * Preset cubic-bezier easing for ease-out.
154+ *
155+ * ease-out 用のプリセット。
156+ *
157+ * @example
158+ * ```ts
159+ * const ease = BEZIER_EASE_OUT
160+ * ```
161+ */
92162export const BEZIER_EASE_OUT = cubicBezier ( 0 , 0 , 0.58 , 1 )
163+ /**
164+ * Preset cubic-bezier easing for ease-in-out.
165+ *
166+ * ease-in-out 用のプリセット。
167+ *
168+ * @example
169+ * ```ts
170+ * const ease = BEZIER_EASE_IN_OUT
171+ * ```
172+ */
93173export const BEZIER_EASE_IN_OUT = cubicBezier ( 0.42 , 0 , 0.58 , 1 )
174+ /**
175+ * Preset cubic-bezier easing for smooth motion.
176+ *
177+ * なめらかな動き向けのプリセット。
178+ *
179+ * @example
180+ * ```ts
181+ * const ease = BEZIER_SMOOTH
182+ * ```
183+ */
94184export const BEZIER_SMOOTH = cubicBezier ( 0.4 , 0 , 0.2 , 1 )
185+ /**
186+ * Preset cubic-bezier easing for sharper motion.
187+ *
188+ * 切れ味のある動き向けのプリセット。
189+ *
190+ * @example
191+ * ```ts
192+ * const ease = BEZIER_SHARP
193+ * ```
194+ */
95195export const BEZIER_SHARP = cubicBezier ( 0.4 , 0 , 0.6 , 1 )
196+ /**
197+ * Preset cubic-bezier easing for acceleration.
198+ *
199+ * 加速を強めるプリセット。
200+ *
201+ * @example
202+ * ```ts
203+ * const ease = BEZIER_ACCELERATE
204+ * ```
205+ */
96206export const BEZIER_ACCELERATE = cubicBezier ( 0.4 , 0 , 1 , 1 )
207+ /**
208+ * Preset cubic-bezier easing for deceleration.
209+ *
210+ * 減速を強めるプリセット。
211+ *
212+ * @example
213+ * ```ts
214+ * const ease = BEZIER_DECELERATE
215+ * ```
216+ */
97217export const BEZIER_DECELERATE = cubicBezier ( 0 , 0 , 0.2 , 1 )
218+ /**
219+ * Preset cubic-bezier easing for snappy motion.
220+ *
221+ * 俊敏な動き向けのプリセット。
222+ *
223+ * @example
224+ * ```ts
225+ * const ease = BEZIER_SNAPPY
226+ * ```
227+ */
98228export const BEZIER_SNAPPY = cubicBezier ( 0.2 , 0.9 , 0.2 , 1 )
229+ /**
230+ * Preset cubic-bezier easing with overshoot.
231+ *
232+ * オーバーシュート向けのプリセット。
233+ *
234+ * @example
235+ * ```ts
236+ * const ease = BEZIER_OVERSHOOT
237+ * ```
238+ */
99239export const BEZIER_OVERSHOOT = cubicBezier ( 0.16 , 1.25 , 0.3 , 1 )
240+ /**
241+ * Soft overshoot cubic-bezier preset.
242+ *
243+ * 柔らかいオーバーシュートのプリセット。
244+ *
245+ * @example
246+ * ```ts
247+ * const ease = BEZIER_OVERSHOOT_SOFT
248+ * ```
249+ */
100250export const BEZIER_OVERSHOOT_SOFT = cubicBezier ( 0.12 , 1.1 , 0.3 , 1 )
251+ /**
252+ * Hard overshoot cubic-bezier preset.
253+ *
254+ * 強めのオーバーシュートのプリセット。
255+ *
256+ * @example
257+ * ```ts
258+ * const ease = BEZIER_OVERSHOOT_HARD
259+ * ```
260+ */
101261export const BEZIER_OVERSHOOT_HARD = cubicBezier ( 0.18 , 1.35 , 0.2 , 1 )
102262
263+ /**
264+ * Ease-out cubic easing.
265+ *
266+ * cubic の ease-out。
267+ *
268+ * @example
269+ * ```ts
270+ * const ease = easeOutCubic
271+ * ```
272+ */
103273export const easeOutCubic : Easing = ( t ) => 1 - Math . pow ( 1 - clamp ( t , 0 , 1 ) , 3 )
274+ /**
275+ * Ease-in-out cubic easing.
276+ *
277+ * cubic の ease-in-out。
278+ *
279+ * @example
280+ * ```ts
281+ * const ease = easeInOutCubic
282+ * ```
283+ */
104284export const easeInOutCubic : Easing = ( t ) => {
105285 const x = clamp ( t , 0 , 1 )
106286 return x < 0.5 ? 4 * x * x * x : 1 - Math . pow ( - 2 * x + 2 , 3 ) / 2
107287}
288+ /**
289+ * Ease-out exponential easing.
290+ *
291+ * 指数関数の ease-out。
292+ *
293+ * @example
294+ * ```ts
295+ * const ease = easeOutExpo
296+ * ```
297+ */
108298export const easeOutExpo : Easing = ( t ) => {
109299 const x = clamp ( t , 0 , 1 )
110300 return x === 1 ? 1 : 1 - Math . pow ( 2 , - 10 * x )
111301}
112302
303+ /**
304+ * Normalized progress between start/end frames.
305+ *
306+ * 開始/終了フレームから 0..1 の進捗を返します。
307+ *
308+ * @example
309+ * ```ts
310+ * const t = frameProgress(frame, 0, 60, easeOutCubic)
311+ * ```
312+ */
113313export const frameProgress = (
114314 frame : number ,
115315 startFrame : number ,
@@ -121,6 +321,16 @@ export const frameProgress = (
121321 return easing ( t )
122322}
123323
324+ /**
325+ * Convenience fade curve with optional in/out lengths.
326+ *
327+ * フェードイン/アウトの簡易カーブを返します。
328+ *
329+ * @example
330+ * ```ts
331+ * const opacity = fadeInOut(frame, durationFrames)
332+ * ```
333+ */
124334export const fadeInOut = ( frame : number , durationFrames : number , opts ?: { in ?: number ; out ?: number } ) => {
125335 const total = Math . max ( 1 , durationFrames )
126336 const fadeIn = Math . max ( 0 , Math . floor ( opts ?. in ?? Math . min ( 18 , total / 6 ) ) )
@@ -131,5 +341,15 @@ export const fadeInOut = (frame: number, durationFrames: number, opts?: { in?: n
131341 return Math . min ( tIn , tOut )
132342}
133343
344+ /**
345+ * Returns a staggered start frame offset.
346+ *
347+ * スタガーの開始フレームを返します。
348+ *
349+ * @example
350+ * ```ts
351+ * const start = stagger(index, seconds(0.1))
352+ * ```
353+ */
134354export const stagger = ( index : number , eachFrames : number , base = 0 ) =>
135355 base + index * Math . max ( 0 , eachFrames )
0 commit comments