func main() { const ( S = 256 W = 8 H = 8 ) dc := gg.NewContext(S*W, S*H) dc.SetRGB(1, 1, 1) dc.Clear() for j := 0; j < H; j++ { for i := 0; i < W; i++ { x := float64(i)*S + S/2 y := float64(j)*S + S/2 fmt.Println("x=%d,y=%d", x, y) dc.Push() dc.Translate(x, y) dc.Scale(S/2, S/2) //if j%2 == 0 { // randomCubic(dc) //} else { // randomQuadratic(dc) //} randomQuadratic(dc) dc.Pop() } } dc.SavePNG("bezier.png") }
func main() { const W = 1024 const H = 1024 dc := gg.NewContext(W, H) //上下文,含长和宽 dc.SetRGB(0, 0, 0) //设置当前色 dc.Clear() //清理一下上下文,下面开始画画 for i := 0; i < 1000; i++ { //画1000 条线,随机位置,长度,颜色和透明度 x1 := rand.Float64() * W y1 := rand.Float64() * H x2 := rand.Float64() * W y2 := rand.Float64() * H r := rand.Float64() g := rand.Float64() b := rand.Float64() a := rand.Float64()*0.5 + 0.5 w := rand.Float64()*4 + 1 dc.SetRGBA(r, g, b, a) dc.SetLineWidth(w) dc.DrawLine(x1, y1, x2, y2) //画线 dc.Stroke() //没有这句是不会把线最终画出来的 } dc.SavePNG("lines.png") //保存上下文为一张图片 }
func main() { dc := gg.NewContext(1000, 1000) dc.DrawCircle(500, 500, 400) dc.SetRGB(0, 0, 0) dc.Fill() dc.SavePNG("out.png") }
func main() { const W = 1024 dc := gg.NewContext(W, W) dc.SetRGB(0, 0, 0) dc.DrawCircle(500, 500, 100) dc.Stroke() dc.SavePNG("circle.png") }
func main() { dc := gg.NewContext(1000, 1000) go GraphUpdater(dc) http.HandleFunc("/Values", ValueInsert) if err := http.ListenAndServe(":8085", nil); err != nil { fmt.Println(err.Error()) } return }
func (p Paths) WriteToPNG(path string, width, height float64) { scale := 1.0 w, h := int(width*scale), int(height*scale) dc := gg.NewContext(w, h) dc.InvertY() dc.SetRGB(1, 1, 1) dc.Clear() dc.SetRGB(0, 0, 0) dc.SetLineWidth(3) for _, path := range p { for _, v := range path { dc.LineTo(v.X*scale, v.Y*scale) } dc.NewSubPath() } dc.Stroke() dc.SavePNG(path) }
func (r *Round) Render() (im image.Image, err error) { guild, _ := r.Session.Guild(r.GuildID) if guild == nil { log.WithFields(log.Fields{ "guild": r.GuildID, }).Warning("Failed to grab guild") return } dc := gg.NewContext(390, 220) dc.DrawCircle(195, 0, 220) dc.SetRGB(0.32, 0.63, 0.20) dc.Fill() dc.SetFontFace(inconsolata.Regular8x16) dc.SetRGB(0, 0, 0) for idx, player := range r.Players { hand := player.Hands[0] slot := tableDrawingSlots[idx] username := utils.GetPreferredName(guild, player.UserID) if len(username) > 14 { username = username[:11] + "..." } dc.DrawStringAnchored(username, slot.NameX, slot.NameY, slot.NameAnchorX, slot.NameAnchorY) cardX, cardY := slot.CardX, slot.CardY for _, card := range hand.Pile.Cards { cardIm, err := card.GetImage() if err != nil { log.Errorf("Error getting card image %v", err) continue } dc.DrawImage(cardIm, cardX, cardY) cardX += 10 cardY += 5 } } dc.DrawStringAnchored("Dealer", tableDealerDrawingSlot.NameX, tableDealerDrawingSlot.NameY, tableDealerDrawingSlot.NameAnchorX, tableDealerDrawingSlot.NameAnchorY) cardX, cardY := tableDealerDrawingSlot.CardX, tableDealerDrawingSlot.CardY for _, card := range r.Dealer.Hands[0].Pile.Cards { cardIm, err := card.GetImage() if err != nil { log.Errorf("Error getting card image %v", err) continue } dc.DrawImage(cardIm, cardX, cardY) cardX += 10 cardY += 5 } return dc.Image(), nil }
func main() { objpath := "" flag.Parse() if flag.NArg() > 0 { objpath = flag.Arg(0) } else { showUsage() os.Exit(0) } obj, err := ReadObjFile(objpath, *dbg) if err != nil { fmt.Println(err) os.Exit(1) } if *dbg { fmt.Println("num vertices: ", len(obj.Vertices())) fmt.Println("num faces: ", len(obj.Triangles())) fmt.Println("scale factor: ", *scale) } // get image w/h by scaling the mesh aabb by the user-defined scale factor aabb := obj.AABB() aabb.Scale(float64(*scale)) offx, offy := int(math.Floor(aabb.MinX)), int(math.Floor(aabb.MinZ)) w, h := int(math.Ceil(aabb.MaxX)), int(math.Ceil(aabb.MaxZ)) dc := gg.NewContext(w, h) dc.SetRGB255(255, 255, 255) dc.Clear() for _, t := range obj.Triangles() { // scale current triangle t.Scale(float64(*scale)) // draw the triangle dc.MoveTo(t.P1.X(), t.P1.Z()) dc.LineTo(t.P2.X(), t.P2.Z()) dc.LineTo(t.P3.X(), t.P3.Z()) dc.LineTo(t.P1.X(), t.P1.Z()) dc.SetRGB255(0, 0, 0) dc.SetLineWidth(1) dc.FillPreserve() dc.Stroke() } fmt.Println("Rasterization completed") fmt.Println("Creating", *pngfile) f, err := os.Create(*pngfile) if err != nil { fmt.Println("Can't create ", *pngfile, ": ", err) os.Exit(1) } // convert image into a black and white one bw := []color.Color{color.Black, color.White} gr := &Converter{dc.Image(), color.Palette(bw)} err = png.Encode(f, gr) if err != nil { fmt.Println("Can't encode ", *pngfile, ": ", err) os.Exit(1) } fmt.Println("Resulting image details") if *dbg { fmt.Println("original mesh bounding box: ", obj.AABB()) fmt.Println("scaled mesh bounding box: ", aabb) } fmt.Println("x, y offset: ", offx, offy) fmt.Println("scale: ", *scale) fmt.Println("width/height: ", w, h) }
func (req memeRequest) Serve(rw http.ResponseWriter) { bgImg, err := req.h.img.Get(req.meme, imgW, imgH) if err != nil { rw.WriteHeader(http.StatusBadRequest) log.Printf("Get('%s'): %s", req.meme, err) return } log.Printf("%s: \"%s\", \"%s\"", req.meme, req.top, req.bottom) dc := gg.NewContext(imgW, imgH) dc.DrawImage(bgImg, 0, 0) dc.SetFontFace(req.h.font) drawString := func(s string, baseY float64) { dc.SetRGB(0, 0, 0) n := 6 // "stroke" size for dy := -n; dy <= n; dy++ { for dx := -n; dx <= n; dx++ { if dx*dx+dy*dy >= n*n { // give it rounded corners continue } x := imgW/2 + float64(dx) y := baseY + float64(dy) dc.DrawStringAnchored(s, x, y, 0.5, 0.5) } } dc.SetRGB(1, 1, 1) dc.DrawStringAnchored(s, imgW/2, baseY, 0.5, 0.5) } drawString(req.top, float64(int(math.Ceil(*size**dpi/72))-20)) drawString(req.bottom, imgH-30) b := bytes.Buffer{} err = png.Encode(&b, dc.Image()) if err != nil { log.Printf("png.Encode: %s", err) rw.WriteHeader(http.StatusInternalServerError) return } rw.Header().Set("Content-Type", "image/png") rw.Write(b.Bytes()) }