Home  >  Q&A  >  body text

SVG - ugly edges with multiple gradient fills

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粉937769356P粉937769356188 days ago304

reply all(1)I'll reply

  • P粉769045426

    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;
    }
    
                    
                        
                            
                            
                        
    
                        
                            
                            
                        
    
                        
                            
                            
                        
    
                    
                    
                        
                        
                        
                        
                        
                    
                    

    reply
    0
  • Cancelreply