Image view

The widget.Image type draws an image to the buffer at the same resolution as it was loaded (a pixel in the source image matches a pixel on screen). What we need to do for the image viewer is scale it to fit the available space. To do this, we create a new custom widget named scaledImage. The code is very similar to the Shiny image widget but with a more complicated PaintBase() function.

This function calculates imgWidth and imgHeight to fit within the current bounds of the widget and maintain the aspect ratio of the source image. It then scales the image using the scaleImage() helper function defined earlier, ready to paint at the correct resolution. Lastly, offset is calculated so that the image is centered within the available ...

Get Hands-On GUI Application Development in Go now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.