简体   繁体   English

HTML5 / Javascript-我的简单画布动画不起作用

[英]HTML5/Javascript - My simple canvas animation isn't working

I thought this would be the kind of thing there would be plenty of tutorials on, however every one I found was either over-complicated, vague, or used features not yet widely supported. 我以为这将是很多教程的内容,但是我发现的每个教程要么过于复杂,含糊不清,要么使用了尚未得到广泛支持的功能。 So, I decided to try it on my own. 因此,我决定自己尝试一下。 My code is below. 我的代码如下。

function drawStuff(){
var x = document.getElementById("myCanvas");
var canvas = x.getContext('2d');

var i;

function animate(){
    canvas.clearRect(0,0,500,500);
    canvas.fillStyle="red";
    canvas.fillRect(0,i,50,50);
    i++;
}

    windows.setInterval(animate(), 125);
}
window.addEventListener("load", drawStuff, false);

Why isn't this working? 为什么这不起作用? Do I misunderstand what the setInterval does? 我会误会setInterval的作用吗? Any help is appreciated. 任何帮助表示赞赏。

请将值设置为变量i

There is a typo in that you refer to windows rather than window , and the argument supplied should be a reference to the function you want to run, so it should be animate rather than animate() which would just call the animate function immediately. 有一个错字,就是您引用windows而不是window ,并且提供的参数应该是要运行的函数的引用,因此应该是animate而不是animate() ,后者会立即调用animate函数。

Those two changes should make: 这两个更改应进行:

window.setInterval(animate, 125);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM