Documentation ¶
Overview ¶
Package canvas provides an API that tries to closely mirror that of the HTML5 canvas API, using OpenGL to do the rendering.
Index ¶
- Constants
- func LoadGL(glimpl GL) (err error)
- type Canvas
- func (cv *Canvas) Activate()
- func (cv *Canvas) Arc(x, y, radius, startAngle, endAngle float64, anticlockwise bool)
- func (cv *Canvas) ArcTo(x1, y1, x2, y2, radius float64)
- func (cv *Canvas) BeginPath()
- func (cv *Canvas) BezierCurveTo(x1, y1, x2, y2, x3, y3 float64)
- func (cv *Canvas) ClearRect(x, y, w, h float64)
- func (cv *Canvas) Clip()
- func (cv *Canvas) ClosePath()
- func (cv *Canvas) DrawImage(image interface{}, coords ...float64)
- func (cv *Canvas) Fill()
- func (cv *Canvas) FillRect(x, y, w, h float64)
- func (cv *Canvas) FillText(str string, x, y float64)
- func (cv *Canvas) GetImageData(x, y, w, h int) *image.RGBA
- func (cv *Canvas) GetLineDash() []float64
- func (cv *Canvas) Height() int
- func (cv *Canvas) LineTo(x, y float64)
- func (cv *Canvas) MeasureText(str string) TextMetrics
- func (cv *Canvas) MoveTo(x, y float64)
- func (cv *Canvas) PutImageData(img *image.RGBA, x, y int)
- func (cv *Canvas) QuadraticCurveTo(x1, y1, x2, y2 float64)
- func (cv *Canvas) Rect(x, y, w, h float64)
- func (cv *Canvas) Restore()
- func (cv *Canvas) Rotate(angle float64)
- func (cv *Canvas) Save()
- func (cv *Canvas) Scale(x, y float64)
- func (cv *Canvas) SetBounds(x, y, w, h int)
- func (cv *Canvas) SetFillStyle(value ...interface{})
- func (cv *Canvas) SetFont(src interface{}, size float64)
- func (cv *Canvas) SetGlobalAlpha(alpha float64)
- func (cv *Canvas) SetLineDash(dash []float64)
- func (cv *Canvas) SetLineDashOffset(offset float64)
- func (cv *Canvas) SetLineEnd(end lineEnd)
- func (cv *Canvas) SetLineJoin(join lineJoin)
- func (cv *Canvas) SetLineWidth(width float64)
- func (cv *Canvas) SetMiterLimit(limit float64)
- func (cv *Canvas) SetShadowBlur(r float64)
- func (cv *Canvas) SetShadowColor(color ...interface{})
- func (cv *Canvas) SetShadowOffset(x, y float64)
- func (cv *Canvas) SetShadowOffsetX(offset float64)
- func (cv *Canvas) SetShadowOffsetY(offset float64)
- func (cv *Canvas) SetSize(w, h int)deprecated
- func (cv *Canvas) SetStrokeStyle(value ...interface{})
- func (cv *Canvas) SetTextAlign(align textAlign)
- func (cv *Canvas) SetTransform(a, b, c, d, e, f float64)
- func (cv *Canvas) Size() (int, int)
- func (cv *Canvas) Stroke()
- func (cv *Canvas) StrokeRect(x, y, w, h float64)
- func (cv *Canvas) StrokeText(str string, x, y float64)
- func (cv *Canvas) Transform(a, b, c, d, e, f float64)
- func (cv *Canvas) Translate(x, y float64)
- func (cv *Canvas) Width() int
- type Font
- type GL
- type Image
- type LinearGradient
- type RadialGradient
- type TextMetrics
Constants ¶
const ( Miter = iota Bevel Round Square Butt )
Line join and end constants for SetLineJoin and SetLineEnd
const ( Left = iota Center Right Start End )
Text alignment constants for SetTextAlign
Variables ¶
This section is empty.
Functions ¶
func LoadGL ¶
LoadGL needs to be called once per GL context to load the GL assets that canvas needs. The parameter is an implementation of the GL interface in this package that should make this package neutral to GL implementations. The goglimpl subpackage contains an implementation based on Go-GL v3.2
Types ¶
type Canvas ¶
type Canvas struct {
// contains filtered or unexported fields
}
Canvas represents an area on the viewport on which to draw using a set of functions very similar to the HTML5 canvas
func New ¶
New creates a new canvas with the given viewport coordinates. While all functions on the canvas use the top left point as the origin, since GL uses the bottom left coordinate, the coordinates given here also use the bottom left as origin
func NewOffscreen ¶
NewOffscreen creates a new canvas with the given size. It does not render directly to the screen but renders to a texture instead
func (*Canvas) Activate ¶
func (cv *Canvas) Activate()
Activate makes the canvas active and sets the viewport. Only needs to be called if any other GL code changes the viewport
func (*Canvas) Arc ¶
Arc adds a circle segment to the end of the path. x/y is the center, radius is the radius, startAngle and endAngle are angles in radians, anticlockwise means that the line is added anticlockwise
func (*Canvas) ArcTo ¶
ArcTo adds to the current path by drawing a line toward x1/y1 and a circle segment of a radius given by the radius parameter. The circle touches the lines from the end of the path to x1/y1, and from x1/y1 to x2/y2. The line will only go to where the circle segment would touch the latter line
func (*Canvas) BeginPath ¶
func (cv *Canvas) BeginPath()
BeginPath clears the current path and starts a new one
func (*Canvas) BezierCurveTo ¶
BezierCurveTo adds a bezier curve to the path. It uses the current end point of the path, x1/y1 and x2/y2 define the curve, and x3/y3 is the end point
func (*Canvas) Clip ¶
func (cv *Canvas) Clip()
Clip uses the current path to clip any further drawing. Use Save/Restore to remove the clipping again
func (*Canvas) ClosePath ¶
func (cv *Canvas) ClosePath()
ClosePath closes the path to the beginning of the path or the last point from a MoveTo call
func (*Canvas) DrawImage ¶
DrawImage draws the given image to the given coordinates. The image parameter can be an Image loaded by LoadImage, a file name string that will be loaded and cached, or a name string that corresponds to a previously loaded image with the same name parameter.
The coordinates must be one of the following:
DrawImage("image", dx, dy) DrawImage("image", dx, dy, dw, dh) DrawImage("image", sx, sy, sw, sh, dx, dy, dw, dh)
Where dx/dy/dw/dh are the destination coordinates and sx/sy/sw/sh are the source coordinates
func (*Canvas) FillText ¶
FillText draws the given string at the given coordinates using the currently set font and font height
func (*Canvas) GetImageData ¶
GetImageData returns an RGBA image of the currently displayed image. The alpha channel is always opaque
func (*Canvas) GetLineDash ¶
GetLineDash gets the line dash style
func (*Canvas) MeasureText ¶
func (cv *Canvas) MeasureText(str string) TextMetrics
MeasureText measures the given string using the current font and font height
func (*Canvas) PutImageData ¶
PutImageData puts the given image at the given x/y coordinates
func (*Canvas) QuadraticCurveTo ¶
QuadraticCurveTo adds a quadratic curve to the path. It uses the current end point of the path, x1/y1 defines the curve, and x2/y2 is the end point
func (*Canvas) Restore ¶
func (cv *Canvas) Restore()
Restore restores the last draw state from the stack if available
func (*Canvas) Rotate ¶
Rotate updates the current transformation with a rotation by the given angle
func (*Canvas) SetBounds ¶
SetBounds updates the bounds of the canvas. This would usually be called for example when the window is resized
func (*Canvas) SetFillStyle ¶
func (cv *Canvas) SetFillStyle(value ...interface{})
SetFillStyle sets the color, gradient, or image for any fill calls. To set a color, there are several acceptable formats: 3 or 4 int values for RGB(A) in the range 0-255, 3 or 4 float values for RGB(A) in the range 0-1, hex strings in the format "#AABBCC", "#AABBCCDD", "#ABC", or "#ABCD"
func (*Canvas) SetFont ¶
SetFont sets the font and font size. The font parameter can be a font loaded with the LoadFont function, a filename for a font to load (which will be cached), or nil, in which case the first loaded font will be used
func (*Canvas) SetGlobalAlpha ¶
SetGlobalAlpha sets the global alpha value
func (*Canvas) SetLineDash ¶
SetLineDash sets the line dash style
func (*Canvas) SetLineDashOffset ¶
func (*Canvas) SetLineEnd ¶
func (cv *Canvas) SetLineEnd(end lineEnd)
SetLineEnd sets the style of line endings for rendering a path with Stroke The value can be Butt, Square, or Round
func (*Canvas) SetLineJoin ¶
func (cv *Canvas) SetLineJoin(join lineJoin)
SetLineJoin sets the style of line joints for rendering a path with Stroke. The value can be Miter, Bevel, or Round
func (*Canvas) SetLineWidth ¶
SetLineWidth sets the line width for any line drawing calls
func (*Canvas) SetMiterLimit ¶
SetMiterLimit sets the limit for how far a miter line join can be extend. The fallback is a bevel join
func (*Canvas) SetShadowBlur ¶
SetShadowBlur sets the gaussian blur radius of the shadow (0 for no blur)
func (*Canvas) SetShadowColor ¶
func (cv *Canvas) SetShadowColor(color ...interface{})
SetShadowColor sets the color of the shadow. If it is fully transparent (default) then no shadow is drawn
func (*Canvas) SetShadowOffset ¶
SetShadowOffset sets the offset of the shadow
func (*Canvas) SetShadowOffsetX ¶
SetShadowOffsetX sets the x offset of the shadow
func (*Canvas) SetShadowOffsetY ¶
SetShadowOffsetY sets the y offset of the shadow
func (*Canvas) SetStrokeStyle ¶
func (cv *Canvas) SetStrokeStyle(value ...interface{})
SetStrokeStyle sets the color, gradient, or image for any line drawing calls. To set a color, there are several acceptable formats: 3 or 4 int values for RGB(A) in the range 0-255, 3 or 4 float values for RGB(A) in the range 0-1, hex strings in the format "#AABBCC", "#AABBCCDD", "#ABC", or "#ABCD"
func (*Canvas) SetTextAlign ¶
func (cv *Canvas) SetTextAlign(align textAlign)
SetTextAlign sets the text align for any text drawing calls. The value can be Left, Center, Right, Start, or End
func (*Canvas) SetTransform ¶
SetTransform replaces the current transformation with the given matrix
func (*Canvas) Stroke ¶
func (cv *Canvas) Stroke()
Stroke uses the current StrokeStyle to draw the path
func (*Canvas) StrokeRect ¶
StrokeRect draws a rectangle using the current stroke style
func (*Canvas) StrokeText ¶
StrokeText draws the given string at the given coordinates using the currently set font and font height and using the current stroke style
type Font ¶
type Font struct {
// contains filtered or unexported fields
}
Font is a loaded font that can be passed to the SetFont method
type GL ¶
type GL interface { Ptr(data interface{}) unsafe.Pointer ActiveTexture(texture uint32) AttachShader(program uint32, shader uint32) BindBuffer(target uint32, buffer uint32) BindFramebuffer(target uint32, framebuffer uint32) BindRenderbuffer(target uint32, renderbuffer uint32) BindTexture(target uint32, texture uint32) BlendFunc(sfactor uint32, dfactor uint32) BufferData(target uint32, size int, data unsafe.Pointer, usage uint32) CheckFramebufferStatus(target uint32) uint32 Clear(mask uint32) ClearColor(red float32, green float32, blue float32, alpha float32) ColorMask(red bool, green bool, blue bool, alpha bool) CompileShader(shader uint32) CreateProgram() uint32 CreateShader(xtype uint32) uint32 DeleteShader(shader uint32) DeleteFramebuffers(n int32, framebuffers *uint32) DeleteRenderbuffers(n int32, renderbuffers *uint32) DeleteTextures(n int32, textures *uint32) Disable(cap uint32) DisableVertexAttribArray(index uint32) DrawArrays(mode uint32, first int32, count int32) Enable(cap uint32) EnableVertexAttribArray(index uint32) FramebufferRenderbuffer(target uint32, attachment uint32, renderbuffertarget uint32, renderbuffer uint32) FramebufferTexture(target uint32, attachment uint32, texture uint32, level int32) GenBuffers(n int32, buffers *uint32) GenFramebuffers(n int32, framebuffers *uint32) GenRenderbuffers(n int32, renderbuffers *uint32) GenTextures(n int32, textures *uint32) GenerateMipmap(target uint32) GetAttribLocation(program uint32, name string) int32 GetError() uint32 GetProgramInfoLog(program uint32) string GetProgramiv(program uint32, pname uint32, params *int32) GetShaderInfoLog(shader uint32) string GetShaderiv(shader uint32, pname uint32, params *int32) GetUniformLocation(program uint32, name string) int32 LinkProgram(program uint32) ReadPixels(x int32, y int32, width int32, height int32, format uint32, xtype uint32, pixels unsafe.Pointer) RenderbufferStorage(target uint32, internalformat uint32, width int32, height int32) Scissor(x int32, y int32, width int32, height int32) ShaderSource(shader uint32, source string) StencilFunc(xfunc uint32, ref int32, mask uint32) StencilMask(mask uint32) StencilOp(fail uint32, zfail uint32, zpass uint32) TexImage2D(target uint32, level int32, internalformat int32, width int32, height int32, border int32, format uint32, xtype uint32, pixels unsafe.Pointer) TexParameteri(target uint32, pname uint32, param int32) TexSubImage2D(target uint32, level int32, xoffset int32, yoffset int32, width int32, height int32, format uint32, xtype uint32, pixels unsafe.Pointer) Uniform1f(location int32, v0 float32) Uniform1fv(location int32, count int32, v *float32) Uniform1i(location int32, v0 int32) Uniform2f(location int32, v0 float32, v1 float32) Uniform4f(location int32, v0 float32, v1 float32, v2 float32, v3 float32) UniformMatrix3fv(location int32, count int32, transpose bool, value *float32) UseProgram(program uint32) VertexAttribPointer(index uint32, size int32, xtype uint32, normalized bool, stride int32, offset uint32) Viewport(x int32, y int32, width int32, height int32) }
The GL interface is used to make this package independent of any particular OpenGL implementation. The goglimpl subpackage conatins an implementation of this interface based on Go-GL v3.2
type Image ¶
type Image struct {
// contains filtered or unexported fields
}
Image represents a loaded image that can be used in various drawing functions
func LoadImage ¶
LoadImage loads an image. The src parameter can be either an image from the standard image package, a byte slice that will be loaded, or a file name string. If you want the canvas package to load the image, make sure you import the required format packages
func (*Image) Delete ¶
func (img *Image) Delete()
Delete deletes the image from memory. Any draw calls with a deleted image will not do anything
func (*Image) Replace ¶
func (img *Image) Replace(src interface{})
Replace replaces the image with the new one
type LinearGradient ¶
type LinearGradient struct {
// contains filtered or unexported fields
}
LinearGradient is a gradient with any number of stops and any number of colors. The gradient will be drawn such that each point on the gradient will correspond to a straight line
func NewLinearGradient ¶
func NewLinearGradient(x0, y0, x1, y1 float64) *LinearGradient
NewLinearGradient creates a new linear gradient with the coordinates from where to where the gradient will apply on the canvas
func (*LinearGradient) AddColorStop ¶
func (g *LinearGradient) AddColorStop(pos float64, color ...interface{})
AddColorStop adds a color stop to the gradient. The stops don't have to be added in order, they are sorted into the right place
type RadialGradient ¶
type RadialGradient struct {
// contains filtered or unexported fields
}
RadialGradient is a gradient with any number of stops and any number of colors. The gradient will be drawn such that each point on the gradient will correspond to a circle
func NewRadialGradient ¶
func NewRadialGradient(x0, y0, r0, x1, y1, r1 float64) *RadialGradient
NewRadialGradient creates a new linear gradient with the coordinates and the radii for two circles. The gradient will apply from the first to the second circle
func (*RadialGradient) AddColorStop ¶
func (g *RadialGradient) AddColorStop(pos float64, color ...interface{})
AddColorStop adds a color stop to the gradient. The stops don't have to be added in order, they are sorted into the right place
type TextMetrics ¶
type TextMetrics struct { Width float64 ActualBoundingBoxAscent float64 ActualBoundingBoxDescent float64 }
TextMetrics is the result of a MeasureText call