Newer
Older
// Room connection and synchronisation.
// Translate local canvas input events to draw messages and send to the room.
// Get back room updates and invoke the local canvas renderer.
import * as HTML from "./elements.js"
const TEST_ROOM = "imperial"
const onRoomConnect = (room_) => {
room = room_
HTML.connectedRoomID.textContent = room.name
HTML.connectedRoomInfoContainer.style.display = "block"
HTML.userIDElem.value = room.ownID || ""
room.addEventListener("allocateOwnID", ({ detail: id }) => {
})
room.addEventListener("userJoin", ({ detail: id }) => {
if (HTML.connectedPeers.children.length == 0) {
Moritz Langenstein
committed
HTML.connectedPeers.innerHTML = ""
}
const peerElem = document.createElement("li")
peerElem.innerHTML = id
HTML.connectedPeers.appendChild(peerElem)
})
room.addEventListener("userLeave", ({ detail: id }) => {
for (const peerElem of HTML.connectedPeers.children) {
if (peerElem.innerHTML == id) {
HTML.connectedPeers.removeChild(peerElem)
Moritz Langenstein
committed
if (HTML.connectedPeers.children.length == 0) {
Moritz Langenstein
committed
HTML.connectedPeers.innerHTML = "No peers are connected"
}
})
room.addEventListener("addOrUpdatePath", ({ detail: { id, points } }) => {
const tryRoomConnect = async (roomID) => {
return await connect(roomID)
.then(onRoomConnect)
.catch((err) => alert(`Error connecting to a room:\n${err}`))
}
Moritz Langenstein
committed
const ERASER_RADIUS = 10
const tools = {
PEN: Symbol("pen"),
ERASER: Symbol("eraser"),
}
let currentTool = tools.PEN
const pathIDsByPointerID = new Map()
Moritz Langenstein
committed
HTML.penButton.addEventListener("click", () => {
if (currentTool == tools.PEN) {
} else {
currentTool = tools.PEN
HTML.penButton.classList.add("selected")
HTML.eraserButton.classList.remove("selected")
}
})
})
window.addEventListener("click", (event) => {
if (event.target == HTML.penProperties) {
hideElement(HTML.penProperties)
hideElement(HTML.palette)
hideElement(HTML.penProperties)
Moritz Langenstein
committed
var svg = HTML.wheel.children
for (var i = 1; i < svg.length; i++) {
svg[i].addEventListener("click", (event) => {
var paletteColour = event.target.getAttribute("fill")
HTML.rectangle.style.backgroundColor = paletteColour
canvas.setStrokeColour(paletteColour)
function showElement(element) {
element.style.display = "block"
}
function hideElement(element) {
element.style.display = "none"
}
HTML.picker.addEventListener("change", () => {
var paletteColour = event.target.value
HTML.rectangle.style.backgroundColor = paletteColour
canvas.setStrokeColour(paletteColour)
})
HTML.eraserButton.addEventListener("click", () => {
currentTool = tools.ERASER
HTML.penButton.classList.remove("selected")
HTML.eraserButton.classList.add("selected")
})
Moritz Langenstein
committed
HTML.peerButton.addEventListener("click", () => {
const peerID = HTML.peerIDElem.value
if (room == null || peerID == "") {
return
room.inviteUser(peerID)
HTML.peerIDElem.value = ""
})
Moritz Langenstein
committed
HTML.roomConnectButton.addEventListener("click", () => {
const selectedRoomID = HTML.roomIDElem.value
if (!selectedRoomID || selectedRoomID == room.name) {
return
if (room != null) {
room.disconnect()
room = null
canvas.clear()
HTML.connectedPeers.innerHTML = "No peers are connected"
const getDistance = (a, b) => {
return Math.sqrt(
(a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]),
)
}
const erasePoint = ([x, y]) => {
if (room == null) {
return
room.getPaths().forEach((points, pathID) => {
points.forEach((point, i) => {
if (getDistance([x, y], point) <= ERASER_RADIUS) {
room.erasePoint(pathID, i)
}
})
})
}
canvas.input.addEventListener("strokestart", ({ detail: e }) => {
if (room == null) {
return
}
pathIDsByPointerID.set(
e.pointerId,
room.addPath([...mousePos, e.pressure, canvas.stroke_colour]),
)
} else if (currentTool == tools.ERASER) {
erasePoint(mousePos)
canvas.input.addEventListener("strokeend", ({ detail: e }) => {
pathIDsByPointerID.delete(e.pointerId)
})
canvas.input.addEventListener("strokemove", ({ detail: e }) => {
if (room == null) {
return
}
room.extendPath(pathIDsByPointerID.get(e.pointerId), [
...mousePos,
e.pressure,
} else if (currentTool == tools.ERASER) {
erasePoint(mousePos)
}