Stop Globe rotation not working

  1. A concise explanation of the problem you’re experiencing.
    Stop Globe rotation function not working

  2. A minimal code example. If you’ve found a bug, this helps us reproduce and repair it.
    var viewer = new Cesium.Viewer(‘cesiumContainer’);
    var scene = viewer.scene;

function spinGlobe( dynamicRate ){
var previousTime = Date.now();

viewer.scene.postRender.addEventListener(function (scene, time){
    var spinRate = dynamicRate;
    var currentTime = Date.now();
    var delta = ( currentTime - previousTime ) / 1000;
    previousTime = currentTime;
    viewer.scene.camera.rotate(Cesium.Cartesian3.UNIT_Z, spinRate * delta);
});

}

function stopglobe() {
//console.log(‘hjhggh’);
spinGlobe(0.0);
viewer.scene.postRender.removeEventListener(spinGlobe);

}

Sandcastle.addToolbarMenu([

{

text : 'Select',

},

{

text : 'Spin Globe',
onselect : function() {
    spinGlobe(0.08);
    
}

}, {
text : ‘Stop GlobeRotation’,
onselect : function() {
stopglobe(0.0);
viewer.scene.postRender.removeEventListener(spinGlobe);

}

}]);

Sandcastle.reset = function() {
viewer.entities.removeAll();
};

  1. Context. Why do you need to do this? We might know a better way to accomplish your goal.
    Project requirement

  2. The Cesium version you’re using, your operating system and browser.
    1.68

Hi Omar,

I used the following logic:

var viewer = new Cesium.Viewer(‘cesiumContainer’);
var isSpinning = true;
//default true for launch app rotate
var scene = viewer.scene;

                    // Add event to rotate camera for globe
                    function addSpinCameraEvent(){
                      viewer.clock.onTick.addEventListener(spinCamera);
                    }
                    
                    // Remove event to rotate camera for globe
                    function removeSpinCameraEvent(){
                      viewer.clock.onTick.removeEventListener(spinCamera);
                    }
                    
                    //rotate globe function
                      function rotateGlobeCamera(){
                        //if not spinning, spin
                        if (!isSpinning){
                          addSpinCameraEvent();
                          isSpinning = true;
                        }
                        else{
                          //stop spinning
                          removeSpinCameraEvent();
                          isSpinning = false;
                        }
                      }
                    
                    //spin camera function
                      function spinCamera(clock) {
                      if (scene.mode === Cesium.SceneMode.SCENE3D)
                      {
                          //scene.camera.rotateRight(0.001);}
                          scene.camera.rotateLeft(0.001);}
                      }
                 
                    Sandcastle.addToolbarButton('click to spin', function(){
                       addSpinCameraEvent();
                      
                    });
                    
                    Sandcastle.addToolbarButton('click to stop', function(){
                        removeSpinCameraEvent();
                    }); 

Thanks,
Kulvinder

1 Like