CanvasRenderingContext2D : CanvasRenderingContext
CanvasRenderingContext2D
is an object that is used to issue 2D drawing commands to a canvas. It is obtained by passing '2d'
to the HTMLCanvasElement.getContext()
method.Instance Properties
The brush used to fill the area of shapes. Must be a css color String
(such as 'black'
or '#000'
), a CanvasGradient
, or a CanvasPattern
.
Example:
RunResults:
The css string describing the font used to draw text. Defaults to '10px sans-serif'
.
Example:
RunResults:
The alpha to use for subsequent paint operations. Must be between 0
and 1
. Defaults to 1
.
Example:
RunResults:
Determines how subsequent paint operations combine with the back buffer. Must be one of the following (source
refers to the current drawing operation and destination
refers to the existing contents of the canvas):
Value | Operation |
---|---|
'source-over' | Draw the source on the canvas normally. |
'source-in' | Keep the source where the destination is opaque. |
'source-out' | Keep the source where the destination is transparent. |
'source-atop' | Draw the source on the destination but only keep pixels that were opaque in the destination. |
'destination-over' | Draw the source under the destination. |
'destination-in' | Keep the destination where the source is opaque. |
'destination-out' | Keep the destination where the source is transparent. |
'destination-atop' | Draw the destination on the source but only keep pixels that were opaque in the source. |
'lighter' | Increase the brightness of pixels under the source. |
'copy' | Keep only the source. |
'xor' | Exclusive OR of the source and destination. |
Defaults to
'source-over'
.Example:
RunResults:
Determines the shape of line endings. Must be one of 'butt'
, 'round'
, or 'square'
. Defaults to 'butt'
.
Example:
RunResults:
Offsets the starting position of the line dash. See also setLineDash().
Example:
RunResults:
Determines how lines meet. Must be one of 'miter'
, 'round'
, or 'bevel'
. Defaults to 'miter'
.
Example:
RunResults:
The width of the stroke. The line is centered on the coordinates so half the width is on either side of the path. Defaults to 1
.
Example:
RunResults:
Adjusts the maximum distance the miter point can extend from the joint.
Example:
RunResults:
Describes how big the shadow blur is. Larger numbers are blurrier. Defaults to 0
.
Example:
RunResults:
Determines the x offset of the shadow from the drawn shape.
Example:
RunResults:
Determines the y offset of the shadow from the drawn shape.
Example:
RunResults:
The brush used to fill the stroke of shapes. Must be a css color String
(such as 'black'
or '#000'
), a CanvasGradient
, or a CanvasPattern
. See also lineDashOffset, setLineDash().
Example:
RunResults:
Determines which edge of the text to place at the x
coordinate passed to fillText
or strokeText
. Must be one of 'start'
, 'end'
, 'left'
, 'right'
, 'center'
. Defaults to 'start'
.
Example:
RunResults:
Determines which edge of the text to place at the y
coordinate passed to fillText
or strokeText
. Must be one of 'top'
, 'hanging'
, 'middle'
, 'alphabetic'
, 'ideographic'
, 'bottom'
. Defaults to 'alphabetic'
.
Example:
RunResults:
Instance Methods
Continues the current path by creating an arc of circle centered at x
, y
of radius radius
from startAngle
to endAngle
. The angles are specified in radians where 0
points to the right, Math.PI / 2
points down, Math.PI
points to the left, etc. The counterclockwise
parameter determines which part of the circle is used for the path.
Example:
RunResults:
Draws 2 imaginary lines between the previous point and the control point and between the control point and the end point. arcTo
fits a circle of the specified radius between these lines. The drawn figure is a line from the previous point to the place where the circle touches the first imaginary line and an arc along the circle to the point where it touches the second imaginary line. This method will not draw all the way to the end point (unless the end point matches the spot where the circle touches the secondary imaginary line).
Example:
RunResults:
Begins a path. Use with moveTo()
, lineTo()
, quadraticCurveTo()
, bezierCurveTo()
, arc()
, arcTo()
, and closePath()
to define the shape of the path. Draw the path with fill()
or stroke()
.
Example:
RunResults:
Draw a bezier curve from the current point to x
, y
using cp1x
, cp1y
and cp2x
, cp2y
as control points. The curve generally does not pass through the control points.
Example:
RunResults:
Creates a buffer of the same size as imageData
that can be filled with pixel data and later copied into the canvas using putImageData()
. The buffer is initialized to transparent. See also getImageData()
and putImageData()
.
Example:
RunResults:
Creates a buffer of size sw
by sh
that can be filled with pixel data and later copied into the canvas using putImageData()
. The buffer is initialized to transparent. See also getImageData()
and putImageData()
.
Example:
RunResults:
Creates a pattern for the specified image
. image
can be either a HTMLImageElement
, HTMLCanvasElement
, HTMLVideoElement
, or ImageBitmap
. repetition
must be one of 'repeat'
, 'repeat-x'
, 'repeat-y'
, or 'no-repeat'
.
Example:
RunResults:
Creates a radial gradient from the circle at x0
, y0
with radius r0
to the circle at x1
, y1
with radius r1
.
Example:
RunResults:
Draws image
to the canvas at dx
, dy
using the natural size of the image. image
can be either an HTMLImageElement
, HTMLCanvasElement
, HTMLVideoElement
, or ImageBitmap
.
Example:
RunResults:
Draws image
to the canvas at dx
, dy
of size dw
by dh
. image
can be either an HTMLImageElement
, HTMLCanvasElement
, HTMLVideoElement
, or ImageBitmap
.
Example:
RunResults:
Draws the subregion of image
starting at sx
, sy
of size sw
by sh
into the subregion of the canvas at dx
, dy
of size dw
by dh
. The image will be stretched and scaled if sw
, sh
does not match dw
by dh
. image
can be either an HTMLImageElement
, HTMLCanvasElement
, HTMLVideoElement
, or ImageBitmap
.
Example:
RunResults:
Draws the specified string at x
and y
using fillStyle
, font
, textAlign
, and textBaseline
.
Example:
RunResults:
Retrieves the contents of the canvas starting at sx
, sy
of size sw
by sh
. See also createImageData()
and putImageData()
.
Example:
RunResults:
Returns true
if the specified location is in the current path (from the most recent call to beginPath()
).
Example:
RunResults:
Copies the contents of imageData
into the canvas at dx
, dy
. If the dirty
parameters are specified, only that region of the imageData
is copied. See also createImageData()
and getImageData()
.
Example:
RunResults:
Draw a quadratic curve from the current point to x
, y
using cpx
, cpy
as a control point. The curve generally does not pass through the control point.
Example:
RunResults:
Applies a rotation transform to the current transform so subsequent drawing operations are rotated by angle
radians. Positive angle
s are clockwise.
Example:
RunResults:
Saves the current state of the this
onto the stack. Call restore()
to restore the saved state. The saved state consists of: transformation matrix, clip region, fillStyle
, font
, globalAlpha
, globalCompositeOperation
, lineCap
, lineJoin
, lineWidth
, miterLimit
, shadowBlur
, shadowColor
, shadowOffsetX
, shadowOffsetY
, strokeStyle
, textAlign
, and textBaseline
.
Example:
RunResults:
Applies a scale transform to the current transform so subsequent drawing operations are scale by x
, y
.
Example:
RunResults:
Defines how long the dash, gaps of stroke are. Set to an empty array to have a solid line. See also lineDashOffset.
Example:
RunResults:
Same as this.setTransform(DOMMatrix.fromMatrix(matrix))
.
Example:
RunResults:
Replaces the current transform with specified matrix
. matrix
can also be an Object that can be passed to the DOMMatrix constructor. See also getTransform().
Example:
RunResults:
Draws a line around the current path with the current strokeStyle
and lineWidth
.
Example:
RunResults:
Draws an outline of the specified string at x
and y
using strokeStyle
, lineWidth
, font
, textAlign
, and textBaseline
.