Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: Disable image smoothing on Canvas object

I'm making a sprite editor using JavaFX for use on desktops.

I'm attempting to implement zooming functionality, but I've run into a problem: I can't figure out how to disable image smoothing on a Canvas object.

I'm calling Canvas.setScaleX() and Canvas.setScaleY() as per every tutorial implementing Canvas zooming. But my image appears blurred when zoomed in.

I have some test code here to demonstrate.

What I getWhat I want

As this is a sprite editor, it's important for me to have crisp edges to work with. The alternative to fixing image smoothing on the Canvas is to have a non-smoothing ImageView, and have a hidden Canvas to draw on, which I would rather avoid.

Help is appreciated.


(here's a link to a related question, but doesn't address my particular problem)

like image 393
sorbet Avatar asked Mar 29 '14 01:03

sorbet


1 Answers

I was having the same issue with the blurring.

In my case, my computer has Retina Display. Retina Display causes a pixel to be rendered with sub-pixels. When drawing images to the canvas, the image would be drawn with antialiasing for the sub-pixels. I have not found a way to prevent this antialiasing from occurring (although it is possible with other canvas technologies such as HTML5's Canvas)

In the meantime, I have a work-around (albeit I'm concerned about performance):

public class ImageRenderer {
   public void render(GraphicsContext context, Image image, int sx, int sy, int sw, int sh, int tx, int ty) {
      PixelReader reader = image.getPixelReader();
      PixelWriter writer = context.getPixelWriter();
      for (int x = 0; x < sw; x++) {
         for (int y = 0; y < sh; y++) {
            Color color = reader.getColor(sx + x, sy + y);
            if (color.isOpaque()) {
              writer.setColor(tx + x, ty + y, color);
            }
         }
      }
   }
}

The PixelWriter bypasses the anti-aliasing that occurs when drawing the image.

like image 199
Wayne Avatar answered Oct 21 '22 18:10

Wayne