HTML5 Canvas Stroke Not Smoothed

I'm just trying to create a circle with a thick, smooth stroke in the canvas.

The circle is drawn as expected, but the edges of the stroke are very uneven. I keep reading that Chrome enhances anti-aliasing, so I'm not sure what to do ...

Fiddle: 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>
+5
source share
1 answer

My colleague has just pointed out that I need to use clearRect to clear the canvas after every draw. The strokes were simply painted on top of each other.

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();
};
+15
source

All Articles