使用javascript进行SVG剪辑

VKS*_*VKS 10 javascript html5 svg

我们正在尝试使用clippath来使用javascript进行折线.我们尝试编写一个可以正常工作的示例HTML代码:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="parent">
    <clippath id="cut-off-bottom">
        <rect x="0" y="0" width="200" height="100" />
    </clippath>
    <circle cx="100" cy="100" r="100" clip-path="url(#cut-off-bottom)" />
</svg>
Run Code Online (Sandbox Code Playgroud)

这绝对没问题.

但是当我们尝试使用javascript生成相同的代码时,它不起作用:

_svgNS = 'http://www.w3.org/2000/svg';
parent = document.getElementById('parent');

circle = document.createElementNS(_svgNS, 'circle');
circle.setAttributeNS(null, 'cx', '100');
circle.setAttributeNS(null, 'cy', '100');
circle.setAttributeNS(null, 'r', '100');

clippath = document.createElementNS(_svgNS, 'clippath');
clippath.setAttributeNS(null, 'id', 'clip');

r = document.createElementNS(_svgNS, 'rect');
r.setAttributeNS(null, 'x', '0');
r.setAttributeNS(null, 'y', '0');
r.setAttributeNS(null, 'width', '200');
r.setAttributeNS(null, 'height', '100');


clippath.appendChild(r);

circle.setAttributeNS(_svgNS, 'clip-path', 'url(#clip)');
parent.appendChild(clippath);
parent.appendChild(circle);
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮助我们找到上述代码的问题...

提前致谢.

Gig*_*igo 17

经过一段时间的摆弄后,事实证明SVG非常区分大小写,clippath元素需要是一个clipPath元素.

看工作小提琴:http://jsfiddle.net/F4mq9/2/

非常奇怪,静态样本可以正常工作.

  • 将其归结为HTML5的超宽容不区分大小写,而不是XHTML. (2认同)