<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>粒子模型动态背景</title>
<style>
*{margin: 0;padding:0;}
html,body{height:100%;width:100%;}
canvas{display: block;}
#myCanvas{background:#001022;}
#myButtons{bottom:20px;left:20px;position:absolute;}
#myButtons button{padding:15px;}
</style>
</head>
<body >
<canvas id="myCanvas" width="500" height="500"></canvas>
<div id="myButtons">
<button id="refreshBtn">刷新</button>
<button id="acceleBtn">加速</button>
<button id="startAnimation">Start</button>
<button id="stopAnimation">Stop</button>
</div>
</body>
</html>
<script src="http://cdn.bootcss.com/jquery/2.1.4/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
/*控制模块*/
$("#refreshBtn").click(function(event) {myGetAsteroid()});
$("#acceleBtn").click(function(event) {animate()});
var playAnimation = true;
var startButton = $('#startAnimation');
var stopButton = $('#stopAnimation');
startButton.hide();
startButton.click(function() {
$(this).hide();
stopButton.show();
playAnimation=true;
animate();
});
stopButton.click(function(){
$(this).hide();
startButton.show();
playAnimation=false;
});
var canvas=$('#myCanvas');
var context=canvas.get(0).getContext("2d");
var canvasWidth = canvas.width();
var canvasHeight= canvas.height();
/*更新画布大小*/
$(window).resize(resizeCanvas);
function resizeCanvas(){
canvas.attr("width",$(window).get(0).innerWidth);
canvas.attr("height",$(window).get(0).innerHeight);
canvasWidth=canvas.width();
canvasHeight=canvas.height();
}
resizeCanvas();
/*物体属性*/
var Asteroid = function(x,y,radius,iscolor,vx,vy,ax,ay){
this.x=x;
this.y=y;
this.radius=radius;//大小
this.iscolor=iscolor;//颜色
this.vx=vx;//x轴速度
this.vy=vy;//y轴速度
this.ax=ax;//x轴加速度
this.ay=ay;//y轴加速度
}
/*创建物体实体*/
var asteroids = new Array();
var getAsteroid = function() {
for (var i = 0; i < 100; i++) {
var x=20+(Math.random()*(canvasWidth-40));
var y=20+(Math.random()*(canvasHeight-40));
var radius=2+Math.random()*10;
var iscolor='rgba('+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+',0.54)';
var vx=Math.random()*4-2;
var vy=Math.random()*4-2;
var ax=Math.random()*0.2-0.1;
var ay=Math.random()*0.2-0.1;
asteroids.push(new Asteroid(x,y,radius,iscolor,vx,vy,ax,ay));
};
}
getAsteroid();
/*重构物体实体*/
var myGetAsteroid = function () {
for (var i = 0; i < asteroids.length; i++) {
asteroids[i].x=20+(Math.random()*(canvasWidth-40));
asteroids[i].y=20+(Math.random()*(canvasHeight-40));
asteroids[i].radius=2+Math.random()*10;
asteroids[i].iscolor='rgba('+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+','+parseInt(Math.random()*255)+',0.54)';
asteroids[i].vx=Math.random()*4-2;
asteroids[i].vy=Math.random()*4-2;
asteroids[i].ax=Math.random()*0.2-0.1;
asteroids[i].ay=Math.random()*0.2-0.1;
}
}
function animate(){
context.clearRect(0,0,canvasWidth,canvasHeight);
context.fillStyle = "rgb(255,255,255)";
var asteroidsLength = asteroids.length;
for (var i = 0; i < asteroidsLength; i++) {
var tmpAsteroid = asteroids[i];
/*设置边界,防止物体运行到画布之外*/
if(tmpAsteroid.x - tmpAsteroid.radius < 0){
tmpAsteroid.x = tmpAsteroid.radius;
tmpAsteroid.vx *=-1;
tmpAsteroid.ax *=-1;
}else if(tmpAsteroid.x + tmpAsteroid.radius > canvasWidth){
tmpAsteroid.x = canvasWidth - tmpAsteroid.radius;
tmpAsteroid.vx *=-1;
tmpAsteroid.ax *=-1;
}
if(tmpAsteroid.y - tmpAsteroid.radius < 0){
tmpAsteroid.y = tmpAsteroid.radius;
tmpAsteroid.vy *=-1;
tmpAsteroid.ay *=-1;
}else if(tmpAsteroid.y + tmpAsteroid.radius > canvasHeight){
tmpAsteroid.y = canvasHeight - tmpAsteroid.radius;
tmpAsteroid.vy *=-1;
tmpAsteroid.ay *=-1;
}
/*限制物体最大移动速度*/
if(Math.abs(tmpAsteroid.vx)<10){
tmpAsteroid.vx+=tmpAsteroid.ax
}
if(Math.abs(tmpAsteroid.vy)<10){
tmpAsteroid.vy+=tmpAsteroid.ay
}
/*摩擦力*/
if(Math.abs(tmpAsteroid.vx)>0.1){
tmpAsteroid.vx*=0.9;
}else{
tmpAsteroid.vx*=0.1;
}
if(Math.abs(tmpAsteroid.vy)>0.1){
tmpAsteroid.vy*=0.9;
}else{
tmpAsteroid.vy*=0.1;
}
// 更新物体之间连线
context.beginPath();
for (var j = asteroidsLength-1; j > 0; j--) {
var p2 = asteroids[j];
var a = tmpAsteroid.x - p2.x
var b = tmpAsteroid.y - p2.y
var dist = Math.sqrt((a * a) + (b * b)).toFixed(2);
if (dist < 100) {
context.moveTo(tmpAsteroid.x + tmpAsteroid.vx, tmpAsteroid.y + tmpAsteroid.vy);
context.lineTo(p2.x + p2.vx, p2.y + p2.vy);
}
}
context.stroke();
context.strokeStyle = 'rgba(204,204,204,0.5)';
context.closePath();
var tx = tmpAsteroid.x+=tmpAsteroid.vx;
var ty = tmpAsteroid.y+=tmpAsteroid.vy;
var tr = tmpAsteroid.radius;
/*更新画布物体*/
context.beginPath();
context.fillStyle = tmpAsteroid.iscolor;
context.arc(tx,ty,tr,0,Math.PI*2,false);
context.closePath();
context.fill();
}
if(playAnimation){
requestAnimationFrame(animate);
}
}
animate();
});
</script>