Canvas -> globalAlpha for custom / round clips

it at kwinan.de it at kwinan.de
Mon Jan 27 07:32:48 UTC 2020


Hello everybody

Maybe I'm making a mistake, but it seems to me that graphicContext.restore() for globalAlpha doesn't work if clipping is used that was not constructed via rect(...).

Java: openjdk 11.0.6 2020-01-14
JavaFX: 13
OS: Windows 10 Pro

Code:

package foo.bar;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
final double width = 1000;
final double height = 600;

final Canvas canvas = new Canvas(width, height);

final Pane root = new StackPane();
root.setStyle("-fx-background-color: bisque;");
root.getChildren().add(canvas);

defaultRectangleClip(100, 100, 200, 200, canvas.getGraphicsContext2D());
customRectangleClip(400, 100, 200, 200, canvas.getGraphicsContext2D());
ovalClip(700, 100, 200, canvas.getGraphicsContext2D());

final Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setMaxWidth(width);
primaryStage.setMaxHeight(height);
primaryStage.show();
}

private void defaultRectangleClip(double x, double y, double width, double height, final GraphicsContext graphicsContext) {
// no problem
graphicsContext.save();
graphicsContext.beginPath();
graphicsContext.rect(x, y, width, height);
graphicsContext.closePath();
graphicsContext.clip();
graphicsContext.setGlobalAlpha(0.1);
graphicsContext.setFill(Color.RED);
graphicsContext.fillRect(x, y, width * 2, height * 2);
graphicsContext.restore();
}

private void customRectangleClip(double x, double y, double width, double height, final GraphicsContext graphicsContext) {
graphicsContext.save();
graphicsContext.beginPath();
graphicsContext.moveTo(x, y);
graphicsContext.lineTo(x + width, y);
graphicsContext.lineTo(x + width, y + height);
graphicsContext.lineTo(x, y + height);
graphicsContext.closePath();
graphicsContext.clip();
graphicsContext.setGlobalAlpha(0.4); // Comment out this line to see the difference.
graphicsContext.setFill(Color.GREEN);
graphicsContext.fillRect(x, y, width * 2, height * 2);
graphicsContext.restore();
}

private void ovalClip(double x, double y, double radii, final GraphicsContext graphicsContext) {
graphicsContext.save();
graphicsContext.beginPath();
graphicsContext.arc(x, y, radii, radii, 0, 360);
graphicsContext.closePath();
graphicsContext.clip();
graphicsContext.setGlobalAlpha(1.0); // Doesn't work -> Should be 1.0 per default?
graphicsContext.setFill(Color.CORNFLOWERBLUE);
graphicsContext.fillRect(x, y, radii * 4, radii * 4);
graphicsContext.restore();
}
}



Best regards
Michael






More information about the openjfx-dev mailing list