forked from jstnhuang/ros-rviz
-
Notifications
You must be signed in to change notification settings - Fork 15
/
ros-rviz-image.html
222 lines (194 loc) · 6.71 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<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>
<style>
#image_stream {
float: left;
margin-left: 105%;
}
#canvas_div {
position: fixed;
z-index: 9;
background-color: transparent;
}
</style>
<div id="image_stream">
<div id="canvas_div" title="[[topic]]">
<canvas id="xy_image" width="280px;" height="280px;"/>
</div>
</div>
<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>
</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,
compression : 'cbor',
messageType : _transportHints[this.transport_hints].type
});
this._initCanvas(canvas, context);
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();
let blobUrl = null;
if (message.data.buffer) {
// Binary path -- load Blob
blobUrl = URL.createObjectURL(new Blob([message.data], {type: 'image/jpg'}));
image.src = blobUrl;
} else {
// Text path -- load base64 string
image.src = "data:image/jpg;base64," + message.data;
}
function finishUpdate() {
updatingImage = false;
if (blobUrl) {
URL.revokeObjectURL(blobUrl);
}
}
image.onerror = function(e) {
finishUpdate();
console.error("Error loading Image", e);
};
image.onload = function() {
finishUpdate();
// 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);
};
});
},
_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;
}
},
_initCanvas: function(canvas, context) {
context.fillStyle = "black";
context.fillRect(0, 0, canvas.width, canvas.height);
context.font = "30pt Roboto";
context.fillStyle = "white";
context.fillText("No image", 55, 240);
},
});
</script>
</dom-module>