To create a realistic spinning wheel, you can use a logarithmic approach to reduce wheel speed.
This means that the angle is reduced by a small percentage for each frame. Since this is a percentage, you get a smooth final turn (you will also notice that you get the infamous wheel aliasing effect ).
This online demo shows a closed loop (implement as you please):
var angle = 2; /// start angle in radians ctx.translate(w, h); /// prepare canvas for rotation (w and h = 50%) ctx.globalAlpha = 0.67; /// optional bonus: motion blur(-ish) loop(); /// start loop function loop() { ctx.rotate(a); /// use incremental rotation ctx.drawImage(img, -w , -h); /// spin down and only loop if angle > certain value a *= 0.995; /// continue if there is enough "inertia" if (a > 0.001) requestAnimationFrame(loop); }
The threshold for continuing the cycle determines how "cruel" you want to stop. If you want the wheel to look heavier (more mass), you can reduce the angle with an even smaller increment (for example, try 0.998
).
For the ball to bounce, you need to resort to physical modeling, or at least pseudo-physical modeling. This includes collision detection for all small parts on the wheel, as well as checking and positioning sub-items (beam casting) along the z axis.
I feel this is a little wide to describe here on SO, but find a good article on collision detection and physical modeling. Here is a good start with the basics .
source share