Capture zoom out event

I want to capture zoom out event as soon as user reduces the map size to an extent i have to change the Map image layer.

var viewer = new Cesium.Viewer("cesiumContainer");
var scene = viewer.scene;
var clock = viewer.clock;
var referenceFramePrimitive;
var camera = viewer.camera;
....
camera.changed.addEventListener(function()
   {
    var height = Cesium.Cartographic.fromCartesian(camera.position).height;
    if(height<4251907)
    {
      var layers = viewer.imageryLayers;
      var baseLayer = layers.get(0);
      layers.remove(baseLayer);
      layers.addImageryProvider(new Cesium.IonImageryProvider({ assetId: 3812,  maximumLevel : 5 }));
    }
  console.log(height);
}.bind(camera));

Is there any way to achieve this.

Thanks

    const viewer = new Cesium.Viewer("cesiumContainer");

    const camera = viewer.camera;

    const scratchCartesian1 = new Cesium.Cartesian3();
    const scratchCartesian2 = new Cesium.Cartesian3();

    let startPos, endPos;

    camera.moveStart.addEventListener(function () {
        startPos = camera.positionWC.clone(scratchCartesian1);

    });

    camera.moveEnd.addEventListener(function () {
        endPos = camera.positionWC.clone(scratchCartesian2);

        const startHeight = Cesium.Cartographic.fromCartesian(startPos).height;
        const endHeight = Cesium.Cartographic.fromCartesian(endPos).height;

        if (startHeight > endHeight) {
            console.log("zoom in");
        } else {
            console.log("zoom out");
        }

    });
1 Like

@ZhefengJin

Thank you for the response. I looked over your solution and it seems robust! @James be sure to take a look at this.

-Sam

@sam.rothstein yes sure i will try this solution thanks @ZhefengJin

1 Like

@James

Great! Keep us posted :grinning:

-Sam