AroundPoint.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * @Description: 相机绕点旋转
  3. * @Version: 1.0
  4. * @Author: joy
  5. * @Date: 2023-04-11 21:42:26
  6. * @LastEditors: joy
  7. * @LastEditTime: 2023-04-12 16:32:25
  8. */
  9. class AroundPoint {
  10. constructor(viewer, amount, position) {
  11. this._viewer = viewer;
  12. this._amount = amount;
  13. this._position = position;
  14. this._range = this._viewer.camera.positionCartographic.height;
  15. }
  16. _bindEvent() {
  17. let _self = this;
  18. this._viewer.clock.onTick.addEventListener(this._aroundPoint, this);
  19. //监听点击事件
  20. var handler = new Cesium.ScreenSpaceEventHandler(this._viewer.scene.canvas);
  21. handler.setInputAction(function(click) {
  22. _self.stop();
  23. }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
  24. }
  25. _unbindEvent() {
  26. this._viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
  27. this._viewer.clock.onTick.removeEventListener(this._aroundPoint, this);
  28. }
  29. start() {
  30. this._viewer.clock.shouldAnimate = true;
  31. this._unbindEvent();
  32. this._bindEvent();
  33. return this;
  34. }
  35. stop() {
  36. this._unbindEvent();
  37. return this;
  38. }
  39. // 相机绕点旋转函数
  40. _aroundPoint() {
  41. let heading = this._viewer.camera.heading;
  42. let pitch = this._viewer.camera.pitch;
  43. let range = this._range;
  44. heading += Cesium.Math.toRadians(this._amount);
  45. if (heading >= Math.PI * 2 || heading <= -Math.PI * 2) {
  46. heading = 0;
  47. }
  48. this._viewer.camera.lookAt(
  49. this._position,
  50. new Cesium.HeadingPitchRange(
  51. heading,
  52. pitch,
  53. range,
  54. )
  55. )
  56. }
  57. }
  58. export default AroundPoint