From 05059648543d4333cfdeefeaf0a186439e1410b6 Mon Sep 17 00:00:00 2001 From: Juraj Kubrican Date: Tue, 18 Feb 2025 14:11:19 +0100 Subject: [PATCH] white + isolate the js --- js/draw.js | 171 ++++++++++++++++++++++++++--------------------------- 1 file changed, 83 insertions(+), 88 deletions(-) diff --git a/js/draw.js b/js/draw.js index 8fe7d3f..964498c 100644 --- a/js/draw.js +++ b/js/draw.js @@ -1,106 +1,101 @@ -const canvas = document.getElementById("drawCanvas"); -const ctx = canvas.getContext("2d"); +(() => { + const canvas = document.getElementById("drawCanvas"); + const ctx = canvas.getContext("2d"); -ctx.lineWidth = 2; -ctx.lineCap = "round"; -ctx.strokeStyle = "white"; + ctx.lineWidth = 2; + ctx.lineCap = "round"; + ctx.strokeStyle = "white"; -let drawing = false; -let lineId = 0; + let drawing = false; + let lineId = 0; -const drawSocketUrl = - (window.location.protocol.startsWith("https") ? "wss://" : "ws://") + - window.location.host + - "/draw/ws"; + const socketUrl = + (window.location.protocol.startsWith("https") ? "wss://" : "ws://") + + window.location.host + + "/draw/ws"; -let drawSocket = new WebSocket(drawSocketUrl); + let socket = new WebSocket(socketUrl); -drawSocket.onerror = (e) => { - console.log("error", e); -}; -drawSocket.onclose = (e) => { - console.log("closed", e); -}; + socket.onerror = console.error; + socket.onclose = console.warn; -setTimeout(() => { - drawSocket.send("ping"); -}, 200); + setTimeout(() => socket.send("ping"), 200); -const sendMessage = (id, x, y) => { - if (drawSocket.readyState !== drawSocket.OPEN) { - drawSocket = new WebSocket(drawSocketUrl); - } + // const queue = [] - drawSocket.send(id + "," + x + "," + y); -}; + const sendMessage = (id, x, y) => { + // queue.push(`${id},${x},${y}`) -const drawStart = () => { - ctx.beginPath(); - lineId++; - drawing = true; -}; + // console.log(queue) -const drawStop = () => { - drawing = false; - ctx.closePath(); -}; + if (socket.readyState !== socket.OPEN) { + socket = new WebSocket(socketUrl); + } -const drawMove = (e) => { - if (!drawing) return; - ctx.lineTo(e.offsetX, e.offsetY); - sendMessage(lineId, e.offsetX, e.offsetY); - ctx.stroke(); -}; - -function touchStart(event) { - console.log("start"); - drawStart(event.touches[0]); -} -function touchMove(event) { - console.log("move", event.touches[0]); - const e = { - offsetX: event.touches[0].clientX - canvas.offsetLeft, - offsetY: event.touches[0].clientY - canvas.offsetTop, + socket.send(id + "," + x + "," + y); }; - drawMove(e); - event.preventDefault(); -} -function touchEnd(event) { - console.log("end"); - drawStop(event.changedTouches[0]); -} -canvas.addEventListener("mousedown", drawStart); -canvas.addEventListener("mouseup", drawStop); -canvas.addEventListener("mousemove", drawMove); + const drawStart = () => { + ctx.beginPath(); + lineId++; + drawing = true; + }; -canvas.addEventListener("touchstart", touchStart, false); -canvas.addEventListener("touchend", touchEnd, false); -canvas.addEventListener("touchmove", touchMove, false); + const drawStop = () => { + drawing = false; + ctx.closePath(); + }; -const receiveCanvas = document.getElementById("receiveCanvas"); -const recCtx = receiveCanvas.getContext("2d"); -recCtx.lineWidth = 2; -recCtx.lineCap = "round"; + const drawMove = (e) => { + if (!drawing) return; + ctx.lineTo(e.offsetX, e.offsetY); + sendMessage(lineId, e.offsetX, e.offsetY); + ctx.stroke(); + }; -let receiveLineId = 0; + const touchStart = (e) => drawStart(e.touches[0]); + const touchMove = (e) => { + drawMove({ + offsetX: e.touches[0].clientX - canvas.offsetLeft, + offsetY: e.touches[0].clientY - canvas.offsetTop, + }); + e.preventDefault(); + }; + const touchEnd = (e) => drawStop(e.changedTouches[0]); -drawSocket.addEventListener("message", function (event) { - const parts = event.data.split(","); - if (parts[0] != receiveLineId) { - recCtx.closePath(); - recCtx.beginPath(); - receiveLineId = parts[0]; - recCtx.moveTo(parts[1], parts[2]); - } else { - recCtx.lineTo(parts[1], parts[2]); - recCtx.stroke(); - } -}); + canvas.addEventListener("mousedown", drawStart); + canvas.addEventListener("mouseup", drawStop); + canvas.addEventListener("mousemove", drawMove); -setInterval(() => { - if (drawSocket.readyState !== drawSocket.OPEN) { - drawSocket = new WebSocket(drawSocketUrl); - } - drawSocket.send("ping"); -}, 10000); + canvas.addEventListener("touchstart", touchStart); + canvas.addEventListener("touchend", touchEnd); + canvas.addEventListener("touchmove", touchMove); + + const receiveCanvas = document.getElementById("receiveCanvas"); + const recCtx = receiveCanvas.getContext("2d"); + recCtx.lineWidth = 2; + recCtx.lineCap = "round"; + recCtx.strokeStyle = "white"; + + let receiveLineId = 0; + + socket.addEventListener("message", function (event) { + const parts = event.data.split(","); + if (parts[0] != receiveLineId) { + recCtx.closePath(); + recCtx.beginPath(); + receiveLineId = parts[0]; + recCtx.moveTo(parts[1], parts[2]); + } else { + recCtx.lineTo(parts[1], parts[2]); + recCtx.stroke(); + } + }); + + setInterval(() => { + if (socket.readyState !== socket.OPEN) { + socket = new WebSocket(socketUrl); + } + socket.send("ping"); + }, 10000); +})();