Try Install Learn Blog API Packages GitHub
Pages
canvas

Search
Basic information
Entities
Versions

Canvas

Functions

arc
(
el
:
Dom.Canvas
x
:
Number
y
:
Number
radius
:
Number
startAngle
:
Number
endAngle
:
Number
counterclockwise
:
Bool
)
:
Dom.Canvas

Creates a circular arc centered at (x, y) with a radius of radius.

The path starts at startAngle, ends at endAngle, and travels in the direction given by counterclockwise.

arcTo
(
el
:
Dom.Canvas
x1
:
Number
y1
:
Number
x2
:
Number
y2
:
Number
radius
:
Number
)
:
Dom.Canvas

Adds a circular arc to the current sub-path, using the given control points and radius.

The arc is automatically connected to the path's latest point with a straight line, if necessary for the specified parameters.

This method is commonly used for making rounded corners.

beginPath
(
el
:
Dom.Canvas
)

Starts a new path by emptying the list of sub-paths. Call this method when you want to create a new path.

bezierCurveTo
(
el
:
Dom.Canvas
cp1x
:
Number
cp1y
:
Number
cp2x
:
Number
cp2y
:
Number
x
:
Number
y
:
Number
)
:
Dom.Canvas

Adds a cubic Bézier curve to the current sub-path.

It requires three points: the first two are control points and the third one is the end point.

The starting point is the latest point in the current path, which can be changed using moveTo() before creating the Bézier curve.

clear
(
el
:
Dom.Canvas
)
:
Dom.Canvas

Clears the entire canvas.

Same as calling clearRect(0, 0, width, height)

clearRect
(
el
:
Dom.Canvas
x
:
Number
y
:
Number
width
:
Number
height
:
Number
)
:
Dom.Canvas

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

clip
(
el
:
Dom.Canvas
fillRule
:
CanvasFillRule
)
:
Dom.Canvas

Turns the current path into the current clipping region.

The previous clipping region, if any, is intersected with the current or given path to create the new clipping region.

clipPath
(
el
:
Dom.Canvas
path
:
Path2D
fillRule
:
CanvasFillRule
)
:
Dom.Canvas

Turns the given path into the current clipping region.

The previous clipping region, if any, is intersected with the current or given path to create the new clipping region.

closePath
(
el
:
Dom.Canvas
)
:
Dom.Canvas

Attempts to add a straight line from the current point to the start of the current sub-path.

If the shape has already been closed or has only one point, this function does nothing.

This method doesn't draw anything to the canvas directly. You can render the path using the stroke() or fill() methods.

createConicGradient
(
el
:
Dom.Canvas
startAngle
:
Number
x
:
Number
y
:
Number
)
:
CanvasGradient

Creates a gradient around a point with given coordinates.

This method returns a conic CanvasGradient. To be applied to a shape, the gradient must first be assigned to the fillStyle or strokeStyle properties.

createImageData
(
el
:
Dom.Canvas
width
:
Number
height
:
Number
colorSpace
:
String
)
:
ImageData

Creates a new, blank ImageData object with the specified dimensions. All of the pixels in the new object are transparent black.

createLinearGradient
(
el
:
Dom.Canvas
x0
:
Number
y0
:
Number
x1
:
Number
y1
:
Number
)
:
CanvasGradient

Creates a gradient along the line connecting two given coordinates.

The gradient transitions colors along the gradient line, starting at point x0, y0 and going to x1, y1, even if those points extend the gradient line beyond the edges of the element on which the gradient is drawn.

This method returns a CanvasGradient. To be applied to a shape, the gradient must first be assigned to the fillStyle or strokeStyle properties.

createPattern
(
el
:
Dom.Canvas
image
:
CanvasImageSource
repetition
:
CanvasPatternRepeat
)
:
Maybe(CanvasPattern)

Creates a pattern using the specified image and repetition. This method returns a CanvasPattern.

This method doesn't draw anything to the canvas directly. The pattern it creates must be assigned to the fillStyle or strokeStyle properties, after which it is applied to any subsequent drawing.

createRadialGradient
(
el
:
Dom.Canvas
x0
:
Number
y0
:
Number
r0
:
Number
x1
:
Number
y1
:
Number
r1
:
Number
)
:
CanvasGradient

Creates a radial gradient using the size and coordinates of two circles.

This method returns a CanvasGradient. To be applied to a shape, the gradient must first be assigned to the fillStyle or strokeStyle properties.

directionFromStr
(
direction
:
String
)
:
CanvasDirection
directionToStr
(
direction
:
CanvasDirection
)
:
String
drawFocusIfNeeded
(
el
:
Dom.Canvas
targetEl
:
Dom.Element
path
:
Maybe(Path2D)
)
:
Dom.Canvas

Draws a CanvasImageSource to the canvas

drawImage
(
el
:
Dom.Canvas
image
:
CanvasImageSource
x
:
Number
y
:
Number
width
:
Number
height
:
Number
dx
:
Number
dy
:
Number
dWidth
:
Number
dHeight
:
Number
)
:
Dom.Canvas

Draws a CanvasImageSource to the canvas

ellipse
(
el
:
Dom.Canvas
x
:
Number
y
:
Number
radiusX
:
Number
radiusY
:
Number
rotation
:
Number
startAngle
:
Number
endAngle
:
Number
counterclockwise
:
Bool
)
:
Dom.Canvas

Adds a circular arc to the current sub-path, using the given control points and radius.

The arc is automatically connected to the path's latest point with a straight line, if necessary for the specified parameters.

This method is commonly used for making rounded corners.

fill
(
el
:
Dom.Canvas
fillRule
:
CanvasFillRule
)
:
Dom.Canvas

Fills the current path with the current or given fillRule.

fillPath
(
el
:
Dom.Canvas
path
:
Path2D
fillRule
:
CanvasFillRule
)
:
Dom.Canvas

Fills given path with the current or given fillRule.

fillRect
(
el
:
Dom.Canvas
x
:
Number
y
:
Number
width
:
Number
height
:
Number
)
:
Dom.Canvas

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

This method draws directly to the canvas without modifying the current path, so any subsequent fill() or stroke() calls will have no effect on it.

fillRuleFromStr
(
fillRule
:
String
)
:
CanvasFillRule
fillRuleToStr
(
fillRule
:
CanvasFillRule
)
:
String
fillText
(
el
:
Dom.Canvas
text
:
String
x
:
Number
y
:
Number
maxWidth
:
Number
)
:
Dom.Canvas

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

An optional parameter allows specifying a maximum width for the rendered text, which the user agent will achieve by condensing the text or by using a lower font size.

This method draws directly to the canvas without modifying the current path, so any subsequent fill() or stroke() calls will have no effect on it.

The text is rendered using the font and text layout configuration as defined by the font, textAlign, textBaseline, and direction properties.

fontKerningFromStr
(
fontKerning
:
String
)
:
CanvasFontKerning
fontKerningToStr
(
fontKerning
:
CanvasFontKerning
)
:
String
getContextAttributes
(
el
:
Dom.Canvas
)
:
String
getDirection
(
el
:
Dom.Canvas
)
:
CanvasDirection
getFillStyle
(
el
:
Dom.Canvas
)
:
CanvasFillStyle
getFilter
(
el
:
Dom.Canvas
)
:
String
getFont
(
el
:
Dom.Canvas
)
:
String
getFontKerning
(
el
:
Dom.Canvas
)
:
CanvasFontKerning
getGlobalAlpha
(
el
:
Dom.Canvas
)
getGlobalCompositeOperation
(
el
:
Dom.Canvas
)
:
CanvasGlobalCompositeOperation
getImageData
(
el
:
Dom.Canvas
x
:
Number
y
:
Number
width
:
Number
height
:
Number
)
:
ImageData

Returns an ImageData object representing the underlying pixel data for a specified portion of the canvas.

This method is not affected by the canvas's transformation matrix. If the specified rectangle extends outside the bounds of the canvas, the pixels outside the canvas are transparent black in the returned ImageData object.

getImageSmoothingEnabled
(
el
:
Dom.Canvas
)
:
Bool
getImageSmoothingQuality
(
el
:
Dom.Canvas
)
:
CanvasImageSmoothingQuality
getLetterSpacing
(
el
:
Dom.Canvas
)
:
Number
getLineCap
(
el
:
Dom.Canvas
)
:
CanvasLineCap
getLineDash
(
el
:
Dom.Canvas
)
:
Array(Number)
getLineDashOffset
(
el
:
Dom.Canvas
)
:
Number
getLineJoin
(
el
:
Dom.Canvas
)
:
CanvasLineJoin
getLineWidth
(
el
:
Dom.Canvas
)
:
Number
getMiterLimit
(
el
:
Dom.Canvas
)
:
Number
getShadowBlur
(
el
:
Dom.Canvas
)
:
Number
getShadowColor
(
el
:
Dom.Canvas
)
:
String
getShadowOffsetX
(
el
:
Dom.Canvas
)
:
Number
getShadowOffsetY
(
el
:
Dom.Canvas
)
:
Number
getStrokeStyle
(
el
:
Dom.Canvas
)
:
CanvasFillStyle
getTextAlign
(
el
:
Dom.Canvas
)
:
CanvasTextAlign
getTextBaseline
(
el
:
Dom.Canvas
)
:
CanvasTextBaseline
getTransform
(
el
:
Dom.Canvas
)
:
DOMMatrix
getWordSpacing
(
el
:
Dom.Canvas
)
:
Number
globalCompositeOperationFromStr
(
globalCompositeOperation
:
String
)
:
CanvasGlobalCompositeOperation
globalCompositeOperationToStr
(
globalCompositeOperation
:
CanvasGlobalCompositeOperation
)
:
String
imageSmoothingFromStr
(
imageSmoothing
:
String
)
:
CanvasImageSmoothingQuality
imageSmoothingToStr
(
imageSmoothing
:
CanvasImageSmoothingQuality
)
:
String
isPointInPath
(
el
:
Dom.Canvas
x
:
Number
y
:
Number
fillRule
:
CanvasFillRule
path
:
Maybe(Path2D)
)
:
Bool

Returns whether or not the specified point is contained in the given path (if passed) or current path.

isPointInStroke
(
el
:
Dom.Canvas
x
:
Number
y
:
Number
path
:
Maybe(Path2D)
)
:
Bool

Returns whether or not the specified point is inside the area contained by the stroking of a path.

lineCapFromStr
(
lineCap
:
String
)
:
CanvasLineCap
lineCapToStr
(
lineCap
:
CanvasLineCap
)
:
String
lineJoinFromStr
(
lineJoin
:
String
)
:
CanvasLineJoin
lineJoinToStr
(
lineJoin
:
CanvasLineJoin
)
:
String
lineTo
(
el
:
Dom.Canvas
x
:
Number
y
:
Number
)
:
Dom.Canvas

Adds a straight line to the current sub-path by connecting the sub-path's last point to the specified (x, y) coordinates.

Like other methods that modify the current path, this method does not directly render anything.

To draw the path onto a canvas, you can use the fill() or stroke() methods.

measureText
(
el
:
Dom.Canvas
text
:
String
)
:
TextMetrics

Returns a TextMetrics object that contains information about the measured text (such as its width, for example).

moveTo
(
el
:
Dom.Canvas
x
:
Number
y
:
Number
)
:
Dom.Canvas

Begins a new sub-path at the point specified by the given (x, y) coordinates.

patternRepeatFromStr
(
patternRepeat
:
String
)
:
CanvasPatternRepeat
patternRepeatToStr
(
patternRepeat
:
CanvasPatternRepeat
)
:
String
putImageData
(
el
:
Dom.Canvas
imageData
:
ImageData
x
:
Number
y
:
Number
)
:
Dom.Canvas

Paints data from the given ImageData object onto the canvas. This method is not affected by the canvas transformation matrix.

putImageDataRect
(
el
:
Dom.Canvas
imageData
:
ImageData
x
:
Number
y
:
Number
rectX
:
Number
rectY
:
Number
rectWidth
:
Number
rectHeight
:
Number
)
:
Dom.Canvas

Paints data from the given ImageData object onto the canvas. Only paints pixels from the defined rectangle. This method is not affected by the canvas transformation matrix.

quadraticCurveTo
(
el
:
Dom.Canvas
cpx
:
Number
cpy
:
Number
x
:
Number
y
:
Number
)
:
Dom.Canvas

Adds a quadratic Bézier curve to the current sub-path.

It requires two points: the first one is a control point and the second one is the end point.

The starting point is the latest point in the current path, which can be changed using moveTo() before creating the quadratic Bézier curve.

rect
(
el
:
Dom.Canvas
x
:
Number
y
:
Number
width
:
number
height
:
number
)
:
Dom.Canvas

Adds a rectangle to the current path.

Like other methods that modify the current path, this method does not directly render anything. To draw the rectangle onto a canvas, you can use the fill() or stroke() methods.

reset
(
el
:
Dom.Canvas
)
:
Dom.Canvas

Resets the rendering context to its default state, allowing it to be reused for drawing something else without having to explicitly reset all the properties.

Resetting clears the backing buffer, drawing state stack, any defined paths, and styles. This includes the current transformation matrix, compositing properties, clipping region, dash list, line styles, text styles, shadows, image smoothing, filters, and so on.

resetTransform
(
el
:
Dom.Canvas
)
:
Dom.Canvas

Resets the current transform to the identity matrix.

restore
(
el
:
Dom.Canvas
)
:
Dom.Canvas

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.

rotate
(
el
:
Dom.Canvas
angle
:
Number
)
:
Dom.Canvas

Adds a rotation to the transformation matrix.

roundRect
(
el
:
Dom.Canvas
x
:
Number
y
:
Number
width
:
Number
height
:
Number
radii
:
Array(Number)
)
:
Dom.Canvas

Adds a rounded rectangle to the current path.

The radii of the corners can be specified in much the same way as the CSS border-radius property.

Like other methods that modify the current path, this method does not directly render anything. To draw the rounded rectangle onto a canvas, you can use the fill() or stroke() methods.

save
(
el
:
Dom.Canvas
)
:
Dom.Canvas

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

scale
(
el
:
Dom.Canvas
x
:
Number
y
:
Number
)
:
Dom.Canvas

Adds a scaling transformation to the canvas units horizontally and/or vertically.

By default, one unit on the canvas is exactly one pixel. A scaling transformation modifies this behavior.

For instance, a scaling factor of 0.5 results in a unit size of 0.5 pixels; shapes are thus drawn at half the normal size.

Similarly, a scaling factor of 2.0 increases the unit size so that one unit becomes two pixels; shapes are thus drawn at twice the normal size.

setDirection
(
el
:
Dom.Canvas
direction
:
CanvasDirection
)
:
Dom.Canvas

Specifies the current text direction used to draw text.

setFillStyle
(
el
:
Dom.Canvas
fillStyle
:
CanvasFillStyle
)
:
Dom.Canvas

Specifies the color, gradient, or pattern to use inside shapes. The default style is #000 (black).

setFilter
(
el
:
Dom.Canvas
filter
:
String
)
:
Dom.Canvas

Provides filter effects such as blurring and grayscaling. It is similar to the CSS filter property and accepts the same values.

setFont
(
el
:
Dom.Canvas
font
:
String
)
:
Dom.Canvas

Specifies the current text style to use when drawing text. This string uses the same syntax as the CSS font specifier.

setFontKerning
(
el
:
Dom.Canvas
fontKerning
:
CanvasFontKerning
)
:
Dom.Canvas

Specifies how font kerning information is used.

Kerning adjusts how adjacent letters are spaced in a proportional font, allowing them to edge into each other's visual area if there is space available. For example, in well-kerned fonts, the characters AV, Ta and We nest together and make character spacing more uniform and pleasant to read than the equivalent text without kerning.

The property corresponds to the font-kerning CSS property.

setGlobalAlpha
(
el
:
Dom.Canvas
globalAlpha
:
Number
)
:
Dom.Canvas

Specifies the alpha (transparency) value that is applied to shapes and images before they are drawn onto the canvas.

setGlobalCompositeOperation
(
el
:
Dom.Canvas
globalCompositeOperation
:
CanvasGlobalCompositeOperation
)
:
Dom.Canvas

Sets the type of compositing operation to apply when drawing new shapes.

See also Compositing and clipping in the Canvas Tutorial.

setImageSmoothingEnabled
(
el
:
Dom.Canvas
imageSmoothingEnabled
:
Bool
)
:
Dom.Canvas

Determines whether scaled images are smoothed (true, default) or not (false). On getting the imageSmoothingEnabled property, the last value it was set to is returned.

This property is useful for games and other apps that use pixel art. When enlarging images, the default resizing algorithm will blur the pixels. Set this property to false to retain the pixels' sharpness.

setImageSmoothingQuality
(
el
:
Dom.Canvas
imageSmoothingQuality
:
CanvasImageSmoothingQuality
)
:
Dom.Canvas

Sets the image smoothing quality

setLetterSpacing
(
el
:
Dom.Canvas
letterSpacing
:
Number
)
:
Dom.Canvas

Specifies the spacing between letters when drawing text.

This corresponds to the CSS letter-spacing property.

setLineCap
(
el
:
Dom.Canvas
lineCap
:
CanvasLineCap
)
:
Dom.Canvas

Determines the shape used to draw the end points of lines.

setLineDash
(
el
:
Dom.Canvas
segments
:
Array(Number)
)
:
Dom.Canvas

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

Segments is an Array of numbers that specify distances to alternately draw a line and a gap (in coordinate space units).

If the number of elements in the array is odd, the elements of the array get copied and concatenated.

For example, [5, 15, 25] will become [5, 15, 25, 5, 15, 25].

If the array is empty, the line dash list is cleared and line strokes return to being solid.

setLineDashOffset
(
el
:
Dom.Canvas
lineDashOffset
:
Number
)
:
Dom.Canvas

Sets the line dash offset, or "phase."

setLineJoin
(
el
:
Dom.Canvas
lineJoin
:
CanvasLineJoin
)
:
Dom.Canvas

Determines the shape used to join two line segments where they meet.

This property has no effect wherever two connected segments have the same direction, because no joining area will be added in this case. Degenerate segments with a length of zero (i.e., with all endpoints and control points at the exact same position) are also ignored.

setLineWidth
(
el
:
Dom.Canvas
lineWidth
:
Number
)
:
Dom.Canvas

Sets the thickness of lines.

setMiterLimit
(
el
:
Dom.Canvas
miterLimit
:
Number
)
:
Dom.Canvas

Sets the miter limit ratio, in coordinate space units. Zero, negative, Infinity, and NaN values are ignored. The default value is 10.0.

setShadowBlur
(
el
:
Dom.Canvas
shadowBlur
:
Number
)
:
Dom.Canvas

Specifies the amount of blur applied to shadows. The default is 0 (no blur).

setShadowColor
(
el
:
Dom.Canvas
shadowColor
:
String
)
:
Dom.Canvas

Specifies the color of shadows.

Be aware that the shadow's rendered opacity will be affected by the opacity of the fillStyle color when filling, and of the strokeStyle color when stroking.

setShadowOffsetX
(
el
:
Dom.Canvas
shadowOffsetX
:
Number
)

Specifies the distance that shadows will be offset horizontally.

Positive values are to the right, and negative to the left. The default value is 0 (no horizontal offset). Infinity and NaN values are ignored.

setShadowOffsetY
(
el
:
Dom.Canvas
shadowOffsetY
:
Number
)

Specifies the distance that shadows will be offset vertically.

Positive values are down, and negative are up. The default value is 0 (no vertical offset). Infinity and NaN values are ignored.

setStrokeStyle
(
el
:
Dom.Canvas
strokeStyle
:
CanvasFillStyle
)

Specifies the color, gradient, or pattern to use for the strokes (outlines) around shapes. The default is #000 (black).

setTextAlign
(
el
:
Dom.Canvas
textAlign
:
CanvasTextAlign
)

Specifies the current text alignment used when drawing text.

The alignment is relative to the x value of the fillText() method. For example, if textAlign is "center", then the text's left edge will be at x - (textWidth / 2).

setTextBaseline
(
el
:
Dom.Canvas
textBaseline
:
CanvasTextBaseline
)

Specifies the current text baseline used when drawing text.

setTransform
(
el
:
Dom.Canvas
a
:
Number
b
:
Number
c
:
Number
d
:
Number
e
:
Number
f
:
Number
)
:
Dom.Canvas

Resets the current transformation to the identity matrix, and then invokes a transformation described by the arguments of this method.

This lets you scale, rotate, translate (move), and skew the context.

setTransformMatrix
(
el
:
Dom.Canvas
transform
:
DOMMatrix2D
)
:
Dom.Canvas

Resets the current transformation to the identity matrix, and then invokes a transformation with the given transform matrix.

This lets you scale, rotate, translate (move), and skew the context.

setWordSpacing
(
el
:
Dom.Canvas
wordSpacing
:
Number
)

Specifies the spacing between words when drawing text.

This corresponds to the CSS word-spacing property.

stroke
(
el
:
Dom.Canvas
path
:
Maybe(Path2D)
)
:
Dom.Canvas

Strokes (outlines) the current or given path with the current stroke style.

Strokes are aligned to the center of a path; in other words, half of the stroke is drawn on the inner side, and half on the outer side.

The stroke is drawn using the non-zero winding rule, which means that path intersections will still get filled.

strokeRect
(
el
:
Dom.Canvas
x
:
Number
y
:
Number
width
:
Number
height
:
Number
)
:
Dom.Canvas

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

This method draws directly to the canvas without modifying the current path, so any subsequent fill() or stroke() calls will have no effect on it.

strokeText
(
el
:
Dom.Canvas
text
:
String
x
:
Number
y
:
Number
maxWidth
:
Number
)
:
Dom.Canvas

Strokes (outlines) the characters of a text string at the specified coordinates.

An optional parameter allows specifying a maxWidth for the rendered text, which the user agent will achieve by condensing the text or by using a lower font size.

This method draws directly to the canvas without modifying the current path, so any subsequent fill() or stroke() calls will have no effect on it.

textAlignFromStr
(
textAlign
:
String
)
:
CanvasTextAlign
textAlignToStr
(
textAlign
:
CanvasTextAlign
)
:
String
textBaselineFromStr
(
textBaseline
:
String
)
:
CanvasTextBaseline
textBaselineToStr
(
textBaseline
:
CanvasTextBaseline
)
:
String
transform
(
el
:
Dom.Canvas
a
:
Number
b
:
Number
c
:
Number
d
:
Number
e
:
Number
f
:
Number
)
:
Dom.Canvas

Multiplies the current transformation with the matrix described by the arguments of this method.

This lets you scale, rotate, translate (move), and skew the context.

translate
(
el
:
Dom.Canvas
x
:
Number
y
:
Number
)
:
Dom.Canvas

Adds a translation transformation to the current matrix.