W3docs

Canvas Reference

The HTML5 <canvas> element is used for drawing graphics via scripting (commonly JavaScript). See what properties and methods can be used to draw on the canvas.

The HTML5 <canvas> element is used for drawing graphics via scripting (commonly JavaScript). On its own, the <canvas> element is just an empty container — you draw into it from a script.

To get something to draw with, you call the element's getContext('2d') method. It returns a CanvasRenderingContext2D object:

const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d'); // a CanvasRenderingContext2D
ctx.fillStyle = 'green';
ctx.fillRect(10, 10, 100, 80);

Almost every property and method on this page belongs to that 2D context object (ctx above), not to the <canvas> element. The two exceptions are getContext() and toDataURL(), which belong to the <canvas> element itself, and the width/height/data properties under Pixel Manipulation, which belong to ImageData objects. These are grouped in their own sections below.

For step-by-step tutorials, see Canvas introduction, drawing shapes, coordinates, text, and gradients.

Colors, Styles, and Shadows

PropertyDescription
fillStyleSets or returns the color, gradient, or pattern used to fill the shapes.
strokeStyleSets or returns the color, gradient, or pattern used for the lines around the shapes.
shadowColorSets or returns the color of the shadows.
shadowBlurSets or returns the blur level of the shadows.
shadowOffsetXSets or returns the x offset of the shadow.
shadowOffsetYSets or returns the y offset of the shadow.
MethodDescription
createLinearGradient()Creates a linear gradient for using on the canvas content.
createPattern()Repeats a particular element in the specified direction.
createRadialGradient()Creates a circular/radial gradient for using on the canvas content.
addColorStop()Defines colors and stop positions in the gradient object.

See the Canvas gradients tutorial for worked examples.

Line Styles

PropertyDescription
lineCapSets or returns the style of the line’s end caps.
lineJoinSets or returns the type of drawn corners.
lineWidthSets or returns the width of the current line.
miterLimitSets or returns the maximum miter length.
lineDashOffsetSets or returns the offset for line dash patterns.
MethodDescription
getLineDash()Returns the current line dash pattern as an array of numbers.
setLineDash()Sets the line dash pattern from an array of numbers (e.g. [5, 3]).

Rectangles

MethodDescription
rect()Creates rectangles.
fillRect()Draws filled rectangles.
strokeRect()Draws rectangular outlines.
clearRect()Clears the specified pixels within a particular rectangle.

Paths

MethodDescription
fill()Fills the current sub-path(s) with the current fillStyle.
stroke()Strokes the current sub-path(s) with the current strokeStyle.
beginPath()Begins a new path or resets the current path.
moveTo()Moves the path to the defined point in the canvas without drawing a line.
closePath()Adds a path from the current point back to the start point.
lineTo()Adds a line to the current sub-path.
clip()Creates a clipping region from the current path.
quadraticCurveTo()Adds a quadratic Bézier curve.
bezierCurveTo()Adds a cubic Bézier curve.
arc()Adds an arc/curve for creating circles or parts of circles.
arcTo()Adds an arc/curve between two tangents.
ellipse()Adds an elliptical arc to the path.
isPointInPath()Defines whether the specified point is in the current path, or not.

Transformations

MethodDescription
scale()Scales up or scales down the current drawing.
rotate()Rotates the current drawing.
translate()Adjusts the coordinate system of the canvas.
transform()Applies transformation matrix to the canvas.
setTransform()Is similar to transform(). Can be used to skew, scale, and translate the canvas.
resetTransform()Resets the current transform to the identity matrix.

See Canvas coordinates for how the transformation matrix maps drawing positions to pixels.

Text

PropertyDescription
fontSets the appearance of a text drawn on the canvas.
textAlignSets the alignment of a text drawn on the canvas.
textBaselineSets the vertical alignment of a text drawn on the canvas.
directionSets or returns the text direction (ltr, rtl, or inherit).
MethodDescription
fillText()Adds text to the canvas.
strokeText()Adds outlined text to the canvas.
measureText()Measures the text width.

See the Canvas text tutorial for full examples.

Image Drawing

PropertyDescription
imageSmoothingEnabledSets or returns whether scaled images are smoothed (true) or pixelated (false).
MethodDescription
drawImage()Draws an image, video, or canvas onto the canvas.

Pixel Manipulation

The properties below belong to the ImageData object returned by createImageData() / getImageData(), not to the <canvas> element or the context.

ImageData propertyDescription
widthReturns an ImageData object’s width in pixels.
heightReturns an ImageData object’s height in pixels.
dataReturns a Uint8ClampedArray containing the RGBA pixel data.
MethodDescription
createImageData()Creates a new ImageData object.
getImageData()Returns an ImageData object which copies the pixel data for the specified rectangle.
putImageData()Puts the image data onto the canvas from the ImageData object.

Compositing

PropertyDescription
globalAlphaSets or returns the current alpha/transparency value.
globalCompositeOperationSets or returns the type of compositing operation when a new image is drawn.
filterSets or returns a CSS filter applied to drawing (e.g. blur(4px), grayscale(1)).

Canvas state

These methods belong to the 2D context and let you save and restore the full drawing state (styles, transforms, clip region) as a stack.

MethodDescription
save()Pushes the current state of the context onto the state stack.
restore()Pops the most recently saved state, restoring those styles and transforms.

The <canvas> element

The following members belong to the <canvas> element itself, not to the 2D context.

MemberDescription
getContext()Returns a drawing context for the canvas — pass '2d' for a CanvasRenderingContext2D.
toDataURL()Returns a data URL (base64 image) of the canvas contents, e.g. 'image/png'.

Practice

Practice
What can the method getContext('2d') do in HTML5 Canvas?
What can the method getContext('2d') do in HTML5 Canvas?
Was this page helpful?