html5 canvas loading circle image

Viewed 253

I'm trying to create a loading circle from an image in HTML5 canvas.

Here's the result I expect when percentage is at 50%:

enter image description here

here's what I've done after a lot of tests: (the blue stroke is just to see the circle, it'll be removed after)

var img = new Image(); 
img.onload = draw;
img.src = "http://i.imgur.com/HVJBZ1L.png";

var canvas = document.getElementsByTagName("canvas")[0];

canvas.width  = 500;
canvas.height = 500;

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

function draw() {
    ctx.globalAlpha = 0.5
    ctx.drawImage(img, 0, 0);
    ctx.globalAlpha = 1;
    
    var X       = 50;
    var Y       = 50;
    var Radius  = img.width / 2;
    var end     = 40;
    var start   = 0;
    var PI2     = Math.PI * 2;
    var quart   = Math.PI / 2;
    var pct     = 50 / 100;
    var extent  = (end - start) * pct;
    var current = (end - start) / 100 * PI2 * pct - quart;
    
    var pattern = ctx.createPattern(img, 'no-repeat');
    
    ctx.beginPath();
    ctx.arc(X, Y, Radius, -quart, current);
    ctx.closePath();
    ctx.fillStyle=pattern;
   ctx.fill();
    ctx.strokeStyle = "blue";
    ctx.stroke();
}
<canvas></canvas>

as you can see, the result here isn't as excepted

enter image description here

What is wrong?

2 Answers
Related