【问题标题】:Scale SVG clipping mask without scaling the clipped element缩放 SVG 剪切蒙版而不缩放被剪切的元素
【发布时间】:2021-04-07 17:16:00
【问题描述】:

我有一个 SVG 作为 div 元素的剪贴蒙版。 div 是一个图像。我想放大(和旋转)SVG 剪贴蒙版,同时使图像 div 保持不动以使用 gsap 动画实现显示效果

我知道我正在向上缩放整个 div,但我不知道如何缩放 SVG 剪辑路径。那么如何放大仅放大 SVG 剪贴蒙版,而不放大图像 div?

.red {
  clip-path: url(#myClip);
  width:300px;
  height:300px;
  background-color: red;
  /*background-image: url(./img/project.jpg);*/
  background-size: cover;
  position:absolute;
}

.red:hover {
    transform: scale(1.5) rotate(-45deg);
}
<svg height="0" width="0">            
    <clipPath id="myClip" clipPathUnits="objectBoundingBox" transform="scale(0.003)" > 
        <path xmlns="http://www.w3.org/2000/svg" class="cls-2" d="M208.5,3.5c-61-4-125,31-141,94-5,19-5,39,4,57-10-48,21-95,64-116,31-15,66-17,99-7,30,9,59,26,77,53-31-33-75-55-121-51-36,3-73,24-86,59,18-27,47-42,79-44a130,130,0,0,1,104,43c31,36,47,85,35,131,10-65-25-138-93-155-19-5-39-5-57,4,48-10,95,21,116,64,15,31,17,66,7,99-9,30-26,59-53,77,33-31,55-75,51-121-3-36-24-73-59-86,27,18,42,47,44,79a130,130,0,0,1-43,104c-36,31-85,47-131,35,65,10,138-25,155-93,5-19,5-39-4-57,10,48-21,95-64,116-31,15-66,17-99,7-30-9-59-26-77-53,31,33,75,55,121,51,36-3,73-24,86-59-18,27-47,42-79,44a130,130,0,0,1-104-43c-31-36-47-85-35-131-10,65,25,138,93,155,19,5,39,5,57-4-48,10-95-21-116-64-15-31-17-66-7-99,9-30,26-59,53-77-33,31-55,75-51,121,3,36,24,73,59,86-27-18-42-47-44-79a130,130,0,0,1,43-104c36-31,85-47,131-35a66,66,0,0,1-14-1Z"/>
    </clipPath>
</svg>
        
<div href="test.html" class="red">
  <h1>Content Content</h1>
  <h1>Content Content</h1>
  <h1>Content Content</h1>
</div>

【问题讨论】:

    标签: css svg clip-path


    【解决方案1】:

    svg 放在div 之后并缩放path 而不是自身。

    .red {
      clip-path: url(#myClip);
      width: 300px;
      height: 300px;
      background-color: red;
      /*background-image: url(./img/project.jpg);*/
      background-size: cover;
      position: absolute;
    }
    
    svg path {
      transform-box: fill-box;
      transform-origin: center;
    }
    
    .red:hover~svg path {
      transform: scale(1.5);
    }
    <div href="test.html" class="red">
      <h1>Content Content</h1>
      <h1>Content Content</h1>
      <h1>Content Content</h1>
    </div>
    
    <svg height="0" width="0">
                <clipPath id="myClip" clipPathUnits="objectBoundingBox" transform="scale(0.003)" > 
                    <path xmlns="http://www.w3.org/2000/svg" class="cls-2" d="M208.5,3.5c-61-4-125,31-141,94-5,19-5,39,4,57-10-48,21-95,64-116,31-15,66-17,99-7,30,9,59,26,77,53-31-33-75-55-121-51-36,3-73,24-86,59,18-27,47-42,79-44a130,130,0,0,1,104,43c31,36,47,85,35,131,10-65-25-138-93-155-19-5-39-5-57,4,48-10,95,21,116,64,15,31,17,66,7,99-9,30-26,59-53,77,33-31,55-75,51-121-3-36-24-73-59-86,27,18,42,47,44,79a130,130,0,0,1-43,104c-36,31-85,47-131,35,65,10,138-25,155-93,5-19,5-39-4-57,10,48-21,95-64,116-31,15-66,17-99,7-30-9-59-26-77-53,31,33,75,55,121,51,36-3,73-24,86-59-18,27-47,42-79,44a130,130,0,0,1-104-43c-31-36-47-85-35-131-10,65,25,138,93,155,19,5,39,5,57-4-48,10-95-21-116-64-15-31-17-66-7-99,9-30,26-59,53-77-33,31-55,75-51,121,3,36,24,73,59,86-27-18-42-47-44-79a130,130,0,0,1,43-104c36-31,85-47,131-35a66,66,0,0,1-14-1Z"/>
                </clipPath>
            </svg>

    【讨论】:

    • 谢谢!这解决了这个问题。虽然后来我必须删除 clipPathUnits 才能调整 div 上的剪辑路径位置。我在 svg path {} 上使用 transform: translate(...) 来定位剪辑路径,但它固定在屏幕上而不是响应式定位。你能建议如何有效地定位剪辑路径吗?
    • 我不明白你的意思。你可以再问一个更详细的问题。
    【解决方案2】:

    考虑添加旋转动画的不同 SVG:

    <svg viewBox="-20 -20 360 360">
     <path d="M208.5,3.5c-61-4-125,31-141,94-5,19-5,39,4,57-10-48,21-95,64-116,31-15,66-17,99-7,30,9,59,26,77,53-31-33-75-55-121-51-36,3-73,24-86,59,18-27,47-42,79-44a130,130,0,0,1,104,43c31,36,47,85,35,131,10-65-25-138-93-155-19-5-39-5-57,4,48-10,95,21,116,64,15,31,17,66,7,99-9,30-26,59-53,77,33-31,55-75,51-121-3-36-24-73-59-86,27,18,42,47,44,79a130,130,0,0,1-43,104c-36,31-85,47-131,35,65,10,138-25,155-93,5-19,5-39-4-57,10,48-21,95-64,116-31,15-66,17-99,7-30-9-59-26-77-53,31,33,75,55,121,51,36-3,73-24,86-59-18,27-47,42-79,44a130,130,0,0,1-104-43c-31-36-47-85-35-131-10,65,25,138,93,155,19,5,39,5,57-4-48,10-95-21-116-64-15-31-17-66-7-99,9-30,26-59,53-77-33,31-55,75-51,121,3,36,24,73,59,86-27-18-42-47-44-79a130,130,0,0,1,43-104c36-31,85-47,131-35a66,66,0,0,1-14-1Z">
       <animateTransform attributeName="transform" attributeType="XML"  type="rotate" from="0 160 160" to="360 160 160" dur="5s" repeatCount="indefinite"/>
      </path>
    </svg>

    然后将其应用为蒙版,您可以轻松调整所需的大小:

    document.querySelector('button').addEventListener('click',function() {
         document.querySelector('.red').classList.toggle('active');
    })
    .red {
      -webkit-mask:url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="-20 -20 360 360"><path xmlns="http://www.w3.org/2000/svg" class="cls-2" d="M208.5,3.5c-61-4-125,31-141,94-5,19-5,39,4,57-10-48,21-95,64-116,31-15,66-17,99-7,30,9,59,26,77,53-31-33-75-55-121-51-36,3-73,24-86,59,18-27,47-42,79-44a130,130,0,0,1,104,43c31,36,47,85,35,131,10-65-25-138-93-155-19-5-39-5-57,4,48-10,95,21,116,64,15,31,17,66,7,99-9,30-26,59-53,77,33-31,55-75,51-121-3-36-24-73-59-86,27,18,42,47,44,79a130,130,0,0,1-43,104c-36,31-85,47-131,35,65,10,138-25,155-93,5-19,5-39-4-57,10,48-21,95-64,116-31,15-66,17-99,7-30-9-59-26-77-53,31,33,75,55,121,51,36-3,73-24,86-59-18,27-47,42-79,44a130,130,0,0,1-104-43c-31-36-47-85-35-131-10,65,25,138,93,155,19,5,39,5,57-4-48,10-95-21-116-64-15-31-17-66-7-99,9-30,26-59,53-77-33,31-55,75-51,121,3,36,24,73,59,86-27-18-42-47-44-79a130,130,0,0,1,43-104c36-31,85-47,131-35a66,66,0,0,1-14-1Z"><animateTransform attributeName="transform" attributeType="XML"  type="rotate" from="0 160 160" to="360 160 160" dur="1.5s" repeatCount="indefinite"/></path></svg>') center no-repeat;
      -webkit-mask-size:0% 0%;
      width: 300px;
      height: 300px;
      background-color: red;
      transition:1s linear;
    }
    .active {
      -webkit-mask-size:280% 280%;
    }
    <button>
    click me
    </button>
    <div href="test.html" class="red">
      <h1>Content Content</h1>
      <h1>Content Content</h1>
      <h1>Content Content</h1>
    </div>

    如果您想在点击时运行旋转,请尝试如下:

    document.querySelector('button').addEventListener('click', function() {
      document.querySelector('.red').classList.toggle('active');
    })
    .red {
      -webkit-mask: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="-20 -20 360 360"><path xmlns="http://www.w3.org/2000/svg" class="cls-2" d="M208.5,3.5c-61-4-125,31-141,94-5,19-5,39,4,57-10-48,21-95,64-116,31-15,66-17,99-7,30,9,59,26,77,53-31-33-75-55-121-51-36,3-73,24-86,59,18-27,47-42,79-44a130,130,0,0,1,104,43c31,36,47,85,35,131,10-65-25-138-93-155-19-5-39-5-57,4,48-10,95,21,116,64,15,31,17,66,7,99-9,30-26,59-53,77,33-31,55-75,51-121-3-36-24-73-59-86,27,18,42,47,44,79a130,130,0,0,1-43,104c-36,31-85,47-131,35,65,10,138-25,155-93,5-19,5-39-4-57,10,48-21,95-64,116-31,15-66,17-99,7-30-9-59-26-77-53,31,33,75,55,121,51,36-3,73-24,86-59-18,27-47,42-79,44a130,130,0,0,1-104-43c-31-36-47-85-35-131-10,65,25,138,93,155,19,5,39,5,57-4-48,10-95-21-116-64-15-31-17-66-7-99,9-30,26-59,53-77-33,31-55,75-51,121,3,36,24,73,59,86-27-18-42-47-44-79a130,130,0,0,1,43-104c36-31,85-47,131-35a66,66,0,0,1-14-1Z"></path></svg>') center no-repeat;
      -webkit-mask-size: 20% 20%;
      width: 300px;
      height: 300px;
      background-color: red;
      transition: -webkit-mask-size 1s linear;
    }
    
    .active {
      -webkit-mask: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="-20 -20 360 360"><path xmlns="http://www.w3.org/2000/svg" class="cls-2" d="M208.5,3.5c-61-4-125,31-141,94-5,19-5,39,4,57-10-48,21-95,64-116,31-15,66-17,99-7,30,9,59,26,77,53-31-33-75-55-121-51-36,3-73,24-86,59,18-27,47-42,79-44a130,130,0,0,1,104,43c31,36,47,85,35,131,10-65-25-138-93-155-19-5-39-5-57,4,48-10,95,21,116,64,15,31,17,66,7,99-9,30-26,59-53,77,33-31,55-75,51-121-3-36-24-73-59-86,27,18,42,47,44,79a130,130,0,0,1-43,104c-36,31-85,47-131,35,65,10,138-25,155-93,5-19,5-39-4-57,10,48-21,95-64,116-31,15-66,17-99,7-30-9-59-26-77-53,31,33,75,55,121,51,36-3,73-24,86-59-18,27-47,42-79,44a130,130,0,0,1-104-43c-31-36-47-85-35-131-10,65,25,138,93,155,19,5,39,5,57-4-48,10-95-21-116-64-15-31-17-66-7-99,9-30,26-59,53-77-33,31-55,75-51,121,3,36,24,73,59,86-27-18-42-47-44-79a130,130,0,0,1,43-104c36-31,85-47,131-35a66,66,0,0,1-14-1Z"><animateTransform attributeName="transform" attributeType="XML"  type="rotate" from="0 160 160" to="360 160 160" dur="1.5s" repeatCount="indefinite"/></path></svg>') center no-repeat;
      -webkit-mask-size: 280% 280%;
    }
    <button>
    click me
    </button>
    <div href="test.html" class="red">
      <h1>Content Content</h1>
      <h1>Content Content</h1>
      <h1>Content Content</h1>
    </div>

    【讨论】:

    • 我想让它在我使用 gsap 点击 div 内容时旋转。这个旋转动画是无限运行的吗?
    • @Hi3uXD 我给你一个更简单的方法,你不需要 Gsap 或任何外部工具。您可以在单击时使用 JS 简单地添加一个类,然后在该类中定义您想要的动画。我做了一个例子来说明这个技巧,但你可以轻松地调整你想要的
    • @Hi3uXD 这是一个基本示例:jsfiddle.net/7zj8qcf4
    • 我明白了!我能不能让你的示例中的 svg 在开始时保持静止,然后在点击时放大 + 旋转?
    • @Hi3uXD 检查更新,添加 2 个示例
    猜你喜欢
    • 2018-07-18
    • 2011-10-20
    • 2015-02-27
    • 2012-03-22
    • 2021-07-31
    • 1970-01-01
    • 2015-01-21
    • 2021-11-26
    • 2014-11-12
    相关资源
    最近更新 更多