Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript clearRect不清除画布中的任何内容_Javascript_Html_Canvas - Fatal编程技术网

Javascript clearRect不清除画布中的任何内容

Javascript clearRect不清除画布中的任何内容,javascript,html,canvas,Javascript,Html,Canvas,我下面的代码是为克里斯课程“圆周运动”教程编写的,但我不明白为什么我的clearRect不工作。这一定是我没有看到的东西,我还有另外两个画布动画正在工作,但这一个无法清除矩形,这让我发疯 感谢所有有时间帮忙的人 函数螺旋(){ const canvas2=document.getElementById('mycanvas'); canvas2.width=document.getElementById('mycanvas').scrollWidth; canvas2.height=docume

我下面的代码是为克里斯课程“圆周运动”教程编写的,但我不明白为什么我的clearRect不工作。这一定是我没有看到的东西,我还有另外两个画布动画正在工作,但这一个无法清除矩形,这让我发疯

感谢所有有时间帮忙的人

函数螺旋(){
const canvas2=document.getElementById('mycanvas');
canvas2.width=document.getElementById('mycanvas').scrollWidth;
canvas2.height=document.getElementById('mycanvas').scrollHeight;
constC2=canvas2.getContext('2d');
常量数组=[
#ff0000",,
“#00ff00”,
#0000ff"
];
addEventListener('调整大小',()=>{
canvas2.width=document.getElementById('mycanvas').scrollWidth;
canvas2.height=document.getElementById('mycanvas').scrollHeight;
init();
});
功能喷丝头图标(h、v、半径、颜色){
这个,h=h;
这个,v=v;
这个颜色=颜色;
这个半径=半径;
this.update=()=>{
这个h+=1;
这个.draw();
};
this.draw=()=>{
c2.beginPath;
c2.弧(此.h,此.v,此.radius,0,数学PI*2,假);
c2.fillStyle=此颜色;
c2.填充();
c2.closePath();
}
}
函数init(){
自旋阵列=[];
for(设i=0;i<1;i++){
Spinnerary.push(新喷丝头图标(canvas2.width/2,canvas2.height/2,5,‘红色’)
}
}
让我们一起来吧;
函数animate(){
请求动画帧(动画);
c2.clearRect(0,0,canvas2.width,canvas2.height);
SpinneArray.forEach(parti=>{
parti.update();
})
}
init();
制作动画();
}
螺旋体()
#我的画布{
背景:蓝色;
}

您的
c2.beginPath
行缺失(),应该是
c2.beginPath()因为它是一个函数。未调用beginPath时,clearPath将不起作用。

您的
c2.beginPath
行缺失(),应该是
c2.beginPath()因为它是一个函数。如果不调用beginPath,clearPath将无法工作。

我就知道会是这样。谢谢!我就知道会是这样。谢谢!