Skip to content

Instantly share code, notes, and snippets.

@scothis
Last active May 26, 2020 23:01
Show Gist options
  • Save scothis/c9f2695f03380ca5dd96 to your computer and use it in GitHub Desktop.
Save scothis/c9f2695f03380ca5dd96 to your computer and use it in GitHub Desktop.
Cesium.js zoom to camera
var eventHandler, mousePosition;
viewer.scene.screenSpaceCameraController.enableZoom = false;
eventHandler = new ScreenSpaceEventHandler(viewer.scene.canvas);
eventHandler.setInputAction(function (event) {
mousePosition = event.endPosition;
}, ScreenSpaceEventType.MOUSE_MOVE);
eventHandler.setInputAction(function (wheelZoomAmount) {
var cameraHeight, directionToZoom, zoomAmount;
if (mousePosition) {
cameraHeight = viewer.scene.globe.ellipsoid.cartesianToCartographic(viewer.camera.position).height || Number.MAX_VALUE;
directionToZoom = viewer.camera.getPickRay(mousePosition).direction;
zoomAmount = wheelZoomAmount * cameraHeight / 1000;
viewer.camera.move(directionToZoom, zoomAmount);
}
}, ScreenSpaceEventType.WHEEL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment