core/AroundPoint.js

/*
 * 绕点旋转类
 * @Author: jianlei wang
 * @Date: 2024-02-23 14:57:07
 * @Last Modified by: jianlei wang
 * @Last Modified time: 2024-03-27 16:22:11
 */

import { Cesium } from '../../namespace'

/**
 * 绕点旋转类
 * @class
 */
class AroundPoint {
  /**
   * 构造函数
   * @param {Object} viewer 地图场景对象
   * @param {Cartesian3} position 目标点坐标,笛卡尔坐标
   * @param {Number} angle 观察角度,-90为垂直正视,建议值区间[-30,-40]
   * @param {Number} amount 旋转360度所需要时间,单位:秒(s)
   * @param {Number} distance 点距离相机距离,单位:米(m)
   */
  constructor(viewer, position, angle, amount, distance) {
    this._viewer = viewer
    this._time = amount
    this._angle = angle
    this._position = position
    this._distance = distance
    this._startTime = Cesium.JulianDate.fromDate(new Date())
  }

  _bindEvent() {
    this._viewer.clock.onTick.addEventListener(this._rotate, this)
  }

  _unbindEvent() {
    this._viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY)
    this._viewer.clock.onTick.removeEventListener(this._rotate, this)
  }

  _rotate() {
    let delTime = Cesium.JulianDate.secondsDifference(
      this._viewer.clock.currentTime,
      this._startTime
    )
    const angle = 360 / this._time
    let heading = Cesium.Math.toRadians(delTime * angle)
    this._viewer.scene.camera.setView({
      destination: this._position, // 点的坐标
      orientation: {
        heading: heading,
        pitch: Cesium.Math.toRadians(this._angle),
      },
    })
    this._viewer.scene.camera.moveBackward(this._distance)
  }

  /**
   * 开始旋转
   * @returns {AroundPoint} 绕点旋转对象
   */
  start() {
    this._viewer.clock.shouldAnimate = true
    this._unbindEvent()
    this._bindEvent()
    return this
  }

  /**
   * 停止旋转
   * @returns {AroundPoint} 绕点旋转对象
   */
  stop() {
    this._unbindEvent()
    return this
  }

  /**
   * 注销对象
   */
  destroy() {
    this._unbindEvent()
  }
}

export default AroundPoint