Creating animations with an svg mask can be effective, however, it seems to only work properly

I have been experimenting with creating a transition effect using SVG masks and CSS. Initially, everything was working as intended, but after a few repetitions (usually 1 or 2, unpredictably), the transition effect would suddenly stop and become instantaneous. Upon thorough examination of my code and monitoring for any issues with the generated SVG, I found that everything appeared to be correct and consistent with the initial executions where the effect worked smoothly.

This anomaly occurs in both Firefox and Chrome browsers.

To demonstrate this issue, I have prepared a JSFiddle: https://jsfiddle.net/nrm4fwg0/1/


function lerp(start, end, t) {
  return start * (1 - t) + end * t;
}
const noise = (visible, wasVisible, durationSeconds = 1, seed = 44, liminal = 0.1) => {
  const startOp = lerp(liminal * 2, 1 + liminal * 2, wasVisible?1:0);
  const endOp = lerp(liminal * 2, 1 + liminal * 2, visible?1:0);

  const slope = -1 / (liminal * 2);
  const startIntercept = (startOp - liminal) * -slope;
  const endIntercept = (endOp - liminal) * -slope;


  return `
<svg xmlns="http://www.w3.org/2000/svg" viewport="0 0 100 100" width="100" height="100">
  <defs>
    <filter id="alpha-effect">
      <feFlood  x="0" y="0"
      width="100" height="100"/>
      <feGaussianBlur stdDeviation="3"/>
      <feComponentTransfer result="SMOOTH">
        <feFuncA type="table" tableValues="1 0.7 0.3 0"/>
      </feComponentTransfer>
      <feTurbulence seed="${seed}" result="NOISE" type="fractalNoise" baseFrequency="0.05" numOctaves="5"/>

      <feMerge>
        <feMergeNode in="NOISE"></feMergeNode>
        <feMergeNode in="SMOOTH"></feMergeNode>
    </feMerge>

      <feComponentTransfer>
        <feFuncA type="linear" slope="${slope}" intercept="${endIntercept}">
            <animate
            attributeName="intercept"
            from="${startIntercept}"
            to="${endIntercept}"
            dur="${durationSeconds}s" />
        </feFuncA>
      </feComponentTransfer>
    </filter>
  </defs>
  
  <rect x="0" y="0" width="100" height="100" 
        fill="blue" filter="url(#alpha-effect)"/>
</svg>
`;
};


let isCardVisible = true;
function toggleCard(){
    const card = document.querySelector('#card');
  const mask = noise(!isCardVisible, isCardVisible);
  isCardVisible = !isCardVisible;
 
  
  card.style['mask-image'] = `url(data:image/svg+xml;base64,${btoa(mask)})`;
  card.style['-webkit-mask-image'] = `url(data:image/svg+xml;base64,${btoa(mask)})`;
  card.style['mask-size'] = 'cover';
  card.style['-webkit-mask-size'] = 'cover';
  card.style['mask-repeat'] = 'no-repeat';  
}

document.querySelector('#button').addEventListener('click', toggleCard);

Answer №1

After some thought, I managed to come up with a solution, although I am still unsure of the reasoning behind it. If anyone has any insights on why this issue is occurring, I would greatly appreciate hearing them.

Essentially, I decided to make the ID of the filter random each time:

const id = `alpha-${Math.random()}`; 

Instead of using the fixed alpha-effect as before. This change ensured that the effect continued to work properly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for inserting a button under a div when clicked

I am working on a layout where I have several div cards aligned side by side using the display: inline-block property. What I want to achieve is that when I click on a card, a button is added below the respective div. To accomplish this, I tried using jqu ...

Using Bootstrap 3 with ResponsiveSlides.js

I've been struggling to center the ResponsiveSlides slider within a bootstrap column. Despite resizing my large images to 80% in the CSS, they still refuse to align properly since they're no longer fitting the entire screen at 100%. Can someone l ...

Moving from the center to the bottom-right with a CSS transition

I have a specific requirement where I need the container to cover the entire page and shrink when clicked, with an animation. Currently, I am using CSS transition to animate the container shrinking towards the top: The container shrinks slowly to the sp ...

Check to see if a div element with an id that contains a numerical value has been

My HTML code contains X elements, each with an ID in the format: viewer_mX In this case, X is a number ranging from 1 to m (where m varies). I am looking to utilize JavaScript to retrieve the respective element's number X when a user clicks on one ...

What causes the Material-UI Grid element to shift upon clicking it?

I have encountered an issue while developing a React app with Material UI. The problem arises on a specific page of the application. This particular page consists of text and a button aligned vertically, along with a second set of text and another button ...

Arranging Images with CSS Overlapping

I am striving to make the blue block and red block scale proportionally with the webpage, while also maintaining their position without any shifting up or down. As depicted in this gif, the blocks somewhat accomplish what I desire when scaling diagonally, ...

Creating a stylish horizontal divider with circular accents at either end

Can someone help me create a border similar to the one shown in this image? I've attempted using the :after and :before CSS attributes, but without success. The HTML element is an h1 that requires a border at the bottom. Is it achievable? ...

Ensuring form input validity in real-time using jQuery's keyup event

I've been working on a form where the user can fill in an input and press enter to move to the next field. Although I have managed to get the functionality to show the next div, I am facing issues with validation... // Moving to next div on Enter ...

I'm having trouble with my sticky position in the code. I tried setting it to the left side, but it's not behaving as

While I am delving into Bootstrap, HTML, and CSS to enhance my skills, I have hit a snag. My intention was to make the sidebar sticky but it seems to stubbornly stick to the left instead. Despite attempting to apply "position: sticky" and setting "left: 0" ...

jquery kwicks problem

I have been grappling with a coding problem for hours on end and I have hit a wall. Assistance is needed. On a staging page, the code was tested and found to be functioning properly. However, on the live page, the same code fails to respond as expected. I ...

Having issues with Angular Material, specifically with mat-list-item and routerLinkActive not functioning as expected

Currently, I am working with a navigation view that utilizes the MatSidenavModule. The issue I am encountering is on mobile screens. When I click a mat-list-item, the mat-sidenav closes as expected. However, upon opening the mat-sidenav again, Material alw ...

Tips on using the Hover property in CSS for an element that includes both :after and :before properties

I've created a hexagon using CSS after and before properties, and now I'm attempting to add a glowing effect around the hexagon when hovering. It works perfectly for the center of the hexagon but not for the top and bottom points of the shape. ...

How to Align col-md-5 in the Center Using Bootstrap?

Is anyone else struggling with this dilemma or is it just me? I've been searching everywhere for a solution to my problem but I can't seem to find one. According to the grid system, if you have a column size of col-md-5, centering it should be ...

a tag's functionality remains unchanged by its class association

In my Laravel project, I am attempting to create an active class for a link tag, but the class is not affecting the link. Below is the code snippet from my blade file: <li class="{{ (request()->is('categories*')) ? 'side-active&ap ...

When the text exceeds its container, the CSS property overflow will display only the

.example{ overflow:auto; } <div class="example"> <p>Some Additional Text Here<P> </div> This particular code snippet showcases the initial portion of the text, allowing for scrolling down to reveal items 5 and 6: 1 2 ...

Tips for creating responsive scrollable columns in an HTML table with Bootstrap

I'm not a professional web designer. I attempted to create a web layout with scrollable columns containing data in anchor tags that are loaded dynamically. To achieve this, I created an HTML table structure along with a stylesheet. Here is the source ...

Issue with rollover button function in Firefox and Internet Explorer when attempting to submit

I have created a rollover submit button using HTML and JavaScript: <input type="image" id="join" name="join" src="images/join.png" value="join"> My JS code implements the rollover/hover effect: <script type="text/javascript" charset="utf-8"> ...

Sloped Divider on the Upper Edge of the Page

I'm currently in the process of developing a new website, and I'm looking to create a unique design for the main navigation bar on the homepage. Here is the ideal layout that I have in mind: https://i.stack.imgur.com/pc8z4.png While I understan ...

What should I designate as the selector when customizing dialog boxes?

I am attempting to change the title bar color of a dialog box in CSS, but I am running into issues. Below is the HTML code for the dialog box and the corresponding CSS. <div id="picture1Dialog" title = "Title"> <p id="picture1Text"> ...

When you have a target element that is deeply nested, consider using a 50% height relative to

There is some confusion surrounding the issue below: <div class="container"> <div class="row"> <div class="group"> <a> <div class="col-lg-3 full-h"></div> </a> <a> < ...