requestanimationframe

原生javascript封装动画库

一个人想着一个人 提交于 2020-01-25 00:17:53
****转载自自己发表于牛人部落专栏的文章**** 一、前言 本文记录了自己利用原生javascript构建自己的动画库的过程,在不断改进的过程中,实现以下动画效果: 针对同一个dom元素上相继发生的动画,针对以下功能,尝试实现方案,(从一个元素向多个元素的拓展并不难,这里不做深入探究): 功能1.知道动画A和动画B的发生顺序(如A先发生,B后发生),能够按照代码撰写顺序实现动画A结束时,动画B调用 功能2.在满足功能1的基础上更进一步,当不知道动画A和动画B的发生顺序(如点击按钮1触发动画A,点击按钮2触发动画B,哪个按钮先点击不确定),能够达到1)两个动画不产生并发干扰;2)可以根据按钮的先后点击顺序,一个动画结束后另一个动画运行,即实现动画序列,以及动画的链式调用。 整个代码实现的过程,是不断改进的过程,包括: 1.利用requestAnimationFrame替代setTimeout来实现动画的平滑效果。 关于requestAnimationFrame的更多资料可参考这篇博客:http://www.zhangxinxu.com/wordpress/2013/09/css3-animation-requestanimationframe-tween-%E5%8A%A8%E7%94%BB%E7%AE%97%E6%B3%95/ 2.尝试引入promise

Achieve somewhat stable framerate with requestAnimationFrame?

匆匆过客 提交于 2020-01-23 05:16:41
问题 I am playing around with the requestAnimationFrame but I get very jerky animations in any other browser than Chrome. I create an object like this: var object = function() { var lastrender = (new Date()).getTime(); var delta = 0; return { update: function() { //do updates using delta value in calculations. }, loop: function() { var looptimestamp = (new Date()).getTime(); delta = looptimestamp - lastrender; lastrender = looptimestamp; this.update(); window.requestAnimationFrame(this.loop.bind

【前端性能】Web 动画帧率(FPS)计算

回眸只為那壹抹淺笑 提交于 2020-01-21 13:35:59
我们知道,动画其实是由一帧一帧的图像构成的。有 Web 动画那么就会存在该动画在播放运行时的帧率。而帧率在不同设备不同情况下又是不一样的。 有的时候,一些复杂或者重要动画,我们需要实时监控它们的帧率,或者说是需要知道它们在不同设备的运行状况,从而更好的优化它们,本文就是介绍 Web 动画帧率(FPS)计算方法。 流畅动画的标准 首先,理清一些概念。FPS 表示的是每秒钟画面更新次数。我们平时所看到的连续画面都是由一幅幅静止画面组成的,每幅画面称为一帧,FPS 是描述“帧”变化速度的物理量。 理论上说,FPS 越高,动画会越流畅,目前大多数设备的屏幕刷新率为 60 次/秒,所以通常来讲 FPS 为 60 frame/s 时动画效果最好,也就是每帧的消耗时间为 16.67ms。 当然,经常玩 FPS 游戏的朋友肯定知道,吃鸡/CSGO 等 FPS 游戏推荐使用 144HZ 刷新率的显示器,144Hz 显示器特指每秒的刷新率达到 144Hz 的显示器。相较于普通显示器每秒60的刷新速度,画面显示更加流畅。因此144Hz显示器比较适用于视角时常保持高速运动的第一人称射击游戏。 不过,这个只是显示器提供的高刷新率特性,对于我们 Web 动画而言,是否支持还要看浏览器,而大多数浏览器刷新率为 60 次/秒。 直观感受,不同帧率的体验: 帧率能够达到 50 ~ 60 FPS 的动画将会相当流畅

How to call requestAnimFrame on an object method?

给你一囗甜甜゛ 提交于 2020-01-03 04:45:32
问题 Say I have the following object: function Foo(value){ this.bar = value; this.update(); } Foo.prototype.update = function(){ console.log(this.bar); this.bar++; requestAnimationFrame(this.update); } Foo.prototype.setBar(value){ this.bar = value; } This does not work. FireFox gives me an error: NS_ERROR_ILLEGAL_VALUE: Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) [nsIDOMWindow.requestAnimationFrame] I would like to know why, and what other solution could be used instead to

requestAnimationFrame at beginning or end of function?

这一生的挚爱 提交于 2020-01-01 08:38:31
问题 If I have a loop using requestAnimationFrame like this: function render() { // Rendering code requestAnimationFrame(render); } Will there be any difference if I put the requestAnimationFrame in the beginning of the function, like this: function render() { requestAnimationFrame(render); // Rendering code } I haven't noticed any difference, but I have seen both implementations, is one of them better in any way, or are they the same? Edit: One thing I have thought about is, if I put it in the

For which browsers are we using Paul Irish's requestAnimationFrame shim?

ぐ巨炮叔叔 提交于 2020-01-01 08:03:41
问题 Paul Irish has a post called requestAnimationFrame for Smart Animating. Now Paul is a smart guy - and I'm just trying to understand the scope of the application of this idea. He says to do HTML5 animation - you should use a requestAnimationFrame shim like this: // shim layer with setTimeout fallback window.requestAnimFrame = (function(){ return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function( callback ){ window.setTimeout

How to control animation speed (requestAnimationFrame)?

三世轮回 提交于 2019-12-30 11:13:31
问题 I change the text color with requestAnimationFrame(animate); function: requestAnimationFrame(animate); function animate(time){ ... // change text color here if (offset_s < offset_e) {requestAnimationFrame(animate);} } offset_s and offset_s indicates start and end positions of the text for color change. In some cases the animation should last for 2 seconds, but in order cases - for 5 seconds, but offset_e - offset_s could be the same in these two cases. What can I do to control the speed of

requestAnimationFrame garbage collection

百般思念 提交于 2019-12-28 05:09:29
问题 I'm profiling the following code's memory usage using the Timeline in Chrome Dev Tools v27. <!DOCTYPE html> <html> <head> <meta http-equiv='content-type' content='text/html; charset=UTF-8' /> <title>RAF</title> </head> <body> <script type='text/javascript' charset='utf-8'> var frame = function() { window.webkitRequestAnimationFrame(frame); }; window.webkitRequestAnimationFrame(frame); </script> </body> </html> Notice it's simple. But eventually I see the a tooth pattern appear that indicates

is requestAnimationFrame run concurrently with other scripts?

耗尽温柔 提交于 2019-12-25 07:02:59
问题 does requestAnimationFrame run the functions queued up on it concurrently with other normally run javascript, i.e. from user input or from setTimeout/setInterval? 回答1: It runs on the same single thread like everything else (except Workers), so yes. 来源: https://stackoverflow.com/questions/14667440/is-requestanimationframe-run-concurrently-with-other-scripts

RequestAnimationFrame position in code

橙三吉。 提交于 2019-12-25 06:59:50
问题 Can someone please share where you should have the RequestAnimationFrame call in the animation loop. Should it be at the beginning of the loop or the end of the loop? I've seen it written about many times with it at the beginning of the loop but wouldn't it be better to be at the bottom of the loop so it won't be called while it might still be processing? Or does the RequestAnimationFrame have a check to see if it is already running and not run it if it is running already? Or does it not