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. 初识JavaScript和jQuery

    JavaScript 1.特性 ①脚本语言.JavaScript是一种解释型的脚本语言,C.C++.Java等语言先编译后执行, 而JavaScript是在程序的运行过程中逐行进行解释. ②基于对象. ...

  2. detect data races The cost of race detection varies by program, but for a typical program, memory usage may increase by 5-10x and execution time by 2-20x.

    小结: 1. conflicting access 2.性能危害 优化 The cost of race detection varies by program, but for a typical ...

  3. 应答流式RPC 请求流式RPC 向流式RPC 流式RPC的三种具体形式

    https://mp.weixin.qq.com/s/pWwSfXl71GQZ3KPmAHE_dA 用Python进行gRPC接口测试(二) 大帆船 搜狗测试 2020-02-07   上期回顾:用P ...

  4. 分布式缓存 — MongoDB

    --- 数据库管理系统 数据库管理系统主要分为俩大类:RDBMS.NOSQL.在个人电脑.大型计算机和主机上应用最广泛的数据库管理系统是关系型DBMS.NoSQL是对不同于传统的关系数据库的数据库管理 ...

  5. 笔趣看小说Python3爬虫抓取

    笔趣看小说Python3爬虫抓取 获取HTML信息 解析HTML信息 整合代码 获取HTML信息 # -*- coding:UTF-8 -*- import requests if __name__ ...

  6. Spring boot 使用Redis 消息队列

    package com.loan.msg.config; import com.loan.msg.service.MessageReceiver; import org.springframework ...

  7. 读取EXCEL文档解析工具类

    package test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException ...

  8. java 验证表单工具类,史上最全

    package com.wiker.utils; import java.util.regex.*; /** * * @version 1.0 * @author wiker * @since JDK ...

  9. shell(shell函数、shell正则表达式)

    本章内容 shell函数 shell正则表达式 1.shell函数 linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. 格式: funname () { CMD #函数体 ...

  10. 软件测试漫谈(web测试,自动化测试,Jmeter)

    软件测试,就是一个过程或一系列过程,用来确定计算机代码完成了其应该完成的功能不执行其不该有的操作. 简单说就是找bug的过程. 测试分类 (1)按测试方式分类:静态测试.动态测试 (2) 按测试方法分 ...