Skip to content
Snippets Groups Projects
Commit 1447ed42 authored by Giovanni Caruso's avatar Giovanni Caruso
Browse files

Implement basic erasing functionality

parent e7d0f66f
No related branches found
No related tags found
1 merge request!24Erasing functionality first prototype
Pipeline #101361 passed
...@@ -31,6 +31,8 @@ ...@@ -31,6 +31,8 @@
Connected peers: Connected peers:
<ul id="connected-peers"></ul> <ul id="connected-peers"></ul>
</div> </div>
<button id="pen">Pen</button>
<button id="eraser">Eraser</button>
<svg <svg
id="whiteboard" id="whiteboard"
......
...@@ -29,6 +29,8 @@ Y({ ...@@ -29,6 +29,8 @@ Y({
const peerIDElem = document.getElementById("peer-id") const peerIDElem = document.getElementById("peer-id")
const peerButton = document.getElementById("peer-connect") const peerButton = document.getElementById("peer-connect")
const connectedP = document.getElementById("connected-peers") const connectedP = document.getElementById("connected-peers")
const penButton = document.getElementById("pen")
const eraserButton = document.getElementById("eraser")
userIDElem.value = y.db.userId userIDElem.value = y.db.userId
...@@ -64,9 +66,29 @@ Y({ ...@@ -64,9 +66,29 @@ Y({
peerIDElem.value = "" peerIDElem.value = ""
} }
// 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
}
}
const whiteboard = document.getElementById("whiteboard") const whiteboard = document.getElementById("whiteboard")
var painting = false var input = false
var paths = new Map() var paths = new Map()
var pathID var pathID
...@@ -104,22 +126,55 @@ Y({ ...@@ -104,22 +126,55 @@ Y({
return path return path
} }
function removeOrUpdatePath(uid) {
var path = paths.get(uid)
if (path !== undefined) {
paths.delete(path)
}
}
whiteboard.onmousedown = function(e) { whiteboard.onmousedown = function(e) {
painting = true input = true
const mouse = { const mouse = {
x: e.offsetX, x: e.offsetX,
y: e.offsetY, y: e.offsetY,
} }
pathID = uuidv4() if (addingLine) {
pathID = uuidv4()
const sharedPath = y.share.drawing.set(pathID, Y.Array) const sharedPath = y.share.drawing.set(pathID, Y.Array)
sharedPath.push([[mouse.x, mouse.y]]) 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
}
}
}
} }
whiteboard.onmouseup = function() { whiteboard.onmouseup = function() {
painting = false input = false
} }
whiteboard.onmousemove = function(e) { whiteboard.onmousemove = function(e) {
...@@ -128,9 +183,46 @@ Y({ ...@@ -128,9 +183,46 @@ Y({
y: e.offsetY, y: e.offsetY,
} }
if (painting) { if (input) {
const sharedPath = y.share.drawing.get(pathID) if (addingLine) {
sharedPath.push([[mouse.x, mouse.y]]) 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
}
}
}
} }
} }
...@@ -149,6 +241,11 @@ Y({ ...@@ -149,6 +241,11 @@ Y({
} }
}) })
break
case "delete":
removeOrUpdatePath(lineID)
break break
} }
}) })
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment