@rive-app/webgl 2.14.4 → 2.15.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rive-app/webgl",
3
- "version": "2.14.4",
3
+ "version": "2.15.1",
4
4
  "description": "Rive's webgl based web api.",
5
5
  "main": "rive.js",
6
6
  "homepage": "https://rive.app",
package/rive.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as rc from "./rive_advanced.mjs";
2
- export type { FileAsset, FontAsset, ImageAsset } from "./rive_advanced.mjs";
2
+ export type { FileAsset, AudioAsset, FontAsset, ImageAsset, } from "./rive_advanced.mjs";
3
3
  /**
4
4
  * Generic type for a parameterless void callback
5
5
  */
@@ -285,6 +285,7 @@ export declare class Rive {
285
285
  private _layout;
286
286
  private renderer;
287
287
  private loaded;
288
+ private _observed;
288
289
  /**
289
290
  * Tracks if a Rive file is loaded; we need this in addition to loaded as some
290
291
  * commands (e.g. contents) can be called as soon as the file is loaded.
@@ -306,6 +307,7 @@ export declare class Rive {
306
307
  private automaticallyHandleEvents;
307
308
  private enableRiveAssetCDN;
308
309
  private _volume;
310
+ private _hasZeroSize;
309
311
  durations: number[];
310
312
  frameTimes: number[];
311
313
  frameCount: number;
@@ -313,6 +315,7 @@ export declare class Rive {
313
315
  constructor(params: RiveParameters);
314
316
  static new(params: RiveParameters): Rive;
315
317
  private onSystemAudioChanged;
318
+ private onCanvasResize;
316
319
  private init;
317
320
  /**
318
321
  * Setup Rive Listeners on the canvas
@@ -325,6 +328,11 @@ export declare class Rive {
325
328
  * Remove Rive Listeners setup on the canvas
326
329
  */
327
330
  removeRiveListeners(): void;
331
+ /**
332
+ * If the instance has audio and the system audio is not ready
333
+ * we hook the instance to the audio manager
334
+ */
335
+ private initializeAudio;
328
336
  private initData;
329
337
  private initArtboard;
330
338
  drawFrame(): void;
@@ -567,17 +575,24 @@ export declare const Testing: {
567
575
  EventManager: typeof EventManager;
568
576
  TaskQueueManager: typeof TaskQueueManager;
569
577
  };
578
+ /**
579
+ * Decodes bytes into an audio asset.
580
+ *
581
+ * Be sure to call `.unref()` on the audio once it is no longer needed. This
582
+ * allows the engine to clean it up when it is not used by any more animations.
583
+ */
584
+ export declare const decodeAudio: (bytes: Uint8Array) => Promise<rc.Audio>;
570
585
  /**
571
586
  * Decodes bytes into an image.
572
587
  *
573
- * Be sure to call `.dispose()` on the image once it is no longer needed. This
588
+ * Be sure to call `.unref()` on the image once it is no longer needed. This
574
589
  * allows the engine to clean it up when it is not used by any more animations.
575
590
  */
576
591
  export declare const decodeImage: (bytes: Uint8Array) => Promise<rc.Image>;
577
592
  /**
578
593
  * Decodes bytes into a font.
579
594
  *
580
- * Be sure to call `.dispose()` on the font once it is no longer needed. This
595
+ * Be sure to call `.unref()` on the font once it is no longer needed. This
581
596
  * allows the engine to clean it up when it is not used by any more animations.
582
597
  */
583
598
  export declare const decodeFont: (bytes: Uint8Array) => Promise<rc.Font>;
package/rive.js CHANGED
@@ -91,52 +91,57 @@ function da() {
91
91
  const ea = m.onRuntimeInitialized;
92
92
  m.onRuntimeInitialized = function() {
93
93
  ea && ea();
94
- let a = m.decodeFont;
95
- m.decodeFont = function(c, d) {
96
- c = a(c);
97
- d(c);
94
+ let a = m.decodeAudio;
95
+ m.decodeAudio = function(d, e) {
96
+ d = a(d);
97
+ e(d);
98
98
  };
99
- const b = m.FileAssetLoader;
100
- m.ptrToAsset = c => {
101
- let d = m.ptrToFileAsset(c);
102
- return d.isImage ? m.ptrToImageAsset(c) : d.isFont ? m.ptrToFontAsset(c) : d;
99
+ let b = m.decodeFont;
100
+ m.decodeFont = function(d, e) {
101
+ d = b(d);
102
+ e(d);
103
103
  };
104
- m.CustomFileAssetLoader = b.extend("CustomFileAssetLoader", {__construct:function({loadContents:c}) {
104
+ const c = m.FileAssetLoader;
105
+ m.ptrToAsset = d => {
106
+ let e = m.ptrToFileAsset(d);
107
+ return e.isImage ? m.ptrToImageAsset(d) : e.isFont ? m.ptrToFontAsset(d) : e.isAudio ? m.ptrToAudioAsset(d) : e;
108
+ };
109
+ m.CustomFileAssetLoader = c.extend("CustomFileAssetLoader", {__construct:function({loadContents:d}) {
105
110
  this.__parent.__construct.call(this);
106
- this.Cb = c;
107
- }, loadContents:function(c, d) {
108
- c = m.ptrToAsset(c);
109
- return this.Cb(c, d);
111
+ this.Cb = d;
112
+ }, loadContents:function(d, e) {
113
+ d = m.ptrToAsset(d);
114
+ return this.Cb(d, e);
110
115
  },});
111
- m.CDNFileAssetLoader = b.extend("CDNFileAssetLoader", {__construct:function() {
116
+ m.CDNFileAssetLoader = c.extend("CDNFileAssetLoader", {__construct:function() {
112
117
  this.__parent.__construct.call(this);
113
- }, loadContents:function(c) {
114
- let d = m.ptrToAsset(c);
115
- c = d.cdnUuid;
116
- if ("" === c) {
118
+ }, loadContents:function(d) {
119
+ let e = m.ptrToAsset(d);
120
+ d = e.cdnUuid;
121
+ if ("" === d) {
117
122
  return !1;
118
123
  }
119
- (function(e, f) {
120
- var g = new XMLHttpRequest();
121
- g.responseType = "arraybuffer";
122
- g.onreadystatechange = function() {
123
- 4 == g.readyState && 200 == g.status && f(g);
124
+ (function(f, g) {
125
+ var k = new XMLHttpRequest();
126
+ k.responseType = "arraybuffer";
127
+ k.onreadystatechange = function() {
128
+ 4 == k.readyState && 200 == k.status && g(k);
124
129
  };
125
- g.open("GET", e, !0);
126
- g.send(null);
127
- })(d.cdnBaseUrl + "/" + c, e => {
128
- d.decode(new Uint8Array(e.response));
130
+ k.open("GET", f, !0);
131
+ k.send(null);
132
+ })(e.cdnBaseUrl + "/" + d, f => {
133
+ e.decode(new Uint8Array(f.response));
129
134
  });
130
135
  return !0;
131
136
  },});
132
- m.FallbackFileAssetLoader = b.extend("FallbackFileAssetLoader", {__construct:function() {
137
+ m.FallbackFileAssetLoader = c.extend("FallbackFileAssetLoader", {__construct:function() {
133
138
  this.__parent.__construct.call(this);
134
139
  this.gb = [];
135
- }, addLoader:function(c) {
136
- this.gb.push(c);
137
- }, loadContents:function(c, d) {
138
- for (let e of this.gb) {
139
- if (e.loadContents(c, d)) {
140
+ }, addLoader:function(d) {
141
+ this.gb.push(d);
142
+ }, loadContents:function(d, e) {
143
+ for (let f of this.gb) {
144
+ if (f.loadContents(d, e)) {
140
145
  return !0;
141
146
  }
142
147
  }
@@ -395,7 +400,7 @@ function Ra(a, b) {
395
400
  return Qa(c, a, b);
396
401
  }));
397
402
  }
398
- var E, F, Va = {688684:(a, b, c, d, e) => {
403
+ var E, F, Va = {688828:(a, b, c, d, e) => {
399
404
  if ("undefined" === typeof window || void 0 === (window.AudioContext || window.webkitAudioContext)) {
400
405
  return 0;
401
406
  }
@@ -458,9 +463,9 @@ var E, F, Va = {688684:(a, b, c, d, e) => {
458
463
  }
459
464
  window.h.Ea += 1;
460
465
  return 1;
461
- }, 690862:() => {
466
+ }, 691006:() => {
462
467
  "undefined" !== typeof window.h && (--window.h.Ea, 0 === window.h.Ea && delete window.h);
463
- }, 691026:() => void 0 !== navigator.mediaDevices && void 0 !== navigator.mediaDevices.getUserMedia, 691130:() => {
468
+ }, 691170:() => void 0 !== navigator.mediaDevices && void 0 !== navigator.mediaDevices.getUserMedia, 691274:() => {
464
469
  try {
465
470
  var a = new (window.AudioContext || window.webkitAudioContext)(), b = a.sampleRate;
466
471
  a.close();
@@ -468,7 +473,7 @@ var E, F, Va = {688684:(a, b, c, d, e) => {
468
473
  } catch (c) {
469
474
  return 0;
470
475
  }
471
- }, 691301:(a, b, c, d, e, f) => {
476
+ }, 691445:(a, b, c, d, e, f) => {
472
477
  if ("undefined" === typeof window.h) {
473
478
  return -1;
474
479
  }
@@ -514,7 +519,7 @@ var E, F, Va = {688684:(a, b, c, d, e) => {
514
519
  a == window.h.H.Da && g.Y.connect(g.I.destination);
515
520
  g.mb = f;
516
521
  return window.h.vc(g);
517
- }, 694178:a => window.h.ua(a).I.sampleRate, 694251:a => {
522
+ }, 694322:a => window.h.ua(a).I.sampleRate, 694395:a => {
518
523
  a = window.h.ua(a);
519
524
  void 0 !== a.Y && (a.Y.onaudioprocess = function() {
520
525
  }, a.Y.disconnect(), a.Y = void 0);
@@ -522,13 +527,13 @@ var E, F, Va = {688684:(a, b, c, d, e) => {
522
527
  a.I.close();
523
528
  a.I = void 0;
524
529
  a.mb = void 0;
525
- }, 694651:a => {
530
+ }, 694795:a => {
526
531
  window.h.yb(a);
527
- }, 694701:a => {
532
+ }, 694845:a => {
528
533
  a = window.h.ua(a);
529
534
  a.I.resume();
530
535
  a.state = window.h.ha.tb;
531
- }, 694840:a => {
536
+ }, 694984:a => {
532
537
  a = window.h.ua(a);
533
538
  a.I.suspend();
534
539
  a.state = window.h.ha.stopped;
@@ -3842,7 +3847,7 @@ var Fe = {__syscall_fcntl64:function(a, b, c) {
3842
3847
  }).catch(ba);
3843
3848
  return {};
3844
3849
  })();
3845
- var Yd = a => (Yd = p.malloc)(a), tc = a => (tc = p.free)(a), Sa = m._ma_device__on_notification_unlocked = a => (Sa = m._ma_device__on_notification_unlocked = p.ma_device__on_notification_unlocked)(a);
3850
+ var tc = a => (tc = p.free)(a), Yd = a => (Yd = p.malloc)(a), Sa = m._ma_device__on_notification_unlocked = a => (Sa = m._ma_device__on_notification_unlocked = p.ma_device__on_notification_unlocked)(a);
3846
3851
  m._ma_malloc_emscripten = (a, b) => (m._ma_malloc_emscripten = p.ma_malloc_emscripten)(a, b);
3847
3852
  m._ma_free_emscripten = (a, b) => (m._ma_free_emscripten = p.ma_free_emscripten)(a, b);
3848
3853
  var Ta = m._ma_device_process_pcm_frames_capture__webaudio = (a, b, c) => (Ta = m._ma_device_process_pcm_frames_capture__webaudio = p.ma_device_process_pcm_frames_capture__webaudio)(a, b, c), Ua = m._ma_device_process_pcm_frames_playback__webaudio = (a, b, c) => (Ua = m._ma_device_process_pcm_frames_playback__webaudio = p.ma_device_process_pcm_frames_playback__webaudio)(a, b, c), pe = () => (pe = p.__errno_location)(), sc = a => (sc = p.__getTypeName)(a);
@@ -4104,7 +4109,7 @@ Ie();
4104
4109
  /* 2 */
4105
4110
  /***/ ((module) => {
4106
4111
 
4107
- module.exports = JSON.parse('{"name":"@rive-app/webgl","version":"2.14.4","description":"Rive\'s webgl based web api.","main":"rive.js","homepage":"https://rive.app","repository":{"type":"git","url":"https://github.com/rive-app/rive-wasm/tree/master/js"},"keywords":["rive","animation"],"author":"Rive","contributors":["Luigi Rosso <luigi@rive.app> (https://rive.app)","Maxwell Talbot <max@rive.app> (https://rive.app)","Arthur Vivian <arthur@rive.app> (https://rive.app)","Umberto Sonnino <umberto@rive.app> (https://rive.app)","Matthew Sullivan <matt.j.sullivan@gmail.com> (mailto:matt.j.sullivan@gmail.com)"],"license":"MIT","files":["rive.js","rive.wasm","rive.js.map","rive.d.ts","rive_advanced.mjs.d.ts"],"typings":"rive.d.ts","dependencies":{},"browser":{"fs":false,"path":false}}');
4112
+ module.exports = JSON.parse('{"name":"@rive-app/webgl","version":"2.15.1","description":"Rive\'s webgl based web api.","main":"rive.js","homepage":"https://rive.app","repository":{"type":"git","url":"https://github.com/rive-app/rive-wasm/tree/master/js"},"keywords":["rive","animation"],"author":"Rive","contributors":["Luigi Rosso <luigi@rive.app> (https://rive.app)","Maxwell Talbot <max@rive.app> (https://rive.app)","Arthur Vivian <arthur@rive.app> (https://rive.app)","Umberto Sonnino <umberto@rive.app> (https://rive.app)","Matthew Sullivan <matt.j.sullivan@gmail.com> (mailto:matt.j.sullivan@gmail.com)"],"license":"MIT","files":["rive.js","rive.wasm","rive.js.map","rive.d.ts","rive_advanced.mjs.d.ts"],"typings":"rive.d.ts","dependencies":{},"browser":{"fs":false,"path":false}}');
4108
4113
 
4109
4114
  /***/ }),
4110
4115
  /* 3 */
@@ -4402,6 +4407,7 @@ __webpack_require__.r(__webpack_exports__);
4402
4407
  /* harmony export */ StateMachineInput: () => (/* binding */ StateMachineInput),
4403
4408
  /* harmony export */ StateMachineInputType: () => (/* binding */ StateMachineInputType),
4404
4409
  /* harmony export */ Testing: () => (/* binding */ Testing),
4410
+ /* harmony export */ decodeAudio: () => (/* binding */ decodeAudio),
4405
4411
  /* harmony export */ decodeFont: () => (/* binding */ decodeFont),
4406
4412
  /* harmony export */ decodeImage: () => (/* binding */ decodeImage)
4407
4413
  /* harmony export */ });
@@ -5526,6 +5532,51 @@ var AudioManager = /** @class */ (function (_super) {
5526
5532
  return AudioManager;
5527
5533
  }(EventManager));
5528
5534
  var audioManager = new AudioManager();
5535
+ /**
5536
+ * This class takes care of any observers that will be attached to an animation.
5537
+ * It should be treated as a singleton because observers are much more performant
5538
+ * when used for observing multiple elements by a single instance.
5539
+ */
5540
+ var ObjectObservers = /** @class */ (function () {
5541
+ function ObjectObservers() {
5542
+ var _this = this;
5543
+ this._elementsMap = new Map();
5544
+ /**
5545
+ * Resize observers trigger both when the element changes its size and also when the
5546
+ * element is added or removed from the document.
5547
+ */
5548
+ this._onObservedEntry = function (entry) {
5549
+ var observed = _this._elementsMap.get(entry.target);
5550
+ if (observed !== null) {
5551
+ observed.onResize(entry.target.clientWidth == 0 || entry.target.clientHeight == 0);
5552
+ }
5553
+ else {
5554
+ _this._resizeObserver.unobserve(entry.target);
5555
+ }
5556
+ };
5557
+ this._onObserved = function (entries) {
5558
+ entries.forEach(_this._onObservedEntry);
5559
+ };
5560
+ this._resizeObserver = new ResizeObserver(this._onObserved);
5561
+ }
5562
+ // Adds an observable element
5563
+ ObjectObservers.prototype.add = function (element, onResize) {
5564
+ var observed = {
5565
+ onResize: onResize,
5566
+ element: element,
5567
+ };
5568
+ this._elementsMap.set(element, observed);
5569
+ this._resizeObserver.observe(element);
5570
+ return observed;
5571
+ };
5572
+ // Removes an observable element
5573
+ ObjectObservers.prototype.remove = function (observed) {
5574
+ this._resizeObserver.unobserve(observed.element);
5575
+ this._elementsMap.delete(observed.element);
5576
+ };
5577
+ return ObjectObservers;
5578
+ }());
5579
+ var observers = new ObjectObservers();
5529
5580
  var Rive = /** @class */ (function () {
5530
5581
  function Rive(params) {
5531
5582
  var _this = this;
@@ -5550,17 +5601,28 @@ var Rive = /** @class */ (function () {
5550
5601
  this.enableRiveAssetCDN = true;
5551
5602
  // Keep a local value of the set volume to update it asynchronously
5552
5603
  this._volume = 1;
5604
+ // Whether the canvas element's size is 0
5605
+ this._hasZeroSize = false;
5553
5606
  // Durations to generate a frame for the last second. Used for performance profiling.
5554
5607
  this.durations = [];
5555
5608
  this.frameTimes = [];
5556
5609
  this.frameCount = 0;
5557
5610
  this.isTouchScrollEnabled = false;
5611
+ this.onCanvasResize = function (hasZeroSize) {
5612
+ _this._hasZeroSize = hasZeroSize;
5613
+ if (!_this._layout.maxX || !_this._layout.maxY) {
5614
+ _this.resizeToCanvas();
5615
+ }
5616
+ };
5558
5617
  /**
5559
5618
  * Used be draw to track when a second of active rendering time has passed.
5560
5619
  * Used for debugging purposes
5561
5620
  */
5562
5621
  this.renderSecondTimer = 0;
5563
5622
  this.canvas = params.canvas;
5623
+ if (params.canvas.constructor === HTMLCanvasElement) {
5624
+ this._observed = observers.add(this.canvas, this.onCanvasResize);
5625
+ }
5564
5626
  this.src = params.src;
5565
5627
  this.buffer = params.buffer;
5566
5628
  this.layout = (_a = params.layout) !== null && _a !== void 0 ? _a : new Layout();
@@ -5613,14 +5675,6 @@ var Rive = /** @class */ (function () {
5613
5675
  this.assetLoader = params.assetLoader;
5614
5676
  // Hook up the task queue
5615
5677
  this.taskQueue = new TaskQueueManager(this.eventManager);
5616
- // Initialize audio
5617
- if (audioManager.status == SystemAudioStatus.UNAVAILABLE) {
5618
- audioManager.add({
5619
- type: EventType.AudioStatusChange,
5620
- callback: function () { return _this.onSystemAudioChanged(); },
5621
- });
5622
- audioManager.establishAudio();
5623
- }
5624
5678
  this.init({
5625
5679
  src: this.src,
5626
5680
  buffer: this.buffer,
@@ -5716,6 +5770,24 @@ var Rive = /** @class */ (function () {
5716
5770
  this.eventCleanup();
5717
5771
  }
5718
5772
  };
5773
+ /**
5774
+ * If the instance has audio and the system audio is not ready
5775
+ * we hook the instance to the audio manager
5776
+ */
5777
+ Rive.prototype.initializeAudio = function () {
5778
+ var _this = this;
5779
+ var _a;
5780
+ // Initialize audio if needed
5781
+ if (audioManager.status == SystemAudioStatus.UNAVAILABLE) {
5782
+ if ((_a = this.artboard) === null || _a === void 0 ? void 0 : _a.hasAudio) {
5783
+ audioManager.add({
5784
+ type: EventType.AudioStatusChange,
5785
+ callback: function () { return _this.onSystemAudioChanged(); },
5786
+ });
5787
+ audioManager.establishAudio();
5788
+ }
5789
+ }
5790
+ };
5719
5791
  // Initializes runtime with Rive data and preps for playing
5720
5792
  Rive.prototype.initData = function (artboardName, animationNames, stateMachineNames, autoplay) {
5721
5793
  var _a;
@@ -5745,6 +5817,8 @@ var Rive = /** @class */ (function () {
5745
5817
  if (this.file) {
5746
5818
  // Initialize and draw frame
5747
5819
  this.initArtboard(artboardName, animationNames, stateMachineNames, autoplay);
5820
+ // Check for audio
5821
+ this.initializeAudio();
5748
5822
  // Everything's set up, emit a load event
5749
5823
  this.loaded = true;
5750
5824
  this.eventManager.fire({
@@ -5822,9 +5896,9 @@ var Rive = /** @class */ (function () {
5822
5896
  * @param time the time at which to render a frame
5823
5897
  */
5824
5898
  Rive.prototype.draw = function (time, onSecond) {
5825
- var before = performance.now();
5826
5899
  // Clear the frameRequestId, as we're now rendering a fresh frame
5827
5900
  this.frameRequestId = null;
5901
+ var before = performance.now();
5828
5902
  // On the first pass, make sure lastTime has a valid value
5829
5903
  if (!this.lastRenderTime) {
5830
5904
  this.lastRenderTime = time;
@@ -5903,7 +5977,10 @@ var Rive = /** @class */ (function () {
5903
5977
  renderer.save();
5904
5978
  // Update the renderer alignment if necessary
5905
5979
  this.alignRenderer();
5906
- this.artboard.draw(renderer);
5980
+ // Do not draw on 0 canvas size
5981
+ if (!this._hasZeroSize) {
5982
+ this.artboard.draw(renderer);
5983
+ }
5907
5984
  renderer.restore();
5908
5985
  renderer.flush();
5909
5986
  // Check for any animations that looped
@@ -5983,6 +6060,10 @@ var Rive = /** @class */ (function () {
5983
6060
  this.stopRendering();
5984
6061
  // Clean up any artboard, animation or state machine instances.
5985
6062
  this.cleanupInstances();
6063
+ // Remove from observer
6064
+ if (this._observed !== null) {
6065
+ observers.remove(this._observed);
6066
+ }
5986
6067
  // Delete the rive file
5987
6068
  (_a = this.file) === null || _a === void 0 ? void 0 : _a.delete();
5988
6069
  this.file = null;
@@ -6576,10 +6657,23 @@ var Testing = {
6576
6657
  };
6577
6658
  // #endregion
6578
6659
  // #region asset loaders
6660
+ /**
6661
+ * Decodes bytes into an audio asset.
6662
+ *
6663
+ * Be sure to call `.unref()` on the audio once it is no longer needed. This
6664
+ * allows the engine to clean it up when it is not used by any more animations.
6665
+ */
6666
+ var decodeAudio = function (bytes) {
6667
+ return new Promise(function (resolve) {
6668
+ return RuntimeLoader.getInstance(function (rive) {
6669
+ rive.decodeAudio(bytes, resolve);
6670
+ });
6671
+ });
6672
+ };
6579
6673
  /**
6580
6674
  * Decodes bytes into an image.
6581
6675
  *
6582
- * Be sure to call `.dispose()` on the image once it is no longer needed. This
6676
+ * Be sure to call `.unref()` on the image once it is no longer needed. This
6583
6677
  * allows the engine to clean it up when it is not used by any more animations.
6584
6678
  */
6585
6679
  var decodeImage = function (bytes) {
@@ -6592,7 +6686,7 @@ var decodeImage = function (bytes) {
6592
6686
  /**
6593
6687
  * Decodes bytes into a font.
6594
6688
  *
6595
- * Be sure to call `.dispose()` on the font once it is no longer needed. This
6689
+ * Be sure to call `.unref()` on the font once it is no longer needed. This
6596
6690
  * allows the engine to clean it up when it is not used by any more animations.
6597
6691
  */
6598
6692
  var decodeFont = function (bytes) {