-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
143 lines (121 loc) · 3.71 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#canvas {
border: black solid 1px;
margin-left: 20px;
}
</style>
<script>
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
class MQTT {
constructor(channel) {
this.channel = channel;
this.client = null
}
connect() {
this.client = mqtt.connect('wss://test.mosquitto.org:8081');
this.client.on('connect', () => {
console.log('kya bakaiti hai')
this.client.subscribe(this.channel, (err) => {
if (err) {
console.log('Something failed', err);
} else {
this.client.publish(this.channel, 'Hello!')
console.log('connected')
}
})
})
}
publishEvent(event, mousePos) {
this.client.publish(this.channel, [event, mousePos.x, mousePos.y].join(' '));
}
onEvent(cb) {
console.log('yahan se');
this.client.on('message', cb)
}
}
class DrawingBoard {
constructor(boardName, ctx) {
this.boardName = boardName;
this.active = false;
this.tool = 'pencil';
this.ctx = ctx;
}
onMouseMove(mousePos) {
if (this.tool === 'pencil' && this.active === true) {
this.ctx.lineTo(mousePos.x, mousePos.y); this.ctx.stroke()
this.ctx.strokeStyle = 'blue';
}
}
onMouseDown(mousePos) {
this.active = true;
if (this.tool==='pencil' && this.active === true) {
console.log('mousedown')
this.ctx.beginPath();
this.ctx.strokeStyle = 'blue';
this.ctx.moveTo(mousePos.x, mousePos.y);
}
}
onMouseUp(mousePos) {
this.active = false;
if (this.tool === 'pencil' && this.active === false) {
console.log('mouseup')
this.ctx.stroke();
}
}
}
window.onload = () => {
const connectTo = prompt('Connect to ?', 'pikapika');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const board = new DrawingBoard(connectTo, ctx);
const messaging = new MQTT(connectTo);
messaging.connect();
messaging.onEvent((topic, msg) => {
console.log(topic, 'Subscription ', msg.toString())
const [event, x, y] = msg.toString().split(' ');
const mousePos = { x, y};
if (event === 'onMouseMove') {
board.onMouseMove(mousePos);
} else if ( event === 'onMouseDown') {
board.onMouseDown(mousePos);
} else if ( event === 'onMouseUp') {
board.onMouseUp(mousePos);
}
})
canvas.onmousemove = function(evt) {
var mousePos = getMousePos(canvas, evt);
board.onMouseMove(mousePos);
messaging.publishEvent('onMouseMove', mousePos);
};
canvas.onmousedown = (e) => {
var mousePos = getMousePos(canvas, e);
board.onMouseDown(mousePos);
messaging.publishEvent('onMouseDown', mousePos);
};
canvas.onmouseup = (e) => {
var mousePos = getMousePos(canvas, e);
board.onMouseUp(mousePos);
messaging.publishEvent('onMouseUp', mousePos);
}
}
</script>
</head>
<body>
<h1>Drawing game</h1>
<canvas id="canvas" height= "500px" width="700px"></canvas>
<script src="https://unpkg.com/mqtt@4.2.8/dist/mqtt.min.js"></script>
</body>
</html>