2d (Entry Nr. 39, by user 1 | edit) |
|
ge_2d =
{
getX: function(obj)
{
var curleft = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
}
else if (obj.x)
curleft += obj.x;
return curleft;
},
getY: function(obj)
{
var curtop = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curtop += obj.offsetTop
obj = obj.offsetParent;
}
}
else if (obj.y) curtop += obj.y;
return curtop;
}
}
// Old Stuff. Dont use in new code: ---------------------------------------------------------------------------
var mg_BrowserIsIE = (document.all) ? 1 : 0;
var mg_BrowserIsNS = (document.layers) ? 1 : 0;
function mg_setElementClass(id,ClassName)
{
document.getElementById(id).className=ClassName
}
function mg_setElementPosition(id,x,y)
{
if (mg_BrowserIsIE)
{
var obj = eval(id+".style");
obj.posLeft=x;
obj.posTop =y;
}
else
if (mg_BrowserIsNS)
{
var obj = eval("document."+id);
nspan.left=x;
nspan.top =y;
}
else
{
document.getElementById(id).style.left=x;
document.getElementById(id).style.top =y;
}
}
function mg_getElementX(id)
{
obj=document.getElementById(id);
var curleft = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
}
else if (obj.x)
curleft += obj.x;
return curleft;
}
function mg_getElementY(id)
{
obj=document.getElementById(id);
var curtop = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curtop += obj.offsetTop
obj = obj.offsetParent;
}
}
else if (obj.y)
curtop += obj.y;
return curtop;
//return document.getElementById(id).offsetTop;
}
function mg_2d_Element(id,x,y,FollowAim)
{
this.id=id;
this.x=x;
this.y=y;
this.SpeedX=0;
this.SpeedY=0;
this.AimX=x;
this.AimY=y;
this.FollowAim=FollowAim;
//document.getElementById(id).style.position="absolute";
this.redraw = function()
{
mg_setElementPosition(this.id,this.x,this.y);
}
this.timeStep = function()
{
this.x+=this.SpeedX;
this.y+=this.SpeedY;
mg_setElementPosition(this.id,this.x,this.y);
if (this.FollowAim)
{
DiffX=this.AimX-this.x;
DiffY=this.AimY-this.y;
this.SpeedX+=DiffX/100;
this.SpeedY+=DiffY/100;
}
// velocity
this.SpeedX*=0.9;
this.SpeedY*=0.9;
}
}
function mg_2d_Swarm(MyName)
{
this.MyName=MyName;
this.Elements=new Array();
// addElement ------------------------------------------------------
this.addElement=function(id,FollowAim)
{
pos_x=mg_getElementX(id);
pos_y=mg_getElementY(id);
this.Elements[id]=new mg_2d_Element(id,pos_x,pos_y,FollowAim);
//document.write("added to swarm: "+id+" ");
}
// ------------------------------------------------------ addElement
this.redraw=function()
{
for (i in this.Elements) this.Elements[i].redraw();
}
this.timeStep=function()
{
for (i in this.Elements) this.Elements[i].timeStep();
timer=setTimeout(MyName+".timeStep()",10);
}
this.start=function()
{
timer=setTimeout(MyName+".timeStep()",10);
for (i in this.Elements) document.getElementById(this.Elements[i].id).style.position="absolute";
}
} |
|
|
Create a new entry at this position
|
|
|