I am currently working on developing a zoom-to-fit function that can accurately adjust a list of points to fit within a specified drawing area, while also allowing for customizable offsets on all sides of the image. In essence, I aim to zoom-to-fit within a specific area of the frame rather than the entire viewer area:
https://i.sstatic.net/6y6Lr.png
(please note that the offsets depicted in this image may not be entirely accurate)
My current setup involves using a perspective camera. The objective is to update the camera's position without altering its parameters or view direction.
Although I have managed to implement a functional zoom-to-fit feature*, I am facing challenges when it comes to integrating the desired offsets.
Initially, my approach involved offsetting the point coordinates using the camera's coordinate system. However, this method did not yield the desired results - while more of the image was displayed, the selected points did not align with the edges of the designated area. This discrepancy can be attributed to the distortion caused by the perspective view, which deviates the points from their intended positions.
Is there anyone who could offer assistance on calculating the correct camera distance and position to address this issue?
* Although Three.js does not include a built-in zoom-to-fit function, various resources and discussions are available online on how to incorporate this functionality. One particularly useful resource for scenarios like these would be CameraViewBox. I have adapted their example to suit my requirements in this fiddle:
import * as THREE from 'https://cdn.skypack.dev/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="62160a10070722524c5351524c53">[email protected]</a>';
...