@rive-app/canvas-lite 0.1.1

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