Try the code在chrome和firefox中绘制了10000行。在Firefox中,它运行得非常慢(3-4秒),在Chrome中运行得更快。我正在编写应用程序,在帧中绘制数千行动画。有谁知道如何加速Firefox? (firefox中的硬件加速开关)。
<!DOCTYPE html>
<body>
<html>
<canvas id="myCanvas"></canvas>
</html>
</body>
<script>
var canvas=document.getElementById("myCanvas");
var context=canvas.getContext("2d");
canvas.width=1000;
canvas.height=600
context.strokeStyle="black";
context.lineWidth=0.3;
context.fillStyle="#8ED6FF";
context.fillRect(0,0,1000,800);
var N=10000;
for (var i=0;i<N;i++){
context.moveTo(500,300);
context.lineTo(500+200*Math.cos(6.28*i/N),300-200*Math.sin(6.28*i/N));
}
context.stroke();
</script>
答案 0 :(得分:1)
您可以在每次循环迭代开始时使用“beginPath”调用:
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas"></canvas>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var canvas=document.getElementById("myCanvas");
var context=canvas.getContext("2d");
canvas.width=1000;
canvas.height=600
context.strokeStyle="black";
context.lineWidth=0.3;
context.fillStyle="#8ED6FF";
context.fillRect(0,0,1000,800);
var N=10000;
for (var i=0;i<N;i++){
context.beginPath();
context.moveTo(500,300);
context.lineTo(500+200*Math.cos(6.28*i/N),300-200*Math.sin(6.28*i/N));
context.stroke();
}
});
</script>
</body>
</html>
但我认为主要是你在Firefox中发现了一个错误,因为正如你所说,在其他浏览器中就可以了。