diff --git a/context.go b/context.go index d62e2cb..8ea1372 100644 --- a/context.go +++ b/context.go @@ -4,14 +4,15 @@ package gg import ( "image" "image/color" - "image/draw" "image/png" "io" "math" "github.com/golang/freetype/raster" + "golang.org/x/image/draw" "golang.org/x/image/font" "golang.org/x/image/font/basicfont" + "golang.org/x/image/math/f64" ) type LineCap int @@ -567,12 +568,17 @@ func (dc *Context) DrawImageAnchored(im image.Image, x, y int, ax, ay float64) { s := im.Bounds().Size() x -= int(ax * float64(s.X)) y -= int(ay * float64(s.Y)) - p := image.Pt(x, y) - r := image.Rectangle{p, p.Add(s)} + transformer := draw.BiLinear + fx, fy := float64(x), float64(y) + m := dc.matrix.Translate(fx, fy) + s2d := f64.Aff3{m.XX, m.XY, m.X0, m.YX, m.YY, m.Y0} if dc.mask == nil { - draw.Draw(dc.im, r, im, image.ZP, draw.Over) + transformer.Transform(dc.im, s2d, im, im.Bounds(), draw.Over, nil) } else { - draw.DrawMask(dc.im, r, im, image.ZP, dc.mask, p, draw.Over) + transformer.Transform(dc.im, s2d, im, im.Bounds(), draw.Over, &draw.Options{ + DstMask: dc.mask, + DstMaskP: image.ZP, + }) } } diff --git a/examples/rotated-image.go b/examples/rotated-image.go new file mode 100644 index 0000000..ad478af --- /dev/null +++ b/examples/rotated-image.go @@ -0,0 +1,27 @@ +package main + +import "github.com/fogleman/gg" + +func main() { + const W = 400 + const H = 200 + im, err := gg.LoadPNG("examples/gopher.png") + if err != nil { + panic(err) + } + dc := gg.NewContext(W, H) + // draw outline + dc.SetHexColor("#ff0000") + dc.SetLineWidth(1) + dc.DrawRectangle(0, 0, float64(W), float64(H)) + dc.Stroke() + // draw image with current matrix applied + dc.SetHexColor("#0000ff") + dc.SetLineWidth(2) + dc.Rotate(gg.Radians(10)) + dc.DrawRectangle(100, 0, float64(im.Bounds().Dx()), float64(im.Bounds().Dy())/2) + dc.StrokePreserve() + dc.Clip() + dc.DrawImage(im, 100, 0) + dc.SavePNG("out.png") +}