游来游去的小鱼

来源:互联网 发布:电梯调度算法 编辑:程序博客网 时间:2024/06/10 18:45
 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>particle-4</title>
<meta http-equiv="imagetoolbar" content="no">
<style type="text/css">
 html {
  overflow: hidden;
 }
 body {
  margin: 0px;
  padding: 0px;
  background: #ccc;
  position: absolute;
  width: 100%;
  height: 100%;
  cursor: crosshair;
 }
 #SF {
  position: absolute;
  left: 203px;
  top: 28px;
  width: 70%;
  height: 70%;
  overflow: hidden;
  background: #fff;
 }
 #SF img {
  cursor: pointer;
  position: absolute;
 }
 #FPS {
  position: absolute;
  right: 5px;
  bottom: 5px;
  font-size: 10px;
  color: #666;
  font-family: courier;
 }
</style>
<script type="text/javascript">
//parentNode.removeChildeg Rd朣_MR剉 <book> CQ }0
var D  = 150;
var NB = 15;
var o = [];
var xm = 0;
var ym = 0;
var R  = 1 / (D * D);
var fps = 0;
var M = 0;
function resize() {
 nx = document.getElementById("SF").offsetWidth;
 ny = document.getElementById("SF").offsetHeight;
}
onresize = resize;
obZ = function() {
 this.x  = Math.random() * nx;
 this.y  = Math.random() * ny;
 this.vx = 0;
 this.vy = 0;
 this.O = new RotImg(this, document.getElementById("SF"), [
  ["p1", 0,0],
  ["p2", 8,-30],
  ["p2", 8,30],
  ["p5", 20,180]
 ]);
}
function RotImg(parent, obj, param){
 this.param = param;
 for(var i in param){
  s = param[i];
  m = document.createElement("img");
  m.src = document.getElementById(s[0]).src;
  m.obj = parent;
  obj.appendChild(m);
  param[i][0] = m;
  param[i][3] = m.width ;
  param[i][4] = m.height ;
  param[i][2] *= Math.PI/180;
 }
 this.position = function (x,y, angle){
  with (this) {
   for(var i in param){
    a = ( angle + param[i][2]);
    r = param[i][1];
    with(param[i][0].style){
     left = Math.round(-param[i][3] + x + Math.cos(a) * r) + 'px';
     top  = Math.round(-param[i][4] + y + Math.sin(a) * r) + 'px';
    }
   }
  }
 }
}
function mainloop(){
 M--;
 fps++;
 for(var i=0; i<NB; i++){
  with(o[i]){
   for(var j=0; j<NB; j++){
    B = o[j];
   if(i != j){
     dx = x - B.x;
     dy = y - B.y;
     d  = (dx * dx + dy * dy);
     if(d < 2 * D * D){
      w = Math.exp(-(dx * dx * R)) *  Math.exp(-(dy * dy * R));
      // cohesion
      vx -= 1.8 * w * dx;
      vy -= 1.8 * w * dy;
      // avoidance
      if(B.x<x) vx += 100 * w; else vx -= 100 * w;
      if(B.y<y) vy += 100 * w; else vy -= 100 * w;
      // alignment
      b = w * B.vx;
      if(Math.abs(b)>10)b = (b / Math.abs(b)) * 10;
      vx += b;
      b = w * B.vy;
      if(Math.abs(b)>10)b = (b / Math.abs(b)) * 10;
      vy += b;
     }
    }
   }
   if(M > 0){
    dx = x - xm;
    dy = y - ym;
    w = Math.exp(-(dx * dx * R)) *  Math.exp(-(dy * dy * R));
    if(xm < x) ex = w; else ex = -w;
   if(ym < y) ey = w; else ey = -w;
    vx += 1000 * NB * ex;
    vy += 1000 * NB * ey;
   }
   vx *= .98;

   vy *= .98;

   x += vx / (NB * 2000);

   y += vy / (NB * 2000);

   if(x > nx + 10) x = -10; else if(x < -10) x = nx + 10;

   if(y > ny + 10) y = -10; else if(y < -10) y = ny + 10;

   O.position(x, y, Math.atan2(vy, vx));
  }
 }
 setTimeout("mainloop();", 1);
}
onload = function(){
 resize();
 xm = 20000;
 ym = 20000;
 for(var i=0; i<NB; i++)o[i] = new obZ()
 mainloop();
 setInterval("document.getElementById('FPS').innerHTML=fps+' fps';fps=0", 1000);
}
</script>
</head>
<body>
<div id="SF"></div>
<div style="visibility: hidden">
 <img id="p1" src="http://www.lanrentuku.com/down/js/images/12476486070.gif">
 <img id="p2" src="http://www.lanrentuku.com/down/js/images/12476486071.gif">
 <img id="p5" src="http://www.lanrentuku.com/down/js/images/12476486072.gif">
</div>
<div id="FPS"></span>
</body>
</html>

原创粉丝点击