最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to make an image round in d3.js - Stack Overflow

programmeradmin0浏览0评论

to append an image i use this code

       node.append("image")
            .attr("xlink:href", function(d) { return d.img; })
            .attr("x", -25)
            .attr("y", -25)
            .attr("width", 50)
            .attr("height", 50)

i want the image to be round i have tried to use this code

   .attr("style", "border-radius: 30px;");

but it didn't work.. i also tried this one

<style>
   .node image{
      border-color: 2px solid orange;
       border-radius: 25px;
    }

</style>

but to no avail. .

to append an image i use this code

       node.append("image")
            .attr("xlink:href", function(d) { return d.img; })
            .attr("x", -25)
            .attr("y", -25)
            .attr("width", 50)
            .attr("height", 50)

i want the image to be round i have tried to use this code

   .attr("style", "border-radius: 30px;");

but it didn't work.. i also tried this one

<style>
   .node image{
      border-color: 2px solid orange;
       border-radius: 25px;
    }

</style>

but to no avail. .

Share Improve this question asked Aug 27, 2014 at 10:35 Lyner KharlLyner Kharl 1251 gold badge3 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

You need to use patterns.

  1. Create patterns containing the images you want to use in a <defs> tag.
  2. Use a circle
  3. Set circle fill to one of the patterns you created.

eg.:

var defs = svg.append("defs").attr("id", "imgdefs")

var catpattern = defs.append("pattern")
                        .attr("id", "catpattern")
                        .attr("height", 1)
                        .attr("width", 1)
                        .attr("x", "0")
                        .attr("y", "0")

Adding the image:

catpattern.append("image")
     .attr("x", -130)
     .attr("y", -220)
     .attr("height", 640)
     .attr("width", 480)
     .attr("xlink:href", imgurl)

And then setting the fill:

svg.append("circle")
    .attr("r", 100)
    .attr("cy", 80)
    .attr("cx", 120)
    .attr("fill", "url(#catpattern)")

A JS Fiddle example: http://jsfiddle/wcnxywuy/1/

发布评论

评论列表(0)

  1. 暂无评论