The CanvasRenderingContext2D interface is used for drawing rectangles, text, images and other objects onto the canvas element. It provides the 2D rendering context for the drawing surface of a <canvas>
element.
To get an instance CanvasRenderingContext2D
, call getContext()
on a canvas
element, supplying 2d
as the argument:
var canvas = document.getElementById('myCanvas'); // in your HTML this element appears as <canvas id="myCanvas"></canvas> var ctx = canvas.getContext('2d');
Once you have the 2D rendering context for a canvas, you can draw within it. For example:
ctx.fillStyle = 'rgb(200,0,0)'; // sets the color to fill in the rectangle with ctx.fillRect(10, 10, 55, 50); // draws the rectangle at position 10, 10 with a width of 55 and a height of 50
See the properties and methods in the sidebar and below. The canvas tutorial has more information, examples, and resources as well.
There are three methods that immediately draw rectangles to the bitmap.
CanvasRenderingContext2D.clearRect()
CanvasRenderingContext2D.fillRect()
CanvasRenderingContext2D.strokeRect()
The following methods are provided for drawing text. See also the TextMetrics
object for text properties.
CanvasRenderingContext2D.fillText()
CanvasRenderingContext2D.strokeText()
CanvasRenderingContext2D.measureText()
TextMetrics
object.The following methods and properties control how lines are drawn.
CanvasRenderingContext2D.lineWidth
1.0
CanvasRenderingContext2D.lineCap
butt
(default), round
, square
.CanvasRenderingContext2D.lineJoin
round
, bevel
, miter
(default).CanvasRenderingContext2D.miterLimit
10
.CanvasRenderingContext2D.getLineDash()
CanvasRenderingContext2D.setLineDash()
CanvasRenderingContext2D.lineDashOffset
The following properties control how text is laid out.
CanvasRenderingContext2D.font
10px sans-serif
.CanvasRenderingContext2D.textAlign
start
(default), end
, left
, right
or center
.CanvasRenderingContext2D.textBaseline
top
, hanging
, middle
, alphabetic
(default), ideographic
, bottom
.CanvasRenderingContext2D.direction
ltr, rtl
, inherit
(default).Fill styling is used for colors and styles inside shapes and stroke styling is used for the lines around shapes.
CanvasRenderingContext2D.fillStyle
#000
(black).CanvasRenderingContext2D.strokeStyle
#000
(black).CanvasRenderingContext2D.createLinearGradient()
CanvasRenderingContext2D.createRadialGradient()
CanvasRenderingContext2D.createPattern()
CanvasImageSource
). It repeats the source in the directions specified by the repetition argument. This method returns a CanvasPattern
.CanvasRenderingContext2D.shadowBlur
0
CanvasRenderingContext2D.shadowColor
CanvasRenderingContext2D.shadowOffsetX
CanvasRenderingContext2D.shadowOffsetY
The following methods can be used to manipulate paths of objects.
CanvasRenderingContext2D.beginPath()
CanvasRenderingContext2D.closePath()
CanvasRenderingContext2D.moveTo()
CanvasRenderingContext2D.lineTo()
x, y
coordinates with a straight line.CanvasRenderingContext2D.bezierCurveTo()
moveTo()
before creating the Bézier curve.CanvasRenderingContext2D.quadraticCurveTo()
CanvasRenderingContext2D.arc()
CanvasRenderingContext2D.arcTo()
CanvasRenderingContext2D.ellipse()
CanvasRenderingContext2D.rect()
CanvasRenderingContext2D.fill()
CanvasRenderingContext2D.stroke()
CanvasRenderingContext2D.drawFocusIfNeeded()
CanvasRenderingContext2D.scrollPathIntoView()
CanvasRenderingContext2D.clip()
clip()
is called appears inside the clipping path only. For an example, see Clipping paths in the Canvas tutorial.CanvasRenderingContext2D.isPointInPath()
CanvasRenderingContext2D.isPointInStroke()
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 SVGMatrix
objects are used in most parts of the API nowadays and will be used in the future instead.
CanvasRenderingContext2D.currentTransform
SVGMatrix
object).CanvasRenderingContext2D.rotate()
CanvasRenderingContext2D.scale()
CanvasRenderingContext2D.translate()
CanvasRenderingContext2D.transform()
CanvasRenderingContext2D.setTransform()
transform()
method with the same arguments.CanvasRenderingContext2D.resetTransform()
CanvasRenderingContext2D.globalAlpha
1.0
(opaque).CanvasRenderingContext2D.globalCompositeOperation
globalAlpha
applied this sets how shapes and images are drawn onto the existing bitmap.CanvasRenderingContext2D.drawImage()
See also the ImageData
object.
CanvasRenderingContext2D.createImageData()
ImageData
object with the specified dimensions. All of the pixels in the new object are transparent black.CanvasRenderingContext2D.getImageData()
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()
ImageData
object onto the bitmap. If a dirty rectangle is provided, only the pixels from that rectangle are painted.CanvasRenderingContext2D.imageSmoothingEnabled
CanvasRenderingContext2D.imageSmoothingQuality
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()
restore()
.CanvasRenderingContext2D.restore()
save()
.CanvasRenderingContext2D.canvas
HTMLCanvasElement
. Might be null
if it is not associated with a <canvas>
element.CanvasRenderingContext2D.addHitRegion()
CanvasRenderingContext2D.removeHitRegion()
id
from the canvas.CanvasRenderingContext2D.clearHitRegions()
CanvasRenderingContext2D.filter
Most of these APIs are deprecated and were removed shortly after Chrome 36.
CanvasRenderingContext2D.clearShadow()
CanvasRenderingContext2D.shadowColor
and CanvasRenderingContext2D.shadowBlur
.CanvasRenderingContext2D.drawImageFromRect()
drawImage
.CanvasRenderingContext2D.setAlpha()
CanvasRenderingContext2D.globalAlpha
instead.CanvasRenderingContext2D.setCompositeOperation()
CanvasRenderingContext2D.globalCompositeOperation
instead.CanvasRenderingContext2D.setLineWidth()
CanvasRenderingContext2D.lineWidth
instead.CanvasRenderingContext2D.setLineJoin()
CanvasRenderingContext2D.lineJoin
instead.CanvasRenderingContext2D.setLineCap()
CanvasRenderingContext2D.lineCap
instead.CanvasRenderingContext2D.setMiterLimit()
CanvasRenderingContext2D.miterLimit
instead.CanvasRenderingContext2D.setStrokeColor()
CanvasRenderingContext2D.strokeStyle
instead.CanvasRenderingContext2D.setFillColor()
CanvasRenderingContext2D.fillStyle
instead.CanvasRenderingContext2D.setShadow()
CanvasRenderingContext2D.shadowColor
and CanvasRenderingContext2D.shadowBlur
instead.CanvasRenderingContext2D.webkitLineDash
CanvasRenderingContext2D.getLineDash()
and CanvasRenderingContext2D.setLineDash()
instead.CanvasRenderingContext2D.webkitLineDashOffset
CanvasRenderingContext2D.lineDashOffset
instead.CanvasRenderingContext2D.webkitImageSmoothingEnabled
CanvasRenderingContext2D.imageSmoothingEnabled
instead.CanvasRenderingContext2D.isContextLost()
WebGLRenderingContext
method it returns true
if the Canvas context has been lost, or false
if not.CanvasRenderingContext2D.webkitBackingStorePixelRatio
CanvasRenderingContext2D.webkitGetImageDataHD
CanvasRenderingContext2D.webkitPutImageDataHD
CanvasRenderingContext2D.mozCurrentTransform
CanvasRenderingContext2D.currentTransform
. CanvasRenderingContext2D.mozCurrentTransformInverse
CanvasRenderingContext2D.mozImageSmoothingEnabled
CanvasRenderingContext2D.imageSmoothingEnabled
.CanvasRenderingContext2D.mozTextStyle
CanvasRenderingContext2D.font
property.CanvasRenderingContext2D.mozDrawText()
CanvasRenderingContext2D.strokeText()
or CanvasRenderingContext2D.fillText()
instead.CanvasRenderingContext2D.mozMeasureText()
CanvasRenderingContext2D.measureText()
instead.CanvasRenderingContext2D.mozPathText()
CanvasRenderingContext2D.mozTextAlongPath()
CanvasRenderingContext2D.drawWindow()
canvas
. The contents of the window's viewport are rendered, ignoring viewport clipping and scrolling.CanvasRenderingContext2D.demote()
CanvasRenderingContext2D.msFillRule
evenodd
or nonzero
(default).Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'CanvasRenderingContext2D' in that specification. | Living Standard |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 1 | Yes | 1.5 | 9 | 9 | 2 |
canvas |
Yes | 12 | Yes | Yes | Yes | Yes |
currentTransform
|
Yes
|
Yes | No
|
No | No | No
|
direction
|
Yes
|
? | No | No | No | Yes |
fillStyle |
Yes | 12 | Yes | Yes | Yes | Yes |
filter
|
52 | ? | 49
|
No | No | No |
font |
Yes | 12 | 3.5 | 9 | Yes | Yes |
globalAlpha |
Yes | 12 | Yes | Yes | Yes | Yes |
globalCompositeOperation |
Yes | 12 | Yes | Yes | Yes | Yes |
imageSmoothingEnabled
|
30
|
15 | 51
|
Yes
|
? | ? |
imageSmoothingQuality
|
54 | ? | No | ? | 41 | Yes |
lineCap |
Yes | 12 | Yes | Yes | Yes | Yes |
lineDashOffset |
Yes | 12 | 27
|
11 | Yes | Yes |
lineJoin |
Yes | 12 | Yes | Yes | Yes | Yes |
lineWidth |
Yes | 12 | Yes | Yes | Yes | Yes |
miterLimit |
Yes | 12 | Yes | Yes | Yes | Yes |
shadowBlur |
Yes | 12 | Yes | Yes | Yes | Yes |
shadowColor |
Yes | 12 | Yes | Yes | Yes | Yes |
shadowOffsetX |
Yes | 12 | Yes | Yes | Yes | Yes |
shadowOffsetY |
Yes | 12 | Yes | Yes | Yes | Yes |
strokeStyle |
Yes | 12 | Yes | Yes | Yes | Yes |
textAlign |
Yes | 12 | 3.5 | Yes | Yes | Yes |
textBaseline |
Yes | 12 | 3.5 | 9 | Yes | Yes |
addHitRegion
|
Yes
|
? | 30
|
No | No | No |
arc |
Yes | 12 | Yes | Yes | Yes | Yes |
arcTo |
Yes | 12 | Yes | Yes | Yes | Yes |
beginPath |
Yes | 12 | Yes | Yes | Yes | Yes |
bezierCurveTo |
Yes | 12 | Yes | Yes | Yes | Yes |
clearHitRegions
|
Yes
|
? | 38
|
No | No | No |
clearRect |
Yes | 12 | Yes | Yes | Yes | Yes |
clip |
Yes | 12 | Yes | Yes | Yes | Yes |
closePath |
Yes | 12 | Yes | Yes | Yes | Yes |
createImageData |
Yes | 12 | Yes | Yes | Yes | Yes |
createLinearGradient |
Yes | 12 | Yes | Yes | Yes | Yes |
createPattern |
Yes | 12 | Yes | Yes | Yes | Yes |
createRadialGradient |
Yes | 12 | Yes | Yes | Yes | Yes |
drawFocusIfNeeded |
Yes | ? | 32
|
No | Yes | Yes |
drawImage |
Yes | 12 | Yes | Yes | Yes | Yes |
drawWidgetAsOnScreen
|
No | No | 41 | No | No | No |
drawWindow
|
No | No | Yes | No | No | No |
ellipse
|
31 | 13 | 48 | No | 18 | 9 |
fill |
Yes | 12 | Yes | Yes | Yes | Yes |
fillRect |
Yes | 12 | Yes | Yes | Yes | Yes |
fillText |
Yes | 12 | 3.5 | 9 | Yes | Yes |
getImageData |
Yes | 12 | Yes
|
Yes | Yes | Yes |
getLineDash |
Yes | 12 | 27 | 11 | Yes | Yes |
isPointInPath |
Yes | 12 | Yes | Yes | Yes | Yes |
isPointInStroke |
Yes | No | Yes | No | Yes | Yes |
lineTo |
Yes | 12 | Yes | Yes | Yes | Yes |
measureText |
Yes | 12 | Yes | 9 | Yes | Yes |
moveTo |
Yes | 12 | Yes | Yes | Yes | Yes |
putImageData |
Yes | 12 | Yes | Yes | Yes | Yes |
quadraticCurveTo |
Yes | 12 | Yes | Yes | Yes | Yes |
rect |
Yes | 12 | Yes | Yes | Yes | Yes |
removeHitRegion
|
Yes
|
? | 30
|
Yes | Yes | Yes |
resetTransform
|
31 | ? | 36 | No | No | No |
restore |
Yes | 12 | Yes | Yes | Yes | Yes |
rotate |
Yes | 12 | Yes | Yes | Yes | Yes |
save |
Yes | 12 | Yes | Yes | Yes | Yes |
scale |
Yes | 12 | Yes | Yes | Yes | Yes |
scrollPathIntoView
|
Yes
|
? | No | No | Yes | No |
setLineDash |
Yes | 12 | 27 | 11 | Yes | Yes |
setTransform |
Yes | 12 | Yes | Yes | Yes | Yes |
stroke |
Yes | 12 | Yes | Yes | Yes | Yes |
strokeRect |
Yes | 12 | Yes | Yes | Yes | Yes |
strokeText |
Yes | 12 | 3.5 | 9 | Yes | Yes |
transform |
Yes | 12 | Yes | Yes | Yes | Yes |
translate |
Yes | 12 | Yes | Yes | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | ? | ? | ? | Yes |
canvas |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
currentTransform
|
No | No | Yes | No | No | No | No |
direction
|
Yes | Yes | ? | No | No | Yes | Yes |
fillStyle |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
filter
|
52 | 52 | ? | 49
|
No | No | 6.0 |
font |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
globalAlpha |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
globalCompositeOperation |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
imageSmoothingEnabled
|
4.4 | ? | Yes | 51
|
? | ? | ? |
imageSmoothingQuality
|
54 | 54 | ? | No | 41 | Yes | 6.0 |
lineCap |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
lineDashOffset |
Yes | Yes | Yes | 27
|
Yes | Yes | Yes |
lineJoin |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
lineWidth |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
miterLimit |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
shadowBlur |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
shadowColor |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
shadowOffsetX |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
shadowOffsetY |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
strokeStyle |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
textAlign |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
textBaseline |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
addHitRegion
|
No | No | ? | 30
|
No | No | No |
arc |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
arcTo |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
beginPath |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
bezierCurveTo |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
clearHitRegions
|
No | No | ? | 38
|
No | No | No |
clearRect |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
clip |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
closePath |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
createImageData |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
createLinearGradient |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
createPattern |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
createRadialGradient |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
drawFocusIfNeeded |
Yes | Yes | ? | 32
|
Yes | Yes | Yes |
drawImage |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
drawWidgetAsOnScreen
|
No | No | No | 41 | No | No | No |
drawWindow
|
No | No | No | Yes | No | No | No |
ellipse
|
No | Yes | Yes | Yes | No | Yes | Yes |
fill |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
fillRect |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
fillText |
Yes | Yes | Yes | 4 | Yes | Yes | Yes |
getImageData |
Yes | Yes | Yes | Yes
|
Yes | Yes | Yes |
getLineDash |
Yes | Yes | Yes | 27 | Yes | Yes | Yes |
isPointInPath |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
isPointInStroke |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
lineTo |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
measureText |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
moveTo |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
putImageData |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
quadraticCurveTo |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
rect |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
removeHitRegion
|
No | No | ? | 30
|
Yes | Yes | No |
resetTransform
|
No | No | ? | 36 | No | No | No |
restore |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
rotate |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
save |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
scale |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
scrollPathIntoView
|
Yes | Yes | ? | No | No | No | Yes |
setLineDash |
Yes | Yes | Yes | No | Yes | Yes | Yes |
setTransform |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
stroke |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
strokeRect |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
strokeText |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
transform |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
translate |
Yes | Yes | Yes | Yes | Yes | Yes | Yes |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D