当前位置:网站首页>requestAnimationFrame的应用
requestAnimationFrame的应用
2022-08-11 11:13:00 【checkMa】
requestAnimationFrame的应用
案例
何时启用 requestAnimationFrame,何时取消,下面是做了一个小案例。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style> .content{
position: relative; } .content > span{
position: absolute; width: 10px; height: 10px; border: 1px solid #bbb; padding: 10px; } </style>
</head>
<body>
<div id="box">
<button id="btn">click</button>
</div>
<div id="content" class="content">
</div>
<script> let list = []; let animating = null; btn.onclick =()=>{
list.push({
left: 0, top: 0 }); animating && window.cancelAnimationFrame(animating) animate() } function render(){
let str = ""; for(let item of list ){
if(item.left < 200){
item.left += easeIn(item.left, 1, 0.02, 200); } if(item.top < 200){
item.top += easeIn(item.top, 1, 0.02, 200); } if(item.top < 200 && item.left < 200){
str += `<span style="left: ${
item.left}px;top: ${
item.top}px"></span>` } } list = list.filter(el=> (el.top < 200 && el.left < 200)); content.innerHTML = str; } function animate(){
render(); if(list.length > 0){
animating = window.requestAnimationFrame(animate) } } function linear(speed){
return speed; } function quicken(value, speed, factor){
return speed + value * factor; } function easeIn(value, speed, factor, max){
if(value < max/2){
return speed + value * factor; }else{
return speed + ( max - value ) * factor; } } </script>
</body>
</html>
边栏推荐
- 神经网络可视化有3D版本了,美到沦陷!(已开源)
- HTM5学习:第一阶段02
- chrome is set to dark mode (including the entire webpage)
- 开源汇智创未来 | 软通动力出席开放原子全球开源峰会OpenAtom openEuler分论坛
- 从零开始配置 vim(12)——主题配置
- 数据库内核面试中我不会的问题(4)
- Notes and Recommendations for Using Logs
- B端产品需求分析与优先级判断
- go语言学习:并发编程(Sync/GMP/爬虫案例)
- How to explain to my girlfriend what is cache penetration, cache breakdown, cache avalanche?
猜你喜欢
大疆2022秋招笔试 —— 最小时间差、数组的最小偏移量
C语言手写魂斗罗(一)
从零开始配置 vim(12)——主题配置
openEuler小程序会议指南
独家采访 | 智能源于自发产生而非计划:进化论拥趸,前OpenAI研究经理、UBC大学副教授Jeff Clune
开源汇智创未来 | 软通动力出席开放原子全球开源峰会OpenAtom openEuler分论坛
分析 Flink 任务如何超过 YARN 容器内存限制
How to explain to my girlfriend what is cache penetration, cache breakdown, cache avalanche?
chrome设置为深色模式(包括整个网页)
Go-Excelize API源码阅读(七)—— CopySheet(from, to int)
随机推荐
Summary of darknet structures
a-upload上传图片
嵌入式开发:提示和技巧——退出时休眠
servlet——servlet介绍 | 发布动态资源
在这个数字化的时代,如何做好用户体验与应用性能管理
Flexmonster 数据透视表和图表组件
Notes and Recommendations for Using Logs
齐话存储未来,诠释分布式缘起
5. 内部类
如何在游戏中实现一场下雨效果
【2022】【论文笔记】基于激光直写氧化石墨烯纸的超薄THz偏转——
巧用自定义函数,文本控件秒变高速缓存
1. 类与对象——什么是对象
独家采访 | 智能源于自发产生而非计划:进化论拥趸,前OpenAI研究经理、UBC大学副教授Jeff Clune
AWS、Splunk和Symantec牵头成立OCSF开放网络安全架构框架
fetch请求设置请求头错误导致无法跨域
AcWing 273. 分级(线性DP+结论)
HyperLynx(五)反射仿真
Bitmap这个“内存刺客”你也要小心
六、一起学习Lua 循环