Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snapshot #2

Open
mipastgt opened this issue Dec 17, 2021 · 1 comment
Open

Snapshot #2

mipastgt opened this issue Dec 17, 2021 · 1 comment

Comments

@mipastgt
Copy link

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants