小球喷泉
<html> <head> <meta charset="utf-8"/> <style> body { margin: 0; padding: 0; height: 100%; width: 100%; overflow: hidden; background: #FFF5F5; } canvas { display: block; margin: 0px auto; } </style> </head> <body> <canvas id="画布"></canvas> </body> <script> window.onload = app(); var 画布; function app() { var 绘图环境; var 小球集合 = []; var 初始化 = function() { 画布 = document.getElementById("画布"); 画布.width = document.body.scrollWidth; 画布.height = (document.documentElement.scrollHeight > document.body.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight); 绘图环境 = 画布.getContext("2d"); 开始(); }; var 开始 = function() { var x = -10, i = 1; var timer = setInterval(function(){ 小球集合.push(new 小球()); 小球集合.push(new 小球()); 小球集合.push(new 小球()); 小球集合.push(new 小球()); 小球集合.push(new 小球()); 小球集合.push(new 小球()); 小球集合.push(new 小球()); 小球集合.push(new 小球()); 小球集合.push(new 小球()); 小球集合.push(new 小球()); 小球集合.push(new 小球()); 小球集合.push(new 小球()); 画(小球集合); 选(小球集合); 更新(小球集合); }, 30); }; var 选 = function(列表) { for (var i = 列表.length - 1; i >= 0; i--) { if (列表[i].y > 画布.height - 10) { 列表[i].y = 画布.height - 10; 列表[i].vx = 列表[i].vy = 列表[i].ay = 0; } if (列表[i].colorA <= 0) { 小球集合.splice(i,1); } } } var 画 = function(列表) { 绘图环境.clearRect(0, 0, 画布.width, 画布.height); for (var i = 列表.length - 1; i >= 0; i--) { 绘图环境.fillStyle = 列表[i].color; 绘图环境.beginPath(); 绘图环境.arc(列表[i].x, 列表[i].y, 列表[i].r, 0,Math.PI*2,true); 绘图环境.closePath(); 绘图环境.fill(); } }; var 更新 = function(列表) { var 小球; for (var i = 列表.length - 1; i >= 0; i--) { 小球 = 列表[i]; 小球.x += 小球.vx; 小球.y += 小球.vy; 小球.vy += 小球.ay; 小球.colorA -= 0.006; 小球.color = 小球.colorRgb + 小球.colorA + ")"; } }; return 初始化 } var 小球 = function() { this.r = 3; this.colorRgb = "rgba(" + (255 - Math.floor(Math.random() * 200)) + "," + (255 - Math.floor(Math.random() * 200)) + "," + (255 - Math.floor(Math.random() * 200)) + ","; this.colorA = 1; this.color = this.colorRgb + this.colorA + ")"; this.x = 画布.width / 2; this.y = 画布.height - 10; this.vx = (Math.random() > 0.5 ? 1 : -1) * Math.random() * 6; this.vy = -21; this.ay = 0.55; }; </script> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <style> body { margin: 0; padding: 0; height: 100%; width: 100%; overflow: hidden; background: #000; } </style> </head> <body> <canvas id="画布"></canvas> <script> var 画布, 绘图环境, 小球集合 = [], 地面, 刷新时间 = 16.67; window.onload = function () { 画布 = document.getElementById("画布"); 画布.width = document.body.scrollWidth; 画布.height = (document.documentElement.scrollHeight > document.body.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight); 地面 = 画布.height - 10; 绘图环境 = 画布.getContext("2d"); var now = Date.now(), then, t; setInterval(重绘, 刷新时间); function 重绘() { then = now; now = Date.now(); t = now - then; var 新小球数量 = Math.floor(t / 4); for (var i = 0; i < 新小球数量; i++) 小球集合.push(new 小球()); 绘图环境.clearRect(0, 0, 画布.width, 画布.height); 画(小球集合); } function 画(列表) { for (var i = 列表.length - 1; i >= 0; i--) { var 当前球 = 列表[i]; 绘图环境.fillStyle = 当前球.color; 绘图环境.beginPath(); 绘图环境.arc(当前球.x, 当前球.y, 当前球.r, 0, Math.PI * 2); 绘图环境.closePath(); 绘图环境.fill(); if (当前球.y > 地面) { 当前球.y = 地面; 当前球.vx = 当前球.vy = 当前球.ay = 0; } if (当前球.colorA <= 0) { 小球集合.splice(i, 1); } 当前球 = 列表[i]; 当前球.x += 当前球.vx * t; 当前球.y += 当前球.vy * t; 当前球.vy += 当前球.ay * t; 当前球.colorA -= 当前球.hideSpeed * t; 当前球.color = 当前球.colorRgb + 当前球.colorA + ")"; } } }; function 小球() { this.colorRgb = "rgba(" + (255 - Math.floor(Math.random() * 200)) + "," + (255 - Math.floor(Math.random() * 200)) + "," + (255 - Math.floor(Math.random() * 200)) + ","; this.colorA = 1; this.color = this.colorRgb + this.colorA + ")"; this.x = 画布.width / 2; this.y = 地面; this.vx = (Math.random() - 0.5) * 0.2; this.vy = -0.4; } 小球.prototype.r = 5; 小球.prototype.ay = 0.0002; 小球.prototype.hideSpeed = 0.0002; </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <style> html, body { margin: 0; padding: 0; background-color: #000; } canvas { display: block; } </style> </head> <body> <canvas id="画布"></canvas> <script> window.onload = function () { var 画布, 绘图环境, 小球集合 = [], 地面, 刷新时间 = 16.67, 小球每毫秒数量 = 0.05; 画布 = document.getElementById("画布"); 画布.width = document.body.clientWidth; 画布.height = (document.documentElement.scrollHeight > document.body.clientHeight ? document.documentElement.scrollHeight : document.body.scrollHeight); 地面 = 画布.height - 10; 绘图环境 = 画布.getContext("2d"); var now = Date.now(), then, t; setInterval(重绘, 刷新时间); function 重绘() { then = now; now = Date.now(); t = now - then; var e = t*小球每毫秒数量; //小球期望密度值 var 新小球数量 = Math.floor(e) + ((Math.random()<(e - Math.floor(e)))?1:0); for (var i = 0; i < 新小球数量; i++) 小球集合.push(new 小球()); 绘图环境.clearRect(0, 0, 画布.width, 画布.height); 画(小球集合); } function 画(列表) { for (var i = 列表.length - 1; i >= 0; i--) { var 当前球 = 列表[i]; 绘图环境.fillStyle = 当前球.color; 绘图环境.beginPath(); 绘图环境.arc(当前球.x, 当前球.y, 当前球.r, 0, Math.PI * 2); 绘图环境.closePath(); 绘图环境.fill(); if (当前球.y > 地面) { 当前球.y = 地面; 当前球.vx = 当前球.vy = 当前球.ay = 0; } if (当前球.colorA <= 0) { 小球集合.splice(i, 1); } 当前球.x += 当前球.vx * t; 当前球.y += 当前球.vy * t; 当前球.vy += 当前球.ay * t; 当前球.colorA -= 当前球.hideSpeed * t; 当前球.color = 当前球.colorRgb + 当前球.colorA + ")"; } } function 小球() { this.colorRgb = "rgba(" + (255 - Math.floor(Math.random() * 200)) + "," + (255 - Math.floor(Math.random() * 200)) + "," + (255 - Math.floor(Math.random() * 200)) + ","; this.colorA = 1; this.color = this.colorRgb + this.colorA + ")"; this.x = 画布.width / 2; this.y = 地面; this.vx = (Math.random() - 0.5) * 0.2; this.vy = -0.4; } 小球.prototype.r = 5; 小球.prototype.ay = 0.0002; 小球.prototype.hideSpeed = 0.0002; }; </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>弹跳小球喷泉</title> <style> html, body { margin: 0; padding: 0; background-color: #000; } canvas { display: block; } </style> </head> <body> <canvas id="画布"></canvas> <script> window.addEventListener('load', function () { var 画布 = document.getElementById("画布"), 绘图环境 = 画布.getContext("2d"), 小球集合 = [], 刷新时间 = 16.67, 小球每毫秒数量 = 0.1; 画布.width = document.body.clientWidth; 画布.height = (document.documentElement.scrollHeight > document.body.clientHeight ? document.documentElement.scrollHeight : document.body.clientHeight); var 地面 = 画布.height - 10, now = Date.now(), then, t, twoPi = Math.PI * 2; setInterval(重绘, 刷新时间); function 重绘() { then = now; now = Date.now(); t = now - then; var e = t * 小球每毫秒数量; //小球密度期望值 var 新小球数量 = Math.floor(e) + ((Math.random() < (e - Math.floor(e))) ? 1 : 0); for (var i = 0; i < 新小球数量; i++) 小球集合.push(new 小球()); 绘图环境.clearRect(0, 0, 画布.width, 画布.height); for (var i = 小球集合.length - 1; i >= 0; i--) { var 当前球 = 小球集合[i]; 绘图环境.fillStyle = 当前球.getColor(); 绘图环境.beginPath(); 绘图环境.arc(当前球.x, 当前球.y, 当前球.r, 0, twoPi); 绘图环境.closePath(); 绘图环境.fill(); if (当前球.y > 地面 && 当前球.vy > 0) { 当前球.vy = -当前球.vy * (Math.random() * 0.5 + 0.25); } if (当前球.colorA <= 0) { 小球集合.splice(i, 1); } 当前球.move(t); } } function 小球() { this.colorRgb = "rgba(" + (255 - Math.floor(Math.random() * 200)) + "," + (255 - Math.floor(Math.random() * 200)) + "," + (255 - Math.floor(Math.random() * 200)) + ","; this.colorA = 1; this.x = 画布.width / 2; this.y = 地面; this.vx = (Math.random() - 0.5) * 0.2; this.vy = -0.4; } 小球.prototype.getColor = function () { return this.colorRgb + this.colorA + ")"; } 小球.prototype.move = function (t) { this.x += this.vx * t; this.y += this.vy * t; this.vy += this.ay * t; this.colorA -= this.hideSpeed * t; }; 小球.prototype.r = 5; 小球.prototype.ay = 0.0002; 小球.prototype.hideSpeed = 0.0001; }); </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>弹跳小球喷泉</title> <style> html, body { background-color: #000; margin: 0; overflow: hidden; height: 100%; } canvas[resize] { width: 100%; height: 100%; } </style> <script src="dist/paper-full.min.js"></script> <script type="text/paperscript" canvas="画布"> var 画布 = document.getElementById("画布"), 小球集合 = [], 小球每毫秒数量 = 0.01, 地面 = 画布.height - 10; var 出发点 = new Point(画布.width / 2, 地面), now = Date.now(), then, t, twoPi = Math.PI * 2, 重力加速度 = 0.0002, 小球消失速度 = 0.0001; function onFrame(event) { then = now; now = Date.now(); t = now - then; var e = t * 小球每毫秒数量; //小球密度期望值 var 新小球数量 = Math.floor(e) + ((Math.random() < (e - Math.floor(e))) ? 1 : 0), myCircle, 当前球; for (var i = 0; i < 新小球数量; i++) { myCircle = new Path.Circle(出发点, 5); myCircle.vx = (Math.random() - 0.5) * 0.2; myCircle.vy = -0.4; myCircle.move= function (t) { this.position.x += this.vx * t; this.position.y += this.vy * t; this.vy += 重力加速度 * t; this.opacity -= 小球消失速度 * t; }; myCircle.fillColor = "rgb(" + (255 - Math.floor(Math.random() * 200)) + "," + (255 - Math.floor(Math.random() * 200)) + "," + (255 - Math.floor(Math.random() * 200)) + ")"; myCircle.opacity = 1; 小球集合.push(myCircle); } for (i = 小球集合.length - 1; i >= 0; i--) { 当前球 = 小球集合[i]; if (当前球.position.y > 地面 && 当前球.vy > 0) { 当前球.vy = -当前球.vy * (Math.random() * 0.5 + 0.25); } if (当前球.opacity <= 0.03) { 小球集合.splice(i, 1); 当前球.remove(); } 当前球.move(t); } } </script> </head> <body> <canvas id="画布" resize></canvas> </body> </html>
[此贴子已经被作者于2016-6-24 01:06编辑过]
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>弹跳小球喷泉</title> <style> html, body { margin: 0; padding: 0; background-color: #000; } canvas { display: block; } </style> </head> <body> <canvas id="画布"></canvas> <script> 'use strict'; window.addEventListener('load', function () { var 画布 = document.getElementById("画布"), 绘图环境 = 画布.getContext("2d"), 小球集合 = [], 刷新时间 = 16.67, 小球每毫秒数量 = 0.02; 绘图环境.globalAlpha = 0.1; 画布.width = document.body.clientWidth; 画布.height = (document.documentElement.scrollHeight > document.body.clientHeight ? document.documentElement.scrollHeight : document.body.clientHeight); var 地面 = 画布.height - 10, now = Date.now(), then, t, twoPi = Math.PI * 2; setInterval(重绘, 刷新时间); function 重绘() { then = now; now = Date.now(); t = now - then; var e = t * 小球每毫秒数量; //小球密度期望值 var 新小球数量 = Math.floor(e) + ((Math.random() < (e - Math.floor(e))) ? 1 : 0); for (var i = 0; i < 新小球数量; i++) 小球集合.push(new 小球()); 绘图环境.clearRect(0, 0, 画布.width, 画布.height); for (var i = 小球集合.length - 1; i >= 0; i--) { var 当前球 = 小球集合[i]; for (var j=-3; j<4; j++) { 绘图环境.fillStyle = 当前球.color.replace(/,[0-9\.]*\)/, ','+当前球.colorA*(1-Math.abs(0.3*j))+')'); 绘图环境.beginPath(); 绘图环境.arc(当前球.x + j*当前球.vx*t*0.2, 当前球.y + j*当前球.vy*t*0.2, 当前球.r, 0, twoPi); 绘图环境.closePath(); 绘图环境.fill(); } if (当前球.y > 地面 && 当前球.vy > 0) { 当前球.vy = -当前球.vy * (Math.random() * 0.5 + 0.25); } if (当前球.colorA <= 0) { 小球集合.splice(i, 1); } 当前球.move(t); } } function 小球() { this.colorRgb = "rgba(" + (255 - Math.floor(Math.random() * 200)) + "," + (255 - Math.floor(Math.random() * 200)) + "," + (255 - Math.floor(Math.random() * 200)) + ","; this.colorA = 0.7; this.x = 画布.width / 2; this.y = 地面; this.vx = (Math.random() - 0.5) * 0.2; this.vy = -0.4; } 小球.prototype = { ay: 0.0002, get color() { return this.colorRgb + this.colorA + ")" }, hideSpeed: 0.0001, move: function(t) { this.x += this.vx * t; this.y += this.vy * t; this.vy += this.ay * t; this.colorA -= this.hideSpeed * t; }, r: 5, }; }); </script> </body> </html>