-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
129 lines (115 loc) · 3.33 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
<!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" />
<meta name="description" content="Multiplayer Drawing Board" />
<style>
* {
color-scheme: dark;
font-family: monospace;
margin: 0;
text-decoration: none;
color: white;
}
body {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
canvas {
border-radius: 1em;
border: 0.2em dotted grey;
background-color: white;
touch-action: none;
cursor: crosshair;
}
div {
margin-top: 0.3em;
margin-bottom: 0.5em;
display: flex;
gap: 1em;
width: 100%;
justify-content: center;
}
div > * {
all: unset;
margin: 0.1em;
padding: 0.3em;
background: white;
color: black;
border-radius: 1em;
text-align: center;
min-width: 3em;
}
@media (max-width: 500px) {
canvas {
width: 90%;
}
}
</style>
<title>Draw Network</title>
</head>
<body>
<h1><a href="https://github.com/SX-9/draw-network">Draw Network</a></h1>
<div>
<input type="color" title="Color" />
<button id="clear">Clear</button>
<button id="save">Download</button>
</div>
<canvas width="500" height="500"></canvas>
<script src="/socket.io/socket.io.js"></script>
<script>
const color = document.querySelector("input[type=color]");
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
const socket = io("/");
document.querySelector("button#clear").onclick = () => ctx.clearRect(0, 0, canvas.width, canvas.height);;
document.querySelector("button#save").onclick = () => window.location.href = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
let isDrag = 0;
document.onmousedown = () => isDrag++;
document.onmouseup = () => isDrag--;
document.ontouchstart = () => isDrag++;
document.ontouchend = () => isDrag--;
socket.on("draw", draw);
canvas.addEventListener(
"mousemove",
(e) => {
if (!isDrag) return;
let cords = pos(canvas, e);
socket.emit("draw", { cords, color: color.value });
draw({ cords, color: color.value });
},
false
);
canvas.addEventListener(
"touchmove",
(e) => {
if (!isDrag) return;
Array.from(e.touches).forEach((t) => {
let cords = pos(canvas, t);
socket.emit("draw", { cords, color: color.value });
draw({ cords, color: color.value });
});
},
false
);
function pos(el, evt) {
let rect = el.getBoundingClientRect();
return {
x: Math.round(evt.clientX - rect.left),
y: Math.round(evt.clientY - rect.top),
};
}
function draw(e) {
let { color: col, cords } = e;
ctx.fillStyle = col;
ctx.fillRect(cords.x, cords.y, 5, 5);
}
</script>
</body>
</html>