apply current matrix when draw image

This commit is contained in:
wsw 2016-12-08 11:16:19 +08:00
parent d60a19b37a
commit f06d3564a1
2 changed files with 38 additions and 5 deletions

View File

@ -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,
})
}
}

27
examples/rotated-image.go Normal file
View File

@ -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")
}