简体   繁体   English

SVG模式在动态添加时不重复

[英]Svg pattern not repeating when dynamically added

I simply can't get pattern to repeat when I added it dynamically. 当我动态添加它时,我简直无法获得重复的模式。 Example is here: 示例在这里:

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/ . http://jsfiddle.net/ubLr4/ It shows pattern, but it doesn't get repeated. 它显示模式,但不会重复。 Anyone has any ideas? 有人有什么想法吗?

If you copy paste generated SVG into file and run, it will display correctly or if you add predefined pattern dynamically it also shows ok. 如果将生成的SVG粘贴复制到文件中并运行,它将正确显示,或者如果您动态添加预定义的模式,它也会显示ok。

SVG is case sensitive. SVG区分大小写。 You have two incorrect lines: 您有两行错误:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM