ui.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { ThemeEnum } from '../enums/appEnum';
  2. import { useRootSetting } from '/@/hooks/setting/useRootSetting';
  3. // 中间背景雨
  4. export function rainBg(className, bgClassName) {
  5. var c = document.querySelector(`.${className}`);
  6. if (!c) return;
  7. var ctx = c.getContext('2d'); //获取canvas上下文
  8. var w = (c.width = document.querySelector(`.${bgClassName}`).clientWidth);
  9. var h = (c.height = document.querySelector(`.${bgClassName}`).clientHeight);
  10. //设置canvas宽、高
  11. function random(min, max) {
  12. return Math.random() * (max - min) + min;
  13. }
  14. function RainDrop() {}
  15. //雨滴对象 这是绘制雨滴动画的关键
  16. RainDrop.prototype = {
  17. init: function () {
  18. this.x = random(0, w); //雨滴的位置x
  19. this.y = h; //雨滴的位置y
  20. this.color = 'hsl(180, 100%, 50%)'; //雨滴颜色 长方形的填充色
  21. this.vy = random(4, 5); //雨滴下落速度
  22. this.hit = 0; //下落的最大值
  23. this.size = 2; //长方形宽度
  24. },
  25. draw: function () {
  26. if (this.y > this.hit) {
  27. var linearGradient = ctx.createLinearGradient(this.x, this.y, this.x, this.y + this.size * 30);
  28. // 设置起始颜色
  29. linearGradient.addColorStop(0, '#14789c');
  30. // 设置终止颜色
  31. linearGradient.addColorStop(1, '#090723');
  32. // 设置填充样式
  33. ctx.fillStyle = linearGradient;
  34. ctx.fillRect(this.x, this.y, this.size, this.size * 50); //绘制长方形,通过多次叠加长方形,形成雨滴下落效果
  35. }
  36. this.update(); //更新位置
  37. },
  38. update: function () {
  39. if (this.y > this.hit) {
  40. this.y -= this.vy; //未达到底部,增加雨滴y坐标
  41. } else {
  42. this.init();
  43. }
  44. },
  45. };
  46. function resize() {
  47. w = c.width = window.innerWidth;
  48. h = c.height = window.innerHeight;
  49. }
  50. //初始化一个雨滴
  51. var rs = [];
  52. for (var i = 0; i < 10; i++) {
  53. setTimeout(function () {
  54. var r = new RainDrop();
  55. r.init();
  56. rs.push(r);
  57. }, i * 300);
  58. }
  59. function anim() {
  60. ctx.clearRect(0, 0, w, h); //填充背景色,注意不要用clearRect,否则会清空前面的雨滴,导致不能产生叠加的效果
  61. for (var i = 0; i < rs.length; i++) {
  62. rs[i].draw(); //绘制雨滴
  63. }
  64. requestAnimationFrame(anim); //控制动画帧
  65. }
  66. window.addEventListener('resize', resize);
  67. //启动动画
  68. anim();
  69. }
  70. export const getAssetURL = (imagePath) => {
  71. return new URL(`../assets/images/${imagePath}`, import.meta.url).href;
  72. };
  73. export const getThemifyImagesURL = (imagePath) => {
  74. const { getDarkMode } = useRootSetting();
  75. switch (getDarkMode.value) {
  76. case ThemeEnum.DEEPBLUE:
  77. return new URL(`../assets/images/themify/${getDarkMode.value}/${imagePath}`, import.meta.url).href;
  78. // case ThemeEnum.LIGHT:
  79. // return new URL(`../assets/images/themify/${getDarkMode.value}/${imagePath}`, import.meta.url).href;
  80. // case ThemeEnum.DARK:
  81. // return new URL(`../assets/images/themify/${getDarkMode.value}/${imagePath}`, import.meta.url).href;
  82. // case ThemeEnum.VENT1:
  83. // return new URL(`../assets/images/themify/${getDarkMode.value}/${imagePath}`, import.meta.url).href;
  84. default:
  85. return new URL(`../assets/images/${imagePath}`, import.meta.url).href;
  86. }
  87. };