Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript point to point angle calculation
    primarykey
    data
    text
    <p>We got 3 points: <strong>start</strong>, <strong>end</strong> and <strong>mail</strong>.</p> <p><img src="https://i.stack.imgur.com/t2Fgu.png" alt="description"></p> <p>The mail image, moves in a curved line from the start and end point, this is done by jQuery animate.</p> <p>Now the next step is to make the mail image rotate while the animation is running. So at the start point and end point it would be rotated 0 degrees, but while animating, it should rotate facing the path of the animation. (see the image)</p> <p>What I have tried:</p> <p><a href="http://jsfiddle.net/R5dRd/6/" rel="nofollow noreferrer">JSFiddle</a></p> <pre><code>// Init dom elements var $start = $('#start'); var $end = $('#end'); var $mail = $('#mail'); // Get position coordinates var startPos = $start.position(); var endPos = $end.Position(); // Angle calculation var getAngle = function(currX, currY, endX, endY) { var angle = Math.atan2(currX - endX, currY - endY) * (180 / Math.PI); if (angle &lt; 0) { angle = Math.abs(angle); } else { angle = 360 - angle; } return angle; }; // Mail angle var getMailAngle = function() { var currPos = $mail.position(); var endPos = $end.position(); return getAngle(currPos.left, currPos.top, endPos.left, endPos.top); }; // Animate $mail.animate({top: endPos.top, left: endPos.left}, { specialEasing: {left: "easeInSine", top: "linear"}, // Executed each "animation" frame, so we rotate here. step: function() { var angle = getMailAngle(); $(this).css('transform', 'rotate(' + angle + 'deg')); } }); </code></pre> <p>But the code above is not correct, the angle doesn't face up when started / ended, I have very little experience with geometry math, so I really appreciate help for the rotating calculations.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload