Hello. Today I would like to learn how to use the svg filter <feComposite>. I have this…
- User
Hello. Today I would like to learn how to use the svg filter . I have this code: Old Amsterdam Old Amsterdam I would like to exclude the text id="front" from the text id="back" using the svg-filter with the operator xor. Do you have an idea how I can do this? Thank you :-)
Assistant
To use
<feComposite>with thexoroperator to exclude the front text from the back text, you'll need to create a filter that combines both text elements. Here's how you can modify your SVG:<svg width="900" height="300" viewBox="0 0 900 300"> <defs> <filter id="xorFilter"> <!-- Create the back text as source --> <feImage href="#back" result="backImage"/> <!-- Create the front text as source --> <feImage href="#front" result="frontImage"/> <!-- Apply XOR composite operation --> <feComposite in="backImage" in2="frontImage" operator="xor"/> </filter> </defs> <!-- Define the text elements but hide them --> <defs> <text id="back" x="50" y="200" stroke="black" stroke-width="20" fill="white" stroke-linejoin="round">Old Amsterdam</text> <text id="front" x="50" y="200" fill="white">Old Amsterdam</text> </defs> <!-- Apply the filter to a container --> <g filter="url(#xorFilter)"> <rect width="900" height="300" fill="none"/> </g> </svg>However, there's a more straightforward approach that works better in practice. Since
feImagecan have cross-browser compatibility issues, here's an alternative method usingfeCompositemore directly:<svg width="900" height="300" viewBox="0 0 900 300"> <defs> <filter id="xorFilter" x="-50%" y="-50%" width="200%" height="200%"> <!-- Get the source graphic (back text) --> <feFlood flood-color="white" flood-opacity="1" result="white"/> <feComposite in="SourceGraphic" in2="white" operator="xor" result="backXor"/> <!-- This approach works by applying the filter to a group --> </filter> </defs> <!-- Group both texts and apply filter --> <g> <text x="50" y="200" stroke="black" stroke-width="20" fill="white" stroke-linejoin="round">Old Amsterdam</text> <text x="50" y="200" fill="black">Old Amsterdam</text> </g> </svg>For the specific effect you're looking for (creating a stroke with the inner text cut out), here's the most practical solution:
<svg width="900" height="300" viewBox="0 0 900 300"> <defs> <mask id="textMask"> <!-- White background (shows everything) --> <rect width="100%" height="100%" fill="white"/> <!-- Black text (hides this area) --> <text x="50" y="200" fill="black">Old Amsterdam</text> </mask> </defs> <!-- Apply mask to the stroked text --> <text x="50" y="200" stroke="black" stroke-width="20" fill="none" stroke-linejoin="round" mask="url(#textMask)">Old Amsterdam</text> </svg>The mask approach is often more reliable than using
feCompositewithxorfor this type of text effect. Thexoroperator infeCompositeperforms a bitwise XOR operation on the pixel values, which can sometimes produce unexpected results depending on the colors involved.If you specifically need to use
feCompositewithxor, you might need to experiment with different color combinations and opacity values to achieve the desired visual effect.- User
OK. Nice that works. Thanks a lot.
Assistant
You're welcome! Glad the mask solution worked for you. It's often the cleanest approach for creating that hollow text effect with a stroke. If you need to explore more SVG filter effects in the future, feel free to ask!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.