@rive-app/canvas-lite 2.14.1 → 2.14.3

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-lite",
3
- "version": "2.14.1",
3
+ "version": "2.14.3",
4
4
  "description": "A lite version of 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
@@ -2252,7 +2252,7 @@ Pc();
2252
2252
  /* 2 */
2253
2253
  /***/ ((module) => {
2254
2254
 
2255
- module.exports = JSON.parse('{"name":"@rive-app/canvas-lite","version":"2.14.1","description":"A lite version of 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}}');
2255
+ module.exports = JSON.parse('{"name":"@rive-app/canvas-lite","version":"2.14.3","description":"A lite version of 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}}');
2256
2256
 
2257
2257
  /***/ }),
2258
2258
  /* 3 */
@@ -2556,6 +2556,21 @@ __webpack_require__.r(__webpack_exports__);
2556
2556
  /* harmony import */ var _rive_advanced_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
2557
2557
  /* harmony import */ var package_json__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2);
2558
2558
  /* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(3);
2559
+ var __extends = (undefined && undefined.__extends) || (function () {
2560
+ var extendStatics = function (d, b) {
2561
+ extendStatics = Object.setPrototypeOf ||
2562
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
2563
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
2564
+ return extendStatics(d, b);
2565
+ };
2566
+ return function (d, b) {
2567
+ if (typeof b !== "function" && b !== null)
2568
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
2569
+ extendStatics(d, b);
2570
+ function __() { this.constructor = d; }
2571
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
2572
+ };
2573
+ })();
2559
2574
  var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
2560
2575
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
2561
2576
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -3399,6 +3414,7 @@ var EventType;
3399
3414
  EventType["Advance"] = "advance";
3400
3415
  EventType["StateChange"] = "statechange";
3401
3416
  EventType["RiveEvent"] = "riveevent";
3417
+ EventType["AudioStatusChange"] = "audiostatuschange";
3402
3418
  })(EventType || (EventType = {}));
3403
3419
  /**
3404
3420
  * Looping types: one-shot, loop, and ping-pong
@@ -3491,8 +3507,176 @@ var TaskQueueManager = /** @class */ (function () {
3491
3507
  };
3492
3508
  return TaskQueueManager;
3493
3509
  }());
3510
+ // #endregion
3511
+ // #region Audio
3512
+ var SystemAudioStatus;
3513
+ (function (SystemAudioStatus) {
3514
+ SystemAudioStatus[SystemAudioStatus["AVAILABLE"] = 0] = "AVAILABLE";
3515
+ SystemAudioStatus[SystemAudioStatus["UNAVAILABLE"] = 1] = "UNAVAILABLE";
3516
+ })(SystemAudioStatus || (SystemAudioStatus = {}));
3517
+ // Class to handle audio context availability and status changes
3518
+ var AudioManager = /** @class */ (function (_super) {
3519
+ __extends(AudioManager, _super);
3520
+ function AudioManager() {
3521
+ var _this = _super !== null && _super.apply(this, arguments) || this;
3522
+ _this._started = false;
3523
+ _this._enabled = false;
3524
+ _this._status = SystemAudioStatus.UNAVAILABLE;
3525
+ return _this;
3526
+ }
3527
+ AudioManager.prototype.delay = function (time) {
3528
+ return __awaiter(this, void 0, void 0, function () {
3529
+ return __generator(this, function (_a) {
3530
+ return [2 /*return*/, new Promise(function (resolve) { return setTimeout(resolve, time); })];
3531
+ });
3532
+ });
3533
+ };
3534
+ AudioManager.prototype.timeout = function () {
3535
+ return __awaiter(this, void 0, void 0, function () {
3536
+ return __generator(this, function (_a) {
3537
+ return [2 /*return*/, new Promise(function (_, reject) { return setTimeout(reject, 50); })];
3538
+ });
3539
+ });
3540
+ };
3541
+ // Alerts animations on status changes and removes the listeners to avoid alerting twice.
3542
+ AudioManager.prototype.reportToListeners = function () {
3543
+ this.fire({ type: EventType.AudioStatusChange });
3544
+ this.removeAll();
3545
+ };
3546
+ /**
3547
+ * The audio context has been resolved.
3548
+ * Alert any listeners that we can now play audio.
3549
+ * Rive will now play audio at the configured volume.
3550
+ */
3551
+ AudioManager.prototype.enableAudio = function () {
3552
+ return __awaiter(this, void 0, void 0, function () {
3553
+ return __generator(this, function (_a) {
3554
+ if (!this._enabled) {
3555
+ this._enabled = true;
3556
+ this._status = SystemAudioStatus.AVAILABLE;
3557
+ this.reportToListeners();
3558
+ }
3559
+ return [2 /*return*/];
3560
+ });
3561
+ });
3562
+ };
3563
+ /**
3564
+ * Check if we are able to play audio.
3565
+ *
3566
+ * We currently check the audio context, when resume() returns before a timeout we know that the
3567
+ * audio context is running and we can enable audio.
3568
+ */
3569
+ AudioManager.prototype.testAudio = function () {
3570
+ return __awaiter(this, void 0, void 0, function () {
3571
+ var _a;
3572
+ return __generator(this, function (_b) {
3573
+ switch (_b.label) {
3574
+ case 0:
3575
+ if (!(this._status === SystemAudioStatus.UNAVAILABLE &&
3576
+ this._audioContext !== null)) return [3 /*break*/, 4];
3577
+ _b.label = 1;
3578
+ case 1:
3579
+ _b.trys.push([1, 3, , 4]);
3580
+ return [4 /*yield*/, Promise.race([this._audioContext.resume(), this.timeout()])];
3581
+ case 2:
3582
+ _b.sent();
3583
+ this.enableAudio();
3584
+ return [3 /*break*/, 4];
3585
+ case 3:
3586
+ _a = _b.sent();
3587
+ return [3 /*break*/, 4];
3588
+ case 4: return [2 /*return*/];
3589
+ }
3590
+ });
3591
+ });
3592
+ };
3593
+ /**
3594
+ * Establish audio for use with rive.
3595
+ * We both test if we can use audio intermittently and listen for user interaction.
3596
+ * The aim is to enable audio playback as soon as the browser allows this.
3597
+ */
3598
+ AudioManager.prototype._establishAudio = function () {
3599
+ return __awaiter(this, void 0, void 0, function () {
3600
+ return __generator(this, function (_a) {
3601
+ switch (_a.label) {
3602
+ case 0:
3603
+ if (!!this._started) return [3 /*break*/, 5];
3604
+ this._started = true;
3605
+ if (!(typeof window == "undefined")) return [3 /*break*/, 1];
3606
+ this.enableAudio();
3607
+ return [3 /*break*/, 5];
3608
+ case 1:
3609
+ this._audioContext = new AudioContext();
3610
+ this.listenForUserAction();
3611
+ _a.label = 2;
3612
+ case 2:
3613
+ if (!(this._status === SystemAudioStatus.UNAVAILABLE)) return [3 /*break*/, 5];
3614
+ return [4 /*yield*/, this.testAudio()];
3615
+ case 3:
3616
+ _a.sent();
3617
+ return [4 /*yield*/, this.delay(1000)];
3618
+ case 4:
3619
+ _a.sent();
3620
+ return [3 /*break*/, 2];
3621
+ case 5: return [2 /*return*/];
3622
+ }
3623
+ });
3624
+ });
3625
+ };
3626
+ AudioManager.prototype.listenForUserAction = function () {
3627
+ var _this = this;
3628
+ // NOTE: AudioContexts are ready immediately if requested in a ui callback
3629
+ // we *could* re request one in this listener.
3630
+ var _clickListener = function () { return __awaiter(_this, void 0, void 0, function () {
3631
+ return __generator(this, function (_a) {
3632
+ // note this has "better" results than calling `await this.testAudio()`
3633
+ // as we force audio to be enabled in the current thread, rather than chancing
3634
+ // the thread to be passed over for some other async context
3635
+ this.enableAudio();
3636
+ return [2 /*return*/];
3637
+ });
3638
+ }); };
3639
+ // NOTE: we should test this on mobile/pads
3640
+ document.addEventListener("pointerdown", _clickListener, {
3641
+ once: true,
3642
+ });
3643
+ };
3644
+ /**
3645
+ * Establish the audio context for rive, this lets rive know that we can play audio.
3646
+ */
3647
+ AudioManager.prototype.establishAudio = function () {
3648
+ return __awaiter(this, void 0, void 0, function () {
3649
+ return __generator(this, function (_a) {
3650
+ this._establishAudio();
3651
+ return [2 /*return*/];
3652
+ });
3653
+ });
3654
+ };
3655
+ Object.defineProperty(AudioManager.prototype, "systemVolume", {
3656
+ get: function () {
3657
+ if (this._status === SystemAudioStatus.UNAVAILABLE) {
3658
+ // We do an immediate test to avoid depending on the delay of the running test
3659
+ this.testAudio();
3660
+ return 0;
3661
+ }
3662
+ return 1;
3663
+ },
3664
+ enumerable: false,
3665
+ configurable: true
3666
+ });
3667
+ Object.defineProperty(AudioManager.prototype, "status", {
3668
+ get: function () {
3669
+ return this._status;
3670
+ },
3671
+ enumerable: false,
3672
+ configurable: true
3673
+ });
3674
+ return AudioManager;
3675
+ }(EventManager));
3676
+ var audioManager = new AudioManager();
3494
3677
  var Rive = /** @class */ (function () {
3495
3678
  function Rive(params) {
3679
+ var _this = this;
3496
3680
  var _a;
3497
3681
  // Tracks if a Rive file is loaded
3498
3682
  this.loaded = false;
@@ -3577,6 +3761,14 @@ var Rive = /** @class */ (function () {
3577
3761
  this.assetLoader = params.assetLoader;
3578
3762
  // Hook up the task queue
3579
3763
  this.taskQueue = new TaskQueueManager(this.eventManager);
3764
+ // Initialize audio
3765
+ if (audioManager.status == SystemAudioStatus.UNAVAILABLE) {
3766
+ audioManager.add({
3767
+ type: EventType.AudioStatusChange,
3768
+ callback: function () { return _this.onSystemAudioChanged(); },
3769
+ });
3770
+ audioManager.establishAudio();
3771
+ }
3580
3772
  this.init({
3581
3773
  src: this.src,
3582
3774
  buffer: this.buffer,
@@ -3592,6 +3784,10 @@ var Rive = /** @class */ (function () {
3592
3784
  console.warn("This function is deprecated: please use `new Rive({})` instead");
3593
3785
  return new Rive(params);
3594
3786
  };
3787
+ // Event handler for when audio context becomes available
3788
+ Rive.prototype.onSystemAudioChanged = function () {
3789
+ this.volume = this._volume;
3790
+ };
3595
3791
  // Initializes the Rive object either from constructor or load()
3596
3792
  Rive.prototype.init = function (_a) {
3597
3793
  var _this = this;
@@ -3735,7 +3931,7 @@ var Rive = /** @class */ (function () {
3735
3931
  return;
3736
3932
  }
3737
3933
  this.artboard = rootArtboard;
3738
- rootArtboard.volume = this._volume;
3934
+ rootArtboard.volume = this._volume * audioManager.systemVolume;
3739
3935
  // Check that the artboard has at least 1 animation
3740
3936
  if (this.artboard.animationCount() < 1) {
3741
3937
  var msg = "Artboard has no animations";
@@ -4476,7 +4672,7 @@ var Rive = /** @class */ (function () {
4476
4672
  set: function (value) {
4477
4673
  this._volume = value;
4478
4674
  if (this.artboard) {
4479
- this.artboard.volume = value;
4675
+ this.artboard.volume = value * audioManager.systemVolume;
4480
4676
  }
4481
4677
  },
4482
4678
  enumerable: false,