People have mentioned two mechanisms – firstly narrowing the aperture through which light gets into the eye, and secondly deforming the eyeball.
The first one is definitely true. It’s not perfect, because you are only narrowing it vertically and not horizontally, but it seems to be somewhat effective. You would get a crisper effect by looking through a round pinhole, except that in dim light, the image you see might be too dark to make out.
I have no idea if the second explanation – squashing your eyeball a bit – also contributes. It has plausibility, because if you are shortsighted, your eyeball is longer than your lens can work with, so shortening the eyeball makes sense, but it seems unlikely to me that it works in practise.
Latest Answers