I am rendering a 3d scene using vtk.js.
I am using vtkMouseCameraTrackballZoomToMouseManipulator
to add zoom to mouse functionality.
Setup is pretty standard I can add example code if needed.
That part works fine. I render my scene and I can zoom to mouse.
But now I would like to add another canvas on top of this one where I will be rendering some 2D stuff. I would like both of those zoom at the same rate.
Since vtk.js
is handling zooming of the 3d scene fine I have created second canvas and my onWheel
handler for it looks something like this, react code.
const transformedMousePosition = useCallback(
(x: number, y: number) => {
if (ctx) {
const originalPoint = new DOMPoint(x, y);
return ctx.getTransform().invertSelf().transformPoint(originalPoint);
}
},
[ctx]
);
const onWheel = (e: WheelEvent<HTMLCanvasElement>) => {
if (ctx && e.shiftKey) {
const { left, top } = e.currentTarget.getBoundingClientRect();
const tmp = transformedMousePosition(e.clientX - left, e.clientY - top);
if (tmp) {
const zoom = ???;
ctx.translate(tmp.x, tmp.y);
ctx.scale(zoom, zoom);
ctx.translate(-tmp.x, -tmp.y);
clear();
draw()
}
}
};
That code works on its own when I setup some logic for the line const zoom = ???;
based on e.deltaX
or e.deltaY
. But ofc it is out of sync with vtk.js scene zoom.
But how can I find out what value should I setup the const zoom = ???;
so its zooming is synchronised with vtk.js 3d scene zooming?
Ive tried looking into vtkMouseCameraTrackballZoomToMouseManipulator
source code. I found references to delta
in few places. A call to dolltToPosition
and the factor value but I can’t figure out how to replicat it.