I want to create a JavaScript color picker. I'm using SVG to create a color triangle like this:
body{ background: black; }
<svg xmlns="http://www.w3.org/2000/svg" width="88" height="88" class="saturation-brightness"><defs> <radialGradient id="darks" cx="7.63px" cy="65px" r="72.7446px" gradientUnits="userSpaceOnUse"> <stop offset="0%" stop-color="#000000" /> <stop offset="100%" stop-color="rgba(0, 0, 0, 0)" /> </radialGradient> <radialGradient id="lights" cx="80.37" cy="65px" r="72.7446px" gradientUnits="userSpaceOnUse"> <stop offset="0%" stop-color="#FFFFFF" /> <stop offset="100%" stop-color="rgba(255, 255, 255, 0)" /> </radialGradient> <radialGradient id="mids" cx="44px" cy="65px" r="36.37px" gradientUnits="userSpaceOnUse"> <stop offset="0%" stop-color="#808080" /> <stop offset="100%" stop-color="rgba(128, 128, 128, 0)" /> </radialGradient> </defs> <polygon points="44,2 7.63,65 80.37,65" fill="red" /> <polygon points="44,2 7.63,65 80.37,65" fill="url(#mids)" /> <polygon points="44,2 7.63,65 80.37,65" fill="url(#darks)" /> <polygon points="44,2 7.63,65 80.37,65" fill="url(#lights)" /> </svg>
However, as you can see, the edges of the triangle are not rendered correctly, this happens in all browsers.
Is there a way to avoid this when using multiple SVG gradient fills? Thanks in advance!
P粉7690454262024-04-02 14:43:13
You're seeing anti-aliasing in action: the pixels at the edge of the shape are a blend of the colors on either side of the geometry's border.
Try using shape-rendering: scrapEdges
to turn off anti-aliasing, but be aware that this property only provides a hint to the browser, but no explicit instructions on what to do. SpecificationIntroduced crispEdges
Values:
body{ background: black; } .saturation-brightness { shape-rendering: crispEdges; }