-
Notifications
You must be signed in to change notification settings - Fork 4
/
room.html
261 lines (227 loc) · 8.08 KB
/
room.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<html>
<head>
<meta charset="UTF-8"/>
<link rel="shortcut icon" href="icon.png">
<title>PDoodle</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body onload="init()" style="background-color: #540B67;">
<canvas id="can" width="1200" height="600" style="border:2px solid;margin-left:150px; background-color: white;"></canvas>
<aside style="position: fixed;
top: 0;
left: 0;
width: 10vw;
height: 100%;
background-color:#222222;">
<img src="icon.png" class="brandicon">
<div class="clrBtn" style="background:green;" id="green" onclick="color(this)"></div>
<div class="clrBtn" style="background:blue;" id="blue" onclick="color(this)"></div>
<div class="clrBtn" style="background:red;" id="red" onclick="color(this)"></div>
<div class="clrBtn" style="background:yellow;" id="yellow" onclick="color(this)"></div>
<div class="clrBtn" style="background:black;" id="black" onclick="color(this)"></div>
<div class="clrBtn" style="background:white;" id="white" onclick="color(this)"></div>
<div class="brushSize">
<i class="material-icons icon">brush</i>
<input type="number" name="brushSize" value="2" class="brushSizeBox" onchange="changeSize();">
</div>
<div class="dl">
<a id="download" download="image.png"><button type="button" onClick="download()" class="newbtn"><i class="material-icons" style="font-size: 30px;">cloud_download</i></button></a>
</div>
</aside>
<img id="canvasimg" style="position:absolute;top:10%;left:52%;">
<!-- <input type="button" value="save" id="btn" size="30" onclick="save()" style="position:absolute;top:55%;left:10%;"> -->
<!-- <input type="button" value="clear" id="clr" size="23" onclick="erase()" style="position:absolute;top:55%;left:15%;"> -->
</body>
<script type="text/javascript">
var roomid;
var canvas, ctx, flag = false,
prevX = 0,
currX = 0,
prevY = 0,
currY = 0,
dot_flag = false;
var x = "black",
y = 2;
// if(typeof(Worker) !== "undefined") {
// if(typeof(w) == "undefined") {
var w ;
// }
//canvas.width = 200;
//canvas.height = 300;
canvas.style.zIndex = 8;
canvas.style.position = "absolute";
//canvas.style.border = "1px solid";
//var body = document.getElementsByTagName("body")[0];
//body.appendChild(canvas);
// ctx2 = canvas.getContext('2d');
// myImage = new Image();
// WAIT TILL IMAGE IS LOADED.
// function fill_canvas(img) {
// // CREATE CANVAS CONTEXT.
// //console.log("1");
// //var canvas = document.getElementById('can');
// //var ctx = canvas.getContext('2d');
// // canvas.width = 1200;
// // canvas.height = 600;
// ctx.drawImage(img, 0, 0);
// }
var rendering = true;
function init() {
var url = document.location.href,
params = url.split('?')[1].split('&'),
data = {}, tmp;
for (var i = 0, l = params.length; i < l; i++) {
tmp = params[i].split('=');
data[tmp[0]] = tmp[1];
}
console.log(data);
roomid = data.id;
w = new Worker("receiveDataWorker.js");
w.postMessage(roomid);
console.log(roomid);
w.onmessage = function(event) {
updateCanvas(event.data);
}
//var img = new Image();
//img.src = 'https://www.encodedna.com/images/hawa-mahal.jpg';
//console.log(img.src);
//console.log("starting");
canvas = document.getElementById('can');
ctx = canvas.getContext("2d");
/*img.onload = function () {
console.log("1.5");
fill_canvas(img);
console.log("2"); // FILL THE CANVAS WITH THE IMAGE.
}*/
canvas.addEventListener("mousemove", function (e) {
findxy('move', e)
}, false);
canvas.addEventListener("mousedown", function (e) {
findxy('down', e)
}, false);
canvas.addEventListener("mouseup", function (e) {
findxy('up', e)
}, false);
canvas.addEventListener("mouseout", function (e) {
findxy('out', e)
}, false);
uploadCanvas();
}
function color(obj) {
switch (obj.id) {
case "green":
x = "green";
break;
case "blue":
x = "blue";
break;
case "red":
x = "red";
break;
case "yellow":
x = "yellow";
break;
case "black":
x = "black";
break;
case "white":
x = "white";
break;
}
// if (x == "white") y = 14;
// else
y = 2;
changeSize();
}
function changeSize()
{
var brush = document.getElementsByClassName('brushSizeBox')[0];
y = brush.value;
}
function draw() {
ctx.beginPath();
ctx.moveTo(prevX, prevY);
ctx.lineTo(currX, currY);
ctx.strokeStyle = x;
ctx.lineWidth = y;
ctx.stroke();
ctx.closePath();
}
// function erase() {
// var m = confirm("Want to clear");
// if (m) {
// ctx.clearRect(0, 0, w, h);
// document.getElementById("canvasimg").style.display = "none";
// }
// }
// function save() {
// document.getElementById("canvasimg").style.border = "2px solid";
// var dataURL = canvas.toDataURL();
// document.getElementById("canvasimg").src = dataURL;
// document.getElementById("canvasimg").style.display = "inline";
// }
function findxy(res, e) {
if (res == 'down') {
prevX = currX;
prevY = currY;
currX = e.clientX - canvas.offsetLeft;
currY = e.clientY - canvas.offsetTop;
flag = true;
dot_flag = true;
if (dot_flag) {
ctx.beginPath();
ctx.fillStyle = x;
ctx.fillRect(currX, currY, 2, 2);
ctx.closePath();
dot_flag = false;
}
}
if (res == 'up' || res == "out") {
flag = false;
uploadCanvas();
}
if (res == 'move') {
if (flag) {
prevX = currX;
prevY = currY;
currX = e.clientX - canvas.offsetLeft;
currY = e.clientY - canvas.offsetTop;
draw();
}
}
}
function uploadCanvas(){
var worker = new Worker("sendDataWorker.js");
worker.postMessage(JSON.stringify({"id":roomid,"image":canvas.toDataURL().replace("\n", "|")}));
worker.onmessage = function(event){
}
}
function updateCanvas(x){
var myImage = new Image();
myImage.src = x; //imgData is base 64 string
myImage.onload = function(){
ctx.drawImage(myImage, 0, 0,myImage.width,myImage.height,0, 0,canvas.width,canvas.height);
};
}
function download(){
var download = document.getElementById("download");
var image = document.getElementById("canv").toDataURL("image/png")
.replace("image/png", "image/octet-stream");
download.setAttribute("href", image);
}
/*window.onload = function () {
var url = document.location.href,
params = url.split('?')[1].split('&'),
data = {}, tmp;
for (var i = 0, l = params.length; i < l; i++) {
tmp = params[i].split('=');
data[tmp[0]] = tmp[1];
}
console.log(data);
roomid = data.id;
w.postMessage(roomid);
console.log(roomid);
}*/
</script>
</html>