ZetCode

Go draw2d

最后修改时间 2024 年 4 月 11 日

在本文中,我们将展示如何在 Go 中使用 draw2d 处理图形。

draw2d 包是一个纯 Go 的二维矢量图形库。它支持多种输出设备,包括图像 (draw2d)、PDF 文档 (draw2dpdf) 和 opengl (draw2dgl)。

在我们的示例中,我们在图像输出设备上进行绘制。

绘制矩形

在第一个示例中,我们绘制一个矩形。

main.go
package main

import (
    "image"

    "github.com/llgcode/draw2d"
    "github.com/llgcode/draw2d/draw2dimg"
    "github.com/llgcode/draw2d/draw2dkit"
)

func main() {

    dest := image.NewRGBA(image.Rect(0, 0, 300, 300))
    gc := draw2dimg.NewGraphicContext(dest)

    gc.SetStrokeColor(image.White)
    draw2dkit.Rectangle(gc, 50, 50, 200, 200)
    gc.Stroke()

    draw2dimg.SaveToPngFile("srect.png", dest)
}

该示例在透明背景上绘制一个白色矩形。

dest := image.NewRGBA(image.Rect(0, 0, 300, 300))

创建了一个绘图目标。

gc := draw2dimg.NewGraphicContext(dest)

从目标中,我们创建一个绘图上下文。它是一个用于绘图的接口。

gc.SetStrokeColor(image.White)

我们使用 SetStrokeColor 将描边颜色设置为白色。

draw2dkit.Rectangle(gc, 50, 50, 200, 200)

我们使用 Rectangle 绘制一个矩形。参数是图形上下文以及矩形左上角和右下角的 x, y 坐标。

gc.Stroke()

使用 Stroke 描边矩形路径。

draw2dimg.SaveToPngFile("srect.png", dest)

我们使用 SaveToPngFile 将绘图保存到 PNG 文件。

绘制直线

在下一个示例中,我们绘制一条直线。

main.go
package main

import (
    "image"

    "github.com/llgcode/draw2d"
    "github.com/llgcode/draw2d/draw2dimg"
    "github.com/llgcode/draw2d/draw2dkit"
)

func main() {

    dest := image.NewRGBA(image.Rect(0, 0, 300, 300))
    gc := draw2dimg.NewGraphicContext(dest)

    gc.SetFillColor(image.White)
    draw2dkit.Rectangle(gc, 0, 0, 300, 300)
    gc.Fill()

    DrawLine(gc, 0, 0, 300, 300)
    draw2dimg.SaveToPngFile("line.png", dest)
}

func DrawLine(gc draw2d.GraphicContext, x0, y0, x1, y1 float64) {

    gc.MoveTo(x0, y0)
    gc.LineTo(x1, y1)
    gc.Stroke()
}

在白色背景上绘制一条对角黑色线。

gc.SetFillColor(image.White)
draw2dkit.Rectangle(gc, 0, 0, 300, 300)
gc.Fill()

我们将表面的背景填充为白色。

func DrawLine(gc draw2d.GraphicContext, x0, y0, x1, y1 float64) {

    gc.MoveTo(x0, y0)
    gc.LineTo(x1, y1)
    gc.Stroke()
}

我们使用 MoveToLineTo 构建路径,然后使用 Stroke 描边它。

绘制圆形

使用 Ellipse 绘制圆形。

main.go
package main

import (
    "image"
    "image/color"

    "github.com/llgcode/draw2d"
    "github.com/llgcode/draw2d/draw2dimg"
    "github.com/llgcode/draw2d/draw2dkit"
)

func main() {

    dest := image.NewRGBA(image.Rect(0, 0, 300, 300))
    gc := draw2dimg.NewGraphicContext(dest)

    gc.SetFillColor(image.White)
    draw2dkit.Rectangle(gc, 0, 0, 300, 300)
    gc.Fill()

    DrawCircle(gc, 0, 0, 300, 300)

    draw2dimg.SaveToPngFile("circle.png", dest)
}

func DrawCircle(gc draw2d.GraphicContext) {

    draw2dkit.Ellipse(gc, 150, 150, 80, 80)
    gc.SetFillColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
    gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
    gc.FillStroke()
}

在表面中间绘制一个灰色圆。

func DrawCircle(gc draw2d.GraphicContext) {

    draw2dkit.Ellipse(gc, 150, 150, 80, 80)
    gc.SetFillColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
    gc.SetStrokeColor(color.RGBA{0x44, 0x44, 0x44, 0xff})
    gc.FillStroke()
}

我们使用 Ellipse 绘制圆。我们使用 SetFillColorSetStrokeColor 设置填充和描边颜色。FillStroke 会填充路径然后描边路径。

绘制三次曲线

使用 CubicCurveTo 绘制三次曲线。

main.go
package main

import (
    "image"

    "github.com/llgcode/draw2d"
    "github.com/llgcode/draw2d/draw2dimg"
    "github.com/llgcode/draw2d/draw2dkit"
)

func main() {

    dest := image.NewRGBA(image.Rect(0, 0, 300, 300))
    gc := draw2dimg.NewGraphicContext(dest)

    gc.SetFillColor(image.White)
    draw2dkit.Rectangle(gc, 0, 0, 300, 300)
    gc.Fill()

    DrawCurvedLine(gc)

    draw2dimg.SaveToPngFile("curved.png", dest)
}

func DrawCurvedLine(gc draw2d.GraphicContext) {

    gc.MoveTo(30, 30)
    gc.CubicCurveTo(200, 150, 250, 100, 280, 30)
    gc.Stroke()
}

该示例绘制一条三次曲线。

func DrawCurvedLine(gc draw2d.GraphicContext) {

    gc.MoveTo(30, 30)
    gc.CubicCurveTo(200, 150, 250, 100, 280, 30)
    gc.Stroke()
}

我们使用 MoveTo 前往起点。使用 CubicCurveTo,我们绘制三次曲线。前两个参数是两个控制点的 x, y 坐标。最后两个值是终点的坐标。

来源

Go draw2d - Github 页面

在本文中,我们使用 draw2d 包在 Golang 中处理了图形。

作者

我叫 Jan Bodnar,我是一名充满热情的程序员,拥有丰富的编程经验。自 2007 年以来,我一直撰写编程文章。迄今为止,我已撰写了 1400 多篇文章和 8 本电子书。我在编程教学方面拥有十多年的经验。

列出所有 Go 教程