A method and apparatus are provided for spatially modulating X-rays or X-ray pulses using microelectromechanical systems (MEMS) based X-ray optics. A torsionally-oscillating MEMS micromirror and a method of leveraging the grazing-angle reflection property are provided to modulate X-ray pulses with a high-degree of controllability.