乐闻世界logo
搜索文章和话题

How can I create a " glow " around a rectangle with svg?

1个答案

1

Creating a 'glow' effect around a rectangle in SVG is typically achieved using the <filter> element. Here is a step-by-step implementation and example:

Step 1: Define SVG Elements

First, we need to define an SVG element and include a rectangle within it.

xml
<svg width="200" height="200"> <rect x="50" y="50" width="100" height="100" style="fill:blue" /> </svg>

Step 2: Add Filter Definition

Next, we add a <defs> element inside the SVG to define the filter. Within the filter, we use <feGaussianBlur> to create a blur effect, which is essential for generating the glow.

xml
<svg width="200" height="200"> <defs> <filter id="glow"> <feGaussianBlur stdDeviation="8" result="blurred"/> <feMerge> <feMergeNode in="blurred"/> <feMergeNode in="SourceGraphic"/> </feMerge> </filter> </defs> <rect x="50" y="50" width="100" height="100" style="fill:blue" filter="url(#glow)" /> </svg>

Explanation:

  1. <filter id="glow">: Defines a filter with the ID glow, which can be referenced where needed.
  2. <feGaussianBlur stdDeviation="8" result="blurred"/>: This element creates a Gaussian blur effect. The stdDeviation attribute controls the blur intensity, and result specifies the output of this blur operation.
  3. <feMerge>: This element merges multiple image streams. By using <feMergeNode>, we combine the blurred image with the original graphic, achieving a glow effect while preserving visibility of the original shape.

Step 3: Apply the Filter

Finally, we apply the filter to the rectangle element by referencing the defined filter #glow using the filter attribute.

In this example, we create a blue rectangle with a glow effect. This method is ideal for creating visually appealing effects on SVG graphics and is commonly used to highlight specific elements or enhance graphical interfaces.

2024年7月20日 03:39 回复

你的答案