Documentation ¶
Overview ¶
Package gg provides a simple API for rendering 2D graphics in pure Go.
包 gg 提供了一个简单的API,用于在纯Go中渲染二维图形。
Index ¶
- func Degrees(radians float64) float64
- func GetWH(path string) (int, int, error)
- func ImageToNRGBA(src image.Image) *image.NRGBA
- func ImageToNRGBA64(src image.Image) *image.NRGBA64
- func ImageToRGBA(src image.Image) *image.RGBA
- func ImageToRGBA64(src image.Image) *image.RGBA64
- func LoadFontFace(path string, points float64) (face font.Face, err error)
- func LoadImage(path string) (image.Image, error)
- func LoadJPG(path string) (image.Image, error)
- func LoadPNG(path string) (image.Image, error)
- func ParseFontFace(b []byte, points float64) (face font.Face, err error)
- func ParseHexColor(x string) (r, g, b, a int)
- func Radians(degrees float64) float64
- func SaveJPG(path string, im image.Image, quality int) error
- func SavePNG(path string, im image.Image) error
- func TakeColor(img image.Image, k int) []color.RGBA
- type Align
- type Context
- func (dc *Context) AsMask() *image.Alpha
- func (dc *Context) Brightness(per int)
- func (dc *Context) Clear()
- func (dc *Context) ClearPath()
- func (dc *Context) Clip()
- func (dc *Context) ClipPreserve()
- func (dc *Context) ClosePath()
- func (dc *Context) Contrast(per int)
- func (dc *Context) CubicTo(x1, y1, x2, y2, x3, y3 float64)
- func (dc *Context) DrawArc(x, y, r, angle1, angle2 float64)
- func (dc *Context) DrawCircle(x, y, r float64)
- func (dc *Context) DrawEllipse(x, y, rx, ry float64)
- func (dc *Context) DrawEllipticalArc(x, y, rx, ry, angle1, angle2 float64)
- func (dc *Context) DrawImage(im image.Image, x, y int)
- func (dc *Context) DrawImageAnchored(im image.Image, x, y int, ax, ay float64)
- func (dc *Context) DrawLine(x1, y1, x2, y2 float64)
- func (dc *Context) DrawPoint(x, y, r float64)
- func (dc *Context) DrawRectangle(x, y, w, h float64)
- func (dc *Context) DrawRegularPolygon(n int, x, y, r, rotation float64)
- func (dc *Context) DrawRoundedRectangle(x, y, w, h, r float64)
- func (dc *Context) DrawString(s string, x, y float64)
- func (dc *Context) DrawStringAnchored(s string, x, y, ax, ay float64)
- func (dc *Context) DrawStringWrapped(s string, x, y, ax, ay, width, lineSpacing float64, align Align)
- func (dc *Context) EncodeJPG(w io.Writer, o *jpeg.Options) error
- func (dc *Context) EncodePNG(w io.Writer) error
- func (dc *Context) Fill()
- func (dc *Context) FillPreserve()
- func (dc *Context) FontHeight() float64
- func (dc *Context) GetCurrentPoint() (Point, bool)
- func (dc *Context) H() int
- func (dc *Context) Hash() [md5.Size]byte
- func (dc *Context) Height() int
- func (dc *Context) Identity()
- func (dc *Context) Image() image.Image
- func (dc *Context) InvertMask()
- func (dc *Context) InvertY()
- func (dc *Context) LineTo(x, y float64)
- func (dc *Context) LoadFontFace(path string, points float64) error
- func (dc *Context) LoadImage(path string, x, y int) error
- func (dc *Context) MeasureMultilineString(s string, lineSpacing float64) (width, height float64)
- func (dc *Context) MeasureString(s string) (w, h float64)
- func (dc *Context) MoveTo(x, y float64)
- func (dc *Context) NewSubPath()
- func (dc *Context) ParseFontFace(data []byte, points float64) error
- func (dc *Context) Pop()
- func (dc *Context) Push()
- func (dc *Context) QuadraticTo(x1, y1, x2, y2 float64)
- func (dc *Context) ResetClip()
- func (dc *Context) Rotate(angle float64)
- func (dc *Context) RotateAbout(angle, x, y float64)
- func (dc *Context) SaveJPG(path string, quality int) error
- func (dc *Context) SavePNG(path string) error
- func (dc *Context) Scale(x, y float64)
- func (dc *Context) ScaleAbout(sx, sy, x, y float64)
- func (dc *Context) SetColor(c color.Color)
- func (dc *Context) SetDash(dashes ...float64)
- func (dc *Context) SetDashOffset(offset float64)
- func (dc *Context) SetFillRule(fillRule FillRule)
- func (dc *Context) SetFillRuleEvenOdd()
- func (dc *Context) SetFillRuleWinding()
- func (dc *Context) SetFillStyle(pattern Pattern)
- func (dc *Context) SetFontFace(fontFace font.Face)
- func (dc *Context) SetHexColor(x string)
- func (dc *Context) SetLineCap(lineCap LineCap)
- func (dc *Context) SetLineCapButt()
- func (dc *Context) SetLineCapRound()
- func (dc *Context) SetLineCapSquare()
- func (dc *Context) SetLineJoin(lineJoin LineJoin)
- func (dc *Context) SetLineJoinBevel()
- func (dc *Context) SetLineJoinRound()
- func (dc *Context) SetLineWidth(lineWidth float64)
- func (dc *Context) SetMask(mask *image.Alpha) error
- func (dc *Context) SetPixel(x, y int)
- func (dc *Context) SetRGB(r, g, b float64)
- func (dc *Context) SetRGB255(r, g, b int)
- func (dc *Context) SetRGBA(r, g, b, a float64)
- func (dc *Context) SetRGBA255(r, g, b, a int)
- func (dc *Context) SetScaleApproxBiLinear()
- func (dc *Context) SetScaleBiLinear()
- func (dc *Context) SetScaleCatmullRom()
- func (dc *Context) SetScaleNearestNeighbor()
- func (dc *Context) SetScaleStyle(s ScaleStyle)
- func (dc *Context) SetStrokeStyle(pattern Pattern)
- func (dc *Context) Shear(x, y float64)
- func (dc *Context) ShearAbout(sx, sy, x, y float64)
- func (dc *Context) String() string
- func (dc *Context) Stroke()
- func (dc *Context) StrokePreserve()
- func (dc *Context) TakeColor(k int) []color.RGBA
- func (dc *Context) TransformPoint(x, y float64) (tx, ty float64)
- func (dc *Context) Translate(x, y float64)
- func (dc *Context) W() int
- func (dc *Context) Width() int
- func (dc *Context) WordWrap(s string, w float64) []string
- type FillRule
- type Gradient
- type LineCap
- type LineJoin
- type Matrix
- func (a Matrix) Multiply(b Matrix) Matrix
- func (a Matrix) Rotate(angle float64) Matrix
- func (a Matrix) Scale(x, y float64) Matrix
- func (a Matrix) Shear(x, y float64) Matrix
- func (a Matrix) TransformPoint(x, y float64) (tx, ty float64)
- func (a Matrix) TransformVector(x, y float64) (tx, ty float64)
- func (a Matrix) Translate(x, y float64) Matrix
- type Pattern
- type Point
- type RepeatOp
- type ScaleStyle
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ImageToNRGBA64 ¶
image.Image 转为 *image.NRGBA64
func LoadFontFace ¶
LoadFontFace is a helper function to load the specified font file with the specified point size. Note that the returned `font.Face` objects are not thread safe and cannot be used in parallel across goroutines. You can usually just use the Context.LoadFontFace function instead of this package-level function.
LoadFontFace 是一个辅助函数,用于加载指定点大小的指定字体文件。 请注意,返回的 `font.Face` 对象不是线程安全的,不能跨 goroutine 并行使用。 您通常可以只使用 Context.LoadFontFace 函数而不是这个包级函数。
func ParseFontFace ¶ added in v1.1.0
ParseFontFace 是一个辅助函数,用于加载指定点大小的指定字体文件。 请注意,返回的 `font.Face` 对象不是线程安全的,不能跨 goroutine 并行使用。 您通常可以只使用 Context.LoadFontFace 函数而不是这个包级函数。
Types ¶
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
func NewContext ¶
NewContext creates a new image.RGBA with the specified width and height and prepares a context for rendering onto that image.
创建一个具有指定宽度和高度的新 image.RGBA 并准备渲染到该图像上的上下文。
func NewContextForImage ¶
NewContextForImage copies the specified image into a new image.RGBA and prepares a context for rendering onto that image.
将指定图像复制到一个新的 image.RGBA 并准备渲染到该图像上的上下文。
func NewContextForRGBA ¶
NewContextForRGBA prepares a context for rendering onto the specified image. No copy is made.
准备渲染到指定图像的上下文。没有复制。
func (*Context) AsMask ¶
AsMask returns an *image.Alpha representing the alpha channel of this context. This can be useful for advanced clipping operations where you first render the mask geometry and then use it as a mask.
返回一个 *image.Alpha 表示此上下文的 Alpha 通道。 这对于您首先渲染蒙版几何图形然后将其用作蒙版的高级裁剪操作很有用。
func (*Context) Brightness ¶ added in v1.1.1
Brightness 调整亮度 范围:±100%
func (*Context) Clear ¶
func (dc *Context) Clear()
Clear fills the entire image with the current color.
用当前颜色填充整个图像。
func (*Context) ClearPath ¶
func (dc *Context) ClearPath()
ClearPath clears the current path. There is no current point after this operation.
清除当前路径。 此操作后没有当前点。
func (*Context) Clip ¶
func (dc *Context) Clip()
Clip updates the clipping region by intersecting the current clipping region with the current path as it would be filled by dc.Fill(). The path is cleared after this operation.
通过与当前的相交来更新剪辑区域 使用当前路径剪切区域,因为它将由 dc.Fill() 填充。 此操作后路径被清除。
func (*Context) ClipPreserve ¶
func (dc *Context) ClipPreserve()
ClipPreserve updates the clipping region by intersecting the current clipping region with the current path as it would be filled by dc.Fill(). The path is preserved after this operation.
通过将当前剪辑区域与当前路径相交来更新剪辑区域,因为它将由 dc.Fill() 填充。 此操作后将保留路径。
func (*Context) ClosePath ¶
func (dc *Context) ClosePath()
ClosePath adds a line segment from the current point to the beginning of the current subpath. If there is no current point, this is a no-op.
添加从当前点到当前子路径开头的线段。 如果没有当前点,这是一个空操作。
func (*Context) CubicTo ¶
CubicTo adds a cubic bezier curve to the current path starting at the current point. If there is no current point, it first performs MoveTo(x1, y1). Because freetype/raster does not support cubic beziers, this is emulated with many small line segments.
向当前路径添加一条三次贝塞尔曲线,从 当前点。 如果没有当前点,则首先执行 MoveTo(x1, y1) 因为 freetype/raster 不支持三次贝塞尔曲线, 这是用许多小线段模拟的。
func (*Context) DrawEllipticalArc ¶
绘制椭圆弧
func (*Context) DrawImageAnchored ¶
DrawImageAnchored draws the specified image at the specified anchor point. The anchor point is x - w * ax, y - h * ay, where w, h is the size of the image. Use ax=0.5, ay=0.5 to center the image at the specified point.
在指定锚点处绘制指定图像。 锚点是x - w * ax, y - h * ay,其中w, h是图像的大小。 使用 ax=0.5, ay=0.5 使图像在指定点居中。
func (*Context) DrawPoint ¶
DrawPoint is like DrawCircle but ensures that a circle of the specified size is drawn regardless of the current transformation matrix. The position is still transformed, but not the shape of the point.
DrawPoint 与 DrawCircle 类似,但确保绘制指定大小的圆,而不管当前的变换矩阵如何。 位置仍然被改变,但不是点的形状。
func (*Context) DrawRectangle ¶
绘制一个矩形 以x, y 坐标为起点进行绘制
func (*Context) DrawRegularPolygon ¶
绘制正多边形
func (*Context) DrawRoundedRectangle ¶
绘制一个圆角矩形 以x, y 坐标为起点进行绘制
func (*Context) DrawString ¶
DrawString draws the specified text at the specified point.
在指定锚点绘制文本, x 为宽度的起点 y 为高度的终点
func (*Context) DrawStringAnchored ¶
DrawStringAnchored draws the specified text at the specified anchor point. The anchor point is x - w * ax, y - h * ay, where w, h is the size of the text. Use ax=0.5, ay=0.5 to center the text at the specified point.
在指定锚点处绘制指定文本。 锚点是 x - w * ax, y - h * ay,其中 w, h 是文本的大小。 使用 ax=0.5, ay=0.5 使文本在指定点居中。
func (*Context) DrawStringWrapped ¶
func (dc *Context) DrawStringWrapped(s string, x, y, ax, ay, width, lineSpacing float64, align Align)
DrawStringWrapped word-wraps the specified string to the given max width and then draws it at the specified anchor point using the given line spacing and text alignment.
将指定的字符串换行到给定的最大宽度, 然后使用给定的行距和文本对齐在指定的锚点处绘制它。
func (*Context) EncodeJPG ¶
EncodeJPG encodes the image as a JPG and writes it to the provided io.Writer in JPEG 4:2:0 baseline format with the given options. Default parameters are used if a nil *jpeg.Options is passed.
将图像编码为JPG,并将其写入提供的 io.Writer 在 JPEG 4:2:0 基线格式中,使用给定选项。 如果为nil,则使用默认参数 *jpeg.Options 进行传递
func (*Context) EncodePNG ¶
EncodePNG encodes the image as a PNG and writes it to the provided io.Writer.
将图像编码为 PNG 并将其写入提供的 io.Writer
func (*Context) Fill ¶
func (dc *Context) Fill()
Fill fills the current path with the current color. Open subpaths are implicity closed. The path is cleared after this operation.
用当前颜色填充当前路径。 打开的子路径是隐式关闭的。 此操作后路径被清除。
func (*Context) FillPreserve ¶
func (dc *Context) FillPreserve()
FillPreserve fills the current path with the current color. Open subpaths are implicity closed. The path is preserved after this operation.
用当前颜色填充当前路径。 打开的子路径是隐式关闭的。 此操作后将保留路径。
func (*Context) GetCurrentPoint ¶
GetCurrentPoint will return the current point and if there is a current point. The point will have been transformed by the context's transformation matrix.
返回当前点,如果存在当前点。 该点将通过上下文的变换矩阵进行变换。
func (*Context) Identity ¶
func (dc *Context) Identity()
Identity resets the current transformation matrix to the identity matrix. This results in no translating, scaling, rotating, or shearing.
将当前变换矩阵重置为单位矩阵。 这不会导致平移、缩放、旋转或剪切。
func (*Context) InvertMask ¶
func (dc *Context) InvertMask()
InvertMask inverts the alpha values in the current clipping mask such that a fully transparent region becomes fully opaque and vice versa.
反转当前剪贴蒙版中的 alpha 值,使完全透明的区域变得完全不透明,反之亦然。
func (*Context) InvertY ¶
func (dc *Context) InvertY()
InvertY flips the Y axis so that Y grows from bottom to top and Y=0 is at the bottom of the image.
反转Y轴,使Y从下到上增长,Y=0 位于图像的底部。
func (*Context) LineTo ¶
LineTo adds a line segment to the current path starting at the current point. If there is no current point, it is equivalent to MoveTo(x, y)
从当前点开始向当前路径添加一条线段。 如果没有当前点,则等价于 MoveTo(x, y)
func (*Context) MeasureMultilineString ¶
具有度量多行字符串的公式
func (*Context) MeasureString ¶
MeasureString returns the rendered width and height of the specified text given the current font face.
返回给定当前字体的指定文本的渲染宽度和高度。
func (*Context) MoveTo ¶
MoveTo starts a new subpath within the current path starting at the specified point.
在当前路径中从指定点开始新的子路径。
func (*Context) NewSubPath ¶
func (dc *Context) NewSubPath()
NewSubPath starts a new subpath within the current path. There is no current point after this operation.
在当前路径中开始一个新的子路径。 此操作后没有当前点。
func (*Context) Pop ¶
func (dc *Context) Pop()
Pop restores the last saved context state from the stack.
从堆栈中恢复上次保存的上下文状态。
func (*Context) Push ¶
func (dc *Context) Push()
Push saves the current state of the context for later retrieval. These can be nested.
保存上下文的当前状态以供以后检索。 这些可以嵌套。
func (*Context) QuadraticTo ¶
QuadraticTo adds a quadratic bezier curve to the current path starting at the current point. If there is no current point, it first performs MoveTo(x1, y1)
将二次贝塞尔曲线添加到从当前点开始的当前路径。 如果没有当前点,则首先执行 MoveTo(x1, y1)
func (*Context) ResetClip ¶
func (dc *Context) ResetClip()
ResetClip clears the clipping region.
清除剪裁区域。
func (*Context) Rotate ¶
Rotate updates the current matrix with a anticlockwise rotation. Rotation occurs about the origin. Angle is specified in radians.
逆时针旋转更新当前矩阵。围绕原点发生旋转。 角度以弧度指定。
func (*Context) RotateAbout ¶
RotateAbout updates the current matrix with a anticlockwise rotation. Rotation occurs about the specified point. Angle is specified in radians.
逆时针旋转更新当前矩阵。围绕指定点进行旋转。 角度以弧度指定。
func (*Context) SaveJPG ¶
SaveJPG encodes the image as a JPG and writes it to disk.
将图像编码为 JPG 并将其写入磁盘。
func (*Context) SavePNG ¶
SavePNG encodes the image as a PNG and writes it to disk.
将图像编码为 PNG 并将其写入磁盘。
func (*Context) Scale ¶
Scale updates the current matrix with a scaling factor. Scaling occurs about the origin.
使用缩放因子更新当前矩阵。缩放发生在原点附近。
func (*Context) ScaleAbout ¶
ScaleAbout updates the current matrix with a scaling factor. Scaling occurs about the specified point.
使用缩放因子更新当前矩阵。在指定点附近发生缩放。
func (*Context) SetColor ¶
SetColor sets the current color(for both fill and stroke).
设置当前颜色(用于填充和笔划)
func (*Context) SetDash ¶
SetDash sets the current dash pattern to use. Call with zero arguments to disable dashes. The values specify the lengths of each dash, with alternating on and off lengths.
设置要使用的当前破折号图案。使用零参数调用 禁用破折号。这些值指定每个破折号的长度,包括: 交替开启和关闭长度。
func (*Context) SetDashOffset ¶
SetDashOffset sets the initial offset into the dash pattern to use when stroking dashed paths.
将初始偏移量设置为虚线模式,以在描边虚线路径时使用。
func (*Context) SetHexColor ¶
SetHexColor sets the current color using a hex string. The leading pound sign (#) is optional. Both 3- and 6-digit variations are supported. 8 digits may be provided to set the alpha value as well.
使用十六进制字符串设置当前颜色。前置的 符号(#)是可选的。支持3位数和6位数的变体。8位数字 也可以提供设置 alpha 值。
func (*Context) SetMask ¶
SetMask allows you to directly set the *image.Alpha to be used as a clipping mask. It must be the same size as the context, else an error is returned and the mask is unchanged.
允许您直接设置 *image.Alpha 用作剪贴蒙版。 它必须与上下文大小相同,否则返回错误并且掩码不变。
func (*Context) SetPixel ¶
SetPixel sets the color of the specified pixel using the current color.
使用当前颜色设置指定像素的颜色。
func (*Context) SetRGB ¶
SetRGB sets the current color. r, g, b values should be between 0 and 1, inclusive. Alpha will be set to 1 (fully opaque).
设置当前颜色。 r、g、b 值应介于 0 和 1 之间, 包括。 Alpha 将设置为 1(完全不透明)。
func (*Context) SetRGB255 ¶
SetRGB255 sets the current color. r, g, b values should be between 0 and 255, inclusive. Alpha will be set to 255 (fully opaque).
设置当前颜色。 r、g、b 值应介于 0 和 255 之间,包括。 Alpha 将设置为 255(完全不透明)。
func (*Context) SetRGBA ¶
SetRGBA sets the current color. r, g, b, a values should be between 0 and 1, inclusive.
SetRGBA 设置当前颜色。 r、g、b、a 值应介于 0 和 1 之间, 包括。
func (*Context) SetRGBA255 ¶
SetRGBA255 sets the current color. r, g, b, a values should be between 0 and 255, inclusive.
设置当前颜色。r、g、b、a 值应介于 0 和 255,包括在内。
func (*Context) Shear ¶
Shear updates the current matrix with a shearing angle. Shearing occurs about the origin.
用剪切角更新当前矩阵。剪切发生在原点附近。
func (*Context) ShearAbout ¶
ShearAbout updates the current matrix with a shearing angle. Shearing occurs about the specified point.
用剪切角更新当前矩阵。剪切发生在指定点附近。
func (*Context) Stroke ¶
func (dc *Context) Stroke()
Stroke strokes the current path with the current color, line width, line cap, line join and dash settings. The path is cleared after this operation.
使用当前颜色、线宽、线帽、线连接和虚线设置描边当前路径。 此操作后路径被清除。
func (*Context) StrokePreserve ¶
func (dc *Context) StrokePreserve()
StrokePreserve strokes the current path with the current color, line width, line cap, line join and dash settings. The path is preserved after this operation.
使用当前颜色、线宽、线帽、线连接和虚线设置描边当前路径。 此操作后将保留路径。
func (*Context) TransformPoint ¶
TransformPoint multiplies the specified point by the current matrix, returning a transformed position.
将指定点乘以当前矩阵,返回转换后的位置。
type Matrix ¶
type Matrix struct {
XX, YX, XY, YY, X0, Y0 float64
}
func (Matrix) TransformPoint ¶
func (Matrix) TransformVector ¶
type Pattern ¶
func NewSolidPattern ¶
type ScaleStyle ¶
type ScaleStyle int
ScaleStyle determines the way image pixels are interpolated when scaled. See
https://pkg.go.dev/golang.org/x/image/draw
for the corresponding interpolators.
确定缩放时图像像素的插值方式。 请看 https://pkg.go.dev/golang.org/x/image/draw 对应的插值器。
const ( // BiLinear is the tent kernel. It is slow, but usually gives high quality // results. // // BiLinear 是帐篷内核。 它很慢,但通常会产生高质量的结果。 BiLinear ScaleStyle = iota // ApproxBiLinear is a mixture of the nearest neighbor and bi-linear // interpolators. It is fast, but usually gives medium quality results. // // It implements bi-linear interpolation when upscaling and a bi-linear // blend of the 4 nearest neighbor pixels when downscaling. This yields // nicer quality than nearest neighbor interpolation when upscaling, but // the time taken is independent of the number of source pixels, unlike the // bi-linear interpolator. When downscaling a large image, the performance // difference can be significant. // // ApproxBiLinear 是最近邻和双线性插值器的混合。 它速度很快,但通常会给出中等质量的结果。 // 它在放大时实现双线性插值,在缩小时实现 4 个最近邻像素的双线性混合。 // 这在放大时产生比最近邻插值更好的质量,但与双线性插值器不同,所花费的时间与源像素的数量无关。 // 缩小大图像时,性能差异可能很大。 ApproxBiLinear // NearestNeighbor is the nearest neighbor interpolator. It is very fast, // but usually gives very low quality results. When scaling up, the result // will look 'blocky'. // // NearestNeighbor 是最近邻插值器。 它非常快,但通常会给出非常低质量的结果。 // 放大时,结果将看起来“块状”。 NearestNeighbor // CatmullRom is the Catmull-Rom kernel. It is very slow, but usually gives // very high quality results. // // It is an instance of the more general cubic BC-spline kernel with parameters // B=0 and C=0.5. See Mitchell and Netravali, "Reconstruction Filters in // Computer Graphics", Computer Graphics, Vol. 22, No. 4, pp. 221-228. // // CatmullRom 是 Catmull-Rom 内核。 它很慢,但通常会给出非常高质量的结果。 // 它是参数 B=0 和 C=0.5 的更一般的三次 BC 样条核的实例。 // 参见 Mitchell 和 Netravali,“计算机图形学中的重构过滤器”, // 计算机图形学,卷.22,第 4 期,第 221-228 页。 CatmullRom )