当前位置:   article > 正文

ROS2D缩放、拖动平移ROS地图_ros ros2djs

ros ros2djs

使用ZoomView、PanView对地图进行缩放平移操作

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>ROS地图</title>
  7. <script type="text/javascript" src="./js/ros/eventemitter2.min.js"></script>
  8. <script type="text/javascript" src="./js/ros/roslib.js"></script>
  9. <script type="text/javascript" src="./js/ros/easeljs.min.js"></script>
  10. <script type="text/javascript" src="./js/ros/ros2d.js"></script>
  11. </head>
  12. <body>
  13. <div class="zoom">
  14. <button onclick="zoomIn()">+ 放大</button>
  15. <button onclick="zoomOut()">- 缩小</button>
  16. <button onclick="recovery()">复原</button>
  17. </div><br>
  18. <div id="map"></div>
  19. <script>
  20. var ros = new ROSLIB.Ros({
  21. url: 'ws://192.168.66.20:9090'
  22. });
  23. var viewer = new ROS2D.Viewer({
  24. divID: 'map',
  25. width: 1200,
  26. height: 800
  27. });
  28. var zoomView = new ROS2D.ZoomView({
  29. rootObject: viewer.scene,
  30. minScale: 0.01
  31. });
  32. var panView = new ROS2D.PanView({
  33. rootObject: viewer.scene
  34. });
  35. var gridClient = new ROS2D.OccupancyGridClient({
  36. ros: ros,
  37. rootObject: viewer.scene,
  38. continuous: true,
  39. // topic: '/move_base_node/global_costmap/costmap' // 代价地图 topic; 默认值:'/map'
  40. });
  41. gridClient.on('change', function () {
  42. viewer.scaleToDimensions(gridClient.currentGrid.width, gridClient.currentGrid.height);
  43. viewer.shift(gridClient.currentGrid.pose.position.x, gridClient.currentGrid.pose.position.y);
  44. zoomView.startZoom(600, 400);
  45. dragMap();
  46. });
  47. // zoom
  48. var zoomSize = 1
  49. function zoomIn() {
  50. zoomSize += 0.1
  51. zoomView.zoom(zoomSize)
  52. };
  53. function zoomOut() {
  54. zoomSize -= 0.1
  55. zoomView.zoom(zoomSize)
  56. };
  57. function recovery() {
  58. zoomSize = 1
  59. zoomView.zoom(zoomSize)
  60. };
  61. // pan
  62. function dragMap() {
  63. var panKey = false
  64. viewer.scene.addEventListener('stagemousedown', function (event) {
  65. panKey = true
  66. panView.startPan(event.stageX, event.stageY);
  67. });
  68. viewer.scene.addEventListener('stagemousemove', function (event) {
  69. if (panKey === true) {
  70. panView.pan(event.stageX, event.stageY);
  71. };
  72. });
  73. viewer.scene.addEventListener('stagemouseup', function (event) {
  74. if (panKey === true) {
  75. panKey = false;
  76. };
  77. });
  78. };
  79. </script>
  80. </body>
  81. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/107769
推荐阅读
相关标签
  

闽ICP备14008679号