Skip to content
Advertisement

JavaFX css grid inconsistent

The following code produces and infinitely pannable grid and also allows for zooming in and out. The grid is being drawn using css.

My issue is that the css grid doesn’t work well when zoomed in very closely or zoomed out a lot. It also is inconsistent and will sometimes display only vertical or only horizontal lines. I’m not sure how to reliably reproduce that, it just kinda happens sometimes.

The perfect solution would be something similar to the grid in blender, where new lines come in to replace the smaller lines that are no longer visible.

Part that creates and sets css:

JavaScript

Full Code:

JavaScript

Advertisement

Answer

As far as I am aware of, the approach you followed is pretty simple and straightforward which simplifies the logic when compared with other approaches.

Having said that, the reason for the undesired behavior is due to relying on % values for color stops, which will be constantly changing based on the base value. I have not done a deep investigation regarding this, but I am quite convinced that this could be the reason :).

So the alternate to % is to rely on “px”. But there is catch here… there is bug in JavaFX CSSParser.java class, where, applying the linear-gradient with “px” based color stops through CSS does not work correctly.

To over come this issue, set the background using “setBackground” API instead of setting it through “setStyle”. What I mean is, instead of setting like

JavaScript

set the background using below approach:

JavaScript

And also there is another issue in using “%” color stops. The width of the grid lines will not be consistent. I mean you cannot maintain same px(say 1px) on different scales. You can notice that in your demo.

So considering all these, I only changed the code of your resetStyle method as below (a fine tuning may be needed) and the output is shown in the attached gif.

JavaScript

enter image description here

I hope you are already well aware of JavaFX gradients. But just in case you can refer to this blog JavaFX Gradients if you need a bit more details. This is very old blog which I wrote back in 2012. I assume nothing is changed much 😉

[UPDATE] : I just found some glitches in the code. Will again update the answer once I fix thim.

[UPDATE 2] : Looks like the gradient is having issues when trying to render some floating point values. When rounded those values, it looks like the issue is fixed. Also updated the code to set the zoom point at center. Can you give a try. I updated my code and gif.

[UPDATE 3] : Explanation about line thickness

The ‘gridSize” is the amount we are setting for the linear gradient to render. The gradient repeats after this amount of size. So given two colors, <color1 stop1>, <color2 stop2> The <color1> is rendered till <stop1> amount(px) and then from there <color2> start rendering till <stop2>. In this case the stop2 value does’nt matter(it can be 1px or 2px or even 100px). So pretty much the stop1 value will decide the line thickness (a.k.a the second color rendering). The below image can give you some quick explanation. enter image description here

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement