### Initialize ROS2D.js Viewer and Map Client Source: https://github.com/robotwebtools/ros2djs/blob/develop/examples/image.html Sets up the ROS connection, creates a viewer, and initializes an ImageMapClient to display a map. It also includes an event listener to scale and shift the viewer based on the map's dimensions and pose. ```javascript function init() { // Connect to ROS. var ros = new ROSLIB.Ros({ url : 'ws://localhost:9090' }); // Create the main viewer. var viewer = new ROS2D.Viewer({ divID : 'map', width : 640, height : 480 }); // Setup the map client. var gridClient = new ROS2D.ImageMapClient({ ros : ros, rootObject : viewer.scene, image : 'map.png' }); // Scale the canvas to fit to the map gridClient.on('change', function() { viewer.scaleToDimensions(gridClient.currentImage.width, gridClient.currentImage.height); viewer.shift(gridClient.currentImage.pose.position.x, gridClient.currentImage.pose.position.y); }); } ``` -------------------------------- ### Initialize ROS2DJS Viewer and Drawing Functionality (JavaScript) Source: https://github.com/robotwebtools/ros2djs/blob/develop/examples/draw.html Sets up the main ROS2DJS viewer and defines callback functions for mouse interactions with a polygon. It handles adding, moving, and removing points, as well as splitting lines based on user input like Shift and Ctrl keys. ```javascript function init() { // Create the main viewer. var viewer = new ROS2D.Viewer({ divID : 'map', width : 640, height : 480, context2dOptions: {willReadFrequently: true}, }); // Callback functions when there is mouse interaction with the polygon var clickedPolygon = false; var selectedPointIndex = null; var pointCallBack = function(type, event, index) { if (type === 'mousedown') { if (event.nativeEvent.shiftKey === true) { polygon.remPoint(index); } else { selectedPointIndex = index; } } clickedPolygon = true; }; var lineCallBack = function(type, event, index) { if (type === 'mousedown') { if (event.nativeEvent.ctrlKey === true) { polygon.splitLine(index); } } clickedPolygon = true; } // Create the polygon var polygon = new ROS2D.PolygonMarker({ lineColor : createjs.Graphics.getRGB(100, 100, 255, 1), pointCallBack : pointCallBack, lineCallBack : lineCallBack }); // Add the polygon to the viewer viewer.scene.addChild(polygon); // Event listeners for mouse interaction with the stage viewer.scene.mouseMoveOutside = false; // doesn't seem to work viewer.scene.addEventListener('stagemousemove', function(event) { // Move point when it's dragged if (selectedPointIndex !== null) { var pos = viewer.scene.globalToRos(event.stageX, event.stageY); polygon.movePoint(selectedPointIndex, pos); } }); viewer.scene.addEventListener('stagemouseup', function(event) { // Add point when not clicked on the polygon if (selectedPointIndex !== null) { selectedPointIndex = null; } else if (viewer.scene.mouseInBounds === true && clickedPolygon === false) { var pos = viewer.scene.globalToRos(event.stageX, event.stageY); polygon.addPoint(pos); } clickedPolygon = false; }); } ``` -------------------------------- ### Initialize ROS2D.js Viewer and Map Client Source: https://github.com/robotwebtools/ros2djs/blob/develop/examples/map.html This JavaScript code initializes a connection to a ROS instance via WebSockets, creates a ROS2D viewer for displaying the map, and sets up an OccupancyGridClient to load and display map data. It also includes a callback to scale and shift the viewer to fit the loaded map. ```javascript function init() { // Connect to ROS. var ros = new ROSLIB.Ros({ url : 'ws://localhost:9090' }); // Create the main viewer. var viewer = new ROS2D.Viewer({ divID : 'map', width : 640, height : 480 }); // Setup the map client. var gridClient = new ROS2D.OccupancyGridClient({ ros : ros, rootObject : viewer.scene }); // Scale the canvas to fit to the map gridClient.on('change', function() { viewer.scaleToDimensions(gridClient.currentGrid.width, gridClient.currentGrid.height); viewer.shift(gridClient.currentGrid.pose.position.x, gridClient.currentGrid.pose.position.y); }); } ``` -------------------------------- ### Initialize ROS2D.js Viewer and Map Client Source: https://github.com/robotwebtools/ros2djs/blob/develop/examples/continuous.html Sets up the connection to the ROS WebSocket server and initializes the ROS2D.js viewer and OccupancyGridClient. The client is configured for continuous updates, and an event listener scales and shifts the viewer to match the map dimensions and pose. ```javascript function init() { // Connect to ROS. var ros = new ROSLIB.Ros({ url : 'ws://localhost:9090' }); // Create the main viewer. var viewer = new ROS2D.Viewer({ divID : 'map', width : 640, height : 480 }); // Setup the map client. var gridClient = new ROS2D.OccupancyGridClient({ ros : ros, rootObject : viewer.scene, // Use this property in case of continuous updates continuous: true }); // Scale the canvas to fit to the map gridClient.on('change', function() { viewer.scaleToDimensions(gridClient.currentGrid.width, gridClient.currentGrid.height); viewer.shift(gridClient.currentGrid.pose.position.x, gridClient.currentGrid.pose.position.y); }); } ``` -------------------------------- ### Initialize and Use ROS2D Viewer Source: https://context7.com/robotwebtools/ros2djs/llms.txt Demonstrates how to create a ROS2D Viewer instance, add visual objects like navigation arrows, and adjust the view to fit map dimensions. It requires a DOM element with the specified 'divID' and utilizes EaselJS for rendering. ```javascript var viewer = new ROS2D.Viewer({ divID: 'map-container', width: 800, height: 600, background: '#2d2d2d' }); var arrow = new ROS2D.NavigationArrow({ size: 0.5, fillColor: createjs.Graphics.getRGB(0, 255, 0) }); arrow.x = 2.5; arrow.y = 1.0; viewer.addObject(arrow); viewer.scaleToDimensions(20.0, 15.0); viewer.shift(-5.0, -3.0); ``` -------------------------------- ### Load Static Map via Service Call Source: https://context7.com/robotwebtools/ros2djs/llms.txt Illustrates using OccupancyGridSrvClient to retrieve a static map by calling the map_server's GetMap service. This is efficient for static environments as it avoids continuous topic subscriptions. The viewer is configured upon successful map retrieval. ```javascript var ros = new ROSLIB.Ros({ url: 'ws://localhost:9090' }); var viewer = new ROS2D.Viewer({ divID: 'static-map', width: 800, height: 600 }); var srvClient = new ROS2D.OccupancyGridSrvClient({ ros: ros, service: '/static_map', rootObject: viewer.scene }); srvClient.on('change', function(currentGrid) { viewer.scaleToDimensions(currentGrid.width, currentGrid.height); viewer.shift(currentGrid.pose.position.x, currentGrid.pose.position.y); console.log('Map loaded: ' + currentGrid.width + 'm x ' + currentGrid.height + 'm'); }); ``` -------------------------------- ### Complete ros2djs Robot Monitoring Interface Source: https://context7.com/robotwebtools/ros2djs/llms.txt This HTML and JavaScript code demonstrates a complete ros2djs application. It connects to a ROS instance, displays an occupancy grid map, visualizes the robot's current pose and trail, and allows users to set navigation goals by clicking on the map. It relies on easeljs, eventemitter2, roslib, and ros2djs libraries. ```html
``` -------------------------------- ### Display Occupancy Grid Map from Topic Source: https://context7.com/robotwebtools/ros2djs/llms.txt Shows how to use OccupancyGridClient to subscribe to a ROS occupancy grid topic and render it as a bitmap. It requires a running rosbridge instance and a ROS topic publishing map data. The viewer is automatically scaled and shifted upon map changes. ```javascript var ros = new ROSLIB.Ros({ url: 'ws://localhost:9090' }); var viewer = new ROS2D.Viewer({ divID: 'map', width: 640, height: 480 }); var gridClient = new ROS2D.OccupancyGridClient({ ros: ros, topic: '/map', rootObject: viewer.scene, continuous: false }); gridClient.on('change', function() { var width = gridClient.currentGrid.width; var height = gridClient.currentGrid.height; var pose = gridClient.currentGrid.pose; viewer.scaleToDimensions(width, height); viewer.shift(pose.position.x, pose.position.y); }); ``` -------------------------------- ### Display Custom PNG Map with ROS Metadata - ROS2DJS Source: https://context7.com/robotwebtools/ros2djs/llms.txt The ImageMapClient displays a custom PNG image as a map, scaled according to ROS map metadata from the /map_metadata topic. It requires a ROS connection and a viewer instance. The client listens for map metadata changes to rescale and reposition the viewer. ```javascript var ros = new ROSLIB.Ros({ url: 'ws://localhost:9090' }); var viewer = new ROS2D.Viewer({ divID: 'image-map', width: 640, height: 480 }); // Load custom image map var imageClient = new ROS2D.ImageMapClient({ ros: ros, topic: '/map_metadata', // Topic for map metadata (default: '/map_metadata') image: 'custom_map.png', // URL or path to the map image rootObject: viewer.scene }); // Scale viewer when metadata is received imageClient.on('change', function() { viewer.scaleToDimensions( imageClient.currentImage.width, imageClient.currentImage.height ); viewer.shift( imageClient.currentImage.pose.position.x, imageClient.currentImage.pose.position.y ); }); ``` -------------------------------- ### ROS2D.TraceShape: Visualize Robot Trail Source: https://context7.com/robotwebtools/ros2djs/llms.txt Records and displays a trail of robot poses with configurable history and distance-based filtering. It visualizes the robot's traveled path. Dependencies include ROSLIBJS and CreateJS. ```javascript var robotTrace = new ROS2D.TraceShape({ strokeSize: 2, strokeColor: createjs.Graphics.getRGB(255, 165, 0), // Orange trace maxPoses: 200, // Maximum poses to keep (0 for infinite) minDist: 0.1 // Minimum distance between recorded poses (meters) }); viewer.addObject(robotTrace); var poseTopic = new ROSLIB.Topic({ ros: ros, name: '/robot_pose', messageType: 'geometry_msgs/Pose' }); poseTopic.subscribe(function(message) { var pose = new ROSLIB.Pose({ position: message.position, orientation: message.orientation }); robotTrace.addPose(pose); }); robotTrace.addPose(new ROSLIB.Pose({ position: { x: 1.0, y: 2.0, z: 0 } })); ``` -------------------------------- ### Implement Zoom Functionality with ROS2D.ZoomView Source: https://context7.com/robotwebtools/ros2djs/llms.txt This snippet demonstrates how to set up and use the ROS2D.ZoomView helper to enable zoom functionality centered on a specific point. It handles mouse wheel events to adjust the zoom level while preserving the view's center. Dependencies include the ROS2D library and a viewer instance. ```javascript var zoomView = new ROS2D.ZoomView({ rootObject: viewer.scene, minScale: 0.001 // Minimum zoom scale to maintain precision }); var currentZoom = 1.0; // Handle mouse wheel zoom viewer.scene.canvas.addEventListener('wheel', function(event) { event.preventDefault(); // Calculate zoom factor var zoomDelta = event.deltaY > 0 ? 0.9 : 1.1; currentZoom *= zoomDelta; // Start zoom centered on mouse position zoomView.startZoom(event.offsetX, event.offsetY); zoomView.zoom(currentZoom); }); ``` -------------------------------- ### ROS2D.PanView: Enable Scene Panning Source: https://context7.com/robotwebtools/ros2djs/llms.txt Provides click-and-drag panning functionality for the visualization scene. It tracks mouse movements to translate the stage. Dependencies include CreateJS. ```javascript var panView = new ROS2D.PanView({ rootObject: viewer.scene }); var isPanning = false; viewer.scene.addEventListener('stagemousedown', function(event) { if (event.nativeEvent.button === 1) { // Middle mouse button isPanning = true; panView.startPan(event.stageX, event.stageY); } }); viewer.scene.addEventListener('stagemousemove', function(event) { if (isPanning) { panView.pan(event.stageX, event.stageY); } }); viewer.scene.addEventListener('stagemouseup', function(event) { isPanning = false; }); ``` -------------------------------- ### Convert Coordinates using ROS2D Viewer Utilities Source: https://context7.com/robotwebtools/ros2djs/llms.txt This code illustrates how to leverage ROS2D.Viewer's extended createjs.Stage methods for coordinate transformations. It shows converting screen coordinates (from mouse events) to ROS world coordinates and vice-versa, as well as converting ROS quaternions to canvas rotation angles. This is crucial for tasks like navigation goal setting and displaying robot positions. ```javascript var viewer = new ROS2D.Viewer({ divID: 'map', width: 640, height: 480 }); // After setting up map scaling... viewer.scaleToDimensions(20.0, 15.0); // Convert screen click to ROS coordinates viewer.scene.addEventListener('stagemouseup', function(event) { // Convert global stage coordinates to ROS world coordinates var rosPos = viewer.scene.globalToRos(event.stageX, event.stageY); console.log('Clicked at ROS position: x=' + rosPos.x + ', y=' + rosPos.y); // Use for sending navigation goals var goal = new ROSLIB.Pose({ position: { x: rosPos.x, y: rosPos.y, z: 0 }, orientation: { w: 1, x: 0, y: 0, z: 0 } }); }); // Convert ROS position to screen coordinates var robotRosPos = { x: 5.0, y: 3.0 }; var screenPos = viewer.scene.rosToGlobal(robotRosPos); console.log('Robot on screen at: x=' + screenPos.x + ', y=' + screenPos.y); // Convert ROS quaternion to canvas rotation var orientation = { w: 0.707, x: 0, y: 0, z: 0.707 }; var degrees = viewer.scene.rosQuaternionToGlobalTheta(orientation); console.log('Rotation: ' + degrees + ' degrees'); ``` -------------------------------- ### ROS2D.PathShape: Visualize nav_msgs/Path Source: https://context7.com/robotwebtools/ros2djs/llms.txt Renders a nav_msgs/Path message as a connected line. It can be initialized with path data or updated via subscription to a ROS topic. Dependencies include ROSLIBJS and CreateJS. ```javascript var pathShape = new ROS2D.PathShape({ strokeSize: 2, strokeColor: createjs.Graphics.getRGB(0, 100, 255) // Blue path }); viewer.addObject(pathShape); var pathTopic = new ROSLIB.Topic({ ros: ros, name: '/move_base/NavfnROS/plan', messageType: 'nav_msgs/Path' }); pathTopic.subscribe(function(message) { pathShape.setPath(message); }); var manualPath = { poses: [ { pose: { position: { x: 0, y: 0 } } }, { pose: { position: { x: 2, y: 1 } } }, { pose: { position: { x: 4, y: 1.5 } } }, { pose: { position: { x: 6, y: 3 } } } ] }; pathShape.setPath(manualPath); ``` -------------------------------- ### ROS2D.PolygonMarker: Interactive Polygon Editor Source: https://context7.com/robotwebtools/ros2djs/llms.txt Creates an interactive and editable polygon shape for defining regions. Users can add, remove, and drag points with mouse events. Supports callbacks for point and line interactions. Dependencies include ROSLIBJS and CreateJS. ```javascript var viewer = new ROS2D.Viewer({ divID: 'polygon-editor', width: 640, height: 480 }); var selectedPointIndex = null; var polygon = new ROS2D.PolygonMarker({ lineSize: 2, lineColor: createjs.Graphics.getRGB(100, 100, 255, 1), pointSize: 8, pointColor: createjs.Graphics.getRGB(255, 0, 0, 0.8), fillColor: createjs.Graphics.getRGB(0, 255, 0, 0.3), pointCallBack: function(type, event, index) { if (type === 'mousedown') { if (event.nativeEvent.shiftKey) { polygon.remPoint(index); } else { selectedPointIndex = index; } } }, lineCallBack: function(type, event, index) { if (type === 'mousedown' && event.nativeEvent.ctrlKey) { polygon.splitLine(index); } } }); viewer.addObject(polygon); viewer.scene.addEventListener('stagemousemove', function(event) { if (selectedPointIndex !== null) { var pos = viewer.scene.globalToRos(event.stageX, event.stageY); polygon.movePoint(selectedPointIndex, pos); } }); viewer.scene.addEventListener('stagemouseup', function(event) { if (selectedPointIndex !== null) { selectedPointIndex = null; } else { var pos = viewer.scene.globalToRos(event.stageX, event.stageY); polygon.addPoint(pos); } }); ``` -------------------------------- ### Display Custom Image as Navigation Marker - ROS2DJS Source: https://context7.com/robotwebtools/ros2djs/llms.txt The NavigationImage displays a custom image as a navigation marker, useful for robot icons or waypoint markers. It allows for customizable size, image source, opacity, and an optional pulsing effect. The marker can be positioned and rotated within the viewer. ```javascript // Create image-based robot marker var robotMarker = new ROS2D.NavigationImage({ size: 50, // Desired size in pixels image: 'robot_icon.png', // URL to marker image pulse: false, // Optional pulsing effect alpha: 1.0 // Opacity (0-1) }); // Position at robot location robotMarker.x = 4.2; robotMarker.y = 3.1; robotMarker.rotation = 45; // Rotation in degrees viewer.addObject(robotMarker); // Create semi-transparent waypoint marker var waypoint = new ROS2D.NavigationImage({ size: 30, image: 'waypoint.png', alpha: 0.7, pulse: true }); waypoint.x = 6.0; waypoint.y = 4.5; viewer.addObject(waypoint); ``` -------------------------------- ### ROS2D.Grid: Visualize Coordinate Grid Source: https://context7.com/robotwebtools/ros2djs/llms.txt Draws a coordinate grid overlay for debugging and scale reference. It features centered axes and configurable grid lines. Dependencies include CreateJS. ```javascript var grid = new ROS2D.Grid({ size: 20, // Grid extent from center (grid spans -20 to +20) cellSize: 1.0, // Size of each grid cell in meters lineWidth: 0.01 // Width of grid lines }); viewer.addObject(grid); ``` -------------------------------- ### Create Navigation Arrow for Robot Pose/Goals - ROS2DJS Source: https://context7.com/robotwebtools/ros2djs/llms.txt The NavigationArrow creates a triangular arrow shape for representing robot pose or navigation goals. It supports customizable colors, sizes, and an optional pulsing animation. The arrow can be positioned and rotated using ROS coordinates and quaternions. ```javascript // Create a navigation arrow for robot pose var robotPose = new ROS2D.NavigationArrow({ size: 15, // Arrow size in pixels strokeSize: 2, // Outline width strokeColor: createjs.Graphics.getRGB(0, 0, 0), // Black outline fillColor: createjs.Graphics.getRGB(0, 200, 0), // Green fill pulse: false // Pulsing animation }); // Position the arrow at robot's location (ROS coordinates) robotPose.x = 3.5; // X position in meters robotPose.y = 2.0; // Y position in meters // Set rotation from ROS quaternion var orientation = { w: 0.707, x: 0, y: 0, z: 0.707 }; // 90 degrees robotPose.rotation = viewer.scene.rosQuaternionToGlobalTheta(orientation); viewer.addObject(robotPose); // Create a pulsing goal marker var goalMarker = new ROS2D.NavigationArrow({ size: 20, fillColor: createjs.Graphics.getRGB(255, 100, 0), pulse: true // Enable pulsing animation }); goalMarker.x = 8.0; goalMarker.y = 5.0; viewer.addObject(goalMarker); ``` -------------------------------- ### Create Directional Arrow Shape - ROS2DJS Source: https://context7.com/robotwebtools/ros2djs/llms.txt The ArrowShape creates a directional arrow with a line body and triangular head, suitable for displaying vectors or directions. It points to the right at 0 degrees rotation by default. The shape can be customized with size, stroke, fill color, and pulsing animation. ```javascript // Create direction arrow var directionArrow = new ROS2D.ArrowShape({ size: 25, // Total arrow length strokeSize: 2, // Line thickness strokeColor: createjs.Graphics.getRGB(50, 50, 50), // Dark gray stroke fillColor: createjs.Graphics.getRGB(255, 0, 0), // Red arrowhead pulse: false // Pulsing animation }); // Position and rotate directionArrow.x = 5.0; directionArrow.y = 3.0; directionArrow.rotation = 135; // Point to upper-left viewer.addObject(directionArrow); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.