- Notifications
You must be signed in to change notification settings - Fork 22.7k
/
Copy pathindex.md
116 lines (86 loc) · 2.86 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
---
title: atan2()
slug: Web/CSS/atan2
page-type: css-function
browser-compat: css.types.atan2
---
{{CSSRef}}
The **`atan2()`**[CSS](/en-US/docs/Web/CSS)[function](/en-US/docs/Web/CSS/CSS_Values_and_Units/CSS_Value_Functions) is a trigonometric function that returns the inverse tangent of two values between `-infinity` and `infinity`. The function accepts two arguments and returns the number of radians representing an {{cssxref("<angle>")}} between `-180deg` and `180deg`.
## Syntax
```css
/* Two <number> values */
transform: rotate(atan2(3, 2));
/* Two <dimension> values */
transform: rotate(atan2(1rem, -0.5rem));
/* Two <percentage> values */
transform: rotate(atan2(20%, -30%));
/* Other values */
transform: rotate(atan2(pi, 45));
transform: rotate(atan2(e, 30));
```
### Parameters
The `atan2(y, x)` function accepts two comma-separated values as its parameters. Each value can be a {{cssxref("<number>")}}, a {{cssxref("<dimension>")}}, or a {{cssxref("<percentage>")}}. Both values must be of the same type, although if they are {{cssxref("<dimension>")}} they can be of different units (example: `atan2(100px, 5vw)` is valid).
-`y`
- : The y-coordinate of the point. A calculation which resolves to a {{cssxref("<number>")}}, a {{cssxref("<dimension>")}}, or a {{cssxref("<percentage>")}}.
-`x`
- : The x-coordinate of the point. A calculation which resolves to a {{cssxref("<number>")}}, a {{cssxref("<dimension>")}}, or a {{cssxref("<percentage>")}}.
### Return value
Given two values `x` and `y`, the function `atan2(y, x)` calculates and returns the {{cssxref("<angle>")}} between the positive x-axis and the ray from the origin to the point `(x, y)`.
## Formal syntax
{{CSSSyntax}}
## Examples
### Rotate elements
The `atan2()` function can be used to {{cssxref("transform-function/rotate", "rotate")}} elements as it return an {{cssxref("<angle>")}}.
#### HTML
```html
<divclass="box box-1"></div>
<divclass="box box-2"></div>
<divclass="box box-3"></div>
<divclass="box box-4"></div>
<divclass="box box-5"></div>
```
#### CSS
```css hidden
body {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
gap: 50px;
}
```
```css
div.box {
width: 100px;
height: 100px;
background: linear-gradient(orange, red);
}
div.box-1 {
transform: rotate(atan2(3, 2));
}
div.box-2 {
transform: rotate(atan2(3%, -2%));
}
div.box-3 {
transform: rotate(atan2(-1, 0.5));
}
div.box-4 {
transform: rotate(atan2(1, 0.5));
}
div.box-5 {
transform: rotate(atan2(1rem, -0.5rem));
}
```
#### Result
{{EmbedLiveSample('Rotate elements', '100%', '200px')}}
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{CSSxRef("sin")}}
- {{CSSxRef("cos")}}
- {{CSSxRef("tan")}}
- {{CSSxRef("asin")}}
- {{CSSxRef("acos")}}
- {{CSSxRef("atan")}}