I have been working on developing a compass app for mobile devices and have successfully created a function that returns the change-values for the compass-needle rotation (e.g. 20). However, I have noticed that setting the 'image-transform' to 'rotate(x deg)' results in a slightly jumpy motion. To enhance the user experience, I am keen on adding a smooth animation for the needle to move.
Could you help me with coding a JavaScript function that can incorporate an animation for my compass needle?
Of utmost importance: How do I handle a situation where the function is called with a parameter '20', and 0.5 seconds later (while the initial animation is in progress) the function is called with a different parameter '-30'. I need the function to cancel the ongoing animation, add the new change-value ('-30') to the old one ('20'), and initiate a new animation seamlessly.
I hope my issue is clear. Below is the basic framework of my function:
var change_value = 20;
function start_animation(change_value){
$needle_element.css("-webkit-transform", "rotate(" + change_value + "deg)")
}