How to constrain an object to the window?

Hi there,

I’m a beginner to three.js and I’m trying to work out how to constrain the position of an object to the window?

Just for some context, I’m trying to achieve diffusion-limited aggregation by translating my code from a previous build in p5.js into three.js.

Could anyone help me?
I found something similar in plain js;

Many thanks,

This code seems not to work properly. I can move the black dot outside of the gray circle:


Yeah I know it’s not being constrained to the circle, but it is being constrained to an invisible box (canvas). That’s what I think I need anyway.