fogleman / gg

Go Graphics - 2D rendering in Go with a simple API.
https://godoc.org/github.com/fogleman/gg
MIT License
4.37k stars 354 forks source link

Vertical gradient with map #134

Open heyitsols opened 3 years ago

heyitsols commented 3 years ago

Hello,

I'm trying to create text with a top to bottom gradient rather than left to right, but I can't find any way of doing this with NewLinearGradient() and Rotate() and RotateAbout() give interesting results when rotating the gradient 270 degrees, drawing it, then rotating it another 90 degrees.

This is the code that I am using to generate the gradient text if it helps

    title := "Title"
    dc := gg.NewContext(1200, 600)
    dc.SetColor(color.RGBA{232, 232, 232, 255})
    dc.DrawRectangle(0, 0, 1200, 600)
    dc.Fill()
    margin := 20.0
    x := margin
    y := margin
    w := float64(dc.Width()) - (2.0 * margin)
    h := float64(dc.Height()) - (2.0 * margin)
    dc.SetColor(color.RGBA{255, 255, 255, 255})
    dc.DrawRectangle(x, y, w, h)
    dc.Fill()
    txt := gg.NewContext(1200, 600)
    txt.LoadFontFace("SFPro-Heavy.ttf", 60)
    txt.SetColor(color.Black)
    textRightMargin := 60.0
    textTopMargin := 60.0
    x = textRightMargin
    y = textTopMargin
    maxWidth := float64(txt.Width()) - textRightMargin - textRightMargin
    txt.DrawStringWrapped(title, x, y, 0, 0, maxWidth, 1.5, gg.AlignLeft)
    mask := txt.AsMask()
    g := gg.NewLinearGradient(0, 0, w, h)
    g.AddColorStop(0, color.RGBA{37, 56, 126, 255})
    g.AddColorStop(1, color.RGBA{212, 35, 33, 255})
    dc.SetFillStyle(g)
    dc.SetMask(mask)
    dc.DrawRectangle(0, 0, w, h)
    dc.Fill()
    mask = summ.AsMask()
    g = gg.NewLinearGradient(0, 0, w, h)
    g.AddColorStop(0, color.RGBA{37, 56, 126, 255})
    g.AddColorStop(1, color.RGBA{255, 255, 255, 255})
    g.AddColorStop(0.8, color.RGBA{255, 255, 255, 255})
    dc.SetFillStyle(g)
    dc.SetMask(mask)
    dc.DrawRectangle(0, 0, w, h)
    dc.Fill()

Thanks in advance!