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. Object level permissions support

    django-guardian (1.1.1+) - Object level permissions support. Home - Django REST framework https://ww ...

  2. 9.5 自定义包和可见性 go mod

    the-way-to-go_ZH_CN/09.5.md at master · Unknwon/the-way-to-go_ZH_CN https://github.com/Unknwon/the-w ...

  3. proc/net/tcp中各项内容的含义

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

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

  5. CNN(Convolutional Neural Network)

    CNN(Convolutional Neural Network) 卷积神经网络(简称CNN)最早可以追溯到20世纪60年代,Hubel等人通过对猫视觉皮层细胞的研究表明,大脑对外界获取的信息由多层的 ...

  6. python--基础1(pip,虚拟环境、python编写规范)

    python简介 1.Python是一种解释型脚本语言; 2.Python在设计上坚持了清晰划一的风格,这使得Python成为一门易读.易维护,并且被大量用户所欢迎的.用途广泛的语言; 3.pytho ...

  7. 2.centos 7清空文件和文件夹

    1.清空文件 测试文件:a.txt 1)方法一,[root@centos test]# > a.txt [root@centos test]# cat a.txt 1hjbfao hjkl23o ...

  8. JavaScript里处理数组的一些常用方法

    修改器方法: 1.pop() 方法从数组中删除最后一个元素 pop() 方法将删除 arrayObject 的最后一个元素,把数组长度减 1,并且返回它删除的元素的值. let arr2 = ['zh ...

  9. codeforces626D . Jerry's Protest (概率)

    Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds ...

  10. NCD 2019 C. Hasan and his lazy students

    题意:给你一组数,求最长的严格上升子序列及个数(mod 1e9+7) 题解:用动态规划来求LIS,记\(dp[i]\)是数组中第i个位置上的数的LIS最优解,我们遍历一遍原数组,然后找i位置前的LIS ...