Strange rotation of rectangle around its center using d3.js

后端 未结 1 1105
忘了有多久
忘了有多久 2021-01-27 16:22

I\'m planning to rotate a rectangle around its center using d3.js. My code is

相关标签:
1条回答
  • 2021-01-27 17:07

    As mentioned in the comment, you can clearly see the difference when you check the attributes...

    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)
    }
    div {white-space: pre;}
    svg {overflow: visible;}
    <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>

    0 讨论(0)
提交回复
热议问题