Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill the 'opposite' shape on canvas?

Tags:

html5-canvas

Say I have a circle (an arc) on HTML5 canvas. I can just fill it like this:

ctx.arc(100, 100, 50, 0, 2 * Math.PI); ctx.fill(); 

It works a treat. However, how can one fill the opposite area? Now it's white with a black circle, but I'd like it to be along the lines of the following image (on which white is the background color and black is the filling color):

opposite area

I know I could just use a black background and paint a white circle, but the background can be anything (all kinds of things have been drawn there before, so just swapping colors is not possible).

Another thing is that not the complete canvas should be filled, but rather a square with an circle canceled out.

I was thinking of a globalCompositeOperation but it does not seem to fit my needs as none of them act according to what I need.

So, how can I accomplish filling the 'opposite' area like in the example image?

like image 326
pimvdb Avatar asked Jun 07 '11 20:06

pimvdb


People also ask

How do you fill a canvas shape?

To fill an HTML5 Canvas shape with a solid color, we can set the fillStyle property to a color string such as blue, a hex value such as #0000FF, or an RGB value such as rgb(0,0,255), and then we can use the fill() method to fill the shape.

How do you fill a path in canvas?

The fill() function is used to draw a path onto the canvas (but not stroking it). To stroke the path you will need to use the stroke() function. The order in which you call the fill() and stroke() functions does matter - by using the fill() function after the stroke() function the fill can obscure half of the stroke.

How do I fill a rectangle with canvas color?

The fillRect() method draws a "filled" rectangle. The default color of the fill is black. Tip: Use the fillStyle property to set a color, gradient, or pattern used to fill the drawing.

How to draw straight lines in canvas?

To draw a line on a canvas, you use the following steps: First, create a new line by calling the beginPath() method. Second, move the drawing cursor to the point (x,y) without drawing a line by calling the moveTo(x, y) . Finally, draw a line from the previous point to the point (x,y) by calling the lineTo(x,y) method.


1 Answers

Draw the black shape in the OP-- that is, draw a rectangle with a circle cut out of the middle.

First call beginPath(); then draw the circle clockwise; then draw the rectangle counter-clockwise (or vice versa); and finally fill().

var ctx = $('#cv').get(0).getContext('2d');    ctx.fillStyle = "grey";  ctx.beginPath();  ctx.arc(200, 200, 100, 0, 2 * Math.PI);  ctx.rect(400, 0, -400, 400);  ctx.fill();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <canvas width="400" height="400" id="cv"></canvas>

It might not be obvious from most of the documentation out there but you can combine multiple subpaths in a single fill(), stroke() or clip() operation. To make a hole in a shape, you just draw the shape of the hole in the opposite direction. The beginPath() method resets the current path. For fill() and clip(), Canvas uses the non-zero winding number rule-- if you read up on that it should become clearer.

like image 119
Bobtato Avatar answered Sep 25 '22 06:09

Bobtato