Tilt Compensation
A phone lying nearly flat on a table is seen at a steep angle, so everything on it is foreshortened vertically: a circle reads as a squat oval. Tilt compensation reads the device's pitch and adds a few pixels of height to components as the phone approaches horizontal, so they keep looking like the shape they were designed as.
Technique by Colin LeBlanc (https://colin.cv). Live demo: https://tilt-button.vercel.app. Source: https://github.com/ColinLeBlancDesign/tilt-compensation (MIT)
The rules
- Step, don't track. Use three discrete levels, not a continuous function of the angle. Continuous tracking makes the UI shimmer with every hand tremor.
- Keep it tiny. The correction is felt, not seen. Starting values that work on 44 to 64px controls:
+0,+2,+3px.+4is already too much. - Add hysteresis. Shift each threshold about 3° depending on which side you came from, so holding the phone at a boundary doesn't flicker between levels.
- Stretch the contents too. Scale labels and icons vertically by the same ratio as their container,
(h + grow) / h. Small icons can take a slightly larger share (up to about+1pxon a 14px glyph), but no more. - Keep strokes constant. Use
vector-effect="non-scaling-stroke"on stretched SVG paths so horizontal strokes don't get thicker than vertical ones. - Make corner radii elliptical.
border-radius: 14px / calc(14px * var(--ratio))keeps rounded corners in proportion. A circle just uses50%. - Reserve the space. Put each component in a fixed-height slot so growing it never shifts its neighbours.
- Ask for permission from a tap. iOS Safari only exposes orientation after
DeviceOrientationEvent.requestPermission()is called inside a user gesture. Requires HTTPS. - Degrade to nothing. No sensor, permission denied, or
prefers-reduced-motion: leave components at their designed size (or apply the change without a transition).
Pitch levels
Pitch is 0° lying flat and 90° upright.
| Pitch | Level | Added height |
|---|---|---|
| Above 55° | 0 | +0px |
| 30° to 55° | 1 | +2px |
| Below 30° | 2 | +3px |
CSS
Each component declares its resting height in --h (unitless px). JavaScript sets one global, --g, the current growth in px.
:root { --g: 0; }
.tilt {
--ratio: calc(1 + var(--g) / var(--h));
height: calc((var(--h) + var(--g)) * 1px);
transition: height 220ms cubic-bezier(.65, 0, .35, 1),
border-radius 220ms cubic-bezier(.65, 0, .35, 1);
}
/* label or icon inside a .tilt component */
.tilt-ink {
display: inline-block;
transform: scaleY(var(--ratio));
transition: transform 220ms cubic-bezier(.65, 0, .35, 1);
}
.icon-button { --h: 64; width: 64px; border-radius: 50%; }
.button { --h: 48; border-radius: 14px / calc(14px * var(--ratio)); }
@media (prefers-reduced-motion: reduce) {
.tilt, .tilt-ink { transition: none; }
}
Height is animated directly rather than with scaleY on the whole component, because a transform would also distort borders and corner radii. The change is 2 to 3px inside a reserved slot, so there is no layout cost worth avoiding.
JavaScript
const T_HIGH = 55, T_LOW = 30, HYST = 3;
const GROW = [0, 2, 3]; // px per level
let level = 0;
function nextLevel(pitch, cur) {
const hi = T_HIGH + (cur === 0 ? -HYST : HYST);
const lo = T_LOW + (cur === 2 ? HYST : -HYST);
if (pitch >= hi) return 0;
if (pitch < lo) return 2;
return 1;
}
function onOrientation(e) {
if (e.beta === null || e.gamma === null) return;
const angle = (screen.orientation && screen.orientation.angle) || window.orientation || 0;
const landscape = Math.abs(angle) === 90 || angle === 270;
let pitch = Math.abs(landscape ? e.gamma : e.beta); // 0 flat, 90 upright
if (pitch > 90) pitch = 180 - pitch;
level = nextLevel(pitch, level);
document.documentElement.style.setProperty('--g', GROW[level]);
}
// Call from a click/tap handler. iOS needs the gesture; other browsers don't.
async function enableTilt() {
const DOE = window.DeviceOrientationEvent;
if (!DOE) return false;
if (typeof DOE.requestPermission === 'function') {
try { if (await DOE.requestPermission() !== 'granted') return false; }
catch { return false; }
}
window.addEventListener('deviceorientation', onOrientation);
return true;
}
On browsers without requestPermission (Android Chrome, Firefox) you can call enableTilt() on load. Desktop browsers may expose the event but never deliver real values, so treat "no event with non-null beta within about a second" as no sensor.
In a framework
Keep the sensor out of the render loop. Attach one listener at the app root, write --g to the document element, and let CSS do the rest. Do not put pitch in React state: it would re-render the tree many times a second for a value that only matters when the level changes.
What not to do
- Don't apply this to text blocks, images, or whole layouts. It is for discrete controls: buttons, inputs, toggles, menu rows.
- Don't stretch the text inside an
<input>; browsers can't scale it separately. Grow the field's box only. - Don't go past about 6% of the component's height.
- Don't prompt for motion access on page load. Tie it to a control the user chose to tap.
Checklist
- Three levels with hysteresis, not continuous tracking
- Growth of 3px or less on standard controls
- Contents stretched by the container's ratio; SVG strokes non-scaling
- Rounded-rect radii elliptical by the same ratio
- Fixed-height slot around each component
- Permission requested from a tap; HTTPS
- Clean fallback with no sensor, denied permission, and reduced motion