Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas does not draw smooth lines

I am not able to get anitialiazed lines when doing freehand drawing in javafx canvas. Following is the code ...

import javafx.application.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.canvas.*;
import javafx.scene.control.*;
import javafx.scene.input.*;
import javafx.scene.shape.*;
import javafx.stage.*;

public class Test2 extends Application {
  GraphicsContext gc;

  public static void main(String[] args) { launch(args); }

  private class MouseDragged implements EventHandler<MouseEvent> {
      public void handle( MouseEvent e ) {
        gc.lineTo( e.getX(), e.getY() );
        gc.stroke();
      }
  }

  private class MousePressed implements EventHandler<MouseEvent> {
      public void handle( MouseEvent e ) {
        gc.moveTo( e.getX(), e.getY() );
      }
  }

  @Override
  public void start(Stage stage) {
    Canvas canvas = new Canvas(500, 500);
    canvas.setOnMouseDragged( new MouseDragged() );
    canvas.setOnMousePressed( new MousePressed() );

    gc = canvas.getGraphicsContext2D();
    gc.setLineCap( StrokeLineCap.ROUND );
    gc.setLineJoin( StrokeLineJoin.ROUND );
    gc.setLineWidth( 0.1 );

    Group root = new Group(canvas);
    Scene scene = new Scene(root);
    stage.setTitle("Test2");
    stage.setScene(scene);
    stage.show();
  }
}

While if I create a swing app I can provide rendering hints to smoothen it out ...

g2.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );

How do I provide rendering hits to the graphics context in canvas?

like image 860
Bhupen Avatar asked May 29 '13 14:05

Bhupen


People also ask

How do you get smooth lines when drawing?

For smoother lines, try not to draw from your fingers and try to draw from your elbow or shoulder. This might require a change of grip. This is a great comparison of hand positions for drawing. Try ghosting the stroke in the air before you commit.

How to draw straight lines in canvas?

For drawing straight lines, use the lineTo() method. Draws a line from the current drawing position to the position specified by x and y . This method takes two arguments, x and y , which are the coordinates of the line's end point.

How do you remove lines in canvas?

To clear the Canvas, you can use the clearRect() method. This method performs pretty well than others for clearing the canvas (such as resetting the width/height, destroying the canvas element and then recreating it, etc..) const context = canvas. getContext('2d'); context.


1 Answers

Try adding this effect to your GraphicsContext:

BoxBlur blur = new BoxBlur();
blur.setWidth(1);
blur.setHeight(1);
blur.setIterations(1);
gc.setEffect(blur);

So (as an example) your start method would now look like this:

  @Override
  public void start(Stage stage) {
    Canvas canvas = new Canvas(500, 500);
    canvas.setOnMouseDragged( new MouseDragged() );
    canvas.setOnMousePressed( new MousePressed() );

    gc = canvas.getGraphicsContext2D();
    gc.setLineCap( StrokeLineCap.ROUND );
    gc.setLineJoin( StrokeLineJoin.ROUND );
    gc.setLineWidth( 1 );

    BoxBlur blur = new BoxBlur();
    blur.setWidth(1);
    blur.setHeight(1);
    blur.setIterations(1);
    gc.setEffect(blur);

    Group root = new Group(canvas);
    Scene scene = new Scene(root);
    stage.setTitle("Test2");
    stage.setScene(scene);
    stage.show();
  }

It's not exactly perfect... but the best thing I've found so far.

like image 111
Nick Rippe Avatar answered Oct 10 '22 10:10

Nick Rippe