如何在不旋转图像的情况下旋转剪辑路径?

7
我有一个带有 CSS 属性 clip-path 的图像,并添加了旋转 clip-path 的动画,我只想旋转 clip-path 而不是图像。从下面的代码中,您可以了解我想要实现的内容。但问题在于,我的代码需要手动设置每个关键帧上的 clip-path 点,这需要很长时间。是否有简短的方法可以实现下面的代码结果,而不需要手动更改关键帧上的点呢?我希望它能够平滑运行,手动设置点非常困难。(请注意,我不需要最后使图像不可见的动画,我无法弄清楚为什么会出现这种情况。)

#profile-img {
    width: 15%;
    margin: 5%;
    -webkit-clip-path: polygon(20% 0%, 0% 20%, 30% 50%, 0% 80%, 20% 100%, 50% 70%, 80% 100%, 100% 80%, 70% 50%, 100% 20%, 80% 0%, 50% 30%);
    clip-path: polygon(20% 0%, 0% 20%, 30% 50%, 0% 80%, 20% 100%, 50% 70%, 80% 100%, 100% 80%, 70% 50%, 100% 20%, 80% 0%, 50% 30%);
    animation: clipRotateAnim 2s linear infinite;
}

@keyframes clipRotateAnim {
    0% {
        -webkit-clip-path: polygon(20% 0%, 0% 20%, 30% 50%, 0% 80%, 20% 100%, 50% 70%, 80% 100%, 100% 80%, 70% 50%, 100% 20%, 80% 0%, 50% 30%);
        clip-path: polygon(20% 0%, 0% 20%, 30% 50%, 0% 80%, 20% 100%, 50% 70%, 80% 100%, 100% 80%, 70% 50%, 100% 20%, 80% 0%, 50% 30%);
    }
    
    25% {
        -webkit-clip-path: polygon(100% 23%, 80% 0, 47% 34%, 16% 0, 0 19%, 26% 53%, 0 78%, 19% 100%, 51% 71%, 76% 100%, 100% 81%, 68% 51%);
        clip-path: polygon(100% 23%, 80% 0, 47% 34%, 16% 0, 0 19%, 26% 53%, 0 78%, 19% 100%, 51% 71%, 76% 100%, 100% 81%, 68% 51%);
    }
    50% {
        -webkit-clip-path: polygon(84% 100%, 100% 75%, 64% 56%, 100% 13%, 81% 0, 49% 28%, 22% 0, 0 29%, 28% 57%, 0 83%, 21% 100%, 42% 74%);
        clip-path: polygon(84% 100%, 100% 75%, 64% 56%, 100% 13%, 81% 0, 49% 28%, 22% 0, 0 29%, 28% 57%, 0 83%, 21% 100%, 42% 74%);
    }
    100% {
        -webkit-clip-path: polygon(27% 0, 0 19%, 29% 49%, 0 79%, 19% 100%, 45% 76%, 84% 100%, 100% 80%, 69% 56%, 100% 18%, 80% 0, 47% 33%);
        clip-path: polygon(27% 0, 0 19%, 29% 49%, 0 79%, 19% 100%, 45% 76%, 84% 100%, 100% 80%, 69% 56%, 100% 18%, 80% 0, 47% 33%);

    }
 
}
<img id="profile-img" src="https://images.pexels.com/photos/1025804/pexels-photo-1025804.jpeg?auto=compress&cs=tinysrgb&h=350">

1个回答

11

将图像用作伪元素的背景,并将其向相反方向旋转:

.image {
  width: 200px;
  height: 200px;
  margin: 20px;
  clip-path: polygon(20% 0%, 0% 20%, 30% 50%, 0% 80%, 20% 100%, 50% 70%, 80% 100%, 100% 80%, 70% 50%, 100% 20%, 80% 0%, 50% 30%);
  animation: clipRotateAnim 2s linear infinite;
  position: relative;
  overflow: hidden;
}

.image:before {
  content: "";
  position: absolute;
  inset: -10%;
  background: var(--i) center/cover;
  animation: inherit ;
  animation-direction:reverse;
}

@keyframes clipRotateAnim {
  to {
    transform: rotate(360deg)
  }
}
<div class="image" style="--i:url(https://images.pexels.com/photos/1025804/pexels-photo-1025804.jpeg?auto=compress&cs=tinysrgb&h=350)">
</div>

另一个提高性能的想法是使用背景创建图像之上的另一层,然后旋转该层。

.image {
   width:200px;
   height:200px;
   margin: 20px;
   position:relative;
   background:var(--i) center/cover;
   clip-path: inset(1px);
}
.image:before {
  content:"";
  position:absolute;
  inset: 5px;
  box-shadow:0 0 0 200px #fff;
  background: conic-gradient(at 140px 140px,#0000 75%,#fff 0) -70px -70px;
  animation: clipRotateAnim 2s linear infinite;
}
@keyframes clipRotateAnim{
  to{transform:rotate(360deg)}
}
<div class="image"  style="--i:url(https://images.pexels.com/photos/1025804/pexels-photo-1025804.jpeg?auto=compress&cs=tinysrgb&h=350)">
  
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接