@rive-app/canvas 2.14.1 → 2.14.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.14.1",
3
+ "version": "2.14.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
@@ -117,7 +117,8 @@ export declare enum EventType {
117
117
  Draw = "draw",
118
118
  Advance = "advance",
119
119
  StateChange = "statechange",
120
- RiveEvent = "riveevent"
120
+ RiveEvent = "riveevent",
121
+ AudioStatusChange = "audiostatuschange"
121
122
  }
122
123
  export type RiveEventPayload = rc.RiveEvent | rc.OpenUrlEvent;
123
124
  export interface Event {
@@ -311,6 +312,7 @@ export declare class Rive {
311
312
  isTouchScrollEnabled: boolean;
312
313
  constructor(params: RiveParameters);
313
314
  static new(params: RiveParameters): Rive;
315
+ private onSystemAudioChanged;
314
316
  private init;
315
317
  /**
316
318
  * Setup Rive Listeners on the canvas
package/rive.js CHANGED
@@ -731,7 +731,7 @@ function Ya(a, b) {
731
731
  return Xa(c, a, b);
732
732
  }));
733
733
  }
734
- var N, P, bb = {433284:(a, b, c, d, e) => {
734
+ var N, P, bb = {433276:(a, b, c, d, e) => {
735
735
  if ("undefined" === typeof window || void 0 === (window.AudioContext || window.webkitAudioContext)) {
736
736
  return 0;
737
737
  }
@@ -794,9 +794,9 @@ var N, P, bb = {433284:(a, b, c, d, e) => {
794
794
  }
795
795
  window.h.Aa += 1;
796
796
  return 1;
797
- }, 435462:() => {
797
+ }, 435454:() => {
798
798
  "undefined" !== typeof window.h && (--window.h.Aa, 0 === window.h.Aa && delete window.h);
799
- }, 435626:() => void 0 !== navigator.mediaDevices && void 0 !== navigator.mediaDevices.getUserMedia, 435730:() => {
799
+ }, 435618:() => void 0 !== navigator.mediaDevices && void 0 !== navigator.mediaDevices.getUserMedia, 435722:() => {
800
800
  try {
801
801
  var a = new (window.AudioContext || window.webkitAudioContext)(), b = a.sampleRate;
802
802
  a.close();
@@ -804,7 +804,7 @@ var N, P, bb = {433284:(a, b, c, d, e) => {
804
804
  } catch (c) {
805
805
  return 0;
806
806
  }
807
- }, 435901:(a, b, c, d, e, f) => {
807
+ }, 435893:(a, b, c, d, e, f) => {
808
808
  if ("undefined" === typeof window.h) {
809
809
  return -1;
810
810
  }
@@ -850,7 +850,7 @@ var N, P, bb = {433284:(a, b, c, d, e) => {
850
850
  a == window.h.H.za && g.$.connect(g.I.destination);
851
851
  g.qb = f;
852
852
  return window.h.Gc(g);
853
- }, 438778:a => window.h.sa(a).I.sampleRate, 438851:a => {
853
+ }, 438770:a => window.h.sa(a).I.sampleRate, 438843:a => {
854
854
  a = window.h.sa(a);
855
855
  void 0 !== a.$ && (a.$.onaudioprocess = function() {
856
856
  }, a.$.disconnect(), a.$ = void 0);
@@ -858,13 +858,13 @@ var N, P, bb = {433284:(a, b, c, d, e) => {
858
858
  a.I.close();
859
859
  a.I = void 0;
860
860
  a.qb = void 0;
861
- }, 439251:a => {
861
+ }, 439243:a => {
862
862
  window.h.Db(a);
863
- }, 439301:a => {
863
+ }, 439293:a => {
864
864
  a = window.h.sa(a);
865
865
  a.I.resume();
866
866
  a.state = window.h.ga.yb;
867
- }, 439440:a => {
867
+ }, 439432:a => {
868
868
  a = window.h.sa(a);
869
869
  a.I.suspend();
870
870
  a.state = window.h.ga.stopped;
@@ -3336,8 +3336,8 @@ m._ma_malloc_emscripten = (a, b) => (m._ma_malloc_emscripten = x.ma_malloc_emscr
3336
3336
  m._ma_free_emscripten = (a, b) => (m._ma_free_emscripten = x.ma_free_emscripten)(a, b);
3337
3337
  var $a = m._ma_device_process_pcm_frames_capture__webaudio = (a, b, c) => ($a = m._ma_device_process_pcm_frames_capture__webaudio = x.ma_device_process_pcm_frames_capture__webaudio)(a, b, c), ab = m._ma_device_process_pcm_frames_playback__webaudio = (a, b, c) => (ab = m._ma_device_process_pcm_frames_playback__webaudio = x.ma_device_process_pcm_frames_playback__webaudio)(a, b, c), Ud = () => (Ud = x.__errno_location)(), Dc = a => (Dc = x.__getTypeName)(a);
3338
3338
  m.__embind_initialize_bindings = () => (m.__embind_initialize_bindings = x._embind_initialize_bindings)();
3339
- m.dynCall_iiji = (a, b, c, d, e) => (m.dynCall_iiji = x.dynCall_iiji)(a, b, c, d, e);
3340
3339
  m.dynCall_iiiji = (a, b, c, d, e, f) => (m.dynCall_iiiji = x.dynCall_iiiji)(a, b, c, d, e, f);
3340
+ m.dynCall_iiji = (a, b, c, d, e) => (m.dynCall_iiji = x.dynCall_iiji)(a, b, c, d, e);
3341
3341
  m.dynCall_jiji = (a, b, c, d, e) => (m.dynCall_jiji = x.dynCall_jiji)(a, b, c, d, e);
3342
3342
  m.dynCall_iij = (a, b, c, d) => (m.dynCall_iij = x.dynCall_iij)(a, b, c, d);
3343
3343
  m.dynCall_jii = (a, b, c) => (m.dynCall_jii = x.dynCall_jii)(a, b, c);
@@ -3404,7 +3404,7 @@ Zd();
3404
3404
  /* 2 */
3405
3405
  /***/ ((module) => {
3406
3406
 
3407
- module.exports = JSON.parse('{"name":"@rive-app/canvas","version":"2.14.1","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}}');
3407
+ module.exports = JSON.parse('{"name":"@rive-app/canvas","version":"2.14.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}}');
3408
3408
 
3409
3409
  /***/ }),
3410
3410
  /* 3 */
@@ -3708,6 +3708,21 @@ __webpack_require__.r(__webpack_exports__);
3708
3708
  /* harmony import */ var _rive_advanced_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
3709
3709
  /* harmony import */ var package_json__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2);
3710
3710
  /* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(3);
3711
+ var __extends = (undefined && undefined.__extends) || (function () {
3712
+ var extendStatics = function (d, b) {
3713
+ extendStatics = Object.setPrototypeOf ||
3714
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
3715
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
3716
+ return extendStatics(d, b);
3717
+ };
3718
+ return function (d, b) {
3719
+ if (typeof b !== "function" && b !== null)
3720
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
3721
+ extendStatics(d, b);
3722
+ function __() { this.constructor = d; }
3723
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
3724
+ };
3725
+ })();
3711
3726
  var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
3712
3727
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3713
3728
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -4551,6 +4566,7 @@ var EventType;
4551
4566
  EventType["Advance"] = "advance";
4552
4567
  EventType["StateChange"] = "statechange";
4553
4568
  EventType["RiveEvent"] = "riveevent";
4569
+ EventType["AudioStatusChange"] = "audiostatuschange";
4554
4570
  })(EventType || (EventType = {}));
4555
4571
  /**
4556
4572
  * Looping types: one-shot, loop, and ping-pong
@@ -4643,8 +4659,176 @@ var TaskQueueManager = /** @class */ (function () {
4643
4659
  };
4644
4660
  return TaskQueueManager;
4645
4661
  }());
4662
+ // #endregion
4663
+ // #region Audio
4664
+ var SystemAudioStatus;
4665
+ (function (SystemAudioStatus) {
4666
+ SystemAudioStatus[SystemAudioStatus["AVAILABLE"] = 0] = "AVAILABLE";
4667
+ SystemAudioStatus[SystemAudioStatus["UNAVAILABLE"] = 1] = "UNAVAILABLE";
4668
+ })(SystemAudioStatus || (SystemAudioStatus = {}));
4669
+ // Class to handle audio context availability and status changes
4670
+ var AudioManager = /** @class */ (function (_super) {
4671
+ __extends(AudioManager, _super);
4672
+ function AudioManager() {
4673
+ var _this = _super !== null && _super.apply(this, arguments) || this;
4674
+ _this._started = false;
4675
+ _this._enabled = false;
4676
+ _this._status = SystemAudioStatus.UNAVAILABLE;
4677
+ return _this;
4678
+ }
4679
+ AudioManager.prototype.delay = function (time) {
4680
+ return __awaiter(this, void 0, void 0, function () {
4681
+ return __generator(this, function (_a) {
4682
+ return [2 /*return*/, new Promise(function (resolve) { return setTimeout(resolve, time); })];
4683
+ });
4684
+ });
4685
+ };
4686
+ AudioManager.prototype.timeout = function () {
4687
+ return __awaiter(this, void 0, void 0, function () {
4688
+ return __generator(this, function (_a) {
4689
+ return [2 /*return*/, new Promise(function (_, reject) { return setTimeout(reject, 50); })];
4690
+ });
4691
+ });
4692
+ };
4693
+ // Alerts animations on status changes and removes the listeners to avoid alerting twice.
4694
+ AudioManager.prototype.reportToListeners = function () {
4695
+ this.fire({ type: EventType.AudioStatusChange });
4696
+ this.removeAll();
4697
+ };
4698
+ /**
4699
+ * The audio context has been resolved.
4700
+ * Alert any listeners that we can now play audio.
4701
+ * Rive will now play audio at the configured volume.
4702
+ */
4703
+ AudioManager.prototype.enableAudio = function () {
4704
+ return __awaiter(this, void 0, void 0, function () {
4705
+ return __generator(this, function (_a) {
4706
+ if (!this._enabled) {
4707
+ this._enabled = true;
4708
+ this._status = SystemAudioStatus.AVAILABLE;
4709
+ this.reportToListeners();
4710
+ }
4711
+ return [2 /*return*/];
4712
+ });
4713
+ });
4714
+ };
4715
+ /**
4716
+ * Check if we are able to play audio.
4717
+ *
4718
+ * We currently check the audio context, when resume() returns before a timeout we know that the
4719
+ * audio context is running and we can enable audio.
4720
+ */
4721
+ AudioManager.prototype.testAudio = function () {
4722
+ return __awaiter(this, void 0, void 0, function () {
4723
+ var _a;
4724
+ return __generator(this, function (_b) {
4725
+ switch (_b.label) {
4726
+ case 0:
4727
+ if (!(this._status === SystemAudioStatus.UNAVAILABLE &&
4728
+ this._audioContext !== null)) return [3 /*break*/, 4];
4729
+ _b.label = 1;
4730
+ case 1:
4731
+ _b.trys.push([1, 3, , 4]);
4732
+ return [4 /*yield*/, Promise.race([this._audioContext.resume(), this.timeout()])];
4733
+ case 2:
4734
+ _b.sent();
4735
+ this.enableAudio();
4736
+ return [3 /*break*/, 4];
4737
+ case 3:
4738
+ _a = _b.sent();
4739
+ return [3 /*break*/, 4];
4740
+ case 4: return [2 /*return*/];
4741
+ }
4742
+ });
4743
+ });
4744
+ };
4745
+ /**
4746
+ * Establish audio for use with rive.
4747
+ * We both test if we can use audio intermittently and listen for user interaction.
4748
+ * The aim is to enable audio playback as soon as the browser allows this.
4749
+ */
4750
+ AudioManager.prototype._establishAudio = function () {
4751
+ return __awaiter(this, void 0, void 0, function () {
4752
+ return __generator(this, function (_a) {
4753
+ switch (_a.label) {
4754
+ case 0:
4755
+ if (!!this._started) return [3 /*break*/, 5];
4756
+ this._started = true;
4757
+ if (!(typeof window == "undefined")) return [3 /*break*/, 1];
4758
+ this.enableAudio();
4759
+ return [3 /*break*/, 5];
4760
+ case 1:
4761
+ this._audioContext = new AudioContext();
4762
+ this.listenForUserAction();
4763
+ _a.label = 2;
4764
+ case 2:
4765
+ if (!(this._status === SystemAudioStatus.UNAVAILABLE)) return [3 /*break*/, 5];
4766
+ return [4 /*yield*/, this.testAudio()];
4767
+ case 3:
4768
+ _a.sent();
4769
+ return [4 /*yield*/, this.delay(1000)];
4770
+ case 4:
4771
+ _a.sent();
4772
+ return [3 /*break*/, 2];
4773
+ case 5: return [2 /*return*/];
4774
+ }
4775
+ });
4776
+ });
4777
+ };
4778
+ AudioManager.prototype.listenForUserAction = function () {
4779
+ var _this = this;
4780
+ // NOTE: AudioContexts are ready immediately if requested in a ui callback
4781
+ // we *could* re request one in this listener.
4782
+ var _clickListener = function () { return __awaiter(_this, void 0, void 0, function () {
4783
+ return __generator(this, function (_a) {
4784
+ // note this has "better" results than calling `await this.testAudio()`
4785
+ // as we force audio to be enabled in the current thread, rather than chancing
4786
+ // the thread to be passed over for some other async context
4787
+ this.enableAudio();
4788
+ return [2 /*return*/];
4789
+ });
4790
+ }); };
4791
+ // NOTE: we should test this on mobile/pads
4792
+ document.addEventListener("pointerdown", _clickListener, {
4793
+ once: true,
4794
+ });
4795
+ };
4796
+ /**
4797
+ * Establish the audio context for rive, this lets rive know that we can play audio.
4798
+ */
4799
+ AudioManager.prototype.establishAudio = function () {
4800
+ return __awaiter(this, void 0, void 0, function () {
4801
+ return __generator(this, function (_a) {
4802
+ this._establishAudio();
4803
+ return [2 /*return*/];
4804
+ });
4805
+ });
4806
+ };
4807
+ Object.defineProperty(AudioManager.prototype, "systemVolume", {
4808
+ get: function () {
4809
+ if (this._status === SystemAudioStatus.UNAVAILABLE) {
4810
+ // We do an immediate test to avoid depending on the delay of the running test
4811
+ this.testAudio();
4812
+ return 0;
4813
+ }
4814
+ return 1;
4815
+ },
4816
+ enumerable: false,
4817
+ configurable: true
4818
+ });
4819
+ Object.defineProperty(AudioManager.prototype, "status", {
4820
+ get: function () {
4821
+ return this._status;
4822
+ },
4823
+ enumerable: false,
4824
+ configurable: true
4825
+ });
4826
+ return AudioManager;
4827
+ }(EventManager));
4828
+ var audioManager = new AudioManager();
4646
4829
  var Rive = /** @class */ (function () {
4647
4830
  function Rive(params) {
4831
+ var _this = this;
4648
4832
  var _a;
4649
4833
  // Tracks if a Rive file is loaded
4650
4834
  this.loaded = false;
@@ -4729,6 +4913,14 @@ var Rive = /** @class */ (function () {
4729
4913
  this.assetLoader = params.assetLoader;
4730
4914
  // Hook up the task queue
4731
4915
  this.taskQueue = new TaskQueueManager(this.eventManager);
4916
+ // Initialize audio
4917
+ if (audioManager.status == SystemAudioStatus.UNAVAILABLE) {
4918
+ audioManager.add({
4919
+ type: EventType.AudioStatusChange,
4920
+ callback: function () { return _this.onSystemAudioChanged(); },
4921
+ });
4922
+ audioManager.establishAudio();
4923
+ }
4732
4924
  this.init({
4733
4925
  src: this.src,
4734
4926
  buffer: this.buffer,
@@ -4744,6 +4936,10 @@ var Rive = /** @class */ (function () {
4744
4936
  console.warn("This function is deprecated: please use `new Rive({})` instead");
4745
4937
  return new Rive(params);
4746
4938
  };
4939
+ // Event handler for when audio context becomes available
4940
+ Rive.prototype.onSystemAudioChanged = function () {
4941
+ this.volume = this._volume;
4942
+ };
4747
4943
  // Initializes the Rive object either from constructor or load()
4748
4944
  Rive.prototype.init = function (_a) {
4749
4945
  var _this = this;
@@ -4887,7 +5083,7 @@ var Rive = /** @class */ (function () {
4887
5083
  return;
4888
5084
  }
4889
5085
  this.artboard = rootArtboard;
4890
- rootArtboard.volume = this._volume;
5086
+ rootArtboard.volume = this._volume * audioManager.systemVolume;
4891
5087
  // Check that the artboard has at least 1 animation
4892
5088
  if (this.artboard.animationCount() < 1) {
4893
5089
  var msg = "Artboard has no animations";
@@ -5628,7 +5824,7 @@ var Rive = /** @class */ (function () {
5628
5824
  set: function (value) {
5629
5825
  this._volume = value;
5630
5826
  if (this.artboard) {
5631
- this.artboard.volume = value;
5827
+ this.artboard.volume = value * audioManager.systemVolume;
5632
5828
  }
5633
5829
  },
5634
5830
  enumerable: false,