# Globe Auto Rotate - Cant find the flaw

**URL:** <https://community.cesium.com/t/globe-auto-rotate-cant-find-the-flaw/18362>\
**Category:** CesiumJS\
**Created:** [April 27, 2022, 9:11pm UTC](https://community.cesium.com/t/globe-auto-rotate-cant-find-the-flaw/18362 "2022-04-27T21:11:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cloudatlas](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/cloudatlas/32/4425_2.png) [@cloudatlas](https://community.cesium.com/u/cloudatlas)\
**Post date:** [April 27, 2022, 9:11pm UTC](https://community.cesium.com/t/globe-auto-rotate-cant-find-the-flaw/18362/1 "2022-04-27T21:11:23Z")

</div>

It cant find the flaw.

This code was working before. The globe rotated.

```auto
// Globe Auto Rotate

const addGlobeAutoRotation = (viewer: Viewer, rotationSpeed: number): void => {
  viewer.canvas.addEventListener('mousemove', (event: MouseEvent) => {
    const mousePosition = new Cartesian2(event.x, event.y);

    // Get the position of the globe below the mouse position
    // Will be undefined if the globe isn't below the cursor
    const cartesian = viewer.camera.pickEllipsoid(
      mousePosition,
      viewer.scene.globe.ellipsoid
    );

    if (!cartesian) {
      startRotation(viewer, rotationSpeed);
      return;
    }

    stopRotation();
  });

  startRotation(viewer, rotationSpeed);
};

// Globe Auto Rotate

let rotationPaused = false;
let lastRotationTime: number | null = null;
let eventHandler: (() => void) | null = null;

const doRotate = (viewer: Viewer, rotationSpeed: number) => {
  if (rotationPaused) {
    return;
  }

  const now = Date.now();
  // Positiv: rotates from left to right
  const spinRate = rotationSpeed;
  const delta = (now - (lastRotationTime ?? now)) / 1000;

  lastRotationTime = now;

  viewer.scene.camera.rotate(Cartesian3.UNIT_Z, spinRate * delta);
};

const startRotation = (viewer: Viewer, rotationSpeed = 0.5) => {
  // Already added, just continue the loop
  if (eventHandler !== null) {
    if (rotationPaused) {
      // Updating this prevents a large rotation after a longer pause
      lastRotationTime = Date.now();
      rotationPaused = false;
    }

    return;
  }

  lastRotationTime = Date.now();
  eventHandler = () => doRotate(viewer, rotationSpeed);

  viewer.scene.postRender.addEventListener(eventHandler);
};

const stopRotation = () => {
  rotationPaused = true;
};

```

This is the whole file.

> <https://github.com/worldpeaceenginelabs/CLOUD-ATLAS-SVELTE/blob/main/src/Cesium.svelte>
