I just started with three js. I want when I click on the object it zooms towards the center.
Thanks for your help
hblee
2
In short, I think you should manipulate the position of the camera with Tween.js, to make the camera get closer and look at where the object is.
thank you for your reply.
I get to the center but the object zoom is larger than the size of canva width
const ob = new Box3().setFromObject(group)
const center = ob.getCenter(new Vector3())
const size = ob.getSize(new Vector3())
camera.position.x = center.x
camera.position.y = center.y
const padding = 0, fov = camera.fov;
let aspectRatio = renderer.domElement.width / renderer.domElement.height;
let tanFOV = Math.tan(Math.PI * fov / 360);
let viewWidth = padding + size.x, viewHeight = padding + size.y;
//The distances are proportional to the view's with or height
let distanceH = viewWidth / 2 / (tanFOV * aspectRatio);
let distanceV = viewHeight / 2 / tanFOV;
if (aspectRatio > 1 != viewWidth > viewHeight) {
distanceH *= viewHeight / viewWidth;
distanceV *= viewWidth / viewHeight;
}
camera.position.z = Math.max(distanceH, distanceV) + ob.max.z;