Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

sin(): a sine value from an angle

Calculate a periodic factor and multiply it by a length to obtain a displacement.

Version: CSS Modules

Syntax

transform: translateY(calc(40px * sin(30deg)));

sin(A) accepts one calculation resolving to an angle or a number. An angle can use deg, rad or turn; a unitless number is interpreted in radians. For finite input the result is a unitless number between -1 and 1. This makes it useful as a factor for positions or sizes. Multiply by the required dimension in calc(): sin(30deg) alone is not a CSS length.

Degrees and radians cannot be swapped by simply omitting the unit. sin(90deg) is 1, whereas sin(90) means 90 radians and is about 0.894. The constant pi allows sin(pi / 2) to express a quarter-turn without a rounded decimal approximation. An input length such as 20px has the wrong type, and an infinite angle or number produces NaN rather than a useful periodic value.

The example uses an amplitude of 40px. Angles 0deg, 30deg, 90deg and 270deg give vertical translations of 0, 20, 40 and -40 pixels. pi / 2 also gives 40 pixels, but unitless 90 gives approximately 35.76 pixels. Values are read from computed transform matrices and printed to two decimal places; no animation is started by the function itself.

Examples

const box=document.createElement('div');document.body.append(box);
const displacement=angle=>{box.style.transform='translateY(calc(40px * sin('+angle+')))';return new DOMMatrixReadOnly(getComputedStyle(box).transform).m42.toFixed(2)};
console.log('degrees='+['0deg','30deg','90deg','270deg'].map(displacement).join(',')+'; radians='+displacement('pi / 2')+'; unitless-90='+displacement('90')+'; length-argument='+CSS.supports('transform','translateY(calc(40px * sin(20px)))'));

Result

degrees=0.00,20.00,40.00,-40.00; radians=40.00; unitless-90=35.76; length-argument=false

Executed in Chromium 157.0.8103.51. The example checks static calculations and rejects a length argument; it does not measure animation quality. Supply a fallback or @supports for browsers without trigonometric CSS functions.

Sources