-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
166 lines (150 loc) · 5.07 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<html>
<head>
<script src="https://code.jquery.com/jquery-3.5.0.min.js" integrity="sha256-xNzN2a4ltkB44Mc/Jz3pT4iU1cmeR0FkXs4pru/JxaQ=" crossorigin="anonymous"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.bundle.min.js" integrity="sha384-6khuMg9gaYr5AxOqhkVIODVIvm9ynTT5J4V1cfthmT+emCG6yVmEZsRHdxlotUnm" crossorigin="anonymous"></script>
<style>
.center1{
margin-left:auto;
margin-right:auto;
}
.body,
.wrapper {
position: absolute;
width: 100%;
height: 100%;
}
.body {
z-index: 1;
}
.wrapper {
z-index: 2;
}
</style>
<script>
var canvas,context,pencilSize, pencilColor;
var dragging=false;
var points=[];
var scaleX = 900;
var socket = new WebSocket("ws://cvboard.herokuapp.com",3000);
//var socket = new WebSocket("ws://localhost:3000");
socket.onmessage = function(msg){
console.log("ws msg");
data = JSON.parse(msg.data);
if(data.ping){
console.log("pinged by server");
return;
}
if(data.clientCount){
document.getElementById('clientCnt').innerHTML = "Clients: " + data.clientCount;
return;
}
//console.log("# " + data.x + " " + data.y);
context.beginPath();
if(data[0].lineWidth) context.lineWidth = data[0].lineWidth;
if(data[0].strokeStyle) context.strokeStyle = data[0].strokeStyle;
for(var i=1;i<data.length;i++){
context.lineTo(data[i].x,data[i].y);
}
context.stroke();
}
socket.onopen = function(e){
console.log("connected to ws server");
}
socket.onclose = function(e){
console.log("disconnected");
}
function afterLoad(){
canvas = $("#board")[0];
pencilSize = $("#pencilSize")[0];
pencilColor = $("#drawColor")[0];
scaleX = canvas.width;
cp = $("#cp")[0];
cp.style.width = cp.parentElement.style.width;
canvas.style.width = cp.clientWidth - parseInt(cp.style.padding.replace('px','')) * 2;
canvas.style.height = canvas.height;
str = canvas.style.width;
str = str.substring(0,str.length-2);
scaleX /= parseInt(str);
context = canvas.getContext('2d');
canvas.addEventListener('mousedown',mouseDown);
canvas.addEventListener('mouseup',mouseUp);
canvas.addEventListener('mousemove',mouseMove);
canvas.addEventListener("touchstart", function (e) {
mousePos = getTouchPos(canvas, e);
mouseDown({
offsetX: mousePos.x-20,
offsetY: mousePos.y-20,
});
e.preventDefault();
}, false);
canvas.addEventListener("touchend", function (e) {
var mouseEvent = new MouseEvent("mouseup", {});
canvas.dispatchEvent(mouseEvent);
e.preventDefault();
}, false);
canvas.addEventListener("touchmove", function (e) {
mousePos = getTouchPos(canvas, e);
mouseMove({
offsetX: mousePos.x-20,
offsetY: mousePos.y-20,
});
e.preventDefault();
}, false);
$("#body")[0].addEventListener('mouseup',mouseUp);
$("#body")[0].addEventListener('mousedown',mouseDown);
context.fillStyle = "white";
context.fillRect(0, 0, canvas.width, canvas.height);
}
// Get the position of a touch relative to the canvas
function getTouchPos(canvasDom, touchEvent) {
var rect = canvasDom.getBoundingClientRect();
return {
x: touchEvent.touches[0].clientX - rect.left,
y: touchEvent.touches[0].clientY - rect.top
};
}
function mouseDown(e){
dragging=true;
context.beginPath();
obj = {};
obj.lineWidth = context.lineWidth=pencilSize.value;
obj.strokeStyle = context.strokeStyle=pencilColor.value;
points.push(obj);
}
function mouseMove(e){
if(dragging){
var x = e.offsetX || e.layerX - canvas.offsetLeft;
var y = e.offsetY || e.layerY - canvas.offsetTop;
x*=scaleX;
context.lineTo(x,y);
context.stroke();
points.push({x:x,y:y});
}
}
function mouseUp(e){
dragging=false;
socket.send(JSON.stringify(points));
points=[];
}
</script>
</head>
<body id='body' class='body wrapper' style='padding:20px;' onload='afterLoad()'>
<div class='container-fluid'>
<h2>TeamBoard</h2>
<kbd id='clientCnt'>Clients: </kbd>
<kbd>
<label for="drawColor">Pencil Color:</label>
<input type="color" id="drawColor" value="#000000" />
</kbd>
<kbd>
<label for="pencilSize">Pencil Size:</label>
</kbd>
<input type='number' id="pencilSize" min='1' max='50' value='3'/>
<div class='jumbotron center1' id='cp' style='padding:20px;width:940px;height:540px;'>
<canvas id='board' height='500px' width='900px' style='border: 1px solid blue;'></canvas>
</div>
</div>
</body>
</html>