Library.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. let library = {
  2. //数组元素位置替换
  3. swapArray: function(arr, index1, index2) {
  4. arr[index1] = arr.splice(index2, 1, arr[index1])[0];
  5. return arr;
  6. },
  7. swapLeft : function(arr) {
  8. let element = arr.shift();
  9. arr.push(element);
  10. },
  11. //生成指定范围随机整数
  12. randomFromIntRange: function(min, max) {
  13. return Math.floor(Math.random() * (max - min)) + min;
  14. },
  15. happenRate: function(rate) {
  16. let result = this.randomFromIntRange(0,100);
  17. if(result<rate)
  18. {
  19. return true;
  20. }
  21. else
  22. {
  23. return false;
  24. }
  25. return;
  26. },
  27. openInWebview () {
  28. let sUserAgent = navigator.userAgent.toLowerCase()
  29. let bIsMobile = /AppleWebKit.*Mobile/i.test(navigator.userAgent)
  30. let bIsIpad = sUserAgent.match(/ipad/i) !== null
  31. let bIsIphoneOs = sUserAgent.match(/iphone os/i) !== null
  32. let bIsMidp = sUserAgent.match(/midp/i) !== null
  33. let bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) !== null
  34. let bIsUc = sUserAgent.match(/ucbrowser/i) !== null
  35. let bIsAndroid = sUserAgent.match(/android/i) !== null
  36. let bIsCE = sUserAgent.match(/windows ce/i) !== null
  37. let bIsWM = sUserAgent.match(/windows mobile/i) !== null
  38. if (
  39. bIsMobile ||
  40. bIsIpad ||
  41. bIsIphoneOs ||
  42. bIsMidp ||
  43. bIsUc7 ||
  44. bIsUc ||
  45. bIsAndroid ||
  46. bIsCE ||
  47. bIsWM
  48. ) {
  49. return false;
  50. } else {
  51. return true;
  52. }
  53. },
  54. indexOf (arr,item) {
  55. for (let i = 0; i < arr.length; i++) {
  56. if (arr[i] == item) return i;
  57. }
  58. return -1;
  59. },
  60. remove(arr, item) {
  61. let index = arr.indexOf(item);
  62. if (index > -1) {
  63. arr.splice(index, 1);
  64. }
  65. },
  66. setImageBase64(base64Url,callback){
  67. let img = new Image();
  68. img.src = base64Url;
  69. img.onload = function(){
  70. let texture = new cc.Texture2D();
  71. texture.initWithElement(img);
  72. texture.handleLoadedTexture();
  73. if (callback)
  74. callback(texture);
  75. }
  76. },
  77. isExitsFunction(funcName) {
  78. try {
  79. if (typeof(eval(funcName)) == "function") {
  80. return true;
  81. }
  82. } catch(e) {}
  83. return false;
  84. }
  85. };
  86. module.exports = library;