svg / svgo

⚙️ Node.js tool for optimizing SVG files
https://svgo.dev/
MIT License
20.82k stars 1.38k forks source link

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

Open Emasoft opened 7 years ago

Emasoft commented 7 years ago

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.

GreLI commented 7 years ago

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

ux-engineer commented 7 years ago

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

luongvo commented 6 years ago

Any update on this?

GreLI commented 6 years ago

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.

AdrianEddy commented 4 years ago

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

GreLI commented 4 years ago

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

jmtrivial commented 4 years ago

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

cluick commented 3 years ago

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.

jmtrivial commented 3 years ago

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

jetrotal commented 2 years ago

Any news on this?

pwichmann commented 2 years ago

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

akre54 commented 1 year ago

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.

aetinx commented 3 weeks ago

Anything on this yet?

akre54 commented 3 weeks ago

I wrote a gist that might help: https://gist.github.com/akre54/4d9ace17fb27d0507a6c790be5047e3d

aetinx commented 3 weeks ago

I wrote a gist that might help: https://gist.github.com/akre54/4d9ace17fb27d0507a6c790be5047e3d

@akre54 I've been looking for a tool to replace the use of masks and clipping paths in some SVG files by "flattening" them... would this be the thing to do it?

akre54 commented 3 weeks ago

@aetinx it's been a while but that code should point you in the right direction. It uses paper.js and the intersect function to remove clipping paths and masks, just outputting a path that is the intersection with the clipping path.

aetinx commented 3 weeks ago

@aetinx it's been a while but that code should point you in the right direction. It uses paper.js and the intersect function to remove clipping paths and masks, just outputting a path that is the intersection with the clipping path.

@akre54 I sure hope someone with a bit more knowledge would be able to help make a proper tool for this down the line... or add it to SVGO.

Some of my pain points: image

aetinx commented 3 weeks ago

Files in question:

u1f1e6_1f1e8 u1f1f0_1f1ea u1f1f2_1f1f0