Cesium基础知识-键盘鼠标事件

  1. viewer = new Cesium.Viewer('cesiumContainer');
  2. //操作1
  3. function oneMouse() {
  4.     camera.setView({
  5.         destination: new Cesium.Cartesian3(x, y, z),
  6.         orientation: {
  7.             heading: headingAngle,
  8.             pitch: pitchAngle,
  9.             roll: rollAngle
  10.         }
  11.     });
  12. }
  13. //oneMouse();
  14. //操作2
  15. function twoMouse() {
  16.     viewer.camera.setView({
  17.         destination: Cesium.Rectangle.fromDegrees(west, south, east, north),
  18.         orientation: {
  19.             heading: headingAngle,
  20.             pitch: pitchAngle,
  21.             roll: rollAngle
  22.         }
  23.     });
  24. }
  25. //twoMouse();
  26. //使用键盘,禁用默认控制
  27. function useKeyboard() {
  28.     // viewer = new Cesium.Viewer('cesiumContainer');
  29.     var scene = viewer.scene;
  30.     var canvas = viewer.canvas;
  31.     canvas.setAttribute('tabindex', '0'); // needed to put focus on the canvas
  32.     canvas.onclick = function () {
  33.         canvas.focus();
  34.     };
  35.     var ellipsoid = viewer.scene.globe.ellipsoid;
  36.     //禁用默认控制
  37.     scene.screenSpaceCameraController.enableRotate = false;
  38.     scene.screenSpaceCameraController.enableTranslate = false;
  39.     scene.screenSpaceCameraController.enableZoom = false;
  40.     scene.screenSpaceCameraController.enableTilt = false;
  41.     scene.screenSpaceCameraController.enableLook = false;
  42.     //记录当前鼠标位置,标记相机移动轨迹
  43.     var startMousePosition;
  44.     var mousePosition;
  45.     var flags = {
  46.         looking: false,
  47.         moveForward: false,
  48.         moveBackward: false,
  49.         moveUp: false,
  50.         moveDown: false,
  51.         moveLeft: false,
  52.         moveRight: false
  53.     };

  54.  
  55.     //添加事件控制相机的标记
  56.     var handler = new Cesium.ScreenSpaceEventHandler(canvas);
  57.     handler.setInputAction(function (movement) {
  58.         flags.looking = true;
  59.         mousePosition = startMousePosition = Cesium.Cartesian3.clone(movement.position);
  60.     }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
  61.     handler.setInputAction(function (movement) {
  62.         mousePosition = movement.endPosition;
  63.     }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
  64.     handler.setInputAction(function (position) {
  65.         flags.looking = false;
  66.     }, Cesium.ScreenSpaceEventType.LEFT_UP);

  67.  
  68.     //键盘事件
  69.     function getFlagForKeyCode(keyCode) {
  70.         switch (keyCode) {
  71.             case 'W'.charCodeAt(0):
  72.                 return 'moveForward';
  73.             case 'S'.charCodeAt(0):
  74.                 return 'moveBackward';
  75.             case 'Q'.charCodeAt(0):
  76.                 return 'moveUp';
  77.             case 'E'.charCodeAt(0):
  78.                 return 'moveDown';
  79.             case 'D'.charCodeAt(0):
  80.                 return 'moveRight';
  81.             case 'A'.charCodeAt(0):
  82.                 return 'moveLeft';
  83.             default:
  84.                 return undefined;
  85.         }
  86.     }
  87.     document.addEventListener('keydown', function (e) {
  88.         var flagName = getFlagForKeyCode(e.keyCode);
  89.         if (typeof flagName !== 'undefined') {
  90.             flags[flagName] = true;
  91.         }
  92.     }, false);
  93.     document.addEventListener('keyup', function (e) {
  94.         var flagName = getFlagForKeyCode(e.keyCode);
  95.         if (typeof flagName !== 'undefined') {
  96.             flags[flagName] = false;
  97.         }
  98.     }, false);
  99.     //更新相机位置
  100.     viewer.clock.onTick.addEventListener(function (clock) {
  101.         var camera = viewer.camera;
  102.         //相机指向鼠标指向的方形
  103.         if (flags.looking) {
  104.             var width = canvas.clientWidth;
  105.             var height = canvas.clientHeight;
  106.             // Coordinate (0.0, 0.0) will be where the mouse was clicked.
  107.             var x = (mousePosition.x - startMousePosition.x) / width;
  108.             var y = -(mousePosition.y - startMousePosition.y) / height;
  109.             var lookFactor = 0.05;
  110.             camera.lookRight(x * lookFactor);
  111.             camera.lookUp(y * lookFactor);
  112.         }
  113.         // 相机移动
  114.         var cameraHeight = ellipsoid.cartesianToCartographic(camera.position).height;
  115.         var moveRate = cameraHeight / 100.0;
  116.         if (flags.moveForward) {
  117.             camera.moveForward(moveRate);
  118.         }
  119.         if (flags.moveBackward) {
  120.             camera.moveBackward(moveRate);
  121.         }
  122.         if (flags.moveUp) {
  123.             camera.moveUp(moveRate);
  124.         }
  125.         if (flags.moveDown) {
  126.             camera.moveDown(moveRate);
  127.         }
  128.         if (flags.moveLeft) {
  129.             camera.moveLeft(moveRate);
  130.         }
  131.         if (flags.moveRight) {
  132.             camera.moveRight(moveRate);
  133.         }
  134.         //参数限制
  135.         //var west = Cesium.Math.toRadians(-77.0);
  136.         //var south = Cesium.Math.toRadians(38.0);
  137.         //var east = Cesium.Math.toRadians(-72.0);
  138.         //var north = Cesium.Math.toRadians(42.0);
  139.         //var extent = new Cesium.Extent(west, south, east, north);
  140.         //camera.viewExtent(extent, Cesium.Ellipsoid.WGS84);

  141.  
  142.         // find intersection of the pixel picked and an ellipsoid
  143.         var ray = camera.getPickRay(mousePosition);
  144.         var intersection = Cesium.IntersectionTests.rayEllipsoid(ray, Cesium.Ellipsoid.WGS84);
  145.     });
  146.     
  147. }
  148. useKeyboard();
上一篇:树莓派ARM64系统中如何设置opencv-python读取高清摄像头画面


下一篇:Jmeter之Bean shell使用(一)