An eye test in the browser: turning CSS pixels into millimetres with a bank card
A distance eye test has one hard requirement: the letter on the screen has to be a known physical size, at a known distance. The browser gives you neither. A CSS pixel isn't a physical unit, there's no API that reports m
A distance eye test has one hard requirement: the letter on the screen has to be a known physical size, at a known distance. The browser gives you neither. A CSS pixel isn't a physical unit, there's no API that reports millimetres, and devicePixelRatio tells you about device pixels, not about the size of the glass.
We built a free eye test that runs in a browser tab anyway (anpheros.com/eye-test). This post covers how it gets a physical size out of a web page, the maths that turns a distance into a letter height, and where the precision quietly runs out.
Step 1: a ruler everyone carries
Bank cards, ID cards and most loyalty cards share one format, ISO/IEC 7810 ID-1: 85.60 Γ 53.98 mm. That's a physical reference almost everyone has in reach.
So the first screen shows a card-shaped box and a slider. You hold a real card flat against the screen and drag until the box is exactly as wide as the card:
let w = Math.min(340, Math.round(window.innerWidth * 0.8));
const size = () => {
card.style.width = w + 'px';
card.style.height = Math.round(w * 53.98 / 85.6) + 'px'; // keep the ID-1 aspect ratio
};
slider.addEventListener('input', () => { w = +slider.value; size(); });
// "The card matches"
pxmm = w / 85.6; // CSS pixels per millimetre, on this screen, in this zoom
Keeping the ID-1 aspect ratio gives a second check for free: if the width matches but the height doesn't, something's off. The Β± buttons next to the slider help with the last couple of pixels, which are hard to hit with a drag.
The result lives only in memory while the page is open. Zoom the page or move to another monitor and it's wrong, so we don't persist it.
Phones: nobody holds a card against a phone
On a phone, pressing a card against the screen is awkward, so there we estimate instead. Phone browsers size the CSS pixel for reading distance, which puts it in a fairly narrow band of CSS pixels per inch. For iPhones the band is narrower still and the device class can be inferred from the screen size and DPR:
const isPhone = () =>
matchMedia('(pointer: coarse)').matches && Math.min(screen.width, screen.height) <= 500;
function phonePxmm() {
const sw = Math.min(screen.width, screen.height), sh = Math.max(screen.width, screen.height);
const dpr = window.devicePixelRatio || 1;
let ppi = 150; // typical Android CSS px per inch
if (/iPhone|iPod/.test(navigator.userAgent))
ppi = dpr < 2.5 ? 163 : (sw === 360 && sh === 780 ? 158.7 : 153.3);
return ppi / 25.4;
}
It's an estimate, and we say so. The card calibration is still offered on phones as "more precise", it's just not the default anymore.
Step 2: from distance to letter height
Standard visual acuity is defined by angle. A letter for acuity 1.0 (20/20, 6/6) is 5 minutes of arc tall at the eye, with strokes of 1 arcminute. For decimal acuity V at distance d:
height = d Γ tan(5β²) / V
At 2 m, an acuity-1.0 letter is about 2.9 mm tall. Multiply by pxmm and you have CSS pixels:
const ARC5 = Math.tan(5 / 60 * Math.PI / 180);
const pxFor = v => Math.max(0, Math.round(dist * ARC5 / v * pxmm / 5) * 5);
const decOf = px => dist * ARC5 / (px / pxmm);
The letter is a tumbling E drawn as an SVG on a 5 Γ 5 grid, with shape-rendering: crispEdges, rotated to one of four directions. Rounding the size to a multiple of 5 pixels keeps every stroke a whole number of pixels, so the bars stay sharp instead of being anti-aliased into grey.
Where the precision runs out
Here's where it gets less tidy. On a typical laptop screen, with roughly 4 CSS pixels per millimetre, the 1.0 letter at 2 m is about 11 px tall. Round to a multiple of 5 and it becomes 10. The steps near the top of the chart (0.63, 0.8, 1.0, 1.25) start collapsing onto the same one or two pixel sizes.
Two consequences shaped the design:
-
The score comes from the size actually shown, not the size requested. That's what
decOf(pxFor(level))does. If the 0.8 line was drawn at 15 px, you get the acuity that 15 px represents at your distance, not "0.8". - Distance is a precision knob. The test offers 1, 2 or 3 metres and says plainly that farther is more precise. At 3 m every letter is 1.5Γ larger in pixels, so the rounding error shrinks. If a screen can't draw a letter small enough at all (under 5 px), the test stops there and suggests stepping back, instead of pretending.
The near-vision test (40 cm) has the opposite problem, letters get tiny, so it rounds to whole device pixels using devicePixelRatio and drops levels that collapse onto the same size.
The procedure: a short staircase
Levels follow the usual roughly logarithmic progression, 0.1 to 1.25. The test starts at 0.2. If you pass, it moves to smaller letters until a line is failed; if you fail, it moves to bigger letters until one is passed. The result is the last line passed.
Each line is up to five tries: three correct passes it, three wrong fails it. "I can't see it" counts as wrong. With four directions, a pure guess is right 25% of the time, so the chance of passing a line by luck alone is about 10% (at least 3 of 5 at p = 0.25). That's not zero, and it's one more reason the result is labelled indicative.
Arrow keys work too, which matters when you're standing three metres from a laptop with a wireless keyboard.
What it is, and what it isn't
The result is shown as decimal acuity with the 6/x and 20/x equivalents, per eye, against the 1.0 reference. It's useful for one thing above all: noticing a change, or a difference between your eyes, and acting on it.
It isn't an eye exam. It doesn't measure a prescription, it can't see the health of the eye, and a good score doesn't mean the eyes are fine. If one eye is clearly worse, or a result drops compared with last time, the right next step is an optometrist. A sudden change in vision needs to be seen quickly, not re-tested online.
Try it
The eye test is at anpheros.com/eye-test, and the other browser checks (hearing, colour vision, contrast, Amsler grid, astigmatism, near vision) are at anpheros.com/tests. Free, no account. The hearing test has its own write-up on how it uses the Web Audio API.
If you know a more reliable way to get physical screen size in a browser without a reference object, I'd genuinely like to hear it. As far as I know there still isn't one.
Disclosure: I'm the founder of Anpheros, a patient-held health record. The tests are informative only; they don't diagnose and don't replace an eye exam.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.