tool.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import {
  2. isIpad,
  3. isIpod,
  4. isIphone,
  5. isWindows,
  6. isMac,
  7. isWechat,
  8. } from './isTerminal';
  9. /**
  10. * 动态加载script
  11. * @param {string} url
  12. * @param {string} callback
  13. */
  14. export function loadScript(url, callback) {
  15. let script = document.createElement('script');
  16. if (script.readyState) {
  17. // IE
  18. script.onreadystatechange = function () {
  19. if (script.readyState === 'loaded' || script.readyState === 'complete') {
  20. script.onreadystatechange = null;
  21. callback();
  22. }
  23. };
  24. } else {
  25. // 其他浏览器
  26. script.onload = function () {
  27. callback();
  28. };
  29. }
  30. script.src = url;
  31. document.getElementsByTagName('head')[0].appendChild(script);
  32. }
  33. /**
  34. * 闪视频登录状态
  35. * @param {function} next
  36. * @returns
  37. */
  38. export function getUser(next, isNotToLogin) {
  39. window.$shanshipin = {};
  40. // 判断闪视频登录状态
  41. if (isWindows || isMac || isWechat) return next && next();
  42. // 获取登录信息
  43. if (isIpad || isIpod || isIphone) {
  44. if (!window.webkit || !window.webkit.messageHandlers) return next && next();
  45. window.setUser = user => {
  46. if (user == '{}' && !isNotToLogin)
  47. return window.webkit.messageHandlers.iosJumpLogin.postMessage([]);
  48. const u1 = JSON.parse(user || '{}');
  49. window.webkit.messageHandlers.getAppInfo.postMessage([]);
  50. window.setAppInfo = userJson => {
  51. const u2 = JSON.parse(userJson || '{}');
  52. window.$shanshipin = {
  53. ...u1,
  54. ...u2,
  55. };
  56. next && next();
  57. };
  58. };
  59. window.webkit.messageHandlers.tideGetUser.postMessage([]);
  60. } else {
  61. if (!window.TideApp) return next && next();
  62. const u1 = JSON.parse(window.TideApp.getUser() || '{}');
  63. if (!u1.UserId && !isNotToLogin) window.TideApp.login();
  64. const u2 = JSON.parse(window.TideApp.getAppInfo() || '{}');
  65. window.$shanshipin = {
  66. ...u1,
  67. ...u2,
  68. };
  69. next && next();
  70. }
  71. }
  72. /**
  73. * 帧动画
  74. * @returns
  75. */
  76. export function requestAnimationFrame() {
  77. return (
  78. window.requestAnimationFrame ||
  79. window.webkitRequestAnimationFrame /* Safari 和 Chrome */ ||
  80. window.mozRequestAnimationFrame /* Firefox */ ||
  81. window.oRequestAnimationFrame /* Opera */ ||
  82. window.msRequestAnimationFrame /* IE 9 */ ||
  83. function (callback) {
  84. window.setTimeout(callback, 6000 / 60);
  85. }
  86. );
  87. }
  88. /**
  89. * 通过canvas生成图片
  90. */
  91. export async function canvasToImage(layers) {
  92. let canvas = document.createElement('canvas');
  93. canvas.width = layers.width;
  94. canvas.height = layers.height;
  95. let ctx = canvas.getContext('2d');
  96. for (let i = 0; i < layers.list.length; i++) {
  97. const v = layers.list[i];
  98. /**
  99. * 将图层绘制到canvas上
  100. */
  101. if (v.type === 'image') {
  102. const img = await createDate(v.src);
  103. ctx.drawImage(img, v.x, v.y, v.width, v.height);
  104. } else if (v.type === 'new_image') {
  105. ctx.drawImage(v.image, v.x, v.y, v.width, v.height);
  106. } else if (v.type === 'text') {
  107. ctx.font = v.font;
  108. ctx.fillStyle = v.color;
  109. ctx.fillText(v.text, v.x, v.y);
  110. }
  111. }
  112. return {
  113. image: canvas.toDataURL('image/png'),
  114. width: layers.width,
  115. height: layers.height,
  116. };
  117. }
  118. function createDate(src) {
  119. return new Promise((resolve, reject) => {
  120. try {
  121. const img = new Image();
  122. img.src = src;
  123. img.setAttribute('crossOrigin', 'anonymous')
  124. img.onload = () => {
  125. resolve(img);
  126. };
  127. } catch (e) {
  128. reject(e);
  129. }
  130. });
  131. }
  132. /**
  133. * 高斯模糊
  134. */
  135. export function gaussianBlur(imgData, radius) {
  136. let pixels = imgData.data;
  137. let width = imgData.width;
  138. let height = imgData.height;
  139. let length = Math.floor(radius * 2) + 1;
  140. let arr = [];
  141. // 创建高斯模糊核
  142. for (let i = 0; i < length; i++) {
  143. arr[i] =
  144. Math.exp(-((i - radius) * (i - radius)) / (2 * radius * radius)) /
  145. (Math.sqrt(2 * Math.PI) * radius);
  146. }
  147. // x方向模糊
  148. for (let y = 0; y < height; y++) {
  149. for (let x = 0; x < width; x++) {
  150. let r = 0,
  151. g = 0,
  152. b = 0,
  153. a = 0;
  154. let wsum = 0;
  155. for (let i = -radius; i <= radius; i++) {
  156. let idx = (y * width + Math.min(width - 1, Math.max(0, x + i))) * 4;
  157. r += pixels[idx] * arr[i + radius];
  158. g += pixels[idx + 1] * arr[i + radius];
  159. b += pixels[idx + 2] * arr[i + radius];
  160. a += pixels[idx + 3] * arr[i + radius];
  161. wsum += arr[i + radius];
  162. }
  163. let idx = (y * width + x) * 4;
  164. pixels[idx] = r / wsum;
  165. pixels[idx + 1] = g / wsum;
  166. pixels[idx + 2] = b / wsum;
  167. pixels[idx + 3] = a / wsum;
  168. }
  169. }
  170. // y方向模糊
  171. for (let x = 0; x < width; x++) {
  172. for (let y = 0; y < height; y++) {
  173. let r = 0,
  174. g = 0,
  175. b = 0,
  176. a = 0;
  177. let wsum = 0;
  178. for (let i = -radius; i <= radius; i++) {
  179. let idx = (Math.min(height - 1, Math.max(0, y + i)) * width + x) * 4;
  180. r += pixels[idx] * arr[i + radius];
  181. g += pixels[idx + 1] * arr[i + radius];
  182. b += pixels[idx + 2] * arr[i + radius];
  183. a += pixels[idx + 3] * arr[i + radius];
  184. wsum += arr[i + radius];
  185. }
  186. let idx = (y * width + x) * 4;
  187. pixels[idx] = r / wsum;
  188. pixels[idx + 1] = g / wsum;
  189. pixels[idx + 2] = b / wsum;
  190. pixels[idx + 3] = a / wsum;
  191. }
  192. }
  193. return imgData;
  194. }