Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG clipping using javascript

We are trying to use clippath for polyline using javascript. We tried writing a sample HTML code which worked fine:

<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>

This works absolutely fine.

But when we try to generate the same code using javascript it doesn't work:

_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);

Can anyone please help us find the issue with the above code...

Thanks in advance.

like image 226
VKS Avatar asked Oct 08 '13 03:10

VKS


People also ask

How to create SVG graphics using JavaScript?

How to create SVG graphics using JavaScript? All modern browsers support SVG and you can easily create it using JavaScript. Google Chrome and Firefox both support SVG. With JavaScript, create a blank SVG document object model (DOM). Using attributes, create a shape like a circle or a rectangle.

What is clipping and masking in SVG?

A mask consists of a shape or image where each pixel has varying degrees of transparency and opaqueness that can peer through, or hide portions in a very subtle fashion. Now let’s discuss some elements and attributes which enable clipping and masking in SVG. An SVG clipPath accepts many attributes and content model types.

What are the different types of SVG clippath attributes?

An SVG clipPath accepts many attributes and content model types. The types of content models accepted are ones such as title, and description along with other types of meta data tags. It also accepts SMIL animation tags such as <animate>, <animateTransform>, SVG shapes (circle, rect, polygon, path) including <text>, <use>, style, and <script>.

Is it possible to add an empty SVG element in HTML?

All of the following demos have an empty SVG element in the HTML. I’ve manually added it, but you can create and add it via JavaScript as well. I also used a background rectangle so we can see what we’re doing.


1 Answers

After a while of fiddling it turns out that SVG is very case-sensitive and the clippath element needs to be a clipPath element.

See working fiddle: http://jsfiddle.net/F4mq9/2/

Very strange that the static sample worked however.

like image 144
Gigo Avatar answered Sep 19 '22 13:09

Gigo