-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse.js
More file actions
executable file
·80 lines (71 loc) · 2.38 KB
/
mouse.js
File metadata and controls
executable file
·80 lines (71 loc) · 2.38 KB
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
window.addEventListener('beforeunload', function () { ws.close(); });
var mousedown=false;
var mysize = 2;
function get_random_color() {
function c() {
var hex = Math.floor(Math.random() * 256).toString(16);
return ("0" + String(hex)).substr(-2); // pad with zero
}
return "#" + c() + c() + c();
}
var mycolor = get_random_color();
var xa = 0;
var ya = 0;
var s = new WebSocket('ws://'+window.location.host+':8080/');
myCanvas.addEventListener("mousedown", function (e) {
mousedown=true;
document.getElementById("myCanvas").style.cursor="pointer";
}, false);
myCanvas.addEventListener("mousemove", function (e) {
if( mousedown == true){
zeichnePunkt(e);
}
xa = e.clientX;
ya = e.clientY;
}, false);
myCanvas.addEventListener("mouseup", function (e) {
mousedown=false;
document.getElementById("myCanvas").style.cursor="crosshair";
}, false);
s.addEventListener('error', function (m) { console.log("error"); });
s.addEventListener('open', function (m) { console.log("websocket connection open"); });
s.addEventListener('message', function (m) {
console.log(m.data);
var kreis = JSON.parse(m.data);
var mycolor = kreis.mycolor;
var x = kreis.x;
var y = kreis.y;
var xa = kreis.xa;
var ya = kreis.ya;
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.moveTo(xa,ya);
ctx.lineTo(x,y);
//ctx.arc(x, y, mysize, 0, 2 * Math.PI);
ctx.strokeStyle = mycolor;
ctx.fillStyle = mycolor;
ctx.fill();
ctx.stroke();
});
function zeichnePunkt(e) {
var x = e.clientX;
var y = e.clientY;
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.lineWidth=10;
ctx.moveTo(xa,ya);
ctx.lineTo(x,y);
//ctx.arc(x, y, mysize, 0, 2 * Math.PI);
ctx.strokeStyle = mycolor;
ctx.fillStyle = mycolor;
ctx.fill();
ctx.stroke();
var coorjs = { "mycolor": mycolor, "x": x, "y": y, "xa": xa, "ya": ya };
document.getElementById("demo").innerHTML = JSON.stringify(coorjs);
s.send(JSON.stringify(coorjs));
}
function clearCoor() {
document.getElementById("demo").innerHTML = "";
}