Calculate camera fov so that the camera view fits the object dimensions and canvas size

Hi All

Is there a way to calculate the camera’s fov so that the camera view fits to object dimension and canvas size ?

I found a way to make the fov camera adjust to the size of the object in this forum:

But I want the fov camera to be able to adjust to the canvas size too. because my project requires that it can run on all devices

This is the PG: https://playground.babylonjs.com/#Y82BS8#3

1 Like

Hey there, one way to do this is to use an ArcRotateCamera, which has framing behavior available. :slightly_smiling_face: :
https://playground.babylonjs.com/#5T0H6Q#2

Edit: IDK if you want it re-adjust on resizing or just once at the start, but if you want it on resizing it looks like @roland’s got you covered. :slight_smile: :beers:

2 Likes

Hi! I’ve created this small PG, but @Blake was faster. :stuck_out_tongue:

https://playground.babylonjs.com/#Y82BS8#5

2 Likes

but i need the fov value and i use free camera, for some reason i can’t use ArcRotateCamera camera

My PG shows how to use the world extends. Or you can use the boundigInfo of the mesh. Look at the API. You basically get the max and min values out of it. You can calculate the fov from these values accordingly.

could you give me some sample please ?

You can change the size of the sphere and the size variable will change accordingly. You can use whatever mesh you want.
https://playground.babylonjs.com/#4F33I3#397

Check the docs:

1 Like

Do you need more detailed help?

thank @roland, i will try to implement it

1 Like