JavaFX snapping and scale

Kevin Rushforth kevin.rushforth at oracle.com
Mon Sep 27 23:57:06 UTC 2021


The basic idea behind snapping to pixel boundaries (which is optional, 
but on by default) is that 2D primitives will look more crisp if they 
are rendered on whole pixel boundaries. When there is no HiDPI involved, 
the operation of snapping to a pixel boundary can be done with a simple 
floor, round, or ceil operation (depending on what you are snapping). 
When there is a HIDPI scale involved, the value in user space needs to 
be chosen such that the transformed value ends up on a pixel boundary. 
That's why you will see snapped values that aren't on integer boundaries 
when the HiDPI scale is, say 1.25.

In the case you ran into, the problem might be that the snapping isn't 
happening in the right place in the computation. Also, it seems quite 
possible that the clipping isn't being calculated correctly with respect 
to pixel snapping.

You can take a look at JDK-8211294 [1] which was fixed by PR #308 [2] 
for a recent example of a HiDPI bug affecting ScrollPane that was fixed. 
There are a couple of follow-on issues that came out of that issue, 
although I suspect they aren't relevant to the bug you are looking at.

-- Kevin

[1] https://bugs.openjdk.java.net/browse/JDK-8211294
[2] https://github.com/openjdk/jfx/pull/308

On 9/27/2021 2:41 AM, Marius Hanl wrote:
>     I recently tried to fix "TableView: visual glitch at borders on
>     horizontal scrolling".
>     Ticket: [1]https://bugs.openjdk.java.net/browse/JDK-8218745
>     PR: [2]https://github.com/openjdk/jfx/pull/630
>
>     As also written in the PR I have problems understanding the
>     snapping/scaling of JavaFX.
>     Here in short what I found out:
>     - Snapping fixed the issue for a scale of 1 or 2, but not for a scale
>     like 1.25 or 1.5
>     --- Also VirtualFlow is the only occurence where we set the layoutX of
>     a clip (might be the problem?)
>     - This visual glitch only happens sometimes when the x value is a
>     decimal number, e.g. 12.66 (never when it's a round number like 13)
>     - Math.round(..) or a cast to int fixed this (for all scales), but is
>     probably not the correct solution or maybe only fixing a symptom here
>
>     Which leads to my question where may some of you can help me:
>     - How does JavaFX renders a node when e.g. x is a decimal number? How
>     many pixel are used then?
>     - And does a decimal number make sense (Why we e.g. don't round the
>     value), which looks like it works fine and doesn't result in visual
>     glitches
>
>     Also information/insights about fixes made in the past which relates
>     with this are welcome.
>     I saw that there were quite some issues with a scale other then 1 in
>     the past.
>
>     Note: If the result is that everything works as expected chances are
>     there might a generic problem with snapping/layout/rendering somewhere
>     then.
>     Any information are welcome and feel free to also have a look at the
>     PR.
>
>     -- Marius
>
> References
>
>     1. https://bugs.openjdk.java.net/browse/JDK-8218745
>     2. https://github.com/openjdk/jfx/pull/630



More information about the openjfx-dev mailing list