正在尝试将字符串作为函数参数传递给SVG

Trying to pass string as function argument to SVG

本文关键字:函数 参数传递 SVG 字符串      更新时间:2023-09-26

我正在尝试动态创建这个函数:

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'});

第二个参数的变量值(从path.move到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'});

但是我得到了一个解析错误。

在我把剩下的头发扯掉之前,有人能帮忙吗?

试试这个

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

我应该使用那些svg构造函数,我使用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' );

不确定路径度量,但您可以修复它们。