Library.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. let library ={
  2. formatSeconds(value) {
  3. let result = parseInt(value);
  4. // let h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
  5. // let m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  6. let m = Math.floor((result / 60 % 60)) < 10 ? '' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  7. let s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
  8. let res = '';
  9. // if(h !== '00') res += `${h}h`;
  10. // if(m !== '00') res += `${m}min`;
  11. // res += `${s}s`;
  12. // if(h !== '00') res += `${h}:`;
  13. res += `${m}:`;
  14. res += `${s}`;
  15. return res;
  16. },
  17. randomInt(min, max){
  18. return Math.floor(Math.random() * (max+1 - min)) + min;
  19. },
  20. getRandom(start, end, fixed=0) {
  21. //fixed 保留几位小数
  22. let differ = end - start
  23. let random = Math.random()
  24. return (start + differ * random).toFixed(fixed)
  25. },
  26. removeObj(arr,obj)
  27. {
  28. let index = this.indexOf(arr,obj);
  29. if (index > -1) {
  30. arr.splice(index, 1);
  31. }
  32. },
  33. indexOf(arr,obj) {
  34. for (var i = 0; i < arr.length; i++) {
  35. if (arr[i] == obj) return i;
  36. }
  37. return -1;
  38. },
  39. insert(arr,index, item) {
  40. arr.splice(index, 0, item);
  41. },
  42. //数组元素位置替换
  43. swapArray: function(arr, index1, index2) {
  44. arr[index1] = arr.splice(index2, 1, arr[index1])[0];
  45. return arr;
  46. },
  47. swapLeft : function(arr) {
  48. let element = arr.shift();
  49. arr.push(element);
  50. },
  51. //生成指定范围随机整数
  52. randomFromIntRange: function(min, max) {
  53. return Math.floor(Math.random() * (max - min)) + min;
  54. },
  55. happenRate: function(rate) {
  56. let result = this.randomFromIntRange(0,100);
  57. if(result<rate)
  58. {
  59. return true;
  60. }
  61. else
  62. {
  63. return false;
  64. }
  65. return;
  66. },
  67. openInWebview () {
  68. let ua = navigator.userAgent.toLowerCase()
  69. if (ua.match(/MicroMessenger/i) == 'micromessenger') { // 微信浏览器判断
  70. return false
  71. } else if (ua.match(/QQ/i) == 'qq') { // QQ浏览器判断
  72. return false
  73. } else if (ua.match(/WeiBo/i) == "weibo") {
  74. return false
  75. } else {
  76. if (ua.match(/Android/i) != null) {
  77. return ua.match(/browser/i) == null
  78. } else if (ua.match(/iPhone/i) != null) {
  79. return ua.match(/safari/i) == null
  80. } else {
  81. return (ua.match(/macintosh/i) == null && ua.match(/windows/i) == null)
  82. }
  83. }
  84. },
  85. remove(arr, item) {
  86. let index = arr.indexOf(item);
  87. if (index > -1) {
  88. arr.splice(index, 1);
  89. }
  90. },
  91. setImageBase64(base64Url,callback){
  92. let img = new Image();
  93. img.src = base64Url;
  94. img.onload = function(){
  95. let texture = new cc.Texture2D();
  96. texture.initWithElement(img);
  97. texture.handleLoadedTexture();
  98. if (callback)
  99. callback(texture);
  100. }
  101. },
  102. };
  103. module.exports = library;