-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbeautiful_test.html
140 lines (127 loc) · 4.18 KB
/
beautiful_test.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>canvas彩色小球组合动画</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<style>
*{margin: 0;padding: 0;}
html,body{height: 100%;}
body{background:#131313;overflow: hidden;}
.btn{position: absolute;bottom: 100px;left: 50%;transform: translateX(-50%);}
input{width: 120px;height: 30px;background: transparent;border: 1px solid #fff;color:#fff;font-size: 14px;line-height: 30px;cursor: pointer;transition: all 0.3s ease;border-radius:5px;}
input:hover{background: #fff;color:#131313;}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div class="btn">
<input id="random" type="button" value="Random">
<input id="circle" type="button" value="Circle">
<input id="heart" type="button" value="Heart">
<input id="wave" type="button" value="Wave">
</div>
<script>
var WINDOW_WIDTH = document.body.offsetWidth||document.documentElement.offsetWidth;
var WINDOW_HEIGHT = document.body.offsetHeight||document.documentElement.offsetHeight;
var canvas;
var context;
var balls = [];
var num = 88;
window.onload = function(){
canvas = document.getElementById('canvas');
canvas.width = WINDOW_WIDTH;
canvas.height = WINDOW_HEIGHT;
context = canvas.getContext('2d');
addBalls();
setInterval(function(){
drawBalls();
},100);
document.getElementById('random').onclick = random;
document.getElementById('circle').onclick = circle;
document.getElementById('heart').onclick = heart;
document.getElementById('wave').onclick = wave;
}
function wave(){
for (var i = 0 ; i < num ; i++){
var Mrnd = Math.round(Math.random()*WINDOW_WIDTH);
balls[i].gotoX = Mrnd;
balls[i].gotoY = WINDOW_HEIGHT/2 + Math.sin((2*Math.PI)/360 * Mrnd)*100;
}
}
function heart(){
//x = 16 sin^3 t, y = (13 cos t - 5 cos 2t - 2 cos 3t - cos 4t)
//100 + r * (16 * Math.pow(Math.sin(t), 3));
//50 - r * (13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t));
for (var i = 0 ; i < num ; i++){
var deg = Math.random()*(2*Math.PI);
balls[i].gotoX = WINDOW_WIDTH/2 - (16 * Math.pow(Math.sin(deg) , 3)) * 15;
balls[i].gotoY = WINDOW_HEIGHT/2 - (13 * Math.cos(deg) - 5 * Math.cos( 2 * deg ) - 2 *Math.cos(3*deg) - Math.cos(4*deg)) * 15;
}
}
function circle(){
for (var i = 0 ; i < num ; i++){
var deg = Math.random()*(2*Math.PI);
balls[i].gotoX = WINDOW_WIDTH/2 + Math.sin(deg) * 200;
balls[i].gotoY = WINDOW_HEIGHT/2 + Math.cos(deg) * 200;
}
}
function random(){
for (var i = 0 ; i < num ; i++){
balls[i].gotoX = parseInt(Math.random()*WINDOW_WIDTH);
balls[i].gotoY = parseInt(Math.random()*WINDOW_HEIGHT);
}
}
function changeCrood(i){
if(balls[i].x<balls[i].gotoX){
balls[i].x += (balls[i].gotoX - balls[i].x)/3;
if(balls[i].x>=balls[i].gotoX){
balls[i].x = balls[i].gotoX;
}
}else if(balls[i].x>balls[i].gotoX){
balls[i].x += (balls[i].gotoX - balls[i].x)/3;
if(balls[i].x<=balls[i].gotoX){
balls[i].x = balls[i].gotoX;
}
}
if(balls[i].y<balls[i].gotoY){
balls[i].y += (balls[i].gotoY - balls[i].y)/3;
if(balls[i].y>=balls[i].gotoY){
balls[i].y = balls[i].gotoY;
}
}else if(balls[i].y>balls[i].gotoY){
balls[i].y += (balls[i].gotoY - balls[i].y)/3;
if(balls[i].y<=balls[i].gotoY){
balls[i].y = balls[i].gotoY;
}
}
}
function drawBalls(){
context.clearRect(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
for(var i = 0; i < num ; i++){
changeCrood(i);
context.beginPath();
context.fillStyle = balls[i].color;
context.arc(balls[i].x , balls[i].y , balls[i].r , 0 , Math.PI*2 , true);
context.fill();
context.closePath();
}
}
function addBalls(){
for(var i = 0;i<num;i++){
var MrndX = parseInt(Math.random()*WINDOW_WIDTH);
var MrndY = parseInt(Math.random()*WINDOW_HEIGHT);
var aBall = {
x:MrndX,
y:MrndY,
r:parseInt(Math.random()*15+5),
color:'rgba('+Math.ceil(Math.random()*255)+','+Math.ceil(Math.random()*255)+','+Math.ceil(Math.random()*255)+',1)',
gotoX:MrndX,
gotoY:MrndY
}
balls.push(aBall);
}
}
</script>
</body>
</html>