atan2()
Baseline
2023
Newly available
Since March 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
atan2()
は CSS の関数で、 -infinity
から infinity
までの 2 つの値の逆タンジェントを返す三角関数です。この関数は 2 つの引数を受け入れ、-180deg
から 180deg
までの <angle>
を表すラジアン数を返します。
構文
/* 2 つの <number> 値 */
transform: rotate(atan2(3, 2));
/* 2 つの <dimension> 値 */
transform: rotate(atan2(1rem, -0.5rem));
/* 2 つの <percentage> 値 */
transform: rotate(atan2(20%, -30%));
/* その他の値 */
transform: rotate(atan2(pi, 45));
transform: rotate(atan2(e, 30));
引数
atan2(y, x)
関数は、カンマで区切られた 2 つの値を引数として受け入れます。それぞれの値は、 <number>
、<dimension>
、<percentage>
のいずれかです。両方の値は同じ型である必要がありますが、<dimension>
の場合は、単位が異なっていてもかまいません (例: atan2(100px, 5vw)
は有効です)。
y
-
この点の y 座標。
<number>
、<dimension>
、<percentage>
のいずれかに解決される計算式。 x
-
この点の x 座標。
<number>
、<dimension>
、<percentage>
のいずれかに解決される計算式。
返値
2 つの値 x
および y
が指定された場合、関数 atan2(y, x)
は、正の x 軸と原点から点 (x, y)
までの光線の間の <angle>
を計算して返します。
形式文法
<atan2()> =
atan2( <calc-sum> , <calc-sum> )
<calc-sum> =
<calc-product> [ [ '+' | '-' ] <calc-product> ]*
<calc-product> =
<calc-value> [ [ '*' | / ] <calc-value> ]*
<calc-value> =
<number> |
<dimension> |
<percentage> |
<calc-keyword> |
( <calc-sum> )
<calc-keyword> =
e |
pi |
infinity |
-infinity |
NaN
例
>要素の回転
atan2()
関数は <angle>
を返すので、要素の回転 (rotate
) に使用することができます。
HTML
<div class="box box-1"></div>
<div class="box box-2"></div>
<div class="box box-3"></div>
<div class="box box-4"></div>
<div class="box box-5"></div>
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));
}
結果
仕様書
Specification |
---|
CSS Values and Units Module Level 4> # trig-funcs> |
ブラウザーの互換性
Loading…