nx.js
Classes

CanvasRenderingContext2D

Properties

PropertyTypeDescription
directionstring-
fontKerningstring-
globalAlphanumberSpecifies the alpha (transparency) value that is applied to shapes and images before they are drawn onto the canvas. Value is between 0.0 (fully transparent) and 1.0 (fully opaque), inclusive. Values outside that range, including Infinity and NaN, will not be set, and globalAlpha will retain its previous value. Default 1.0 See https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/globalAlpha
globalCompositeOperationGlobalCompositeOperationSpecifies the type of compositing operation to apply when drawing new shapes. Default "source-over" See https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation
imageSmoothingEnabledbooleanDetermines whether scaled images are smoothed (true) or not (false). Default true See https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/imageSmoothingEnabled
imageSmoothingQualityImageSmoothingQualityDetermines the quality of image smoothing. Default "low" Note For this property to have an effect, imageSmoothingEnabled must be true. See https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/imageSmoothingQuality
lineCapCanvasLineCapDetermines the shape used to draw the end points of lines. Default "butt" See https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/lineCap
lineDashOffsetnumberDetermines the line dash offset (or "phase"). See https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/lineDashOffset
lineJoinCanvasLineJoinDetermines the shape used to join two line segments where they meet. This property has no effect wherever two connected segments have the same direction, because no joining area will be added in this case. Degenerate segments with a length of zero (i.e. with all endpoints and control points at the exact same position) are also ignored. Default "miter" See https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/lineJoin
lineWidthnumberDetermines the thickness of lines. Default 1.0 See https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/lineWidth
miterLimitnumberSpecifies the miter limit ratio, in coordinate space units. Zero, negative, Infinity, and NaN values are ignored. Default 10.0 See https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/miterLimit
textAlignCanvasTextAlignSpecifies the current text alignment used when drawing text. The alignment is relative to the x value of the fillText() / strokeText() methods. For example, if textAlign is "center", then the text's left edge will be at x - (textWidth / 2). Default "start" See https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/textAlign
textBaselineCanvasTextBaselineSpecifies the current text baseline used when drawing text. Default "alphabetic" See https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/textBaseline

Accessors

canvas

get canvas(): Screen

A read-only reference to the Canvas object that is associated with the context.

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/canvas

Returns

Screen


fillStyle

get fillStyle(): string

Specifies the color, gradient, or pattern to use inside shapes.

Default

"#000" (black)

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/fillStyle

set fillStyle(v): void

Parameters

ParameterType
vstring

Returns

string


font

get font(): string

Specifies the current text style to use when drawing text. This string uses the same syntax as the CSS font specifier.

Default

"10px sans-serif"

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/font

set font(v): void

Parameters

ParameterType
vstring

Returns

string


strokeStyle

get strokeStyle(): string

Specifies the color, gradient, or pattern to use for the strokes (outlines) around shapes.

Default

"#000" (black)

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/strokeStyle

set strokeStyle(v): void

Parameters

ParameterType
vstring

Returns

string

Methods

arc()

arc(x, y, radius, startAngle, endAngle, counterclockwise?): void

Parameters

ParameterType
xnumber
ynumber
radiusnumber
startAnglenumber
endAnglenumber
counterclockwise?boolean

Returns

void


arcTo()

arcTo(x1, y1, x2, y2, radius): void

Parameters

ParameterType
x1number
y1number
x2number
y2number
radiusnumber

Returns

void


beginPath()

beginPath(): void

Starts a new path by emptying the list of sub-paths. Call this method when you want to create a new path.

Returns

void

Note

To create a new sub-path (i.e. one matching the current canvas state), you can use CanvasRenderingContext2D.moveTo().

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/beginPath


bezierCurveTo()

bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y): void

Parameters

ParameterType
cp1xnumber
cp1ynumber
cp2xnumber
cp2ynumber
xnumber
ynumber

Returns

void


clearRect()

clearRect(x, y, width, height): void

Erases the pixels in a rectangular area by setting them to transparent black.

Parameters

ParameterTypeDescription
xnumberThe x-axis coordinate of the rectangle's starting point.
ynumberThe y-axis coordinate of the rectangle's starting point.
widthnumberThe rectangle's width. Positive values are to the right, and negative to the left.
heightnumberThe rectangle's height. Positive values are down, and negative are up.

Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/clearRect


clip()

clip(fillRule)

clip(fillRule?): void

Turns the current or given path into the current clipping region. The previous clipping region, if any, is intersected with the current or given path to create the new clipping region.

Parameters
ParameterType
fillRule?CanvasFillRule
Returns

void

Example
// Create circular clipping region
ctx.beginPath();
ctx.arc(100, 75, 50, 0, Math.PI * 2);
ctx.clip();
 
// Draw stuff that gets clipped
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "orange";
ctx.fillRect(0, 0, 100, 100);
See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/clip

clip(path, fillRule)

clip(path, fillRule?): void

Parameters
ParameterType
pathPath2D
fillRule?CanvasFillRule
Returns

void


closePath()

closePath(): void

Attempts to add a straight line from the current point to the start of the current sub-path. If the shape has already been closed or has only one point, this function does nothing.

This method doesn't draw anything to the canvas directly. You can render the path using the stroke() or fill() methods.

Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/closePath


createConicGradient()

createConicGradient(startAngle, x, y): CanvasGradient

Parameters

ParameterType
startAnglenumber
xnumber
ynumber

Returns

CanvasGradient


createImageData()

createImageData(sw, sh, settings)

createImageData(sw, sh, settings?): ImageData

Parameters
ParameterType
swnumber
shnumber
settings?ImageDataSettings
Returns

ImageData

createImageData(imagedata)

createImageData(imagedata): ImageData

Parameters
ParameterType
imagedataImageData
Returns

ImageData


createLinearGradient()

createLinearGradient(x0, y0, x1, y1): CanvasGradient

Parameters

ParameterType
x0number
y0number
x1number
y1number

Returns

CanvasGradient


createPattern()

createPattern(image, repetition): null | CanvasPattern

Parameters

ParameterType
imageCanvasImageSource
repetitionnull | string

Returns

null | CanvasPattern


createRadialGradient()

createRadialGradient(x0, y0, r0, x1, y1, r1): CanvasGradient

Parameters

ParameterType
x0number
y0number
r0number
x1number
y1number
r1number

Returns

CanvasGradient


drawImage()

drawImage(image, dx, dy)

drawImage(image, dx, dy): void

Draws an image onto the canvas.

Parameters
ParameterTypeDescription
imageCanvasImageSourceThe image to draw onto the canvas.
dxnumberThe x-axis coordinate in the destination canvas at which to place the top-left corner of the source image.
dynumberThe y-axis coordinate in the destination canvas at which to place the top-left corner of the source image.
Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/drawImage

drawImage(image, dx, dy, dWidth, dHeight)

drawImage(image, dx, dy, dWidth, dHeight): void

Draws an image onto the canvas.

Parameters
ParameterTypeDescription
imageCanvasImageSourceThe image to draw onto the canvas.
dxnumberThe x-axis coordinate in the destination canvas at which to place the top-left corner of the source image.
dynumberThe y-axis coordinate in the destination canvas at which to place the top-left corner of the source image.
dWidthnumberThe width to draw the image in the destination canvas. This allows scaling of the drawn image.
dHeightnumberThe height to draw the image in the destination canvas. This allows scaling of the drawn image.
Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/drawImage

drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)

drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight): void

Draws an image onto the canvas.

Parameters
ParameterTypeDescription
imageCanvasImageSourceThe image to draw onto the canvas.
sxnumberThe x-axis coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context.
synumberThe y-axis coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context.
sWidthnumberThe width of the sub-rectangle of the source image to draw into the destination context.
sHeightnumberThe height of the sub-rectangle of the source image to draw into the destination context.
dxnumberThe x-axis coordinate in the destination canvas at which to place the top-left corner of the source image.
dynumberThe y-axis coordinate in the destination canvas at which to place the top-left corner of the source image.
dWidthnumberThe width to draw the image in the destination canvas. This allows scaling of the drawn image.
dHeightnumberThe height to draw the image in the destination canvas. This allows scaling of the drawn image.
Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/drawImage


ellipse()

ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise?): void

Parameters

ParameterType
xnumber
ynumber
radiusXnumber
radiusYnumber
rotationnumber
startAnglenumber
endAnglenumber
counterclockwise?boolean

Returns

void


fill()

fill(fillRule)

fill(fillRule?): void

Fills the current or given path with the current fillStyle.

Parameters
ParameterTypeDescription
fillRule?CanvasFillRule
Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/fill

fill(path, fillRule)

fill(path, fillRule?): void

Parameters
ParameterType
pathPath2D
fillRule?CanvasFillRule
Returns

void


fillRect()

fillRect(x, y, width, height): void

Draws a rectangle that is filled according to the current fillStyle.

This method draws directly to the canvas without modifying the current path, so any subsequent fill() or stroke() calls will have no effect on it.

Parameters

ParameterTypeDescription
xnumberThe x-axis coordinate of the rectangle's starting point.
ynumberThe y-axis coordinate of the rectangle's starting point.
widthnumberThe rectangle's width. Positive values are to the right, and negative to the left.
heightnumberThe rectangle's height. Positive values are down, and negative are up.

Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/fillRect


fillText()

fillText(text, x, y, maxWidth?): void

Draws a text string at the specified coordinates, filling the string's characters with the current fillStyle.

Parameters

ParameterTypeDescription
textstringA string specifying the text string to render into the context.
xnumberThe x-axis coordinate of the point at which to begin drawing the text, in pixels.
ynumberThe y-axis coordinate of the baseline on which to begin drawing the text, in pixels.
maxWidth?numberThe maximum number of pixels wide the text may be once rendered. If not specified, there is no limit to the width of the text.

Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/fillText


getImageData()

getImageData(sx, sy, sw, sh, settings?): ImageData

Returns an ImageData object representing the underlying pixel data for a specified portion of the canvas.

This method is not affected by the canvas's transformation matrix. If the specified rectangle extends outside the bounds of the canvas, the pixels outside the canvas are transparent black in the returned ImageData object.

Parameters

ParameterTypeDescription
sxnumberThe x-axis coordinate of the top-left corner of the rectangle from which the ImageData will be extracted.
synumberThe y-axis coordinate of the top-left corner of the rectangle from which the ImageData will be extracted.
swnumberThe width of the rectangle from which the ImageData will be extracted. Positive values are to the right, and negative to the left.
shnumberThe height of the rectangle from which the ImageData will be extracted. Positive values are down, and negative are up.
settings?ImageDataSettings-

Returns

ImageData

An ImageData object containing the image data for the rectangle of the canvas specified.

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/getImageData


getLineDash()

getLineDash(): number[]

Gets the current line dash pattern.

Returns

number[]

An Array of numbers that specify distances to alternately draw a line and a gap (in coordinate space units). If the number, when setting the elements, is odd, the elements of the array get copied and concatenated. For example, setting the line dash to [5, 15, 25] will result in getting back [5, 15, 25, 5, 15, 25].

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/getLineDash


getTransform()

getTransform(): DOMMatrix

Retrieves the current transformation matrix being applied to the context.

Returns

DOMMatrix

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/getTransform


isPointInPath()

isPointInPath(x, y, fillRule)

isPointInPath(x, y, fillRule?): boolean

Reports whether or not the specified point is contained in the current path.

Parameters
ParameterType
xnumber
ynumber
fillRule?CanvasFillRule
Returns

boolean

Example
ctx.beginPath();
ctx.rect(10, 10, 100, 100);
console.log(ctx.isPointInPath(30, 70));
// true
See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/isPointInPath

isPointInPath(path, x, y, fillRule)

isPointInPath(path, x, y, fillRule?): boolean

Parameters
ParameterType
pathPath2D
xnumber
ynumber
fillRule?CanvasFillRule
Returns

boolean


isPointInStroke()

isPointInStroke(x, y)

isPointInStroke(x, y): boolean

Reports whether or not the specified point is inside the area contained by the stroking of a path.

Parameters
ParameterType
xnumber
ynumber
Returns

boolean

Example
ctx.beginPath();
ctx.rect(10, 10, 100, 100);
console.log(ctx.isPointInStroke(50, 10));
// true
See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/isPointInStroke

isPointInStroke(path, x, y)

isPointInStroke(path, x, y): boolean

Parameters
ParameterType
pathPath2D
xnumber
ynumber
Returns

boolean


lineTo()

lineTo(x, y): void

Parameters

ParameterType
xnumber
ynumber

Returns

void


measureText()

measureText(text): TextMetrics

Returns a TextMetrics object that contains information about the measured text (such as its width, for example).

Parameters

ParameterTypeDescription
textstringThe text string to measure.

Returns

TextMetrics

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/measureText


moveTo()

moveTo(x, y): void

Parameters

ParameterType
xnumber
ynumber

Returns

void


putImageData()

putImageData(imageData, dx, dy): void

Parameters

ParameterType
imageDataImageData
dxnumber
dynumber

Returns

void


quadraticCurveTo()

quadraticCurveTo(cpx, cpy, x, y): void

Parameters

ParameterType
cpxnumber
cpynumber
xnumber
ynumber

Returns

void


rect()

rect(x, y, w, h): void

Parameters

ParameterType
xnumber
ynumber
wnumber
hnumber

Returns

void


resetTransform()

resetTransform(): void

Resets the current transform to the identity matrix.

Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/resetTransform


restore()

restore(): void

Saves the entire state of the canvas by pushing the current state onto a stack.

Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/save


rotate()

rotate(angle): void

Adds the specified angle of rotation to the transformation matrix.

Parameters

ParameterTypeDescription
anglenumberThe rotation angle, clockwise in radians. You can use degree * Math.PI / 180 to calculate a radian from a degree.

Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/rotate


roundRect()

roundRect(x, y, width, height, radii): void

Adds a rounded rectangle to the current path.

Parameters

ParameterTypeDefault valueDescription
xnumberundefinedThe x-axis coordinate of the rectangle's starting point, in pixels.
ynumberundefinedThe y-axis coordinate of the rectangle's starting point, in pixels.
widthnumberundefinedThe rectangle's width. Positive values are to the right, and negative to the left.
heightnumberundefinedThe rectangle's height. Positive values are down, and negative are up.
radiinumber | DOMPointInit | Iterable<number | DOMPointInit>0A number or list specifying the radii of the circular arc to be used for the corners of the rectangle.

Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/roundRect


save()

save(): void

Restores the most recently saved canvas state by popping the top entry in the drawing state stack. If there is no saved state, this method does nothing.

Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/restore


scale()

scale(x, y): void

Adds a scaling transformation to the canvas units horizontally and/or vertically.

By default, one unit on the canvas is exactly one pixel. A scaling transformation modifies this behavior. For instance, a scaling factor of 0.5 results in a unit size of 0.5 pixels; shapes are thus drawn at half the normal size. Similarly, a scaling factor of 2.0 increases the unit size so that one unit becomes two pixels; shapes are thus drawn at twice the normal size.

Parameters

ParameterTypeDescription
xnumberScaling factor in the horizontal direction. A negative value flips pixels across the vertical axis. A value of 1 results in no horizontal scaling.
ynumberScaling factor in the vertical direction. A negative value flips pixels across the horizontal axis. A value of 1 results in no vertical scaling.

Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/scale


setLineDash()

setLineDash(segments): void

Sets the line dash pattern used when stroking lines. It uses an array of values that specify alternating lengths of lines and gaps which describe the pattern.

Parameters

ParameterTypeDescription
segmentsnumber[]An Array of numbers that specify distances to alternately draw a line and a gap (in coordinate space units). If the number of elements in the array is odd, the elements of the array get copied and concatenated. For example, [5, 15, 25] will become [5, 15, 25, 5, 15, 25]. If the array is empty, the line dash list is cleared and line strokes return to being solid.

Returns

void

Note

To return to using solid lines, set the line dash list to an empty array.

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/setLineDash


setTransform()

setTransform(a, b, c, d, e, f)

setTransform(a, b, c, d, e, f): void

Resets (overrides) the current transformation to the identity matrix, and then invokes a transformation described by the arguments of this method. This lets you scale, rotate, translate (move), and skew the context.

Parameters
ParameterTypeDescription
anumber(m11) The cell in the first row and first column of the matrix.
bnumber(m12) The cell in the second row and first column of the matrix.
cnumber(m21) The cell in the first row and second column of the matrix.
dnumber(m22) The cell in the second row and second column of the matrix.
enumber(m41) The cell in the first row and third column of the matrix.
fnumber(m42) The cell in the second row and third column of the matrix.
Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/setTransform

setTransform(transform)

setTransform(transform?): void

Resets (overrides) the current transformation to the identity matrix, and then invokes a transformation described by the arguments of this method. This lets you scale, rotate, translate (move), and skew the context.

Parameters
ParameterTypeDescription
transform?DOMMatrix2DInitAn object containing the DOMMatrix 2D transformation values.
Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/setTransform


stroke()

stroke(path?): void

Strokes (outlines) the current or given path with the current stroke style.

Parameters

ParameterTypeDescription
path?Path2DA Path2D path to stroke.

Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/stroke


strokeRect()

strokeRect(x, y, width, height): void

Draws a rectangle that is stroked (outlined) according to the current strokeStyle and other context settings.

This method draws directly to the canvas without modifying the current path, so any subsequent fill() or stroke() calls will have no effect on it.

Parameters

ParameterTypeDescription
xnumberThe x-axis coordinate of the rectangle's starting point.
ynumberThe y-axis coordinate of the rectangle's starting point.
widthnumberThe rectangle's width. Positive values are to the right, and negative to the left.
heightnumberThe rectangle's height. Positive values are down, and negative are up.

Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/strokeRect


strokeText()

strokeText(text, x, y, maxWidth?): void

Draws the outlines of the characters of the text string at the specified coordinates, stroking the string's characters with the current strokeStyle.

Parameters

ParameterTypeDescription
textstringA string specifying the text string to render into the context.
xnumberThe x-axis coordinate of the point at which to begin drawing the text, in pixels.
ynumberThe y-axis coordinate of the baseline on which to begin drawing the text, in pixels.
maxWidth?numberThe maximum number of pixels wide the text may be once rendered. If not specified, there is no limit to the width of the text.

Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/strokeText


transform()

transform(a, b, c, d, e, f): void

Multiplies the current transformation with the matrix described by the arguments of this method. This lets you scale, rotate, translate (move), and skew the context.

Parameters

ParameterTypeDescription
anumber(m11) The cell in the first row and first column of the matrix.
bnumber(m12) The cell in the second row and first column of the matrix.
cnumber(m21) The cell in the first row and second column of the matrix.
dnumber(m22) The cell in the second row and second column of the matrix.
enumber(m41) The cell in the first row and third column of the matrix.
fnumber(m42) The cell in the second row and third column of the matrix.

Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/transform


translate()

translate(x, y): void

Adds a translation transformation to the current matrix by moving the canvas and its origin x units horizontally and y units vertically on the grid.

Parameters

ParameterTypeDescription
xnumberDistance to move in the horizontal direction. Positive values are to the right, and negative to the left.
ynumberDistance to move in the vertical direction. Positive values are down, and negative are up.

Returns

void

See

https://developer.mozilla.org/docs/Web/API/CanvasRenderingContext2D/translate

On this page