Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6001429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:50:00+00:00 2026-05-23T00:50:00+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:50:01+00:00Added an answer on May 23, 2026 at 12:50 am

    You can do that using another canvas as mask :

    // This is the canvas where you want to draw
    var canvas = document.getElementById('your-canvas');
    var ctx = canvas.getContext('2d');
    
    // I'll use a skyblue background that covers everything
    // Just to demonstrate
    ctx.fillStyle = "skyblue";
    ctx.fillRect(0, 0, canvas.width, canvas.height);
    
    // Create a canvas that we will use as a mask
    var maskCanvas = document.createElement('canvas');
    // Ensure same dimensions
    maskCanvas.width = canvas.width;
    maskCanvas.height = canvas.height;
    var maskCtx = maskCanvas.getContext('2d');
    
    // This color is the one of the filled shape
    maskCtx.fillStyle = "black";
    // Fill the mask
    maskCtx.fillRect(0, 0, maskCanvas.width, maskCanvas.height);
    // Set xor operation
    maskCtx.globalCompositeOperation = 'xor';
    // Draw the shape you want to take out
    maskCtx.arc(30, 30, 10, 0, 2 * Math.PI);
    maskCtx.fill();
    
    // Draw mask on the image, and done !
    ctx.drawImage(maskCanvas, 0, 0);
    <canvas id="your-canvas">

    Demo here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to spread an image inside html5 canvas.. like say I have
Let's say I have a super type (Shape) and two children (like Square, Circle).
I have a circle, say radius of 10, and I can find the outer
In Python, say I have some class, Circle, that inherits from Shape. Shape needs
Lets say have this immutable record type: public class Record { public Record(int x,
Say we have deep hashes like: b = {1 => {2 => {} },
Say i have a few fields like the following: abd738927 jaksm234234 hfk342 ndma0834 jon99322
Let's say I have circle bouncing around inside a rectangular area. At some point
Say I have a list of shapes: type shape = | Circle of float
let's say we have an image that contain square,circle and other few object at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.