ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

A HTML 5 Tutorial: Turn Your Web Site into a Dazzling One With SVG Special Effects

Updated on May 7, 2014

SVG Filters

A developer can use SVG filters to add some special effects to their web pages. We will attempt to demonstrate some of them here.Since filters can be combined with other filters the tutorial try to cover the major one and the underlying framework for their use. With that accomplished it should be relatively easy to experiment the other that you might find in reference articles.

The SVG Filters Currently Available

  • feBlend - filter for combining images
  • feColorMatrix - filter for color transforms
  • feComponentTransfer
  • feComposite
  • feConvolveMatrix
  • feDiffuseLighting
  • feDisplacementMap
  • feFlood
  • feGaussianBlur - is a means to create an indistinct image
  • feImage
  • feMerge
  • feMorphology
  • feOffset - filter for drop shadows
  • feSpecularLighting
  • feTile
  • feTurbulence
  • feDistantLight - filter for lighting
  • fePointLight - filter for lighting
  • feSpotLight - filter for lighting

The Basic Structure of a Program Using SVG Filters

There are only a few rules you need to remember in order to apply filters. All filters are nested in <defs>, definition HTML element tags. There should be a corresponding closing to tag to match the opening <defs> tag.

The <filter> tags which are nested in the <defs> tag have one requirement. Thet must have an id attribute which links a graphic to a corresponding filter.

So, we have something like this:

<svg height= 200px width= 200px>
<defs>
<filter id= must have a value >
<name of the filter to be associated with the filter id>
......possibly more filters.......
</filter>
</defs>
.......one (or more statements in regard to the type of shape: circle, rectangle........with a filter attribute which matches the id value in the <defs> section.
</svg>




The <feTile> Special Effects Element

The feTile Element has the following properties:

  • height and width
  • x and y coordinates
  • result, a reference for the filter ouput result
  • in identifies the input to the filter.

What you get is a tiling effect in the rectangle defined. The number of copies created is dependent on the filter rectangle are and the size of the image.

Tiling an Image With <feTile>

Repeating an image or texture as a banner line can be quite effective if not over done.
Repeating an image or texture as a banner line can be quite effective if not over done.

Filling a Rectangle Using the <feflood> Tags

The SVG <feflood> element can be used to create a rectangle. It can have a number of attributes which are common to a number of filter definitions. The Basic tag is as follows:

<feFlood flood-color="colorvalue" flood-opacity="opacityvalue"/>


Generally, this is expanded to include:

  • x and y coordinates
  • height and width
  • and a result value

along with

  • flood-color
  • flood-opacity

If any of the attributes are not specified,inherited values are used.


A Simple <feflood> Example

In this example we see the definition of the filter with a required id. The feflood element provides the details on color, opacity, size, and location. This is merely a definition. The use statement makes the "magic" happen.
In this example we see the definition of the filter with a required id. The feflood element provides the details on color, opacity, size, and location. This is merely a definition. The use statement makes the "magic" happen.

The <feGaussianBlur> Special Effect Element

The snapshot below illustrates the use of the Gaussian blur effect. Note the connection between the id in the <defs> section and the use section creating a Gaussian blur for a circle.


The <feGaussianBlur> requires only two attributes: the id, as mentioned above, and a stdDeviation attribute. A zero would indicate no blur. Higher numbers indicate more blur and at some point the image disappears.

The in attribute defines the space you wish to apply to filter to. In the snapshot we specified SourceGraphic, this would be the entire shape. Other options would include SourceAlpha or BackgroundImage.



Example of a Gaussian Blur on a Circle

Attribute 'stdDeviation' determines the depth of the blur. The higher the number, the more the image blurs. The in attribute states what space to you want to apply the filter to.
Attribute 'stdDeviation' determines the depth of the blur. The higher the number, the more the image blurs. The in attribute states what space to you want to apply the filter to.

Using Lighting Effects in SVG

The next four sections look at potential light effect. In the first example, we just draw a circle with no special lighting specification. The would be equivalent to the drawn circle in my previous tutorial.


No Special Lighting Effects Here

The circle was just drawn and filled with the color blue.
The circle was just drawn and filled with the color blue.

The Basic Outline for Lighting Effects

In the next three examples the following code skeleton is used:

<filter id="some unique filter name">


Followed by referencing feDiffuseLighting element

<feDiffuseLighting in="SourceGraphic" result="a chosen name"
 lighting-color="color">


This is followed by ONE OF THE FOLLOWING THREE STATEMENTS:

 <feDistantLight azimuth="some numeric value" elevation="some numeric
 value"/>


OR


<fePointLight x="coordinate value" y="coordinate value" z="coordinate


 value" />

OR


<feSpotLight x="some numeric value" y="numeric value" z="numeric value"


 limitingConeAngle="numeric value"
 pointsAtX=numeric value" pointsAtY="numeric value"


 pointsAtZ="numeric value"/>


The closing tag for follows:

</feDiffuseLighting>
<feComposite in="SourceGraphic" in2="light"
 operator="arithmetic" k1="numeric" k2="numeric" k3="numeric" k4="numeric"/>

 </filter>

And then the circle is drawn:

<circle cx="x coordinate" cy="y coordinate" r="radius" fill="fill color" filter="url(#unique id)" />


Finally the svg section end tag is added.

With <fePointLight> There is Some Shading

Shading is visible when we used the fePointLight element.
Shading is visible when we used the fePointLight element.

<feDistantLight> Is Darker Than No Light

The image is darker when feDistantLight is invoked.
The image is darker when feDistantLight is invoked.

<feSportLight> A Noticeable Change

With feSportLight the image has shows a distinct difference.
With feSportLight the image has shows a distinct difference.

Using the <feComposite> Element

This I have found to be a very sketchy point. It is used to combine two images. The values for k1 through k4 can be set to values between zero and one. It opertaes differently with what SVG element define the images at. I have found it to be something you just have to experiment with.

There Are A Lot Of Clever Thing That Can Be Done With SVG

It takes patience to do some things in SVG. At times however using it in conjunction with real images can be advantageous.
It takes patience to do some things in SVG. At times however using it in conjunction with real images can be advantageous.

Wrap Up and What's Next

With these fe SVG elements if you have never used them before is probably "enough on your plate for now. In the next tutorial I plan on offering some more information on creating shadows, blurs, and gradient..

Well that's all for today! Go create some SVG images.

Was this tutorial helpful?

Cast your vote for Please rate this tutorial. Also add commentsr comments

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)