JavaScript的HTML5的画布 - 绘制在使用setInterval弧导致锯齿状边缘(Jav

2019-10-19 19:10发布

我想画一个圆环图,到目前为止,我已经成功了。 问题是,试图通过结合动画弧时stroke()setInterval()它的工作原理,但它看起来并不顺畅,并产生锯齿边缘。 我尝试使用clearRect()来清除画布动画已完成并重新添加动画的最终版本后,但我必须一直使用clearRect()错误的,因为它什么也没做。 如果您有关于如何得到这个看起来不错的任何想法,我会很感激。

你可以看到的jsfiddle我当前的代码: http://jsfiddle.net/4mJCy/及以下:

身体:

<canvas id="Donutchart" width="215" height="500" style="border:1px solid #000;">
Your browser does not support the HTML5 canvas tag.  Please Upgrade to the latest version of Chrome, Firefox, or Safari.</canvas>

JavaScript的:

var canvas=document.getElementById("Donutchart");
var maincircle=canvas.getContext("2d");
maincircle.beginPath();
maincircle.arc(107,107,100,0,2*Math.PI);
maincircle.fillStyle="#FF0000";
maincircle.fill();



var currentEndAngle = 1.5;
var currentStartAngle = 1.5;

var pie1 = setInterval(draw, 15);

var counter = 0;

function draw(){


    if(counter >= 101){



    } else {

        var startAngle = currentStartAngle * Math.PI;
        var endAngle = (currentEndAngle) * Math.PI;

        currentEndAngle = currentEndAngle + 0.01;

        var counterClockwise = false;


        var donutslice1=canvas.getContext("2d");    
        donutslice1.beginPath();
        donutslice1.arc(107,107,75,startAngle,endAngle);
        donutslice1.lineWidth = 50;
        donutslice1.strokeStyle="#222";
        donutslice1.stroke();

    }



    counter++;

}


var innercircle=canvas.getContext("2d");
innercircle.beginPath();
innercircle.arc(107,107,50,0,2*Math.PI);
innercircle.fillStyle="#FFF";
innercircle.fill();

Answer 1:

锯齿被反复过度写你的弧引起的。

相反,重构代码,以清除与每个动画循环画布。

演示: http://jsfiddle.net/m1erickson/xNLEQ/

事情是这样的:

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
    body{ background-color: white; }
    canvas{border:1px solid red;}
</style>
<script>
$(function(){

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");

    var currentEndAngle = 1.5;
    var currentStartAngle = 1.5;

    var pie1 = setInterval(draw, 15);

    var counter = 0;

    function draw(){

      if(counter >= 101){

      } else {

        var startAngle = currentStartAngle * Math.PI;
        var endAngle = (currentEndAngle) * Math.PI;

        currentEndAngle = currentEndAngle + 0.01;

        var counterClockwise = false;

        ctx.clearRect(0,0,canvas.width,canvas.height);
        // red
        drawCircle(75,"#FF0000");
        // gray
        drawDonut(startAngle,endAngle);

      }

      counter++;
    }

    //
    function drawDonut(startAngle,endAngle){
        ctx.beginPath();
        ctx.arc(107,107,75,startAngle,endAngle);
        ctx.lineWidth = 50;
        ctx.strokeStyle="#444";
        ctx.stroke();
    }
    //
    function drawCircle(radius,color){
        ctx.beginPath();
        ctx.arc(107,107,radius,0,2*Math.PI);
        ctx.lineWidth = 50;
        ctx.strokeStyle=color;
        ctx.stroke();
    }

}); // end $(function(){});
</script>
</head>
<body>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>


文章来源: JavaScript HTML5-Canvas - Drawing an Arc with SetInterval Causes Jagged Edges