Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identify reused elements and turn them into a <defs> and <use> #1781

Open
bramp opened this issue Sep 9, 2023 · 0 comments
Open

Identify reused elements and turn them into a <defs> and <use> #1781

bramp opened this issue Sep 9, 2023 · 0 comments

Comments

@bramp
Copy link

bramp commented Sep 9, 2023

Is your feature request related to a problem? Please describe.

I have multiple SVGs that contain repeated identical elements. For example, a map with a symbol reused in multiple places. However, the SVG file contains the element for each symbol separately, even though they are identical (just translated).

For example

<svg viewBox="0 0 80 20" xmlns="http://www.w3.org/2000/svg">
  <circle cx="5" cy="5" r="1"  />
  <circle cx="20" cy="5" r="1" />
  <circle cx="35" cy="5" r="1" />
  <circle cx="50" cy="5" r="1" />
  <circle cx="65" cy="5" r="1" />
</svg>

Describe the solution you'd like

SVGO to identify repeated elements of the SVG, extract them into a <defs><symbols> and then replace their usage with a shorter <use>.

For example:

<svg viewBox="0 0 80 20" xmlns="http://www.w3.org/2000/svg">
  <!-- Our symbol in its own coordinate system -->
  <symbol id="myDot" width="10" height="10" viewBox="0 0 2 2">
    <circle cx="1" cy="1" r="1" />
  </symbol>

  <!-- All instances of our symbol -->
  <use href="#myDot" x="5" y="5" />
  <use href="#myDot" x="20" y="5" />
  <use href="#myDot" x="35" y="5" />
  <use href="#myDot" x="50" y="5" />
  <use href="#myDot" x="65" y="5" />
</svg>

(Ok my example makes it look longer, but imagine the symbol being more complex)

Describe alternatives you've considered

The software that generated the SVGs could be adapted to do this. I've recently done this by hand for a couple of images, and it reduced my file considerabile. I've also not confirmed this, but in theory the SVG could render faster when it has to repeat the same symbol.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant