Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery canvas circle

Tags:

jquery

I made a countdown with a canvas drawing

working code : http://jsfiddle.net/ajFsx/

window.onload = function() {
    canvas  = document.getElementById('timer'),
    seconds = document.getElementById('counter'),
    ctx     = canvas.getContext('2d'), 
    sec     = 180,
    countdown = sec;
    ctx.lineWidth = 8;
    ctx.strokeStyle = "#528f20";
    var 
    startAngle = 0, 
    time       = 0,
    intv       = setInterval(function(){
        var endAngle = (Math.PI * time * 2 / sec);
        ctx.arc(65, 35, 30, startAngle , endAngle, false);   
        startAngle = endAngle;
        ctx.stroke();

        countdown--;
        if ( countdown > 60){
            seconds.innerHTML = Math.floor(countdown/60);
            seconds.innerHTML += ":" + countdown%60;
        }
        else{
            seconds.innerHTML = countdown;
        }
        if (++time > sec,countdown == 0 ) { clearInterval(intv), $("#timer, #counter").remove(), $("#timers").prepend('<img id="theImg" src="#" />'); }
    }, 10);
}​

My question is the following how can i get this drawing better looking, so no pixels?

I googled a lot at jquery canvas but i can't seem to find where i'm looking for.

like image 672
Ivo Avatar asked Oct 29 '12 04:10

Ivo


1 Answers

Canvas drawing is anti-aliased by default.

You're always drawing over your previous frames.

jsFiddle (proof).

You can easily fix that by calling...

ctx.clearRect(0, 0, 200, 200);

jsFiddle.

This clears the previous drawn 200px square in the top left corner.

like image 95
alex Avatar answered Oct 18 '22 04:10

alex