gpt4 book ai didi

javascript - 在 requestAnimationFrame 中传递类的方法

转载 作者:太空宇宙 更新时间:2023-11-04 15:28:50 25 4
gpt4 key购买 nike

当我在控制台中调用 app.update() 时,这是有效的,但是当我使用 requestAnimationFrame 时,出现错误:

Uncaught TypeError: Cannot read property 'drops' of undefined at update (oee.html:40)

它在使用setInterval时有效。

我错过了什么?

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style media="screen">
*{
margin:0;
padding:0;
}
#app{
height:100vh;
width:100vw;
background-color: red;
overflow: hidden;
}
.drop{
position:absolute;
background-color:#fff;
top:10px;
width: 5px;
}
</style>
</head>
<body>
<div id="app">
</div>
<script type="text/javascript">
class App{
constructor(){
this.el = document.getElementById("app");
this.height=this.el.clientHeight;
this.width=this.el.clientWidth;
this.drops=[];
for(var i=0;i<100;i++){
this.drops.push(new Drop(this.height,this.width));
}
}
update(){
this.drops.forEach(function(drop){
drop.update();
});
}
}

class Drop{
constructor(appHeight,appWidth){
this.speed=Math.random();
this.el=document.createElement("div");
this.el.setAttribute("class","drop");
this.el.style.height=(Math.random()*10+5)+"px";
this.el.style.left=(Math.random()*appWidth)+"px";
this.appHeight=appHeight;
document.getElementById("app").appendChild(this.el);
this.el.style.top=0;
}

update(){
this.top=this.el.style.top.replace("px","");
this.el.style.top=(this.top>this.appHeight)?"0px":(parseFloat(this.top) + parseFloat(this.speed*300))+"px";
}
}
var app=new App();
requestAnimationFrame(app.update);
</script>
</body>
</html>

最佳答案

当您调用app.update()时,您正在调用update函数作为 app 的方法对象。

但是问题中显示的代码不会将您的函数作为方法调用:

requestAnimationFrame(app.update);

当你写app.update时(不使用括号),您只是获得对 app.update 的引用函数作为独立函数。它失去了与 app 的连接目的。当requestAnimationFrame()稍后调用您的update函数,它只是直接调用函数,没有任何 this目的。那么当 update引用文献this.drops它失败了。

您没有列出您的setInterval()版本,但我们可以确定的一件事是它不是看起来像这样:

setInterval( app.update, 10 );

如果这是您使用的代码,它会遇到与 requestAnimationFrame() 相同的问题。您可能写过这样的内容:

setInterval( function() {
app.update();
}, 10 );

现在你的setInterval()回调是匿名函数,该函数调用 app.update() 作为一种方法

您可以使用 requestAnimationFrame() 执行相同的操作,除了您需要调用requestAnimationFrame()每次你想等待另一帧。换句话说,它更像是setTimeout()而不是setInterval() 。所以它可能看起来像这样:

var updater = function() {
app.update();
requestAnimationFrame( updater ); // for subsequent frames
};

requestAnimationFrame( updater ); // for the first frame

现在您的代码应该按预期工作。

这是一个updated version of yuriy636's CodePen test动画工作。

关于javascript - 在 requestAnimationFrame 中传递类的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44974719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com