search
HomeWeb Front-endH5 TutorialSVG进阶|SVG过滤器(SVG Filters)


  SVG过滤器可以为SVG图形添加一些非常酷的效果,如阴影、模糊和高光等效果。

  SVG过滤器的例子

  先来看一个简单的SVG过滤器的例子,直观的感受一些它的效果:

<defs>
  <filter id="blurFilter" y="-5" height="40"
      <feGaussianBlur in="SourceGraphic" stdDeviation="3" y="-"/>
  </filter>
</defs>
 
<ellipse cx="55" cy="60" rx="25" ry="15"
  style="stroke: none; fill: #663399; " />
 
<ellipse cx="155" cy="60" rx="25" ry="15"
  style="stroke: none; fill: #663399; filter: url(#blurFilter);" />


  在这个例子中,对一个SVG椭圆形应用了模糊滤镜,得到的结果如下:
990.png
  SVG过滤器的输入和输出

  SVG过滤器在应用过滤效果的时候需要一个输入源。这个输入源可以是一个图形,或图形的alpha通道,或另一个过滤器的输出值。

  SVG过滤器可以从输入源中产生一个输出图像。一个过滤器的输出可以是另一个过滤器的输入,这样,过滤器可以被链接起来使用。

  下面是一张SVG过滤器输入和输出的说明图片:

  991.png

  SVG过滤器的输入通常在SVG滤镜的in属性中指定,例如:

<feGaussianBlur stdDeviation="3" in="SourceGraphic" />



      如果你需要将一个SVG过滤器的输出作为另一个过滤器的输入,需要在输出元素上添加一个result属性:

    <feGaussianBlur stdDeviation="3" in="SourceGraphic" result="blur"/>


        这样,在另一个过滤器中,可以通过在in属性中设置值为blur来使用它作为输入源。

        过滤器的尺寸

        一个SVG过滤器的尺寸由x、y、width和height属性来决定。

        x和y属性是相对于输入源图形的x和y属性来设定。由于过滤器的输出图形通常会比输入图形大(例如对图形添加模糊效果),因此,我们通常需要将x和y属性设置为负值来剪切掉多出的部分。

        width和height属性指定过滤器的宽度和高度,大多数时候你需要指定宽度和高度大于输出图像的尺寸,以便于在剪切后尺寸和原来的图形基本相等。

        多重过滤器

        你可以通过f44581c4ff918450f05f7d367fc9e950元素来同时使用多个SVG过滤器。看下面的例子:

      <defs>
          <filter id="blurFilter2" y="-10" height="40" x="-10" width="150">
              <feOffset in="SourceAlpha" dx="3" dy="3" result="offset2" />
              <feGaussianBlur in="offset2" stdDeviation="3"  result="blur2"/>
       
              <feMerge>
                  <feMergeNode in="blur2" />
                  <feMergeNode in="SourceGraphic" />
              </feMerge>
          </filter>
      </defs>
       
      <ellipse cx="55" cy="60" rx="25" ry="15"
               style="stroke: none; fill: #0000ff; filter: url(#blurFilter2);" />


      这个例子中创建了一个SVG过滤器,它包括两个滤镜元素:c18c6935688fb13afbb9319b9b72a56e和ad11e455a1398b09cace1648f731f602。偏移滤镜的输入源是椭圆图形的alpha通道,高斯模糊滤镜的输入源是偏移滤镜的输出。

      f44581c4ff918450f05f7d367fc9e950元素将原始图像和高斯模糊滤镜的输出相结合。在f44581c4ff918450f05f7d367fc9e950元素中的结合顺序决定了它们的显示顺序,后输入的元素会显示在先输入元素的上面。


        上面的代码得到的结果类似于一个drop阴影效果,下面是输出的结果:
                    992.png
        高斯模糊滤镜

        SVG高斯模糊滤镜可以将图像进行模糊处理。要使用高斯模糊滤镜,可以使用元素。下面是一个例子:

      <defs>
          <filter id="blurFilter4" x="-20" y="-20" width="200" height="200">
              <feGaussianBlur in="SourceGraphic" stdDeviation="10" />
          </filter>
      </defs>
      <rect x="20" y="20" width="90" height="90"
            style="stroke: none; fill: #00ff00; filter: url(#blurFilter4);" />



       这个例子中,在1d24e586ca31f4bd05eca427459d98c7元素中使用了ad11e455a1398b09cace1648f731f602滤镜。在矩形元素中的style属性中使用filter来指向这个SVG过滤器,得到的结果如下面图像所示:  


      993.png

      模糊的尺寸

        ad11e455a1398b09cace1648f731f602元素的stdDeviation属性决定图像的模糊尺寸大小。它的数值越大,图像的模糊尺寸越大。在下面的例子中分别设置了三个不同的stdDeviation值。

      <defs>
          <filter id="blurFilter5" x="-20" y="-20" width="200" height="200">
              <feGaussianBlur in="SourceGraphic" stdDeviation="2" />
          </filter>
          <filter id="blurFilter6" x="-20" y="-20" width="200" height="200">
              <feGaussianBlur in="SourceGraphic" stdDeviation="6" />
          </filter>
          <filter id="blurFilter7" x="-20" y="-20" width="200" height="200">
              <feGaussianBlur in="SourceGraphic" stdDeviation="12" />
          </filter>
      </defs>
       
      <rect x="20" y="24" width="90" height="90"
            style="stroke: none; fill: #00ff00; filter: url(#blurFilter5);" />
      <rect x="150" y="24" width="90" height="90"
            style="stroke: none; fill: #00ff00; filter: url(#blurFilter6);" />
      <rect x="300" y="24" width="90" height="90"
            style="stroke: none; fill: #00ff00; filter: url(#blurFilter7);" />


       得到的返回结果如下:


      994.png

        通过ALPHA通道进行模糊

        在上面的例子中,过滤器的输入源是SourceGraphic,意思是使用图形的RGB颜色来作为输入源。你也可以使用图形的alpha通道来作为输入源,只需要将ad11e455a1398b09cace1648f731f602元素的in属性设置为SourceAlpha即可。下面是一个例子:

      <defs>
          <filter id="blurFilter8" x="-20" y="-20" width="200" height="200">
              <feGaussianBlur <b>in="SourceAlpha"</b> stdDeviation="10" />
          </filter>
      </defs>
      <rect x="20" y="20" width="90" height="90"
            style="stroke: none; fill: #00ff00; filter: url(#blurFilter8);" />



        得到的返回结果如下:
      995.png
        注意观察,矩形的填充色是绿色的,但是通过alpha通道来进行模糊之后,得到的结果是黑白色的图像。

        偏移滤镜

        偏移滤镜会将输入图形进行移动之后作为结果输出。你可以使用它来上下左右移动图形。通常偏移滤镜都是用于制作drop阴影效果。下面是一个例子:

      <defs>
          <filter id="offsetFilter1" x="-20" y="-20" width="200" height="200">
              <feOffset in="SourceGraphic" dx="80" dy="20" />
          </filter>
      </defs>
      <rect x="20" y="20" width="90" height="90"
            style="stroke: #9a12b3; fill: none; filter: url(#offsetFilter1);" />
      <rect x="20" y="20" width="90" height="90"
            style="stroke: #1f3a93; fill: none; " />


        得到的结果如下所示:

      996.png
        这个例子中定义了两个矩形,它们的尺寸和位置都相同。紫色描边的矩形被应用了偏移滤镜,使它相对于原来的位置向右移动了80个单位,向下移动了20各单位。

        颜色矩阵滤镜

        颜色矩阵滤镜用来在图形的颜色中应用矩阵变换。下面是一个例子:

      <defs>
          <filter id="colorMatrixFilter1" x="-20" y="-20" width="200" height="200">
              <feColorMatrix in="SourceGraphic" type="matrix"
                      values="0 0 0 1 0
                              0 0 0 1 0
                              0 0 0 1 0
                              0 0 0 1 0
                      "/>
          </filter>
      </defs>
      <rect x="20" y="20" width="90" height="90"
            style="stroke: none; fill: #049372; filter: url(#colorMatrixFilter1);" />
      <rect x="150" y="20" width="90" height="90"
                style="stroke: #049372; fill: #049372;" />

      矩阵的值有06ee419339fcea58566d1018aac4be83元素中的values属性提供。这里共有4X5=20个值。在原始图像中它们的值类似下面的样子:

      0 0 0 red   0
      0 0 0 green 0
      0 0 0 blue  0
      0 0 0 1     0



        上面的代码得到的返回结果如下:

      997.png

        注意观察,左边的图形应用了颜色矩阵滤镜,它原来有一个绿色的填充颜色,在使用了颜色矩阵之后只剩下描边了。右边是没有使用颜色矩阵的图形。

        混合(BLEND)滤镜

        混合滤镜可以将多个输入滤镜混合为一个。下面是一个例子:

      <svg width="500" height="100">
        <defs>
          <filter id="blurFilter3" y="-10" height="40" x="-10" width="150">
            <feOffset in="SourceAlpha" dx="3" dy="3" result="offset3" />
            <feGaussianBlur in="offset3" stdDeviation="3"  result="blur3"/>
       
            <feBlend  in="SourceGraphic" in2="blur3" x="-10" width="160"/>
       
          </filter>
        </defs>
       
        <ellipse cx="55" cy="60" rx="25" ry="15"
               style="stroke: none; fill: #1f3a93;
                      filter: url(#blurFilter3);" />
       
      </svg>


      这个例子声明了三个滤镜。第一个偏移滤镜,第二个是高斯模糊滤镜。高斯模糊滤镜的输入是偏移滤镜的输出。第三个4a2de5b5de45a23b60781a758be85295滤镜有两个输入,它将这两个输入进行了混合:

      上面代码得到的结果如下:
      998.png

      以上就是SVG进阶|SVG过滤器(SVG Filters)的内容,更多相关内容请关注PHP中文网(www.php.cn)!


      Statement
      The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
      How to Add Audio to My HTML5 Website?How to Add Audio to My HTML5 Website?Mar 10, 2025 pm 03:01 PM

      This article explains how to embed audio in HTML5 using the <audio> element, including best practices for format selection (MP3, Ogg Vorbis), file optimization, and JavaScript control for playback. It emphasizes using multiple audio f

      How do I use the HTML5 Page Visibility API to detect when a page is visible?How do I use the HTML5 Page Visibility API to detect when a page is visible?Mar 13, 2025 pm 07:51 PM

      The article discusses using the HTML5 Page Visibility API to detect page visibility, improve user experience, and optimize resource usage. Key aspects include pausing media, reducing CPU load, and managing analytics based on visibility changes.

      How to Use HTML5 Forms for User Input?How to Use HTML5 Forms for User Input?Mar 10, 2025 pm 02:59 PM

      This article explains how to create and validate HTML5 forms. It details the <form> element, input types (text, email, number, etc.), and attributes (required, pattern, min, max). The advantages of HTML5 forms over older methods, incl

      How do I use viewport meta tags to control page scaling on mobile devices?How do I use viewport meta tags to control page scaling on mobile devices?Mar 13, 2025 pm 08:00 PM

      The article discusses using viewport meta tags to control page scaling on mobile devices, focusing on settings like width and initial-scale for optimal responsiveness and performance.Character count: 159

      How do I handle user location privacy and permissions with the Geolocation API?How do I handle user location privacy and permissions with the Geolocation API?Mar 18, 2025 pm 02:16 PM

      The article discusses managing user location privacy and permissions using the Geolocation API, emphasizing best practices for requesting permissions, ensuring data security, and complying with privacy laws.

      How to Create Interactive Games with HTML5 and JavaScript?How to Create Interactive Games with HTML5 and JavaScript?Mar 10, 2025 pm 06:34 PM

      This article details creating interactive HTML5 games using JavaScript. It covers game design, HTML structure, CSS styling, JavaScript logic (including event handling and animation), and audio integration. Essential JavaScript libraries (Phaser, Pi

      How do I use the HTML5 WebSockets API for bidirectional communication between client and server?How do I use the HTML5 WebSockets API for bidirectional communication between client and server?Mar 12, 2025 pm 03:20 PM

      This article explains the HTML5 WebSockets API for real-time, bidirectional client-server communication. It details client-side (JavaScript) and server-side (Python/Flask) implementations, addressing challenges like scalability, state management, an

      How do I use the HTML5 Drag and Drop API for interactive user interfaces?How do I use the HTML5 Drag and Drop API for interactive user interfaces?Mar 18, 2025 pm 02:17 PM

      The article explains how to use the HTML5 Drag and Drop API to create interactive user interfaces, detailing steps to make elements draggable, handle key events, and enhance user experience with custom feedback. It also discusses common pitfalls to a

      See all articles

      Hot AI Tools

      Undresser.AI Undress

      Undresser.AI Undress

      AI-powered app for creating realistic nude photos

      AI Clothes Remover

      AI Clothes Remover

      Online AI tool for removing clothes from photos.

      Undress AI Tool

      Undress AI Tool

      Undress images for free

      Clothoff.io

      Clothoff.io

      AI clothes remover

      AI Hentai Generator

      AI Hentai Generator

      Generate AI Hentai for free.

      Hot Article

      R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
      2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
      Repo: How To Revive Teammates
      4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
      Hello Kitty Island Adventure: How To Get Giant Seeds
      4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

      Hot Tools

      MantisBT

      MantisBT

      Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

      SublimeText3 Chinese version

      SublimeText3 Chinese version

      Chinese version, very easy to use

      Dreamweaver CS6

      Dreamweaver CS6

      Visual web development tools

      mPDF

      mPDF

      mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

      DVWA

      DVWA

      Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software