-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
169 lines (146 loc) · 4.19 KB
/
index.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>
基于canvas的超酷跟随鼠标粒子动画|DEMO_jQuery之家-自由分享jQuery、html5、css3的插件库
</title>
<!--CSS RESET-->
<!--演示页面样式,使用时可以不引用-->
<style>
html {
margin: 0;
padding: 0;
border: 0;
font-family: "Nunito", sans-serif;
}
canvas {
margin: 0;
padding: 0;
display: block;
touch-action: none;
}
</style>
</head>
<body>
<canvas></canvas>
<script>
var canvas = document.querySelector("canvas");
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
c = canvas.getContext("2d");
window.addEventListener("resize", function () {
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
initCanvas();
});
var mouse = {
x: undefined,
y: undefined,
};
window.addEventListener("mousemove", function (event) {
mouse.x = event.x;
mouse.y = event.y;
drawCircles();
});
window.addEventListener("touchmove", function (event) {
let touch = event.touches[0];
mouse.x = touch.clientX;
mouse.y = touch.clientY;
drawCircles();
});
function Circle(x, y, radius, vx, vy, rgb, opacity, birth, life) {
this.x = x;
this.y = y;
this.radius = radius;
this.minRadius = radius;
this.vx = vx;
this.vy = vy;
this.birth = birth;
this.life = life;
this.opacity = opacity;
this.draw = function () {
c.beginPath();
c.arc(this.x, this.y, this.radius, Math.PI * 2, false);
c.fillStyle = "rgba(" + rgb + "," + this.opacity + ")";
c.fill();
};
this.update = function () {
if (this.x + this.radius > innerWidth || this.x - this.radius < 0) {
this.vx = -this.vx;
}
if (this.y + this.radius > innerHeight || this.y - this.radius < 0) {
this.vy = -this.vy;
}
this.x += this.vx;
this.y += this.vy;
this.opacity = 1 - ((frame - this.birth) * 1) / this.life;
if (frame > this.birth + this.life) {
for (let i = 0; i < circleArray.length; i++) {
if (
this.birth == circleArray[i].birth &&
this.life == circleArray[i].life
) {
circleArray.splice(i, 1);
break;
}
}
} else {
this.draw();
}
};
}
var circleArray = [];
function initCanvas() {
circleArray = [];
}
var colorArray = ["355,85,80", "9,80,100", "343,81,45"];
function drawCircles() {
for (let i = 0; i < 6; i++) {
let radius = Math.floor(Math.random() * 4) + 2;
let vx = Math.random() * 2 - 1;
let vy = Math.random() * 2 - 1;
let spawnFrame = frame;
let rgb = colorArray[Math.floor(Math.random() * colorArray.length)];
let life = 100;
circleArray.push(
new Circle(
mouse.x,
mouse.y,
radius,
vx,
vy,
rgb,
1,
spawnFrame,
life
)
);
}
}
var frame = 0;
function animate() {
requestAnimationFrame(animate);
frame += 1;
c.clearRect(0, 0, innerWidth, innerHeight);
for (let i = 0; i < circleArray.length; i++) {
circleArray[i].update();
}
}
initCanvas();
animate();
// This is just for demo purposes :
for (let i = 1; i < 110; i++) {
(function (index) {
setTimeout(function () {
mouse.x = 100 + i * 10;
mouse.y = 100;
drawCircles();
}, i * 10);
})(i);
}
</script>
</body>
</html>