fogleman / gg

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

Rotate letters inside string? #75

Open s3rj1k opened 5 years ago

s3rj1k commented 5 years ago

How to rotate multiple string characters in different angles?

Can you provide example with multiple string objects rotated by 10/-10 degrees that are located in one line?

Use case for captcha generation.

jpap commented 5 years ago

I would try just rendering each character separately, using a per-character rotational transform. You won't get proper kerning without some work, but the per-character rotation will likely mean using regular kerning makes the whole thing look silly anyhow. (Many captchas I've seen have fixed spacing between letters, though there's no reason that spacing can also be randomly perturbed.)

s3rj1k commented 5 years ago

@jpap that was my initial approach, I failed on rotating per character :))

jpap commented 5 years ago

Have you looked at the examples/rotated-text.go example?

For N characters, I'd imagine you have a loop over n \in N and do something like:

s3rj1k commented 5 years ago

@jpap exactly what I did, but the rotation angle seems off with every next letter. Maybe a missed Translate() ...

tagaism commented 4 years ago

@s3rj1k any update on this ussie? Did you figure out how to rotate several objects in different angles?

s3rj1k commented 4 years ago

@tagaism I think I did not used rotation in the end, but the issue is still valid.

avdb13 commented 3 years ago
for _, char := range str {
    dc.Push()
    dc.RotateAbout(gg.Radians(20 * rand.Float64()), 0, (stdHeight/2))
    dc.SetRGBA(0, 0, 0, (rand.Float64()) / 2 + 0.25)
    dc.DrawStringAnchored(string(char), spacePerChar / 2, float64(stdHeight/2), 0.5, 0.5)
    dc.Pop()
    dc.Translate(spacePerChar, 0)
}

This seems to be working. You have to play around with things a little bit to reset the rotation angle but not the place translation.