使用d3.js在矩形周围奇怪地旋转

时间:2015-09-05 07:19:47

标签: d3.js

我计划使用d3.js围绕其中心旋转一个矩形。 我的代码是



var svg = d3.select("body")
  .append("svg").attr("width", 200).attr("height", 200).append("rect")
  .attr("x", 50)
  .attr("y", 50)
  .attr("fill", "black")
  .attr("width", 100)
  .attr("height", 100)
  .transition()
  .ease("linear")
  .duration(1000)
  .attr("transform", "rotate(180,100,100)");

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
&#13;
&#13;
&#13;

它旋转并到达其原始的x,y坐标矩形,但是 在旋转过程中不会围绕其中心旋转。

我在网上找到的正确代码是

&#13;
&#13;
var svg = d3.select("body").append("svg").attr("width", 200).attr("height", 200);

var rect = svg.append("rect")
    .attr("x", 50)
    .attr("y", 50)
    .attr("width", 100)
    .attr("height", 100);

rect.transition().duration(5000)
    .attrTween("transform", rotTween);

function rotTween() {
    var i = d3.interpolate(0, 360);
    return function(t) {
        return "rotate(" + i(t) + ",100,100)";
    };
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
&#13;
&#13;
&#13;

此矩形正确地围绕其中心旋转。但我看不出两个代码之间有什么区别。有什么区别?

1 个答案:

答案 0 :(得分:0)

正如评论中所提到的,当您检查属性时,您可以清楚地看到差异......

&#13;
&#13;
var log = d3.ui.log("#log").style("vertical-align", "top"),
	logTween = d3.ui.log("#logTween").style("vertical-align", "top"),
    stoppedTween = false,
    stopped = false;
log.writeLine("translate\t\trotate")
logTween.writeLine("translate\t\trotate")

var rect = d3.select("body")
    .insert("svg", "#log").attr("width", 200).attr("height", 100).append("rect")
    .attr("x", 20)
    .attr("y", 20)
    .attr("fill", "black")
    .attr("width", 40)
    .attr("height", 40);

rect.transition()
    .ease("linear")
    .duration(1000)
    .attr("transform", "rotate(180,100,100)")
    .each("start", function () {
    d3.timer(function () {
        var t = d3.transform(rect.attr("transform"));
        log.writeLine([t.translate.map(f).join(","), f(t.rotate)].join("\t"))
        return stopped;
    })
})
    .each("end", function () {
    stopped = true
});
var svg = d3.select("body").insert("svg", "#logTween").attr("width", 100).attr("height", 100);

var rectTween = svg.append("rect")
    .attr("x", 20)
    .attr("y", 20)
    .attr("width", 40)
    .attr("height", 40);


rectTween.transition().duration(1000)
    .ease("linear")
    .attrTween("transform", rotTween)
    .each("start", function () {
    d3.timer(function () {
        var t = d3.transform(rectTween.attr("transform"));
        logTween.writeLine([t.translate.map(f).join(","), f(t.rotate)].join("\t"))
        return stoppedTween;
    })
})
    .each("end", function () {
    stoppedTween = true
});


function rotTween() {
    var i = d3.interpolate(0, 360);
    return function(t) {
        return "rotate(" + i(t) + ",40,40)";
    };
}

function f(x) {
    return d3.format(" >6.1f")(x)
}
&#13;
div {white-space: pre;}
svg {overflow: visible;}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="UTF-8"></script>
<script src="https://rawgit.com/cool-Blue/d3-lib/master/Output/log/log.js"></script>
<div id="log"></div>
<div id="logTween"></div>
&#13;
&#13;
&#13;