Webplot: add "clear" msg sent from server->browser
There wasn't a way to clear the display from the server, which is
important for using webplot as the visualizer for the touch fw
test. The idea being that instead of restarting the server/etc for
each gesture we'll want to have one long-lived connection that is
simply cleared between gestures.
This CL just adds an additional command that webplot.js can accept.
When the server sends "clear" over the websocket the JS will clear
the screen.
BUG=chromium:473286
TEST=I added a signal handler into the server (only for testing) that
allowed me to send the "clear" command to the browser by sending
SIGUSR1 to the server. When I ran "kill -10 $WEBPLOT_PID" the screen
would be cleared and I could continue plotting points as usual after.
Change-Id: Icfed758cb7b3228474f6fb672742e2f9eaa35f97
Signed-off-by: Charlie Mooney <charliemooney@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/263783
Reviewed-by: Shyh-In Hwang <josephsih@chromium.org>
diff --git a/webplot/webplot.js b/webplot/webplot.js
index 55673b1..21e6c37 100644
--- a/webplot/webplot.js
+++ b/webplot/webplot.js
@@ -298,7 +298,7 @@
// and notify the server that this client quits.
window.ws.send('quit');
- canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
+ clear(false);
if (closed_by_server) {
webplot.fillText('The python server has quit.', startX, startY, font);
startY += 100;
@@ -316,15 +316,13 @@
*/
function keyupHandler() {
var webplot = window.webplot;
- var canvas = document.getElementById('canvasWebplot');
var key = String.fromCharCode(event.which).toLowerCase();
var ESC = String.fromCharCode(27);
switch(String.fromCharCode(event.which).toLowerCase()) {
// ESC: clearing the canvas
case ESC:
- canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
- webplot.updateCanvasDimension();
+ clear(true);
break;
// 'b': toggle the background color between black and white
@@ -370,6 +368,14 @@
}
+function clear(should_redraw_border) {
+ var canvas = document.getElementById('canvasWebplot');
+ canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
+ if (should_redraw_border) {
+ window.webplot.updateCanvasDimension();
+ }
+}
+
/**
* Create a web socket and a new webplot object.
*/
@@ -387,6 +393,8 @@
if (event.data == 'quit') {
save();
quit(true);
+ } else if (event.data == 'clear') {
+ clear(true);
} else {
var snapshot = JSON.parse(event.data);
webplot.processSnapshot(snapshot);