473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Animation - only first and last images display

I have some images that move positions fine using onmousedown or
onmousemove to feed a function the coordinates. It doesn't work when I
plug it into conditional loops with setTimeout to set the coordinates.
When I use loops to feed the MoveFunction its coordinates, all that
displays are the images in their initial positions, and then their final
positions (after the timeouts, etc.)

The timeouts are happening, but the screen fails to refresh the new
image positions during the timeouts. It appears that I need to add some
code that forces the browser to refresh so it can display the images as
they change.

Can anybody steer me in the right direction? There are a **ton** of
posts in Usenet about this but no definitive answers that I can find.

Thanks .......

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
4 4131
Paul LeBlanc <pa***********@msn.com> writes:
I have some images that move positions fine using onmousedown or
onmousemove to feed a function the coordinates. It doesn't work when I
plug it into conditional loops with setTimeout to set the coordinates.
Show us code!

Without the code, I have to guess blindly (something I am pretty good
at, but it's still not optimal for any of us).
When I use loops to feed the MoveFunction its coordinates, all that
displays are the images in their initial positions, and then their final
positions (after the timeouts, etc.)


Do you do something similar to:

var coords = [ ... ];
for(var i=0;i<coords.length;i++) {
setTimeout(function(){ setPosition(coords[i]); }, i*100);
}

, i.e., create a loop that makes a bunch of setTimeout's with different
delays, and expect each timeout event to set the coordiantes according
to the value of the index ("i" here)?

If so, then your problem is that all the functions refer to the same
variable "i", and it only have one value at a time. It can be any
variable the you expect to vary between the functions. It won't, there
is only one value, and a closure captures the reference to it, not
the value of it.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
Lee
Paul LeBlanc said:

I have some images that move positions fine using onmousedown or
onmousemove to feed a function the coordinates. It doesn't work when I
plug it into conditional loops with setTimeout to set the coordinates.
When I use loops to feed the MoveFunction its coordinates, all that
displays are the images in their initial positions, and then their final
positions (after the timeouts, etc.)


You're probably using setTimeout() incorrectly.
There are a couple of common errors that could cause these symptoms.
Show us your code.

Jul 20 '05 #3
Hi Lasse,
Sorry no code but I thought I was asking about code that I have yet to
write! Here's a sample of what I have written. Again, this code works
when I generate the (x,y) coordinates with a mousedown or mousemove, and
all the timeouts DO WORK. The problem is that when I feed the
coordinates programatically with a loop then I only get the initial
positions and final positions drawn to display.

Here's how I generate the coordinates and call the moveDucks function:

function posGenerator(xaxis, yaxis) {

x=xaxis;
y=yaxis;
x2 = x
y2 = y

var dummy2;

do {
x2+=1;
y2-=1;
setTimeout('dummy2 = moveDucks x2,y2)',500);
clearTimeout;
TextBox4.value=(x2+","+y2)

} while (x2<500);
}


function moveDucks(xPos, yPos) {

mother_xPos = xPos;
mother_yPos = yPos;
setTimeout('mother.left = newDuckPos(mother_yPos, mother_xPos)',100);
clearTimeout;
setTimeout('mother.top = newDuckPos(mother_xPos, mother_yPos)',100);

*****end code snip******

The above calls another function newDuckPos that does the math to figure
out the next positions for each duck image (there is a mother duck and
three ducklings and the duckling follow the mother.)

All of it seems pretty well wrapped in setTimeout statements, and they
do work. My problem is getting the display to refresh the new image
locations real time. Right now it skips to the end after all the
timeouts are done. The end position(s) is correct. I just need the
trick to get the display to show all the steps.

Thanks!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #4
Paul LeBlanc <pa***********@msn.com> writes:
Hi Lasse,
Sorry no code but I thought I was asking about code that I have yet to
write! Here's a sample of what I have written. Again, this code works
when I generate the (x,y) coordinates with a mousedown or mousemove, and
all the timeouts DO WORK. The problem is that when I feed the
coordinates programatically with a loop then I only get the initial
positions and final positions drawn to display.

Here's how I generate the coordinates and call the moveDucks function:

function posGenerator(xaxis, yaxis) {

x=xaxis;
y=yaxis;
x2 = x
y2 = y

var dummy2;

do {
x2+=1;
y2-=1;
setTimeout('dummy2 = moveDucks x2,y2)',500);
I assume there is a parenthesis missing here:
setTimeout('dummy2 = moveDucks(x2,y2)',500);

Notice that you embed the variable *names* x2 and y2 in the string.
That means that when the code is executed (in 500 milliseconds), the
value of x2 and y2 at that point is used.

I think you have slightly misunderstood how setTimeout and
clearTimeout works (and trust me, you are not the first to do that).

setTimeout schedules some code to be executed after some time.
The call to setTimeout itself return immediately.
clearTimeout;
clearTimeout is a function that can prevent a scheduled timeout.
Here, you don't call the function, so the line does nothing.

The argument to clearTimeout is a timeout identifier returned by
setTimeout.
TextBox4.value=(x2+","+y2)

} while (x2<500);


Since setTimeout returns immediately, you might run through this entire
loop before 500 milliseconds have passed. At that time, x2 and y2
have their final values. Then 500 scheduled calls to moveDucks happens,
all setting the same position.

What you can do to fix this:

1) Embed values in the scheduled code instead of variable names:
setTimeout('moveDucks(' + x2 + ',' + y2 + ')',500);
You might want to schedule later events at a later time, e.g.,
make a counter and multiply the 500 by the counter. Then the steps
will happen with half second intervals.

2) Put the position updating in the scheduled code.
setTimeout('x2+=1;y2-=1;moveDucks(x2,y2)',500);
Again, you don't want all the events to happen at the same time.

3) A completely different approach:
Use setInterval instead of setTimeout. It will automatically call
every <delay> milliseconds, until you stop it.

Use a local function as argument to setInterval, so you have the
xaxis and yaxis variables in scope. It saves you from making too
many global variables.

Update the values in the scheduled code.
---
function posGenerator(xaxis, yaxis) {
var intervalId = setInterval( function () {
xaxis+=1;
yaxis-=1;
moveDucks(xaxis,yaxis);
if (xaxis >= 500) {
clearInterval(intervalId);
}
}, 500);
}
---

Hope this helps.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Brian Angliss | last post by:
I'm relatively new to scripting in JavaScript, so I'm not too surprised I'm having difficulty scripting up an animation effect for my personal site. What I'm trying to do is the following: When...
2
by: billrdio | last post by:
I am trying to make a JavaScript animation of real-time images - i.e. images that will periodically change on the server. The problem I am having is that the JavaScript animation I have created is...
1
by: gencode | last post by:
I have this bit if javascript and it animates an image well, the problem is that the more it runs the more memory it eats on the client machine. Can anyone help me make this not eat all avaliable...
4
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the...
0
by: JayCee | last post by:
Hi All, I am new to XAML & C#. Is it possible to display a gif animation ( such as a rotating wheel ) on a form whilst waiting for an operation to complete. I have the .gif file which works fine...
0
by: 123shailesh | last post by:
I have a picturebox (imgViewer) which is displaying an image (a map). Now i want to display another image (a smaller one) on top of this image. But this smaller image is a GIF animation of a blinking...
4
by: =?Utf-8?B?dmluZWV0?= | last post by:
In WPF (even with considerable improvement in version 3.5), while running multiple animations in a single window, The performance of one animation suffers a lot. for example, i have two image...
4
by: Sin Jeong-hun | last post by:
Most applications, including Windows Explorer, show some sort of 'wait' dialog with animation when a lengthy operation is going on. For example, When the Windows Explorer is searching for...
6
by: dantz | last post by:
HI everyone, I hope someone can help me on this. I have form application that has 3 Timers that does an animation (changing an image for every interval) Each image are loaded at start of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.