html - 带有 CSS 过渡的 SVG 过滤器

标签 html css animation svg css-transitions

我正在做 svg 过滤器和 css 动画。我用

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <defs>
    <filter id="goo">
      <feGaussianBlur in="SourceGraphic" stdDeviation="10" result="blur" />
      <feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0  0 1 0 0 0  0 0 1 0 0  0 0 0 18 -7" result="goo" />
      <feComposite in="SourceGraphic" in2="goo" operator="atop"/>
   </filter>
 </defs>
</svg>

filter:url('#goo');

用于 CSS 中的容器。

这是一个 fiddle https://codepen.io/sergey_mell/pen/MoRMwR

我陷入了下一期。动画似乎在某个盒子内执行(它的大小似乎取决于初始动画状态大小)。 谁能帮我避免这种情况?

最佳答案

SVG 滤镜有一个定义的“滤镜区域”,在其中应用效果。这是因为某些操作可能非常慢(例如高斯模糊),并且您通常希望限制计算它们的区域。

过滤器的默认过滤区域是:

x="-10%" y="-10%" width="120%" height="120%"

换句话说,被过滤的元素,加上它周围 10% 的边框。该区域之外的任何内容都将被剪裁(并且不可见)。

解决方案是增加过滤区域,使其包含所有 Blob 。因此,例如,如果我们将 margin 增加到 50%

<filter id="goo" x="-50%" y="-50%" width="200%" height="200%">

它现在可以正常工作了。

body{
  background:white;
  background-image:url(https://i.imgur.com/d47ZIU3.jpg);
  background-size:cover;
}
.blobs{
  filter:url('#goo');
  position:absolute;
  top:100px;
  left:200px;
}

@keyframes blob-left-top-anim{
  0%{
    transform:scale(1.1) translate(0,0);
  }
  33%{
    transform:scale(0.9) translate(-65px,0);
  }
  62%{
    transform:scale(0.7) translate(-65px,-65px);

  }
  94%{
    transform:scale(1.1) translate(0,0);
  }
}

@keyframes blob-right-top-anim{
  0%{
    transform:scale(1.1) translate(0,0);
  }
  33%{
    transform:scale(0.9) translate(65px,0);
  }
  64%{
    transform:scale(0.7) translate(65px,-65px);
  }
  96%{
    transform:scale(1.1) translate(0,0);
  }
}
@keyframes blob-left-bottom-anim{
  0%{
    transform:scale(1.1) translate(0,0);
  }
  33%{
    transform:scale(0.9) translate(-65px,0);
  }
  66%{
    transform:scale(0.7) translate(-65px,65px);
  }
  98%{
    transform:scale(1.1) translate(0,0);
  }
}

@keyframes blob-right-bottom-anim{
  0%{
    transform:scale(1.1) translate(0,0);
  }
  33%{
    transform:scale(0.9) translate(65px,0);
  }
  68%{
    transform:scale(0.7) translate(65px,65px);
  }
  100%{
    transform:scale(1.1) translate(0,0);
  }
}
.blob{
  position:absolute;
  background:#e97b7a;
  left:50%;
  top:50%;
  width:100px;
  height:100px;
  line-height:100px;
  text-align:center;
  color:white;
  font-size:40px;
  border-radius:100%;
  margin-top:-50px;
  margin-left:-50px;
  animation:blob-left-top-anim cubic-bezier(0.770, 0.000, 0.175, 1.000) 4s infinite;
}

  
.blob:nth-child(2){
  animation-name:blob-right-top-anim;
}
.blob:nth-child(3){
  animation-name:blob-left-bottom-anim;
}
.blob:nth-child(4){
  animation-name:blob-right-bottom-anim;
}
<div class="blobs">
  <div class="blob">4</div>
  <div class="blob">3</div>
  <div class="blob">2</div>
  <div class="blob">1</div>
</div>

<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  <defs>
    <filter id="goo" x="-50%" y="-50%" width="200%" height="200%">
      <feGaussianBlur in="SourceGraphic" stdDeviation="10" result="blur" />
      <feColorMatrix in="blur" mode="matrix" values="1 0 0 0 0  0 1 0 0 0  0 0 1 0 0  0 0 0 18 -7" result="goo" />
      <feComposite in="SourceGraphic" in2="goo" operator="atop"/>
  	</filter>
  </defs>
</svg>

关于html - 带有 CSS 过渡的 SVG 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45146779/

相关文章:

javascript - 如何使用 Javascript 以外的语言操作 DOM?

javascript - $sce.trustAsHtml 不起作用?

javascript - 在单击标签滚动期间需要修复表格

html - 获取背景图像以使用背景完全调整大小

javascript - 如何使用 JavaScript 测量动画发生的时间

javascript - maxHeight 的 jQuery 动画

html - 绝对元素不放在相对元素之上

css - 样式标签中的源映射是否有效?

javascript - 有问题的 JQuery 悬停动画

javascript - 在没有提交按钮的情况下提交最接近的表单?