【问题标题】:Rotating an SVG image using D3.JS not working使用 D3.JS 旋转 SVG 图像不起作用
【发布时间】:2020-11-30 00:48:55
【问题描述】:

我一直在尝试使用 D3.js 重复顺时针旋转 svg 图像中的一些齿轮,但我似乎无法理解代码或图像有什么问题。我能够使用下面的代码平移和旋转每个齿轮...

d3.selectAll(".st6")
  .transition()
  .delay(function(d, i, n) { return i * 50; })
  .on("start", function repeat() {
      d3.active(this)
        .transition()
        .duration(2500)
        .attr('transform', 'rotate(0)')
        .transition() //And rotate back again
        .duration(2500)
        .attr('transform' , 'rotate(90) ')
        .on("start", repeat); //at end, call it again to create infinite loop
  });

但是当我尝试使用为我重复旋转文本的相同代码时,图像变得静止并且不再移动......

这是为我重复旋转文本的代码...

var width = 600;
var height = 300;
    
var holder = d3.select("body")
.append("svg")
.attr("width", width)    
.attr("height", height); 
    
//draw the text
holder.append("text")
  .style("fill", "black")
  .style("font-size", "56px")
  .attr("dy", ".35em")
  .attr("text-anchor", "middle")
  .attr("transform", "translate(300,150) rotate(0)")
  .text("Hi, how r u doing");
    
var i = 0;
var timeInterval = 10;
setInterval(function(){
    i += 1;
    update(i % 360) 
}, timeInterval);
    
var n;
// update the element
function update(n) {
    // rotate the text
    holder.select("text")
      .attr("transform", "translate(300,150) rotate("+n+")");
}

这是我尝试复制上述方法但无济于事...

var width = 600;
var height = 300;
    
var holder = d3.select("body")
  .append("svg")
  .attr("width", width)    
  .attr("height", height); 
    
holder.append("svg:image")
  .attr("image-anchor", "middle")
  .attr("xlink:href", "produc.svg")
  .attr("transform", "translate(300,150) rotate(0)")
// Initial starting angle of the text
    
var i = 0;
var timeInterval = 10;
setInterval(function(){
    i += 1;
    update(i % 360) 
},timeInterval);
    
    
var n;
// update the element
function update(n) {
    // rotate the text
    holder.select("text")
      .attr("transform", "translate(300,150) rotate("+n+")");
}

这是 CodePenclick here 上的一个工作示例

【问题讨论】:

  • 在你的更新函数中你还没有更新这一行holder.select("text") - 你想旋转一个文本元素还是一个图像?
  • 我正在尝试旋转图像,但我不确定放在那里的正确内容是什么。
  • 请将您的代码转成可运行的minimal reproducible example,以便我们调试并为您提供更好的答案。您可以使用可运行堆栈 sn-ps,或外部 jsFiddle/Codepen。
  • 好的。我马上就做。
  • 请检查codepen链接的问题

标签: d3.js svg-animate


【解决方案1】:

至于你的代码的问题(不考虑是不是“一锤定音”):

您正在使用旋转文本的工作代码,而不是完全更新它以反映您现在正在处理图像。您已更新 append 语句以附加图像,但尚未更新 update 函数以选择该图像 - 它仍在寻找文本元素:

function update(n) {
    // rotate the text
    holder.select("text")
      .attr("transform", "translate(300,150) rotate("+n+")");
}

由于不再有文本元素,此函数不会选择任何内容,因此不会设置任何元素的变换。如评论中所述,您需要选择图像:

function update(n) {
    // rotate the text
    holder.select("image")
      .attr("transform", "translate(300,150) rotate("+n+")");
}

如下图:

var width = 600;
var height = 300;
    
var holder = d3.select("body")
  .append("svg")
  .attr("width", width)    
  .attr("height", height); 
    
holder.append("svg:image")
  .attr("image-anchor", "middle")
  .attr("xlink:href", "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/compass.svg")
  .attr("transform", "translate(200,20) rotate(0)")
  .attr("width", 100)
// Initial starting angle of the text
    
var i = 0;
var timeInterval = 10;
setInterval(function(){
    i += 1;
    update(i % 360) 
},timeInterval);
    
// update the element
function update(n) {
    // rotate the text
    holder.select("image")
      .attr("transform", "translate(200,20) rotate("+n+",50,50)");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

【讨论】:

  • 谢谢。这通过旋转图像来工作,但仍然不会在固定点旋转它。我希望图像在齿轮/牙齿旋转时保持固定在某个点,就好像您在某个点旋转自行车轮胎一样
  • @DevLayi,你可以围绕一个点旋转,指定为rotate()的第二个和第三个参数我使用的图像的宽度/高度为100像素,所以rotate(degrees,50,50)可以在这里工作。将其添加到答案 sn-p。
猜你喜欢
  • 2022-01-21
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 2018-11-03
  • 2018-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多