首页 > 其他分享 >(转)如何实现 Cut-Out Effect

(转)如何实现 Cut-Out Effect

时间:2023-06-29 19:23:39浏览次数:26  
标签:Cut -- 100% mask Effect height width var Out

原文地址:Thinking About The Cut-Out Effect: CSS or SVG?

Cut-Out Effect,就是 月牙效果,或者叫,咬一口效果

 1. User Avatar 月牙效果

1. 1 - Clip Path

Pros
  Cross-browser, works on all major versions of Chrome, Edge, Firefox, and Safari.
  Good for very basic examples. It can get complex with borders or shadows.
Cons
  To remove the cut-out effect, we need to change the path. This can be hard to do in a component with different states.
  It needs some experience in dealing with merging shapes in a design app.

<div class="avatar">
  <img class="item-2" src="https://ishadeed.com/assets/shadeed.jpg" alt="" />
  <span class="badge"></span>
</div>

<svg class="svg">
  <defs>
    <clipPath id="my-clip-path" clipPathUnits="objectBoundingBox">
      <path d="M0.5,0 C0.776,0,1,0.224,1,0.5 C1,0.603,0.969,0.7,0.915,0.779 C0.897,0.767,0.876,0.76,0.853,0.76 C0.794,0.76,0.747,0.808,0.747,0.867 C0.747,0.888,0.753,0.908,0.764,0.925 C0.687,0.972,0.597,1,0.5,1 C0.224,1,0,0.776,0,0.5 C0,0.224,0.224,0,0.5,0"></path>
    </clipPath>
  </defs>
</svg>
.avatar {
    position: relative;
    --size: 175px;
    width: var(--size);
    height: var(--size);
    margin: 1rem auto;

    &:after {
        content: "";
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        border-radius: 50%;
        border: 1px solid;
        opacity: 0.2;
        clip-path: url("#my-clip-path");
    }
}
  
.item-2 {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
    clip-path: url("#my-clip-path");
}

*,
*:before,
*:after {
    box-sizing: border-box;
}

1.2 - CSS Mask

Pros
  Cross-browser, but needs the vendor prefix all browsers except Firefox.
Cons
  Can be limited or complex in other examples

<img class="item" src="https://ishadeed.com/assets/shadeed.jpg" alt="" />
.item {
  display: block;
  width: 200px;
  height: 200px;
  -webkit-mask-image: radial-gradient(
    circle 20px at calc(100% - 30px) calc(100% - 30px),
    transparent 30px,
    #000 0
  );
  object-fit: cover;
  border-radius: 50%;
  margin: 2rem auto;
}

*,
*:before,
*:after {
  box-sizing: border-box;
}

1.3 - SVG Mask

Pros
  A simple solution.
  Great cross-browser support.
  Can be maintained.

Cons
  I can’t think of any cons for this solution except that it can be a bit hard for a person who doesn’t know SVG

<div class="test-2">
  <svg role="none">
    <mask id="test2">
      /* white black 做差集,white white 做并集,类似于css的mask-composite */
      <circle fill="white" cx="100" cy="100" r="100"></circle>
      <circle fill="black" cx="86%" cy="86%" r="18"></circle>
    </mask>
    <g mask="url(#test2)">
      <image x="0" y="0" height="100%" preserveAspectRatio="xMidYMid slice" width="100%" xlink:href="https://ishadeed.com/assets/shadeed.jpg"></image>
      <circle fill="none" cx="100" cy="100" r="100" stroke="rgba(0,0,0,0.1)" stroke-width="2"></circle>
    </g>
  </svg>
</div>
.test-2 {
  width: 200px;
  height: 200px;
  margin: 2rem auto;

  svg {
    width: 100%;
    height: 100%;
  }

  img {
    max-width: 100%;
  }
}

2. Seen Avatars 头像重叠

2.1 - css mask

/* 椭圆裁切,30.5用于消除锯齿 */
.item {
    -webkit-mask-image: radial-gradient(ellipse 54px 135px at 11px center, #0000 30px, #000 30.5px);
}

/* 椭圆图片,渐变全背景,两个mask进行差集裁切 */
.item {
    -webkit-mask-image: url(oval.svg), linear-gradient(#000, #000);
    -webkit-mask-repeat: no-repeat;
    -webkit-mask-position: -26px 54%, 100% 100%;;
    -webkit-mask-size: 80px 140px, 100% 100%;
    mask-composite: exclude;
    -webkit-mask-composite: destination-out;
}

2.2 - SVG

核心思路,svg两个圆差集,black圆cx用负值,盖住white圆,white圆差去和black圆重叠部分,就形成月牙

<svg role="none" class="avatar-wrapper">
    <mask id="cut">
        <circle cx="50" cy="50" r="50" fill="white"></circle>
        <circle fill="black" cx="-30" cy="50" r="50"></circle>
    </mask>
    <g mask="url(#cut)">
        <image x="0" y="0" height="100%" width="100%" xlink:href="shadeed.jpg"></image>
        <circle fill="none" stroke="rgba(0,0,0,0.1)" stroke-width="2"></circle>
    </g>
</svg>

详细可参考

3. Website Header

3.1 - CSS Radial Gradient

background用radial-gradient挖洞,logo盖在上面即可

:root {
    --header-height: 100px;
    --radius: clamp(48px, 4vw, 60px);
    --logo-size: calc(calc(var(--radius) * 2) - 8px);
}

.logo {
    display: block;
    position: relative;
    top: calc(var(--header-height) * 0.7 - var(--radius) + 2px);
    width: var(--logo-size);
    margin-left: auto;
    margin-right: auto;
}

.site-header {
  background: radial-gradient(
    circle at 50% 70%,
    rgba(0, 0, 0, 0) var(--radius),
    #95a57f var(--radius),
    #95a57f 100%
  );
}

详细可参考

3.2 - SVG Mask

svg挖个洞,作为mask,盖住header即可

<header class="site-header">
    <img src="assets/logo.svg" alt="" />
    <svg role="none" height="80">
        <defs>
            <mask id="hole">
                <rect width="100%" height="100%" fill="white" />
                <circle cx="50%" cy="80%" r="58" fill="black"></circle>
            </mask>
        </defs>

        <rect width="100%" height="100%" mask="url(#hole)" />
    </svg>
</header>
.site-header {
    position: relative;
}

.site-header svg {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
}

详细可参考

标签:Cut,--,100%,mask,Effect,height,width,var,Out
From: https://www.cnblogs.com/mengff/p/17515024.html

相关文章

  • npm install报gyp ERR! stack Error: Can't find Python executable "python", you ca
    1gypERR!stackError:Can'tfindPythonexecutable"python",youcansetthePYTHONenvvariable.2gypERR!stackatPythonFinder.failNoPython(/Users/scott/project/dx_project/front_project/workflow-frontend-elem/node_modules/node-......
  • mybatis 动态数据源核心--AbstractRoutingDataSource
    1publicabstractclassAbstractRoutingDataSourceextendsAbstractDataSourceimplementsInitializingBean{2@Nullable3privateMap<Object,Object>targetDataSources;4@Nullable5privateObjectdefaultTargetDataSource;......
  • Vue Router 源码分析
    专栏分享:vue2源码专栏,玩具项目专栏,硬核......
  • Arena Breakout
    官网:http://www.arenabreakout.com/点点数据:https://app.diandian.com/app/40qikumlvzq6s1n/googleplay-rank?market=11&country=124&id=com.proximabeta.mf.uamo&language=11 ......
  • React学习时,outlet配置(token判定,页面path监听)
    尽管写过outlet路由的配置。考虑到token判定和路由页变更,我不了解v6是不是有更详解的做法。决定调一下配置,期望在任何页面异步更新时,token都可以在跳转前被检测到,防止无token跳转发生。为src文件配置v6版本:路由子组件App.jsimport{HashRouter,Routes,Ro......
  • Android LayoutManager高端玩家,实现花式表格!
    如果你对RecyclerView原理还不是特别了解,非常建议你读一下。本文的项目也是学习自定义LayoutManager绝佳资料,大家有需要的可以好好拜读。前言表格是自打我进公司以后就使用的控件,起初使用的是ScrollablePanel,从一开始的被花式吊打,到后期的熟练使用。大佬写的控件确实给我的工作带来......
  • Android线程管理之ExecutorService线程池
    为什么要引入线程池?   1.)newThread()的缺点每次newThread()耗费性能调用newThread()创建的线程缺乏管理,被称为野线程,而且可以无限制创建,之间相互竞争,会导致过多占用系统资源导致系统瘫痪。不利于扩展,比如如定时执行、定期执行、线程中断  2.)采用线程池的优点重用存在的......
  • C++中cout打印内容丢失
    原文地址:https://www.cnblogs.com/liqinglucky/p/cout.html写代码时遇到C++中cout打印不全的问题。现象代码调整前:我有个函数c_func(output,input,inlen)定义在原来的文件file.c里,然后在c++中调用了这个函数。file.cpp文件里的调用格式如下:cpp_func(){ c_func(output......
  • SAP UI5 sap.ui.layout 命名空间的作用介绍
    SAPUI5是一种用于构建企业级Web应用程序的开发框架。它提供了丰富的UI控件和工具,使开发人员能够快速构建现代化、可扩展和可定制的应用程序。在SAPUI5中,sap.ui.layout是一个重要的命名空间,用于提供布局和容器相关的控件和功能。下面将详细介绍sap.ui.layout的作用和一些示例。......
  • c++ std::execution::par,std::execution::par_unseq
    #include<algorithm>#include<chrono>#include<cstdint>#include<execution>#include<iostream>#include<random>#include<vector>std::random_devicerd;std::mt19937_64mt{rd()};template<typenameT>......