Class 13 Fyne GoLang GUI Linear, Radial, Horizontal and Vertical Gradient
Want to color your widget. Here is an option in FYNE for gradients.
Gradient is simply 2 or more color combination which can beautify your design and UI.
Normally we use two most common gradients.
LINER AND HORIZONTAL
But you can use LINER gradient in two different ways.
HORIZONTALLY & VERTICALLY.
Remember we need to use 2 colors to create a gradient.
You need to specify both colors first.
Here is the code for linear gradient.
[sourcecode lang=”go”]
// Linear Gradient
gradient2 := canvas.NewLinearGradient(
color.White, color.NRGBA{R: 255, G: 0, B: 0, A: 255}, 270)
// 45 degree angle
[sourcecode lang=”go”] // Radial Gradient
gradient1 := canvas.NewRadialGradient(
color.White, color.NRGBA{R: 255, G: 0, B: 0, A: 255})
// 45 degree angle [/sourcecode] Here is the code for horizontal gradient.
[sourcecode lang=”go”]
// Horizontal Gradient
gradient3 := canvas.NewHorizontalGradient(
color.White, color.NRGBA{R: 255, G: 0, B: 0, A: 255})
// 45 degree angle
[/sourcecode]
Here is the code for vertical gradient.
[sourcecode lang=”go”]
// Vertical Gradient
gradient4 := canvas.NewVerticalGradient(
color.White, color.NRGBA{R: 255, G: 0, B: 0, A: 255})
// 45 degree angle
// import Fyne
import (
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
)
func main() {
// New App
a := app.New()
// New Window and Title
w := a.NewWindow("Gradient : Canvas")
w.Resize(fyne.NewSize(400, 400))
// Radial Gradient
gradient1 := canvas.NewRadialGradient(
color.White, color.NRGBA{R: 255, G: 0, B: 0, A: 255}) // 45 degree angle
// Linear Gradient
//gradient2 := canvas.NewLinearGradient(
// color.White, color.NRGBA{R: 255, G: 0, B: 0, A: 255}, 270) // 45 degree angle
// Horizontal Gradient
//gradient3 := canvas.NewHorizontalGradient(
// color.White, color.NRGBA{R: 255, G: 0, B: 0, A: 255})
// Vertical Gradient
//gradient4 := canvas.NewVerticalGradient(
// color.White, color.NRGBA{R: 255, G: 0, B: 0, A: 255}) // 45 degree angle
// setup content
w.SetContent(gradient1)
w.ShowAndRun()
}
Fyne GoLang GUI Course