SVG path intersection using Raphael.js

I have had a fight, trying to figure out how to get subtract two paths from eath other. I needed the part which was cut out, not the overlapping part. Raphael.js have a function which came to my rescue: pathInterSection(path1, path2) . Update : Please see A native better solution below.

Two paths

Paths: sec and pri

Calc the intersection

Raphael.pathIntersection(sec, pri) Reset

Subtract intersection from sec

This is sec subtracted pri .

A native better solution: mask

I discovered some problem with the above method. It is not bullit proof. I found a bettes solution which uses native SVG mask tag.

Masking out

I create a mask which is a p (ath) element. It is a combination of a path based rectangle which fills out the whole canvas. I is then added the pri element which I want to mask out on sec element.

For this to work the mask needs to use the fill-rule " evenodd ". Look inside the HTML to see the trick.