utils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. function getParameterByName(name, url) {
  2. if (!url) url = window.location.href;
  3. name = name.replace(/[\[\]]/g, '\\$&');
  4. var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
  5. results = regex.exec(url);
  6. if (!results) return null;
  7. if (!results[2]) return '';
  8. return decodeURIComponent(results[2].replace(/\+/g, ' '));
  9. }
  10. function filterFloat(value) {
  11. if (/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/
  12. .test(value))
  13. return Number(value);
  14. return 0;
  15. }
  16. function random(min, max) {
  17. return (min + Math.random() * (max - min) + 0.5) | 0;
  18. }
  19. function wrapText(context, text, x, y, maxWidth, lineHeight) {
  20. var words = text.split(' ');
  21. var line = '';
  22. for(var n = 0; n < words.length; n++) {
  23. var testLine = line + words[n] + ' ';
  24. var metrics = context.measureText(testLine);
  25. var testWidth = metrics.width;
  26. if (testWidth > maxWidth && n > 0) {
  27. context.fillText(line, x, y);
  28. line = words[n] + ' ';
  29. y += lineHeight;
  30. }
  31. else {
  32. line = testLine;
  33. }
  34. }
  35. context.fillText(line, x, y);
  36. }
  37. function loadSvgImage(data, callback) {
  38. var DOMURL = window.URL || window.webkitURL || window;
  39. var img = new Image();
  40. var svg = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  41. var url = DOMURL.createObjectURL(svg);
  42. img.onload = function () {
  43. var canvas = document.createElement("canvas");
  44. canvas.width = img.width;
  45. canvas.height = img.height;
  46. var ctx = canvas.getContext("2d");
  47. ctx.drawImage(img, 0, 0);
  48. DOMURL.revokeObjectURL(url);
  49. callback(canvas);
  50. };
  51. img.src = url;
  52. }
  53. function encodeSvg(str) {
  54. return str.replace(/"/g,"'")
  55. .replace(/</g,"%3C")
  56. .replace(/>/g,"%3E")
  57. .replace(/&/g,"%26")
  58. .replace(/#/g,"%23");
  59. }
  60. function findAngle2pDeg(p1, p2) {
  61. var dy = p2.y - p1.y;
  62. var dx = p2.x - p1.x;
  63. var theta = Math.atan2(dy, dx);
  64. theta *= 180 / Math.PI;
  65. return theta;
  66. }
  67. function findAngle2pDeg360(p1, p2) {
  68. var theta = findAngle2pDeg(p1, p2);
  69. if (theta < 0) theta = 360 + theta;
  70. return theta;
  71. }
  72. function rotate(cx, cy, x, y, angle) {
  73. var radians = (Math.PI / 180) * angle,
  74. cos = Math.cos(radians),
  75. sin = Math.sin(radians),
  76. nx = (cos * (x - cx)) + (sin * (y - cy)) + cx,
  77. ny = (cos * (y - cy)) - (sin * (x - cx)) + cy;
  78. return [nx, ny];
  79. }
  80. function lerp(value1, value2, amount) {
  81. amount = amount < 0 ? 0 : amount;
  82. amount = amount > 1 ? 1 : amount;
  83. return value1 + (value2 - value1) * amount;
  84. }
  85. function splitText(str, regx) {
  86. return str.split(regx);
  87. }
  88. function capitalizeFirstLetter(string) {
  89. return string.charAt(0).toUpperCase() + string.slice(1);
  90. }
  91. function toDataURL(url, callback) {
  92. var xhr = new XMLHttpRequest();
  93. xhr.onload = function() {
  94. var reader = new FileReader();
  95. reader.onloadend = function() {
  96. callback(reader.result);
  97. }
  98. reader.readAsDataURL(xhr.response);
  99. };
  100. xhr.open('GET', url);
  101. xhr.responseType = 'blob';
  102. xhr.send();
  103. }
  104. function getFullPathURL() {
  105. let port = window.location.port;
  106. let protocol = window.location.protocol;
  107. let domain_name = window.location.hostname;
  108. let split_path = window.location.pathname.split('index')[0];
  109. if (port != "") {
  110. port = ":" + port;
  111. }
  112. return protocol + "//" + domain_name + port + split_path;
  113. }
  114. function clamp(num, min, max) {
  115. return num <= min ? min : num >= max ? max : num;
  116. }