score:0

Accepted answer

not possible to pause execution at a line of code or wait for an animation to complete.

Yes, that's true, since animations in JS are asynchronous. You have 2 choices:

  • Make your algorithm asynchronous as well. This would be a function that gets the current state of the algorithm and returns the state after one step (and/or possibly the step itself so the matching single animation can be chosen). The step function would then be called from the animation loop each time.
  • Maintain an animation queue (I guess your libraries have helper functions for that). Then run your algorithm as it is, and in each step chain another animation-to-be-executed at the end of the queue. Once the algorithm is ran, start the animation queue.

Option #1 is more elegant, but of course more complicated - all loops will need to become recursive functions etc. Yet this is the only possibility if your algorithm is non-terminating, or the animation queue of option #2 would become too long (and memory-eating).

score:1

To simulate an animation in Javascript, you could modify inline style properties over time.

For example, to make a div element move to the right linearly, you might do something like this:

var div = document.getElementById("some-div");

//This would normally be done elsewhere
div.style.position = "absolute";
div.style.width = "100px;"
div.style.height = "100px;"
div.style.left = "0px";

//Called every 30 milliseconds
function animate() {
    div.style.left = (parseInt(div.style.left,10) + 5) + "px";
    setTimeout(function() {
       animate();
    }, 30);
}
animate();

However, I'd advise you look into using CSS3 transitions or animations, as it separates the site logic from the presentation and uses much less code.

score:1

now this is a really wide question to answer but i will try to cover some aspects of it ..

id like to start with the creation of animation ..

normally you would be manipulating the css of some element in order for it to look like it's an animation .. i would be playing with firebug and increasing the width of a div element and if i keep holding the up arrow it will look like its expanding .. further more , complex animations involve more aspects .. JQuery library provide you with some really cool animations out of the box , and there are more advanced ones to see here is a simple fade-out animation if im not going to use jquery fadeOut() function

var op = 1; // set opacity to one 

setInterval(function(){
  op = op - 0.1 // decrease the opacity by 0.1
  $('div').css({opacity : op  }); // selecting the element an apply the css on it 
},50) // over an interval of 50 ms

now about stopping an animation or pausing it , nothing is impossible ... with the previous animation here is an example .. http://jsfiddle.net/9SaKL/1/

further more cool stuff can be done .. but this is just a lame example (don't apply it to real life project lol)

note that there is no use of re-inviting the wheel tons of animations can be done with just the basic ones used and combined together

I realize that its not really possible in JavaScript to pause execution at a line of code (or wait) for an animation to complete.

totally wrong , chaining is basically wating for one function to finish and then doing the other ... clearing an interval is stopping ..

in real life if there is time and there is some thing that can be done on time then you can speed it or make it slower or you can stop it , (except time huh?)


Related Query

More Query from same tag