Newer
Older
import { line, curveBasis } from "d3-shape"
import uuidv4 from "uuid/v4"
import yMemory from "y-memory"
import yMap from "y-map"
import yArray from "y-array"
import Y from "yjs"
import yWebrtc from "./y-webrtc/index.js"
yMemory(Y)
yMap(Y)
yArray(Y)
yWebrtc(Y)
Moritz Langenstein
committed
Moritz Langenstein
committed
name: "memory",
},
connector: {
name: "webrtc",
room: "imperial",
Moritz Langenstein
committed
drawing: "Map",
},
}).then((y) => {
const userIDElem = document.getElementById("user-id")
const peerIDElem = document.getElementById("peer-id")
const peerButton = document.getElementById("peer-connect")
const connectedP = document.getElementById("connected-peers")
const penButton = document.getElementById("pen")
const eraserButton = document.getElementById("eraser")
userIDElem.value = y.db.userId
y.connector.onUserEvent(function(event) {
switch (event.action) {
case "userID":
userIDElem.value = event.id
break
case "userJoined":
var peerElem = document.createElement("li")
peerElem.innerHTML = event.user
connectedP.appendChild(peerElem)
break
case "userLeft":
for (var peer of connectedP.children) {
if (peer.innerHTML == event.user) {
connectedP.removeChild(peer)
}
}
break
}
})
peerButton.onclick = function() {
const peerID = peerIDElem.value
if (peerID == "") {
return
}
y.connector.connectToPeer(peerID)
peerIDElem.value = ""
Moritz Langenstein
committed
// Used to check what kind of tool is selected
var addingLine = true
var removingLine = false
penButton.onclick = function() {
// If pen tool selected
if (!addingLine) {
addingLine = true
removingLine = false
}
}
eraserButton.onclick = function() {
// If eraser tool selected
if (!removingLine) {
removingLine = true
addingLine = false
}
}
Moritz Langenstein
committed
const whiteboard = document.getElementById("whiteboard")
Moritz Langenstein
committed
var paths = new Map()
var pathID
function createOrUpdatePath(uid, points) {
Moritz Langenstein
committed
const lineFn = line()
.x((d) => d[0])
.y((d) => d[1])
.curve(curveBasis)
Moritz Langenstein
committed
var path = paths.get(uid)
if (path === undefined) {
path = document.createElementNS("http://www.w3.org/2000/svg", "path")
path.setAttribute("stroke", "blue")
path.setAttribute("stroke-width", 3)
path.setAttribute("fill", "none")
path.setAttribute("pointer-events", "none")
whiteboard.appendChild(path)
paths.set(uid, path)
}
points = points.toArray().filter((point) => point !== undefined)
if (points.length <= 0) {
path.removeAttribute("d")
return path
}
Moritz Langenstein
committed
path.setAttribute("d", lineFn(points))
Moritz Langenstein
committed
Moritz Langenstein
committed
return path
}
function removeOrUpdatePath(uid) {
var path = paths.get(uid)
if (path !== undefined) {
paths.delete(path)
}
}
Moritz Langenstein
committed
whiteboard.onmousedown = function(e) {
Moritz Langenstein
committed
const mouse = {
x: e.offsetX,
y: e.offsetY,
}
if (addingLine) {
pathID = uuidv4()
const sharedPath = y.share.drawing.set(pathID, Y.Array)
} else if (removingLine) {
// Iterate over all the possible paths in the Map
const mapPaths = y.share.drawing.keys()
for (var mapPath of mapPaths) {
var found = false
// Get the array of coordinates
var mouseYArray = y.share.drawing.get(mapPath)
var mouseArray = mouseYArray.toArray()
// Check the array for current position
for (var i = 0; i < mouseArray.length; i++) {
var point = mouseArray[i]
if (checkRadius(point, mouse)) {
// Delete point
point[2] = false
mouseYArray.insert(i, point)
y.share.drawing.set(mapPath, mouseYArray)
found = true
break
}
}
if (found) {
break
}
}
}
Moritz Langenstein
committed
}
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
/* Helper function that checks whether a point is within the mouse radius */
function checkRadius(mouseMap, mouse) {
var mouseMapX = mouseMap[0]
var mouseMapY = mouseMap[1]
var mouseX = mouse.x
var mouseY = mouse.y
for (var i = 0; i < CHECKRADIUS; i++) {
// Chech x-axis
if (mouseX + i == mouseMapX) {
return true
} else if (mouseX - i == mouseMapX) {
return true
}
// Check y-axis
if (mouseY + i == mouseMapY) {
return true
} else if (mouseY - i == mouseMapY) {
return true
}
}
return false
}
Moritz Langenstein
committed
whiteboard.onmouseup = function() {
Moritz Langenstein
committed
}
whiteboard.onmousemove = function(e) {
const mouse = {
x: e.offsetX,
y: e.offsetY,
}
if (input) {
if (addingLine) {
const sharedPath = y.share.drawing.get(pathID)
} else if (removingLine) {
// Iterate over all the possible paths in the Map
const mapPaths = y.share.drawing.keys()
for (var mapPath of mapPaths) {
var found = false
// Get the array of coordinates
var mouseYArray = y.share.drawing.get(mapPath)
var mouseArray = mouseYArray.toArray()
// Check the array for current position
for (var i = 0; i < mouseArray.length; i++) {
var point = mouseArray[i]
if (checkRadius(point, mouse)) {
// Delete point
point[2] = false
mouseYArray.insert(i, point)
y.share.drawing.set(mapPath, mouseYArray)
found = true
break
}
}
if (found) {
break
}
}
}
Moritz Langenstein
committed
}
}
y.share.drawing.observe(function(lineEvent) {
const lineID = lineEvent.name
switch (lineEvent.type) {
case "add":
createOrUpdatePath(lineID, lineEvent.value)
lineEvent.value.observe(function(pointEvent) {
switch (pointEvent.type) {
case "insert":
createOrUpdatePath(lineID, pointEvent.object)
break
}
})
lineEvent.value.observe(function(pointEvent) {
switch (pointEvent.type) {
case "insert":
removeOrUpdatePath(lineID)
break
}
})
Moritz Langenstein
committed
break
}
})