forked from felixpalmer/go_images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwscanvas.html
55 lines (49 loc) · 1.36 KB
/
wscanvas.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
<!DOCTYPE html>
<meta charset="utf-8" />
<title>Websocket Canvas</title>
<script language="javascript" type="text/javascript">
var wsUri = "ws://localhost:1234/ws";
var ctx;
function wsConnect()
{
var canvas = document.getElementById( "canvas" );
ctx = canvas.getContext( '2d' );
ws = new WebSocket( wsUri );
ws.onopen = function( e ) {
console.log( "CONNECTED" );
ws.send( "READY" );
};
ws.onclose = function( e ) {
console.log( "DISCONNECTED" );
};
ws.onmessage = function( e ) {
var data = e.data.split("\n");
for ( var line in data ) {
var msg = data[line].split(" ");
var cmd = msg[0];
if ( cmd == "CLEAR" ) {
ctx.clearRect( 0, 0, 512, 512 );
} else if ( cmd == "PIXEL" ) {
var x = parseInt(msg[1]);
var y = parseInt(msg[2]);
var color = msg[3];
ctx.fillStyle = color;
ctx.fillRect( x, y, 1, 1 );
} else {
console.log( "Unknown command: " + cmd );
}
}
};
ws.onerror = function( e ) {
console.log( 'WS Error: ' + e.data );
};
}
window.addEventListener( "load", wsConnect, false );
</script>
<body>
<h2>Websocket Canvas</h2>
<canvas id="canvas" width="512" height="512">
Sorry, your browser does not support Canvas
</canvas>
</body>
</html>