热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

css蒙版_我如何使用CSS蒙版构建情绪变化动画

css蒙版byAnkitKarnany通过AnkitKarnany我如何使用CSS蒙版构建情绪变化动画(HowIbuiltamoodchanginganimationusingCS

css 蒙版

by Ankit Karnany

通过Ankit Karnany

我如何使用CSS蒙版构建情绪变化动画 (How I built a mood changing animation using CSS masks)

Remember the cartoons we used to watch during our childhood? At that time they were the epitome of animations. Nowadays, animations aren’t just limited to cartoons — we come across them almost everyday when we check our phone or look at any device which has a screen.

还记得我们小时候看过的动画片吗? 当时它们是动画的缩影。 如今,动画不仅限于卡通,当我们检查手机或查看具有屏幕的任何设备时,几乎每天都会遇到动画。

Today, animation is used not only to attract attention but to enhance the user experience and guide user flow. In any good design, animations are added in such a way that they blend with the common flow, thus creating a seamless user experience.

如今,动画不仅用于吸引注意力,而且还可以增强用户体验并引导用户流程。 在任何好的设计中,动画的添加方式均应使其与常见流程融合在一起,从而创造出无缝的用户体验。

So, in this article we will build a simple animation of a face with different expressions, and we’ll learn a little bit of CSS in that process.

因此,在本文中,我们将构建具有不同表情的人脸的简单动画,并在此过程中学习一些CSS。

入门 (Getting started)

We will use a CSS technique which is somewhat rare among web developers, but that designers use quite often. It is called masking.

我们将使用CSS技术,这种技术在Web开发人员中很少见,但设计人员经常使用。 这称为遮罩

So what comes to your mind when you hear “mask?”

那么,当您听到“面具”时您会想到什么?

You’re probably picturing a cover on top of something. That’s all we need to understand.

您可能在想在某物上面盖上盖子。 这就是我们需要了解的所有内容。

Wait — but this article is related to coding and using CSS animation…

等等,但是本文与编码和使用CSS动画有关……

Don’t worry! We’ll get right to it.

不用担心 我们会正确处理的。

创建基本遮罩 (Creating the basic mask)

Let say we have a iv> with a background: green; and it looks something like this:

假设我们ith a background: iv> ith a background:绿色; 它看起来像这样:

Now, say I have a face.svg:

现在,说我有一个face.svg :

If we apply a CSS property mask-image: url(face.svg); on the iv>, you’ll be amazed to see what we get:

如果我们应用CSS属性mask-image: url(face.svg); iv>上,您会惊讶地看到我们得到了什么:

You might be thinking that something is odd here. The face.svg was placed over the iv>, but it took up the color of the background. This is the opposite of what you might have expected. This happens because of the mask-type property which makes the opaque part of the svg transparent. This allows the background color to be visible.

您可能会认为这里有些奇怪。 所述face.svg被放置在 IV>,但它占去了彩色Ó f the back地面。 这与您预期的相反。 发生这种情况是因为f the mas k型属性使svg的不透明部分透明。 这样可以使背景色可见。

Let’s not get deeper into that now. Just keep in mind that we can use background-color to change the color of the mask. If you are familiar with different ways of using background-color, we can also use gradients and write a simple gradient which fills red in the center and radially spreads into black outwards. The code for that is the following:

现在让我们不深入讨论。 请记住,我们可以使用background-color更改蒙版的颜色。 如果您熟悉使用background-color不同方式,我们还可以使用渐变并编写一个简单的渐变,该渐变在中心填充红色,并在径向向外扩展为黑色。 相应的代码如下:

background-image: -webkit-radial-gradient( hsla(0, 100%, 50%, .7), hsla(0, 100%, 20%, .8), hsla(0, 100%, 10%, 1));

Which results in this:

结果如下:

添加动画 (Adding the animation)

Now let’s add some animation to this empty face. For this I have expression.svg which looks like the below image. For simplicity, I have created all the svgs with the same width and height, so that we avoid aligning the face and expression manually.

现在,让我们向该空面Kong添加一些动画。 为此,我有expression.svg ,如下图所示。 为简单起见,我创建了所有具有相同宽度和高度的svg,以便避免手动对齐面部和表情。

Now mask-image has this cool option which allows multiple images to be used as masks. So we can do this: mask-image: url(face.svg), url(expression.svg);. This is what we have now:

现在mask-image有了这个很酷的选项,可以将多个图像用作遮罩。 这样我们就可以做到: mask-image: url(face.svg), url(expression.svg); 。 这就是我们现在所拥有的:

One of the most important properties of CSS masks is mask-position, which positions the mask from the top left corner relative to its parent. And I can position multiple masks using the property mask-position just like mask-image.

CSS遮罩的最重要属性之一是mask-position ,它相对于其父遮罩从左上角定位遮罩。 而且我可以使用属性mask-position来放置多个遮罩,就像mask-image

So to make the face sad, we can use something like this: mask-position: 0 0, 0 12px;. And the result is this:

因此,要使脸部悲伤,我们可以使用类似这样的方法: mask-position: 0 0, 0 12px; 。 结果是这样的:

The first position 0 0 is for the face.svg, and the second 0 12px is for expression.svg. This pushes it 12px from above and results in the above expression.

第一个位置0 0用于face.svg ,第二个0 12px用于expression.svg 。 这会将其从上方推12px ,并产生以上表达式。

应用功能 (Applying functionality)

Now let’s animate these expressions on hover. So the complete code we get after applying the hover pseudo class is the following:

现在让我们在悬停时为这些表达式设置动画。 因此,在应用悬停伪类后获得的完整代码如下:

i { background-image: -webkit-radial-gradient(hsla(0, 100%, 50%, .7), hsla(0, 100%, 20%, .8) 60%, hsla(0, 100%, 10%, 1)); mask-image: url('face.svg'), url('expression.svg'); mask-position: 0 0, 0 12px; /* To make the sad expression */

transition: mask-position .5s ease-out;}

i:hover { background-image: -webkit-radial-gradient(hsla(120, 100%, 50%, .7), hsla(120, 100%, 20%, .8) 60%, hsla(120, 100%, 10%, 1));

mask-position: 0 0, 0 0; /* To make the happy expression */

transition: mask-position .1s linear;}

After playing with the CSS a bit more, we can do this:

在使用CSS多一点之后,我们可以执行以下操作:

This is one of the methods we can use to build those gripping animations we come across almost daily.

这是我们几乎每天都可以用来制作那些令人抓狂的动画的方法之一。

重要说明 (One Important Note)

The masking properties might not work across all the browsers. So to make them work in all the browsers, just prepend browser specific labels like -webkit- , -moz- & -0- .

屏蔽属性可能不适用于所有浏览器。 因此,要使其在所有浏览器中都能正常工作,只需在浏览器特定的标签前加上-webkit- ,- -moz--0-

You can look at the complete code here on github and codepen.

您可以在github和codepen上查看完整的代码。

Thanks for reading! I hope you learned something.

谢谢阅读! 我希望你学到了一些东西。

翻译自: https://www.freecodecamp.org/news/how-i-built-a-mood-changing-animation-using-css-masks-565b16ed051f/

css 蒙版



推荐阅读
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • ImmutableX Poised to Pioneer Web3 Gaming Revolution
    ImmutableX is set to spearhead the evolution of Web3 gaming, with its innovative technologies and strategic partnerships driving significant advancements in the industry. ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 采用IKE方式建立IPsec安全隧道
    一、【组网和实验环境】按如上的接口ip先作配置,再作ipsec的相关配置,配置文本见文章最后本文实验采用的交换机是H3C模拟器,下载地址如 ... [详细]
  • 在编译BSP包过程中,遇到了一个与 'gets' 函数相关的编译错误。该问题通常发生在较新的编译环境中,由于 'gets' 函数已被弃用并视为安全漏洞。本文将详细介绍如何通过修改源代码和配置文件来解决这一问题。 ... [详细]
  • 探讨ChatGPT在法律和版权方面的潜在风险及影响,分析其作为内容创造工具的合法性和合规性。 ... [详细]
  • 本题要求在一组数中反复取出两个数相加,并将结果放回数组中,最终求出最小的总加法代价。这是一个经典的哈夫曼编码问题,利用贪心算法可以有效地解决。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 本文将介绍如何使用 Go 语言编写和运行一个简单的“Hello, World!”程序。内容涵盖开发环境配置、代码结构解析及执行步骤。 ... [详细]
  • 本文介绍了如何在C#中启动一个应用程序,并通过枚举窗口来获取其主窗口句柄。当使用Process类启动程序时,我们通常只能获得进程的句柄,而主窗口句柄可能为0。因此,我们需要使用API函数和回调机制来准确获取主窗口句柄。 ... [详细]
  • 本文介绍如何使用阿里云的fastjson库解析包含时间戳、IP地址和参数等信息的JSON格式文本,并进行数据处理和保存。 ... [详细]
  • 在 Flutter 开发过程中,开发者经常会遇到 Widget 构造函数中的可选参数 Key。对于初学者来说,理解 Key 的作用和使用场景可能是一个挑战。本文将详细探讨 Key 的概念及其应用场景,并通过实例帮助你更好地掌握这一重要工具。 ... [详细]
  • 本文详细介绍了 org.apache.commons.io.IOCase 类中的 checkCompareTo() 方法,通过多个代码示例展示其在不同场景下的使用方法。 ... [详细]
  • 本文介绍了如何利用Python的高精度计算库mpmath实现π的100种不同计算方法。通过设置更高的精度和优化的数学函数,这些方法能够提供极其精确的结果。 ... [详细]
author-avatar
白白加黑黑99_889
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有