Canvas 画布颜色渐变动画错误:未能执行“createRadialGradient”:提供的双精度值是非有限的

Canvas 画布颜色渐变动画错误:未能执行“createRadialGradient”:提供的双精度值是非有限的,canvas,Canvas,我试图回顾我所学的内容,并打算制作一个单圈颜色渐变动画,这个错误仍然像往常一样出现,这让我有点困惑。脚本如下所示: canvas = document.getElementById('myCanvas'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; console.log(canvas.width,canvas.height); ctx = canvas.getContext('2d'); fu

我试图回顾我所学的内容,并打算制作一个单圈颜色渐变动画,这个错误仍然像往常一样出现,这让我有点困惑。脚本如下所示:

canvas = document.getElementById('myCanvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
console.log(canvas.width,canvas.height);


ctx = canvas.getContext('2d');
function drawCircle(x,y,r){
    ctx.clearRect(0,0,canvas.height,canvas.width);
    requestAnimationFrame(drawCircle);

    var grad = ctx.createRadialGradient(x,y,0,x,y,r);
    var colorArray = ['blue','yellow','green','red','grey'];
    grad.addColorStop(0,colorArray[Math.floor(Math.random()*colorArray.length)]);
    grad.addColorStop(.3,colorArray[Math.floor(Math.random()*colorArray.length)]);
    grad.addColorStop(.5,colorArray[Math.floor(Math.random()*colorArray.length)]);
    grad.addColorStop(.6,colorArray[Math.floor(Math.random()*colorArray.length)]);
    grad.addColorStop(.9,colorArray[Math.floor(Math.random()*colorArray.length)]);

    ctx.beginPath();
    ctx.arc(x,y,r,0,Math.PI*2);
    ctx.fillStyle = grad;
    ctx.fill();
}

drawCircle(100,100,20);

有人能帮我检查一下吗?

发生错误的原因是,在通过requestAnimationFrame调用drawCircle函数时,您没有传递参数x、y、r

所以,要解决这个问题,你需要调用函数,就像这样

requestAnimationFrame(function() {
    drawCircle(x, y, r);
});
canvas=document.getElementById'myCanvas'; ctx=canvas.getContext'2d'; 画布宽度=200; 画布高度=200; 函数drawCirclex,y,r{ ctx.clearRect0,0,canvas.height,canvas.width; var grad=ctx.createRadialGradientx,y,0,x,y,r; var colorArray=[“蓝色”、“黄色”、“绿色”、“红色”、“灰色”]; grad.addColorStop0,colorArray[Math.floorMath.random*colorArray.length]; grad.addColorStop0.3,colorArray[Math.floorMath.random*colorArray.length]; grad.addColorStop0.5,colorArray[Math.floorMath.random*colorArray.length]; grad.addColorStop0.6,colorArray[Math.floorMath.random*colorArray.length]; grad.addColorStop0.9,colorArray[Math.floorMath.random*colorArray.length]; ctx.beginPath; ctx.arcx,y,r,0,Math.PI*2; ctx.fillStyle=梯度; ctx.fill; requestAnimationFramefunction{ drawCirclex,y,r; }; } 图纸圆圈100、100、20; 正文{溢出:隐藏}画布{边框:1px实心ccc;}
对不起,我刚从大学回来,是的,我有点错过了在回调函数时传递这些参数,谢谢你的帮助!