canvas
canvas copied to clipboard
Text renders upside down and mirrored
Thank you for the fabulous library! It's very fun to use.
I've noticed that text renders upside down and flipped. I checked against fogleman/gg, which does not have the same issue.
Here’s code to reproduce:
package main
import "github.com/h8gi/canvas"
func main() {
const w, h = 1024, 768
c := canvas.NewCanvas(&canvas.CanvasConfig{
Width: w,
Height: h,
FrameRate: 60,
})
c.Draw(func(ctx *canvas.Context) {
ctx.SetRGB(1, 1, 1)
ctx.Clear()
ctx.SetRGB(0, 0, 0)
if err := ctx.LoadFontFace("SpaceMono-Regular.ttf", 48); err != nil {
panic(err)
}
ctx.DrawStringAnchored("Hello, world!", w/2, h/2, 0.5, 0.5)
// Note: this will save the current frame with text rendering correctly.
// ctx.SavePNG("out.png")
})
}
Here’s how it renders:
The full source code for the above, which includes the font can be found in this gist. Let me know if you have any questions!
As a note, I haven't been able to locate the problem, but was able to correct text rendering with a transformation matrix on the text portions of the gg.Context. Specifically, something like:
ctx.ScaleAbout(-1, 1, w/2, h/2)
ctx.RotateAbout(math.Pi, w/2, h/2)
This worked for me
ctx.Push()
x := 100 // text position
y := 100 // text position
ctx.ScaleAbout(-1, 1, x, y)
ctx.RotateAbout(math.Pi, x, y)
ctx.SetColor(colornames.White)
ctx.DrawStringAnchored("Hello wolrd", x, y, 0.5, 0.5)
ctx.Stroke()
ctx.Pop()
I think it has something to do with the origin not being at the upper left corner.