Why is my SVG path unexpectedly clipped in Chrome, Edge, and Firefox? - svg

Consider the following SVG image code:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 1000">
<title>Clipped Path Problem Demo</title>
<g id="group:image">
<defs>
<mask id="mask">
<rect id="rectangle:inset" x="250" y="134" width="500" height="732" fill="white"/>
</mask>
</defs>
<g id="group:curves" mask="url('#mask')">
<!-- <rect id="rectangle:filler.shape" width="1" height="1" fill="transparent"/> -->
<path id="path:curves" d="
M 441 327
c -85 88 -85 175 -85 175
c 0 88 85 175 85 175
s 90 103 90 103
c 85 88 85 175 85 175" fill="transparent" stroke="black" stroke-width="73"/>
</g>
</g>
<g id="group:guides">
<rect id="rectangle:guide.area:inset" x="250" y="134" width="500" height="732" fill="transparent" stroke="black" stroke-dasharray="20 10" stroke-width="1"/>
</g>
</svg>
(CodePen: https://codepen.io/patrickdark/full/zPEVVg/. (I added a height="500px" attribute to the pen to make it more likely to fit on screen.))
The left side of the curved path is clipped to a vertical line even though the mask is well over the size needed to contain that portion of the image.
If I insert a shape of any size with non-zero dimensions into the masked g element—such as the 1×1 transparent rectangle in the XML comment—the clipped edge is magically unclipped.
I would think this is a bug except that it’s happening in Chrome (62), Edge (41), and Firefox Developer Edition (58 Beta 4). What I would like to know is why this seemingly nonsensical clipping is occurring.
(Note: Clipping at the bottom of the curved path is intentional.)

Since a mask works on color and alpha channel values (a potentially costly operation), it doesn't stretch to infinity, but rendering is clipped of by default at the object bounding box of the masked element plus 10% in every direction (defined by the attributes x, y, width, height in bounding box units).
The object bounding box although is defined not to include stroke widths. You are surpassing these limits and need to make adjustments at least to the x and width attribute of the mask.
Since this seems to be not intuitive, the general algorithm for applying a mask goes like this:
Identify the region the mask is applied to. This is always the rectangle given by the x, y, width, height attributes of the <mask> element. If, as is the default, the maskUnits attribute is given as objectBoundingBox, the coordinates are determined by the object bounding box of the element the mask is applied to. The contents of the mask are not taken into account. 0 and 1 in that coordinate system are the minimum and maximum of the joined bounding box of the masked element and all its descendants.
Superimpose the content of the mask element with the element it is applied to. As a default, the coordinate system used for placing these elements is given as maskContentUnits="userSpaceOnUse", which means the same coordinate system the masked element is drawn into.
For every point within the region defined above, multiply the opacity of the masked content with the luminance of the masking content to compute a resulting opacity of the masked element.
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 1000">
<title>Clipped Path Problem Resoved</title>
<g id="group:image">
<defs>
<mask id="mask" x="-20%" width="140%">
<rect id="rectangle:inset" x="250" y="134" width="500" height="732" fill="white"/>
</mask>
</defs>
<g id="group:curves"mask="url('#mask')">
<!-- <rect id="rectangle:filler.shape" width="1" height="1" fill="transparent"/> -->
<path id="path:curves" d="
M 441 327
c -85 88 -85 175 -85 175
c 0 88 85 175 85 175
s 90 103 90 103
c 85 88 85 175 85 175" fill="transparent" stroke="black" stroke-width="73"/>
</g>
</g>
<g id="group:guides">
<rect id="rectangle:guide.area:inset" x="250" y="134" width="500" height="732" fill="transparent" stroke="black" stroke-dasharray="20 10" stroke-width="1"/>
</g>
</svg>

Related

How to make stroke-width scale with svg width but not with height?

I need strokes to scale with width but not with height.
This is the closest I've come to a solution:
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="50">
<rect width="100%" height="100%" fill="black"/>
<svg viewBox="0 0 1 1" preserveAspectRatio="none">
<path vector-effect="non-scaling-stroke" stroke-width="5" stroke="blue" d="
M 0,0 L 1,1
M 0,1 L 1,0
"/>
</svg>
</svg>
This lets me change the height without affecting the stroke width, but also I need the strokes to scale when the entire svg is scaled. The problem is that I can't make the strokes scalable again. I've tried using percentages, but they are relative to the diagonal size.

SVG fill not coloring the background

I have an svg and when I define the fill it behaves as stroke
For example
<svg stroke="black" fill="red">
<path d="M10 10 H 90 V 90 H 10 Z"/>
</svg>
This works as expected, but when I have
<svg stroke="black" fill="red">
<path d="M12,5.41,19.59,13H15v6H9V13H4.41L12,5.41M12,4,3.71,12.29A1,1,0,0,0,4.41,14H8v5a1,1,0,0,0,1,1h6a1,1,0,0,0,1-1V14h3.59a1,1,0,0,0,.71-1.71L12,4Z"/>
</svg>
It doesn't. Any suggestions why the fill doesn't work as the other svg ?
DEMO
<svg stroke="black" fill="red">
<path d="M12,5.41,19.59,13H15v6H9V13H4.41L12,5.41M12,4,3.71,12.29A1,1,0,0,0,4.41,14H8v5a1,1,0,0,0,1,1h6a1,1,0,0,0,1-1V14h3.59a1,1,0,0,0,.71-1.71L12,4Z"/>
</svg>
<svg stroke="black" fill="red">
<path d="M10 10 H 90 V 90 H 10 Z"/>
</svg>
If we enlarge the arrow, you will see what is happening.
<svg viewBox="0 0 40 40"
stroke="black" fill="red" stroke-width="0.2">
<path d="M12,5.41,19.59,13H15v6H9V13H4.41L12,5.41M12,4,3.71,12.29A1,1,0,0,0,4.41,14H8v5a1,1,0,0,0,1,1h6a1,1,0,0,0,1-1V14h3.59a1,1,0,0,0,.71-1.71L12,4Z"/>
</svg>
As Robert said, you have an arrow shape with an arrow shaped hole inside it. Your arrow was appearing black, instead of red, because at that small size the thickness of the black stroke was hiding the red fill.
If you want a red arrow with a black outline, then you will need to redesign your shape so that it is just a single outline, not a "double-walled" one.

How to create a trapezium mainly bending the right side of the rectangle

This is my svg path. I am trying to achieve trapezium. Left of the rectangle has come out fine. I wanted the same way on the right side. How do I bend it?
<path d="M20 20 H 300 V 70 H 10 Z" fill="transparent" style="stroke:black; stroke-width:2"/>
This is the image reference of the shape I wanted:
http://amsi.org.au/teacher_modules/C3/C3g37.png
If you don't want a vertical line best not to use V as that command draws them. I've replaced the V with an L below.
<svg width="100%" height="100%">
<path d="M20 20 H 300 L 310 70 H 10 Z" fill="transparent" style="stroke:black; stroke-width:2"/>
</svg>

How does SVG opacity stack?

I'm wondering, how do SVG elements stack their opacity? More specifically, if I have an element with opacity: 0.4, what will the overlapping element's opacity need to be in order to result in opacity: 0.8 at the overlap?
What is the resulting opacity at the overlap?
https://jsfiddle.net/HZr7v/18/
The answer is 0.6667.
The rule is that transparency combines by multiplication. So if you have two overlapping objects with transparencies of 60% and 33.33%, then the transparency of the overlapping region will be (0.6 × 0.3333) = 0.2.
An object's alpha value is equal to 1 minus its transparency, so the combination of α=0.4 and α=0.6667 is equal to 1 - (1-0.4) × (1-0.6667) = 1 - 0.6 × 0.3333 = 1 - 0.2 = 0.8.
By way of illustration, here's an SVG image containing two overlapping circles with alpha values of 0.4 and 0.6667 next to a solid circle filled with 80% black (#333):
<svg width="340" height="200" viewBox="0 0 340 200">
<circle cx="100" cy="80" r="60" fill="#000" opacity="0.4" />
<circle cx="100" cy="120" r="60" fill="#000" opacity="0.6667" />
<circle cx="180" cy="100" r="60" fill="#333" opacity="1" />
<text x="0" y="30">α=0.4</text>
<text x="0" y="180">α=0.6667</text>
<text x="250" y="110">80% black</text>
</svg>

SVG shadow cut off

The SVG I'm working with has a drop shadow via feGaussianBlur filter.
The shadow itself is displayed properly, but gets cut off on top and bottom edges.
Like so:
The SVG in question is:
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE svg
PUBLIC '-//W3C//DTD SVG 1.1//EN'
'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>
<svg height="600" version="1.1" width="700" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs/>
<filter id="SVGID_0">
<feGaussianBlur in="SourceGraphic" stdDeviation="6.6"/>
<feOffset dx="0" dy="0"/>
<feMerge>
<feMergeNode/>
<feMergeNode in="SourceGraphic"/>
</feMerge>
</filter>
<path d="M 0 83 Q 0 83 0 83 Q 0 83 6 79.5 Q 12 76 17 71 Q 22 66 30.5 57.5 Q 39 49 54 36 Q 69 23 82.5 16.5 Q 96 10 120 4.5 Q 144 -1 170.5 0 Q 197 1 218 16.5 Q 239 32 253.5 51 Q 268 70 278 83.5 Q 288 97 299 110 Q 310 123 320 129.5 Q 330 136 338 136.5 Q 346 137 355 129.5 L 364 122" stroke-linecap="round" style="stroke: #005e7a; stroke-width: 30; fill: none; filter: url(#SVGID_0);" transform="translate(50 50)" />
</svg>
The cropping seems to happen consistently in Chrome (30), Firefox (25), and Opera (12).
I can see that it's not a viewbox limitation, as it's set to 600x700.
I can also see in devtools inspector the bounding box of <path> element, and it's almost as if that's what cuts off the shadow:
If that's the case:
Why is the shadow only cut off vertically and not horizontally?
How to work around it, so that it's not clipped like this?
If it's not the bounding box, what causes this and how to avoid this clipping?
You need to increase the size of the filter region.
<filter id="SVGID_0" y="-40%" height="180%">
works just fine. The silent defaults for the filter region are: x="-10%" y="-10%" width="120%" height="120%" - large blurs usually get clipped. (Your shadow isn't getting clipped horizontally because your width is about 2.5x your height - so that 10% results in a wider horizontal filter region). Also, the y filter region seems to be calculated as if the path had a zero pixel stroke, so it's ignoring the stroke width. (Different browsers have different behavior wrt whether they consider the stroke to be part of the bounding box for purposes of filter region calculation.)
(Update: Moving up observations from comments)
Please note that if your particular shape is either zero width or zero height (e.g. a horizontal or vertical line), then you must specify filterUnits="userSpaceOnUse" as part of the filter declaration and explicitly specify a filter region (x,y,width height) in userSpaceUnits (usually pixels) that creates enough room to display a shadow.
Add an attribute to the filter shadow svg tag:
filterUnits="userSpaceOnUse"
Final Output:
<filter id="dropshadow" filterUnits="userSpaceOnUse" height="800" width="1600">
Which makes the shadow absolutely positioned and visible outside of its container.
If you're using it inside an HTML, you can simply use CSS properties to fix this issue.
svg {
overflow: visible !important;
}
I haven't checked other browsers, but chrome has the overflow: hidden by default on svg tags.
A bit late, but I hope it's helpful.

Resources