Home > database >  Animating distantLight filter with svg.js
Animating distantLight filter with svg.js

Time:03-12

I am attempting to create a simple SVG animation using SVG.js. My desired result would be equivalent to:

<filter">
  <feDiffuseLighting result="diffOut" in="SourceGraphic" diffuseConstant="1.2"
    lighting-color="white">
    <feDistantLight azimuth="240" elevation="100">
        <animate attributeName="elevation"
            values="100; 20" dur="7s"
         />
     </feDistantLight>
  </feDiffuseLighting>
  <feComposite in="SourceGraphic" in2="diffOut" operator="arithmetic"
     result="diffPointOut" k1="1" k2="0" k3="0" k4="0" />
</filter>

As of now I was able to replicate everything but the animation via using svg.js and svg.filter.js:

foreground.filterWith(function (filter) {
    diff = filter.diffuseLighting().attr({
        'lighting-color': 'white',
        'diffuseConstant': 1.2
    });
    dLight = SVG('<feDistantLight azimuth="240" elevation="100"/>');
    diff.add(dLight);

    filter.composite(filter.source, diff)
        .attr({
            operator: 'arithmetic',
            k1: 1, k2: 0, k3: 0, k4: 0
        });
});

This snippet of code generates in resulting DOM the exact same filter without the animation To add an animation I've attempted to use:

 dLight.animate(7000, 0, 'now').attr({ "elevation": 20 });

But it causes an error declaring, that 'dLight' has no function named 'animate'. If I try to create my own svg.js animation runner and pass dLight to it, it would cause a similar error, declaring, that it doesn't have a '_prepareRunner' function.

On the other hand I can animate any attribute of 'diff' - the diffuseLight filter just fine. Which means my trouble is probably caused by a way I used to create distantLightFilter via SVG(...) method, but I found no other method to create it, since as according to last post here: https://github.com/svgdotjs/svg.filter.js/issues/18 the svg.filter.js does not provide a constructor for it anymore.
I am looking for help in either adding an animation to result of SVG(...) call or creating a distantLight filter in more proper animatable way.

CodePudding user response:

According to this comment there is actually a constructor for a distantLight (and other types of light) filter inside the diffuseLight filter class. When initialised via those constructors distantLight filter does support animations properly. All credit for this answer goes to Fuzzyma.

  • Related