@rive-app/webgl 2.14.0 → 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/webgl",
3
- "version": "2.14.0",
3
+ "version": "2.14.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
@@ -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, FontAsset, ImageAsset } from "./rive_advanced.mjs";
3
3
  /**
4
4
  * Generic type for a parameterless void callback
5
5
  */
@@ -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 {
@@ -304,12 +305,14 @@ export declare class Rive {
304
305
  private shouldDisableRiveListeners;
305
306
  private automaticallyHandleEvents;
306
307
  private enableRiveAssetCDN;
308
+ private _volume;
307
309
  durations: number[];
308
310
  frameTimes: number[];
309
311
  frameCount: number;
310
312
  isTouchScrollEnabled: boolean;
311
313
  constructor(params: RiveParameters);
312
314
  static new(params: RiveParameters): Rive;
315
+ private onSystemAudioChanged;
313
316
  private init;
314
317
  /**
315
318
  * Setup Rive Listeners on the canvas
@@ -525,6 +528,11 @@ export declare class Rive {
525
528
  * Returns the contents of a Rive file: the artboards, animations, and state machines
526
529
  */
527
530
  get contents(): RiveFileContents;
531
+ /**
532
+ * getter and setter for the volume of the artboard
533
+ */
534
+ get volume(): number;
535
+ set volume(value: number);
528
536
  }
529
537
  /**
530
538
  * Contents of a state machine input
package/rive.js CHANGED
@@ -3852,8 +3852,8 @@ m.dynCall_iiiij = (a, b, c, d, e, f) => (m.dynCall_iiiij = p.dynCall_iiiij)(a, b
3852
3852
  m.dynCall_viiij = (a, b, c, d, e, f) => (m.dynCall_viiij = p.dynCall_viiij)(a, b, c, d, e, f);
3853
3853
  m.dynCall_iiij = (a, b, c, d, e) => (m.dynCall_iiij = p.dynCall_iiij)(a, b, c, d, e);
3854
3854
  m.dynCall_viij = (a, b, c, d, e) => (m.dynCall_viij = p.dynCall_viij)(a, b, c, d, e);
3855
- m.dynCall_iiji = (a, b, c, d, e) => (m.dynCall_iiji = p.dynCall_iiji)(a, b, c, d, e);
3856
3855
  m.dynCall_iiiji = (a, b, c, d, e, f) => (m.dynCall_iiiji = p.dynCall_iiiji)(a, b, c, d, e, f);
3856
+ m.dynCall_iiji = (a, b, c, d, e) => (m.dynCall_iiji = p.dynCall_iiji)(a, b, c, d, e);
3857
3857
  m.dynCall_jiji = (a, b, c, d, e) => (m.dynCall_jiji = p.dynCall_jiji)(a, b, c, d, e);
3858
3858
  m.dynCall_iij = (a, b, c, d) => (m.dynCall_iij = p.dynCall_iij)(a, b, c, d);
3859
3859
  m.dynCall_jii = (a, b, c) => (m.dynCall_jii = p.dynCall_jii)(a, b, c);
@@ -4104,7 +4104,7 @@ Ie();
4104
4104
  /* 2 */
4105
4105
  /***/ ((module) => {
4106
4106
 
4107
- module.exports = JSON.parse('{"name":"@rive-app/webgl","version":"2.14.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}}');
4107
+ module.exports = JSON.parse('{"name":"@rive-app/webgl","version":"2.14.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}}');
4108
4108
 
4109
4109
  /***/ }),
4110
4110
  /* 3 */
@@ -4408,6 +4408,21 @@ __webpack_require__.r(__webpack_exports__);
4408
4408
  /* harmony import */ var _rive_advanced_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
4409
4409
  /* harmony import */ var package_json__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2);
4410
4410
  /* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(3);
4411
+ var __extends = (undefined && undefined.__extends) || (function () {
4412
+ var extendStatics = function (d, b) {
4413
+ extendStatics = Object.setPrototypeOf ||
4414
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
4415
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
4416
+ return extendStatics(d, b);
4417
+ };
4418
+ return function (d, b) {
4419
+ if (typeof b !== "function" && b !== null)
4420
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
4421
+ extendStatics(d, b);
4422
+ function __() { this.constructor = d; }
4423
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
4424
+ };
4425
+ })();
4411
4426
  var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
4412
4427
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4413
4428
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -5251,6 +5266,7 @@ var EventType;
5251
5266
  EventType["Advance"] = "advance";
5252
5267
  EventType["StateChange"] = "statechange";
5253
5268
  EventType["RiveEvent"] = "riveevent";
5269
+ EventType["AudioStatusChange"] = "audiostatuschange";
5254
5270
  })(EventType || (EventType = {}));
5255
5271
  /**
5256
5272
  * Looping types: one-shot, loop, and ping-pong
@@ -5343,8 +5359,176 @@ var TaskQueueManager = /** @class */ (function () {
5343
5359
  };
5344
5360
  return TaskQueueManager;
5345
5361
  }());
5362
+ // #endregion
5363
+ // #region Audio
5364
+ var SystemAudioStatus;
5365
+ (function (SystemAudioStatus) {
5366
+ SystemAudioStatus[SystemAudioStatus["AVAILABLE"] = 0] = "AVAILABLE";
5367
+ SystemAudioStatus[SystemAudioStatus["UNAVAILABLE"] = 1] = "UNAVAILABLE";
5368
+ })(SystemAudioStatus || (SystemAudioStatus = {}));
5369
+ // Class to handle audio context availability and status changes
5370
+ var AudioManager = /** @class */ (function (_super) {
5371
+ __extends(AudioManager, _super);
5372
+ function AudioManager() {
5373
+ var _this = _super !== null && _super.apply(this, arguments) || this;
5374
+ _this._started = false;
5375
+ _this._enabled = false;
5376
+ _this._status = SystemAudioStatus.UNAVAILABLE;
5377
+ return _this;
5378
+ }
5379
+ AudioManager.prototype.delay = function (time) {
5380
+ return __awaiter(this, void 0, void 0, function () {
5381
+ return __generator(this, function (_a) {
5382
+ return [2 /*return*/, new Promise(function (resolve) { return setTimeout(resolve, time); })];
5383
+ });
5384
+ });
5385
+ };
5386
+ AudioManager.prototype.timeout = function () {
5387
+ return __awaiter(this, void 0, void 0, function () {
5388
+ return __generator(this, function (_a) {
5389
+ return [2 /*return*/, new Promise(function (_, reject) { return setTimeout(reject, 50); })];
5390
+ });
5391
+ });
5392
+ };
5393
+ // Alerts animations on status changes and removes the listeners to avoid alerting twice.
5394
+ AudioManager.prototype.reportToListeners = function () {
5395
+ this.fire({ type: EventType.AudioStatusChange });
5396
+ this.removeAll();
5397
+ };
5398
+ /**
5399
+ * The audio context has been resolved.
5400
+ * Alert any listeners that we can now play audio.
5401
+ * Rive will now play audio at the configured volume.
5402
+ */
5403
+ AudioManager.prototype.enableAudio = function () {
5404
+ return __awaiter(this, void 0, void 0, function () {
5405
+ return __generator(this, function (_a) {
5406
+ if (!this._enabled) {
5407
+ this._enabled = true;
5408
+ this._status = SystemAudioStatus.AVAILABLE;
5409
+ this.reportToListeners();
5410
+ }
5411
+ return [2 /*return*/];
5412
+ });
5413
+ });
5414
+ };
5415
+ /**
5416
+ * Check if we are able to play audio.
5417
+ *
5418
+ * We currently check the audio context, when resume() returns before a timeout we know that the
5419
+ * audio context is running and we can enable audio.
5420
+ */
5421
+ AudioManager.prototype.testAudio = function () {
5422
+ return __awaiter(this, void 0, void 0, function () {
5423
+ var _a;
5424
+ return __generator(this, function (_b) {
5425
+ switch (_b.label) {
5426
+ case 0:
5427
+ if (!(this._status === SystemAudioStatus.UNAVAILABLE &&
5428
+ this._audioContext !== null)) return [3 /*break*/, 4];
5429
+ _b.label = 1;
5430
+ case 1:
5431
+ _b.trys.push([1, 3, , 4]);
5432
+ return [4 /*yield*/, Promise.race([this._audioContext.resume(), this.timeout()])];
5433
+ case 2:
5434
+ _b.sent();
5435
+ this.enableAudio();
5436
+ return [3 /*break*/, 4];
5437
+ case 3:
5438
+ _a = _b.sent();
5439
+ return [3 /*break*/, 4];
5440
+ case 4: return [2 /*return*/];
5441
+ }
5442
+ });
5443
+ });
5444
+ };
5445
+ /**
5446
+ * Establish audio for use with rive.
5447
+ * We both test if we can use audio intermittently and listen for user interaction.
5448
+ * The aim is to enable audio playback as soon as the browser allows this.
5449
+ */
5450
+ AudioManager.prototype._establishAudio = function () {
5451
+ return __awaiter(this, void 0, void 0, function () {
5452
+ return __generator(this, function (_a) {
5453
+ switch (_a.label) {
5454
+ case 0:
5455
+ if (!!this._started) return [3 /*break*/, 5];
5456
+ this._started = true;
5457
+ if (!(typeof window == "undefined")) return [3 /*break*/, 1];
5458
+ this.enableAudio();
5459
+ return [3 /*break*/, 5];
5460
+ case 1:
5461
+ this._audioContext = new AudioContext();
5462
+ this.listenForUserAction();
5463
+ _a.label = 2;
5464
+ case 2:
5465
+ if (!(this._status === SystemAudioStatus.UNAVAILABLE)) return [3 /*break*/, 5];
5466
+ return [4 /*yield*/, this.testAudio()];
5467
+ case 3:
5468
+ _a.sent();
5469
+ return [4 /*yield*/, this.delay(1000)];
5470
+ case 4:
5471
+ _a.sent();
5472
+ return [3 /*break*/, 2];
5473
+ case 5: return [2 /*return*/];
5474
+ }
5475
+ });
5476
+ });
5477
+ };
5478
+ AudioManager.prototype.listenForUserAction = function () {
5479
+ var _this = this;
5480
+ // NOTE: AudioContexts are ready immediately if requested in a ui callback
5481
+ // we *could* re request one in this listener.
5482
+ var _clickListener = function () { return __awaiter(_this, void 0, void 0, function () {
5483
+ return __generator(this, function (_a) {
5484
+ // note this has "better" results than calling `await this.testAudio()`
5485
+ // as we force audio to be enabled in the current thread, rather than chancing
5486
+ // the thread to be passed over for some other async context
5487
+ this.enableAudio();
5488
+ return [2 /*return*/];
5489
+ });
5490
+ }); };
5491
+ // NOTE: we should test this on mobile/pads
5492
+ document.addEventListener("pointerdown", _clickListener, {
5493
+ once: true,
5494
+ });
5495
+ };
5496
+ /**
5497
+ * Establish the audio context for rive, this lets rive know that we can play audio.
5498
+ */
5499
+ AudioManager.prototype.establishAudio = function () {
5500
+ return __awaiter(this, void 0, void 0, function () {
5501
+ return __generator(this, function (_a) {
5502
+ this._establishAudio();
5503
+ return [2 /*return*/];
5504
+ });
5505
+ });
5506
+ };
5507
+ Object.defineProperty(AudioManager.prototype, "systemVolume", {
5508
+ get: function () {
5509
+ if (this._status === SystemAudioStatus.UNAVAILABLE) {
5510
+ // We do an immediate test to avoid depending on the delay of the running test
5511
+ this.testAudio();
5512
+ return 0;
5513
+ }
5514
+ return 1;
5515
+ },
5516
+ enumerable: false,
5517
+ configurable: true
5518
+ });
5519
+ Object.defineProperty(AudioManager.prototype, "status", {
5520
+ get: function () {
5521
+ return this._status;
5522
+ },
5523
+ enumerable: false,
5524
+ configurable: true
5525
+ });
5526
+ return AudioManager;
5527
+ }(EventManager));
5528
+ var audioManager = new AudioManager();
5346
5529
  var Rive = /** @class */ (function () {
5347
5530
  function Rive(params) {
5531
+ var _this = this;
5348
5532
  var _a;
5349
5533
  // Tracks if a Rive file is loaded
5350
5534
  this.loaded = false;
@@ -5364,6 +5548,8 @@ var Rive = /** @class */ (function () {
5364
5548
  this.automaticallyHandleEvents = false;
5365
5549
  // Allow the runtime to automatically load assets hosted in Rive's runtime.
5366
5550
  this.enableRiveAssetCDN = true;
5551
+ // Keep a local value of the set volume to update it asynchronously
5552
+ this._volume = 1;
5367
5553
  // Durations to generate a frame for the last second. Used for performance profiling.
5368
5554
  this.durations = [];
5369
5555
  this.frameTimes = [];
@@ -5427,6 +5613,14 @@ var Rive = /** @class */ (function () {
5427
5613
  this.assetLoader = params.assetLoader;
5428
5614
  // Hook up the task queue
5429
5615
  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
+ }
5430
5624
  this.init({
5431
5625
  src: this.src,
5432
5626
  buffer: this.buffer,
@@ -5442,6 +5636,10 @@ var Rive = /** @class */ (function () {
5442
5636
  console.warn("This function is deprecated: please use `new Rive({})` instead");
5443
5637
  return new Rive(params);
5444
5638
  };
5639
+ // Event handler for when audio context becomes available
5640
+ Rive.prototype.onSystemAudioChanged = function () {
5641
+ this.volume = this._volume;
5642
+ };
5445
5643
  // Initializes the Rive object either from constructor or load()
5446
5644
  Rive.prototype.init = function (_a) {
5447
5645
  var _this = this;
@@ -5494,7 +5692,8 @@ var Rive = /** @class */ (function () {
5494
5692
  .filter(function (sm) { return sm.playing && _this.runtime.hasListeners(sm.instance); })
5495
5693
  .map(function (sm) { return sm.instance; });
5496
5694
  var touchScrollEnabledOption = this.isTouchScrollEnabled;
5497
- if (riveListenerOptions && 'isTouchScrollEnabled' in riveListenerOptions) {
5695
+ if (riveListenerOptions &&
5696
+ "isTouchScrollEnabled" in riveListenerOptions) {
5498
5697
  touchScrollEnabledOption = riveListenerOptions.isTouchScrollEnabled;
5499
5698
  }
5500
5699
  this.eventCleanup = (0,_utils__WEBPACK_IMPORTED_MODULE_2__.registerTouchInteractions)({
@@ -5584,6 +5783,7 @@ var Rive = /** @class */ (function () {
5584
5783
  return;
5585
5784
  }
5586
5785
  this.artboard = rootArtboard;
5786
+ rootArtboard.volume = this._volume * audioManager.systemVolume;
5587
5787
  // Check that the artboard has at least 1 animation
5588
5788
  if (this.artboard.animationCount() < 1) {
5589
5789
  var msg = "Artboard has no animations";
@@ -6311,6 +6511,25 @@ var Rive = /** @class */ (function () {
6311
6511
  enumerable: false,
6312
6512
  configurable: true
6313
6513
  });
6514
+ Object.defineProperty(Rive.prototype, "volume", {
6515
+ /**
6516
+ * getter and setter for the volume of the artboard
6517
+ */
6518
+ get: function () {
6519
+ if (this.artboard && this.artboard.volume !== this._volume) {
6520
+ this._volume = this.artboard.volume;
6521
+ }
6522
+ return this._volume;
6523
+ },
6524
+ set: function (value) {
6525
+ this._volume = value;
6526
+ if (this.artboard) {
6527
+ this.artboard.volume = value * audioManager.systemVolume;
6528
+ }
6529
+ },
6530
+ enumerable: false,
6531
+ configurable: true
6532
+ });
6314
6533
  // Error message for missing source or buffer
6315
6534
  Rive.missingErrorMessage = "Rive source file or data buffer required";
6316
6535
  return Rive;