svgo icon indicating copy to clipboard operation
svgo copied to clipboard

Feature Req: Add Option to remove/flatten all clipPath and all mask elements

Open Emasoft opened this issue 7 years ago • 18 comments

SVGs exported from Adobe Illustrator make often extensive use of clipPath and mask elements. Sometimes those clipped or masked elements are as big as the image even if the part displayed in the end is very small. Those are very slow because of multiple layers redraw and also tend to bloat the code. A clipPath applied on a closed curve or a polygon can be easily replaced by a single curve with a simple boolean operation.

In my tests with Inkscape I've found that an svg file with 5-6 layers of complex closed curves + clipPaths exported from Adobe Illustrator can be flattened with boolean operations and resulting in a single merged element made of closed paths with no loss of quality. Masks can be also transformed in flat shapes using alpha colors and bezier curves.

The result is identical to the original svg image, only flattened and with reduced overdraw.

Emasoft avatar Mar 10 '17 22:03 Emasoft

Path merging and computation is a complex topic. Can you suggest an effective algorithm (i.e. better then O(n²))?

GreLI avatar Mar 11 '17 08:03 GreLI

I have this problem as well when trying to optimize an SVG that has the world map in vector format, and only part of it is shown by masking... Any solutions for optimizing such SVG with Sketch or Illustrator (or other but those I mainly use)?

Update: Used this solutions before using this svg optimizer...got 1.6mb down to 770kt =§

ux-engineer avatar Jun 16 '17 06:06 ux-engineer

Any update on this?

luongvo avatar Mar 30 '18 04:03 luongvo

No update and unlikely there will be in the near future. I have no idea how to do this effectively. It's better to do such thinks in an image editor.

GreLI avatar Mar 30 '18 17:03 GreLI

How about this: https://gist.github.com/timo22345/9413158 ?

AdrianEddy avatar Oct 10 '19 18:10 AdrianEddy

Seems like it's just converting to shapes and applying transformations. Nothing to do with clip-paths.

GreLI avatar Oct 12 '19 11:10 GreLI

https://github.com/w8r/martinez/ is a very good tool to compute intersections between polygons. Might be a good approach.

jmtrivial avatar Mar 29 '20 00:03 jmtrivial

This would be a very nice feature. I work frequently with SVGs converted from other formats like DWG and these files also contain a lot of layers and invisible (clipped) paths. Removing/optimizing them would greatly reduce the file size.

cluick avatar Jan 12 '21 10:01 cluick

A work in progress implementation of such clipping: https://github.com/ACTIVmap/svg-to-stl

jmtrivial avatar Jan 12 '21 10:01 jmtrivial

Any news on this?

jetrotal avatar Apr 15 '22 11:04 jetrotal

Any news on this? This appears to be a useful functionality for SVGO.

pwichmann avatar Aug 23 '22 11:08 pwichmann

For anyone stuck on this, check out this StackOverflow answer: https://stackoverflow.com/a/70278985

I'd love to see this incorporated into svgo - looks like a pretty straightforward use of paper.js's intersect function.

akre54 avatar Nov 25 '22 17:11 akre54