Skip to content Skip to sidebar Skip to footer

Make Rectangle Overlay Image With Canvas

I'd like to reach the following with HTML5 canvas: My code: jsfiddle // Options var maxImageWidth = 250, maxImageHeight = 196, radius = 50; var canvas = $('#ddayCanvas')

Solution 1:

You have the image being drawn in the asynchronous callback onload. The result will change depending on if the image loads first, or the canvas draw is called first.

Try moving your draw code into the same callback.

For more efficient clipping you can use the non-zero winding order rule: http://blogs.adobe.com/webplatform/2013/01/30/winding-rules-in-canvas/

TLDR: Consecutive canvas draw calls will add or subtract from the previous calls depending on whether the order of points is clockwise or anti-clockwise.

See: http://jsfiddle.net/jJjt7/2/

context.fillStyle = sectorColor;
context.rect(0, 0, canvasWidth, canvasHeight);
context.arc(canvasWidth/2, canvasHeight/2, radius, 0, Math.PI*2, true);
context.fill();

The above example draws a rectangle with a clockwise point order, then subtracts the arc (circle) by using an anti-clockwise point order. The last argument of the arc method defines 'anti-clockwise'. In this case true.

Edit: This negates the need to draw twice. Efficiency, FTW.

Solution 2:

You need to change the order that you're drawing the objects.

// Why does this rectangle not overlay the previous image?
context.fillStyle = sectorColor;
context.fillRect(0, 0, canvasWidth, canvasHeight);

drawDday('clip');      
drawDday(); 

Post a Comment for "Make Rectangle Overlay Image With Canvas"