I want to do the following with an HTML5 canvas:

My code: jsfiddle
var maxImageWidth = 250,
maxImageHeight = 196,
radius = 50;
var canvas = $('#ddayCanvas'),
canvasWidth = canvas.width(),
canvasHeight = canvas.height(),
sectorColor = $('.product-box').css('background-color'),
context = canvas[0].getContext('2d'),
imageSrc = canvas.data('image');
function drawDday (option) {
var imageObj = new Image();
imageObj.onload = function() {
if (option == 'clip'){
context.save();
context.clip();
}
var imageWidth = imageObj.width,
imageHeight = imageObj.height;
if (imageWidth > maxImageWidth){
imageHeight = imageHeight - (imageWidth - maxImageWidth);
imageWidth = maxImageWidth;
}
if (imageHeight > maxImageHeight) {
imageWidth = imageWidth - (imageHeight - maxImageHeight);
imageHeight = maxImageHeight;
}
context.drawImage(imageObj, Math.ceil((canvasWidth - imageWidth) / 2), Math.ceil((canvasHeight - imageHeight) / 2), imageWidth, imageHeight);
};
imageObj.src = imageSrc;
}
drawDday();
context.fillStyle = sectorColor;
context.fillRect(0, 0, canvasWidth, canvasHeight);
drawDday('clip');
context.restore();
context.moveTo(0, 0);
context.beginPath();
context.fillStyle = '#fff';
context.arc(canvasWidth / 2, canvasHeight/2, radius, 0, 2*Math.PI);
context.fill();
Why doesn't the rectangle I draw overlap the previous image? It is also strange that the canvas is drawn in different ways, sometimes when the site is reloaded.
source
share