I am creating a floor map using d3.js and Vue js. I want to add a feature where the user clicks a button which creates a rectangle (a doorway) that the user can change the scale and X,Y positions. I am quite new to vue js and d3.js so I am using this as a reference. The button works and adds X amount of rectangles for X amount of presses. However, when I add the first doorway and try to drag the rectangle it is completely off from the user's mouse coordinates. Further, when I add the second, third, fourth, etc rectangle I can't change the position of those, only the first rectangle. I think this is because I'm only selecting one rectangle in the dragging function. However Im more concerned about getting the dragging right on the rectangles. Would someone be able to point me in the right direction please?
<template>
<div id = "app">
<header>
<Navbar />
</header>
<div class = "floor">
<div id = "tooltip"></div>
<svg width = "850" height = "800">
</svg>
</div>
<div class = "buttons">
<button v-on:click="addRec()"> Add Doorway </button>
</div>
</div>
</template>
addRec(){
console.log("Doorway Added")
var doorGroup = d3.selectAll("svg")
.append("g")
//setting attributes for doorway
var doorway = doorGroup.append("rect")
.attr("width",100)
.attr("height", 25)
.attr("x", 150)
.attr("y", 200)
//setting style attributes for doorway
.attr("fill","#F5F5F5")
.attr("stroke", "black")
.attr("stroke-width", 2.5)
.attr("cursor", "move")
//binding drag method onto rectangle
.call(d3.drag()
.on('start', dragStart)
.on('drag', dragging)
.on('end', dragEnd)
)
},
//this is to be called once used has clicked on the doorway
dragStart(d,i,nodes){
d3.select(nodes[i])
.style("stroke", "red")
},
/*
this is to be called once used has started dragging the doorway
dragging(d) upadtes the position of the doorway, from the mouse X,Y Coors
*/
dragging(d,i,nodes){
var width = 850
var height = 800
var xCoor = d3.pointer(event)[0]
var yCoor = d3.pointer(event)[1]
// d3.select(nodes[i])
// .attr("transform", "translate(" + (xCoor - 200) + "," + (yCoor - 300) + ")")
d3.select(nodes[i])
.attr("x", xCoor)
.attr("y", yCoor)
},
//this is to be called once user has clicked off the doorway
dragEnd(d,i,nodes){
d3.select(nodes[i])
.style("stroke", "black")
},