Its because of a phenomenon called tyndall effect it basically means scattering of light . When light hits the particles on its path it scatters , when the the object that casts the shadow is closer to the wall then the scattering is lower and a sharper image is obtained.
It is the same reason why there is no sunlight in the vaccum of space as there are no particles for light to bounce off.
Latest Answers