-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnow.html
87 lines (76 loc) · 1.73 KB
/
snow.html
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
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>canvas学习</title>
<style>
body{
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<!-- 雪花 -->
<canvas id="snowflake" style="background: black"></canvas>
</body>
<script type="text/javascript">
let canvas = document.getElementById("snowflake");
let ctx = canvas.getContext("2d");
let h = window.innerHeight;
let w = window.innerWidth;
canvas.width = w;
canvas.height = h;
window.onsize = function () {
h = window.innerHeight;
w = window.innerWidth;
canvas.width = w;
canvas.height = h;
};
let snow = function(x, y, r, c)
{
this.x = x;
this.y = y;
this.r = r;
this.c = c;
this.draw = function ()
{
ctx.beginPath();
ctx.fillStyle = this.c;
ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2, false);
ctx.stroke();
ctx.fill();
};
this.update = function(speed)
{
this.y = this.y + Math.random() * speed;//让雪往下下
if(this.y > h)
{
this.y = 0;
}
this.x = this.x - Math.random() * 0.5;//让雪往左下
if(this.x < 0)
{
this.x = w;
}
this.draw();
}
};
let point = [];
for(let i = 0; i < 1000; i ++)
{
let p = new snow(Math.random() * w, Math.random() * h, Math.random() * 5, "white");
point[i] = p;
p.draw();
}
setInterval(updateAll, 1000/60);
function updateAll()
{
ctx.clearRect(0, 0, w, h);
for(let i = 0; i < point.length; i++)
{
point[i].update(1);
}
}
</script>
</html>