• Pl chevron_right

      Benjamin Otte: Snapping

      news.movim.eu / PlanetGnome • 1:41 • 1 minute

    With the release of 4.23.1, GTK’s renderer will come with a new feature that we’ve called snapping .

    How does it work?

    Snapping is enabled by calling gtk_snapshot_set_snap() . If enabled, it will slightly adjust the placement of rectangles when drawing so that they align with the pixel grid and don’t cover half a pixel.

    GSK_RECT_SNAP_ROUND

    Content drawn with GTK is scaled automatically by the desktop’s scale factor. But with the arrival of native fractional scaling, it is no longer possible to know if content is aligned to the pixel grid.

    While that is usually not a problem, there are a few cases where it is:

    Sprite grids

    Gameeky is a learning game that plays on a grid. Unfortunately, on a fractionally scaled machine, it can end up looking like this:

    A screenshot of a Gameeky game without snapping. A distracting grid can be seen between the sprites.

    Once those sprites are snapped to the pixel grid by rounding to the nearest pixel, the same image looks like this
    A screenshot of a Gameeky game with snapping. No grid is visible anymore and it looks like a single image.

    Sharp images

    Often Applications want to display images in a way that matches the pixels of the image 1:1 with pixels of the monitor. This is a challenge on a fractionally scaled display. Not only is it important to get the scale factor right, it’s also important to align the pixels correctly, or they will appear slightly blurry.

    The use case is not just image viewers that want to offer a 1:1 zoom factor, but all applications that redirect drawing, from game emulators to viewers like Boxes or Connections .

    Hardware optimizations

    And finally, there are optimizations like graphics offload that rely on content being aligned to the pixel grid or the hardware cannot optimize them. So it is important to snap content to the pixel grid for those cases.

    Why don’t we just always snap to the grid?

    There is one big problem with automatic snapping: smoothness. Because snapping only works on full pixels, doing slow animations causes content to jump from one pixel to the next. And that causes jitter.

    The main situation where one can see this is smooth scrolling, like in this example:

    Summary

    The next GTK release will offer a new way to tame the effects of fractional scaling.  Please try it out and let us know how it works!