zoom in to mouse point


I came across this code for zooming to mouse point, instead of the map center point:

It works fine for 3D but not for 2D. you can check it in sandcastle.

Can you please tell me what should I do to get this work in 2D?
If you have a better way for zooming to mouse point in 2D,
I would be very grateful to know about it.


I don’t have a complete code sample handy, but here is some pseudocode where you need to modify

eventHandler.setInputAction(function (wheelZoomAmount) {
var cameraHeight, directionToZoom, zoomAmount;
if (mousePosition) {
cameraHeight = viewer.scene.globe.ellipsoid.cartesianToCartographic(camera.position).height || Number.MAX_VALUE;
zoomAmount = wheelZoomAmount * cameraHeight / 1000;

    if (scene.mode !== Cesium.SceneMode.SCENE3D) {
        //Determine Camera position https://groups.google.com/d/msg/cesium-dev/QSFf3RxNRfE/XotNBSBdFQgJ

        //calculate the vector between camera ground position and mouse ground position so you can calculate a
        //moveLeft amount and a moveForward amount;  This will need probably have a scaling factor based on height
        //similar to the zoomAmount;

        camera.zoomIn (zoomAmount);
    } else {
        directionToZoom = camera.getPickRay(mousePosition).direction;
        camera.move(directionToZoom, zoomAmount);

}, Cesium.ScreenSpaceEventType.WHEEL);


I finally wrote something that works in 2D

here is a link to that code https://github.com/erezy/cesium-2D-zoom-in/blob/master/src/zoom-in.js

you can copy&paste it in Sandcastle.

Don’t forget to change the map to 2D.

Thanks for sharing the code sample.

FYI to people interested in this feature, a pull request is open to do this official and make it the default zoom behavior: https://github.com/AnalyticalGraphicsInc/cesium/pull/2810

This feature (#2810) was just merged into master and will be in Cesium 1.11 on July 1.

If you have the same experience I did, you will quickly get use to it and will not want to go back to the old zoom.


Any chance there will ever be zoom out on mouse position?

Is July first soon enough?



Mike, the July 1st release does not have zoom out on mouse position, though I agree with Denver in that we should absolutely support it.

There is already an issue open for it here: https://github.com/AnalyticalGraphicsInc/cesium/issues/2834

Depending on how hard it is, it might go into July 1st, but I have to refer to Dan, since he’s the one that knows this area of the code.