Alban
Alban

Reputation: 3215

Constructed SVG does not work where HTML-drawn does

When I draw a SVG with viewBox attribute in HTML source code I can re-size the window and my SVG automatically scales.

<svg xmlns="http://www.w3.org/2000/svg" viewBox="-350 -250 700 500">
    <circle r="200" class="face" fill="green"/>
    <path fill="none" class="face"
       transform="translate(-396,-230)" 
       d="M487.41, 282.411c-15.07, 36.137-50.735, 61.537-92.333,
          61.537 c-41.421, 0-76.961-25.185-92.142-61.076"/>
    <circle cx="-60" cy="-50" r="20" fill="black"/>
    <circle cx="60" cy="-50" r="20" fill="black"/>
</svg>

But when I construct the exact same SVG with D3.js the viewBox doesn't work!

var smileyRed = d3.select("#js")
                .append("svg")
                .attr("xmlns", "http://www.w3.org/2000/svg")
                .attr("viewbox", "-350 -250 700 500");

smileyRed.append("circle")
     .attr('r','200')
     .attr('class','face')
     .attr('fill','red');
smileyRed.append("path")
    .attr('fill','none')
    .attr('class','face')
    .attr('transform','translate(-396,-230)')
    .attr('d','M487.41, 282.411c-15.07, 36.137-50.735, 61.537-92.333,
               61.537 c-41.421, 0-76.961-25.185-92.142-61.076');
smileyRed.append("circle")
     .attr('cx','-60')
     .attr('cy','-50')
     .attr('r','20')
     .attr('fill','black');
smileyRed.append("circle")
     .attr('cx','60')
     .attr('cy','-50')
     .attr('r','20')
     .attr('fill','black');

JSfiddle Demo

Upvotes: 1

Views: 215

Answers (1)

Luca Rainone
Luca Rainone

Reputation: 16468

viewBox is a case-sensitive attribute

replace viewbox with viewBox

.attr("viewBox", "-350 -250 700 500");

from here: http://www.w3.org/TR/SVG/styling.html#CaseSensitivity

Property declarations via presentation attributes are expressed in XML [XML10], which is case-sensitive.

Upvotes: 3

Related Questions