Detection of scanning velocity or position by detecting the scanned beam or a reference beam after modulation by a grating, mask or the like