简体   繁体   中英

Slow html5 drawing in firefox

Try the code below that draws 10000 lines in chrome and in firefox. In firefox it runs extremely slow (3-4 seconds), in chrome it runs much faster. I am writing application that draw in animation thousands of lines in frame. Does anybody know how to accelerate firefox? (the hardware acceleration in firefox is swith on).

<!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>

You can probably use a "beginPath" call at the beginning of each loop iteration:

<!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>

But I think that mainly you have revealed a bug in Firefox, because as you said, in other browsers is just OK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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