Add a Light Source with CSS and the fePointLight SVG Filter

Rodrigo Pombo
InstructorRodrigo Pombo
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

The CSS filter property lets us apply the power of SVG filter effects to HTML elements. One little-known effect included in modern browsers is <fePointLight>. We'll use it together with <feDiffuseLighting> to add a light source to an <h1 /> element.

Instructor: [0:00] We can use the filter CSS property to change how an element looks. For example, we can blur the element or add a shadow. One special filter we can use is the URL function. It takes an ID as an argument. That's the idea of a special filter.

[0:19] Somewhere on the HTML, we need an SVG element with a filter, and that filter should have the ID from the CSS. Inside the filter, we need to add effects. We can have a basic effect like a blur, or we can have something very complex combining several effects. We're going to focus on the point light effect.

[0:44] First, it doesn't seem to do too much. Let's see what happens when we change the set parameter. We have a light source at the top left of the h1. We moved it closer to the viewer, away from the web page.

[1:01] The diffuse lighting effect is taking the h1's alpha channel and using it as a bump map. That means that the opaque part of the h1 detects, and border are higher than the background, which is transparent, so the browser is taking the height map and the position of the light source to calculate the output.

[1:24] We can change the height of the opaque part using the surface scale. Now the letters and the border are closer to the viewer. We can also change the light color, and we can move the light source to a different position. Here, we are using pixels. We can change the primitiveUnits property of the filter to offset bounding box. Now, instead of pixels, we can use fractions of the h1 size.

egghead
egghead
~ 4 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today