-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas特效星空.html
81 lines (70 loc) · 1.82 KB
/
canvas特效星空.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
html,body {
margin:0;
overflow:hidden;
width:100%;
height:100%;
cursor:none;
background:black;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
var ctx = document.getElementById('canvas'),
content = ctx.getContext('2d'),
round = [],
WIDTH,
HEIGHT,
initRoundPopulation = 80;
WIDTH = document.documentElement.clientWidth;
HEIGHT = document.documentElement.clientHeight;
ctx.width = WIDTH;
ctx.height = HEIGHT;
function Round_item(index, x, y) {
this.index = index
this.x = x
this.y = y
this.r = Math.random() * 2 + 1
var alpha = (Math.floor(Math.random() * 10) + 1) / 10 / 2
this.color = "rgba(255, 255, 255," + alpha + ")"
}
function animate() {
content.clearRect(0, 0, WIDTH, HEIGHT)
for (var i in round) {
round[i].move()
}
requestAnimationFrame(animate)
}
Round_item.prototype.draw = function () {
content.fillStyle = this.color
content.shadowBlur = this.r * 2
content.beginPath()
content.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false)
content.closePath()
content.fill()
}
Round_item.prototype.move = function () {
this.y -= 0.15
if (this.y <= -10) {
this.y = HEIGHT + 10
}
this.draw()
}
function init () {
for (var i= 0; i < initRoundPopulation; i++) {
round[i] = new Round_item(i,Math.random() * WIDTH,Math.random() * HEIGHT);
round[i].draw();
}
animate();
}
init();
</script>
</body>
</html>