Canvas

Table of contents

Drawing interface of CanvasView

Interface description

ClearRect(x, y, width, height float64)

Erases the pixels in a rectangular area by setting them to transparent black

ClipPath(path Path)

Turns the path into the current clipping region. It replaces any previous clipping region

ClipRect(x, y, width, height float64)

Turns the rectangle into the current clipping region. It replaces any previous clipping region

DrawImage(x, y float64, image Image)

Draws the image at the (x, y) position

DrawImageFragment(srcX, srcY, srcWidth, srcHeight, dstX, dstY, dstWidth, dstHeight float64, image Image)

Draws the fragment (described by srcX, srcY, srcWidth, srcHeight) of image in the rectangle (dstX, dstY, dstWidth, dstHeight), scaling in height and width if necessary

DrawImageInRect(x, y, width, height float64, image Image)

Draws the image in the rectangle (x, y, width, height), scaling in height and width if necessary

DrawLine(x0, y0, x1, y1 float64)

Draws a line according to the current strokeStyle and other context settings

FillAndStrokeEllipse(x, y, radiusX, radiusY, rotation float64)

Draws a ellipse that is filled according to the current FillStyle and is stroked (outlined) according to the current strokeStyle and other context settings

FillAndStrokePath(path Path)

Draws a path that is filled according to the current FillStyle and is stroked (outlined) according to the current strokeStyle and other context settings

FillAndStrokeRect(x, y, width, height float64)

Draws a rectangle that is filled according to the current FillStyle and is stroked (outlined) according to the current strokeStyle and other context settings

FillAndStrokeRoundedRect(x, y, width, height, r float64)

Draws a rounded rectangle that is filled according to the current FillStyle and is stroked (outlined) according to the current strokeStyle and other context settings

FillEllipse(x, y, radiusX, radiusY, rotation float64)

Draws a ellipse that is filled according to the current FillStyle. x, y - coordinates of the ellipse's center; radiusX - the ellipse's major-axis radius. Must be non-negative; radiusY - the ellipse's minor-axis radius. Must be non-negative; rotation - the rotation of the ellipse, expressed in radians.

FillPath(path Path)

Draws a path that is filled according to the current FillStyle.

FillRect(x, y, width, height float64)

Draws a rectangle that is filled according to the current FillStyle.

FillRoundedRect(x, y, width, height, r float64)

Draws a rounded rectangle that is filled according to the current FillStyle.

FillText(x, y float64, text string)

Draws a text string at the specified coordinates, filling the string's characters with the current FillStyle

Height() float64

Returns the height in pixels of the canvas area

NewPath() Path

Creates a new Path object

NewPathFromSvg(data string) Path

Creates a new Path and initialize it by a string consisting of SVG path data

ResetShadow()

Sets shadow parameters to default values (invisible shadow)

ResetTransformation()

Resets the current transform to the identity matrix

Restore()

Restores the most recently saved canvas state by popping the top entry in the drawing state stack. If there is no saved state, this method does nothing

Save()

Saves the entire state of the canvas by pushing the current state onto a stack

SetFont(name string, size SizeUnit)

Sets the current text style to use when drawing text

SetFontWithParams(name string, size SizeUnit, params FontParams)

Sets the current text style to use when drawing text

SetImageFillStyle(image Image, repeat int)

Set the image as the filling pattern. repeat - indicating how to repeat the pattern's image. Possible values are: NoRepeat (0) - neither direction, RepeatXY (1) - both directions, RepeatX (2) - horizontal only, RepeatY (3) - vertical only

SetLineCap(cap int)

Sets the shape used to draw the end points of lines. Possible values are: ButtCap (0), RoundCap (1), SquareCap (2). All other values are ignored

SetLineDash(dash []float64, offset float64)

Sets the line dash pattern used when stroking lines. dash - an array of values that specify alternating lengths of lines and gaps which describe the pattern. offset - the line dash offset

SetLineJoin(join int)

Sets the shape used to join two line segments where they meet. Possible values are: MiterJoin (0), RoundJoin (1), BevelJoin (2). All other values are ignored

SetLineWidth(width float64)

Sets the line width, in coordinate space units. Zero, negative, Infinity, and NaN values are ignored.

SetLinearGradientFillStyle(x0, y0 float64, color0 Color, x1, y1 float64, color1 Color, stopPoints []GradientPoint)

Sets a gradient along the line connecting two given coordinates to use inside shapes x0, y0 - coordinates of the start point; x1, y1 - coordinates of the end point; startColor, endColor - the start and end color stopPoints - the array of stop points

SetLinearGradientStrokeStyle(x0, y0 float64, color0 Color, x1, y1 float64, color1 Color, stopPoints []GradientPoint)

Sets a gradient along the line connecting two given coordinates to use for the strokes (outlines) around shapes x0, y0 - coordinates of the start point; x1, y1 - coordinates of the end point; color0, color1 - the start and end color stopPoints - the array of stop points

SetRadialGradientFillStyle(x0, y0, r0 float64, color0 Color, x1, y1, r1 float64, color1 Color, stopPoints []GradientPoint)

Sets a radial gradient using the size and coordinates of two circles to use inside shapes x0, y0 - coordinates of the center of the start circle; r0 - the radius of the start circle; x1, y1 - coordinates the center of the end circle; r1 - the radius of the end circle; color0, color1 - the start and end color stopPoints - the array of stop points

SetRadialGradientStrokeStyle(x0, y0, r0 float64, color0 Color, x1, y1, r1 float64, color1 Color, stopPoints []GradientPoint)

Sets a radial gradient using the size and coordinates of two circles to use for the strokes (outlines) around shapes x0, y0 - coordinates of the center of the start circle; r0 - the radius of the start circle; x1, y1 - coordinates the center of the end circle; r1 - the radius of the end circle; color0, color1 - the start and end color stopPoints - the array of stop points

SetRotation(angle float64)

Adds a rotation to the transformation matrix. angle - the rotation angle, clockwise in radians

SetScale(x, y float64)

Adds a scaling transformation to the canvas units horizontally and/or vertically. x - scaling factor in the horizontal direction. A negative value flips pixels across the vertical axis. A value of 1 results in no horizontal scaling; y - scaling factor in the vertical direction. A negative value flips pixels across the horizontal axis. A value of 1 results in no vertical scaling

SetShadow(offsetX, offsetY, blur float64, color Color)

Sets shadow parameters: offsetX, offsetY - the distance that shadows will be offset horizontally and vertically; blur - the amount of blur applied to shadows. Must be non-negative; color - the color of shadows

SetSolidColorFillStyle(color Color)

Sets the color to use inside shapes

SetSolidColorStrokeStyle(color Color)

Sets color to use for the strokes (outlines) around shapes

SetTextAlign(align int)

Sets the current text alignment used when drawing text. Possible values are: LeftAlign (0), RightAlign (1), CenterAlign (2), StartAlign (3) and EndAlign (4). All other values are ignored

SetTextBaseline(baseline int)

Sets the current text baseline used when drawing text. Possible values are: AlphabeticBaseline (0), TopBaseline (1), MiddleBaseline (2), BottomBaseline (3), HangingBaseline (4) and IdeographicBaseline (5). All other values are ignored

SetTransformation(xScale, yScale, xSkew, ySkew, dx, dy float64)

Multiplies the current transformation with the matrix described by the arguments of this method. This allow to scale, rotate, translate (move), and skew the context. The transformation matrix is described by: ⎡ xScale xSkew dx ⎤ ⎢ ySkew yScale dy ⎥ ⎣ 0 0 1 ⎦ xScale, yScale - horizontal and vertical scaling. A value of 1 results in no scaling; xSkew, ySkew - horizontal and vertical skewing; dx, dy - horizontal and vertical translation (moving)

SetTranslation(x, y float64)

Adds a translation transformation to the current matrix. x - distance to move in the horizontal direction. Positive values are to the right, and negative to the left; y - distance to move in the vertical direction. Positive values are down, and negative are up.

StrokeEllipse(x, y, radiusX, radiusY, rotation float64)

Draws a ellipse that is stroked (outlined) according to the current strokeStyle and other context settings

StrokePath(path Path)

Draws a path that is stroked (outlined) according to the current strokeStyle and other context settings

StrokeRect(x, y, width, height float64)

Draws a rectangle that is stroked (outlined) according to the current strokeStyle and other context settings

StrokeRoundedRect(x, y, width, height, r float64)

Draws a rounded rectangle that is stroked (outlined) according to the current strokeStyle and other context settings

StrokeText(x, y float64, text string)

Draws the outlines of the characters of a string at the specified coordinates

TextMetrics(text string, fontName string, fontSize SizeUnit, fontParams FontParams) TextMetrics

Calculates metrics of the text drawn by a given font

View() CanvasView

Return the view for the drawing

Width() float64

Returns the width in pixels of the canvas area