@rive-app/webgl 2.15.0 → 2.15.2

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.15.0",
3
+ "version": "2.15.2",
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
@@ -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;
package/rive.js CHANGED
@@ -4109,7 +4109,7 @@ Ie();
4109
4109
  /* 2 */
4110
4110
  /***/ ((module) => {
4111
4111
 
4112
- module.exports = JSON.parse('{"name":"@rive-app/webgl","version":"2.15.0","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.2","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}}');
4113
4113
 
4114
4114
  /***/ }),
4115
4115
  /* 3 */
@@ -5532,6 +5532,60 @@ var AudioManager = /** @class */ (function (_super) {
5532
5532
  return AudioManager;
5533
5533
  }(EventManager));
5534
5534
  var audioManager = new AudioManager();
5535
+ var FakeResizeObserver = /** @class */ (function () {
5536
+ function FakeResizeObserver() {
5537
+ }
5538
+ FakeResizeObserver.prototype.observe = function () { };
5539
+ FakeResizeObserver.prototype.unobserve = function () { };
5540
+ FakeResizeObserver.prototype.disconnect = function () { };
5541
+ return FakeResizeObserver;
5542
+ }());
5543
+ var MyResizeObserver = globalThis.ResizeObserver || FakeResizeObserver;
5544
+ /**
5545
+ * This class takes care of any observers that will be attached to an animation.
5546
+ * It should be treated as a singleton because observers are much more performant
5547
+ * when used for observing multiple elements by a single instance.
5548
+ */
5549
+ var ObjectObservers = /** @class */ (function () {
5550
+ function ObjectObservers() {
5551
+ var _this = this;
5552
+ this._elementsMap = new Map();
5553
+ /**
5554
+ * Resize observers trigger both when the element changes its size and also when the
5555
+ * element is added or removed from the document.
5556
+ */
5557
+ this._onObservedEntry = function (entry) {
5558
+ var observed = _this._elementsMap.get(entry.target);
5559
+ if (observed !== null) {
5560
+ observed.onResize(entry.target.clientWidth == 0 || entry.target.clientHeight == 0);
5561
+ }
5562
+ else {
5563
+ _this._resizeObserver.unobserve(entry.target);
5564
+ }
5565
+ };
5566
+ this._onObserved = function (entries) {
5567
+ entries.forEach(_this._onObservedEntry);
5568
+ };
5569
+ this._resizeObserver = new MyResizeObserver(this._onObserved);
5570
+ }
5571
+ // Adds an observable element
5572
+ ObjectObservers.prototype.add = function (element, onResize) {
5573
+ var observed = {
5574
+ onResize: onResize,
5575
+ element: element,
5576
+ };
5577
+ this._elementsMap.set(element, observed);
5578
+ this._resizeObserver.observe(element);
5579
+ return observed;
5580
+ };
5581
+ // Removes an observable element
5582
+ ObjectObservers.prototype.remove = function (observed) {
5583
+ this._resizeObserver.unobserve(observed.element);
5584
+ this._elementsMap.delete(observed.element);
5585
+ };
5586
+ return ObjectObservers;
5587
+ }());
5588
+ var observers = new ObjectObservers();
5535
5589
  var Rive = /** @class */ (function () {
5536
5590
  function Rive(params) {
5537
5591
  var _this = this;
@@ -5556,17 +5610,28 @@ var Rive = /** @class */ (function () {
5556
5610
  this.enableRiveAssetCDN = true;
5557
5611
  // Keep a local value of the set volume to update it asynchronously
5558
5612
  this._volume = 1;
5613
+ // Whether the canvas element's size is 0
5614
+ this._hasZeroSize = false;
5559
5615
  // Durations to generate a frame for the last second. Used for performance profiling.
5560
5616
  this.durations = [];
5561
5617
  this.frameTimes = [];
5562
5618
  this.frameCount = 0;
5563
5619
  this.isTouchScrollEnabled = false;
5620
+ this.onCanvasResize = function (hasZeroSize) {
5621
+ _this._hasZeroSize = hasZeroSize;
5622
+ if (!_this._layout.maxX || !_this._layout.maxY) {
5623
+ _this.resizeToCanvas();
5624
+ }
5625
+ };
5564
5626
  /**
5565
5627
  * Used be draw to track when a second of active rendering time has passed.
5566
5628
  * Used for debugging purposes
5567
5629
  */
5568
5630
  this.renderSecondTimer = 0;
5569
5631
  this.canvas = params.canvas;
5632
+ if (params.canvas.constructor === HTMLCanvasElement) {
5633
+ this._observed = observers.add(this.canvas, this.onCanvasResize);
5634
+ }
5570
5635
  this.src = params.src;
5571
5636
  this.buffer = params.buffer;
5572
5637
  this.layout = (_a = params.layout) !== null && _a !== void 0 ? _a : new Layout();
@@ -5619,14 +5684,6 @@ var Rive = /** @class */ (function () {
5619
5684
  this.assetLoader = params.assetLoader;
5620
5685
  // Hook up the task queue
5621
5686
  this.taskQueue = new TaskQueueManager(this.eventManager);
5622
- // Initialize audio
5623
- if (audioManager.status == SystemAudioStatus.UNAVAILABLE) {
5624
- audioManager.add({
5625
- type: EventType.AudioStatusChange,
5626
- callback: function () { return _this.onSystemAudioChanged(); },
5627
- });
5628
- audioManager.establishAudio();
5629
- }
5630
5687
  this.init({
5631
5688
  src: this.src,
5632
5689
  buffer: this.buffer,
@@ -5722,6 +5779,24 @@ var Rive = /** @class */ (function () {
5722
5779
  this.eventCleanup();
5723
5780
  }
5724
5781
  };
5782
+ /**
5783
+ * If the instance has audio and the system audio is not ready
5784
+ * we hook the instance to the audio manager
5785
+ */
5786
+ Rive.prototype.initializeAudio = function () {
5787
+ var _this = this;
5788
+ var _a;
5789
+ // Initialize audio if needed
5790
+ if (audioManager.status == SystemAudioStatus.UNAVAILABLE) {
5791
+ if ((_a = this.artboard) === null || _a === void 0 ? void 0 : _a.hasAudio) {
5792
+ audioManager.add({
5793
+ type: EventType.AudioStatusChange,
5794
+ callback: function () { return _this.onSystemAudioChanged(); },
5795
+ });
5796
+ audioManager.establishAudio();
5797
+ }
5798
+ }
5799
+ };
5725
5800
  // Initializes runtime with Rive data and preps for playing
5726
5801
  Rive.prototype.initData = function (artboardName, animationNames, stateMachineNames, autoplay) {
5727
5802
  var _a;
@@ -5751,6 +5826,8 @@ var Rive = /** @class */ (function () {
5751
5826
  if (this.file) {
5752
5827
  // Initialize and draw frame
5753
5828
  this.initArtboard(artboardName, animationNames, stateMachineNames, autoplay);
5829
+ // Check for audio
5830
+ this.initializeAudio();
5754
5831
  // Everything's set up, emit a load event
5755
5832
  this.loaded = true;
5756
5833
  this.eventManager.fire({
@@ -5828,9 +5905,9 @@ var Rive = /** @class */ (function () {
5828
5905
  * @param time the time at which to render a frame
5829
5906
  */
5830
5907
  Rive.prototype.draw = function (time, onSecond) {
5831
- var before = performance.now();
5832
5908
  // Clear the frameRequestId, as we're now rendering a fresh frame
5833
5909
  this.frameRequestId = null;
5910
+ var before = performance.now();
5834
5911
  // On the first pass, make sure lastTime has a valid value
5835
5912
  if (!this.lastRenderTime) {
5836
5913
  this.lastRenderTime = time;
@@ -5909,7 +5986,10 @@ var Rive = /** @class */ (function () {
5909
5986
  renderer.save();
5910
5987
  // Update the renderer alignment if necessary
5911
5988
  this.alignRenderer();
5912
- this.artboard.draw(renderer);
5989
+ // Do not draw on 0 canvas size
5990
+ if (!this._hasZeroSize) {
5991
+ this.artboard.draw(renderer);
5992
+ }
5913
5993
  renderer.restore();
5914
5994
  renderer.flush();
5915
5995
  // Check for any animations that looped
@@ -5989,6 +6069,10 @@ var Rive = /** @class */ (function () {
5989
6069
  this.stopRendering();
5990
6070
  // Clean up any artboard, animation or state machine instances.
5991
6071
  this.cleanupInstances();
6072
+ // Remove from observer
6073
+ if (this._observed !== null) {
6074
+ observers.remove(this._observed);
6075
+ }
5992
6076
  // Delete the rive file
5993
6077
  (_a = this.file) === null || _a === void 0 ? void 0 : _a.delete();
5994
6078
  this.file = null;