๐Ÿ“ฆ aquaticcalf / samcan

๐Ÿ“„ api.ts ยท 536 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536import {
    AnimationRuntime,
    type AnimationData,
} from "./animation/animationruntime"
import { RendererFactory } from "./renderer/rendererfactory"
import { AssetManager } from "./asset/assetmanager"
import { Serializer } from "./serialization/serializer"
import type { Renderer, RendererBackend } from "./renderer/renderer"
import type { SamcanFile } from "./serialization/types"
import type { Artboard } from "./scene/nodes/artboard"
import type { SceneNode } from "./scene/node"
import { AnimationError } from "./error/animationerror"

/**
 * Configuration options for creating an animation player
 */
export interface PlayerConfig {
    /**
     * The canvas element to render to
     */
    canvas: HTMLCanvasElement

    /**
     * Preferred rendering backend (will fallback automatically if unavailable)
     * @default "webgl"
     */
    backend?: RendererBackend

    /**
     * Whether to start playing immediately after loading
     * @default false
     */
    autoplay?: boolean

    /**
     * Whether to loop the animation
     * @default false
     */
    loop?: boolean

    /**
     * Playback speed multiplier
     * @default 1.0
     */
    speed?: number

    /**
     * Asset manager instance (optional, will create one if not provided)
     */
    assetManager?: AssetManager
}

/**
 * Options for loading animations
 */
export interface LoadOptions {
    /**
     * Whether to preload all assets before resolving
     * @default true
     */
    preloadAssets?: boolean

    /**
     * Timeout in milliseconds for asset loading
     * @default 30000 (30 seconds)
     */
    assetTimeout?: number
}

/**
 * Simple animation player that wraps AnimationRuntime with convenient defaults
 * This is the recommended way to play animations in most applications
 *
 * @example
 * ```typescript
 * // Create a player
 * const player = await samcan.createPlayer({
 *   canvas: document.getElementById('canvas'),
 *   autoplay: true,
 *   loop: true
 * })
 *
 * // Load and play an animation
 * await player.load('animation.samcan')
 *
 * // Control playback
 * player.pause()
 * player.play()
 * player.stop()
 * ```
 */
export class AnimationPlayer {
    private _runtime: AnimationRuntime
    private _renderer: Renderer
    private _assetManager: AssetManager
    private _serializer: Serializer
    private _config: Required<
        Omit<PlayerConfig, "canvas" | "backend" | "assetManager">
    >

    constructor(
        runtime: AnimationRuntime,
        renderer: Renderer,
        assetManager: AssetManager,
        config: PlayerConfig,
    ) {
        this._runtime = runtime
        this._renderer = renderer
        this._assetManager = assetManager
        this._serializer = new Serializer()
        this._config = {
            autoplay: config.autoplay ?? false,
            loop: config.loop ?? false,
            speed: config.speed ?? 1.0,
        }

        // Apply initial configuration
        this._runtime.setSpeed(this._config.speed)
        this._runtime.setLoop(this._config.loop ? "loop" : "none")
    }

    /**
     * Get the underlying AnimationRuntime instance for advanced usage
     */
    get runtime(): AnimationRuntime {
        return this._runtime
    }

    /**
     * Get the renderer instance
     */
    get renderer(): Renderer {
        return this._renderer
    }

    /**
     * Get the asset manager instance
     */
    get assetManager(): AssetManager {
        return this._assetManager
    }

    /**
     * Load an animation from a URL or SamcanFile object
     *
     * @param source - URL string or SamcanFile object
     * @param options - Loading options
     */
    async load(
        source: string | SamcanFile,
        options?: LoadOptions,
    ): Promise<void> {
        const opts: Required<LoadOptions> = {
            preloadAssets: options?.preloadAssets ?? true,
            assetTimeout: options?.assetTimeout ?? 30000,
        }

        let file: SamcanFile

        // Load file from URL or use provided object
        if (typeof source === "string") {
            file = await this._loadFromUrl(source)
        } else {
            file = source
        }

        // Validate file
        if (!file.artboards || file.artboards.length === 0) {
            throw AnimationError.invalidData(
                "No artboards found in animation file",
            )
        }

        // Preload assets if requested
        if (opts.preloadAssets && file.assets && file.assets.length > 0) {
            await this._preloadAssets(file.assets, opts.assetTimeout)
        }

        // Deserialize the first artboard and its timeline
        const artboardData = file.artboards[0]
        if (!artboardData) {
            throw AnimationError.invalidData("Invalid artboard data")
        }

        const artboard = this._serializer.deserializeArtboard(artboardData)

        // Build node map for timeline deserialization
        const nodeMap = new Map<string, SceneNode>()
        _buildNodeMapForDeserialization(artboard, nodeMap)

        const timeline = this._serializer.deserializeTimeline(
            artboardData.timeline,
            nodeMap,
        )

        // Load into runtime
        const animationData: AnimationData = {
            artboard,
            timeline,
        }

        await this._runtime.load(animationData)

        // Autoplay if configured
        if (this._config.autoplay) {
            this._runtime.play()
        }
    }

    /**
     * Load animation from a URL
     */
    private async _loadFromUrl(url: string): Promise<SamcanFile> {
        try {
            const response = await fetch(url)

            if (!response.ok) {
                throw new Error(
                    `HTTP ${response.status}: ${response.statusText}`,
                )
            }

            const contentType = response.headers.get("content-type")

            // Check if compressed (gzip)
            if (contentType?.includes("gzip") || url.endsWith(".gz")) {
                const arrayBuffer = await response.arrayBuffer()
                const compressed = new Uint8Array(arrayBuffer)
                return await this._serializer.fromCompressedJSON(compressed)
            }

            // Otherwise treat as JSON
            const json = await response.text()
            return this._serializer.fromJSON(json)
        } catch (error) {
            throw AnimationError.invalidData(
                `Failed to load animation from ${url}: ${error instanceof Error ? error.message : String(error)}`,
            )
        }
    }

    /**
     * Preload assets with timeout
     */
    private async _preloadAssets(
        assets: SamcanFile["assets"],
        timeout: number,
    ): Promise<void> {
        const preloadPromise = this._assetManager.preload(
            assets.map((asset) => ({
                url: asset.url,
                type: asset.type,
                family: asset.type === "font" ? asset.name : undefined,
            })),
        )

        // Add timeout
        const timeoutPromise = new Promise<never>((_, reject) => {
            setTimeout(() => {
                reject(new Error(`Asset loading timed out after ${timeout}ms`))
            }, timeout)
        })

        await Promise.race([preloadPromise, timeoutPromise])
    }

    /**
     * Start or resume playback
     */
    play(): void {
        this._runtime.play()
    }

    /**
     * Pause playback
     */
    pause(): void {
        this._runtime.pause()
    }

    /**
     * Stop playback and reset to beginning
     */
    stop(): void {
        this._runtime.stop()
    }

    /**
     * Seek to a specific time
     * @param time - Time in seconds
     */
    seek(time: number): void {
        this._runtime.seek(time)
    }

    /**
     * Set playback speed
     * @param speed - Speed multiplier (1.0 = normal)
     */
    setSpeed(speed: number): void {
        this._config.speed = speed
        this._runtime.setSpeed(speed)
    }

    /**
     * Set loop mode
     * @param loop - Whether to loop the animation
     */
    setLoop(loop: boolean): void {
        this._config.loop = loop
        this._runtime.setLoop(loop ? "loop" : "none")
    }

    /**
     * Get current playback time in seconds
     */
    get currentTime(): number {
        return this._runtime.currentTime
    }

    /**
     * Get animation duration in seconds
     */
    get duration(): number {
        return this._runtime.duration
    }

    /**
     * Check if animation is currently playing
     */
    get isPlaying(): boolean {
        return this._runtime.isPlaying
    }

    /**
     * Get the loaded artboard
     */
    get artboard(): Artboard | null {
        return this._runtime.artboard
    }

    /**
     * Register an event listener
     * @param event - Event name
     * @param callback - Callback function
     * @returns Unsubscribe function
     */
    on(
        event: "play" | "pause" | "stop" | "complete" | "loop",
        callback: () => void,
    ): () => void {
        return this._runtime.on(event, callback)
    }

    /**
     * Resize the canvas and renderer
     * @param width - New width in pixels
     * @param height - New height in pixels
     */
    resize(width: number, height: number): void {
        this._renderer.resize(width, height)
    }

    /**
     * Clean up resources
     */
    destroy(): void {
        this._runtime.unload()
        // Note: We don't destroy the renderer or asset manager as they might be shared
    }
}

/**
 * Create an animation player with the specified configuration
 *
 * @param config - Player configuration
 * @returns Promise resolving to an AnimationPlayer instance
 *
 * @example
 * ```typescript
 * const player = await samcan.createPlayer({
 *   canvas: document.getElementById('canvas'),
 *   autoplay: true,
 *   loop: true,
 *   speed: 1.5
 * })
 *
 * await player.load('animation.samcan')
 * ```
 */
export async function createPlayer(
    config: PlayerConfig,
): Promise<AnimationPlayer> {
    // Create renderer with fallback
    const renderer = await RendererFactory.create(
        config.canvas,
        config.backend ?? "webgl",
    )

    // Create or use provided asset manager
    const assetManager = config.assetManager ?? new AssetManager()

    // Create runtime
    const runtime = new AnimationRuntime(renderer)

    // Create and return player
    return new AnimationPlayer(runtime, renderer, assetManager, config)
}

/**
 * Load and play an animation with minimal configuration
 * This is the simplest way to play an animation
 *
 * @param canvas - Canvas element to render to
 * @param url - URL of the animation file
 * @param options - Optional configuration
 * @returns Promise resolving to an AnimationPlayer instance
 *
 * @example
 * ```typescript
 * const player = await samcan.play(
 *   document.getElementById('canvas'),
 *   'animation.samcan',
 *   { loop: true }
 * )
 * ```
 */
export async function play(
    canvas: HTMLCanvasElement,
    url: string,
    options?: {
        loop?: boolean
        speed?: number
        backend?: RendererBackend
    },
): Promise<AnimationPlayer> {
    const player = await createPlayer({
        canvas,
        autoplay: true,
        loop: options?.loop ?? false,
        speed: options?.speed ?? 1.0,
        backend: options?.backend,
    })

    await player.load(url)

    return player
}

/**
 * Load an animation file from a URL
 *
 * @param url - URL of the animation file
 * @returns Promise resolving to a SamcanFile object
 *
 * @example
 * ```typescript
 * const animation = await samcan.loadAnimation('animation.samcan')
 * console.log(animation.metadata.name)
 * console.log(`Duration: ${animation.artboards[0].timeline.duration}s`)
 * ```
 */
export async function loadAnimation(url: string): Promise<SamcanFile> {
    const serializer = new Serializer()

    try {
        const response = await fetch(url)

        if (!response.ok) {
            throw new Error(`HTTP ${response.status}: ${response.statusText}`)
        }

        const contentType = response.headers.get("content-type")

        // Check if compressed
        if (contentType?.includes("gzip") || url.endsWith(".gz")) {
            const arrayBuffer = await response.arrayBuffer()
            const compressed = new Uint8Array(arrayBuffer)
            return await serializer.fromCompressedJSON(compressed)
        }

        // Otherwise treat as JSON
        const json = await response.text()
        return serializer.fromJSON(json)
    } catch (error) {
        throw AnimationError.invalidData(
            `Failed to load animation from ${url}: ${error instanceof Error ? error.message : String(error)}`,
        )
    }
}

/**
 * Helper method to build node map for timeline deserialization
 * Maps node IDs (strings) to SceneNode instances
 */
function _buildNodeMapForDeserialization(
    node: SceneNode,
    nodeMap: Map<string, SceneNode>,
    idCounter = { value: 0 },
): void {
    const id = `node_${idCounter.value++}`
    nodeMap.set(id, node)

    for (const child of node.children) {
        _buildNodeMapForDeserialization(child, nodeMap, idCounter)
    }
}

/**
 * Get information about available rendering backends
 *
 * @returns Object with backend availability information
 *
 * @example
 * ```typescript
 * const info = samcan.getBackendInfo()
 * console.log('Available backends:', info.available)
 * console.log('WebGL supported:', info.webgl)
 * ```
 */
export function getBackendInfo(): {
    available: RendererBackend[]
    canvas2d: boolean
    webgl: boolean
    webgpu: boolean
} {
    const available = RendererFactory.getAvailableBackends()

    return {
        available,
        canvas2d: available.includes("canvas2d"),
        webgl: available.includes("webgl"),
        webgpu: available.includes("webgpu"),
    }
}