Designed by Maxim Popov ecto ru function delay_move id var obj documen

 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
/*Designed by Maxim Popov — ecto.ru */
function delay_move(id)
{
var obj=document.getElementById(id);
var startx;
var starty;
var destx;
var desty;
var fullPath;
var halfFullPath;
var maxSpeed=10;
var timeout;
var sinu;
var cosi;
var nowx;
var nowy;
var speed=0;
var speedOfSpeed=10;
this.move=function(x,y)
{
destx=x;
desty=y;
nowx=obj.offsetLeft;
nowy=obj.offsetTop;
startx=obj.offsetLeft;
starty=obj.offsetTop;
fullPath=countPath(nowx,nowy);
halfFullPath=fullPath/2;
window.clearInterval(timeout);
timeout=window.setInterval(moving,15);
sinu=(destx-nowx)/fullPath;
cosi=(desty-nowy)/fullPath;
speed=0;
}
function moving()
{
if((path=countPath(nowx,nowy))<=1)
{
setPosition(destx,desty);
window.clearInterval(timeout);
return false;
}
if(path>halfFullPath)
{
speed=(fullPath-path)/speedOfSpeed+0.5;
//speed+=0.4 //linear speed
}
else
{
speed=(path)/speedOfSpeed+0.5;
//speed-=0.4 //linear speed
}
if(speed>maxSpeed)
speed=maxSpeed;
if(speed<0.1)
speed=0.1;
setPosition(nowx+speed*sinu,nowy+speed*cosi);
return true;
}
function setPosition(x,y)
{ obj.style.left=x+px; obj.style.top=y+px;
nowx=x;
nowy=y;
}
function countPath(x,y)
{
return Math.sqrt(Math.pow(x-destx,2)+Math.pow(y-desty,2));
}
}