Home > Web Front-end > JS Tutorial > CSS animation vs JavaScript animation comparison

CSS animation vs JavaScript animation comparison

伊谢尔伦
Release: 2017-01-16 16:37:56
Original
1406 people have browsed it

Advantages of CSS3 animation:

  • The performance will be slightly better, and the browser will make some optimizations for CSS3 animation (such as creating a new layer to run animation )

  • The code is relatively simple

but its shortcomings are also obvious:

  • In animation control Not flexible enough

  • Poor compatibility

Some animation functions cannot be implemented (such as scrolling animation, parallax scrolling, etc.)

JavaScript animation just makes up for these two shortcomings. It has strong control capabilities and can control and transform single frames. At the same time, if it is well written, it is fully compatible with IE6 and has powerful functions. But consider that the transform matrix of CSS animation is a C++-level calculation, which must be faster than JavaScript-level calculations. In addition, dependence on libraries is also a headache.

For some complex controlled animations, it is more reliable to use javascript. When implementing some small interactive effects, consider more CSS.

Based on actual project experience, in the same context, there is little difference in the efficiency of animation between the two schemes. What affects the efficiency more is:

  • Whether it causes layout

  • The area of ​​repaint

  • Whether it has high consumption attributes (css shadow, etc.)

  • Whether to enable hardware acceleration

Dynamic changes in the margin and height of regular flow elements will lead to a large-area layout process, which is equally slow with JS or CSS3.
Dynamically changing the translate3D of the element naturally turns on 3D acceleration. There is no big frame rate difference between using setTimeout/setInterval/requestAnimationFrame and CSS3.

The main difference today is

1. Function coverage, JS is larger than CSS3

  • The @keyframes that define the animation process are not Supports recursive definition. If there are multiple similar animation processes and multiple parameters need to be adjusted to generate them, there will be a lot of redundancy (such as jQuery Mobile’s animation solution), while JS can naturally implement multiple functions with a set of functions. A different animation process

  • On the time scale, the animation granularity of @keyframes is coarse, while the animation granularity control of JS can be very fine

  • CSS3 There are very few time functions supported in animation, which are not flexible enough

  • With the existing interface, CSS3 animation cannot support more than two state transitions

2. The difficulty of implementation/reconstruction varies. CSS3 is simpler than JS, and the performance tuning direction is fixed

3. For low-version browsers with poor frame rate performance, CSS3 can be natural To downgrade, JS needs to write additional code

4. CSS3 has compatibility issues, but JS most of the time does not have compatibility issues


The following will be step by step Tell you why Javascript-based DOM animation libraries (such as Velocity.js and GSAP) can be more efficient than jQuery and CSS-based animation libraries.

jQuery

Let’s start with the basics: Javascript and jQuery don’t mix. Javascript animations are fast, while jQuery animations are slow. why? Because although jQuery is extremely powerful, its design goal is not to be an efficient animation engine:

  • jQuery cannot avoid layout thrashing (some people like to translate it as "layout thrashing", which will cause (redundant relayout/reflow), because its code is not only used for animation, it is also used in many other scenarios.

  • jQuery consumes a lot of memory and often triggers garbage collection. It is easy for the animation to get stuck when garbage collection is triggered.

  • jQuery uses setInterval instead of reqeustAnimationFrame(RAF) because RAF will stop firing when the window loses focus, which will cause jQuery bugs. (Currently jQuery already uses RAF)

Note that layout thrashing will cause the animation to freeze at the beginning, and the triggering of garbage collection will cause the animation to freeze during the running process. Do not use RAF This will result in a low animation frame rate.

Implementation Example

In order to avoid layout thrashing, we need to access and update the DOM in batches.

var currentTop,
    currentLeft;
/* 有 layout thrashing. */
currentTop = element.style.top; /* 访问 */
element.style.top = currentTop + 1; /* 更新 */
currentLeft = element.style.left; /* 访问 */
element.style.left = currentLeft + 1; /* 更新 */
/* 没有 layout thrashing. */
currentTop = element.style.top; /* 访问 */
currentLeft = element.style.left; /* 访问 */
element.style.top = currentTop + 1; /* 更新 */
element.style.left = currentLeft + 1; /* 更新 */
Copy after login

The access operation after the update operation will force the browser to recalculate the style of the page element (because the updated style must be applied to obtain the correct value). This does not cause much performance loss in normal operation, but it will cause significant performance overhead when placed in animations with an interval of only 16ms. Just slightly changing the order of operations can greatly improve animation performance.

Similarly, using RAF does not require you to refactor your code a lot. Let’s compare the difference between using RAF and using setInterval:

var startingTop = 0;
/* setInterval: Runs every 16ms to achieve 60fps (1000ms/60 ~= 16ms). */
setInterval(function() {
    /* Since this ticks 60 times a second, we divide the top property's increment of 1 unit per 1 second by 60. */
    element.style.top = (startingTop += 1/60);
}, 16);
/* requestAnimationFrame: Attempts to run at 60fps based on whether the browser is in an optimal state. */
function tick () {
    element.style.top = (startingTop += 1/60);
}
window.requestAnimationFrame(tick);
Copy after login

You only need to slightly modify the code to use RAF, and your animation performance will be greatly improved.

CSS Transition

  CSS transition 的动画逻辑是由浏览器来执行,所以它的性能能够比 jQuery 动画好。它的优势体现在:

  1. 通过优化 DOM 操作,避免内存消耗来减少卡顿

  2. 使用与 RAF 类似的机制

  3. 强制使用硬件加速 (通过 GPU 来提高动画性能)

  然而实际上Javascript也可以使用这些优化。GSAP 已经做这些优化很久了。Velocity.js 是一个新兴的动画引擎,它不仅仅做了这些优化,甚至走的更远些。我们稍后会谈到这些。

  面对事实,让 Javascript 动画得以媲美 CSS 动画的性能只是我们伟大计划的第一步。第二步才是重头戏,要让 Javascript 动画比 CSS 动画还要快!

  让我们来看看 CSS 动画库的缺陷吧:

  1. Transition 强制使用了 GPU 的硬件加速。导致浏览器一直处于高负荷运转的状态,这反而会让动画变的卡顿。这在移动浏览器上更为严重。(特别要说明的是,当数据在浏览器的主线程和合成线程之间频繁传输的时候特别消耗性能,故容易导致卡顿。某些 CSS 属性,不会受到影响。Adobe 的博客谈到过这个问题。

  2. IE 10以下的浏览器不支持 transition。而目前 IE8 和 IE9 还是很流行的。

  3. transition 不能完全被 Javascript 控制(只能通过 Javascript 来触发 transition),因为浏览器不知道如何同时让 Javascript 控制动画又同时优化动画的性能。

  反过来说: 基于 Javascript 可以决定什么时候启用硬件加速,它可以支持全版本的 IE,并且它完全可以进行批量动画的优化。

Javascript 动画

  所以 Javascript 可以比 CSS transition 性能更好。但是它到底有多块呢?它快到足够可以构建一个3D 动画的demo,通常需要用到 WebGL 才能完成。并且它快到足够搭建一个多媒体小动画,通常需要 Flash 或者 After Effects 才能完成。并且它还快到可以构建一个虚拟世界,通常需要 canvas 才能完成。

  为了更直接的来比较主流动画库的性能,包括 Transit(使用了 CSS transition),让我们打开Velocity的官方文档。

  之前那个问题还在:Javascript 是如何达到高性能的呢?下面是一个列表,列举了基于 Javascript 的动画库能做的事情:

  1. 同步DOM -> 在整个动画链中微调堆栈以达到最小的layout thrashing。

  2. 缓存链式操作中的属性值,这样可以最小化DOM的查询操作(这就是高性能 DOM 动画的阿喀琉斯之踵)

  3. 在同一个跨同层元素的调用中缓存单位转化比率(例如px转换成%、em等等单位)

  4. 忽略那些变动小到根本看不出来的DOM更新

  让我们重新温习下之前学到的关于layout thrashing的知识点。Velocity.js 运用了这些最佳实践,缓存了动画结束时的属性值,在紧接的下一次动画开始时使用。这样可以避免重新查询动画的起始属性值。

$element
    /* Slide the element down into view. */
    .velocity({ opacity: 1, top: "50%" })
    /* After a delay of 1000ms, slide the element out of view. */
    .velocity({ opacity: 0, top: "-50%" }, { delay: 1000 });
Copy after login

在上面的样例中,第二次调用 Velocity 时已经知道了 opacity 的起始值为 1,top 的值为 50%。

  浏览器也可以使用与此类似的优化,但是要做这些事情太过激进,使用场景也会受到限制,开发者就有可能会写出有bug的动画代码。jQuery就是因为这个原因没有使用RAF(如上所说),浏览器永远不会强行实施可能打破规范或者可能偏离期望行为的优化。

  最后,让我们来比较下两个Javascript框架(velocity.js 和 GSAP)。

  • GASP 是一个快速且功能丰富的动画平台。Velocity则更为轻量级,它大大地改善了UI动画性能和工作流程。

  • GSAP 需要付费才能用于商业产品。Velocity 是完全免费的,它使用了自由度极高的 MIT 协议。

  • 性能方面,两者几乎相当,很难区分胜负。

Velocity.js

  之前提到了 GSAP 有着丰富的功能,但这不代表 Velocity 的功能简单。相反的,Velocity 在 zip 压缩之后只有 7kb,它不仅仅实现了 jQuery animate 方法的所有功能,还包含了 颜色、transforms、loop、easings、class 动画和滚动动画等功能。

  简单的说就是 Velocity 包含了 jQuery、 jQuery UI 和 CSS transition 的功能。

  更进一步从易用性的角度来讲,Velocity 使用了 jQuery 的$.queue() 方法,因此可以无缝过渡到 jQuery 的$.animate()、$.fade()和$.delay()方法。并且 Velocity 的语法和$.animate()一摸一样,所以我们根本不需要修改页面的现有代码。

  让我们快速过一下 Velocity.js 的例子:

$element
    .delay(1000)
    /* Use Velocity to animate the element's top property over a duration of 2000ms. */
    .velocity({ top: "50%" }, 2000)
    /* Use a standard jQuery method to fade the element out once Velocity is done animating top. */
    .fadeOut(1000);
Copy after login

  如下是一个高级用法:滚动网页到当前元素并且旋转元素。这样的动画只需要简单的几行代码:

$element
    /* Scroll the browser to the top of this element over a duration of 1000ms. */
    .velocity("scroll", 1000)
    /* Then rotate the element around its Y axis by 360 degrees. */
    .velocity({ rotateY: "360deg" }, 1000);
Copy after login

Velocity 的目标是成为 DOM 动画领域性能最好易用性最高的库。这篇文章主要关注了性能方面。易用性方面可以前往 VelocityJS.org 了解。

请记住一个高性能的 UI 绝不仅仅是选择一个正确的动画库。页面上的其他代码也需要优化。


source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template