Skip to content

Commit 5b4dbc4

Browse files
committed
docs
1 parent e15f4bb commit 5b4dbc4

File tree

15 files changed

+944
-3
lines changed

15 files changed

+944
-3
lines changed

src/lib/animation.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,38 @@ import type { Easing } from "./animation/functions"
55

66
type Lerp<T> = (from: T, to: T, t: number) => T
77

8+
/**
9+
* 2D vector type.
10+
*
11+
* 2 次元ベクトル型。
12+
*
13+
* @example
14+
* ```ts
15+
* const v: Vec2 = { x: 10, y: 20 }
16+
* ```
17+
*/
818
export type Vec2 = { x: number; y: number }
19+
/**
20+
* 3D vector type.
21+
*
22+
* 3 次元ベクトル型。
23+
*
24+
* @example
25+
* ```ts
26+
* const v: Vec3 = { x: 1, y: 2, z: 3 }
27+
* ```
28+
*/
929
export type Vec3 = { x: number; y: number; z: number }
30+
/**
31+
* Supported variable value types for animation.
32+
*
33+
* アニメーション変数で使える値の型。
34+
*
35+
* @example
36+
* ```ts
37+
* const value: VariableType = { x: 0, y: 0 }
38+
* ```
39+
*/
1040
export type VariableType = number | Vec2 | Vec3
1141

1242
type VariableKind = "number" | "vec2" | "vec3"
@@ -27,6 +57,17 @@ type VariableStateBase = {
2757
ownerId: number | null
2858
}
2959

60+
/**
61+
* Animation variable with timeline-aware sampling.
62+
*
63+
* タイムラインに応じた値取得ができるアニメーション変数。
64+
*
65+
* @example
66+
* ```tsx
67+
* const pos = useVariable({ x: 0, y: 0 })
68+
* const value = pos.use()
69+
* ```
70+
*/
3071
export type Variable<T> = {
3172
use: () => T
3273
get: (frame: number) => T
@@ -196,6 +237,17 @@ const sampleVariable = (state: VariableStateBase, frame: number) => {
196237
return value
197238
}
198239

240+
/**
241+
* Thenable handle returned by animation commands.
242+
*
243+
* アニメーション操作が返す thenable ハンドル。
244+
*
245+
* @example
246+
* ```tsx
247+
* const handle = ctx.move(position).to({ x: 100, y: 0 }, seconds(1))
248+
* await handle
249+
* ```
250+
*/
199251
export class AnimationHandle {
200252
private resolved = false
201253
public readonly endFrame: number
@@ -219,6 +271,17 @@ export class AnimationHandle {
219271
}
220272
}
221273

274+
/**
275+
* Creates an animatable variable.
276+
*
277+
* アニメーション可能な変数を作成します。
278+
*
279+
* @example
280+
* ```tsx
281+
* const opacity = useVariable(0)
282+
* const pos = useVariable({ x: 0, y: 0 })
283+
* ```
284+
*/
222285
export function useVariable(initial: number): Variable<number>
223286
export function useVariable(initial: Vec2): Variable<Vec2>
224287
export function useVariable(initial: Vec3): Variable<Vec3>
@@ -260,6 +323,19 @@ export function useVariable<T extends VariableType>(initial: T): Variable<T> {
260323
return { use: useValue, get, _state: state }
261324
}
262325

326+
/**
327+
* Defines an animation sequence and reports its duration to the current clip.
328+
*
329+
* アニメーションシーケンスを定義し、クリップへ長さを報告します。
330+
*
331+
* @example
332+
* ```tsx
333+
* useAnimation(async (ctx) => {
334+
* await ctx.sleep(seconds(0.5))
335+
* await ctx.move(pos).to({ x: 200, y: 0 }, seconds(1))
336+
* }, [])
337+
* ```
338+
*/
263339
export const useAnimation = (
264340
run: (ctx: AnimationContext) => Promise<void> | void,
265341
deps: DependencyList = [run],

src/lib/animation/functions.ts

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,50 @@
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+
*/
111
export 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+
*/
323
export 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+
*/
636
export 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+
*/
848
export 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+
*/
90140
export 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+
*/
91151
export 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+
*/
92162
export 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+
*/
93173
export 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+
*/
94184
export 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+
*/
95195
export 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+
*/
96206
export 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+
*/
97217
export 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+
*/
98228
export 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+
*/
99239
export 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+
*/
100250
export 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+
*/
101261
export 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+
*/
103273
export 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+
*/
104284
export 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+
*/
108298
export 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+
*/
113313
export 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+
*/
124334
export 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+
*/
134354
export const stagger = (index: number, eachFrames: number, base = 0) =>
135355
base + index * Math.max(0, eachFrames)

0 commit comments

Comments
 (0)