点击查看代码
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2 <HTML>
3 <HEAD>
4 <TITLE> ❤🧡💛💚泉泉💜🤎🖤🤍</TITLE>
5 <META NAME="Generator" CONTENT="EditPlus">
6 <META NAME="Author" CONTENT="">
7 <META NAME="Keywords" CONTENT="">
8 <META NAME="Description" CONTENT="">
9 <style>
10 html, body {
11 height: 100%;
12 padding: 0;
13 margin: 0;
14 background: #000;
15 }
16 canvas {
17 position: absolute;
18 width: 100%;
19 height: 100%;
20 }
21 </style>
22 </HEAD>
23 <BODY>
24 <canvas id="pinkboard"></canvas>
25 <script>
26 /*
27 * Settings
28 */
29 var settings = {
30 particles: {
31 length: 500, // maximum amount of particles
32 duration: 2, // particle duration in sec
33 velocity: 100, // particle velocity in pixels/sec
34 effect: -0.75, // play with this for a nice effect
35 size: 30, // particle size in pixels
36 },
37 };
38
39 /*
40 * RequestAnimationFrame polyfill by Erik Möller
41 */
42 (function(){var b=0;var c=["ms","moz","webkit","o"];for(var a=0;a<c.length&&!window.requestAnimationFrame;++a){window.requestAnimationFrame=window[c[a]+"RequestAnimationFrame"];window.cancelAnimationFrame=window[c[a]+"CancelAnimationFrame"]||window[c[a]+"CancelRequestAnimationFrame"]}if(!window.requestAnimationFrame){window.requestAnimationFrame=function(h,e){var d=new Date().getTime();var f=Math.max(0,16-(d-b));var g=window.setTimeout(function(){h(d+f)},f);b=d+f;return g}}if(!window.cancelAnimationFrame){window.cancelAnimationFrame=function(d){clearTimeout(d)}}}());
43
44 /*
45 * Point class
46 */
47 var Point = (function() {
48 function Point(x, y) {
49 this.x = (typeof x !== 'undefined') ? x : 0;
50 this.y = (typeof y !== 'undefined') ? y : 0;
51 }
52 Point.prototype.clone = function() {
53 return new Point(this.x, this.y);
54 };
55 Point.prototype.length = function(length) {
56 if (typeof length == 'undefined')
57 return Math.sqrt(this.x * this.x + this.y * this.y);
58 this.normalize();
59 this.x *= length;
60 this.y *= length;
61 return this;
62 };
63 Point.prototype.normalize = function() {
64 var length = this.length();
65 this.x /= length;
66 this.y /= length;
67 return this;
68 };
69 return Point;
70 })();
71
72 /*
73 * Particle class
74 */
75 var Particle = (function() {
76 function Particle() {
77 this.position = new Point();
78 this.velocity = new Point();
79 this.acceleration = new Point();
80 this.age = 0;
81 }
82 Particle.prototype.initialize = function(x, y, dx, dy) {
83 this.position.x = x;
84 this.position.y = y;
85 this.velocity.x = dx;
86 this.velocity.y = dy;
87 this.acceleration.x = dx * settings.particles.effect;
88 this.acceleration.y = dy * settings.particles.effect;
89 this.age = 0;
90 };
91 Particle.prototype.update = function(deltaTime) {
92 this.position.x += this.velocity.x * deltaTime;
93 this.position.y += this.velocity.y * deltaTime;
94 this.velocity.x += this.acceleration.x * deltaTime;
95 this.velocity.y += this.acceleration.y * deltaTime;
96 this.age += deltaTime;
97 };
98 Particle.prototype.draw = function(context, image) {
99 function ease(t) {
100 return (--t) * t * t + 1;
101 }
102 var size = image.width * ease(this.age / settings.particles.duration);
103 context.globalAlpha = 1 - this.age / settings.particles.duration;
104 context.drawImage(image, this.position.x - size / 2, this.position.y - size / 2, size, size);
105 };
106 return Particle;
107 })();
108
109 /*
110 * ParticlePool class
111 */
112 var ParticlePool = (function() {
113 var particles,
114 firstActive = 0,
115 firstFree = 0,
116 duration = settings.particles.duration;
117
118 function ParticlePool(length) {
119 // create and populate particle pool
120 particles = new Array(length);
121 for (var i = 0; i < particles.length; i++)
122 particles[i] = new Particle();
123 }
124 ParticlePool.prototype.add = function(x, y, dx, dy) {
125 particles[firstFree].initialize(x, y, dx, dy);
126
127 // handle circular queue
128 firstFree++;
129 if (firstFree == particles.length) firstFree = 0;
130 if (firstActive == firstFree ) firstActive++;
131 if (firstActive == particles.length) firstActive = 0;
132 };
133 ParticlePool.prototype.update = function(deltaTime) {
134 var i;
135
136 // update active particles
137 if (firstActive < firstFree) {
138 for (i = firstActive; i < firstFree; i++)
139 particles[i].update(deltaTime);
140 }
141 if (firstFree < firstActive) {
142 for (i = firstActive; i < particles.length; i++)
143 particles[i].update(deltaTime);
144 for (i = 0; i < firstFree; i++)
145 particles[i].update(deltaTime);
146 }
147
148 // remove inactive particles
149 while (particles[firstActive].age >= duration && firstActive != firstFree) {
150 firstActive++;
151 if (firstActive == particles.length) firstActive = 0;
152 }
153
154
155 };
156 ParticlePool.prototype.draw = function(context, image) {
157 // draw active particles
158 if (firstActive < firstFree) {
159 for (i = firstActive; i < firstFree; i++)
160 particles[i].draw(context, image);
161 }
162 if (firstFree < firstActive) {
163 for (i = firstActive; i < particles.length; i++)
164 particles[i].draw(context, image);
165 for (i = 0; i < firstFree; i++)
166 particles[i].draw(context, image);
167 }
168 };
169 return ParticlePool;
170 })();
171
172 /*
173 * Putting it all together
174 */
175 (function(canvas) {
176 var context = canvas.getContext('2d'),
177 particles = new ParticlePool(settings.particles.length),
178 particleRate = settings.particles.length / settings.particles.duration, // particles/sec
179 time;
180
181 // get point on heart with -PI <= t <= PI
182 function pointOnHeart(t) {
183 return new Point(
184 160 * Math.pow(Math.sin(t), 3),
185 130 * Math.cos(t) - 50 * Math.cos(2 * t) - 20 * Math.cos(3 * t) - 10 * Math.cos(4 * t) + 25
186 );
187 }
188
189 // creating the particle image using a dummy canvas
190 var image = (function() {
191 var canvas = document.createElement('canvas'),
192 context = canvas.getContext('2d');
193 canvas.width = settings.particles.size;
194 canvas.height = settings.particles.size;
195 // helper function to create the path
196 function to(t) {
197 var point = pointOnHeart(t);
198 point.x = settings.particles.size / 2 + point.x * settings.particles.size / 350;
199 point.y = settings.particles.size / 2 - point.y * settings.particles.size / 350;
200 return point;
201 }
202 // create the path
203 context.beginPath();
204 var t = -Math.PI;
205 var point = to(t);
206 context.moveTo(point.x, point.y);
207 while (t < Math.PI) {
208 t += 0.01; // baby steps!
209 point = to(t);
210 context.lineTo(point.x, point.y);
211 }
212 context.closePath();
213 // create the fill
214 context.fillStyle = '#ea80b0';
215 context.fill();
216 // create the image
217 var image = new Image();
218 image.src = canvas.toDataURL();
219 return image;
220 })();
221
222 // render that thing!
223 function render() {
224 // next animation frame
225 requestAnimationFrame(render);
226
227 // update time
228 var newTime = new Date().getTime() / 1000,
229 deltaTime = newTime - (time || newTime);
230 time = newTime;
231
232 // clear canvas
233 context.clearRect(0, 0, canvas.width, canvas.height);
234
235 // create new particles
236 var amount = particleRate * deltaTime;
237 for (var i = 0; i < amount; i++) {
238 var pos = pointOnHeart(Math.PI - 2 * Math.PI * Math.random());
239 var dir = pos.clone().length(settings.particles.velocity);
240 particles.add(canvas.width / 2 + pos.x, canvas.height / 2 - pos.y, dir.x, -dir.y);
241 }
242
243 // update and draw particles
244 particles.update(deltaTime);
245 particles.draw(context, image);
246 }
247
248 // handle (re-)sizing of the canvas
249 function onResize() {
250 canvas.width = canvas.clientWidth;
251 canvas.height = canvas.clientHeight;
252 }
253 window.onresize = onResize;
254
255 // delay rendering bootstrap
256 setTimeout(function() {
257 onResize();
258 render();
259 }, 10);
260 })(document.getElementById('pinkboard'));
261 </script>
262 </BODY>
263 </HTML>
转自:https://github.com/Sivan-w/lovePatterm