@rive-app/webgl2 2.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@rive-app/webgl2",
3
+ "version": "2.11.0",
4
+ "description": "Rive's webgl2 based web api.",
5
+ "main": "rive.js",
6
+ "homepage": "https://rive.app",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/rive-app/rive-wasm/tree/master/js"
10
+ },
11
+ "keywords": [
12
+ "rive",
13
+ "animation"
14
+ ],
15
+ "author": "Rive",
16
+ "contributors": [
17
+ "Luigi Rosso <luigi@rive.app> (https://rive.app)",
18
+ "Maxwell Talbot <max@rive.app> (https://rive.app)",
19
+ "Arthur Vivian <arthur@rive.app> (https://rive.app)",
20
+ "Umberto Sonnino <umberto@rive.app> (https://rive.app)",
21
+ "Matthew Sullivan <matt.j.sullivan@gmail.com> (mailto:matt.j.sullivan@gmail.com)",
22
+ "Chris Dalton <chris@rive.app> (https://rive.app)"
23
+ ],
24
+ "license": "MIT",
25
+ "files": [
26
+ "rive.js",
27
+ "rive.wasm",
28
+ "rive.js.map",
29
+ "rive.d.ts",
30
+ "rive_advanced.mjs.d.ts"
31
+ ],
32
+ "typings": "rive.d.ts",
33
+ "dependencies": {},
34
+ "browser": {
35
+ "fs": false,
36
+ "path": false
37
+ }
38
+ }
package/rive.d.ts ADDED
@@ -0,0 +1,555 @@
1
+ import * as rc from "./rive_advanced.mjs";
2
+ export type { FileAsset, FontAsset, ImageAsset } from './rive_advanced.mjs';
3
+ /**
4
+ * Generic type for a parameterless void callback
5
+ */
6
+ export type VoidCallback = () => void;
7
+ export type AssetLoadCallback = (asset: rc.FileAsset, bytes: Uint8Array) => Boolean;
8
+ /**
9
+ * Type for artboard bounds
10
+ */
11
+ export type Bounds = rc.AABB;
12
+ export declare enum Fit {
13
+ Cover = "cover",
14
+ Contain = "contain",
15
+ Fill = "fill",
16
+ FitWidth = "fitWidth",
17
+ FitHeight = "fitHeight",
18
+ None = "none",
19
+ ScaleDown = "scaleDown"
20
+ }
21
+ export declare enum Alignment {
22
+ Center = "center",
23
+ TopLeft = "topLeft",
24
+ TopCenter = "topCenter",
25
+ TopRight = "topRight",
26
+ CenterLeft = "centerLeft",
27
+ CenterRight = "centerRight",
28
+ BottomLeft = "bottomLeft",
29
+ BottomCenter = "bottomCenter",
30
+ BottomRight = "bottomRight"
31
+ }
32
+ export interface LayoutParameters {
33
+ fit?: Fit;
34
+ alignment?: Alignment;
35
+ minX?: number;
36
+ minY?: number;
37
+ maxX?: number;
38
+ maxY?: number;
39
+ }
40
+ export declare class Layout {
41
+ private cachedRuntimeFit;
42
+ private cachedRuntimeAlignment;
43
+ readonly fit: Fit;
44
+ readonly alignment: Alignment;
45
+ readonly minX: number;
46
+ readonly minY: number;
47
+ readonly maxX: number;
48
+ readonly maxY: number;
49
+ constructor(params?: LayoutParameters);
50
+ static new({ fit, alignment, minX, minY, maxX, maxY, }: LayoutParameters): Layout;
51
+ /**
52
+ * Makes a copy of the layout, replacing any specified parameters
53
+ */
54
+ copyWith({ fit, alignment, minX, minY, maxX, maxY, }: LayoutParameters): Layout;
55
+ runtimeFit(rive: rc.RiveCanvas): rc.Fit;
56
+ runtimeAlignment(rive: rc.RiveCanvas): rc.Alignment;
57
+ }
58
+ export type RuntimeCallback = (rive: rc.RiveCanvas) => void;
59
+ export declare class RuntimeLoader {
60
+ private static runtime;
61
+ private static isLoading;
62
+ private static callBackQueue;
63
+ private static rive;
64
+ private static wasmURL;
65
+ private constructor();
66
+ private static loadRuntime;
67
+ static getInstance(callback: RuntimeCallback): void;
68
+ static awaitInstance(): Promise<rc.RiveCanvas>;
69
+ static setWasmUrl(url: string): void;
70
+ }
71
+ export declare enum StateMachineInputType {
72
+ Number = 56,
73
+ Trigger = 58,
74
+ Boolean = 59
75
+ }
76
+ /**
77
+ * An input for a state machine
78
+ */
79
+ export declare class StateMachineInput {
80
+ readonly type: StateMachineInputType;
81
+ private runtimeInput;
82
+ constructor(type: StateMachineInputType, runtimeInput: rc.SMIInput);
83
+ /**
84
+ * Returns the name of the input
85
+ */
86
+ get name(): string;
87
+ /**
88
+ * Returns the current value of the input
89
+ */
90
+ get value(): number | boolean;
91
+ /**
92
+ * Sets the value of the input
93
+ */
94
+ set value(value: number | boolean);
95
+ /**
96
+ * Fires a trigger; does nothing on Number or Boolean input types
97
+ */
98
+ fire(): void;
99
+ }
100
+ export declare enum RiveEventType {
101
+ General = 128,
102
+ OpenUrl = 131
103
+ }
104
+ /**
105
+ * Supported event types triggered in Rive
106
+ */
107
+ export declare enum EventType {
108
+ Load = "load",
109
+ LoadError = "loaderror",
110
+ Play = "play",
111
+ Pause = "pause",
112
+ Stop = "stop",
113
+ Loop = "loop",
114
+ Draw = "draw",
115
+ Advance = "advance",
116
+ StateChange = "statechange",
117
+ RiveEvent = "riveevent"
118
+ }
119
+ export type RiveEventPayload = rc.RiveEvent | rc.OpenUrlEvent;
120
+ export interface Event {
121
+ type: EventType;
122
+ data?: string | string[] | LoopEvent | number | RiveEventPayload;
123
+ }
124
+ /**
125
+ * Looping types: one-shot, loop, and ping-pong
126
+ */
127
+ export declare enum LoopType {
128
+ OneShot = "oneshot",
129
+ Loop = "loop",
130
+ PingPong = "pingpong"
131
+ }
132
+ /**
133
+ * Loop events are returned through onloop callbacks
134
+ */
135
+ export interface LoopEvent {
136
+ animation: string;
137
+ type: LoopType;
138
+ }
139
+ /**
140
+ * Loop events are returned through onloop callbacks
141
+ */
142
+ export type EventCallback = (event: Event) => void;
143
+ /**
144
+ * Event listeners registered with the event manager
145
+ */
146
+ export interface EventListener {
147
+ type: EventType;
148
+ callback: EventCallback;
149
+ }
150
+ /**
151
+ * FPS Reporting through callbacks sent to the WASM runtime
152
+ */
153
+ export type FPSCallback = (fps: number) => void;
154
+ declare class EventManager {
155
+ private listeners;
156
+ constructor(listeners?: EventListener[]);
157
+ private getListeners;
158
+ add(listener: EventListener): void;
159
+ /**
160
+ * Removes a listener
161
+ * @param listener the listener with the callback to be removed
162
+ */
163
+ remove(listener: EventListener): void;
164
+ /**
165
+ * Clears all listeners of specified type, or every listener if no type is
166
+ * specified
167
+ * @param type the type of listeners to clear, or all listeners if not
168
+ * specified
169
+ */
170
+ removeAll(type?: EventType): void;
171
+ fire(event: Event): void;
172
+ }
173
+ export interface Task {
174
+ action?: VoidCallback;
175
+ event?: Event;
176
+ }
177
+ declare class TaskQueueManager {
178
+ private eventManager;
179
+ private queue;
180
+ constructor(eventManager: EventManager);
181
+ add(task: Task): void;
182
+ process(): void;
183
+ }
184
+ export interface RiveParameters {
185
+ canvas: HTMLCanvasElement | OffscreenCanvas;
186
+ src?: string;
187
+ buffer?: ArrayBuffer;
188
+ artboard?: string;
189
+ animations?: string | string[];
190
+ stateMachines?: string | string[];
191
+ layout?: Layout;
192
+ autoplay?: boolean;
193
+ useOffscreenRenderer?: boolean;
194
+ /**
195
+ * Allow the runtime to automatically load assets hosted in Rive's CDN.
196
+ * enabled by default.
197
+ */
198
+ enableRiveAssetCDN?: boolean;
199
+ /**
200
+ * Turn off Rive Listeners. This means state machines that have Listeners
201
+ * will not be invoked, and also, no event listeners pertaining to Listeners
202
+ * will be attached to the <canvas> element
203
+ */
204
+ shouldDisableRiveListeners?: boolean;
205
+ /**
206
+ * Enable Rive Events to be handled by the runtime. This means any special Rive Event may have
207
+ * a side effect that takes place implicitly.
208
+ *
209
+ * For example, if during the render loop an OpenUrlEvent is detected, the
210
+ * browser may try to open the specified URL in the payload.
211
+ *
212
+ * This flag is false by default to prevent any unwanted behaviors from taking place.
213
+ * This means any special Rive Event will have to be handled manually by subscribing to
214
+ * EventType.RiveEvent
215
+ */
216
+ automaticallyHandleEvents?: boolean;
217
+ onLoad?: EventCallback;
218
+ onLoadError?: EventCallback;
219
+ onPlay?: EventCallback;
220
+ onPause?: EventCallback;
221
+ onStop?: EventCallback;
222
+ onLoop?: EventCallback;
223
+ onStateChange?: EventCallback;
224
+ onAdvance?: EventCallback;
225
+ assetLoader?: AssetLoadCallback;
226
+ /**
227
+ * @deprecated Use `onLoad()` instead
228
+ */
229
+ onload?: EventCallback;
230
+ /**
231
+ * @deprecated Use `onLoadError()` instead
232
+ */
233
+ onloaderror?: EventCallback;
234
+ /**
235
+ * @deprecated Use `onPoad()` instead
236
+ */
237
+ onplay?: EventCallback;
238
+ /**
239
+ * @deprecated Use `onPause()` instead
240
+ */
241
+ onpause?: EventCallback;
242
+ /**
243
+ * @deprecated Use `onStop()` instead
244
+ */
245
+ onstop?: EventCallback;
246
+ /**
247
+ * @deprecated Use `onLoop()` instead
248
+ */
249
+ onloop?: EventCallback;
250
+ /**
251
+ * @deprecated Use `onStateChange()` instead
252
+ */
253
+ onstatechange?: EventCallback;
254
+ }
255
+ export interface RiveLoadParameters {
256
+ src?: string;
257
+ buffer?: ArrayBuffer;
258
+ autoplay?: boolean;
259
+ artboard?: string;
260
+ animations?: string | string[];
261
+ stateMachines?: string | string[];
262
+ useOffscreenRenderer?: boolean;
263
+ shouldDisableRiveListeners?: boolean;
264
+ }
265
+ export interface RiveResetParameters {
266
+ artboard?: string;
267
+ animations?: string | string[];
268
+ stateMachines?: string | string[];
269
+ autoplay?: boolean;
270
+ }
271
+ export declare class Rive {
272
+ private readonly canvas;
273
+ private src;
274
+ private buffer;
275
+ private _layout;
276
+ private renderer;
277
+ private loaded;
278
+ /**
279
+ * Tracks if a Rive file is loaded; we need this in addition to loaded as some
280
+ * commands (e.g. contents) can be called as soon as the file is loaded.
281
+ * However, playback commands need to be queued and run in order once initial
282
+ * animations and autoplay has been sorted out. This applies to play, pause,
283
+ * and start.
284
+ */
285
+ private readyForPlaying;
286
+ private runtime;
287
+ private artboard;
288
+ private eventCleanup;
289
+ private file;
290
+ private eventManager;
291
+ private taskQueue;
292
+ private animator;
293
+ private assetLoader;
294
+ private static readonly missingErrorMessage;
295
+ private shouldDisableRiveListeners;
296
+ private automaticallyHandleEvents;
297
+ private enableRiveAssetCDN;
298
+ durations: number[];
299
+ frameTimes: number[];
300
+ frameCount: number;
301
+ constructor(params: RiveParameters);
302
+ static new(params: RiveParameters): Rive;
303
+ private init;
304
+ private setupRiveListeners;
305
+ private initData;
306
+ private initArtboard;
307
+ drawFrame(): void;
308
+ private lastRenderTime;
309
+ private frameRequestId;
310
+ /**
311
+ * Used be draw to track when a second of active rendering time has passed.
312
+ * Used for debugging purposes
313
+ */
314
+ private renderSecondTimer;
315
+ /**
316
+ * Draw rendering loop; renders animation frames at the correct time interval.
317
+ * @param time the time at which to render a frame
318
+ */
319
+ private draw;
320
+ /**
321
+ * Align the renderer
322
+ */
323
+ private alignRenderer;
324
+ get fps(): number;
325
+ get frameTime(): string | 0;
326
+ /**
327
+ * Cleans up all Wasm-generated objects that need to be manually destroyed:
328
+ * artboard instances, animation instances, state machine instances,
329
+ * renderer instance, file and runtime.
330
+ *
331
+ * Once this is called, you will need to initialise a new instance of the
332
+ * Rive class
333
+ */
334
+ cleanup(): void;
335
+ /**
336
+ * Cleans up the Renderer object. Only call this API if you no longer
337
+ * need to render Rive content in your session.
338
+ */
339
+ deleteRiveRenderer(): void;
340
+ /**
341
+ * Cleans up any Wasm-generated objects that need to be manually destroyed:
342
+ * artboard instances, animation instances, state machine instances.
343
+ *
344
+ * Once this is called, things will need to be reinitialized or bad things
345
+ * might happen.
346
+ */
347
+ cleanupInstances(): void;
348
+ /**
349
+ * Tries to query the setup Artboard for a text run node with the given name.
350
+ *
351
+ * @param textRunName - Name of the text run node associated with a text object
352
+ * @returns - TextValueRun node or undefined if the text run cannot be queried
353
+ */
354
+ private retrieveTextRun;
355
+ /**
356
+ * Returns a string from a given text run node name, or undefined if the text run
357
+ * cannot be queried.
358
+ *
359
+ * @param textRunName - Name of the text run node associated with a text object
360
+ * @returns - String value of the text run node or undefined
361
+ */
362
+ getTextRunValue(textRunName: string): string | undefined;
363
+ /**
364
+ * Sets a text value for a given text run node name if possible
365
+ *
366
+ * @param textRunName - Name of the text run node associated with a text object
367
+ * @param textRunValue - String value to set on the text run node
368
+ */
369
+ setTextRunValue(textRunName: string, textRunValue: string): void;
370
+ play(animationNames?: string | string[], autoplay?: true): void;
371
+ pause(animationNames?: string | string[]): void;
372
+ scrub(animationNames?: string | string[], value?: number): void;
373
+ stop(animationNames?: string | string[] | undefined): void;
374
+ /**
375
+ * Resets the animation
376
+ * @param artboard the name of the artboard, or default if none given
377
+ * @param animations the names of animations for playback
378
+ * @param stateMachines the names of state machines for playback
379
+ * @param autoplay whether to autoplay when reset, defaults to false
380
+ *
381
+ */
382
+ reset(params?: RiveResetParameters): void;
383
+ load(params: RiveLoadParameters): void;
384
+ set layout(layout: Layout);
385
+ /**
386
+ * Returns the current layout. Note that layout should be treated as
387
+ * immutable. If you want to change the layout, create a new one use the
388
+ * layout setter
389
+ */
390
+ get layout(): Layout;
391
+ /**
392
+ * Sets the layout bounds to the current canvas size; this is typically called
393
+ * when the canvas is resized
394
+ */
395
+ resizeToCanvas(): void;
396
+ /**
397
+ * Accounts for devicePixelRatio as a multiplier to render the size of the canvas drawing surface.
398
+ * Uses the size of the backing canvas to set new width/height attributes. Need to re-render
399
+ * and resize the layout to match the new drawing surface afterwards.
400
+ * Useful function for consumers to include in a window resize listener
401
+ */
402
+ resizeDrawingSurfaceToCanvas(customDevicePixelRatio?: number): void;
403
+ get source(): string;
404
+ /**
405
+ * Returns the name of the active artboard
406
+ */
407
+ get activeArtboard(): string;
408
+ get animationNames(): string[];
409
+ /**
410
+ * Returns a list of state machine names from the current artboard
411
+ */
412
+ get stateMachineNames(): string[];
413
+ /**
414
+ * Returns the inputs for the specified instanced state machine, or an empty
415
+ * list if the name is invalid or the state machine is not instanced
416
+ * @param name the state machine name
417
+ * @returns the inputs for the named state machine
418
+ */
419
+ stateMachineInputs(name: string): StateMachineInput[];
420
+ get playingStateMachineNames(): string[];
421
+ get playingAnimationNames(): string[];
422
+ get pausedAnimationNames(): string[];
423
+ /**
424
+ * Returns a list of paused machine names
425
+ * @returns a list of state machine names that are paused
426
+ */
427
+ get pausedStateMachineNames(): string[];
428
+ /**
429
+ * @returns true if any animation is playing
430
+ */
431
+ get isPlaying(): boolean;
432
+ /**
433
+ * @returns true if all instanced animations are paused
434
+ */
435
+ get isPaused(): boolean;
436
+ /**
437
+ * @returns true if no animations are playing or paused
438
+ */
439
+ get isStopped(): boolean;
440
+ /**
441
+ * @returns the bounds of the current artboard, or undefined if the artboard
442
+ * isn't loaded yet.
443
+ */
444
+ get bounds(): Bounds;
445
+ /**
446
+ * Subscribe to Rive-generated events
447
+ * @param type the type of event to subscribe to
448
+ * @param callback callback to fire when the event occurs
449
+ */
450
+ on(type: EventType, callback: EventCallback): void;
451
+ /**
452
+ * Unsubscribes from a Rive-generated event
453
+ * @param type the type of event to unsubscribe from
454
+ * @param callback the callback to unsubscribe
455
+ */
456
+ off(type: EventType, callback: EventCallback): void;
457
+ /**
458
+ * Unsubscribes from a Rive-generated event
459
+ * @deprecated
460
+ * @param callback the callback to unsubscribe from
461
+ */
462
+ unsubscribe(type: EventType, callback: EventCallback): void;
463
+ /**
464
+ * Unsubscribes all Rive listeners from an event type, or everything if no type is
465
+ * given
466
+ * @param type the type of event to unsubscribe from, or all types if
467
+ * undefined
468
+ */
469
+ removeAllRiveEventListeners(type?: EventType): void;
470
+ /**
471
+ * Unsubscribes all listeners from an event type, or everything if no type is
472
+ * given
473
+ * @deprecated
474
+ * @param type the type of event to unsubscribe from, or all types if
475
+ * undefined
476
+ */
477
+ unsubscribeAll(type?: EventType): void;
478
+ /**
479
+ * Stops the rendering loop; this is different from pausing in that it doesn't
480
+ * change the state of any animation. It stops rendering from occurring. This
481
+ * is designed for situations such as when Rive isn't visible.
482
+ *
483
+ * The only way to start rendering again is to call `startRendering`.
484
+ * Animations that are marked as playing will start from the position that
485
+ * they would have been at if rendering had not been stopped.
486
+ */
487
+ stopRendering(): void;
488
+ /**
489
+ * Starts the rendering loop if it has been previously stopped. If the
490
+ * renderer is already active, then this will have zero effect.
491
+ */
492
+ startRendering(): void;
493
+ /**
494
+ * Enables frames-per-second (FPS) reporting for the runtime
495
+ * If no callback is provided, Rive will append a fixed-position div at the top-right corner of
496
+ * the page with the FPS reading
497
+ * @param fpsCallback - Callback from the runtime during the RAF loop that supplies the FPS value
498
+ */
499
+ enableFPSCounter(fpsCallback?: FPSCallback): void;
500
+ /**
501
+ * Disables frames-per-second (FPS) reporting for the runtime
502
+ */
503
+ disableFPSCounter(): void;
504
+ /**
505
+ * Returns the contents of a Rive file: the artboards, animations, and state machines
506
+ */
507
+ get contents(): RiveFileContents;
508
+ }
509
+ /**
510
+ * Contents of a state machine input
511
+ */
512
+ interface StateMachineInputContents {
513
+ name: string;
514
+ type: StateMachineInputType;
515
+ initialValue?: boolean | number;
516
+ }
517
+ /**
518
+ * Contents of a state machine
519
+ */
520
+ interface StateMachineContents {
521
+ name: string;
522
+ inputs: StateMachineInputContents[];
523
+ }
524
+ /**
525
+ * Contents of an artboard
526
+ */
527
+ interface ArtboardContents {
528
+ animations: string[];
529
+ stateMachines: StateMachineContents[];
530
+ name: string;
531
+ }
532
+ /**
533
+ * contents of a Rive file
534
+ */
535
+ interface RiveFileContents {
536
+ artboards?: ArtboardContents[];
537
+ }
538
+ export declare const Testing: {
539
+ EventManager: typeof EventManager;
540
+ TaskQueueManager: typeof TaskQueueManager;
541
+ };
542
+ /**
543
+ * Decodes bytes into an image.
544
+ *
545
+ * Be sure to call `.dispose()` on the image once it is no longer needed. This
546
+ * allows the engine to clean it up when it is not used by any more animations.
547
+ */
548
+ export declare const decodeImage: (bytes: Uint8Array) => Promise<rc.Image>;
549
+ /**
550
+ * Decodes bytes into a font.
551
+ *
552
+ * Be sure to call `.dispose()` on the font once it is no longer needed. This
553
+ * allows the engine to clean it up when it is not used by any more animations.
554
+ */
555
+ export declare const decodeFont: (bytes: Uint8Array) => Promise<rc.Font>;