drWonderful drWonderful - 1 year ago 162
Javascript Question

Restrict Javascript Rotation Angle (With Propeller.js)

I am trying to figure out how to use propeller.js to restrict the rotation angle of an image. As of right now, if I spin my image twice, my output would read 720. I would like to make it so the element’s output will reset to 0 when spun over 360. In addition, I would like it to not output negative angles if I spin counterclockwise. For example going from 10 to 280.

Here is a codepen of where I am currently at: http://codepen.io/anon/pen/YyZaXK

$('img').propeller({inertia: 1, speed: 10,onRotate: function(){ console.log(this.angle); }});

Any suggestions?


Answer Source

Figured it out!

Using a Modulo made this work the way I wanted!

Here’s what I added:

(degrees + 360) % 360

Which resulted in:

$('img').propeller({inertia: 1, speed: 10,onRotate: function(){ console.log((this.angle + 360) % 360); }});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download