common.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import {
  2. createSizedArray,
  3. } from './helpers/arrays';
  4. let subframeEnabled = true;
  5. let expressionsPlugin = null;
  6. let expressionsInterfaces = null;
  7. let idPrefix = '';
  8. const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
  9. let _shouldRoundValues = false;
  10. const bmPow = Math.pow;
  11. const bmSqrt = Math.sqrt;
  12. const bmFloor = Math.floor;
  13. const bmMax = Math.max;
  14. const bmMin = Math.min;
  15. const BMMath = {};
  16. (function () {
  17. var propertyNames = ['abs', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', 'atan2', 'ceil', 'cbrt', 'expm1', 'clz32', 'cos', 'cosh', 'exp', 'floor', 'fround', 'hypot', 'imul', 'log', 'log1p', 'log2', 'log10', 'max', 'min', 'pow', 'random', 'round', 'sign', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc', 'E', 'LN10', 'LN2', 'LOG10E', 'LOG2E', 'PI', 'SQRT1_2', 'SQRT2'];
  18. var i;
  19. var len = propertyNames.length;
  20. for (i = 0; i < len; i += 1) {
  21. BMMath[propertyNames[i]] = Math[propertyNames[i]];
  22. }
  23. }());
  24. function ProjectInterface() { return {}; }
  25. BMMath.random = Math.random;
  26. BMMath.abs = function (val) {
  27. var tOfVal = typeof val;
  28. if (tOfVal === 'object' && val.length) {
  29. var absArr = createSizedArray(val.length);
  30. var i;
  31. var len = val.length;
  32. for (i = 0; i < len; i += 1) {
  33. absArr[i] = Math.abs(val[i]);
  34. }
  35. return absArr;
  36. }
  37. return Math.abs(val);
  38. };
  39. let defaultCurveSegments = 150;
  40. const degToRads = Math.PI / 180;
  41. const roundCorner = 0.5519;
  42. function roundValues(flag) {
  43. _shouldRoundValues = !!flag;
  44. }
  45. function bmRnd(value) {
  46. if (_shouldRoundValues) {
  47. return Math.round(value);
  48. }
  49. return value;
  50. }
  51. function styleDiv(element) {
  52. element.style.position = 'absolute';
  53. element.style.top = 0;
  54. element.style.left = 0;
  55. element.style.display = 'block';
  56. element.style.transformOrigin = '0 0';
  57. element.style.webkitTransformOrigin = '0 0';
  58. element.style.backfaceVisibility = 'visible';
  59. element.style.webkitBackfaceVisibility = 'visible';
  60. element.style.transformStyle = 'preserve-3d';
  61. element.style.webkitTransformStyle = 'preserve-3d';
  62. element.style.mozTransformStyle = 'preserve-3d';
  63. }
  64. function BMEnterFrameEvent(type, currentTime, totalTime, frameMultiplier) {
  65. this.type = type;
  66. this.currentTime = currentTime;
  67. this.totalTime = totalTime;
  68. this.direction = frameMultiplier < 0 ? -1 : 1;
  69. }
  70. function BMCompleteEvent(type, frameMultiplier) {
  71. this.type = type;
  72. this.direction = frameMultiplier < 0 ? -1 : 1;
  73. }
  74. function BMCompleteLoopEvent(type, totalLoops, currentLoop, frameMultiplier) {
  75. this.type = type;
  76. this.currentLoop = currentLoop;
  77. this.totalLoops = totalLoops;
  78. this.direction = frameMultiplier < 0 ? -1 : 1;
  79. }
  80. function BMSegmentStartEvent(type, firstFrame, totalFrames) {
  81. this.type = type;
  82. this.firstFrame = firstFrame;
  83. this.totalFrames = totalFrames;
  84. }
  85. function BMDestroyEvent(type, target) {
  86. this.type = type;
  87. this.target = target;
  88. }
  89. function BMRenderFrameErrorEvent(nativeError, currentTime) {
  90. this.type = 'renderFrameError';
  91. this.nativeError = nativeError;
  92. this.currentTime = currentTime;
  93. }
  94. function BMConfigErrorEvent(nativeError) {
  95. this.type = 'configError';
  96. this.nativeError = nativeError;
  97. }
  98. function BMAnimationConfigErrorEvent(type, nativeError) {
  99. this.type = type;
  100. this.nativeError = nativeError;
  101. }
  102. const createElementID = (function () {
  103. var _count = 0;
  104. return function createID() {
  105. _count += 1;
  106. return idPrefix + '__lottie_element_' + _count;
  107. };
  108. }());
  109. function HSVtoRGB(h, s, v) {
  110. var r;
  111. var g;
  112. var b;
  113. var i;
  114. var f;
  115. var p;
  116. var q;
  117. var t;
  118. i = Math.floor(h * 6);
  119. f = h * 6 - i;
  120. p = v * (1 - s);
  121. q = v * (1 - f * s);
  122. t = v * (1 - (1 - f) * s);
  123. switch (i % 6) {
  124. case 0: r = v; g = t; b = p; break;
  125. case 1: r = q; g = v; b = p; break;
  126. case 2: r = p; g = v; b = t; break;
  127. case 3: r = p; g = q; b = v; break;
  128. case 4: r = t; g = p; b = v; break;
  129. case 5: r = v; g = p; b = q; break;
  130. default: break;
  131. }
  132. return [r,
  133. g,
  134. b];
  135. }
  136. function RGBtoHSV(r, g, b) {
  137. var max = Math.max(r, g, b);
  138. var min = Math.min(r, g, b);
  139. var d = max - min;
  140. var h;
  141. var s = (max === 0 ? 0 : d / max);
  142. var v = max / 255;
  143. switch (max) {
  144. case min: h = 0; break;
  145. case r: h = (g - b) + d * (g < b ? 6 : 0); h /= 6 * d; break;
  146. case g: h = (b - r) + d * 2; h /= 6 * d; break;
  147. case b: h = (r - g) + d * 4; h /= 6 * d; break;
  148. default: break;
  149. }
  150. return [
  151. h,
  152. s,
  153. v,
  154. ];
  155. }
  156. function addSaturationToRGB(color, offset) {
  157. var hsv = RGBtoHSV(color[0] * 255, color[1] * 255, color[2] * 255);
  158. hsv[1] += offset;
  159. if (hsv[1] > 1) {
  160. hsv[1] = 1;
  161. } else if (hsv[1] <= 0) {
  162. hsv[1] = 0;
  163. }
  164. return HSVtoRGB(hsv[0], hsv[1], hsv[2]);
  165. }
  166. function addBrightnessToRGB(color, offset) {
  167. var hsv = RGBtoHSV(color[0] * 255, color[1] * 255, color[2] * 255);
  168. hsv[2] += offset;
  169. if (hsv[2] > 1) {
  170. hsv[2] = 1;
  171. } else if (hsv[2] < 0) {
  172. hsv[2] = 0;
  173. }
  174. return HSVtoRGB(hsv[0], hsv[1], hsv[2]);
  175. }
  176. function addHueToRGB(color, offset) {
  177. var hsv = RGBtoHSV(color[0] * 255, color[1] * 255, color[2] * 255);
  178. hsv[0] += offset / 360;
  179. if (hsv[0] > 1) {
  180. hsv[0] -= 1;
  181. } else if (hsv[0] < 0) {
  182. hsv[0] += 1;
  183. }
  184. return HSVtoRGB(hsv[0], hsv[1], hsv[2]);
  185. }
  186. const rgbToHex = (function () {
  187. var colorMap = [];
  188. var i;
  189. var hex;
  190. for (i = 0; i < 256; i += 1) {
  191. hex = i.toString(16);
  192. colorMap[i] = hex.length === 1 ? '0' + hex : hex;
  193. }
  194. return function (r, g, b) {
  195. if (r < 0) {
  196. r = 0;
  197. }
  198. if (g < 0) {
  199. g = 0;
  200. }
  201. if (b < 0) {
  202. b = 0;
  203. }
  204. return '#' + colorMap[r] + colorMap[g] + colorMap[b];
  205. };
  206. }());
  207. const setSubframeEnabled = (flag) => { subframeEnabled = !!flag; };
  208. const getSubframeEnabled = () => subframeEnabled;
  209. const setExpressionsPlugin = (value) => { expressionsPlugin = value; };
  210. const getExpressionsPlugin = () => expressionsPlugin;
  211. const setExpressionInterfaces = (value) => { expressionsInterfaces = value; };
  212. const getExpressionInterfaces = () => expressionsInterfaces;
  213. const setDefaultCurveSegments = (value) => { defaultCurveSegments = value; };
  214. const getDefaultCurveSegments = () => defaultCurveSegments;
  215. const setIdPrefix = (value) => { idPrefix = value; };
  216. const getIdPrefix = () => idPrefix;
  217. export {
  218. setSubframeEnabled,
  219. getSubframeEnabled,
  220. setExpressionsPlugin,
  221. getExpressionsPlugin,
  222. setExpressionInterfaces,
  223. getExpressionInterfaces,
  224. setDefaultCurveSegments,
  225. getDefaultCurveSegments,
  226. isSafari,
  227. bmPow,
  228. bmSqrt,
  229. bmFloor,
  230. bmMax,
  231. bmMin,
  232. degToRads,
  233. roundCorner,
  234. styleDiv,
  235. bmRnd,
  236. roundValues,
  237. BMEnterFrameEvent,
  238. BMCompleteEvent,
  239. BMCompleteLoopEvent,
  240. BMSegmentStartEvent,
  241. BMDestroyEvent,
  242. BMRenderFrameErrorEvent,
  243. BMConfigErrorEvent,
  244. BMAnimationConfigErrorEvent,
  245. createElementID,
  246. addSaturationToRGB,
  247. addBrightnessToRGB,
  248. addHueToRGB,
  249. rgbToHex,
  250. setIdPrefix,
  251. getIdPrefix,
  252. BMMath,
  253. ProjectInterface,
  254. };