[英]Transition of triangle svg path in D3.js
I would like to create 30 equilateral triangles of different dimensions and colors, in a random position inside the body, that are moving away from the current mouse position, stopping at a short distance from the border of the body. 我想在体内任意位置创建30个不同尺寸和颜色的等边三角形,这些三角形正从当前鼠标位置移开,停在距身体边界不远的位置。
Here is the code: 这是代码:
var body = d3.select("body");
var mouse = [];
var width = 1000;
var height = 600;
var numberOfTriangles = 30;
var isMouseMoving = false;
var triangle = d3.symbol()
.type(d3.symbolTriangle);
function drawTriangles(number) {
for(var i=0;i<number;i++){
var dim = Math.random()*400;
svg.append("path")
.attr("d", triangle.size(dim))
.attr("transform", function(d) { return "translate(" + Math.random()*width + "," + Math.random()*height + ")"; })
.attr("fill", "rgb("+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+")")
.attr("opacity", 2)
.attr("class", "path"+i);
}
}
function moveMouse(number) {
if (isMouseMoving) {
console.log("posix: x="+mouse[0]+",y="+mouse[1]+"\n");
for(var i=0;i<number;i++){
svg.select('.path'+i).transition().duration(5)
.attr({'x':-mouse[0],'y':-mouse[1]})
}
}
}
var svg = body.append("svg")
.attr("width", width)
.attr("height", height)
.style("border", "1px solid black")
.on("mousemove", function() {
mouse = d3.mouse(this);
isMouseMoving=true;
});
drawTriangles(numberOfTriangles);
d3.timer(function(){moveMouse(numberOfTriangles)});
I have few questions: 我有几个问题:
1) How to position the triangles inside the body? 1)如何在体内放置三角形? In this way that I have done, a little part of some triangles is outside the body.
通过这种方式,一些三角形的一小部分位于身体外部。 Math.random()*weight and Math.random()*height are not enough?
Math.random()*重量和Math.random()*高度还不够吗?
2) I saw some examples that implement the transition like I have done, but in my case doesn't work. 2)我看到了一些像我一样实现过渡的示例,但对我而言不起作用。 How can I implement the transition of the triangles so that they move away from the current position of the mouse and they stop at a short distance from the border of the body?
如何实现三角形的过渡,以使它们远离鼠标的当前位置,并在距身体边界不远的地方停下来?
Thank you in advice. 谢谢你的指教。
1) It depends on how the triangles are drawn. 1)取决于三角形的绘制方式。
If d3.symbolTriangle
draws lineTo into negative x territory (i think it does), then you have the potential that the left half of your triangle will be cut off by the bounds of your body. 如果
d3.symbolTriangle
将lineTo绘制到负x区域(我认为是),则您的三角形的左半部分可能会被身体的边界截断。
Try adding 1/2 of the triangle's width when you translate it on the x direction. 在x方向上平移三角形时,尝试增加其宽度的1/2。
2) At the moment, your code is attempting to move the triangles to the position of the mouse, not away from it. 2)目前,您的代码正在尝试将三角形移动到鼠标的位置,而不是远离它。 Here is that working:
这是工作原理:
var body = d3.select("body");
var mouse = [];
var width = 1000;
var height = 600;
var numberOfTriangles = 30;
var isMouseMoving = false;
var triangle = d3.symbol()
.type(d3.symbolTriangle);
function drawTriangles(number) {
for (var i = 0; i < number; i++) {
var dim = Math.random() * 400;
svg.append("path")
.attr("d", triangle.size(dim))
.attr("transform", function(d) {
return "translate(" + Math.random() * width + "," + Math.random() * height + ")";
})
.attr("fill", "rgb(" + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + "," + parseInt(Math.random() * 255) + ")")
.attr("opacity", 2)
.attr("class", "path" + i);
}
}
function moveMouse() {
if (isMouseMoving) {
//console.log("posix: x="+mouse[0]+",y="+mouse[1]+"\n");
svg.selectAll('path').each(function(d, i) {
var self = d3.select(this);
self.attr('transform', function() {
return "translate(" + mouse[0] + "," + mouse[1] + ")";
})
})
}
}
var svg = body.append("svg")
.attr("width", width)
.attr("height", height)
.style("border", "1px solid black")
.on("mousemove", function() {
mouse = d3.mouse(this);
isMouseMoving = true;
});
drawTriangles(numberOfTriangles);
d3.timer(function() {
moveMouse()
});
Fiddle: https://jsfiddle.net/hLcvdpjk/ 小提琴: https : //jsfiddle.net/hLcvdpjk/
D3 triangle: https://github.com/d3/d3-shape/blob/master/src/symbol/triangle.js D3三角形: https : //github.com/d3/d3-shape/blob/master/src/symbol/triangle.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.