Da'sBlog

js-animate-canvas满屏小球动画

逻辑简单 效果不错
https://codepen.io/whqet/pen/bNWGaj

Canvas is not supported in your browser.

body {
overflow:hidden;
}

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
// requestAnimationFrame polyfill by Erik Möller.
if (!Date.now)
Date.now = function() { return new Date().getTime(); };
(function() {
'use strict';
var vendors = ['webkit', 'moz'];
for (var i = 0; i < vendors.length && !window.requestAnimationFrame; ++i) {
var vp = vendors[i];
window.requestAnimationFrame = window[vp+'RequestAnimationFrame'];
window.cancelAnimationFrame = (window[vp+'CancelAnimationFrame']
|| window[vp+'CancelRequestAnimationFrame']);
}
if (/iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent) // iOS6 is buggy
|| !window.requestAnimationFrame || !window.cancelAnimationFrame) {
var lastTime = 0;
window.requestAnimationFrame = function(callback) {
var now = Date.now();
var nextTime = Math.max(lastTime + 16, now);
return setTimeout(function() { callback(lastTime = nextTime); },
nextTime - now);
};
window.cancelAnimationFrame = clearTimeout;
}
}());
var getRandomColor = function(){
return '#'+(Math.random()*0xffffff<<0).toString(16);
}
var canvas = document.getElementById("motion"),
c = canvas.getContext("2d"),
particles = {},
particleIndex = 0,
particleNum = 0.2;
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
function Particle(){
this.x = canvas.width/2;
this.y = canvas.height/2;
this.vx = Math.random() * 6 - 3;
this.vy = Math.random() * 4 - 2;
this.growth = ( Math.abs(this.vx) + Math.abs(this.vy) ) * 0.007;
particleIndex++;
particles[particleIndex] = this;
this.id = particleIndex;
this.size = Math.random() * 1;
this.color = getRandomColor();
}
Particle.prototype.draw = function(){
this.x += this.vx;
this.y += this.vy;
this.size += this.growth;
if(this.x > canvas.width || this.y > canvas.height){
delete particles[this.id];
}
c.fillStyle = this.color;
c.beginPath();
c.arc(this.x, this.y, this.size,0*Math.PI,2*Math.PI);
c.fill();
};
function animate(){
requestAnimationFrame( animate );
c.fillStyle = "#000";
c.fillRect(0,0,canvas.width,canvas.height);
if(Math.random() > particleNum){
new Particle();
}
for(var i in particles){
particles[i].draw();
}
}
requestAnimationFrame( animate );
坚持原创技术分享,您的支持将鼓励我继续创作!