How to restrict the position of object to viewport of camera

I want to restrict a ball’s position to the viewport of the camera so it doesnt go out of the view port.
I wasnt able to find the solution any where, however I found it on unity forum
which was like this,

  1. void Update() {
  2.     Vector3 pos = Camera.main.WorldToViewportPoint (transform.position);`
    
  3.     pos.x = Mathf.Clamp01(pos.x);`
    
  4.     pos.y = Mathf.Clamp01(pos.y);`
    
  5.     transform.position = Camera.main.ViewportToWorldPoint(pos);`
    
  6. }
    

As you can see in line number 2 I use the WorldToViewPort and the transform is of the the object. Remember this is in Unity.

Now I am trying to implement this exact same functionality here in playcanvas.
The project is this:


Controls: Pan left,right,up or down to control the ball

Any help would be appreciated.