Adjusting zoom to fit in the Forge viewer

A question came up during this week's Forge Accelerator in Dublin that I thought would be of broader interest. It related to the fact that the Forge viewer's default behaviour when you double-click the background and perform a zoom-to-fit isn't always desirable. In this case the developer was fine with a zoom-to-fit being performed, but felt the resultant view wasn't a good enough fit.

Here's the default zoom level on double-click in Dasher 360, for instance:

Zoom to fit

As you can see the model remains quite far from the camera.

The good news is that debugging into the Forge viewer source, you can easily see where this calculation happens, and it's based on a few variables that can be modified in your custom application.

From your GuiViewer3D instance, you can access the following properties via the navigation object:

Default navigation state

The main variables – with the default value of 0.05 – are FIT_TO_VIEW_HORIZONTAL_MARGIN and FIT_TO_VIEW_VERTICAL_MARGIN. This means – as far as I can tell from the code – that the camera will be moved 5% further away than the position at which it has a tight fit for the model.

The obvious thing to try is to set these to zero. I actually found this made very little difference (probably exactly 5%, in fact ;-), so ended up tweaking the variables to move the camera a bit more radically.

Modified navigation state

Setting these two variables to –0.5 (moving the camera 50% closer to the target) and tweaking FIT_TO_VIEW_VERTICAL_OFFSET to 0.15 resulted in a much better fit, at least for this model. (As a hint, it's much easier to set a breakpoint – such as in the computeFit ot fitBounds function – and then modify the state in the browser before you end up adding custom code to your application… this is a handy way to work out what the best values are for your particular application requirements.)

Here's how it looks with these tweaks:

Zoom to fit better

I'm heading back a bit early from a great week, here in Dublin. I'll post a brief summary when I'm back home tomorrow.


One response to “Adjusting zoom to fit in the Forge viewer”

  1. I think is was 1997 when we developed the API for WHIP! ActiveX control for DWF files. Users could do everything with the API that they could do with the UI except for "zoom to fit." And boy did I hear about it. Developers really wanted to be able to specify that initial view. The cries were so loud that rather than take time to add a new command, since we already had named views that we supported, we just added a special case to our code so that if namedview="*" then our ActiveX Control did a zoom to fit. It's nice to see that things haven't changed in 22 years.

Leave a Reply

Your email address will not be published. Required fields are marked *