From 5ea3a02fcc122be445f8676ede8a008b2c86bdbf Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Mon, 24 Aug 2015 17:58:48 -0400 Subject: [PATCH] FX - align to pixel grid when drawing 1 px strokes --- core/src/processing/javafx/PGraphicsFX2D.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/core/src/processing/javafx/PGraphicsFX2D.java b/core/src/processing/javafx/PGraphicsFX2D.java index dd9774533..93fd7dabf 100644 --- a/core/src/processing/javafx/PGraphicsFX2D.java +++ b/core/src/processing/javafx/PGraphicsFX2D.java @@ -611,6 +611,12 @@ public class PGraphicsFX2D extends PGraphics { @Override public void line(float x1, float y1, float x2, float y2) { + if (drawingThinLines()) { + x1 += 0.5f; + x2 += 0.5f; + y1 += 0.5f; + y2 += 0.5f; + } context.strokeLine(x1, y1, x2, y2); } @@ -618,6 +624,14 @@ public class PGraphicsFX2D extends PGraphics { @Override public void triangle(float x1, float y1, float x2, float y2, float x3, float y3) { + if (drawingThinLines()) { + x1 += 0.5f; + x2 += 0.5f; + x3 += 0.5f; + y1 += 0.5f; + y2 += 0.5f; + y3 += 0.5f; + } context.beginPath(); context.moveTo(x1, y1); context.lineTo(x2, y2); @@ -631,6 +645,16 @@ public class PGraphicsFX2D extends PGraphics { @Override public void quad(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { + if (drawingThinLines()) { + x1 += 0.5f; + x2 += 0.5f; + x3 += 0.5f; + x4 += 0.5f; + y1 += 0.5f; + y2 += 0.5f; + y3 += 0.5f; + y4 += 0.5f; + } context.beginPath(); context.moveTo(x1, y1); context.lineTo(x2, y2); @@ -658,6 +682,12 @@ public class PGraphicsFX2D extends PGraphics { protected void rectImpl(float x1, float y1, float x2, float y2) { // rect.setFrame(x1, y1, x2-x1, y2-y1); // drawShape(rect); + if (drawingThinLines()) { + x1 += 0.5f; + x2 += 0.5f; + y1 += 0.5f; + y2 += 0.5f; + } if (fill) context.fillRect(x1, y1, x2 - x1, y2 - y1); if (stroke) context.strokeRect(x1, y1, x2 - x1, y2 - y1); } @@ -679,6 +709,10 @@ public class PGraphicsFX2D extends PGraphics { protected void ellipseImpl(float x, float y, float w, float h) { // ellipse.setFrame(x, y, w, h); // drawShape(ellipse); + if (drawingThinLines()) { + x += 0.5f; + y += 0.5f; + } if (fill) context.fillOval(x, y, w, h); if (stroke) context.strokeOval(x, y, w, h); } @@ -699,6 +733,10 @@ public class PGraphicsFX2D extends PGraphics { float start, float stop, int mode) { // 0 to 90 in java would be 0 to -90 for p5 renderer // but that won't work, so -90 to 0? + if (drawingThinLines()) { + x += 0.5f; + y += 0.5f; + } start = -start * RAD_TO_DEG; stop = -stop * RAD_TO_DEG; @@ -1729,6 +1767,12 @@ public class PGraphicsFX2D extends PGraphics { } + protected boolean drawingThinLines() { + // align strokes to pixel centers when drawing thin lines + return stroke && strokeWeight == 1; + } + + //////////////////////////////////////////////////////////////