module.js 4.0 KB

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