snap svg : change viewBox using parameters

走远了吗. 提交于 2019-12-07 06:44:35

问题


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?


回答1:


concatenate your values and your seperators (,) as a string. Try this, it should be working.

worldMap.attr({viewBox:x+","+y+","+wi+","+hi});



回答2:


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 ' '



来源:https://stackoverflow.com/questions/33005934/snap-svg-change-viewbox-using-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!