The CanvasRenderingContext2D
interface, part of the Canvas API, provides the 2D rendering context for the drawing surface of a <canvas>
element. It is used for drawing shapes, text, images, and other objects.
The interface's properties and methods are described in the reference section of this page. The Canvas tutorial has more explanation, examples, and resources, as well.
For OffscreenCanvas
, there is an equivalent interface that provides the rendering context. The offscreen rendering context inherits most of the same properties and methods as the CanvasRenderingContext2D
and is described in more detail in the OffscreenCanvasRenderingContext2D
reference page.
Basic example
To get a CanvasRenderingContext2D
instance, you must first have an HTML <canvas>
element to work with:
<canvas id="my-house" width="300" height="300"></canvas>
To get the canvas' 2D rendering context, call getContext()
on the <canvas>
element, supplying '2d'
as the argument:
const canvas = document.getElementById("my-house");
const ctx = canvas.getContext("2d");
With the context in hand, you can draw anything you like. This code draws a house:
ctx.lineWidth = 10;
ctx.strokeRect(75, 140, 150, 110);
ctx.fillRect(130, 190, 40, 60);
ctx.beginPath();
ctx.moveTo(50, 140);
ctx.lineTo(150, 60);
ctx.lineTo(250, 140);
ctx.closePath();
ctx.stroke();
The resulting drawing looks like this:
Reference
Context
-
CanvasRenderingContext2D.isContextLost()
Experimental
-
Returns true
if the rendering context was lost.
Drawing rectangles
There are three methods that immediately draw rectangles to the canvas.
CanvasRenderingContext2D.clearRect()
-
Sets all pixels in the rectangle defined by starting point (x, y) and size (width, height) to transparent black, erasing any previously drawn content.
CanvasRenderingContext2D.fillRect()
-
Draws a filled rectangle at (x, y) position whose size is determined by width and height.
CanvasRenderingContext2D.strokeRect()
-
Paints a rectangle which has a starting point at (x, y) and has a w width and an h height onto the canvas, using the current stroke style.
Drawing text
The following methods draw text. See also the TextMetrics
object for text properties.
CanvasRenderingContext2D.fillText()
-
Draws (fills) a given text at the given (x, y) position.
CanvasRenderingContext2D.strokeText()
-
Draws (strokes) a given text at the given (x, y) position.
CanvasRenderingContext2D.measureText()
-
Returns a TextMetrics
object.
Line styles
The following methods and properties control how lines are drawn.
CanvasRenderingContext2D.lineWidth
-
Width of lines. Default 1.0
.
CanvasRenderingContext2D.lineCap
-
Type of endings on the end of lines. Possible values: butt
(default), round
, square
.
CanvasRenderingContext2D.lineJoin
-
Defines the type of corners where two lines meet. Possible values: round
, bevel
, miter
(default).
CanvasRenderingContext2D.miterLimit
-
Miter limit ratio. Default 10
.
CanvasRenderingContext2D.getLineDash()
-
Returns the current line dash pattern array containing an even number of non-negative numbers.
CanvasRenderingContext2D.setLineDash()
-
Sets the current line dash pattern.
CanvasRenderingContext2D.lineDashOffset
-
Specifies where to start a dash array on a line.
Text styles
The following properties control how text is laid out.
CanvasRenderingContext2D.font
-
Font setting. Default value "10px sans-serif"
.
CanvasRenderingContext2D.textAlign
-
Text alignment setting. Possible values: start
(default), end
, left
, right
, center
.
CanvasRenderingContext2D.textBaseline
-
Baseline alignment setting. Possible values: top
, hanging
, middle
, alphabetic
(default), ideographic
, bottom
.
CanvasRenderingContext2D.direction
-
Directionality. Possible values: ltr
, rtl
, inherit
(default).
CanvasRenderingContext2D.letterSpacing
-
Letter spacing. Default: 0px
.
CanvasRenderingContext2D.fontKerning
-
Font kerning. Possible values: auto
(default), normal
, none
.
-
CanvasRenderingContext2D.fontStretch
Experimental
-
Font stretch. Possible values: ultra-condensed
, extra-condensed
, condensed
, semi-condensed
, normal
(default), semi-expanded
, expanded
, extra-expanded
, ultra-expanded
.
-
CanvasRenderingContext2D.fontVariantCaps
Experimental
-
Font variant caps. Possible values: normal
(default), small-caps
, all-small-caps
, petite-caps
, all-petite-caps
, unicase
, titling-caps
.
CanvasRenderingContext2D.textRendering
-
Text rendering. Possible values: auto
(default), optimizeSpeed
, optimizeLegibility
, geometricPrecision
.
CanvasRenderingContext2D.wordSpacing
-
Word spacing. Default value: 0px
Fill and stroke styles
Fill styling is used for colors and styles inside shapes and stroke styling is used for the lines around shapes.
CanvasRenderingContext2D.fillStyle
-
Color or style to use inside shapes. Default #000
(black).
CanvasRenderingContext2D.strokeStyle
-
Color or style to use for the lines around shapes. Default #000
(black).
Gradients and patterns
CanvasRenderingContext2D.createConicGradient()
-
Creates a conic gradient around a point given by coordinates represented by the parameters.
CanvasRenderingContext2D.createLinearGradient()
-
Creates a linear gradient along the line given by the coordinates represented by the parameters.
CanvasRenderingContext2D.createRadialGradient()
-
Creates a radial gradient given by the coordinates of the two circles represented by the parameters.
CanvasRenderingContext2D.createPattern()
-
Creates a pattern using the specified image. It repeats the source in the directions specified by the repetition argument. This method returns a CanvasPattern
.
Shadows
CanvasRenderingContext2D.shadowBlur
-
Specifies the blurring effect. Default: 0
.
CanvasRenderingContext2D.shadowColor
-
Color of the shadow. Default: fully-transparent black.
CanvasRenderingContext2D.shadowOffsetX
-
Horizontal distance the shadow will be offset. Default: 0
.
CanvasRenderingContext2D.shadowOffsetY
-
Vertical distance the shadow will be offset. Default: 0
.
Paths
The following methods can be used to manipulate paths of objects.
CanvasRenderingContext2D.beginPath()
-
Starts a new path by emptying the list of sub-paths. Call this method when you want to create a new path.
CanvasRenderingContext2D.closePath()
-
Causes the point of the pen to move back to the start of the current sub-path. It tries to draw a straight line from the current point to the start. If the shape has already been closed or has only one point, this function does nothing.
CanvasRenderingContext2D.moveTo()
-
Moves the starting point of a new sub-path to the (x, y) coordinates.
CanvasRenderingContext2D.lineTo()
-
Connects the last point in the current sub-path to the specified (x, y) coordinates with a straight line.
CanvasRenderingContext2D.bezierCurveTo()
-
Adds a cubic Bézier curve to the current path.
CanvasRenderingContext2D.quadraticCurveTo()
-
Adds a quadratic Bézier curve to the current path.
CanvasRenderingContext2D.arc()
-
Adds a circular arc to the current path.
CanvasRenderingContext2D.arcTo()
-
Adds an arc to the current path with the given control points and radius, connected to the previous point by a straight line.
CanvasRenderingContext2D.ellipse()
-
Adds an elliptical arc to the current path.
CanvasRenderingContext2D.rect()
-
Creates a path for a rectangle at position (x, y) with a size that is determined by width and height.
CanvasRenderingContext2D.roundRect()
-
Creates a path for a rounded rectangle with a specified position, width, height, and corner radii.
Drawing paths
CanvasRenderingContext2D.fill()
-
Fills the current sub-paths with the current fill style.
CanvasRenderingContext2D.stroke()
-
Strokes the current sub-paths with the current stroke style.
CanvasRenderingContext2D.drawFocusIfNeeded()
-
If a given element is focused, this method draws a focus ring around the current path.
-
CanvasRenderingContext2D.scrollPathIntoView()
Experimental
-
Scrolls the current path or a given path into the view.
CanvasRenderingContext2D.clip()
-
Creates a clipping path from the current sub-paths. Everything drawn after clip()
is called appears inside the clipping path only. For an example, see Clipping paths in the Canvas tutorial.
CanvasRenderingContext2D.isPointInPath()
-
Reports whether or not the specified point is contained in the current path.
CanvasRenderingContext2D.isPointInStroke()
-
Reports whether or not the specified point is inside the area contained by the stroking of a path.
Objects in the CanvasRenderingContext2D
rendering context have a current transformation matrix and methods to manipulate it. The transformation matrix is applied when creating the current default path, painting text, shapes and Path2D
objects. The methods listed below remain for historical and compatibility reasons as DOMMatrix
objects are used in most parts of the API nowadays and will be used in the future instead.
CanvasRenderingContext2D.getTransform()
-
Retrieves the current transformation matrix being applied to the context.
CanvasRenderingContext2D.rotate()
-
Adds a rotation to the transformation matrix. The angle argument represents a clockwise rotation angle and is expressed in radians.
CanvasRenderingContext2D.scale()
-
Adds a scaling transformation to the canvas units by x horizontally and by y vertically.
CanvasRenderingContext2D.translate()
-
Adds a translation transformation by moving the canvas and its origin x horizontally and y vertically on the grid.
CanvasRenderingContext2D.transform()
-
Multiplies the current transformation matrix with the matrix described by its arguments.
CanvasRenderingContext2D.setTransform()
-
Resets the current transform to the identity matrix, and then invokes the transform()
method with the same arguments.
CanvasRenderingContext2D.resetTransform()
-
Resets the current transform by the identity matrix.
Compositing
CanvasRenderingContext2D.globalAlpha
-
Alpha value that is applied to shapes and images before they are composited onto the canvas. Default 1.0
(opaque).
CanvasRenderingContext2D.globalCompositeOperation
-
With globalAlpha
applied this sets how shapes and images are drawn onto the existing bitmap.
Drawing images
CanvasRenderingContext2D.drawImage()
-
Draws the specified image. This method is available in multiple formats, providing a great deal of flexibility in its use.
Pixel manipulation
See also the ImageData
object.
CanvasRenderingContext2D.createImageData()
-
Creates a new, blank ImageData
object with the specified dimensions. All of the pixels in the new object are transparent black.
CanvasRenderingContext2D.getImageData()
-
Returns an ImageData
object representing the underlying pixel data for the area of the canvas denoted by the rectangle which starts at (sx, sy) and has an sw width and sh height.
CanvasRenderingContext2D.putImageData()
-
Paints data from the given ImageData
object onto the bitmap. If a dirty rectangle is provided, only the pixels from that rectangle are painted.
Image smoothing
CanvasRenderingContext2D.imageSmoothingEnabled
-
Image smoothing mode; if disabled, images will not be smoothed if scaled.
CanvasRenderingContext2D.imageSmoothingQuality
-
Allows you to set the quality of image smoothing.
The canvas state
The CanvasRenderingContext2D
rendering context contains a variety of drawing style states (attributes for line styles, fill styles, shadow styles, text styles). The following methods help you to work with that state:
CanvasRenderingContext2D.save()
-
Saves the current drawing style state using a stack so you can revert any change you make to it using restore()
.
CanvasRenderingContext2D.restore()
-
Restores the drawing style state to the last element on the 'state stack' saved by save()
.
CanvasRenderingContext2D.canvas
-
A read-only back-reference to the HTMLCanvasElement
. Might be null
if it is not associated with a <canvas>
element.
CanvasRenderingContext2D.getContextAttributes()
-
Returns an object containing the context attributes used by the browser. Context attributes can be requested when using HTMLCanvasElement.getContext()
to create the 2D context.
CanvasRenderingContext2D.reset()
-
Resets the rendering context, including the backing buffer, the drawing state stack, path, and styles.
-
CanvasRenderingContext2D.isContextLost()
Experimental
-
Returns true
if the rendering context was lost.
Filters
CanvasRenderingContext2D.filter
-
Applies a CSS or SVG filter to the canvas, e.g., to change its brightness or blurriness.
Specifications
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
CanvasRenderingContext2D |
1 |
12 |
1.5 |
9 |
9 |
2 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
arc |
1 |
12 |
1.5 |
9 |
11.6 |
3 |
4.4 |
18 |
4 |
12 |
1 |
1.0 |
arcTo |
1 |
12 |
1.5 |
9 |
11.6 |
2 |
4.4 |
18 |
4 |
12 |
1 |
1.0 |
beginPath |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
bezierCurveTo |
1 |
12 |
1.5 |
9 |
11.6 |
2 |
4.4 |
18 |
4 |
12 |
1 |
1.0 |
canvas |
1 |
12 |
1.5 |
9 |
≤12.1 |
3 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
clearRect |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
clip |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
closePath |
1 |
12 |
1.5 |
9 |
11.6 |
2 |
4.4 |
18 |
4 |
12 |
1 |
1.0 |
createConicGradient |
99 |
99 |
11290Implements an older version of the specification. The gradient starts from a line going vertically up from the center, like the equivalent CSS function.
|
No |
85 |
16.115Implements an older version of the specification. The gradient starts from a line going vertically up from the center, like the equivalent CSS function.
|
99 |
99 |
11290Implements an older version of the specification. The gradient starts from a line going vertically up from the center, like the equivalent CSS function.
|
68 |
16.115Implements an older version of the specification. The gradient starts from a line going vertically up from the center, like the equivalent CSS function.
|
18.0 |
createImageData |
2 |
12 |
3.5 |
9 |
≤12.1 |
4 |
≤37 |
18 |
4 |
≤12.1 |
3.2 |
1.0 |
createLinearGradient |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
createPattern |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
createRadialGradient |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
direction |
77 |
79 |
101 |
No |
64 |
9 |
77 |
77 |
101 |
55 |
9 |
12.0 |
drawFocusIfNeeded |
37 |
14 |
3228 |
No |
24 |
8 |
37 |
37 |
3228 |
24 |
8 |
3.0 |
drawImage |
1 |
12 |
1.5 |
9 |
9 |
2 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
ellipse |
31 |
13 |
48 |
No |
18 |
9 |
4.4.3 |
31 |
48 |
18 |
9 |
2.0 |
fill |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
fillRect |
1 |
12 |
1.5 |
9 |
9 |
2 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
fillStyle |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
fillText |
2 |
12 |
3.5 |
9 |
≤12.1 |
4 |
≤37 |
18 |
4 |
≤12.1 |
3.2 |
1.0 |
filter |
52 |
79 |
49 |
No |
39 |
No |
52 |
52 |
49 |
41 |
No |
6.0 |
font |
2 |
12 |
3.5 |
9 |
≤12.1 |
4 |
≤37 |
18 |
4 |
≤12.1 |
3.2 |
1.0 |
fontKerning |
99 |
99 |
104 |
No |
85 |
No |
99 |
99 |
104 |
68 |
No |
18.0 |
fontStretch |
99 |
99 |
117 |
No |
85 |
No |
99 |
99 |
117 |
68 |
No |
18.0 |
fontVariantCaps |
99 |
99 |
117 |
No |
85 |
No |
99 |
99 |
117 |
68 |
No |
18.0 |
getContextAttributes |
7332–60 |
79 |
117 |
No |
6019–47 |
15 |
734.4.3–60 |
7332–60 |
117 |
5219–44 |
15 |
11.02.0–8.0 |
getImageData |
2 |
12 |
2Since Firefox 5, getImageData now correctly accepts rectangles that extend beyond the bounds of the canvas; pixels outside the canvas are returned as transparent black and now also returns at least one pixel's worth of image data if a rectangle smaller than one pixel is specified. |
9 |
9.5 |
4 |
≤37 |
18 |
4Since Firefox 5, getImageData now correctly accepts rectangles that extend beyond the bounds of the canvas; pixels outside the canvas are returned as transparent black and now also returns at least one pixel's worth of image data if a rectangle smaller than one pixel is specified. |
10.1 |
3.2 |
1.0 |
getLineDash |
23 |
12 |
27 |
11 |
15 |
7 |
4.4 |
25 |
27 |
14 |
7 |
1.5 |
getTransform |
68 |
79 |
70 |
No |
55 |
11.1 |
68 |
68 |
79 |
48 |
11.3 |
10.0 |
globalAlpha |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
globalCompositeOperation |
1 |
12 |
1.5 |
9 |
9 |
2 |
4.4 |
18 |
4 |
10.1 |
1 |
1.0 |
imageSmoothingEnabled |
30 |
15 |
51 |
11 |
17 |
9.1 |
4.4 |
30 |
51 |
18 |
9.3 |
2.0 |
imageSmoothingQuality |
54 |
79 |
No |
No |
41 |
9.1 |
54 |
54 |
No |
41 |
9.3 |
6.0 |
isContextLost |
99 |
99 |
No |
No |
85 |
No |
99 |
99 |
No |
68 |
No |
18.0 |
isPointInPath |
1 |
12 |
2 |
9 |
≤12.1 |
3.1 |
4.4 |
18 |
4 |
≤12.1 |
2 |
1.0 |
isPointInStroke |
26 |
79 |
19 |
No |
15 |
7 |
4.4 |
26 |
19 |
14 |
7 |
1.5 |
letterSpacing |
99 |
99 |
115 |
No |
85 |
No |
99 |
99 |
115 |
68 |
No |
18.0 |
lineCap |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
lineDashOffset |
23 |
12 |
277 |
11 |
15 |
7 |
4.4 |
25 |
277 |
14 |
7 |
1.5 |
lineJoin |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
lineTo |
1 |
12 |
1.5 |
9 |
11.6 |
2 |
4.4 |
18 |
4 |
12 |
1 |
1.0 |
lineWidth |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
measureText |
2 |
12 |
3.5 |
9 |
≤12.1 |
4 |
≤37 |
18 |
4 |
≤12.1 |
3.2 |
1.0 |
miterLimit |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
moveTo |
1 |
12 |
1.5 |
9 |
11.6 |
2 |
4.4 |
18 |
4 |
12 |
1 |
1.0 |
putImageData |
2 |
12 |
2 |
9 |
≤12.1 |
4 |
≤37 |
18 |
4 |
≤12.1 |
3.2 |
1.0 |
quadraticCurveTo |
1 |
12 |
1.5 |
9 |
11.6 |
3 |
4.4 |
18 |
4 |
12 |
1 |
1.0 |
rect |
1 |
12 |
1.5 |
9 |
11.6 |
2 |
4.4 |
18 |
4 |
12 |
1 |
1.0 |
reset |
99 |
99 |
113 |
No |
85 |
No |
99 |
99 |
113 |
68 |
No |
18.0 |
resetTransform |
31 |
79 |
36 |
No |
18 |
10.1 |
4.4.3 |
31 |
36 |
18 |
10.3 |
2.0 |
restore |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
rotate |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
roundRect |
99 |
99 |
112 |
No |
85 |
16 |
99 |
99 |
112 |
68 |
16 |
18.0 |
save |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
scale |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
scrollPathIntoView |
36 |
79 |
No |
No |
23 |
No |
No |
36 |
No |
No |
No |
No |
setLineDash |
23 |
12 |
27 |
11 |
15 |
7 |
4.4 |
25 |
27 |
14 |
7 |
1.5 |
setTransform |
2 |
12 |
3 |
9 |
≤12.1 |
4 |
≤37 |
18 |
4 |
≤12.1 |
3.2 |
1.0 |
shadowBlur |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
shadowColor |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
shadowOffsetX |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
shadowOffsetY |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
stroke |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
strokeRect |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
strokeStyle |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
strokeText |
2 |
12 |
3.5 |
9 |
≤12.1 |
4 |
≤37 |
18 |
4 |
≤12.1 |
3.2 |
1.0 |
textAlign |
2 |
12 |
3.5 |
9 |
≤12.1 |
4 |
≤37 |
18 |
4 |
≤12.1 |
3.2 |
1.0 |
textBaseline |
2 |
12 |
3.5 |
9 |
≤12.1 |
4 |
≤37 |
18 |
4 |
≤12.1 |
3.2 |
1.0 |
textRendering |
99 |
99 |
116 |
No |
85 |
No |
99 |
99 |
116 |
68 |
No |
18.0 |
transform |
1 |
12 |
3 |
9 |
≤12.1 |
3.1 |
4.4 |
18 |
4 |
≤12.1 |
2 |
1.0 |
translate |
1 |
12 |
1.5 |
9 |
≤12.1 |
2 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
wordSpacing |
99 |
99 |
115 |
No |
85 |
No |
99 |
99 |
115 |
68 |
No |
18.0 |
See also