首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSS clip-path不能使用path()拉伸

CSS clip-path不能使用path()拉伸
EN

Stack Overflow用户
提问于 2021-11-04 17:55:40
回答 1查看 258关注 0票数 4

我正在尝试使用具有path值的clip-path property。但是由于某些原因,裁剪并不能像使用HTML svg作为url()那样随元素缩放。

有没有办法让path()的行为像url()一样(拉伸剪辑)?

注意:我也尝试生成url-encoded svg并使用clip-path: url('data:image...'),但似乎不起作用

代码语言:javascript
复制
.heart {
  background: red;
  color: white;
  display: grid;
  place-content: center;
  width: 180px;
  height: 100px;
  margin: 30px auto;
}

.svg {
  clip-path: url(#myPath)
}

.path {
  clip-path: path('M15,45 A30,30,0,0,1,75,45 A30,30,0,0,1,135,45 Q135,90,75,130 Q15,90,15,45 Z')
}
代码语言:javascript
复制
<div class="heart svg">With SVG</div>
<div class="heart path">With PATH</div>

<svg>
  <clipPath id="myPath" clipPathUnits="objectBoundingBox">
    <path d="M0.5,1
      C 0.5,1,0,0.7,0,0.3
      A 0.25,0.25,1,1,1,0.5,0.3
      A 0.25,0.25,1,1,1,1,0.3
      C 1,0.7,0.5,1,0.5,1 Z" />
  </clipPath>
</svg>

EN

回答 1

Stack Overflow用户

发布于 2021-11-08 06:02:55

您可以通过将内容缩小到剪辑路径的大小,然后对其进行裁剪,然后再将其重新缩放来完成此操作

代码语言:javascript
复制
.heart {
    background: red;
    color: white;
    display: grid;
    place-content: center;
    width: calc(var(--desired-width) * 1px);
    height: calc(var(--desired-height) * 1px);
}
.heart-clip {
    clip-path: path("M0.5,1 C 0.5,1,0,0.7,0,0.3 A 0.25,0.25,1,1,1,0.5,0.3 A 0.25,0.25,1,1,1,1,0.3 C 1,0.7,0.5,1,0.5,1 Z");
    width: 1px;
    height: 1px;
}
.heart-scale-vars {
    --path-width: 1;
    --path-height: 1;
    --desired-width: 180;
    --desired-height: 100;
}
.scale-pre {
    transform: scale(
        calc(var(--path-width) / var(--desired-width)),
        calc(var(--path-height) / var(--desired-height))
    );
    transform-origin: top left;
}
.scale-post {
    transform-origin: top left;
    transform: scale(
        calc(var(--desired-width) / var(--path-width)),
        calc(var(--desired-height) / var(--path-height))
    );
}
.scale-postpost {
    width: calc(var(--desired-width) * 1px);
    height: calc(var(--desired-height) * 1px);
}
代码语言:javascript
复制
<div class="heart-scale-vars scale-postpost"><div class="scale-post heart-clip"><div class="scale-pre heart">With PATH</div></div></div>

或者通过使用url编码的svg作为掩码

代码语言:javascript
复制
.heart {
    background: red;
    color: white;
    display: grid;
    place-content: center;
    width: 180px;
    height: 100px;
}
.heart-mask {
    mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1' preserveAspectRatio='none'%3E%3Cpath d='M.5 1C.5 1 0 .7 0 .3A .25.25 1 1 1 .5.3A.25 .25 1 1 1 1 .3C1 .7 .5 1 .5 1Z'/%3E%3C/svg%3E");
    -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1' preserveAspectRatio='none'%3E%3Cpath d='M.5 1C.5 1 0 .7 0 .3A .25.25 1 1 1 .5.3A.25 .25 1 1 1 1 .3C1 .7 .5 1 .5 1Z'/%3E%3C/svg%3E");
}
代码语言:javascript
复制
<div class="heart heart-mask">With MASK</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69843750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档