fei.wang
2025-04-18 11f6acee504c77a8919a4e0ddfe3e70a746e3522
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
雪花特效
*/
 
<template>
    <canvas ref="canvas" class="canvas"></canvas>
  </template>
  <script>
  
  class Snowflake {
    constructor(x, y, speed, radius) {
      this.x = x;
      this.y = y;
      this.speed = speed;
      this.radius = radius;
    }
    update() {
      this.y += this.speed;
      if (this.y > window.innerHeight) {
        this.y = -this.radius;
      }
    }
    draw(ctx) {
      ctx.beginPath();
      ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
      ctx.fillStyle = '#fff';
      ctx.fill();
    }
  }
  export default {
    data() {
      return {
        snowflakes: [],
      };
    },
    mounted() {
      this.canvas = this.$refs.canvas;
      this.ctx = this.canvas.getContext('2d');
      this.canvas.width = window.innerWidth;
      this.canvas.height = window.innerHeight;
      window.addEventListener('resize', () => {
        this.canvas.width = window.innerWidth;
        this.canvas.height = window.innerHeight;
      });
      this.animate();
    },
    methods: {
      animate() {
        this.ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);
        if (Math.random() < 0.05) {
          const x = Math.random() * window.innerWidth;
          const y = -10;
          const speed = Math.random() * 3 + 1;
          const radius = Math.random() * 3 + 1;
          this.snowflakes.push(new Snowflake(x, y, speed, radius));
        }
        this.snowflakes.forEach((snowflake) => {
          snowflake.update();
          snowflake.draw(this.ctx);
        });
        requestAnimationFrame(this.animate);
      },
    },
  };
  </script>
  <style>
  .canvas {
    position: fixed;
    top: 0;
    left: 0;
    background-color: #16222a;
    z-index: 9999;
  }
  </style>