javascript - 在 D3 中选择和移动导入的 SVG 文件

标签 javascript svg d3.js

我正在尝试使用 d3,并且我一直在尝试使用我用 Illustrator 制作的简单 svg 图像。

我可以将 submarine.svg 图像附加到我的工作文档中,但我无法选择它并调整它的位置。

我知道我可以用另一种方式做到这一点,但我想了解更多关于 d3 的信息。

这是我尝试将 svg 放置在 x 轴上 500px 处。

var submarine = d3.xml("submarine.svg", "image/svg+xml", function(xml) { document.body.appendChild(xml.documentElement)
      }) d3.select(this).attr("x","500");

最佳答案

请看这个例子:http://jsfiddle.net/y1ye2u8s/2/

// Define a drag behavior for the image.
var drag = d3.behavior.drag()
    .on("drag", dragmove);

// Function that is called upong dragging.
function dragmove(d) {

      var x = d3.event.x;
      var y = d3.event.y;

      // Select and move the image upon dragging.
      d3.select(this)
          .attr("transform", "translate(" + (x-200) + "," + (y-200) + ")");
}

d3.xml("https://rawgit.com/VengadoraVG/moving-to-gnulinux/master/img/tux.svg", "image/svg+xml", 

    function (xml) {

        // Add the image to the document.
        document.body.appendChild(xml.documentElement);

        // Select the svg element representing the image.
        d3.select("svg")

            // Set the svg viewport dimensions.
            .attr("width", "1000")
            .attr("height", "1000");

       // Apply the drag behavior to the image.
       d3.select("svg").select("g")
            .call(drag);    

});

关于javascript - 在 D3 中选择和移动导入的 SVG 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25292879/

相关文章:

javascript - jQuery - 新窗口上的选择器

javascript - 定位圆和箭头

css - 可以使用 SVG 滤镜对图像进行像素化吗?

javascript - "Apply"RaphaelJs 中的转换

d3.js - 如何使用 moment.js 初始化 d3.scale.utc()

javascript - 将垂直堆积条转换为水平堆积条 D3.js v4

javascript - 点击播放YOUTUBE视频并隐藏div

javascript - 流程图转换 C# Javascript/AJAX - 条形图

javascript - Nextjs 将数据(页面标题)发送到组件

javascript - 如何在绘图区域图中用我们选择的颜色填充区域