Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render svg elements with crisp edges while still keeping anti-aliasing?

Tags:

html

svg

Is there a way to render svg elements with crisp edges while still keeping anti-aliasing?

I'm creating a browser-based tool that works in modern browsers.

Playing around with the shape-rendering attribute doesn't give me the results I'm looking for.

I want my elements to have nice anti-aliasing so that the paths look smooth like below with shape-rendering: auto:

enter image description here

But I also want elements that don't require anti-aliasing, like the start box to look sharp and crisp, such as when rendered with shape-rendering: crispEdges:

enter image description here

Is this possible? Am I looking to have my cake and eat it too?

like image 551
brice Avatar asked Mar 04 '14 02:03

brice


People also ask

How do I make SVG crisp?

To achieve crisp edges, the user agent might turn off anti-aliasing for all lines and curves or possibly just for straight lines which are close to vertical or horizontal. Also, the user agent might adjust line positions and line widths to align edges with device pixels.

How is SVG rendered?

SVG uses a "painters model" of rendering. Paint is applied in successive operations to the output device such that each operation paints onto some area of the output device, possibly obscuring paint that has previously been layed down.

Can I use shape rendering?

The shape-rendering property is used to hint the renderer about the tradeoffs that have to be made while rendering shapes like circles, rectangles or paths. The renderer can be told to make the shape geometrically precise or optimize the shape to speed up rendering in certain situations.


1 Answers

Perhaps you set shape-rendering property for root svg element.
You should set shape-rendering property for each shape elements, like this.

<?xml version="1.0" standalone="no"?> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">     <rect x="10" y="10" width="150" height="20" shape-rendering="crispEdges"          fill="none" stroke="black"/>     <path d="M80,30l100,100" shape-rendering="optimizeQuality"          stroke="black" stroke-width="5"/> </svg> 
like image 166
defghi1977 Avatar answered Sep 18 '22 14:09

defghi1977