【问题标题】:HTML5 Canvas stroke not anti-aliasedHTML5 Canvas 笔划没有抗锯齿
【发布时间】:2012-08-23 06:53:06
【问题描述】:

我只是想在画布上画一个带有粗抗锯齿笔触的圆圈。

圆圈按预期绘制,但笔划的边缘非常锯齿。我一直在阅读 Chrome 强制抗锯齿,所以不知道该怎么做......

小提琴:http://jsfiddle.net/nipponese/hWsxw/

HTML

<div id="main">
    <canvas id="myCanvas" width="400" height="400" style="border: 1px solid #000"></canvas>
        <div id="counter" style="height: 100px; width: 100px; border: 1px solid #000">
     </div>
</div>

JS + jQuery

<script>
    function calc(myVal) {
        var canvas = document.getElementById("myCanvas");
        var ctx = canvas.getContext("2d");
        var radius = 70;

        ctx.beginPath();
        ctx.arc(140, 140, 20, myVal * Math.PI, 0, true);
        ctx.lineWidth = 14;
        ctx.stroke();
    };
    $(document).ready(function() {
        var count = 0;
        var parsedCount;
        function go(){  
            if (count <= 200) {
                parsedCount = count*.01
                $('#counter').html('<p>' + parsedCount + '</p>');
                calc(parsedCount);
                count++;
            }
        }
        setInterval(go, 10)
    });
</script>

【问题讨论】:

    标签: html5-canvas antialiasing stroke


    【解决方案1】:

    我的同事刚刚指出我需要在每次绘制后使用 clearRect 来清除画布。笔画只是在彼此之上绘制。

    function calc(myVal) {
        var canvas = document.getElementById("myCanvas");
        var ctx = canvas.getContext("2d");
        var radius = 70;
        ctx.clearRect(0, 0, canvas.width, canvas.height);
    
        ctx.beginPath();
        ctx.arc(140, 140, 20, myVal * Math.PI, 0, true);
        ctx.lineWidth = 14;
        ctx.stroke();
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 2011-05-31
      • 2013-07-25
      相关资源
      最近更新 更多