I have a need to calculate the adjacent angle of a right angle triangle formed by a rectangle that is dictated by two sets of points. My plan is to use offset() on two elements to get absolute position co-ordinates, then determine the internal angle from the bottom left corner of this implied box so that i may rotate a thin rectangle element using css rota开发者_如何学Gote.
Possible?
The only part i wouldn't know how to do right off the bat is the trigonometry syntax, and whether i need an external library for that.
*Note: I do understand that css rotate would rotate around the center of the element, so i would have to shift the rectangle over to compensate.
The answer lies not in jQuery, but in JavaScript. JavaScript has a Math
object which contains sin
, cos
, tan
and other trigonometry and math-related things.
For example, to calculate the sine of 45 degrees:
var degrees=45;
var radians=degrees*Math.PI/180;
var sine=Math.sin(radians);
alert("sin("+degrees+" deg) is roughly equal to "+sine);
If you are planning to do some real graphics work using things like rotate, create circles, create lines, draw charts etc, try the Raphael Graphics library for jquery/javascript.
http://raphaeljs.com/reference.html
It provides a lot of standard function do to stuff, which will help you get your job done faster and more efficiently, rather than concentrating on things like the point to rotate the line etc.
e.g there is a rotate function in Raphael which will allow you to rotate any object by a specific angle clockwise. There is a function for translate, scale etc.
It supports SVG graphics as well.
精彩评论