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
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,
}
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
if (addingLine) {
pathID = uuidv4()
const sharedPath = y.share.drawing.set(pathID, Y.Array)
sharedPath.push([[mouse.x, mouse.y]])
} 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 mouseArray = y.share.drawing.get(mapPath).toArray()
// Check the array for current position
for (var i = 0; i < mouseArray.length; i++) {
var mouseMap = mouseArray[i]
if (mouseMap[0] == mouse.x && mouseMap[1] == mouse.y) {
// Delete coordinates from the array
mouseArray.delete(i)
// Update map
y.share.drawing.set(mapPath, mouseArray)
found = true
break
}
}
if (found) {
break
}
}
}
Moritz Langenstein
committed
}
whiteboard.onmouseup = function() {
Moritz Langenstein
committed
}
whiteboard.onmousemove = function(e) {
const mouse = {
x: e.offsetX,
y: e.offsetY,
}
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
if (input) {
if (addingLine) {
const sharedPath = y.share.drawing.get(pathID)
sharedPath.push([[mouse.x, mouse.y]])
} 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 mouseArray = y.share.drawing.get(mapPath).toArray()
// Check the array for current position
for (var i = 0; i < mouseArray.length; i++) {
var mouseMap = mouseArray[i]
if (mouseMap[0] == mouse.x && mouseMap[1] == mouse.y) {
console.log(
"mouseMap[0] (" +
mouseMap[0] +
") == mouse.x (" +
mouse.x +
"mouseMap[1] (" +
mouseMap[1] +
") == mouse.y (" +
mouse.y +
"y",
)
// Delete coordinates from the array
mouseArray.delete(i)
// Update map
y.share.drawing.set(mapPath, mouseArray)
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
}
})
break
case "delete":
removeOrUpdatePath(lineID)
Moritz Langenstein
committed
break
}
})