当前位置:网站首页>利用 clip-path 绘制不规则的图形
利用 clip-path 绘制不规则的图形
2022-07-06 11:32:00 【不二博客】
在日常开发过程中UI设计师会存在不规则的形状和设计,在大多数的情况下我们使用了背景图片来渲染不规则的形状,但这与性能优化减少HTTP请求
的原则是背道而驰的。
比如说UI设计师希望我们渲染出一个带有渐变颜色的菱形。

也许大家想到可以通过 transform: skew(-10deg)
函数来使图形倾斜,这看起来是一个不错的方案,但是我们想里面添加一些内容。

如下图右侧我们使用 transform: skew(-10deg)
改变你的元素的倾斜角度,但是其内部的元素也继承了父级的 transform
属性发生了倾斜。显然这不是UI设计师想要的效果。
此刻肯定会有机智的小伙伴说,既然子元素继承了父元素的 transform: skew(-10deg)
属性,那么我们设置子元素的 transform: skew(10deg)
属性不就可以了吗?

而此时此刻又出现了一下新的问题,虽然我们已经使子元素水平展示,但是我们元素的中点也发生了倾斜,同样也达不到UI设计师的要求。
而想要实现效果其实很简单,我们只需要 clip-path
来代替掉 transform
即可。
// transform: skew(-10deg);
clip-path: polygon(0% 100%, 15% 0%, 100% 0%, 85% 100%);
clip-path
顾名思义,clip-path
是一个剪裁的路径,路径内的展示,路径外的隐藏。
inset 矩形

circle 圆形

ellipse 椭圆

polygon 多边形

怎么样,现在看到以上的属性,再反思一下之前的项目中,一定会有clip-path
可代替或者简化的地方。
在这里推荐一款 clip-path
可视化工具,可以帮助我们快速的构建出想要的图形。
https://bennettfeely.com/clippy/

url 路径
除了以上基础的图形以外,我们还可以设置 svg
作为我们的路径进行剪裁。
<!-- 爱心 svg -->
<svg>
<clipPath id="love" 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>
<style>
div {
clip-path: url(#love);
}
</style>

延伸
以上就是 clip-path
的常用的一些属性了,那么我们到底可以通过 clip-path
实现什么效果呢?
比如我们来绘制绘制一个包含气泡消息的的聊天窗口。

<div class="message">
<div class="message-pohoto">
<img src="./logo.png" alt="" />
</div>
<div class="message-text">
没有什么比让自己时刻保持优秀让人感到辛苦的了。从今天起,放弃对优秀的执念吧。与其执着于优秀,不妨让自己拥有不逃避任何事情的勇敢。不惧失败,果断地推进工作,让自己尽快成为那个敢于面对一切的勇者。
</div>
</div>
<style>
body,
html {
background-color: #f9f9f9;
}
.message {
display: flex;
align-items: flex-start;
padding: 20px 10px;
}
.message-pohoto {
padding-right: 20px;
}
.message-pohoto > img {
width: 46px;
height: 46px;
border-radius: 50%;
}
.message-text {
display: inline-block;
position: relative;
min-height: 46px;
box-sizing: border-box;
display: flex;
align-items: center;
color: #fff;
font-size: 14px;
padding: 10px 10px 10px 15px;
--bg: linear-gradient(45deg, #ff3c41, #ff9800);
filter: drop-shadow(0 0 4px rgba(0, 0, 0, 0.5));
}
.message-text::before,
.message-text::after {
content: "";
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
background: var(--bg);
z-index: -1;
}
.message-text::before {
clip-path: inset(0 0 0 5px round 5px);
}
.message-text::after {
clip-path: polygon(0% 23px, 5px calc(23px + 5px), 5px calc(23px - 5px));
}
总结
clip-path
可以剪裁出各种不规则的形状,使用的好可以简化我们的工作,同时也可以实现很多特殊的效果,是非常值得大家去学习和运用~
最后
感谢你的阅读~
如果觉得这篇文章对你有所帮助,点赞、在看是最大的支持!
边栏推荐
- 深入分析,Android面试真题解析火爆全网
- An error occurs when installing MySQL: could not create or access the registry key needed for the
- Mysql Information Schema 学习(二)--Innodb表
- zabbix 代理服务器 与 zabbix-snmp 监控
- [pytorch] yolov5 train your own data set
- Looting iii[post sequence traversal and backtracking + dynamic planning]
- Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
- spark基础-scala
- Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
- Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
猜你喜欢
Mysql Information Schema 学习(二)--Innodb表
A popular explanation will help you get started
Lucun smart sprint technology innovation board: annual revenue of 400million, proposed to raise 700million
通俗的讲解,带你入门协程
深入分析,Android面试真题解析火爆全网
全套教学资料,阿里快手拼多多等7家大厂Android面试真题
ROS自定义消息发布订阅示例
史上超级详细,想找工作的你还不看这份资料就晚了
思維導圖+源代碼+筆記+項目,字節跳動+京東+360+網易面試題整理
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
随机推荐
The second day of rhcsa study
Countdown 2 days | live broadcast preview of Tencent cloud message queue data import platform
凤凰架构3——事务处理
R语言ggplot2可视化时间序列柱形图:通过双色渐变配色颜色主题可视化时间序列柱形图
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
Airiot IOT platform enables the container industry to build [welding station information monitoring system]
Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting
包装行业商业供应链管理平台解决方案:布局智慧供应体系,数字化整合包装行业供应链
关于静态类型、动态类型、id、instancetype
Unlock 2 live broadcast themes in advance! Today, I will teach you how to complete software package integration Issues 29-30
LeetCode-1279. 红绿灯路口
Black Horse - - Redis Chapter
In 50W, what have I done right?
A popular explanation will help you get started
R language ggplot2 visualization: use ggviolin function of ggpubr package to visualize violin diagram
时钟轮在 RPC 中的应用
PMP practice once a day | don't get lost in the exam -7.6
How to improve website weight
Reflection and illegalaccessexception exception during application
Translation D28 (with AC code POJ 26:the nearest number)