I have an SVG "g" object that has several components. I'd like to render the whole thing partly transparent (e.g. alpha = 0.5) I'd also like to to be darker if possible. I know individual fill colors can be adjusted, but how about all of them together, possibly in some parameters to the "g" (grouping) structure.
You use an addtional attribute; fill-opacity : This attribute takes a decimal number between 0.0 and 1.0, inclusive; where 0.0 is completely transparent. Additionally you have the following: stroke-opacity attribute for the stroke.
SVG uses three distinct properties to control opacity of basic shapes and text: opacity , fill-opacity , and stroke-opacity . All of these can be set with presentation attributes or with CSS style rules. CSS Color Module Level 3 extended the opacity property to apply to all content.
SVG also supports filters that allow you create an alpha channel (transparency mask) – not unlike those in Photoshop.
The fill-opacity attribute is a presentation attribute defining the opacity of the paint server (color, gradient, pattern, etc.) applied to a shape. Note: As a presentation attribute fill-opacity can be used as a CSS property.
Changing the opacity
of the <g>
(either by the opacity="..."
attribute, or the opacity
CSS rule) will cause the contents of the group to first be composited and then the result to be lowered in opacity. Note that this is distinctly different from lowering the opacity on all the elements inside the group (which you can also do via CSS):
Uses this SVG:
<svg xmlns="http://www.w3.org/2000/svg" width="400" height="400">
<defs><filter id="Darker">
<feColorMatrix type="matrix" values="
0.3 0 0 0 0
0 0.3 0 0 0
0 0 0.3 0 0
0 0 0 0.8 0"/>
</filter></defs>
<g id="g1" transform="translate(60,60)"> <!-- top left -->
<circle r="40" /><rect width="80" height="60" />
</g>
<g id="g2" transform="translate(220,60)"><!-- top right -->
<circle r="40" /><rect width="80" height="60" />
</g>
<g id="g3" transform="translate(60,200)"><!-- bottom left -->
<circle r="40" /><rect width="80" height="60" />
</g>
<g id="g4" transform="translate(220,200)"><!-- bottom right -->
<circle r="40" /><rect width="80" height="60" />
</g>
</svg>
…with this CSS:
circle { fill:red }
rect { fill:blue }
#g2 * { opacity:0.5 } /* Change the opacity of each shape */
#g3 { opacity:0.5 } /* Change the opacity of the group */
#g4 { filter:url(#Darker) } /* Darkens and drops opacity for group */
Note in particular the difference in appearance where the circle and square overlap.
The feColorMatrix
filter looks daunting. All it does is set the RGB values to each be 30% of the original RGB (i.e. darker), and the alpha to be 80% of the original alpha. Change the values as you see fit.
Oh, and if you want to desaturate also you can throw this into the filter (before or after the darken step):
<feColorMatrix type="saturate" values="0.5"/>
<!-- values="0" will drop all color, leaving grayscale only -->
<!-- values="1" will leave the current saturation color -->
<!-- values="99" will super-saturate the colors -->
You could set opacity on the <g>
itself and that would work. If you want it darker you'll need to apply a filter to the <g>
something along these lines perhaps
<filter id="Darker" filterUnits="objectBoundingBox" x="0" y="0" width="100%" height="100%">
<feFlood in="SourceGraphic" flood-color="#0f0" flood-opacity="0.5" result="img2"/>
<feBlend in="SourceGraphic" in2="img2" mode="darken"/>
</filter>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With