ImageLayerSplit.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* 引入Cesium */
  2. // import * as Cesium from 'Cesium';
  3. /**
  4. * 卷帘对
  5. * Cesium 影像卷帘对,就是把上层影像卷起来,和下面一层影像做对比。
  6. * 可左卷也可右卷
  7. */
  8. class ImageLayerSplit {
  9. /**
  10. * 默认初始化
  11. * @param {Object} viewer 三维场景
  12. */
  13. constructor(viewer) {
  14. if (!viewer) throw new Cesium.DeveloperError('no viewer object!');
  15. this._viewer = viewer;
  16. }
  17. }
  18. /**
  19. * 通用对外公开函数
  20. */
  21. Object.assign(ImageLayerSplit.prototype, /** @lends ImageLayerSplit.prototype */ {
  22. /**
  23. * 卷帘对比初始化
  24. */
  25. initSplit() {
  26. let _self = this;
  27. this.viewer = this._viewer;
  28. let sliderDiv = document.getElementById("image_slider");
  29. if (sliderDiv == null) {
  30. //创建画布
  31. sliderDiv = document.createElement('div');
  32. sliderDiv.id = "image_slider";
  33. sliderDiv.style.position = "absolute";
  34. sliderDiv.style.left = "50%";
  35. sliderDiv.style.top = "0px";
  36. sliderDiv.style.backgroundColor = "#d3d3d3";
  37. sliderDiv.style.width = "5px";
  38. sliderDiv.style.height = "100%";
  39. sliderDiv.style.zIndex = "9999";
  40. sliderDiv.onmouseover = function() {
  41. //设置其背景颜色为黄色
  42. this.style.cursor = "ew-resize";
  43. };
  44. /* 加入到页面 */
  45. document.body.appendChild(sliderDiv);
  46. }
  47. // 设置图像拆分位置
  48. this.slider = sliderDiv;
  49. viewer.scene.splitPosition = this.slider.offsetLeft / this.slider.parentElement.offsetWidth; //确定分割点位置,占据父级容器的比例
  50. if (this.handler) {
  51. this.handler.destroy();
  52. this.handler = null;
  53. }
  54. //处理用户输入事件。可以添加自定义功能以在用户输入时执行;参数为任意
  55. this.handler = new Cesium.ScreenSpaceEventHandler(this.slider);
  56. var moveActive = false;
  57. // 计算拆分
  58. function move(movement) {
  59. if (!moveActive) {
  60. return;
  61. }
  62. //捕获滑动停止的位置
  63. var relativeOffset = movement.endPosition.x;
  64. var splitPosition = (_self.slider.offsetLeft + relativeOffset) / _self.slider.parentElement.offsetWidth;
  65. _self.slider.style.left = `${100.0 * splitPosition}%`;
  66. viewer.scene.splitPosition = splitPosition;
  67. }
  68. //对分割条的操作
  69. this.handler.setInputAction(function() {
  70. moveActive = true;
  71. }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
  72. this.handler.setInputAction(function() {
  73. moveActive = true;
  74. }, Cesium.ScreenSpaceEventType.PINCH_START);
  75. this.handler.setInputAction(move, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  76. this.handler.setInputAction(move, Cesium.ScreenSpaceEventType.PINCH_MOVE);
  77. this.handler.setInputAction(function() {
  78. moveActive = false;
  79. }, Cesium.ScreenSpaceEventType.LEFT_UP);
  80. this.handler.setInputAction(function() {
  81. moveActive = false;
  82. }, Cesium.ScreenSpaceEventType.PINCH_END);
  83. },
  84. /**
  85. * 卷帘对比
  86. * @param {Object} earthAtLeft 左侧图层
  87. * @param {Object} earthAtRight 右侧图层
  88. */
  89. addSplitLayer(earthAtLeft, earthAtRight) {
  90. let _self = this;
  91. if (this.earthAtLeft) {
  92. this._viewer.imageryLayers.remove(this.earthAtLeft); //移除图层
  93. }
  94. if (this.earthAtRight) {
  95. this._viewer.imageryLayers.remove(this.earthAtRight); //移除图层
  96. }
  97. this.earthAtLeft = earthAtLeft;
  98. this.earthAtRight = earthAtRight;
  99. this.earthAtLeft.splitDirection = Cesium.SplitDirection.LEFT;
  100. this.earthAtRight.splitDirection = Cesium.SplitDirection.RIGHT;
  101. },
  102. /**
  103. * 移除卷帘
  104. */
  105. removeSplitLayer() {
  106. document.body.removeChild(this.slider);
  107. if (this.earthAtLeft) {
  108. this._viewer.imageryLayers.remove(this.earthAtLeft); //移除图层
  109. }
  110. if (this.earthAtRight) {
  111. this._viewer.imageryLayers.remove(this.earthAtRight); //移除图层
  112. }
  113. },
  114. });
  115. export default ImageLayerSplit;