@aarsteinmedia/dotlottie-player 6.1.0 → 6.2.0

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/dist/svg.js CHANGED
@@ -1,4 +1,4 @@
1
- import { isServer, PlayMode, PreserveAspectRatio, namespaceSVG, RendererType, createElementID, PlayerEvents, download, getFilename } from '@aarsteinmedia/lottie-web/utils';
1
+ import { isServer, PlayMode, PreserveAspectRatio, namespaceSVG, RendererType, createElementID, PlayerEvents, download, getFilename, clamp } from '@aarsteinmedia/lottie-web/utils';
2
2
  export { PlayMode, PlayerEvents } from '@aarsteinmedia/lottie-web/utils';
3
3
  import Lottie from '@aarsteinmedia/lottie-web/svg';
4
4
  import { convert, getAnimationData } from '@aarsteinmedia/lottie-web/dotlottie';
@@ -323,6 +323,7 @@ const notImplemented = 'Method is not implemented';
323
323
  'mode',
324
324
  'playOnClick',
325
325
  'playOnVisible',
326
+ 'selector',
326
327
  'speed',
327
328
  'src',
328
329
  'subframe'
@@ -572,6 +573,18 @@ const notImplemented = 'Method is not implemented';
572
573
  return RendererType.SVG;
573
574
  }
574
575
  /**
576
+ * Play on clicked element by id attribute, other than animation.
577
+ */ set selector(value) {
578
+ if (value) {
579
+ this.setAttribute('selector', value);
580
+ return;
581
+ }
582
+ this.removeAttribute('selector');
583
+ }
584
+ get selector() {
585
+ return this.getAttribute('selector');
586
+ }
587
+ /**
575
588
  * Hide advanced controls.
576
589
  */ set simple(value) {
577
590
  this.setAttribute('simple', value.toString());
@@ -760,6 +773,12 @@ const notImplemented = 'Method is not implemented';
760
773
  }
761
774
  break;
762
775
  }
776
+ case 'selector':
777
+ {
778
+ const selector = document.getElementById(this.selector ?? '');
779
+ selector?.addEventListener('click', this._handleClick);
780
+ break;
781
+ }
763
782
  case 'speed':
764
783
  {
765
784
  const val = Number(value);
@@ -868,6 +887,19 @@ const notImplemented = 'Method is not implemented';
868
887
  if (!animations || animations.some((animation)=>!isLottie(animation))) {
869
888
  throw new Error('Broken or corrupted file');
870
889
  }
890
+ const ldScript = this.parentElement?.querySelector('script[type="application/ld+json"]');
891
+ if (ldScript) {
892
+ const settings = JSON.parse(ldScript.innerHTML);
893
+ if (settings.selector) {
894
+ this.selector = settings.selector;
895
+ }
896
+ if (settings.segment) {
897
+ this.setSegment(settings.segment);
898
+ }
899
+ if (settings.multiAnimationSettings) {
900
+ this.setMultiAnimationSettings(settings.multiAnimationSettings);
901
+ }
902
+ }
871
903
  this._isBounce = this.mode === PlayMode.Bounce;
872
904
  if (this._multiAnimationSettings.length > 0 && this._multiAnimationSettings[this._currentAnimation]?.mode) {
873
905
  this._isBounce = this._multiAnimationSettings[this._currentAnimation]?.mode === PlayMode.Bounce;
@@ -1237,7 +1269,7 @@ const notImplemented = 'Method is not implemented';
1237
1269
  /**
1238
1270
  * Handle click.
1239
1271
  */ _handleClick() {
1240
- if (!this.playOnClick) {
1272
+ if (!this.playOnClick && !this.selector) {
1241
1273
  return;
1242
1274
  }
1243
1275
  this.togglePlay();
@@ -1423,23 +1455,29 @@ const notImplemented = 'Method is not implemented';
1423
1455
  console.warn('DotLottie: Scroll animations might not work properly in a Server Side Rendering context. Try to wrap this in a client component.');
1424
1456
  return;
1425
1457
  }
1426
- if (this._playerState.visible) {
1427
- if (this._playerState.scrollTimeout) {
1428
- clearTimeout(this._playerState.scrollTimeout);
1429
- }
1430
- this._playerState.scrollTimeout = setTimeout(()=>{
1431
- this.playerState = PlayerState.Paused;
1432
- }, 400);
1433
- const adjustedScroll = scrollY > this._playerState.scrollY ? scrollY - this._playerState.scrollY : this._playerState.scrollY - scrollY, clampedScroll = Math.min(Math.max(adjustedScroll / 3, 1), this._lottieInstance.totalFrames * 3), roundedScroll = clampedScroll / 3;
1434
- requestAnimationFrame(()=>{
1435
- if (roundedScroll < (this._lottieInstance?.totalFrames ?? 0)) {
1436
- this.playerState = PlayerState.Playing;
1437
- this._lottieInstance?.goToAndStop(roundedScroll, true);
1438
- } else {
1439
- this.playerState = PlayerState.Paused;
1440
- }
1441
- });
1458
+ if (!this._playerState.visible) {
1459
+ return;
1442
1460
  }
1461
+ if (this._playerState.scrollTimeout) {
1462
+ clearTimeout(this._playerState.scrollTimeout);
1463
+ }
1464
+ this._playerState.scrollTimeout = setTimeout(()=>{
1465
+ this.playerState = PlayerState.Paused;
1466
+ }, 400);
1467
+ const { totalFrames } = this._lottieInstance;
1468
+ let scrollPosition = scrollY - this._playerState.scrollY;
1469
+ if (scrollY <= this._playerState.scrollY) {
1470
+ scrollPosition = this._playerState.scrollY - scrollY;
1471
+ }
1472
+ const scrollProgress = scrollPosition / innerHeight, currentFrame = clamp(scrollProgress * (totalFrames - 1), 1, totalFrames);
1473
+ requestAnimationFrame(()=>{
1474
+ if (currentFrame >= totalFrames) {
1475
+ this.playerState = PlayerState.Paused;
1476
+ return;
1477
+ }
1478
+ this.playerState = PlayerState.Playing;
1479
+ this._lottieInstance?.goToAndStop(currentFrame, true);
1480
+ });
1443
1481
  }
1444
1482
  _handleWindowBlur({ type }) {
1445
1483
  if (this.dontFreezeOnBlur) {
@@ -1621,7 +1659,20 @@ const notImplemented = 'Method is not implemented';
1621
1659
  this._lottieInstance[method]('data_ready', this._dataReady);
1622
1660
  this._lottieInstance[method]('data_failed', this._dataFailed);
1623
1661
  }
1624
- if (this._container) {
1662
+ if (this.selector) {
1663
+ const selector = document.getElementById(this.selector);
1664
+ if (selector) {
1665
+ if (this.hover) {
1666
+ selector[method]('mouseenter', this._mouseEnter);
1667
+ selector[method]('mouseleave', this._mouseLeave);
1668
+ } else {
1669
+ selector[method]('click', this._handleClick);
1670
+ }
1671
+ } else {
1672
+ this.selector = null;
1673
+ }
1674
+ }
1675
+ if (this._container && !this.selector) {
1625
1676
  if (this.hover) {
1626
1677
  this._container[method]('mouseenter', this._mouseEnter);
1627
1678
  this._container[method]('mouseleave', this._mouseLeave);