tool.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 === 'redius_image') {
  107. const img_lever_0 = await createDate(v.src0);
  108. const img_lever_1 = v.src1;
  109. const w = (img_lever_0.width * v.height) / img_lever_0.height;
  110. ctx.drawImage(img_lever_1, 0, 0, layers.width, layers.height);
  111. ctx.save();
  112. ctx.beginPath();
  113. ctx.arc(v.arc_x, v.arc_y, v.width / 2, 0, Math.PI * 2, false);
  114. ctx.clip();
  115. ctx.drawImage(img_lever_0, v.x - (w - v.width) / 2, v.y, w, v.height);
  116. ctx.restore();
  117. } else if (v.type === 'text') {
  118. ctx.font = v.font;
  119. ctx.fillStyle = v.color;
  120. ctx.fillText(v.text, v.x, v.y);
  121. }
  122. }
  123. return {
  124. image: canvas.toDataURL('image/png'),
  125. width: layers.width,
  126. height: layers.height,
  127. };
  128. }
  129. export function createDate(src) {
  130. return new Promise((resolve, reject) => {
  131. try {
  132. const img = new Image();
  133. img.src = src;
  134. img.setAttribute('crossOrigin', 'anonymous');
  135. img.onload = () => {
  136. resolve(img);
  137. };
  138. } catch (e) {
  139. reject(e);
  140. }
  141. });
  142. }
  143. /**
  144. * 高斯模糊
  145. */
  146. export function gaussianBlur(imgData, radius) {
  147. let pixels = imgData.data;
  148. let width = imgData.width;
  149. let height = imgData.height;
  150. let length = Math.floor(radius * 2) + 1;
  151. let arr = [];
  152. // 创建高斯模糊核
  153. for (let i = 0; i < length; i++) {
  154. arr[i] =
  155. Math.exp(-((i - radius) * (i - radius)) / (2 * radius * radius)) /
  156. (Math.sqrt(2 * Math.PI) * radius);
  157. }
  158. // x方向模糊
  159. for (let y = 0; y < height; y++) {
  160. for (let x = 0; x < width; x++) {
  161. let r = 0,
  162. g = 0,
  163. b = 0,
  164. a = 0;
  165. let wsum = 0;
  166. for (let i = -radius; i <= radius; i++) {
  167. let idx = (y * width + Math.min(width - 1, Math.max(0, x + i))) * 4;
  168. r += pixels[idx] * arr[i + radius];
  169. g += pixels[idx + 1] * arr[i + radius];
  170. b += pixels[idx + 2] * arr[i + radius];
  171. a += pixels[idx + 3] * arr[i + radius];
  172. wsum += arr[i + radius];
  173. }
  174. let idx = (y * width + x) * 4;
  175. pixels[idx] = r / wsum;
  176. pixels[idx + 1] = g / wsum;
  177. pixels[idx + 2] = b / wsum;
  178. pixels[idx + 3] = a / wsum;
  179. }
  180. }
  181. // y方向模糊
  182. for (let x = 0; x < width; x++) {
  183. for (let y = 0; y < height; y++) {
  184. let r = 0,
  185. g = 0,
  186. b = 0,
  187. a = 0;
  188. let wsum = 0;
  189. for (let i = -radius; i <= radius; i++) {
  190. let idx = (Math.min(height - 1, Math.max(0, y + i)) * width + x) * 4;
  191. r += pixels[idx] * arr[i + radius];
  192. g += pixels[idx + 1] * arr[i + radius];
  193. b += pixels[idx + 2] * arr[i + radius];
  194. a += pixels[idx + 3] * arr[i + radius];
  195. wsum += arr[i + radius];
  196. }
  197. let idx = (y * width + x) * 4;
  198. pixels[idx] = r / wsum;
  199. pixels[idx + 1] = g / wsum;
  200. pixels[idx + 2] = b / wsum;
  201. pixels[idx + 3] = a / wsum;
  202. }
  203. }
  204. return imgData;
  205. }