【发布时间】:2015-03-23 21:50:41
【问题描述】:
我正在创建一个使用 HTML5 Canvas 将几何图形绘制到窗口的站点。我有一个“绘图”函数,我将它传递给一个使用 requestAnimationFrame 命令的函数,如下所示:
function animate(drawMe) {
var requestAnimationFrame =
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame ||
function(callback) {
return setTimeout(callback, 10);
};
//draw the function that has been passed through
drawMe();
requestAnimationFrame(animate);
}
但请检查一下:当我调用“drawMe();”时在上面的代码中,通过的函数执行得很好,但也给了我错误“Uncaught TypeError: number is not a function”。如果我包含“console.log(typeof drawMe);”行控制台会打印出两个结果,“function”和“number”。
例如,我可以有一个非常简单的函数:
drawloop1 = function() {
//draws a Rect to my canvas context
cxt.drawRect(0,0,100,100);
}
为了确保我做对了,我运行“console.log(typeof drawloop1);”这会打印出“功能”,这是我所期望的。然后我将它传递给我的动画函数,如下所示:
animate(drawloop1);
这似乎很简单。但是一旦它被传递到“animate”中,它就会开始返回“function”和“number”。它按预期工作,但也会引发 TypeError。我试过不同的变量名,怕某处有重复的变量。有人可以向我解释这里发生了什么吗?
【问题讨论】:
标签: javascript html animation canvas typeerror