Table of Contents
What is CSS Motion Path?
CSS Traditional Way to Implement Linear Path Animation
CSS traditional way to implement curve path animation
CSS Motion Path implements straight path animation
CSS Motion Path 实现曲线路径动画
理解 offset-anchor 运动锚点
运用 Motion Path 制作动画效果
利用 Motion Path 制作按钮效果
Use Motion-Path to draw map path pathfinding animation
Use Motion-Path to draw path animation
Can i Use - Motion-Path
Finally
Home Web Front-end CSS Tutorial What is the CSS motion path module? How to animate motion paths?

What is the CSS motion path module? How to animate motion paths?

Jul 02, 2021 am 10:38 AM
css3 animation

What is the CSS motion path module? In this article, we will learn more about the CSS motion path module, talk about its usage, and introduce how to use this module to create simple and complex path animations.

What is the CSS motion path module? How to animate motion paths?

There is a very interesting module in CSS - CSS Motion Path Module Level 1, which translates to motion path. This article will take a closer look at motion path. Through this article, you can learn:

  • What is CSS motion path
  • Use CSS motion path to create simple path animations
  • Use CSS motion path to create complex path animations

What is CSS Motion Path?

What is CSS Motion Path? Using the properties stipulated in this specification, we can control the animation of the element's position transformation according to a specific path. Moreover, this path can be a very complex path.

Before introducing CSS Motion Path further, let’s first look at how we can implement path animation using the capabilities of traditional CSS.

CSS Traditional Way to Implement Linear Path Animation

Before, we wanted to move an object in a straight line from point A to point B. Generally speaking, we can use transform: translate(), top | left | bottom | right or margin and other attributes that can change the position of the object.

A simple Demo:

<div></div>
Copy after login
div {
    width: 60px;
    height: 60px;
    background: #000;
    animation: move infinite 1s alternate linear;
}
@keyframes move {
    100% {
        transform: translate(100px, 100px);
    }
}
Copy after login

The effect of simple linear motion from point A to point B is as follows:

What is the CSS motion path module? How to animate motion paths?

CSS traditional way to implement curve path animation

Of course, CSS can also implement some simple curve path animation. What should we do if we want to move from point A to point B not in a straight line, but in a curve?

For some simple arc curve paths, you can still achieve it with the help of some clever methods. Take a look at the following example.

This time, we used two elements. The child element is a small ball that we hope to move in a curve. But in fact, we set the transform-origin of the parent element to make The parent element performed a transform: rotate() movement that drove the ball of the child element:

<div class="g-container">
    <div class="g-ball"></div>
</div>
Copy after login
.g-container {
    position: relative;
    width: 10vmin;
    height: 70vmin;
    transform-origin: center 0;
    animation: rotate 1.5s infinite alternate;
}
.g-ball {
    position: absolute;
    width: 10vmin;
    height: 10vmin;
    border-radius: 50%;
    background: radial-gradient(circle, #fff, #000);
    bottom: 0;
    left: 0;
}
@keyframes rotate {
    100% {
        transform: rotate(90deg);
    }
}
Copy after login

In order to facilitate understanding, during the movement, I let the outline of the parent element appear. Come out:

What is the CSS motion path module? How to animate motion paths?

In this way, we can barely get a non-linear path motion animation, and its actual motion trajectory is a curve.

However, this is basically the limit of what CSS can do before. Using pure CSS methods, there is no way to achieve more complex path animations, such as the following path animation:

2-What is the CSS motion path module? How to animate motion paths?

Until now, we have a more powerful specification dedicated to doing this, which is the protagonist of this article-- CSS Motion Path.

CSS Motion Path implements straight path animation

CSS Motion Path specification mainly includes the following attributes:

  • ##offset- path: Receive an SVG path (similar to SVG path and clip-path in CSS), specify the geometric path of motion
  • offset-distance: Control the current element based on offset-path Movement distance
  • offset-position: Specify the initial position of offset-path
  • offset -anchor: Defines the anchor point of the element positioned along offset-path. This is easy to understand. The moving element may not be a point, so you need to specify which point in the element is attached to the path for movement
  • offset-rotate: define along offset-path is the direction of the element during positioning. In human terms, it means the angle of the element during movement.
Below, we use Motion Path to implement a simple linear displacement animation.

<div></div>
Copy after login
Copy after login
div {
    width: 60px;
    height: 60px;
    background: linear-gradient(#fc0, #f0c);
    offset-path: path("M 0 0 L 100 100");
    offset-rotate: 0deg;
    animation: move 2000ms infinite alternate ease-in-out;
}
@keyframes move {
    0% {
        offset-distance: 0%;
    }
    100% {
        offset-distance: 100%;
    }
}
Copy after login

offset-path Receives the path of an SVG. Here our path content is a custom path path("M 0 0 L 100 100") , translated as moving from 0 0 point to 100px 100px point.

offset-path Receives an SVG path specifying the geometric path of motion. Similar to SVG path and clip-path in CSS, if you don’t know much about this SVG Path, you can click here to learn about the SVG path content: SVG path
We will get the following results:

What is the CSS motion path module? How to animate motion paths?

Perform path animation of the element by controlling the element's

offset-distance from 0% to 100%.

Of course, the above animation is the most basic. I can make full use of the characteristics of path, add multiple intermediate key frames, and slightly modify the above code:

div {
    // 只改变运动路径,其他保持一致
    offset-path: path("M 0 0 L 100 0 L 200 0 L 300 100 L 400 0 L 500 100 L 600 0 L 700 100 L 800 0");
    animation: move 2000ms infinite alternate linear;
}
@keyframes move {
    0% {
        offset-distance: 0%;
    }
    100% {
        offset-distance: 100%;
    }
}
Copy after login

这里最主要还是运用了 path 中的 L 指令,得到了如下图这样一条直线路径:

What is the CSS motion path module? How to animate motion paths?

最终的效果如下,与利用 transform: translate() 添加多个关键帧类似:

What is the CSS motion path module? How to animate motion paths?

完整的 Demo :CodePen Demo -- CSS Motion Path Demo

地址:https://codepen.io/Chokcoco/pen/gOgqoem

CSS Motion Path 实现曲线路径动画

上面的运动轨迹都是由直线构成,下面我们看看如何使用 CSS Motion Path 实现曲线路径动画。

其实原理还是一模一样,只需要在 offset-path: path() 中添加曲线相关的路径即可。

在 SVG 的 Path 中,我们取其中一种绘制曲线的方法 -- 贝塞尔曲线,譬如下述这条 path,其中的 path 为 d="M 10 80 C 80 10, 130 10, 190 80 S 300 150, 360 80"

<svg width="400"    style="max-width:90%" xmlns="http://www.w3.org/2000/svg">
  <path d="M 10 80 C 80 10, 130 10, 190 80 S 300 150, 360 80" stroke="black" fill="transparent"/>
</svg>
Copy after login

对应这样一条连续的贝塞尔曲线:

What is the CSS motion path module? How to animate motion paths?

将对应的路径应用在 offset-path: path 中:

<div></div>
Copy after login
Copy after login
div:nth-child(2) {
    width: 40px;
    height: 40px;
    background: linear-gradient(#fc0, #f0c);
    offset-path: path(&#39;M 10 80 C 80 10, 130 10, 190 80 S 300 150, 360 80&#39;);
}
@keyframes move {
    0% {
        offset-distance: 0%;
    }
    100% {
        offset-distance: 100%;
    }
}
Copy after login

可以得到如下运动效果:

What is the CSS motion path module? How to animate motion paths?

可以看到,元素是沿着贝塞尔曲线的路径进行运动的,并且,由于这次没有限制死 offset-rotate,元素的朝向也是跟随路径的朝向一直变化的。(可以联想成开车的时候,车头一直跟随道路会进行变化的,带动整个车身的角度变化)

完整的 Demo :CodePen Demo -- CSS Motion Path Demo

地址:https://codepen.io/Chokcoco/pen/gOgqoem

理解 offset-anchor 运动锚点

OK,那么接下来,我们再看看 offset-anchor 如何理解。

还是上述的 DEMO,我们把小正方形替换成一个三角形,并且把运动的曲线给画到页面上,像是这样:

What is the CSS motion path module? How to animate motion paths?

其中,三角形是通过 clip-path 实现的:

    width: 40px;
    height: 40px;
    clip-path: polygon(0 0, 100% 50%, 0 100%);
    background: linear-gradient(#fc0, #f0c);
Copy after login

What is the CSS motion path module? How to animate motion paths?

通常而言,沿着曲线运动的是物体的中心点(类比 transform-origin),在这里,我们可以通过 offset-anchor 改变运动的锚点,譬如,我们希望三角形的最下方沿着曲线运动:

.ball {
    width: 40px;
    height: 40px;
    clip-path: polygon(0 0, 100% 50%, 0 100%);
    offset-path: path(&#39;M 10 80 C 80 10, 130 10, 190 80 S 300 150, 360 80&#39;);
    offset-anchor: 0 100%;
    background: linear-gradient(#fc0, #f0c);
    animation: move 3000ms infinite alternate linear;
}
@keyframes move {
    0% {
        offset-distance: 0%;
    }
    100% {
        offset-distance: 100%;
    }
}
Copy after login

What is the CSS motion path module? How to animate motion paths?

经过实测,Can i use 上写着 offset-anchor 属性的兼容性在为 Chrome 79+、Firefox 72+,但是实际只有 Firefox 支持,Chrome 下暂时无法生效~

完整的 Demo :CodePen Demo -- CSS Motion Path offset-anthor Demo

地址:https://codepen.io/Chokcoco/pen/poRGZeE

运用 Motion Path 制作动画效果

OK,上面我们基本把原理给过了一遍,下面我们就看看,运用 Motion Path,可以在实践中如何运用。

利用 Motion Path 制作按钮效果

利用运动路径,我们可以制作一些简单的按钮点击效果。在之前,我在 CodePen 上见到过这样一种按钮点击效果:

1What is the CSS motion path module? How to animate motion paths?

其原理是运用了 background-radial 去生成每一个小圆点,通过控制 background-position 控制小圆点的位移

详细的 Demo 代码:CodePen Demo -- Bubbly button (Design by Gal Shir)

地址:https://codepen.io/Chokcoco/pen/bGGMLdd

但是小圆点的运动路径基本上都是直线,运用本文的 Motion Path,我们也可以实现一些类似的效果,核心代码如下,HTML 这里我们使用了 Pug 模板,CSS 使用了 SASS

.btn
  -for(var i=0; i<60; i++)
    span.dot
Copy after login
.btn {
  position: relative;
  padding: 1.5rem 4.5rem;
}
.btn .dot {
  position: absolute;
  width: 4px;
  height: 4px;
  
  @for $i from 1 through $count { 
    &:nth-child(#{$i}) {
        top: 50%;
        left: 50%;
        transform: translate3d(-50%, -50%, 0) rotate(#{360 / $count * $i}deg);
      }
  }
  
  &::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 4px;
    height: 4px;
    border-radius: 50%;
    offset-path: path("M0 1c7.1 0 10.7 2 14.3 4s7.1 4 14.3 4 10.7-2 14.3-4 7.2-4 14.3-4 10.7 2 14.3 4 7.1 4 14.3 4 10.7-2 14.3-4 7.1-4 14.3-4 10.7 2 14.3 4 7.1 4 14.3 4 10.7-2 14.3-4 7.1-4 14.3-4 10.7 2 14.3 4 7.1 4 14.3 4");
    offset-distance: 0;
  }
}

.btn.is-animating:active .dot:nth-child(4n+1)::before {
  animation: dot var(--animation-time) var(--animation-timging-function);
}
.btn.is-animating:active .dot:nth-child(4n+2)::before {
  border: 1px solid var(--color-primary);
  background: transparent;
  animation: dot var(--animation-time) var(--animation-timging-function) 0.1s;
}
.btn.is-animating:active .dot:nth-child(4n+3)::before {
  animation: dot var(--animation-time) var(--animation-timging-function) 0.2s;
}
.btn.is-animating:active .dot:nth-child(4n)::before {
  border: 1px solid var(--color-primary);
  background: transparent;
  animation: dot var(--animation-time) var(--animation-timging-function) 0.3s;
}

@keyframes dot {
  0% {
    offset-distance: 0%;
    opacity: 1;
  }
  90% {
    offset-distance: 60%;
    opacity: .5;
  }
  100% {
    offset-distance: 100%;
    opacity: 0;
  }
}
Copy after login

别看代码多有一点点复杂,但是不难理解,本质就是给每个子元素小点点设置同样的 offset-path: path(),给不同分组下的子元素设定不同的旋转角度,并且利用了动画延迟 animation-delay 设定了 4 组同时出发的动画。

这里我们的轨迹 path 不是直线,效果如下:

1What is the CSS motion path module? How to animate motion paths?

Complete code: CodePen Demo -- Button Animation with CSS Offset Paths

Address: https://codepen.io/Chokcoco/pen/xxgMPzJ

Use Motion-Path to draw map path pathfinding animation

This is also very practical. Now we can fully use CSS Motion-Path to realize the pathfinding animation on the map. Pathfinding animation:

1What is the CSS motion path module? How to animate motion paths?

The Demo comes from Ahmad Emran, the complete code: CodePen Demo -- CodePen Home Animation with offset-path | Only Using CSS & HTML

Address: https://codepen.io/ahmadbassamemran/pen/bXByBv

Use Motion-Path to draw path animation

again Or, we can use Path to draw any path to realize various paths we want, such as parabolas added to shopping carts, or various motion trajectories. Here is another Demo:

What is the CSS motion path module? How to animate motion paths?

CodePen Demo -- CSS Motion Path offset-path animation

Address: https://codepen.io/Chokcoco/pen/dyNaZea

Can i Use - Motion-Path

Let’s take a look at the current compatibility of Motion-Path? As of 2021-04-27.

Can i Use - Motion-Path:

What is the CSS motion path module? How to animate motion paths?

#Currently, apart from the IE browser, we are waiting for when Safari will be compatible and whether to use it , but also need to make choices based on the browser usage of the target group.

Finally

Okay, this article ends here. It introduces the motion path animation Motion Path, and uses it to achieve some path animation effects that could not be easily achieved in the past. Hope it helps you:)

This article is reproduced from: https://segmentfault.com/a/1190000039916159

Author: chokcoco

More For more programming related knowledge, please visit: programming video! !

The above is the detailed content of What is the CSS motion path module? How to animate motion paths?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to speed up animation effects in Windows 11: 2 methods explained How to speed up animation effects in Windows 11: 2 methods explained Apr 24, 2023 pm 04:55 PM

When Microsoft launched Windows 11, it brought a lot of changes. One of the changes is an increase in the number of user interface animations. Some users want to change the way things appear, and they have to find a way to do it. Having animations makes it feel better and more user-friendly. Animation uses visual effects to make the computer look more attractive and responsive. Some of them include sliding menus after a few seconds or minutes. There are many animations on your computer that can affect PC performance, slow it down, and interfere with your work. In this case you have to turn off animation. This article will introduce several ways that users can improve the speed of their animations on PC. You can apply the changes using Registry Editor or a custom file you run. How to improve animations in Windows 11

Animation not working in PowerPoint [Fixed] Animation not working in PowerPoint [Fixed] Feb 19, 2024 am 11:12 AM

Are you trying to create a presentation but can't add animation? If animations are not working in PowerPoint on your Windows PC, then this article will help you. This is a common problem that many people complain about. For example, animations may stop working during presentations in Microsoft Teams or during screen recordings. In this guide, we will explore various troubleshooting techniques to help you fix animations not working in PowerPoint on Windows. Why aren't my PowerPoint animations working? We have noticed that some possible reasons that may cause the animation in PowerPoint not working issue on Windows are as follows: Due to personal

CSS Animation: How to Achieve the Flash Effect of Elements CSS Animation: How to Achieve the Flash Effect of Elements Nov 21, 2023 am 10:56 AM

CSS animation: How to achieve the flash effect of elements, specific code examples are needed. In web design, animation effects can sometimes bring a good user experience to the page. The glitter effect is a common animation effect that can make elements more eye-catching. The following will introduce how to use CSS to achieve the flash effect of elements. 1. Basic implementation of flash First, we need to use the animation property of CSS to achieve the flash effect. The value of the animation attribute needs to specify the animation name, animation execution time, and animation delay time

How to set up ppt animation to enter first and then exit How to set up ppt animation to enter first and then exit Mar 20, 2024 am 09:30 AM

We often use ppt in our daily work, so are you familiar with every operating function in ppt? For example: How to set animation effects in ppt, how to set switching effects, and what is the effect duration of each animation? Can each slide play automatically, enter and then exit the ppt animation, etc. In this issue, I will first share with you the specific steps of entering and then exiting the ppt animation. It is below. Friends, come and take a look. Look! 1. First, we open ppt on the computer, click outside the text box to select the text box (as shown in the red circle in the figure below). 2. Then, click [Animation] in the menu bar and select the [Erase] effect (as shown in the red circle in the figure). 3. Next, click [

After a two-year delay, the domestic 3D animated film 'Er Lang Shen: The Deep Sea Dragon' is scheduled to be released on July 13 After a two-year delay, the domestic 3D animated film 'Er Lang Shen: The Deep Sea Dragon' is scheduled to be released on July 13 Jan 26, 2024 am 09:42 AM

This website reported on January 26 that the domestic 3D animated film "Er Lang Shen: The Deep Sea Dragon" released a set of latest stills and officially announced that it will be released on July 13. It is understood that "Er Lang Shen: The Deep Sea Dragon" is produced by Mihuxing (Beijing) Animation Co., Ltd., Horgos Zhonghe Qiancheng Film Co., Ltd., Zhejiang Hengdian Film Co., Ltd., Zhejiang Gongying Film Co., Ltd., Chengdu The animated film produced by Tianhuo Technology Co., Ltd. and Huawen Image (Beijing) Film Co., Ltd. and directed by Wang Jun was originally scheduled to be released in mainland China on July 22, 2022. Synopsis of the plot of this site: After the Battle of the Conferred Gods, Jiang Ziya took the "Conferred Gods List" to divide the gods, and then the Conferred Gods List was sealed by the Heavenly Court under the deep sea of ​​Kyushu Secret Realm. In fact, in addition to conferring divine positions, there are also many powerful evil spirits sealed in the Conferred Gods List.

How to use Vue to implement typewriter animation effects How to use Vue to implement typewriter animation effects Sep 19, 2023 am 09:33 AM

How to use Vue to implement typewriter animation special effects Typewriter animation is a common and eye-catching special effect that is often used in website titles, slogans and other text displays. In Vue, we can achieve typewriter animation effects by using Vue custom instructions. This article will introduce in detail how to use Vue to achieve this special effect and provide specific code examples. Step 1: Create a Vue project First, we need to create a Vue project. You can use VueCLI to quickly create a new Vue project, or manually

The final trailer for Netflix's claymation film 'Chicken Run 2” has been announced and will be released on December 15 The final trailer for Netflix's claymation film 'Chicken Run 2” has been announced and will be released on December 15 Nov 20, 2023 pm 01:21 PM

The final trailer for Netflix's claymation film "Chicken Run 2" has been released. The film is expected to be released on December 15. This site noticed that the trailer for "Chicken Run 2" shows Chicken Loki and King Kong. Jay launches an operation to find his daughter Molly. Molly is taken away by a truck at FunLand Farm, and Rocky and Ginger risk their lives to retrieve their daughter. The film is directed by Sam Fehr and stars Sandy Way Newton, Zachary Levi, Bella Ramsey, Imelda Staunton and David Bradley. It is understood that "Chicken Run 2" is the sequel to "Chicken Run" after more than 20 years. The first work was released in China on January 2, 2001. It tells the story of a group of chickens who face the fate of being turned into chicken pies in a chicken factory.

How to disable animations in Windows 11 How to disable animations in Windows 11 Apr 16, 2023 pm 11:34 PM

Microsoft Windows 11 includes many new features and functions. The user interface has been updated and the company has also introduced some new effects. By default, animation effects are applied to controls and other objects. Should I disable these animations? Although Windows 11 features visually appealing animations and fade effects, they can cause your computer to feel sluggish to some users as they add a bit of lag to certain tasks. It's easy to turn off animations for a more responsive user experience. After we see what other changes have been made to the operating system, we'll walk you through how to turn animation effects on or off in Windows 11. We also have an article on how to

See all articles