JavaFX GraphicsContext clearRect doesn't work with clip mask

javafx canvas
javafx clear canvas
javafx canvas not showing
javafx canvas draw text
javafx canvas example
javafx canvas background color
javafx canvas layers
javafx canvas draw line

The documentation for the clearRect method of GraphicsContext states that it uses the current clip, but this isn't currently working for me. Consider:

GraphicsContext context = canvas.getGraphicsContext2D();
context.rect(0,0,100,100); //Set the current path to a rectangle
context.stroke(); //Highlights where the current path is
context.clip();   //Intersect current clip with rectangle
context.fillOval(80, 80, 40, 40); //This correctly draws the oval clipped
context.clearRect(0,0,100,100); //This does nothing at all

The above code sets the clip mask correctly, as evidenced by the fact that fillOval works correctly, however clearRect does nothing (although it works normally without the context.clip()). Why is this?

(Note that I specifically need the clip mask to be working, as later I plan on setting it to specific shapes to erase in non-rectangular shapes.)

-- Edit --

To be clear, clearRect does literally nothing, not even erase the oval. I realise that it won't erase the stroked rectangle but that's not what I'm concerned about.

-- Edit 2 --

Updating to the latest JDK has partially fixed the issue. The above code now works correctly. However, using a non-rectangular clip mask still has the same problem. e.g.

GraphicsContext context = canvas.getGraphicsContext2D();
context.arc(50, 50, 40, 40, 0, 360); // Make a circular clip mask
context.fillRect(0, 0, 200, 200); //Draw a circle clipped correctly, shows clip mask is working
context.clearRect(0, 0, 200, 200); //Does nothing

I realise I could use save and restore to get a rectangular clip mask back, and then clearRect would work. However I want to be able to erase in non-rectangular shapes.

Full code for reproducing this is (created by making a new JavaFX project in eclipse and adding the above lines):

public class Main extends Application {
    public void start(Stage primaryStage) {
        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root, 400, 400);

            Canvas canvas = new Canvas(500, 500);

            GraphicsContext context = canvas.getGraphicsContext2D();
            context.arc(50, 50, 40, 40, 0, 360);
            context.fillRect(0, 0, 200, 200);
            context.clearRect(0, 0, 200, 200);
        } catch (Exception e) {

    public static void main(String[] args) {

This should show a blank screen, but the circle is not being cleared.

As per Edit 2, this behaviour seems to be a bug. In summary, clearRect has no effect when a non-rectangular clip is set. (If it isn't working for you even with a rectangular clip, update to the latest JDK.) I've filed a bug report for this.

GraphicsContext (JavaFX 8), This class is used to issue draw calls to a Canvas using a buffer. Each call import javafx.scene.canvas.*; Group root void, clearRect(double x, double y, double w, double h) This method does NOT alter the current state in any way. Also� JavaFX GraphicsContext clearRect doesn't work with clip mask. Ask Question Asked 4 years, 6 months ago. Active 4 months ago. Viewed 2k times 1. The documentation for

While i agree that this should be fixed, but am facing the fact that it just is not, i have come up with a solution that meets my specific needs at least.

It is a method that clears an area based on an SVGPath using the GraphicsContext.

private void clearPath(GraphicsContext gc, SVGPath path) {
        int xstart = (int) path.getLayoutX();
        int xend = (int) (xstart + path.getLayoutBounds().getMaxX());
        int ystart = (int) path.getLayoutY();
        int yend = (int) (ystart + path.getLayoutBounds().getMaxY());

        PixelWriter pw = gc.getPixelWriter();
        for (int x = xstart; x <= xend; x++) {
            for (int y = ystart; y <= yend; y++) {
                if(path.contains(new Point2D(x, y))) {
                    pw.setColor(x, y, Color.TRANSPARENT);

GraphicsContext (JavaFX 2.2), import javafx.scene.canvas.Canvas; import javafx.scene.canvas. However, fillRect does not working for setting a transparent background so� The following are Jave code examples for showing how to use clearRect() of the javafx.scene.canvas.GraphicsContext class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.

The code works just fine on Windows 7 with JavaFX 8u40.

What you should do is to provide a MCVE. Nobody can possibly guess what else you've done with the GraphicsContext. There may be a save and restore missing and what not. By the way, your code lacks a closePath().

Answer before your edit:

The problem you are facing is the way JavaFX draws the lines. Check out the documentation of the Node class:

At the device pixel level, integer coordinates map onto the corners and cracks between the pixels and the centers of the pixels appear at the midpoints between integer pixel locations. Because all coordinate values are specified with floating point numbers, coordinates can precisely point to these corners (when the floating point values have exact integer values) or to any location on the pixel. For example, a coordinate of (0.5, 0.5) would point to the center of the upper left pixel on the Stage. Similarly, a rectangle at (0, 0) with dimensions of 10 by 10 would span from the upper left corner of the upper left pixel on the Stage to the lower right corner of the 10th pixel on the 10th scanline. The pixel center of the last pixel inside that rectangle would be at the coordinates (9.5, 9.5).

That's why you have a thin line on the right and at the bottom. The lines aren't crisp.

I suggest you move the rectangle to the center and also use the fill method to make this more visible for you.

Your code without clearRect:

Your code with clearRect:

javafx.scene.canvas.GraphicsContext.clearRect java code examples , GraphicsContext; import javafx.scene.paint.Color; import This does not handle the Custom Color option. Note that // when clearRect(0,0,overlay.getWidth()� GraphicsContext gc = myCanvas.getGraphicsContext2D(); gc.setFill(Color.ALICEBLUE); gc.fillRect(0, 0, 300, 200); Could you tell me if this will work consistently and whether it is the standard way to achieve this goal?

javafx.scene.canvas.GraphicsContext#clearRect, Type: Enhancement; Component: javafx; Sub-Component: scenegraph JDK- 8115239 - Canvas Command Buffer get corrupted as a result of MT changes We could also add a "clear" method which is "clearRect(0, 0, w, h)" to make this But internally, by default, what it actually does is clear the canvas and render the � To clear the image I am using gc.clearRect(0, 0, gc.getCanvas().getWidth(), gc.getCanvas().getHeight()). As per java doc this Clears a portion of the canvas with a transparent color value. So this is visually clearing the image by making it transparent but it isn't really clearing the image so when I save the form its still saving the image.

[JDK-8143602] GraphicsContext.clearRect has no effect with non , This java examples will help you to understand the usage of javafx.scene.canvas. clearRect(0, 0, width, height); // Paint it gc. fillText("You won! Overview. The JavaFX Canvas API provides a custom texture that you can write to. It is defined by classes Canvas and GraphicsContext in the javafx.scene.canvas package. Using this API involves creating a Canvas object, obtaining its GraphicsContext, and invoking drawing operations to render your custom shapes on screen., The CanvasRenderingContext2D.clearRect() method of the Canvas 2D API erases the pixels in a rectangular area by setting them to� Best Java code snippets using javafx.scene.canvas.GraphicsContext (Showing top 20 results out of 315) Refine search. gc. clearRect (0, 0, canvas.getWidth(),

  • While I believe this is working for you, this definitely isn't working for me. Sorry for being unclear, but when I said clearRect wasn't doing anything, I meant that it wasn't even erasing the (quarter) oval (I only had the stroke there to illustrate where the clip mask was)l. Filling the background like you suggest and then doing clearRect still has no affect, it looks like your first picture.
  • Which JavaFX version do you use on which system? Because the oval was deleted with exactly the same code you use. There's a closePath() missing btw. And next time you should provide a MCVE when you have a question. With focus on complete.
  • I updated my JDK installation to the latest and the issue is now partially fixed. The original code is now working as expected. However clearRect is still not working if you set a non-rectangular clip mask (using arc). See my edit. I've added in a complete code example like you recommended, can you reproduce my problem? Thanks a lot for the help :)
  • Looks like you're out of luck and are left with filling using the background color. clearRect requires a rectangular clip and not a non-rectangular path to have any effect. It doesn't even work if you e. g. create a simple triangle using moveTo/lineTo and clip that. You could file a bug report.
  • Thanks for your help in this. I've awarded you the bounty and filed a bug report.