| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- import { ThemeEnum } from '../enums/appEnum';
- import { useRootSetting } from '/@/hooks/setting/useRootSetting';
- // 中间背景雨
- export function rainBg(className, bgClassName) {
- var c = document.querySelector(`.${className}`);
- if (!c) return;
- var ctx = c.getContext('2d'); //获取canvas上下文
- var w = (c.width = document.querySelector(`.${bgClassName}`).clientWidth);
- var h = (c.height = document.querySelector(`.${bgClassName}`).clientHeight);
- //设置canvas宽、高
- function random(min, max) {
- return Math.random() * (max - min) + min;
- }
- function RainDrop() {}
- //雨滴对象 这是绘制雨滴动画的关键
- RainDrop.prototype = {
- init: function () {
- this.x = random(0, w); //雨滴的位置x
- this.y = h; //雨滴的位置y
- this.color = 'hsl(180, 100%, 50%)'; //雨滴颜色 长方形的填充色
- this.vy = random(4, 5); //雨滴下落速度
- this.hit = 0; //下落的最大值
- this.size = 2; //长方形宽度
- },
- draw: function () {
- if (this.y > this.hit) {
- var linearGradient = ctx.createLinearGradient(this.x, this.y, this.x, this.y + this.size * 30);
- // 设置起始颜色
- linearGradient.addColorStop(0, '#14789c');
- // 设置终止颜色
- linearGradient.addColorStop(1, '#090723');
- // 设置填充样式
- ctx.fillStyle = linearGradient;
- ctx.fillRect(this.x, this.y, this.size, this.size * 50); //绘制长方形,通过多次叠加长方形,形成雨滴下落效果
- }
- this.update(); //更新位置
- },
- update: function () {
- if (this.y > this.hit) {
- this.y -= this.vy; //未达到底部,增加雨滴y坐标
- } else {
- this.init();
- }
- },
- };
- function resize() {
- w = c.width = window.innerWidth;
- h = c.height = window.innerHeight;
- }
- //初始化一个雨滴
- var rs = [];
- for (var i = 0; i < 10; i++) {
- setTimeout(function () {
- var r = new RainDrop();
- r.init();
- rs.push(r);
- }, i * 300);
- }
- function anim() {
- ctx.clearRect(0, 0, w, h); //填充背景色,注意不要用clearRect,否则会清空前面的雨滴,导致不能产生叠加的效果
- for (var i = 0; i < rs.length; i++) {
- rs[i].draw(); //绘制雨滴
- }
- requestAnimationFrame(anim); //控制动画帧
- }
- window.addEventListener('resize', resize);
- //启动动画
- anim();
- }
- export const getAssetURL = (imagePath) => {
- return new URL(`../assets/images/${imagePath}`, import.meta.url).href;
- };
- export const getThemifyImagesURL = (imagePath) => {
- const { getDarkMode } = useRootSetting();
- switch (getDarkMode.value) {
- case ThemeEnum.DEEPBLUE:
- return new URL(`../assets/images/themify/${getDarkMode.value}/${imagePath}`, import.meta.url).href;
- // case ThemeEnum.LIGHT:
- // return new URL(`../assets/images/themify/${getDarkMode.value}/${imagePath}`, import.meta.url).href;
- // case ThemeEnum.DARK:
- // return new URL(`../assets/images/themify/${getDarkMode.value}/${imagePath}`, import.meta.url).href;
- // case ThemeEnum.VENT1:
- // return new URL(`../assets/images/themify/${getDarkMode.value}/${imagePath}`, import.meta.url).href;
- default:
- return new URL(`../assets/images/${imagePath}`, import.meta.url).href;
- }
- };
|