Skip to Content
Onevra

Image

Decoded bitmaps and SVGs, fitted, rounded or in grayscale, and before-and-after comparisons.

Loading the live example…

image.go
import (
	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
)

type photoFits struct {
	photo *ui.Bitmap // decoded once, when the application starts
}

func (p *photoFits) View(c *ui.Context) {
	t := c.Theme()
	sample := func(name string, image ui.Element) {
		ui.Column(c).Key(name).Gap(t.Space(1.5)).AlignItems(ui.Center).Children(func() {
			ui.Box(c).Background(t.Surface).Radius(t.Radius).Children(func() { image.Label(name) })
			components.Text(c, name).FontSize(t.Rem(0.8)).TextColor(t.TextMuted)
		})
	}
	ui.Row(c).Gap(16).Wrap().Justify(ui.Center).Children(func() {
		sample("Contain", components.Image(c, p.photo).Size(140, 100))
		sample("Cover", components.Image(c, p.photo).Size(140, 100).Fit(ui.Cover))
		sample("Rounded", components.Image(c, p.photo).Size(100, 100).Fit(ui.Cover).Radius(50))
		sample("Grayscale", components.Image(c, p.photo).Size(140, 100).Fit(ui.Cover).Grayscale())
	})
}
Source

components.Image shows a *ui.Bitmap or *ui.SVG the application has decoded. It contain-fits by default; Fit(ui.Cover) crops to fill. Never load or decode images in a builder.

Comparing

components.ImageCompare lays one view over another and reveals the first up to a draggable divider. Press anywhere to move the divider there; the handle is a slider for the keyboard.

Loading the live example…

image_compare.go
import (
	"github.com/egoist/mygo/ui"
	"github.com/oneo-me/onevra/source/components"
)

type beforeAfter struct {
	photo    *ui.Bitmap // decoded once, when the application starts
	position float32    // the share, from 0 to 100, that "before" covers
}

func (b *beforeAfter) View(c *ui.Context) {
	side := func(gray bool) func() {
		return func() {
			image := components.Image(c, b.photo).Fill().Fit(ui.Cover)
			if gray {
				image.Grayscale()
			}
		}
	}
	components.ImageCompare(c, &b.position, side(true), side(false), components.ImageCompareOptions{
		BeforeLabel: "Before",
		AfterLabel:  "After",
	}).Label("Color restoration").Width(420).MaxWidthPercent(100)
}
Source

API

Image

func Image(c *ui.Context, source ui.ImageSource) ui.Element

Image displays a decoded *ui.Bitmap or *ui.SVG in its own colors. Applications own loading, decoding, caching and errors; reuse decoded assets rather than performing file or network I/O in a content builder.

Images default to their intrinsic size in DIPs and contain-fit when resized. Use Size, Fit, Radius and Grayscale on the returned element to customize its presentation, and Label for application-owned accessible text. A nil source produces an empty element. MyGo owns rendering, layout and image semantics.

pkg.go.dev

ImageCompare

func ImageCompare(c *ui.Context, position *float32, before, after func(), options ...ImageCompareOptions) *ImageCompareControl

ImageCompare overlays before on after in a rounded, clipped frame and reveals before up to a draggable divider. *position is the application-owned share of the frame, from 0 to 100, that before covers; it must be nonnil and finite, and values outside the range are clamped when shown. Both builders lay out at the full frame size so their content stays aligned, and both remain built at every position. Nil builders leave their side empty. Content receives no pointer input.

ImageCompare(c, &position, func() { Image(c, raw).Fill().Fit(ui.Cover) },
	func() { Image(c, edited).Fill().Fit(ui.Cover) },
	ImageCompareOptions{BeforeLabel: "Before", AfterLabel: "After"}).
	Label("Comparison").FillWidth()

Pressing anywhere on the frame moves the divider there with a spring; dragging tracks the pointer and keeps its release velocity as it settles, while the handle stretches into a capsule. Double-clicking the handle restores Default. The focusable handle reports its position as a slider: axis arrows move it by 1, or 10 with Shift, PageUp/PageDown by 10, and Home/End to 0 and 100. Captions fade as the divider nears them. Reduced Motion moves the divider at once. Label defaults to "Divider".

pkg.go.dev

ImageCompareOptions

ImageCompareOptions configures a before-and-after comparison.

Prop

Type

pkg.go.dev
Edit this page on GitHub

On this page