-
Notifications
You must be signed in to change notification settings - Fork 47
/
ros-rviz-image.html
175 lines (156 loc) · 5.64 KB
/
ros-rviz-image.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-checkbox/paper-checkbox.html">
<link rel="import" href="../paper-dropdown-menu/paper-dropdown-menu.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="../paper-listbox/paper-listbox.html">
<link rel="import" href="imports.html">
<dom-module id="ros-rviz-image">
<template>
<paper-input label="Image topic" value="{{topic}}"></paper-input>
<paper-dropdown-menu id="transport_hints_dropdown" label="Transport hints" on-iron-select="transportHintSelected">
<paper-listbox slot="dropdown-content" class="dropdown-content" selected="0">
<paper-item>Compressed</paper-item>
<paper-item>CompressedDepth</paper-item>
</paper-listbox>
</paper-dropdown-menu>
<div style="position: relative;">
<div id="canvas_div" style="position: fixed; z-index: 9; background-color: transparent;" title="[[topic]]">
<canvas id="xy_image" width="280px;" height="280px;"/>
</div>
</div>
</template>
<script>
const _transportHints = {
Compressed: {type: 'sensor_msgs/CompressedImage', topicSuffix: '/compressed'},
CompressedDepth: {type: 'sensor_msgs/CompressedImage', topicSuffix: '/compressedDepth'},
};
Polymer({
is: 'ros-rviz-image',
properties: {
name: {
type: String,
value: 'Image',
},
topic: {
type: String,
value: '/camera/image',
notify: true,
},
transport_hints: {
type: String,
value: 'Compressed',
notify: true,
},
globalOptions: Object,
isShown: Boolean,
ros: Object,
viewer: Object,
},
observers: [
'_optionsChanged(viewer, topic, ros)',
],
transportHintSelected: function(e) {
this.transport_hints = this.$.transport_hints_dropdown.selectedItemLabel;
this._optionsChanged();
},
ready: function() {
// Make the image DIV draggable.
this._dragImageDiv(this.$.canvas_div);
},
destroy: function() {
// Nothing to destroy.
},
hide: function() {
if (this._imageTopic) {
this._imageTopic.unsubscribe();
this._imageTopic = null;
this.$.canvas_div.style.visibility = 'hidden';
}
},
show: function() {
if (this.viewer && this.isShown) {
if (!this._imageTopic) {
this._updateDisplay();
}
this.$.canvas_div.style.visibility = 'visible';
}
},
_optionsChanged: function(viewer, topic, ros) {
this.hide();
this._updateDisplay();
this.show();
},
_updateDisplay: function(callback) {
const that = this;
const canvas = this.$.xy_image;
const context = canvas.getContext('2d');
if (!(this.ros && this.viewer && this.topic &&
(this.transport_hints))) {
return;
}
if (this._imageTopic) {
this._imageTopic.unsubscribe();
}
this._imageTopic = new ROSLIB.Topic({
ros : this.ros,
name : this.topic + _transportHints[this.transport_hints].topicSuffix,
messageType : _transportHints[this.transport_hints].type
});
let updatingImage = false;
this._imageTopic.subscribe(function(message) {
if (updatingImage) {
return; // Drop frame if previous one didn't load yet.
}
updatingImage = true;
let image = new Image();
image.src = "data:image/jpg;base64," + message.data;
image.onload = function() {
// Resize keeping ratio and center the image on the canvas.
const hRatio = canvas.width / image.width;
const vRatio = canvas.height / image.height;
const ratio = Math.min(hRatio, vRatio);
const centerShift_x = (canvas.width - image.width * ratio ) / 2;
const centerShift_y = (canvas.height - image.height * ratio ) / 2;
context.clearRect(0, 0, canvas.width, canvas.height);
context.drawImage(image, 0, 0, image.width, image.height,
centerShift_x, centerShift_y,
image.width * ratio, image.height * ratio);
updatingImage = false;
};
});
},
_dragImageDiv: function(imageDiv) {
// Reference: https://www.w3schools.com/howto/howto_js_draggable.asp.
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
imageDiv.onmousedown = dragMouseDown;
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// Get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// Call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// Calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// Set the element's new position:
imageDiv.style.top = (imageDiv.offsetTop - pos2) + "px";
imageDiv.style.left = (imageDiv.offsetLeft - pos1) + "px";
}
function closeDragElement() {
// Stop moving when mouse button is released.
document.onmouseup = null;
document.onmousemove = null;
}
},
});
</script>
</dom-module>