动态添加时不会重复Svg模式

时间:2013-06-16 19:14:39

标签: javascript svg

当我动态添加模式时,我无法重复模式。示例如下:

var SVG_NS =  $('svg')[0].namespaceURI;

pattern = document.createElementNS(SVG_NS,'pattern');
pattern.setAttribute('id','test');
pattern.setAttribute('patternunits','userSpaceOnUse');
pattern.setAttribute('width','10');
pattern.setAttribute('height','10');
pattern.setAttribute('x','0');
pattern.setAttribute('y','0');
pattern.setAttribute('viewbox','0 0 10 10');

path = document.createElementNS(SVG_NS,'rect');
path.setAttribute('x','0');
path.setAttribute('y','0');
path.setAttribute('width','5');
path.setAttribute('height','5');
path.setAttribute('fill','lightblue');
pattern.appendChild(path);
path1 = document.createElementNS(SVG_NS,'rect');
path1.setAttribute('x','5');
path1.setAttribute('y','5');
path1.setAttribute('width','5');
path1.setAttribute('height','5');
path1.setAttribute('fill','lightblue');
pattern.appendChild(path1);

var defs = $('svg')[0].querySelector('defs');
defs.appendChild(pattern);
$('svg path')[0].setAttribute('style','fill: url(#test);');

http://jsfiddle.net/ubLr4/。 它显示模式,但不会重复。有人有什么想法吗?

如果将粘贴生成的SVG复制到文件并运行,它将正确显示,或者如果您动态添加预定义模式,它也会显示确定。

1 个答案:

答案 0 :(得分:2)

SVG区分大小写。您有两行不正确的行:

pattern.setAttribute('patternUnits','userSpaceOnUse');
...
pattern.setAttribute('viewBox','0 0 10 10');