js animation & requestAnimationFrame

https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame

window.requestAnimationFrame(callback);

const callback = () => {
window.requestAnimationFrame(callback);
}
// inint
callback();

https://caniuse.com/#feat=requestanimationframe

refs

https://gomakethings.com/how-to-use-requestanimationframe-with-vanilla-js/

https://flaviocopes.com/requestanimationframe/

https://github.com/flaviocopes

https://css-tricks.com/using-requestanimationframe/

http://www.javascriptkit.com/javatutors/requestanimationframe.shtml

See the Pen svg paths (auto player) by xgqfrms
(@xgqfrms) on CodePen.


// define array to store balls
let balls = []; // define loop that keeps drawing the scene constantly
function loop() {
ctx.fillStyle = 'rgba(0,0,0,0.25)';
ctx.fillRect(0,0,width,height);
while(balls.length < 25) {
var ball = new Ball(
random(0,width),
random(0,height),
random(-7,7),
random(-7,7),
'rgb(' + random(0,255) + ',' + random(0,255) + ',' + random(0,255) +')',
random(10,20)
);
balls.push(ball);
}
for(var i = 0; i < balls.length; i++) {
balls[i].draw();
balls[i].update();
balls[i].collisionDetect();
}
// auto loop
requestAnimationFrame(loop);
} loop();

generator *function & yield

const log = console.log;

const d = document.querySelector(`[id="svg-path"]`);

log(`d`, d);
// d <path id=​"svg-path" d=​"M10 10">​</path>​ function *test() {
yield d.setAttribute(`d`, `M10 10L10,100`);
yield d.setAttribute(`stroke`, `#0f0`);
yield d.setAttribute(`d`, `M10 10L10,100 H200`);
yield d.setAttribute(`d`, `M10 10L10,100 H200 V10`);
yield d.setAttribute(`d`, `M10 10L10,100 H200 V10 H10`);
yield d.setAttribute(`fill`, `#f0f`);
yield d.setAttribute(`fill`, `#fff7`);
} let steps = 7;
const loop = () => {
steps = 7;
const app = test();
const flag = setInterval(() => {
if(steps > 0) {
steps--;
app.next(steps);
} else {
clearInterval(flag);
requestAnimationFrame(loop);
}
}, 1000);
}
loop();

requestAnimationFrame 降帧/降频?


refs



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


js animation & requestAnimationFrame的更多相关文章

  1. CSS vs. JS Animation: 哪个更快

    CSS vs. JS Animation: 哪个更快? CSS vs. JS Animation: 哪个更快? 基于JavaScript的动画竟然已经默默地比CSS的transition动画快了?而且 ...

  2. CSS3动画 相比JS Animation 哪个更快?

    CSS vs. JS Animation: 哪个更快? 基于JavaScript的动画竟然已经默默地比CSS的transition动画快了?而且,Adobe和 Google竟然一直在发布可以媲美原生应 ...

  3. 微信小游戏 demo 飞机大战 代码分析 (三)(spirit.js, animation.js)

    微信小游戏 demo 飞机大战 代码分析(三)(spirit.js, animation.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码 ...

  4. js动画最佳实现——requestAnimationFrame

    我们经常用setInterval来实现动画,其实这种做法不是太好,因为不同浏览器的刷新频率也不一样(一般认为设置16为最佳,按每秒60帧算,1000/60≍16.67) var dis = 0,tim ...

  5. WebGL与three.js

    前面学习了一些webgl的基础知识,现在就用一下three.js写一个小例子,记录一下学习的过程. 效果图: 1.去github下载three.js,然后将它加载到网页中 <script src ...

  6. 移动端 transition动画函数的封装(仿Zepto)以及 requestAnimationFrame动画函数封装(仿jQuery)

    移动端 css3 transition 动画 ,requestAnimationFrame 动画  对于性能的要求,h5优先考虑: 移动端 单页有时候 制作只用到简单的css3动画即可,我们封装一下, ...

  7. webGL之three.js入门2

    入门建议: webGL中文翻译教程,基于NeHe的openGL教程:http://www.hiwebgl.com/?p=42 . WebGL中文网 http://www.hewebgl.com/ ,里 ...

  8. requestAnimationFrame优势何在?

    大概半年前,无意中在网上看到一个新的js函数requestAnimationFrame,据说,此函数可以优化传统的js动画效果,似乎是未来js动画的新方向. 当时我所在的项目正好用到了和js动画有关的 ...

  9. Three.js基础:建立Cube并实现鼠标交互,动画旋转

    index.html文件: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

随机推荐

  1. 回归测试_百度百科 https://baike.baidu.com/item/%E5%9B%9E%E5%BD%92%E6%B5%8B%E8%AF%95

    回归测试_百度百科https://baike.baidu.com/item/%E5%9B%9E%E5%BD%92%E6%B5%8B%E8%AF%95

  2. JavaScript代码是怎么执行的?

    前言 众所周知,JavaScript是单线程语言.所以JavaScript是按顺序执行的! 先编译再执行 变量提升 请看下面的例子: console.log(cat) catName("Ch ...

  3. 精通MySQL之索引篇,这篇注重练习!

    老刘是即将找工作的研究生,自学大数据开发,一路走来,感慨颇深,网上大数据的资料良莠不齐,于是想写一份详细的大数据开发指南.这份指南把大数据的[基础知识][框架分析][源码理解]都用自己的话描述出来,让 ...

  4. Flutter GetX使用---简洁的魅力!

    前言 使用Bloc的时候,有一个让我至今为止十分在意的问题,无法真正的跨页面交互!在反复的查阅官方文档后,使用一个全局Bloc的方式,实现了"伪"跨页面交互,详细可查看:flutt ...

  5. Pycharm 使用学习

    作为一个菜鸟,为了督促自己坚持学习python,记录每日学习日记是一个不错的选择 电脑安装python,python可以丛网络上下载相关版本进行安装,目前我电脑安装的是pyhon 3.7.3的版本,p ...

  6. GJ项目技术代码相关总结

    第一次实习公司的GJ项目快要结束,自己总结了一些工作中的代码,留到记录学习. 根据下拉条件,进行查询,展示出不同的表单选项:并在鼠标进入到指定区域时显示部分内容,鼠标移出内容区域时,隐藏内容. 焦点移 ...

  7. (转)pip和easy_install使用方式

    easy_install 跟 pip 都是 Python 的套件管理程式,有了它們,在使用 Python 開發程式的時候會帶來不少方便. easy_install 和 pip 有什麼不一樣?據 pip ...

  8. Codeforces Round #646 (Div. 2) C. Game On Leaves(树上博弈)

    题目链接:https://codeforces.com/contest/1363/problem/C 题意 有一棵 $n$ 个结点的树,每次只能取叶子结点,判断谁能最先取到结点 $x$ . 题解 除非 ...

  9. codeblocks从安装到环境配置

    在去官网下载codeblocks的时候可不要只下载一个外壳: 这个就是外壳 你安装之后还是不能编译程序<_> 你要下载集成环境,例如 这样这里面已经带了一些编译器,你就不需要去下载各种插件 ...

  10. A - Promotions

    题目详见http://7xjob4.com1.z0.glb.clouddn.com/3f644de6844d64706eb36baa3a0c27b0 这题是普通的拓扑排序,要把每一层的都保存下来. # ...