main.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { setLocationHref, setWebWorker } from '../main';
  2. import animationManager from '../animation/AnimationManager';
  3. import {
  4. setDefaultCurveSegments,
  5. getDefaultCurveSegments,
  6. roundValues,
  7. setIdPrefix,
  8. setSubframeEnabled,
  9. setExpressionsPlugin,
  10. } from '../utils/common';
  11. import PropertyFactory from '../utils/PropertyFactory';
  12. import ShapePropertyFactory from '../utils/shapes/ShapeProperty';
  13. import Matrix from '../3rd_party/transformation-matrix';
  14. const lottie = {};
  15. var standalone = '__[STANDALONE]__';
  16. var animationData = '__[ANIMATIONDATA]__';
  17. var renderer = '';
  18. function setLocation(href) {
  19. setLocationHref(href);
  20. }
  21. function searchAnimations() {
  22. if (standalone === true) {
  23. animationManager.searchAnimations(animationData, standalone, renderer);
  24. } else {
  25. animationManager.searchAnimations();
  26. }
  27. }
  28. function setSubframeRendering(flag) {
  29. setSubframeEnabled(flag);
  30. }
  31. function setPrefix(prefix) {
  32. setIdPrefix(prefix);
  33. }
  34. function loadAnimation(params) {
  35. if (standalone === true) {
  36. params.animationData = JSON.parse(animationData);
  37. }
  38. return animationManager.loadAnimation(params);
  39. }
  40. function setQuality(value) {
  41. if (typeof value === 'string') {
  42. switch (value) {
  43. case 'high':
  44. setDefaultCurveSegments(200);
  45. break;
  46. default:
  47. case 'medium':
  48. setDefaultCurveSegments(50);
  49. break;
  50. case 'low':
  51. setDefaultCurveSegments(10);
  52. break;
  53. }
  54. } else if (!isNaN(value) && value > 1) {
  55. setDefaultCurveSegments(value);
  56. }
  57. if (getDefaultCurveSegments() >= 50) {
  58. roundValues(false);
  59. } else {
  60. roundValues(true);
  61. }
  62. }
  63. function inBrowser() {
  64. return typeof navigator !== 'undefined';
  65. }
  66. function installPlugin(type, plugin) {
  67. if (type === 'expressions') {
  68. setExpressionsPlugin(plugin);
  69. }
  70. }
  71. function getFactory(name) {
  72. switch (name) {
  73. case 'propertyFactory':
  74. return PropertyFactory;
  75. case 'shapePropertyFactory':
  76. return ShapePropertyFactory;
  77. case 'matrix':
  78. return Matrix;
  79. default:
  80. return null;
  81. }
  82. }
  83. lottie.play = animationManager.play;
  84. lottie.pause = animationManager.pause;
  85. lottie.setLocationHref = setLocation;
  86. lottie.togglePause = animationManager.togglePause;
  87. lottie.setSpeed = animationManager.setSpeed;
  88. lottie.setDirection = animationManager.setDirection;
  89. lottie.stop = animationManager.stop;
  90. lottie.searchAnimations = searchAnimations;
  91. lottie.registerAnimation = animationManager.registerAnimation;
  92. lottie.loadAnimation = loadAnimation;
  93. lottie.setSubframeRendering = setSubframeRendering;
  94. lottie.resize = animationManager.resize;
  95. // lottie.start = start;
  96. lottie.goToAndStop = animationManager.goToAndStop;
  97. lottie.destroy = animationManager.destroy;
  98. lottie.setQuality = setQuality;
  99. lottie.inBrowser = inBrowser;
  100. lottie.installPlugin = installPlugin;
  101. lottie.freeze = animationManager.freeze;
  102. lottie.unfreeze = animationManager.unfreeze;
  103. lottie.setVolume = animationManager.setVolume;
  104. lottie.mute = animationManager.mute;
  105. lottie.unmute = animationManager.unmute;
  106. lottie.getRegisteredAnimations = animationManager.getRegisteredAnimations;
  107. lottie.useWebWorker = setWebWorker;
  108. lottie.setIDPrefix = setPrefix;
  109. lottie.__getFactory = getFactory;
  110. lottie.version = '[[BM_VERSION]]';
  111. function checkReady() {
  112. if (document.readyState === 'complete') {
  113. clearInterval(readyStateCheckInterval);
  114. searchAnimations();
  115. }
  116. }
  117. function getQueryVariable(variable) {
  118. var vars = queryString.split('&');
  119. for (var i = 0; i < vars.length; i += 1) {
  120. var pair = vars[i].split('=');
  121. if (decodeURIComponent(pair[0]) == variable) { // eslint-disable-line eqeqeq
  122. return decodeURIComponent(pair[1]);
  123. }
  124. }
  125. return null;
  126. }
  127. var queryString = '';
  128. if (standalone) {
  129. var scripts = document.getElementsByTagName('script');
  130. var index = scripts.length - 1;
  131. var myScript = scripts[index] || {
  132. src: '',
  133. };
  134. queryString = myScript.src ? myScript.src.replace(/^[^\?]+\??/, '') : ''; // eslint-disable-line no-useless-escape
  135. renderer = getQueryVariable('renderer');
  136. }
  137. var readyStateCheckInterval = setInterval(checkReady, 100);
  138. // this adds bodymovin to the window object for backwards compatibility
  139. try {
  140. if (!(typeof exports === 'object' && typeof module !== 'undefined')
  141. && !(typeof define === 'function' && define.amd) // eslint-disable-line no-undef
  142. ) {
  143. window.bodymovin = lottie;
  144. }
  145. } catch (err) {
  146. //
  147. }
  148. export default lottie;