本文整理汇总了Golang中github.com/llgcode/draw2d/draw2dimg.NewGraphicContext函数的典型用法代码示例。如果您正苦于以下问题:Golang NewGraphicContext函数的具体用法?Golang NewGraphicContext怎么用?Golang NewGraphicContext使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewGraphicContext函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Load
// Load an image from a PNG file
func Load(path string) (*Image, error) {
var img Image
file, err := os.Open(path)
if err != nil {
fmt.Println("ERROR: Cannot open file", path)
return nil, err
}
defer func() {
if err := file.Close(); err != nil {
panic(err)
}
}()
pngImage, err := png.Decode(file)
if err != nil {
fmt.Println("ERROR: Cannot decode PNG file", path)
return nil, err
}
// Copy to surface
b := pngImage.Bounds()
img.Surf = image.NewRGBA(image.Rect(0, 0, b.Max.X-b.Min.X, b.Max.Y-b.Min.Y))
draw.Draw(img.Surf, img.Surf.Bounds(), pngImage, pngImage.Bounds().Min, draw.Src)
img.Ctx = draw2dimg.NewGraphicContext(img.Surf)
br := img.Surf.Bounds()
img.W, img.H = br.Max.X-br.Min.X, br.Max.Y-br.Min.Y
return &img, nil
}
示例2: DrawClosedLine
// DrawClosedLine draws the line represented by the Points and closes it.
func DrawClosedLine(p Points, width, height float64, name string) {
// Initialize the graphic context on an RGBA image
dest := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
gc := draw2dimg.NewGraphicContext(dest)
// Set some properties
gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
gc.SetLineWidth(5)
paths := []*draw2d.Path{}
// Draw a path
for idx, point := range p[:len(p)] {
path := new(draw2d.Path)
path.MoveTo(float64(point.X), height-float64(point.Y))
if idx < len(p)-1 {
path.LineTo(float64(p[idx+1].X), height-float64(p[idx+1].Y))
} else {
path.LineTo(float64(p[0].X), height-float64(p[0].Y))
}
paths = append(paths, path)
}
gc.Stroke(paths...)
gc.FillStroke()
// Save to file
draw2dimg.SaveToPngFile(name, dest)
}
示例3: faceDetect
func faceDetect(settings CropSettings, i image.Image, o image.Image) error {
cvImage := opencv.FromImage(i)
_, err := os.Stat(settings.FaceDetectionHaarCascadeFilepath)
if err != nil {
return err
}
cascade := opencv.LoadHaarClassifierCascade(settings.FaceDetectionHaarCascadeFilepath)
faces := cascade.DetectObjects(cvImage)
gc := draw2dimg.NewGraphicContext((o).(*image.RGBA))
if settings.DebugMode == true {
log.Println("Faces detected:", len(faces))
}
for _, face := range faces {
if settings.DebugMode == true {
log.Printf("Face: x: %d y: %d w: %d h: %d\n", face.X(), face.Y(), face.Width(), face.Height())
}
draw2dkit.Ellipse(
gc,
float64(face.X()+(face.Width()/2)),
float64(face.Y()+(face.Height()/2)),
float64(face.Width()/2),
float64(face.Height())/2)
gc.SetFillColor(color.RGBA{255, 0, 0, 255})
gc.Fill()
}
return nil
}
示例4: render
func render(fr ani.Frame, fnum int) image.Image {
w, h := fr.SurfaceDims()
// Blank white image
bounds := image.Rect(0, 0, int(w), int(h))
dest := image.NewRGBA(bounds)
draw.Draw(dest, bounds, &image.Uniform{white}, image.ZP, draw.Src)
shapes := []string{
"circle",
"square",
}
plts := []plot{}
for _, sh := range shapes {
plts = append(plts, plotShape(sh, fr))
}
plts = append(plts, plotExplode(fr))
gc := dimg.NewGraphicContext(dest)
gc.SetStrokeColor(black)
gc.SetFillColor(black)
for _, plot := range plts {
plot.draw(gc)
}
return dest
}
示例5: newImg
func newImg(filename string, w, h, levels int) img {
// Leave a 5% buffer on the sides so that the image doesn't cut right up to
// the edge
bw, bh := float64(w)*0.95, float64(h)*0.95
var levelWidth float64
if w > h {
levelWidth = bh / 2 / float64(levels)
} else {
levelWidth = bw / 2 / float64(levels)
}
if levelWidth < 1 {
log.Fatalf("level width is too small! %f", levelWidth)
}
rgba := image.NewRGBA(image.Rect(0, 0, w, h))
ctx := draw2dimg.NewGraphicContext(rgba)
return img{
filename: filename,
w: w,
h: h,
centerX: float64(w) / 2,
centerY: float64(h) / 2,
levelWidth: int(levelWidth),
rgba: rgba,
ctx: ctx,
}
}
示例6: drawLines
func drawLines(src image.Image, lines []polarLine) image.Image {
dst := image.NewRGBA(src.Bounds())
gc := draw2dimg.NewGraphicContext(dst)
gc.SetFillColor(color.RGBA{0x0, 0x0, 0x0, 0xff})
gc.SetStrokeColor(color.RGBA{0x0, 0xff, 0x0, 0xff})
gc.SetLineWidth(2)
gc.Clear()
gc.DrawImage(src)
for _, line := range lines {
a := math.Cos(line.Theta)
b := math.Sin(line.Theta)
x0 := a * float64(line.Distance)
y0 := b * float64(line.Distance)
x1 := (x0 + 10000*(-b))
y1 := (y0 + 10000*(a))
x2 := (x0 - 10000*(-b))
y2 := (y0 - 10000*(a))
gc.MoveTo(x1, y1)
gc.LineTo(x2, y2)
gc.Close()
}
gc.FillStroke()
return dst
}
示例7: Create
// Create an image of the given size
func Create(w, h int, mode ColorSpace) *Image {
var img Image
img.Surf = image.NewRGBA(image.Rect(0, 0, w, h))
img.Ctx = draw2dimg.NewGraphicContext(img.Surf)
img.W, img.H = w, h
return &img
}
示例8: main
func main() {
// Initialize the graphic context on an RGBA image
dest := image.NewRGBA(image.Rect(0, 0, 1300, 1200))
gc := draw2dimg.NewGraphicContext(dest)
gb := draw2dimg.NewGraphicContext(dest)
n := draw2d.NewRotationMatrix(math.Pi / 2)
n.Translate(100, -100)
gb.SetMatrixTransform(n)
Draw(gb, "My Text")
// Set some properties
gc.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff})
gc.SetStrokeColor(color.RGBA{0x00, 0x00, 0x00, 0xff})
gc.SetLineWidth(3)
m := draw2d.NewRotationMatrix(-0.3)
// m.TransformRectangle(40, 40, 100, 500)
m.Translate(75, 75)
gc.SetMatrixTransform(m)
// Draw a closed shape
gc.MoveTo(39, 39) // should always be called first for a new path
gc.LineTo(39, 512)
gc.LineTo(512, 512)
gc.LineTo(512, 39)
gc.LineTo(39, 39)
//gc.LineTo(100, 50)
//gc.QuadCurveTo(100, 10, 10, 10)
gc.Close()
gc.FillStroke()
gc.SetFillColor(color.RGBA{0xff, 0xff, 0x44, 0x77})
gc.SetMatrixTransform(draw2d.NewRotationMatrix(0))
gc.MoveTo(47, 47) // should always be called first for a new path
gc.LineTo(47, 617)
gc.LineTo(617, 617)
gc.LineTo(617, 47)
gc.LineTo(47, 47)
gc.Close()
gc.FillStroke()
g1 := []Point{{1, 2}, {2, 3}, {3, 4}, {4, 3}}
Plot(gb, g1)
// Save to file
draw2dimg.SaveToPngFile("hello.png", dest)
}
示例9: NewCanvas
func NewCanvas(c *Canvas) *Canvas {
dest := image.NewRGBA(c.Rect())
ctx := dwg.NewGraphicContext(dest)
c.Dest = dest
c.GraphicContext = ctx
return c
}
示例10: NewContext
func NewContext() *Context {
img := image.NewRGBA(image.Rect(0, 0, 640, 360))
ctx := &Context{
img,
draw2dimg.NewGraphicContext(img),
StyleDefault,
}
ctx.SetFontData(draw2d.FontData{Name: "Default"})
return ctx
}
示例11: Draw
func (r *Renderer) Draw() image.Image {
pixelsX, pixelsY := int(r.width), int(r.height)
dest := image.NewRGBA(image.Rect(0, 0, pixelsX, pixelsY))
draw.Draw(dest, dest.Bounds(), &image.Uniform{r.m.BgColor}, image.ZP, draw.Src)
draw2d.SetFontFolder("/Library/Fonts/")
draw2d.SetFontNamer(func(fontData draw2d.FontData) string {
return fontData.Name + ".ttf"
})
gc := draw2dimg.NewGraphicContext(dest)
// gc.DPI = 300
gc.SetLineCap(draw2d.RoundCap)
gc.SetFillColor(r.m.BgColor)
gc.SetStrokeColor(r.m.Stroke)
gc.SetFontData(draw2d.FontData{Name: "Georgia", Family: draw2d.FontFamilySerif, Style: draw2d.FontStyleNormal})
dx := math.Abs(r.bbox[2] - r.bbox[0])
dy := math.Abs(r.bbox[3] - r.bbox[1])
pxf, pyf := float64(pixelsX), float64(pixelsY)
r1, r2 := (pxf / dx), (pyf / dy)
r0 := math.Min(r1, r2)
w, h := dx*r0, dy*r0
ox, oy := (pxf-w)/2, (pyf-h)/2
img_box := [4]float64{ox, oy, ox + w, oy + h}
r.matrix = draw2d.NewMatrixFromRects(r.bbox, img_box)
for _, layer := range r.m.Layers {
q := query.NewQuery(r.m.Bounds()).Select(layer.SourceQuery())
if ds := layer.LoadSource(); ds != nil {
defer ds.Close()
for shp := range ds.Query(q) {
var symbolizerType util.SymbolizerType
switch shp.(type) {
case geom.LineShape, geom.MultiLineShape:
symbolizerType = util.PathType
case geom.PolygonShape:
symbolizerType = util.PolygonType
}
for _, symbolizer := range r.findSymbolizers(layer, symbolizerType) {
symbolizer.Draw(gc, shp)
}
for _, symbolizer := range r.findSymbolizers(layer, util.TextType) {
symbolizer.Draw(gc, shp)
}
}
}
}
return dest
}
示例12: NewPlotter
func NewPlotter(height, width float64) *Plotter {
pl := Plotter{lineWidth: 3, height: height, width: width, border: 10, xSteps: 10, ySteps: 5}
pl.image = image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
pl.g = draw2dimg.NewGraphicContext(pl.image)
// Set some properties
pl.g.SetFillColor(color.RGBA{0x44, 0xff, 0x44, 0xff})
pl.g.SetStrokeColor(color.Black)
return &pl
}
示例13: main
func main() {
i := image.NewRGBA(image.Rect(0, 0, 200, 200))
gc := draw2dimg.NewGraphicContext(i)
gc.Save()
gc.SetStrokeColor(color.Black)
gc.SetFillColor(color.Black)
draw2d.Rect(gc, 10, 10, 100, 100)
gc.FillStroke()
gc.Restore()
draw2dimg.SaveToPngFile("yay-rectangle.png", i)
}
示例14: DrawTest
func DrawTest() {
// Initialize the graphic context on an RGBA image
dest := image.NewRGBA(image.Rect(0, 0, 297, 210.0))
gc := draw2dimg.NewGraphicContext(dest)
DrawHello(gc, "Hello World")
// Save to png
err := draw2dimg.SaveToPngFile("_testHello.png", dest)
if err != nil {
log.Fatalln("Saving failed:", err)
}
}
示例15: drawPolyLine
func drawPolyLine(img *image.RGBA, color color.Color, coords [][]float64) {
path := new(draw2d.Path)
for i, coord := range coords {
if i == 0 {
path.MoveTo(coord[0], coord[1])
} else {
path.LineTo(coord[0], coord[1])
}
}
gc := draw2dimg.NewGraphicContext(img)
gc.SetStrokeColor(color)
gc.Stroke(path)
}