score:0
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?)
Source: stackoverflow.com
Related Query
- Animating an algorithm in Javascript
- Images packing algorithm in javascript
- Animating SVG group object using the JavaScript library
- Javascript library d3 call function
- D3 javascript Difference between foreach and each
- x >= x pattern in JavaScript
- Binding javascript (d3.js) to shiny
- How do I convert a JavaScript forEach loop/function to CoffeeScript
- Animating D3 donut chart on load
- transparent color in javascript D3.js
- (Embed and) Refer to an external SVG via D3 and/or javascript
- How to add in zero values into a time series in d3.js / JavaScript
- Select data from a CSV before loading it with javascript (d3 library)
- Javascript Concatenate Array to String
- How do I ensure D3 finishes loading several CSVs before javascript runs?
- how to assign unique id to SVG text element in d3 javascript
- variable scope in d3 javascript
- d3 javascript alternate colors on click
- Retrieving Keys from JSON Array key-value pair dynamically - Javascript
- How are input parameters filled in javascript method chains?
- How to use a JavaScript chart library like D3.js or Raphaƫl in server-side Java
- Binning an array in javascript for a histogram
- How to set the Origin (drag.origin) for drag behavior in d3 JavaScript library
- draw text in d3 arc javascript
- Converting Javascript function to Typescript including getComputedTextLength()
- Algorithm for automatic placement of flowchart shapes
- javascript charting - nvd3 line chart with two Y-axis
- Putting an arrow (marker) at specific point on a path when using the d3 javascript library
- Unable to get click event in D3 JavaScript library
- Javascript event listener for SVG transform
More Query from same tag
- add attribute to a table so I can use css on it
- Leaflet Markercluster showCoverageOnHover triggered wrong
- Extending paths in D3 with transition
- d3: flatten nested data?
- Add points to path
- BI stack: Mondrian, Pivot4J and d3.js
- Javascript d3 insert image from local file
- Accessing values from different levels of a nested object within the same append instance using D3.js
- How to draw from the last radius finish
- Programmatically Zooming in D3?
- Visual Treelist with D3.js
- How to select elements in a forEach loop? D3
- How do I use d3.js to display text in an SVG rect only if the text will fit?
- Multiple polygons with different roll over actions in d3
- Directed graph - node level CSS styles
- d3.js v4, how do I have a line follow the mouse on hover, but also have a circle follow the path?
- Determining nested depth to assign size of node D3.js
- How to omit a data range with no filtering
- D3.js realtime graph not updating smoothly
- D3 graph with slider need too change color of outside nodes
- How can I get a specific object from an array in javascript?
- D3: use .map() to rearrange an array of data
- how can I turn strings into numbers in d3js
- D3 offset X-axis
- X axis not showing all labels for certain dates
- I have a d3 project using fisheye, But why I have to point my mouse on the data point
- d3.json not parsing my JSON correctly?
- Rendering d3.js html table without blocking UI
- d3 Bubble Chart mixing v3 and v5 on scale
- Graphs update only when I write enter() at the end