gpt4 book ai didi

javascript - 等待动画与 html5 Canvas

转载 作者:太空狗 更新时间:2023-10-29 15:09:08 24 4
gpt4 key购买 nike

我正在尝试实现提到的那种等待动画 in this question ,尤其是看起来像这样的东西:

enter image description here

但我不想使用图形文件,并试图在纯 html5 Canvas 和 javascript 中实现它。另外,我想要一个圆形的黑色背景而不是方形的。作为第一步,我尝试绘制一个静态框架(没有任何移动/旋转)并这样做:

<html>
<head><script>
window.onload = function(){
var c=document.getElementById("waiting").getContext("2d");
c.lineCap="round";
c.fillStyle="#353535";
c.translate(100,100);
function slit(p){
shade = 256*p;
th = 2*Math.PI*p;
cos = Math.cos(th);
sin = Math.sin(th);
c.strokeStyle = '#'+((shade<<16)+(shade<<8)+shade).toString(16);
c.moveTo(55*cos, 55*sin);
c.lineTo(84*cos, 84*sin);
c.stroke();
c.closePath();
}
c.lineWidth=0;
c.arc(0,0,100,0,Math.PI*2);
c.fill();
c.lineWidth=7;
for(var i = 0;i<1;i+=0.05){slit(i);}
}
</script></head>
<body><canvas id="waiting" width=200 height=200></canvas></body>
</html>

我得到的结果是:

enter image description here

问题是,lineCap="round" 不能对所有的“狭缝”正常工作,而 lineWidth=0 属性不适用于圆的边缘。我究竟做错了什么?我用 Chrome 16.0.912.63 和 Firefox 10.0 检查了它,得到了类似的结果。


下一步,我将让我在上面创建的部分功能与

window.animationFrames = (function(callback){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback){window.setTimeout(callback, 1000 / 60);};
})();

但暂时我需要先解决这个问题。

最佳答案

这里有点困惑。

零不是可接受的线宽值。规范规定,如果您说 lineWidth = 0 它将是空操作。

此外,你没有在那里使用 lineWidth 因为你没有抚摸。 fill() 不考虑线宽。

至于另一个问题,你所要做的就是调用beginPath!看这里:

http://jsfiddle.net/JfcDL/

只需添加 beginPath 调用,您就可以通过代码获得:

enter image description here

您做错的是用每个新的 stroke() 绘制到目前为止的整个路径。您需要调用 beginPath 来打开一个新路径,以便 stroke 仅适用于最后一部分,而不适用于目前所做的所有部分。

关于javascript - 等待动画与 html5 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8649706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com