Creating a Pulsing Circle Animation

Creating a Pulsing Circle Animation

原文 https://www.kirupa.com/animations/creating_pulsing_circle_animation.htm

Outside of transitions that animate between states, we don't see a whole lot of actual animation in the many UIs we interact with. We don't have the random pixel moving around the screen simply because it is cool to do so. This isn't 2002. There are some nice exceptions, though. I ran into one such exception when setting up my awesome Nest camera (image taken from here):

As part of the setup process, you get to a screen where you see your Nest product with a bunch of blue circles scaling out and fading into view. You can see what I am referring to in this unboxing and setup video (at the 5:23 mark). In this tutorial, we'll learn how to re-create this awesome pulsing circle animation. Along the way, we will also learn how to use the various tricks we have up our sleeve when working with CSS Animations that will serve us well for not just this animation but for many animations beyond this one.

Onwards!

OMG! An Animation Book Written by Kirupa?!!

To kick your animations skills into the stratosphere, everything you need to be an animations expert is available in both paperback and digital editions.

BUY ON AMAZON

The Example

Before we go on, take a look what the pulsing circle animation we will be creating will look like:

Notice how the circles appear and disappear. Understanding the subtle details of how this animation works at a high level is crucial before we actually go about creating it. Let's understand the subtle details together.

What the Animation Is Doing

As you are watching the circle pulsing animation, you'll notice that there is a continuity where as one circle effortlessly fades away another circle prominently arrives to take its place. The way this works isn't by animating a single circle. It is done by animating several circles in sequence.

If we had to examine the behavior of just one circle, it will start off as follows:

It will initially be very small and be fully visible. As the animation progresses, our circle gets larger and gains a bit of transparency:

Towards the end, our circle gets even larger and is barely visible:

At the end of the animation, the circle reaches its maximum size and fully fades out of view. Once this point is reached, the process starts over from the beginning. When we have just one circle, this animation looks a little unnatural. It is less of a pulsing effect and more of just one deranged lone circle fading into and out of view.

To fix this, we can add another circle into the mix. This new circle will animate exactly in the same way as the earlier circle, but it will simply have a slight delay in its starting time to have its movement be offset. This means both of the circles will be performing the same animation, but they just won't be in-sync. This can be visualized as follows:

Notice that as one circle is about to fully fade out, the other circle is just beginning its journey. You know what would make all of this even better? MORE CIRCLES! Here is what having three circles will look like:

Pretty neat, right? For maximum awesomeness, our final effect is made up of FOURcircles each animating in the same way with a slight offset in when their animation starts. A better way of visualizing this effect is as follows where we look at the animation duration and when each animation is set to begin:

Each circle starts and ends each iteration with the same duration. As you can see, the only variation is when each animation iteration starts for each circle. By offsetting the starting point, we get the staggered animation that we see that makes up our pulsing effect.

Actual Implementation

Now that you have a good idea of how the animation works, the easy part is actually building the animation. We will build the final animation in stages, but the first thing we need is a starting point. Create a new HTML document and add the following content into it:

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Pulsing Circle</title>
  <style>
    #container {
      width: 600px;
      height: 400px;
      display: flex;
      align-items: center;
      justify-content: center;
      overflow: hidden;
      position: relative;
    }
    .circle {
      border-radius: 50%;
      
      width: 150px;
      height: 150px;
      position: absolute;
      opacity: 0;
    }
    .item {
      z-index: 100;
      padding: 5px;
    }
    .item img {
      width: 150px;
    }
  </style>
</head>
<body>
  <div id="outerContainer">
    <div id="container">
      <div class="item">
        <img src="https://www.kirupa.com/images/orange.png" />
      </div>
      <div class="circle"></div>
    </div>
  </div>
</body>
</html>

If you save this document and preview what you see in your browser, you'll see something that looks as follows:

Before we go further, take a few moments to look the code we just added. There shouldn't be any surprises, for there is just some boring HTML and CSS. The exciting HTML and CSS will be coming up next when we get the ball rolling on creating our animation.

Animating our (Lonely) Circle

Right now, we have a single circle element defined in our HTML, and the CSS that corresponds to it looks as follows:

.circle {
  border-radius: 50%;
  background-color: deepskyblue;
  width: 150px;
  height: 150px;
  position: absolute;
  opacity: 0;
}

Let's define the animation to scale and fade this circle out. Inside this .circle style rule, add the following highlighted line:

.circle {
  border-radius: 50%;
  background-color: deepskyblue;
  width: 150px;
  height: 150px;
  position: absolute;
  opacity: 0;
  animation: scaleIn 4s infinite cubic-bezier(.36, .11, .89, .32);
}

We are defining our animation property here. Our animation is set to run for 4 seconds, loop forever, and rely on a custom easing specified by the cubic-bezier function. The actual animation keyframes are specified by scaleIn, which we will add next.

Below the existing style rules defined inside our style element, add the following:

@keyframes scaleIn {
  from {
    transform: scale(.5, .5);
    opacity: .5;
  }
  to {
    transform: scale(2.5, 2.5);
    opacity: 0;
  }
}

Here we define our scaleIn keyframes that specify what exactly our animation will do. As animations go, this is a simple one. Our animation will start off at 50% (.5) scale with an opacity of .5. It will end with a scale that is 250% (2.5) and an opacity of 0. If you save all of the changes you made and preview our document in your browser right now, you should see our circle animation come alive.

Animating More Circles

Having just one animated circle is a good starting point, but it doesn't give us the richness that having multiple animated circles bring to the table. The first thing we will do is add more circles. In our HTML, add the following highlighted lines:

<div id="outerContainer">
  <div id="container">
    <div class="item">
      <img src="https://www.kirupa.com/images/orange.png" />
    </div>
    <div class="circle"></div>
    <div class="circle"></div>
    <div class="circle"></div>
    <div class="circle"></div>
  </div>
</div>

What you now have are four circles each starting at the same time and running the same scaleIn animation. That isn't exactly what we want. What we want to do is stagger when each circle's animation plays so that we create a more natural and continuous pulsing effect. The easiest way to do that is by giving each of our circles a different starting point. That can easily be done by setting the animation-delay property. Because each of our circles will need to have its own unique animation-delay value, let's just set this value inline as opposed to creating a new style rule for each circle. Go ahead and make the following change:

<div class="circle" style="animation-delay: 0s"></div>
<div class="circle" style="animation-delay: 1s"></div>
<div class="circle" style="animation-delay: 2s"></div>
<div class="circle" style="animation-delay: 3s"></div>

We are setting the style attribute on each circle with the animation-delay property set to a value of 0s1s2s, or 3s. This means the first circle animation will start immediately, the second circle animation will start after 1 second, the third circle animation will start after 2 seconds, and...you get the picture. If you preview what we have done in our browser now, you will see our pulsing effect taking its final shape:

Looks like we are done, right? Well, there is one more thing we should consider maybe sorta kinda doing.

Starting our Animation in the Middle

Right now, when we load our page, our animation starts from the beginning. This means there are a few seconds where nothing shows up before each circle gradually starts appearing. If you don't like this behavior where things start off empty, we can easily change that. We can have it so that our animation looks like it has been running for a while when the page loads to give a more busy look.

The following diagram highlights the difference you will see during page load between what we see currently and what we will see when we add in this busy-ness:

The way we can achieve our desired behavior (as highlighted by the right version in the above diagram) is by giving our animation-delay property a negative time value. A negative time value tells our animation to not start at the beginning but instead start in the middle. The magnitude of the negative value specifies the exact point in the middle that our animation will begin. This sounds exactly like what we want, so go ahead and change the animation-delay values in our circle div elements to the following:

<div class="circle" style="animation-delay: -3s"></div>
<div class="circle" style="animation-delay: -2s"></div>
<div class="circle" style="animation-delay: -1s"></div>
<div class="circle" style="animation-delay: 0s"></div>

When you do this, our circles will start their animation journey three seconds in, two seconds in, one second in, and 0 seconds in (at the beginning basically) when the page loads. This will lead our animation to look like it has been running for a while even though it was loaded for the very first time.

Conclusion

The pulsing circle effect is one of my favorite examples because of how simple it really is. At first glance, creating this effect may look like it will need some JavaScript or an animation defined entirely on the canvas. By taking advantage of the animation-delayproperty and how it treats negative values, our path is much more simple. All we needed to do was copy/paste a few more circles and alter the animation-delay value each one sported. Simple!

If you liked this and want to see more, check out all the Web Animation tutorials on this site.

 

If you have a question about this or any other topic, the easiest thing is to drop by our forums where a bunch of the friendliest people you'll ever run into will be happy to help you out!

posted on 2019-05-01 15:20 NET未来之路 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/10799914.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/424912.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第四十九期:化繁为简的五种码农必备工具

如今&#xff0c;开发工具已成为了软件开发过程中必不可少的组成部分。本文将向您介绍当前软件开发市场上颇具影响力的五种化繁为简的码农必备工具。 不知您是否已经发现&#xff1a;那些以任务为中心的软件开发工作&#xff0c;会比独立的研究式开发复杂得多。针对软件产品的开…

第五十期:工作强度超996,失业半年即出局,硅谷为何如此“嗜血”?

在硅谷&#xff0c;靠创业发财的人被称为中了“硅谷六合彩”&#xff0c;大多数个体的艰难挣扎&#xff0c;最终换来了硅谷长久的繁荣昌盛。 划重点 1、在硅谷&#xff0c;靠创业发财的人被称为中了“硅谷六合彩”。 2、谷歌的合同工必须比正式工早两小时到公司打卡&#xff…

【数据结构与算法】二叉树

树 1.树、二叉树 2.二叉查找树 3.平衡二叉树、红黑树 4.递归树 一、树 1.树的常用概念 根节点、叶子节点、父节点、子节点、兄弟节点&#xff0c;还有节点的高度、深度以及层数&#xff0c;树的高度。 2.概念解释 节点&#xff1a;树中的每个元素称为节点 父子关系&#xff…

第二十四期:面试问:Kafka为什么速度那么快?该怎么回答

针对Kafka的基准测试可以参考&#xff0c;Apache Kafka基准测试&#xff1a;每秒写入2百万(在三台廉价机器上)下面从数据写入和读取两方面分析&#xff0c;为什么Kafka速度这么快 Kafka的消息是保存或缓存在磁盘上的&#xff0c;一般认为在磁盘上读写数据是会降低性能的&#x…

【数据结构与算法】平衡二叉树、红黑树

1.树、二叉树 2.二叉查找树 3.平衡二叉树、红黑树 4.递归树 一&#xff0c;什么是“平衡二叉查找树” 1&#xff0c;定义&#xff1a;二叉树中任意一个节点的左右子树的高度相差不能大于1。 所以&#xff1a;完全二叉树&#xff0c;满二叉树都是平衡二叉树&#xff0c;非完全…

第五十一期:互联网不如国企,去BAT的程序员都是diao丝?

要说互联网是目前最热门的行业&#xff0c;应该没人反驳吧。尤其是技术&#xff0c;大家都想毕业后去BAT大厂&#xff0c;甚至比如微软、google等外企科技公司&#xff0c;学编程出身的高校学子&#xff0c;去国企的还是比较少。除非为了拿一线城市的户口&#xff0c;不然可能真…

文档排序--相似度模型--VSM

说明&#xff1a;文章内容来源于课程视频和课程ppt。我只学习了课程没有做习题。文章不是翻译&#xff0c;是我对课程的理解。 上文提到文档排序函数是TR的核心。文档排序函数的实现有几种思路&#xff0c;其中一种是基于相似度的模型。这种模型具体是用空间向量模型(Vector Sp…

第二十五期:搞定Linux Shell文本处理工具,看完这篇集锦就够了

Linux Shell是一种基本功&#xff0c;由于怪异的语法加之较差的可读性&#xff0c;通常被Python等脚本代替。既然是基本功&#xff0c;那就需要掌握&#xff0c;毕竟学习Shell脚本的过程中&#xff0c;还是能了解到很多Linux系统的内容。 Linux Shell是一种基本功&#xff0c;由…

【数据结构与算法】递归树

1.树、二叉树 2.二叉查找树 3.平衡二叉树、红黑树 4.递归树 一、什么是递归树 如果我们把这个一层一层的分解过程画成图&#xff0c;它其实就是一棵树。我们给这棵树起一个名字&#xff0c;叫作递归树。 时间复杂度分析的递归树法 分析每一步核心操作的时间复杂度分析树高&…

【02】Kubernets:使用 kubeadm 部署 K8S 集群

写在前面的话 通过上一节&#xff0c;知道了 K8S 有 Master / Node 组成&#xff0c;但是具体怎么个组成法&#xff0c;就是这一节具体谈的内容。概念性的东西我们会尽量以实验的形式将其复现。 部署 K8S 集群 互联网常用的 K8S 集群部署方式&#xff1a; 1. kubeadm&#xff0…

构建一个文本搜索系统

说明&#xff1a;文章内容来源于课程视频和课程ppt。我只学习了课程没有做习题。文章不是翻译&#xff0c;是我对课程的理解。 1 TR的主要构成 在文章中文本检索系统全文检索系统TR System  从图中看到(红色的方框)TR的主要过程有&#xff1a;分词(Tokenizer)、索引(Indexer)…

[Linux][Ubuntu]Linux实习常用操作/Debug总结

一、 Win下装Jupter 1.安装anaconda 配置环境变量 打开jupyter 2.切换目录 装软件 二、 移动复制粘贴上锁文件 sudo nautilus 三、 视频播放 sudo nano /etc/environment 添加QT_X11_NO_MITSHM1 四、 修改阿里源 提高下载速度 1.1打开software&updates 1.2 打…

第二十六期:100 个网络基础知识普及,看完成半个网络高手

本篇文章是关于100个网络基础知识普及&#xff0c;看完成半个网络高手!下面&#xff0c;我们一起来看。 作者&#xff1a;佚名来源 本篇文章是关于100个网络基础知识普及&#xff0c;看完成半个网络高手!下面&#xff0c;我们一起来看。 1)什么是链接? 链接是指两个设备之间…

团队冲刺第一天

今天要做什么&#xff1a;每个人都在网上学习Android软件开发的知识&#xff0c;完成简单的操作&#xff0c;实现简单功能。 明天要做什么&#xff1a;学习各种控件的作用&#xff0c;用法 站立会议照片&#xff1a; 任务看板照片&#xff1a; 燃尽图&#xff1a; 转载于:https…

[Leetcode][第679题][JAVA][24点游戏][回溯][暴力]

【问题描述】[困难] 【解答思路】 回溯 时间复杂度&#xff1a;O(1) 空间复杂度&#xff1a;O(1) class Solution {static final int TARGET 24;static final double EPSILON 1e-6;static final int ADD 0,MULTIPLY1,SUBTRACT 2,DIVIDE 3;public boolean judgePoint24(int…

第二十七期:网络爬虫程序员被抓,我们还敢爬虫吗?细数那些Java爬虫技术

最近&#xff0c;某大数据科技公司因为涉嫌非法抓取某招聘网站用户的简历信息&#xff0c;公司被查封&#xff0c;负责编写抓取程序的程序员也将面临坐牢。 作者&#xff1a;架构之路来源 最近&#xff0c;某大数据科技公司因为涉嫌非法抓取某招聘网站用户的简历信息&#xff…

文本搜索系统的评估

说明&#xff1a;文章内容来源于课程视频和课程ppt。我只学习了课程没有做习题。文章不是翻译&#xff0c;是我对课程的理解。  这部分本应该继续说反馈(FeedBack)的。但是课程中安排的是评估(Evaluation)。评估是用于衡量搜索引擎质量的。反馈是为了提高搜索引擎质量而进行的…

UE3客户端服务器GamePlay框架

客户端(当前玩家)与服务器对应关系图&#xff1a; 整体上看&#xff0c;UE3的GamePlay框架使用的是MVC架构 ① 橙色的Actor对象及橙色箭头相连的成员变量只会被同步给Owner客户端 Controller&#xff1a;控制器&#xff0c;包括PlayerController和AIController InventoryManage…

[Leetcode][第201题][JAVA][数字范围按位与][位运算][Brian Kernighan]

【问题描述】[中等] 【解答思路】 1. 暴力 逐位与 &#xff0c;只需要判断i 0 或 i 2147483647 的话&#xff0c;就跳出 for 循环即可。 时间复杂度&#xff1a;O(N) 空间复杂度&#xff1a;O(1) public int rangeBitwiseAnd(int m, int n) {//m 要赋值给 i&#xff0c;所…

文档排序模型--查询似然Query Likelihood

在概率模型中&#xff0c;我们定义排序函数是基于文档与查询之间的相关度、可能性来做的。f(d,q) p(R1|d,q), R ∈{0,1}。概率模型有几个小分类&#xff1a;   经典概率模型——>BM25   语言模型Language Model —–>Query Likelihood   Divergence from rando…