I'm trying to fit my SVG image inside a parent container. However, I probably misunderstood something because nothing works for me.
I tried use vievBox
and preserveAspectRatio
but it's not effect anything. JS FIDDLE
Then I tried to write a script to transform SVG but still it's not matching viewbox perfectly JS FIDDLE 2
Somehow both examples working in Internet Explorer 11 but not in Firefox and Chrome.
HTML
<div class="svg_box">
<svg id="svg_map" viewBox="0 0 800 400" preserveAspectRatio="xMidYMid meet">
<g id="viewport">
<g id="County_fills">
<g id="wicklow">
<path d="m 660.19812,671.17693 c 4.3002,-24.4434 25.98479,-42.3466 13.668,-66.4151 3.521,-20.6076 -8.00616,-48.1039 -28.42028,-38.027 -11.84916,-7.4307 -15.52234,9.0615 -25.5361,-10.1857 -13.21206,-5.8314 -15.00724,18.141 -19.90659,23.682 -3.4252,15.0746 -22.32726,16.7667 -25.17803,34.5218 -0.30454,15.0967 8.81716,24.9106 24.882,22.849 7.57843,8.1943 17.12841,-1.2087 9.03025,18.9026 -10.09526,5.7923 -29.7139,-17.2323 -30.74574,1.6656 7.44289,0.675 23.28352,39.2398 37.71449,22.0728 5.70401,-21.6558 29.89655,-24.6066 41.068,-9.182 -0.82169,2.7052 6.39378,3.4876 3.424,0.116 z" id="path3626" />
</g>
<!-- ... other counties ... --->
</g>
</g>
</svg>
JS
$('#viewport').each(function () {
var objThs = $(this);
scaleH = $('#svg_map').innerHeight() / objThs[0].getBoundingClientRect().height;
scaleW = $('#svg_map').innerWidth() / objThs[0].getBoundingClientRect().width;
if (scaleH < scaleW) {
scale = scaleH;
}
else {
scale = scaleW;
}
centerX = parseInt($('#svg_map').innerWidth() - objThs[0].getBoundingClientRect().width * scale) / 2;
centerY = parseInt($('#svg_map').innerHeight() - objThs[0].getBoundingClientRect().height * scale) / 2;
objThs.attr('transform', "matrix(" + scale + ",0,0," + scale + "," + centerX + ","+centerY+")");
});
I will be realy happy if it's possible to do that without JS but JS is better than nothing ;-)
Any help will be appreciated.