Clipping objects inside objects in 2D space

I wanted to know if objects can be masked / clipped inside a 2d shape of any geometry and how it can be done, something like this:
demo