gg icon indicating copy to clipboard operation
gg copied to clipboard

Rotate letters inside string?

Open s3rj1k opened this issue 5 years ago • 7 comments

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.

s3rj1k avatar May 12 '19 11:05 s3rj1k

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.)

jpap avatar May 16 '19 07:05 jpap

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

s3rj1k avatar May 16 '19 19:05 s3rj1k

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:

  • You Push() a new context.
  • Apply a Translate() to move across one character space.
  • Apply a new rotation using RotateAbout().
  • Draw the n-th character using DrawStringAnchored().
  • You Pop() the old context back.

jpap avatar May 16 '19 20:05 jpap

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

s3rj1k avatar May 16 '19 20:05 s3rj1k

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

tagaism avatar Jul 09 '20 15:07 tagaism

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

s3rj1k avatar Jul 09 '20 16:07 s3rj1k

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.

avdb13 avatar Jul 08 '21 09:07 avdb13