Microsoft provide an excellent SVG gradient maker so IE9 can also have "CSS3" gradients (click Custom).
I currently utilise their logic for my Fireworks and Dreamweaver extensions to convert gradients to SVG, but I only know how to do it for standard top, bottom, left, right directions. If you enter an angle, I don't do the conversion, because I'm not sure how I would convert x1, x2, y1, y2 to CSS3 angle degrees.
The gradient generator provides values like this: x1="0%" y1="0%" x2="56.262833675564686%" y2="68.29999651227678%"
I'm not great with mathematics or trigonometry, so could somebody help me out? I'd also like to use the same math in a Sass mixin to do a similar thing, if possible.
Instead of using Math.tan function You should use Math.atan2:
Here is an example of use:
and this will return a degree from <-180;180>.
If you in a Quadrant
P1=(X0,Y0)
P2=(X1,Y1)
a=(X0-X1)
b=(Y0-Y2)
the deg will between 0 ~ 180
If you get
deltaX
anddeltaY
from your coordinates thenMath.atan2
returns the arctangent of the quotient of its arguments. The return value is in radians.Then you can convert it to degrees as easy as:
Edit
There was some bugs in my initial answer. I believe in the updated answer all bugs are addressed. Please comment here if you think there is a problem here.
This function takes 2 elements and returns the degree between the middle of the elements.
For example, I used it on a world map, to make the image of plane rotate in the direction of a city.
The currently accepted answer is incorrect. First of all,
Math.tan
is totally wrong -- I suspect Mohsen meantMath.atan
and this is just a typo.However, as other responses to that answer state, you should really use
Math.atan2(y,x)
instead. The regular inverse tangent will only return values between -pi/2 and pi/2 (quadrants 1 and 4) because the input is ambiguous -- the inverse tangent has no way of knowing if the input value belongs in quadrant 1 vs 3, or 2 vs 4.Math.atan2
, on the other hand, can use the xy values given to figure out what quadrant you're in and return the appropriate angle for any coordinates in all 4 quadrants. Then, as others have noted, you can just multiply by(180/Math.pi)
to convert radians to degrees, if you need to.