admin管理员组文章数量:1023102
I've created a line in d3, which I want to drag around.
var line = d3.select("svg")
.append("line")
.attr("x1",10)
.attr("y1",10)
.attr("x2",200)
.attr("y2",200)
.attr("stroke-width",10)
.attr("stroke","black")
.call(drag);
The problem I have right now, is how to move both Points (x1,y1) (x2,y2) from the line relative to my mouse position, as I probably will need the dragstart mouse position as well.
let drag = d3.behavior.drag()
.on('dragstart', null)
.on('drag', function(){
// move circle
let x1New = d3.select(this).attr('x1')+ ???;
let y1New = d3.select(this).attr('y1')+ ???;
let x2New = d3.select(this).attr('x2')+ ???;
let y2New = d3.select(this).attr('y2')+ ???;
line.attr("x1",x1New)
.attr("y1",y1New)
.attr("x2",x2New)
.attr("y2",y2New);
})
.on('dragend', function(){
});
I hope you can help me with that.
I've created a line in d3, which I want to drag around.
var line = d3.select("svg")
.append("line")
.attr("x1",10)
.attr("y1",10)
.attr("x2",200)
.attr("y2",200)
.attr("stroke-width",10)
.attr("stroke","black")
.call(drag);
The problem I have right now, is how to move both Points (x1,y1) (x2,y2) from the line relative to my mouse position, as I probably will need the dragstart mouse position as well.
let drag = d3.behavior.drag()
.on('dragstart', null)
.on('drag', function(){
// move circle
let x1New = d3.select(this).attr('x1')+ ???;
let y1New = d3.select(this).attr('y1')+ ???;
let x2New = d3.select(this).attr('x2')+ ???;
let y2New = d3.select(this).attr('y2')+ ???;
line.attr("x1",x1New)
.attr("y1",y1New)
.attr("x2",x2New)
.attr("y2",y2New);
})
.on('dragend', function(){
});
I hope you can help me with that.
Share Improve this question asked May 14, 2016 at 9:18 Anh Tuan NguyenAnh Tuan Nguyen 9793 gold badges13 silver badges28 bronze badges1 Answer
Reset to default 5Do it like this inside your drag function:
.on('drag', function(d){
// move circle
var dx = d3.event.dx;//this will give the delta x moved by drag
var dy = d3.event.dy;//this will give the delta y moved by drag
var x1New = parseFloat(d3.select(this).attr('x1'))+ dx;
var y1New = parseFloat(d3.select(this).attr('y1'))+ dy;
var x2New = parseFloat(d3.select(this).attr('x2'))+ dx;
var y2New = parseFloat(d3.select(this).attr('y2'))+ dy;
line.attr("x1",x1New)
.attr("y1",y1New)
.attr("x2",x2New)
.attr("y2",y2New);
}).on('dragend', function(){
});
working code here
I've created a line in d3, which I want to drag around.
var line = d3.select("svg")
.append("line")
.attr("x1",10)
.attr("y1",10)
.attr("x2",200)
.attr("y2",200)
.attr("stroke-width",10)
.attr("stroke","black")
.call(drag);
The problem I have right now, is how to move both Points (x1,y1) (x2,y2) from the line relative to my mouse position, as I probably will need the dragstart mouse position as well.
let drag = d3.behavior.drag()
.on('dragstart', null)
.on('drag', function(){
// move circle
let x1New = d3.select(this).attr('x1')+ ???;
let y1New = d3.select(this).attr('y1')+ ???;
let x2New = d3.select(this).attr('x2')+ ???;
let y2New = d3.select(this).attr('y2')+ ???;
line.attr("x1",x1New)
.attr("y1",y1New)
.attr("x2",x2New)
.attr("y2",y2New);
})
.on('dragend', function(){
});
I hope you can help me with that.
I've created a line in d3, which I want to drag around.
var line = d3.select("svg")
.append("line")
.attr("x1",10)
.attr("y1",10)
.attr("x2",200)
.attr("y2",200)
.attr("stroke-width",10)
.attr("stroke","black")
.call(drag);
The problem I have right now, is how to move both Points (x1,y1) (x2,y2) from the line relative to my mouse position, as I probably will need the dragstart mouse position as well.
let drag = d3.behavior.drag()
.on('dragstart', null)
.on('drag', function(){
// move circle
let x1New = d3.select(this).attr('x1')+ ???;
let y1New = d3.select(this).attr('y1')+ ???;
let x2New = d3.select(this).attr('x2')+ ???;
let y2New = d3.select(this).attr('y2')+ ???;
line.attr("x1",x1New)
.attr("y1",y1New)
.attr("x2",x2New)
.attr("y2",y2New);
})
.on('dragend', function(){
});
I hope you can help me with that.
Share Improve this question asked May 14, 2016 at 9:18 Anh Tuan NguyenAnh Tuan Nguyen 9793 gold badges13 silver badges28 bronze badges1 Answer
Reset to default 5Do it like this inside your drag function:
.on('drag', function(d){
// move circle
var dx = d3.event.dx;//this will give the delta x moved by drag
var dy = d3.event.dy;//this will give the delta y moved by drag
var x1New = parseFloat(d3.select(this).attr('x1'))+ dx;
var y1New = parseFloat(d3.select(this).attr('y1'))+ dy;
var x2New = parseFloat(d3.select(this).attr('x2'))+ dx;
var y2New = parseFloat(d3.select(this).attr('y2'))+ dy;
line.attr("x1",x1New)
.attr("y1",y1New)
.attr("x2",x2New)
.attr("y2",y2New);
}).on('dragend', function(){
});
working code here
本文标签: javascriptDrag a Line in d3Stack Overflow
版权声明:本文标题:javascript - Drag a Line in d3 - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745539410a2155112.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论