D3.js CodeFlower Image as circle background

void

I am using CodeFlower built upon D3.js. I want to show an image as a background instead of arbitrary colors, and i successfully did that using SVG Patterns.

DEMO FIDDLE

  // Enter any new nodes
  this.node.enter().append('defs')
        .append('pattern')
            .attr('id', function(d) { return (d.id+"-icon-img");}) // just create a unique id (id comes from the json)
            .attr('patternUnits', 'userSpaceOnUse')
            .attr('width', 80)
            .attr('height', 80)
            .append("svg:image")
                .attr("xlink:xlink:href", function(d) { return (d.icon);}) // "icon" is my image url. It comes from json too. The double xlink:xlink is a necessary hack (first "xlink:" is lost...).
                .attr("x", 0)
                .attr("y", 0)
                .attr("height", 80)
                .attr("width", 80)
                .attr("preserveAspectRatio", "xMinYMin slice");

  this.node.enter().append('svg:circle')
    .attr("class", "node CodeFlowerNode")
    .classed('directory', function(d) { return (d._children || d.children) ? 1 : 0; })
    .attr("r", function(d) { return d.children ? 3.5 : Math.pow(d.size, 2/5) || 1; })
    .style("fill", function(d) { return ("url(#"+d.id+"-icon-img)");})
    /* .style("fill", function color(d) {
      return "hsl(" + parseInt(360 / total * d.id, 10) + ",90%,70%)";
    })*/
    .call(this.force.drag)
    .on("click", this.click.bind(this))
    .on("mouseover", this.mouseover.bind(this))
    .on("mouseout", this.mouseout.bind(this));

The problem i am seeing is the image is not centrally aligned in the circle it is kind of tile layout composed of 4 images.

enter image description here

How can i make its position center and covering the circle nicely.

DEMO FIDDLE

Paul LeBeau

You need to change the way you define your pattern. You should define it with respect to the element it is being applied to. So leave patternUnits at the default of objectBoundingBox, and set the width and height to 1.

Then you need to also set the patternContentUnits to objectBoundingBox also, and give the <image> the same size (width and height of 1).

  this.node.enter().append('defs')
        .append('pattern')
            .attr('id', function(d) { return (d.id+"-icon");})  
            .attr('width', 1)
            .attr('height', 1)
            .attr('patternContentUnits', 'objectBoundingBox')
            .append("svg:image")
                .attr("xlink:xlink:href", function(d) { return (d.icon);})
                .attr("height", 1)
                .attr("width", 1)
                .attr("preserveAspectRatio", "xMinYMin slice");

Demo fiddle here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Image within circle object causes pixelation d3 js

From Dev

d3.js display image as circle

From Dev

d3 image in circle not rendering properly

From Dev

How to Add Image to Circle Dynamically in D3js

From Dev

Adding an image within a circle object in d3 javascript?

From Dev

D3 conditionally append image OR circle element based on variable

From Dev

d3: svg image in zoom circle packing

From Dev

Dynamically place buttons where there is a circle on background image

From Dev

Background image to SVG choropleth map using D3

From Dev

Using D3 to fill an svg with a background image

From Dev

How to add a background image to a plot created using D3

From Dev

How to add a background image to a plot created using D3

From Dev

how to draw circle with text in middle and an image as a background of the circle?

From Dev

how to fill hexagon with background image in d3js?

From Dev

D3.js Can not add background image to rectangle

From Dev

Smart background div with circle but what if is background-image

From Dev

circle with gradient background border and changing colors with js

From Dev

How to get an non-overlapped circle packing d3 js chart?

From Dev

How to get an non-overlapped circle packing d3 js chart?

From Dev

D3/Raphael js draws 1000+ animated circle with slow fps

From Dev

Change circle "start-stop" button background image on click in WPF

From Dev

Draw Circle in the background of Text View, over each image in Fragment

From Dev

Appending div to circle in d3.js

From Dev

d3.js circle hover animation

From Dev

Appending div to circle in d3.js

From Dev

d3.js circle hover animation

From Dev

Change div background image with JS

From Dev

Fullpage.js Background Image

From Dev

Style background image with JS not working

Related Related

  1. 1

    Image within circle object causes pixelation d3 js

  2. 2

    d3.js display image as circle

  3. 3

    d3 image in circle not rendering properly

  4. 4

    How to Add Image to Circle Dynamically in D3js

  5. 5

    Adding an image within a circle object in d3 javascript?

  6. 6

    D3 conditionally append image OR circle element based on variable

  7. 7

    d3: svg image in zoom circle packing

  8. 8

    Dynamically place buttons where there is a circle on background image

  9. 9

    Background image to SVG choropleth map using D3

  10. 10

    Using D3 to fill an svg with a background image

  11. 11

    How to add a background image to a plot created using D3

  12. 12

    How to add a background image to a plot created using D3

  13. 13

    how to draw circle with text in middle and an image as a background of the circle?

  14. 14

    how to fill hexagon with background image in d3js?

  15. 15

    D3.js Can not add background image to rectangle

  16. 16

    Smart background div with circle but what if is background-image

  17. 17

    circle with gradient background border and changing colors with js

  18. 18

    How to get an non-overlapped circle packing d3 js chart?

  19. 19

    How to get an non-overlapped circle packing d3 js chart?

  20. 20

    D3/Raphael js draws 1000+ animated circle with slow fps

  21. 21

    Change circle "start-stop" button background image on click in WPF

  22. 22

    Draw Circle in the background of Text View, over each image in Fragment

  23. 23

    Appending div to circle in d3.js

  24. 24

    d3.js circle hover animation

  25. 25

    Appending div to circle in d3.js

  26. 26

    d3.js circle hover animation

  27. 27

    Change div background image with JS

  28. 28

    Fullpage.js Background Image

  29. 29

    Style background image with JS not working

HotTag

Archive