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

draw two text in different rotation with accurate x, y ? #121

Closed JasonkayZK closed 3 years ago

JasonkayZK commented 3 years ago

I try to draw two text in different rotation degrees, but i found it wrong that the text is not in the exact x, y which has been given:

func main() {
    F := 40.0
    dc := gg.NewContext(500, 500)
    dc.Clear()

    if err := dc.LoadFontFace("Deng.ttf", F); err != nil {
        panic(err)
    }

    dc.SetRGB(1, 0, 0)
    dc.SetLineWidth(4)

    dc.Rotate(gg.Radians(30))
    dc.DrawStringAnchored("Hello 1", 200, 200, 0.5, 0.5)
    dc.Rotate(gg.Radians(30))
    dc.DrawStringAnchored("Hello 2", 300, 300, 0.5, 0.5)

    dc.Fill()

    _ = dc.SavePNG("out.png")
}

The result img below:

out

Even the "Hello 2" is not in the image?!

fogleman commented 3 years ago

Rotation occurs about the origin. You can try the RotateAbout function to rotate about a different location.

JasonkayZK commented 3 years ago

This does solve the problem. But new problem occurred when I was trying to create two different string:

    F := 40.0
    dc := gg.NewContext(500, 500)
    dc.Clear()

    str1 := "Hello 1"

    if err := dc.LoadFontFace("Deng.ttf", F); err != nil {
        panic(err)
    }

    dc.SetRGB(1, 0, 0)
    dc.SetLineWidth(4)

    w, h := dc.MeasureString(str1)
    dc.RotateAbout(gg.Radians(30), 200-w/2, 200 - h/2)
    dc.DrawString(str1, 200, 200)
    dc.RotateAbout(gg.Radians(30), 200-w/2, 200 - h/2)
    dc.DrawString("Hello 2", 200, 200)

    dc.Fill()
    _ = dc.SavePNG("out.png")

The result is below:

out

It seems that the second string is influenced by the first drawing. So i was wondering if there has any API that could draw the second string without being influenced by the frist RotateAbout ?

fogleman commented 3 years ago

Use dc.Identity() to reset the transformation function. Alternatively, nest the transformations within dc.Push(), dc.Pop()

JasonkayZK commented 3 years ago

thanks, problem solved!