1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>爱心</title>
6 <style>
7 *{margin:0; padding:0;}
8 body{ background-color: #1E1E1E; }
9 </style>
10 </head>
11 <body>
12
13 <canvas id="drawHeart"></canvas>
14
15 <script>
16 var hearts = [];
17 var canvas = document.getElementById('drawHeart');
18 var wW = window.innerWidth;
19 var wH = window.innerHeight;
20 // 创建画布
21 var ctx = canvas.getContext('2d');
22 // 创建图片对象
23 var heartImage = new Image();
24 heartImage.src = 'img/heart.svg';
25 var num = 100;
26
27 init();
28
29 window.addEventListener('resize', function(){
30 wW = window.innerWidth;
31 wH = window.innerHeight;
32 });
33 // 初始化画布大小
34 function init(){
35 canvas.width = wW;
36 canvas.height = wH;
37 for(var i = 0; i < num; i ){
38 hearts.push(new Heart(i%5));
39 }
40 requestAnimationFrame(render);
41 }
42
43 function getColor(){
44 var val = Math.random() * 10;
45 if(val > 0 && val <= 1){
46 return '#00f';
47 } else if(val > 1 && val <= 2){
48 return '#f00';
49 } else if(val > 2 && val <= 3){
50 return '#0f0';
51 } else if(val > 3 && val <= 4){
52 return '#368';
53 } else if(val > 4 && val <= 5){
54 return '#666';
55 } else if(val > 5 && val <= 6){
56 return '#333';
57 } else if(val > 6 && val <= 7){
58 return '#f50';
59 } else if(val > 7 && val <= 8){
60 return '#e96d5b';
61 } else if(val > 8 && val <= 9){
62 return '#5be9e9';
63 } else {
64 return '#d41d50';
65 }
66 }
67
68 function getText(){
69 var val = Math.random() * 10;
70 if(val > 1 && val <= 3){
71 return '爱你一辈子';
72 } else if(val > 3 && val <= 5){
73 return '感谢你';
74 } else if(val > 5 && val <= 8){
75 return '喜欢你';
76 } else{
77 return 'I Love You';
78 }
79 }
80
81 function Heart(type){
82 this.type = type;
83 // 初始化生成范围
84 this.x = Math.random() * wW;
85 this.y = Math.random() * wH;
86
87 this.opacity = Math.random() * .5 .5;
88
89 // 偏移量
90 this.vel = {
91 x: (Math.random() - .5) * 5,
92 y: (Math.random() - .5) * 5
93 }
94
95 this.initialW = wW * .5;
96 this.initialH = wH * .5;
97 // 缩放比例
98 this.targetScale = Math.random() * .15 .02; // 最小0.02
99 this.scale = Math.random() * this.targetScale;
100
101 // 文字位置
102 this.fx = Math.random() * wW;
103 this.fy = Math.random() * wH;
104 this.fs = Math.random() * 10;
105 this.text = getText();
106
107 this.fvel = {
108 x: (Math.random() - .5) * 5,
109 y: (Math.random() - .5) * 5,
110 f: (Math.random() - .5) * 2
111 }
112 }
113
114 Heart.prototype.draw = function(){
115 ctx.save();
116 ctx.globalAlpha = this.opacity;
117 ctx.drawImage(heartImage, this.x, this.y, this.width, this.height);
118 ctx.scale(this.scale 1, this.scale 1);
119 if(!this.type){
120 // 设置文字属性
121 ctx.fillStyle = getColor();
122 ctx.font = 'italic ' this.fs 'px sans-serif';
123 // 填充字符串
124 ctx.fillText(this.text, this.fx, this.fy);
125 }
126 ctx.restore();
127 }
128 Heart.prototype.update = function(){
129 this.x = this.vel.x;
130 this.y = this.vel.y;
131
132 if(this.x - this.width > wW || this.x this.width < 0){
133 // 重新初始化位置
134 this.scale = 0;
135 this.x = Math.random() * wW;
136 this.y = Math.random() * wH;
137 }
138 if(this.y - this.height > wH || this.y this.height < 0){
139 // 重新初始化位置
140 this.scale = 0;
141 this.x = Math.random() * wW;
142 this.y = Math.random() * wH;
143 }
144
145 // 放大
146 this.scale = (this.targetScale - this.scale) * .1;
147 this.height = this.scale * this.initialH;
148 this.width = this.height * 1.4;
149
150 // -----文字-----
151 this.fx = this.fvel.x;
152 this.fy = this.fvel.y;
153 this.fs = this.fvel.f;
154
155 if(this.fs > 50){
156 this.fs = 2;
157 }
158
159 if(this.fx - this.fs > wW || this.fx this.fs < 0){
160 // 重新初始化位置
161 this.fx = Math.random() * wW;
162 this.fy = Math.random() * wH;
163 }
164 if(this.fy - this.fs > wH || this.fy this.fs < 0){
165 // 重新初始化位置
166 this.fx = Math.random() * wW;
167 this.fy = Math.random() * wH;
168 }
169 }
170
171 function render(){
172 ctx.clearRect(0, 0, wW, wH);
173 for(var i = 0; i < hearts.length; i ){
174 hearts[i].draw();
175 hearts[i].update();
176 }
177 requestAnimationFrame(render);
178 }
179 </script>
180 </body>
181 </html>