// FillString draws a filled and stroked string. func FillString(gc draw2d.GraphicContext, x, y, width, height float64) { sx, sy := width/100, height/100 gc.Save() gc.SetStrokeColor(image.Black) gc.SetLineWidth(1) draw2d.RoundRect(gc, x+sx*5, y+sy*5, x+sx*95, y+sy*95, sx*10, sy*10) gc.FillStroke() gc.SetFillColor(image.Black) gc.SetFontSize(height / 6) gc.Translate(x+sx*6, y+sy*52) gc.SetFontData(draw2d.FontData{ Name: "luxi", Family: draw2d.FontFamilyMono, Style: draw2d.FontStyleBold | draw2d.FontStyleItalic}) w := gc.FillString("Hug") gc.Translate(w+sx, 0) left, top, right, bottom := gc.GetStringBounds("cou") gc.SetStrokeColor(color.NRGBA{255, 0x33, 0x33, 0x80}) draw2d.Rect(gc, left, top, right, bottom) gc.SetLineWidth(height / 50) gc.Stroke() gc.SetFillColor(color.NRGBA{0x33, 0x33, 0xff, 0xff}) gc.SetStrokeColor(color.NRGBA{0x33, 0x33, 0xff, 0xff}) gc.SetLineWidth(height / 100) gc.StrokeString("Hug") gc.Restore() }
// Draw "Hello World" func Draw(gc draw2d.GraphicContext, text string) { // Draw a rounded rectangle using default colors draw2d.RoundRect(gc, 5, 5, 292, 205, 10, 10) gc.FillStroke() // Set the font luximbi.ttf gc.SetFontData(draw2d.FontData{ Name: "luxi", Family: draw2d.FontFamilyMono, Style: draw2d.FontStyleBold | draw2d.FontStyleItalic}) // Set the fill text color to black gc.SetFillColor(image.Black) gc.SetDPI(72) gc.SetFontSize(14) // Display Hello World gc.SetStrokeColor(color.NRGBA{0x33, 0xFF, 0x33, 0xFF}) gc.MoveTo(8, 0) gc.LineTo(8, 52) gc.LineTo(297, 52) gc.Stroke() gc.FillString(text) gc.FillStringAt(text, 8, 52) gc.Save() gc.SetFillColor(color.NRGBA{0xFF, 0x33, 0x33, 0xFF}) gc.SetStrokeColor(color.NRGBA{0xFF, 0x33, 0x33, 0xFF}) gc.Translate(145, 85) gc.StrokeStringAt(text, -50, 0) gc.Rotate(math.Pi / 4) gc.SetFillColor(color.NRGBA{0x33, 0x33, 0xFF, 0xFF}) gc.SetStrokeColor(color.NRGBA{0x33, 0x33, 0xFF, 0xFF}) gc.StrokeString(text) gc.Restore() }
// Draw the image frame with certain parameters. func Draw(gc draw2d.GraphicContext, png string, dw, dh, margin, lineWidth float64) error { // Draw frame draw2dkit.RoundedRectangle(gc, lineWidth, lineWidth, dw-lineWidth, dh-lineWidth, 100, 100) gc.SetLineWidth(lineWidth) gc.FillStroke() // load the source image source, err := draw2dimg.LoadFromPngFile(png) if err != nil { return err } // Size of source image sw, sh := float64(source.Bounds().Dx()), float64(source.Bounds().Dy()) // Draw image to fit in the frame // TODO Seems to have a transform bug here on draw image scale := math.Min((dw-margin*2)/sw, (dh-margin*2)/sh) gc.Save() gc.Translate((dw-sw*scale)/2, (dh-sh*scale)/2) gc.Scale(scale, scale) gc.Rotate(0.2) gc.DrawImage(source) gc.Restore() return nil }
// Main draws a left hand and ear of a gopher. Afterwards it returns // the filename. This should only be used during testing. func Main(gc draw2d.GraphicContext, ext string) (string, error) { gc.Save() gc.Scale(0.5, 0.5) // Draw a (partial) gopher Draw(gc) gc.Restore() // Return the output filename return samples.Output("gopher", ext), nil }
// PathTransform scales a path differently in horizontal and vertical direction. func PathTransform(gc draw2d.GraphicContext, x, y, width, height float64) { gc.Save() gc.SetLineWidth(width / 10) gc.Translate(x+width/2, y+height/2) gc.Scale(1, 4) gc.ArcTo(0, 0, width/8, height/8, 0, math.Pi*2) gc.Close() gc.Stroke() gc.Restore() }
// Main draws a rotated face of the gopher. Afterwards it returns // the filename. This should only be used during testing. func Main(gc draw2d.GraphicContext, ext string) (string, error) { gc.SetStrokeColor(image.Black) gc.SetFillColor(image.White) gc.Save() // Draw a (partial) gopher gc.Translate(-60, 65) gc.Rotate(-30 * (math.Pi / 180.0)) Draw(gc, 48, 48, 240, 72) gc.Restore() // Return the output filename return samples.Output("gopher2", ext), nil }
// Star draws many lines from a center. func Star(gc draw2d.GraphicContext, x, y, width, height float64) { gc.Save() gc.Translate(x+width/2, y+height/2) gc.SetLineWidth(width / 40) for i := 0.0; i < 360; i = i + 10 { // Go from 0 to 360 degrees in 10 degree steps gc.Save() // Keep rotations temporary gc.Rotate(i * (math.Pi / 180.0)) // Rotate by degrees on stack from 'for' gc.MoveTo(0, 0) gc.LineTo(width/2, 0) gc.Stroke() gc.Restore() } gc.Restore() }
// Main draws the tiger func Main(gc draw2d.GraphicContext, ext string) (string, error) { gc.Save() // flip the image gc.Translate(0, 200) gc.Scale(0.35, -0.35) gc.Translate(70, -200) // Tiger postscript drawing tiger := samples.Resource("image", "tiger.ps", ext) // Draw tiger Draw(gc, tiger) gc.Restore() // Return the output filename return samples.Output("postscript", ext), nil }