what is the correct syntax to supply a viewBox attribute of an svg element using pre-defined values? I have this :
var mySvg=Snap("#mySvg");
var worldMap=mySvg.select("#worldMap");//worldMap is an svg inside svg
when i tried this :
worldMap.attr({viewBox:"760, 455, 132, 78"});
it works just fine. However when i tried it using parameters :
var x=760;
var y=455;
var wi=132;
var hi=78;
worldMap.attr({viewBox:"x, y, wi, hi");
nothing happened, why? I belive the problem is to find the correct syntax. I also tried :
worldMap.attr({viewBox:x, y, wi, hi);
worldMap.attr({viewBox:{x, y, wi, hi});
worldMap.attr({viewBox:(x, y, wi, hi));
worldMap.attr({viewBox:[x, y, wi, hi]);
nothing works so far...any suggestion?
concatenate your values and your seperators (,) as a string. Try this, it should be working.
worldMap.attr({viewBox:x+","+y+","+wi+","+hi});
you can also use:
worldMap.attr({viewBox:[x,y,wi,hi].join(',')});
worldMap.attr({viewBox:[x,y,wi,hi].join(' ')});
it's more readable
Edit: in my first answer i used joining with ',', but in MDN tutorial definition is with space char ' '
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With