Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camera.zoomIn doesn't work when viewport width < height #4898

Closed
hpinkos opened this issue Jan 23, 2017 · 2 comments
Closed

Camera.zoomIn doesn't work when viewport width < height #4898

hpinkos opened this issue Jan 23, 2017 · 2 comments

Comments

@hpinkos
Copy link
Contributor

hpinkos commented Jan 23, 2017

Reported on the forum: https://groups.google.com/forum/?hl=en#!topic/cesium-dev/5Urmnt2xwqE

The following example works correctly when the canvas width is greater than the canvas height, but not when the width is less than the height:

var viewer = new Cesium.Viewer('cesiumContainer', {sceneMode : Cesium.SceneMode.SCENE2D});
var scene = viewer.scene;
var clock = viewer.clock;

viewer.camera.maximumZoomFactor = 0;
var currentMagnitude = viewer.camera.getMagnitude();
console.log('currentMagnitude - ' + currentMagnitude);
viewer.camera.zoomIn((currentMagnitude - 1000));
console.log('NewMagnitude - '+ viewer.camera.getMagnitude());
@cfickler
Copy link

This could probably be shortened, but it solves the issue:

function zoom2D(camera, amount) {
var frustum = camera.frustum;

            if (!defined(frustum.left) || !defined(frustum.right) || !defined(frustum.top) || !defined(frustum.bottom)) {
        throw new DeveloperError('The camera frustum is expected to be orthographic for 2D camera control.');
    }
    
    //************************************************************************************************************************************
    // Calculate the frustum based on the height and width of the map        
    //************************************************************************************************************************************        
    if((Math.abs(frustum.top) + Math.abs(frustum.bottom)) > (Math.abs(frustum.left) + Math.abs(frustum.right))) {        	
    	amount = amount * 0.5;
        var newTop = frustum.top - amount;
        var newBottom = frustum.bottom + amount;

        var maxBottom = camera._maxCoord.x;
        if (camera._scene.mapMode2D === MapMode2D.ROTATE) {
            maxBottom *= camera.maximumZoomFactor;
        }

        if (newBottom > maxBottom) {
            newRight = maxBottom;
            newTop = -maxBottom;
        }

        if (newTop <= newBottom) {
            newTop = 1.0;
            newBottom = -1.0;
        }
        
    	var ratio = frustum.right / frustum.top;
        frustum.top = newTop;
        frustum.bottom = newBottom;
        frustum.right = frustum.top * ratio;
        frustum.left = -frustum.right;
    } else {
    	amount = amount * 0.5;
        var newRight = frustum.right - amount;
        var newLeft = frustum.left + amount;

        var maxRight = camera._maxCoord.x;
        if (camera._scene.mapMode2D === MapMode2D.ROTATE) {
            maxRight *= camera.maximumZoomFactor;
        }

        if (newRight > maxRight) {
            newRight = maxRight;
            newLeft = -maxRight;
        }

        if (newRight <= newLeft) {
            newRight = 1.0;
            newLeft = -1.0;
        }
    	var ratio = frustum.top / frustum.right;
        frustum.right = newRight;
        frustum.left = newLeft;
        frustum.top = frustum.right * ratio;
        frustum.bottom = -frustum.top;
    }
    //******************************************************************************************************************************************
}

@hpinkos
Copy link
Contributor Author

hpinkos commented Feb 14, 2017

Thanks @cfickler! Would you be interested in opening a pull request with that fix? You can see instructions in our contributing documentation: https://github.com/AnalyticalGraphicsInc/cesium/blob/master/CONTRIBUTING.md#opening-a-pull-request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants