Trying to pass string as function argument to SVG

188 views Asked by At

I'm trying to dynamically create this function:

svg.path($('#shape23'), 
path.move(333,410)
.smoothC(333,410,333,410)
.smoothC(217.5,415.75,217.5,415.75)
.smoothC(333,500,333,500)
.close(),  {fill: '#CCCCCC'});

...with a variable value for the second argument (from path.move to close()):

var myPath = 'path.move(333,410)';
myPath += '.smoothC(333,410,333,410)';
myPath += '.smoothC(217.5,415.75,217.5,415.75)';
myPath += '.smoothC(333,500,333,500)';
myPath += '.close()';

 svg.path($('#shape23'), myPath,  {fill: '#CCCCCC'});

...but I'm getting a parsing error.

Can anyone help before I tear out my remaining hair?

2

There are 2 answers

0
ShankarSangoli On BEST ANSWER

Try this

svg.path($('#shape23'), eval(myPath),  {fill: '#CCCCCC'});
0
Rick On

I should play with those svg constructors, I use XML...

path = Document.createElementNS( 'http://www.w3.org/2000/svg', 'path' );
path.setAttribute( 'id', '#shape23' );
path.setAttribute( 'd', 'm33,410 c333,410 217.5,415.75 333,500z' );
path.setAttribute( 'fill', '#CCCCCC' );

Not sure of the path metrics, but you can fix them.