Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clipPath in multiple SVG tags

Suppose you have multiple SVG tags where in each you define a different clip path with the same ID.

<svg id="svg1" width="200" height="200">
    <defs>
        <clipPath id="nodeclipper">
            <rect width="100" height="100" x="0" y="0" />
        </clipPath>
    </defs>
</svg>

<svg id="svg2" width="200" height="200">
    <defs>
        <clipPath id="nodeclipper">
            <circle cx="20" cy="0" r="40" />
        </clipPath>
    </defs>
</svg>

I also made a JSFiddle. What is the expected behaviour? I thought that an element could only reference definitions inside its own SVG tag, but that doesn't seem to be the case:

  • Chrome 26: Uses circle clip path two times.
  • Firefox 17: Uses rect clip path two times.
  • Safari 6: Renders one rect and one circle clip path as expected.

It gets weird when you hide one of the SVG tags because Chrome and Safari then drop the clip-path entirely.

I know it works when the clipPaths have different IDs but is it supposed to be that way? As far as I see the spec doesn't contain information about the issue.

like image 537
prayerslayer Avatar asked Apr 09 '13 20:04

prayerslayer


People also ask

What is clipPath SVG?

The <clipPath> SVG element defines a clipping path, to be used by the clip-path property. A clipping path restricts the region to which paint can be applied. Conceptually, parts of the drawing that lie outside of the region bounded by the clipping path are not drawn.

How to use clipPath SVG?

The clip-path property is used to specify a clipping path that is to be applied to an element. Using clip-path , you can apply an SVG <clipPath> to an element by referencing that path in the property value. You can also define a clipping path using one of the basic shapes defined in the CSS Shapes module.

What is clipPath?

The clip-path CSS property creates a clipping region that sets what part of an element should be shown. Parts that are inside the region are shown, while those outside are hidden.


1 Answers

What you are doing is invalid per http://www.w3.org/TR/SVG/struct.html#IDAttribute this references http://www.w3.org/TR/2008/REC-xml-20081126/ which addresses this specific issue directly...

Values of type ID MUST match the Name production. A name MUST NOT appear more than once in an XML document as a value of this type; i.e., ID values MUST uniquely identify the elements which bear them.

like image 197
Robert Longson Avatar answered Sep 28 '22 07:09

Robert Longson