The photon-like propagation of the Dirac electrons in graphene, together with its record-high electronic mobility1,2,3, can lead to applications based on ultrafast electronic response and low dissipation4,5,6. However, the chiral nature of the charge carriers that is responsible for the high mobility also makes it difficult to control their motion and prevents electronic switching. Here, we show how to manipulate the charge carriers by using a circular p–n junction whose size can be continuously tuned from the nanometre to the micrometre scale7,8. The junction size is controlled with a dual-gate device consisting of a planar back gate and a point-like top gate made by decorating a scanning tunnelling microscope tip with a gold nanowire. The nanometre-scale junction is defined by a deep potential well created by the tip-induced charge. It traps the Dirac electrons in quantum-confined states, which are the graphene equivalent of the atomic collapse states (ACSs) predicted to occur at supercritically charged nuclei9,10,11,12,13. As the junction size increases, the transition to the optical regime is signalled by the emergence of whispering-gallery modes14,15,16, similar to those observed at the perimeter of acoustic or optical resonators, and by the appearance of a Fabry–Pérot interference pattern17,18,19,20 for junctions close to a boundary.