【发布时间】:2017-04-25 10:18:09
【问题描述】:
我正在尝试使用 window.requestAnimationFrame() 做一个简单的动画。不知何故,该函数没有被递归调用,也没有给出正确的代码。我文件的 javascript 代码是:
function OilPainting(){
this.initialize=function(){
var x=100;
var canvas=document.getElementById("canvas");
canvas.width=window.innerWidth;
canvas.height=window.innerHeight;
var context=canvas.getContext('2d');
animate(canvas,context);
}
}
var x=100;
var animate=function(canvas,context){
window.requestAnimationFrame(animate);
console.log("a");
//console.log("a");
/*for(var i=0;i<1;i++){
var x=Math.random()*window.innerWidth;
var y=Math.random()*window.innerHeight;
context.beginPath();
context.arc(x,y,30,0,2*Math.PI);
context.stroke();
//console.log("here");
x+=1;*/
context.beginPath();
context.arc(x,100,30,0,2*Math.PI);
context.clearRect(0,0,innerWidth,innerHeight);
//console.log(x);
context.stroke();
x+=100;
// console.log(x);
}
var app=new OilPainting();
app.initialize();
在这里,虽然控制台 a 正在递归打印,但圆圈没有形成。我的 Codepen 的链接是 Here。requestAnimationFrame() 究竟是如何使用的?
【问题讨论】:
-
requestAnimationFrame 的第一个参数是时间。应该是
function animated(time){ requestAnimationFrame(animate);...
标签: javascript html canvas