Oliver-Loeffler / DeriveColorsFX

A small tools to play with JavaFX Color.derive() function - allows to create custom colors and to save those in color palettes.
Apache License 2.0
11 stars 1 forks source link

Snapshot #2

Open mipastgt opened 2 years ago

mipastgt commented 2 years ago

Maybe I can help you with your snapshot problem.

public static WritableImage renderScaleAwareSnapshot(Canvas canvas, double renderScale) {
    return renderScaleAwareSnapshot(canvas, renderScale, null, 0.0, 0.0, canvas.getWidth(), canvas.getHeight());
}

public static WritableImage renderScaleAwareCanvasSnapshot(Canvas canvas, double renderScale,
        WritableImage writableImage) {
    return renderScaleAwareSnapshot(canvas, renderScale, writableImage, 0.0, 0.0, canvas.getWidth(),
            canvas.getHeight());
}

public static WritableImage renderScaleAwareSnapshot(Node node, double renderScale, WritableImage writableImage,
        double x, double y, double width, double height) {
    int renderWidth = (int) Math.rint(renderScale * width);
    int renderHeight = (int) Math.rint(renderScale * height);
    if (writableImage == null || (int) Math.rint(writableImage.getWidth()) != renderWidth
            || (int) Math.rint(writableImage.getHeight()) != renderHeight) {
        writableImage = new WritableImage(renderWidth, renderHeight);
    }
    SnapshotParameters spa = new SnapshotParameters();
    spa.setViewport(new Rectangle2D(x * renderScale, y * renderScale, width, height));
    spa.setTransform(Transform.scale(renderScale, renderScale));
    return node.snapshot(spa, writableImage);
}

This code does a canvas snapshot but the principle is the same for any kind of node. You can see how I handle the renderScale (2.0 for Retina).

Oliver-Loeffler commented 2 years ago

Thanks @mipastgt for this nice proposal. I will explore this, it looks as if it should work well!