Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

snap svg : change viewBox using parameters

Tags:

svg

snap.svg

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?

like image 506
Rickard Avatar asked Oct 08 '15 03:10

Rickard


2 Answers

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

worldMap.attr({viewBox:x+","+y+","+wi+","+hi});
like image 74
Holger Will Avatar answered Nov 15 '22 09:11

Holger Will


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

like image 32
Искрен Станиславов Avatar answered Nov 15 '22 10:11

Искрен Станиславов