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 back
ground. This is the opposite of what you might have expected. This happens because of the mas
k-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 蒙版