liveBook · Manning

Degrees

As explained in Math for Web Design

A degree is an angular measurement unit equal to one 360th of a full rotation.

To rotate a page element by one eighth of a circle, write `transform: rotate(45deg)`. A degree measures rotation: a full circle is 360 degrees, so one eighth is 45 degrees. In CSS rotation, positive angles turn clockwise and negative angles turn counterclockwise. section 1.4.7

CSS writes degrees with the `deg` unit. Angles wrap after a full turn: `360deg` has the same direction as `0deg`, `450deg` as `90deg`, and `-90deg` as `270deg`. CSS can also express angles in radians (`rad`), gradians (`grad`), or turns (`turn`). section 9.1.2

Convert an angle for JavaScript

CSS commonly uses `deg`, but JavaScript’s `Math` trigonometric functions take unitless radians. Convert before calling one: `const radians = degrees * (Math.PI / 180); const x = Math.cos(radians);`. For the reverse conversion, use `const degrees = radians * (180 / Math.PI);`. Thus 135 degrees is `3π/4` radians, or approximately 2.356. section 9.1.3 section 9.5

Turn a pointer position into a dial angle

A rotary control starts with the pointer’s position relative to the dial center: `deltaX = e.clientX - centerX` and `deltaY = e.clientY - centerY`. Get its direction with `Math.atan2(deltaY, deltaX)`, convert the returned radians using `* (180 / Math.PI)`, then apply the resulting angle with `rotate(${angle}deg)`. If the indicator’s zero position is at the top rather than the right, adjust the angle by 90 degrees. section 13.2.2 section 13.4

Keep the coordinate system in mind when checking a result. Browser coordinates increase to the right and downward, so a rightward direction is 0 degrees and a downward direction is 90 degrees. Degrees make a visual rotation easy to specify; converting units and choosing the right zero direction make calculated rotations behave as intended. section 10.1 section 9.1.3

How Math for Web Design covers it

How deep it goes
Defines a degree through a full rotation, then works through angle equivalence, radian conversion, and a pointer-driven rotation.
Examples
Shows a 45-degree CSS rotation, a 135-degree conversion, and pointer-angle calculations for a rotary control.
What it assumes
Introduces angles as rotation between rays before using degree measures, while relying on basic CSS and JavaScript syntax for applications.
What you can do after
Apply it via CSS and JavaScript to rotate elements and convert pointer directions into display angles.
How it connects
Connects degrees to radians, CSS transforms, trigonometry, vectors, and browser coordinates.

Also explained in

Compare how 2 books explain degrees →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Math for Web Design. The section links point to the parts of the book it draws on.