I’d like to build a function like
fromHeretoThere(x1,y1,x2,y2){
//....
}
So that I can move a <div> or an image from one point on the HTML page to another point in a curve.
Is this doable only using Canvas? HTML5? any plugin/scripts yo suggest?
Edit: Here’s a work in progress that packages up the second concept described below as a re-usable JS object. You can edit the code or visually drag the curve to see the resulting code:
http://phrogz.net/SVG/animation_on_a_curve.html
I’d personally use SVG, which makes this sort of thing (animating along an arbitrary Bézier curve) trivial using the
<animateMotion>element. As a bonus, you can even cause it to calculate the rotation for you. Some examples:Note that you don’t even have to actually use SVG to display the result; you could simply create an off-screen SVG with this animation and sample the transform of the animated element to get your desired point/rotation.
Alternatively (if you don’t want the rotation, or want to calculate it yourself while controlling the rate of traversal) you can create an SVG path and just use
getPointAtLength()/getTotalLength()to find where you should be along the path at a given percentage of the total traversal distance. With this you don’t even need an SVG document:Now all you have to do is set the
.style.topand.style.leftof your<div>or<img>appropriately. The only ‘hard’ part is deciding what you want to the curve to look like and defining where to put the handles.