@rive-app/canvas 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/canvas",
3
- "version": "2.15.0",
3
+ "version": "2.15.2",
4
4
  "description": "Rive's canvas 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
@@ -3409,7 +3409,7 @@ Zd();
3409
3409
  /* 2 */
3410
3410
  /***/ ((module) => {
3411
3411
 
3412
- module.exports = JSON.parse('{"name":"@rive-app/canvas","version":"2.15.0","description":"Rive\'s canvas 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.js.map","rive.wasm","rive.d.ts","rive_advanced.mjs.d.ts"],"typings":"rive.d.ts","dependencies":{},"browser":{"fs":false,"path":false}}');
3412
+ module.exports = JSON.parse('{"name":"@rive-app/canvas","version":"2.15.2","description":"Rive\'s canvas 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.js.map","rive.wasm","rive.d.ts","rive_advanced.mjs.d.ts"],"typings":"rive.d.ts","dependencies":{},"browser":{"fs":false,"path":false}}');
3413
3413
 
3414
3414
  /***/ }),
3415
3415
  /* 3 */
@@ -4832,6 +4832,60 @@ var AudioManager = /** @class */ (function (_super) {
4832
4832
  return AudioManager;
4833
4833
  }(EventManager));
4834
4834
  var audioManager = new AudioManager();
4835
+ var FakeResizeObserver = /** @class */ (function () {
4836
+ function FakeResizeObserver() {
4837
+ }
4838
+ FakeResizeObserver.prototype.observe = function () { };
4839
+ FakeResizeObserver.prototype.unobserve = function () { };
4840
+ FakeResizeObserver.prototype.disconnect = function () { };
4841
+ return FakeResizeObserver;
4842
+ }());
4843
+ var MyResizeObserver = globalThis.ResizeObserver || FakeResizeObserver;
4844
+ /**
4845
+ * This class takes care of any observers that will be attached to an animation.
4846
+ * It should be treated as a singleton because observers are much more performant
4847
+ * when used for observing multiple elements by a single instance.
4848
+ */
4849
+ var ObjectObservers = /** @class */ (function () {
4850
+ function ObjectObservers() {
4851
+ var _this = this;
4852
+ this._elementsMap = new Map();
4853
+ /**
4854
+ * Resize observers trigger both when the element changes its size and also when the
4855
+ * element is added or removed from the document.
4856
+ */
4857
+ this._onObservedEntry = function (entry) {
4858
+ var observed = _this._elementsMap.get(entry.target);
4859
+ if (observed !== null) {
4860
+ observed.onResize(entry.target.clientWidth == 0 || entry.target.clientHeight == 0);
4861
+ }
4862
+ else {
4863
+ _this._resizeObserver.unobserve(entry.target);
4864
+ }
4865
+ };
4866
+ this._onObserved = function (entries) {
4867
+ entries.forEach(_this._onObservedEntry);
4868
+ };
4869
+ this._resizeObserver = new MyResizeObserver(this._onObserved);
4870
+ }
4871
+ // Adds an observable element
4872
+ ObjectObservers.prototype.add = function (element, onResize) {
4873
+ var observed = {
4874
+ onResize: onResize,
4875
+ element: element,
4876
+ };
4877
+ this._elementsMap.set(element, observed);
4878
+ this._resizeObserver.observe(element);
4879
+ return observed;
4880
+ };
4881
+ // Removes an observable element
4882
+ ObjectObservers.prototype.remove = function (observed) {
4883
+ this._resizeObserver.unobserve(observed.element);
4884
+ this._elementsMap.delete(observed.element);
4885
+ };
4886
+ return ObjectObservers;
4887
+ }());
4888
+ var observers = new ObjectObservers();
4835
4889
  var Rive = /** @class */ (function () {
4836
4890
  function Rive(params) {
4837
4891
  var _this = this;
@@ -4856,17 +4910,28 @@ var Rive = /** @class */ (function () {
4856
4910
  this.enableRiveAssetCDN = true;
4857
4911
  // Keep a local value of the set volume to update it asynchronously
4858
4912
  this._volume = 1;
4913
+ // Whether the canvas element's size is 0
4914
+ this._hasZeroSize = false;
4859
4915
  // Durations to generate a frame for the last second. Used for performance profiling.
4860
4916
  this.durations = [];
4861
4917
  this.frameTimes = [];
4862
4918
  this.frameCount = 0;
4863
4919
  this.isTouchScrollEnabled = false;
4920
+ this.onCanvasResize = function (hasZeroSize) {
4921
+ _this._hasZeroSize = hasZeroSize;
4922
+ if (!_this._layout.maxX || !_this._layout.maxY) {
4923
+ _this.resizeToCanvas();
4924
+ }
4925
+ };
4864
4926
  /**
4865
4927
  * Used be draw to track when a second of active rendering time has passed.
4866
4928
  * Used for debugging purposes
4867
4929
  */
4868
4930
  this.renderSecondTimer = 0;
4869
4931
  this.canvas = params.canvas;
4932
+ if (params.canvas.constructor === HTMLCanvasElement) {
4933
+ this._observed = observers.add(this.canvas, this.onCanvasResize);
4934
+ }
4870
4935
  this.src = params.src;
4871
4936
  this.buffer = params.buffer;
4872
4937
  this.layout = (_a = params.layout) !== null && _a !== void 0 ? _a : new Layout();
@@ -4919,14 +4984,6 @@ var Rive = /** @class */ (function () {
4919
4984
  this.assetLoader = params.assetLoader;
4920
4985
  // Hook up the task queue
4921
4986
  this.taskQueue = new TaskQueueManager(this.eventManager);
4922
- // Initialize audio
4923
- if (audioManager.status == SystemAudioStatus.UNAVAILABLE) {
4924
- audioManager.add({
4925
- type: EventType.AudioStatusChange,
4926
- callback: function () { return _this.onSystemAudioChanged(); },
4927
- });
4928
- audioManager.establishAudio();
4929
- }
4930
4987
  this.init({
4931
4988
  src: this.src,
4932
4989
  buffer: this.buffer,
@@ -5022,6 +5079,24 @@ var Rive = /** @class */ (function () {
5022
5079
  this.eventCleanup();
5023
5080
  }
5024
5081
  };
5082
+ /**
5083
+ * If the instance has audio and the system audio is not ready
5084
+ * we hook the instance to the audio manager
5085
+ */
5086
+ Rive.prototype.initializeAudio = function () {
5087
+ var _this = this;
5088
+ var _a;
5089
+ // Initialize audio if needed
5090
+ if (audioManager.status == SystemAudioStatus.UNAVAILABLE) {
5091
+ if ((_a = this.artboard) === null || _a === void 0 ? void 0 : _a.hasAudio) {
5092
+ audioManager.add({
5093
+ type: EventType.AudioStatusChange,
5094
+ callback: function () { return _this.onSystemAudioChanged(); },
5095
+ });
5096
+ audioManager.establishAudio();
5097
+ }
5098
+ }
5099
+ };
5025
5100
  // Initializes runtime with Rive data and preps for playing
5026
5101
  Rive.prototype.initData = function (artboardName, animationNames, stateMachineNames, autoplay) {
5027
5102
  var _a;
@@ -5051,6 +5126,8 @@ var Rive = /** @class */ (function () {
5051
5126
  if (this.file) {
5052
5127
  // Initialize and draw frame
5053
5128
  this.initArtboard(artboardName, animationNames, stateMachineNames, autoplay);
5129
+ // Check for audio
5130
+ this.initializeAudio();
5054
5131
  // Everything's set up, emit a load event
5055
5132
  this.loaded = true;
5056
5133
  this.eventManager.fire({
@@ -5128,9 +5205,9 @@ var Rive = /** @class */ (function () {
5128
5205
  * @param time the time at which to render a frame
5129
5206
  */
5130
5207
  Rive.prototype.draw = function (time, onSecond) {
5131
- var before = performance.now();
5132
5208
  // Clear the frameRequestId, as we're now rendering a fresh frame
5133
5209
  this.frameRequestId = null;
5210
+ var before = performance.now();
5134
5211
  // On the first pass, make sure lastTime has a valid value
5135
5212
  if (!this.lastRenderTime) {
5136
5213
  this.lastRenderTime = time;
@@ -5209,7 +5286,10 @@ var Rive = /** @class */ (function () {
5209
5286
  renderer.save();
5210
5287
  // Update the renderer alignment if necessary
5211
5288
  this.alignRenderer();
5212
- this.artboard.draw(renderer);
5289
+ // Do not draw on 0 canvas size
5290
+ if (!this._hasZeroSize) {
5291
+ this.artboard.draw(renderer);
5292
+ }
5213
5293
  renderer.restore();
5214
5294
  renderer.flush();
5215
5295
  // Check for any animations that looped
@@ -5289,6 +5369,10 @@ var Rive = /** @class */ (function () {
5289
5369
  this.stopRendering();
5290
5370
  // Clean up any artboard, animation or state machine instances.
5291
5371
  this.cleanupInstances();
5372
+ // Remove from observer
5373
+ if (this._observed !== null) {
5374
+ observers.remove(this._observed);
5375
+ }
5292
5376
  // Delete the rive file
5293
5377
  (_a = this.file) === null || _a === void 0 ? void 0 : _a.delete();
5294
5378
  this.file = null;