diff --git a/src/assets/newImgs/HMScreen/snow.png b/src/assets/newImgs/HMScreen/snow.png new file mode 100644 index 0000000..11e9499 --- /dev/null +++ b/src/assets/newImgs/HMScreen/snow.png Binary files differ diff --git a/src/assets/newImgs/HMScreen/snow.png b/src/assets/newImgs/HMScreen/snow.png new file mode 100644 index 0000000..11e9499 --- /dev/null +++ b/src/assets/newImgs/HMScreen/snow.png Binary files differ diff --git a/src/views/sponeScreen/cityGK/canvasRain.vue b/src/views/sponeScreen/cityGK/canvasRain.vue index 51730f9..769834a 100644 --- a/src/views/sponeScreen/cityGK/canvasRain.vue +++ b/src/views/sponeScreen/cityGK/canvasRain.vue @@ -1,7 +1,11 @@ @@ -12,9 +16,10 @@ // 画笔 const ctx = ref(null); +const ctxSnow = ref(null); // 画布的宽高 let w = window.innerWidth; -let h = window.innerWidth; +let h = window.innerHeight; // 存放雨滴的数组 let arr = []; // 雨滴的数量 @@ -78,8 +83,8 @@ function initCanvas() { const canvas = document.querySelector('#canvasRain'); ctx.value = canvas.getContext('2d'); - canvas.width = window.innerWidth; - canvas.height = window.innerHeight; + canvas.width = w; + canvas.height = h; init(ctx.value); // 1-12个月的不同降雨量变化 let rainArr = [ @@ -101,17 +106,71 @@ size.value = rainArr[i].rain; i++; if (i == 12) i = 0; - init(ctx.value); - }, 1500); + init(ctx.value); //降雨效果 + // 下雪效果 + if (i >= 2 && i <= 10) { + ctxSnow.value && ctxSnow.value.clearRect(0, 0, w, h); // 清除画布重新渲染 + ctxSnow.value = null; + } else { + initSnow(); + } + }, 2083); +} + +// 降雪效果 +function initSnow() { + const canvas2 = document.querySelector('#canvasSnow'); + ctxSnow.value = canvas2.getContext('2d'); + // 设置canvas画布大小 + canvas2.width = w; + canvas2.height = h; + + const config = { + number: 300, // 生成的雪花数量 + snowArr: [], + pic: 'https://www.deanhan.cn/wp-content/uploads/2017/12/snow.png', // 雪花图片 + }; + let snowImg = new Image(); + snowImg.src = config.pic; + + for (let i = 0; i < config.number; i++) { + config.snowArr.push({ + x: Math.random() * w, // 定义每片雪花的X轴 + y: Math.random() * h, // Y轴 + toX: Math.random(), // 雪花每一次渲染的X距离 + toY: Math.random() * 1 + 5, //速度 + size: Math.random() * 20 + 5, // 雪花的大小 + }); + } + + const dropAnimation = () => { + if (!!!ctxSnow.value) return; + ctxSnow.value.clearRect(0, 0, w, h); // 清除画布重新渲染 + for (let i = 0; i < config.snowArr.length; i++) { + let snow = config.snowArr[i]; + // ctx.beginPath(); //绘制圆形雪花 后面直接图片代替 + // ctx.arc(snow.x, snow.y, snow.size, 0, Math.PI * 2, true); + // ctx.fillStyle = '#666'; + // ctx.fill(); + ctxSnow.value.drawImage(snowImg, snow.x, snow.y, snow.size, snow.size); + + snow.x = snow.x > w ? 0 : snow.x + snow.toX; // 每调一次函数向右移动一点 + snow.y = snow.y > h ? 0 : snow.y + snow.toY; // 向下移动 + } + requestAnimationFrame(dropAnimation); + }; + requestAnimationFrame(dropAnimation); } onMounted(() => { initCanvas(); + initSnow(); }); onBeforeUnmount(() => { if (timer.value) clearInterval(timer.value); if (timerAll.value) clearInterval(timerAll.value); clearDraw(); + ctxSnow.value && ctxSnow.value.clearRect(0, 0, w, h); }); @@ -125,5 +184,17 @@ top: 0px; z-index: 100; pointer-events: none; + #canvasRain { + position: absolute; + left: 0px; + top: 0px; + z-index: 102; + } + #canvasSnow { + position: absolute; + left: 0px; + top: 0px; + z-index: 101; + } }