Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to make a loop SLOW DOWN 1

Status
Not open for further replies.

Tels

IS-IT--Management
Jul 10, 2001
290
GB
Hi there, I hope someone can help me.
I have a script which moves layers around, so far I have got it to move the layers OK, but I want the layers to animate, not move instantly. Although the script moves the layer a pixel at a time, the loop is so quick the effect is lost.I can't seem to make setTimeout() work here, so I'm looking for any suggestion.
I'm quite inexperienced here so any help is appreciated.

// Code starts here, IE only
var the_layer; // the layer to be moved
var deststr // the dest in string form
var dest; // the dest in int form
var px_pos; // the position of "px" (lit)
var pixelstr
var cur_x // Current X coordinate
var cur_y // Current Y Coordinate


function layerMove(l_name, l_direction, l_pixelstr, l_delaystr){
l_pixels = parseInt(l_pixelstr); //Convert pixel to integer
l_delay = parseInt(l_delaystr); // Convert delay to integer
if(document.layers){alert("This code should not be run on non-IE browsers.");}
if(document.all){ //good to go for IE5+

// Next stage stores layer position in cur_x_real
cur_x = document.all[l_name].style.left; //current x position +"px"
px_pos = cur_x.indexOf("px"); // Find "px" in the buffer
var cur_x_str = cur_x.substring(0,px_pos); // snip off px (leaving number only)
var cur_x_real = parseInt(cur_x_str); // Convert to integer
alert("current _x_ position")
alert(cur_x_real);
// As above, stores layer y position in cur_y_real
cur_y = document.all[l_name].style.top; //current y position +"px"
px_pos = cur_y.indexOf("px"); // find "px" in the buffer
var cur_y_str = cur_y.substring(0,px_pos); // remove "px" !!
var cur_y_real = parseInt(cur_y_str); // Convert to integer
alert("current _y_ position")
alert(cur_y_real);

// The next few lines decide what to do (dependant on l_direction)
// THE NEXT PART IS THE PART I NEED TO LOOP
if(l_direction=='up'){
dest = (cur_y_real - l_pixels);
while(cur_y_real > dest){ // Loop while the current position is diff from destination
cur_y_real --;
document.all[l_name].style.top = cur_y_real; // Move towards destination
}
}
else if (l_direction=='down'){
dest = (cur_y_real + l_pixels);
while(cur_y_real < dest){
cur_y_real ++;
document.all[l_name].style.top = cur_y_real;
}
}
// more else if()'s here for other directions
// you get the idea

Mixed Linux/Win2000 Network Administrator
 
nothing here tells me you wouldn't be able to use setTimeout()

here is a simple way of sliding an object :

<html>
<head>
<title>simple slide</title>
<script>

function slideObjectTo(id, x, y)
{
var obj = document.getElementById(id);
var objx = parseInt(obj.style.left);
var objy = parseInt(obj.style.top);

if (x == objx && y == objy)
{
return;
}

if (objx > x)
{
obj.style.left = objx - 1;
}
else if (objx < x)
{
obj.style.left = objx + 1;
}


if (objy > y)
{
obj.style.top = objy - 1;
}
else if (objy < y)
{
obj.style.top = objy + 1;
}

window.status = document.getElementById(id).style.left + &quot; &quot; + document.getElementById(id).style.tops
setTimeout(&quot;slideObjectTo('&quot; +id + &quot;' ,&quot;+ x +&quot;,&quot;+ y +&quot;)&quot;, 20)
}

</script>
</head>
<body>

<a href=&quot;#&quot; onclick=&quot;slideObjectTo('myDiv',200,250)&quot;>slide</a>
<div id=&quot;myDiv&quot; style=&quot;position:absolute;left:100;top:100;&quot;>
Here a pile of text or html
Here a pile of text or html
Here a pile of text or html
Here a pile of text or html
Here a pile of text or html
Here a pile of text or html
Here a pile of text or html
Here a pile of text or html
Here a pile of text or html
Here a pile of text or html
Here a pile of text or html
Here a pile of text or html
Here a pile of text or html
Here a pile of text or html
Here a pile of text or html
</div>

</body>
</html> Gary Haran
 
Could not have hoped for a better answer. Not that it makes me any less proud of my big, blocky first attempt - I'm still learning y'know!! ;)

ps. can your code be used in Netscrape, and do you mind if I put it to work?

Thank you very much indeed
Tels
Mixed Linux/Win2000 Network Administrator
 
I hammered this together to call your script, but move layers relative to their current position (ie up down left right)
It does'nt stop the page from working, but it doesn't work!
any ideas?

function relativeShift(layerId,direction,amount){
var cur_x_pos = document.getElementById(layerId).style.left;
var cur_y_pos = document.getElementById(layerId).style.top;
var dest_x = cur_x_pos;
var dest_y = cur_y_pos;
if(direction==&quot;up&quot;)
{dest_y = cur_y_pos - amount; dest_x = cur_x_pos; slideObjectTo(layerId,dest_x,dest_y);}
else if(direction==&quot;down&quot;)
{dest_y = cur_y_pos + amount; dest_x = cur_x_pos; slideObjectTo(layerId,dest_x,dest_y);}
else if(direction==&quot;left&quot;)
{dest_y = cur_y_pos; dest_x = cur_x_pos - amount; slideObjectTo(layerId,dest_x,dest_y);}
else if(direction==&quot;right&quot;)
{dest_y = cur_y_pos; dest_x = cur_x_pos + amount; slideObjectTo(layerId,dest_x,dest_y);}
} Mixed Linux/Win2000 Network Administrator
 
how about you use this. First paste this code :

var _divs = new Array();
function Div(id)
{
var _dom = (document.getElementById);
var _ie = (document.all);
var _moz = (window.sidebar);

var obj = _dom ? document.getElementById(id) : document.all[id] ;

if (!obj)
{
return false;
}

obj._id = _divs.length;

obj.time = 10; // how long between moves
obj.distance = 2; // how far we move in one time unit

// visibility
obj.show = function(){obj.style.visibility = &quot;visible&quot;}
obj.hide = function(){obj.style.visibility = &quot;hidden&quot;}
obj.isVisible = function(){return (obj.style.visibility != &quot;hidden&quot;)}
obj.setZIndex = function(z){obj.style.zIndex = z}
obj.getZIndex = function(){return obj.style.zIndex}

// display
obj.setAsNone = function(){obj.style.display = &quot;none&quot;}
obj.setAsBlock = function(){obj.style.display = &quot;block&quot;}
obj.swapDisplay = function(){obj.style.display = (obj.style.display != &quot;none&quot;) ? &quot;none&quot; : &quot;block&quot;;}

// size
obj.setHeight = function(h){obj.style.height = h}
obj.setWidth = function(w){obj.style.width = w}
obj.getHeight = function(){return parseInt(obj.style.height)}
obj.getWidth = function(){return parseInt(obj.style.width) }
obj.resizeTo = function(h, w){obj.setHeight(h || obj.getHeight());obj.setWidth(w || obj.getWidth())}

// position
obj.setLeft = function(x){obj.style.left = x;return x}
obj.setTop = function(y){obj.style.top = y;return y}
obj.getLeft = function(){return parseInt(obj.style.left)}
obj.getTop = function(){return parseInt(obj.style.top)}
obj.getPosition = function(){return [obj.getLeft(), obj.getTop()]}
obj.moveTo = function(x, y){return [obj.setLeft(x), obj.setTop(y)]}
obj.moveBy = function(x, y)
{
return obj.moveTo( (x ? obj.getLeft() + x : obj.getLeft()),(y ? obj.getTop() + y : obj.getTop()) )
}

obj.slideTo = function(left, top, lod, nextAction)
{
if (!obj.sliding)
{
obj.slideFromPath(twoPointPath([obj.getLeft(), obj.getTop()], [left, top], lod), nextAction)
}
}

obj.slideFromPath = function(path, nextAction)
{
obj.nextAction = nextAction;
obj.path = path.reverse();
obj._startSlideFromPath();
}

obj._startSlideFromPath = function()
{
if (!obj.sliding)
{
obj.sliding = true;
obj._slide();
}
}

obj._startSlide = function()
{
if (obj.sliding) return
obj.sliding = true;
repsTotal = Math.sqrt(Math.pow(obj.leftTravelDistance, 2) + Math.pow(obj.topTravelDistance, 2)) / obj.distance
mX = obj.leftTravelDistance / repsTotal;
mY = obj.topTravelDistance / repsTotal;

obj.path = [[obj.getLeft(), obj.getTop()]];

for (var i = 0; i < repsTotal; i++)
{
var last = obj.path.last();
obj.path.push([last[0] + mX, last[1] + mY])
}

obj.path.reverse();
obj._slide()
}

obj._slide = function()
{
if (obj.sliding)
{
if (obj.path.length)
{
var point = obj.path.pop();
obj.moveTo(point[0], point[1]);
setTimeout(&quot;_divs[&quot; + obj._id + &quot;]._slide()&quot;, obj.time)
return false;
}
else
{
obj.repsDone = 0;
obj.repsTotal = 0;
obj.leftTravelDistance = 0;
obj.topTravelDistance = 0;
obj.sliding = false;
eval(obj.nextAction)
obj.nextAction = &quot;&quot;
return true;
}
}
}

// clipping
obj.clip = function(trbl)
{
obj.style.clip = &quot;rect(&quot; + trbl + &quot;)&quot;;
}

obj.getClip = function()
{
/rect\((\d{1,3})\D+(\d{1,3})\D+(\d{1,3})\D+(\d{1,3})\D+/i.test(obj.style.clip);
return [RegExp.$1, RegExp.$2, RegExp.$3, RegExp.$4]
}

// special
obj.getOpacity = function()
{
/opacity=(\d{1,3})/i.test(obj.style.filter);
return (_ie) ? RegExp.$1 : parseInt(obj.style.MozOpacity) ;
}

obj.setOpacity = function(o)
{
if (_moz)
{
obj.style.MozOpacity = o + &quot;%&quot;;
}
else if (obj.style.filter)
{
obj.style.filter = 'alpha(opacity=' + o +')'
}
}

obj.report = reporter;

_divs.push(obj)
return obj;
}

function fastBezierPoint(perc, start, flex1, flex2, end)
{
var oneMinusPerc = 1 - perc;
var bp1 = Math.pow(perc,3);
var bp2 = 3*Math.pow(perc, 2)*oneMinusPerc;
var bp3 = 3*perc*Math.pow(oneMinusPerc, 2);
var bp4 = Math.pow(oneMinusPerc, 3);

return [ start[0]*bp1 + flex1[0]*bp2 + flex2[0]*bp3 + end[0]*bp4,
start[1]*bp1 + flex1[1]*bp2 + flex2[1]*bp3 + end[1]*bp4 ]
}

function bezierPath(start, flex1, flex2, end, lod)
{
var iterations = lod ? lod : 100;
var iteration = iterations;
var path = [];
while (iteration--)
{
path.push(fastBezierPoint(1 / (iterations / iteration), start, flex1, flex2, end));
}
return path;
}

function twoPointPath(start, end, lod)
{
var iterations = lod ? lod : 100;

var xMoveBy = (start[0] - end[0]) / iterations;
var yMoveBy = (start[1] - end[1]) / iterations;

var path = [[start[0], start[1]]];
var last;

while (iterations--)
{
last = path.last();
path.push([last[0] - xMoveBy, last[1] - yMoveBy])
}
return path;
}

Then use it like so :

var myDiv = Div('myDiv');
myDiv.slideTo(200,250)
Gary Haran
 
wow
what does it do? Mixed Linux/Win2000 Network Administrator
 
Like it!! 8)
Tels Mixed Linux/Win2000 Network Administrator
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top