<canvas>
element.To get an object of this interface, 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
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()
Most of these APIs are deprecated and will be removed in the future.
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.getContextAttributes()
WebGLRenderingContext
method it returns an Canvas2DContextAttributes
object that contains the attributes "storage" to indicate which storage is used ("persistent" by default) and the attribute "alpha" (true
by default) to indicate that transparency is used in the canvas.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.filter
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.asyncDrawXULElement()
canvas
.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 |
---|---|---|
WHATWG HTML Living Standard The definition of 'CanvasRenderingContext2D' in that specification. | Living Standard |
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | 1 | (Yes) | 1.5 (1.8) | 9 | 9 | 2 |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | ? | ? | (Yes) | ? | ? | ? | ? |
translate()
, transform()
, rotate(),
scale(),
rect()
, clearRect()
, fillRect()
, strokeRect()
, lineTo()
, moveTo()
, quadraticCurveTo()
, arc()
, shadowOffsetX
, shadowOffsetY
, shadowBlur
.
© 2005–2017 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