<!DOCTYPE html>
<html>
<head>
</head>
<meta charset="utf-8">
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 10).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > (width-7.5)) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", 10).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
}
}
});
}
let longText = "Now is the time for all good men to come to the aid of their country and asds and stuff an such jdsfj kljasdkflj kdsfj sdfkj klsjd kljdsf lk sdfj lkdfs jlk sd a";
var coordX
var coordY
let svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500)
let g = svg.append("g")
\t .on("mouseover", function(d) {
\t \t d3.select(this)
\t \t \t .style("cursor","pointer");
\t \t d3.select(this).select("rect")
\t \t \t .style("fill", "#325d81");
\t })
\t .on("mouseout", function(d){
\t \t d3.select(this)
\t \t \t .style("cursor","default");
\t \t d3.select(this).select("rect")
\t \t \t .style("fill","#4682b4");
\t })
.on('mousedown', function() {
coordX= d3.mouse(this)[0];
coordY= d3.mouse(this)[1];
})
\t .call(d3.drag()
\t .on("drag", dragged))
let rect2 = g.append("rect")
.attr("x",5)
\t .attr("y",5)
\t .attr("rx",20)
\t .attr("ry",20)
\t .attr("width",300)
\t .attr("height",150)
\t .style("fill","#4682b4")
\t .style("stroke","black")
\t .style("stroke-width",5)
\t .style("opacity",0.5)
\t ;
\t var txt = g.append('text')
.text(longText) //<-- our super long text
.attr('x', 0)
.attr('y', 30)
.attr('dy', '.71em')
.style('fill', 'white')
.call(wrap, 300); //<-- wrap it according to our width
var height = txt.node().getBBox().height + 40; //<-- get our height plus a margin
rect2.attr('height', height); //<-- change our rect
function dragged() {
var transX=d3.event.x-coordX
var transY=d3.event.y-coordY
d3.select(this).attr("transform","translate("+transX+" "+transY+")")
}
</script>
</body>
</html>
einen Blick auf d3.behavior.drag nehmen() –
@FrancisHemsher Ich habe so etwas wie versucht zu tun: var drag = d3.behavior.drag () .on ("drag", dragmove); Funktion Ziehen (d) { \t var x = d3.event.x; \t var y = d3.event.y; \t d3.select (this) .attr ("transformieren", "übersetzen (" + x + "" + y + ")"); \t} aber es war – cobolstinks
Fehler Ich sehe, Sie verwenden D3 v4. Daher siehe d3.drag(), was veraltet d3.behavior.drag() –