Skip to content

Interface: CanvasRenderingContext2D ​

Defined in: types/desktop-engine.d.ts:1586

2D rendering context (Canvas2DRenderingContext); largely the same interface as on the web

Properties ​

canvas ​

ts
readonly canvas: Canvas | null;

Defined in: types/desktop-engine.d.ts:1588

The owning canvas


fillStyle ​

ts
fillStyle: 
  | string
  | CanvasGradient
  | CanvasPattern;

Defined in: types/desktop-engine.d.ts:1604

Fill style. Colors: #rgb, #rgba, #rrggbb, #rrggbbaa, rgb()/rgba() and hsl()/hsla() with commas or spaces (rgb(255 0 0 / 50%)), or a CSS color name; invalid colors are ignored, like on the web. Colors read back as an rgba(r,g,b,a) string


font ​

ts
font: string;

Defined in: types/desktop-engine.d.ts:1625

Font, the CSS font shorthand: [italic] [weight] size[/line-height] family[, family…], such as "600 14px system-ui" or "bold 16px 'Avenir Next', Helvetica"; defaults to 10px Helvetica.

  • weight: normal, bold or 1–1000 (400 normal, 700 bold)
  • size: px or pt; the line height is ignored
  • family: the first installed one of the list. system-ui (also -apple-system, BlinkMacSystemFont, ui-sans-serif) is the system font, San Francisco with PingFang for Chinese; ui-rounded, ui-monospace and ui-serif are its rounded, monospaced and serif (New York) designs; sans-serif, serif and monospace are Helvetica, Times and Courier. Without an installed family the font keeps its family

An invalid value is ignored. Reads back as [italic ][weight ]<size>px <family>, e.g. 600 14px system-ui


globalAlpha ​

ts
globalAlpha: number;

Defined in: types/desktop-engine.d.ts:1608

Global alpha


globalCompositeOperation ​

ts
globalCompositeOperation: GlobalCompositeOperation;

Defined in: types/desktop-engine.d.ts:1590

Compositing mode


imageSmoothingEnabled ​

ts
imageSmoothingEnabled: boolean;

Defined in: types/desktop-engine.d.ts:1635

Whether images are smoothed when scaled


letterSpacing ​

ts
letterSpacing: string;

Defined in: types/desktop-engine.d.ts:1631

Space added after each character of fillText, strokeText and measureText, a CSS length: "2px", "0.1em" (of the font's size; rem too), pt, pc, in, cm, mm, q, or "0" without a unit (it reads as "0px"); "0px" by default. Anything else is ignored (API 5)


lineCap ​

ts
lineCap: CanvasLineCap;

Defined in: types/desktop-engine.d.ts:1592

Line cap


lineDashOffset ​

ts
lineDashOffset: number;

Defined in: types/desktop-engine.d.ts:1637

Line dash offset


lineJoin ​

ts
lineJoin: CanvasLineJoin;

Defined in: types/desktop-engine.d.ts:1594

Line join


lineWidth ​

ts
lineWidth: number;

Defined in: types/desktop-engine.d.ts:1610

Line width


miterLimit ​

ts
miterLimit: number;

Defined in: types/desktop-engine.d.ts:1612

Miter limit


shadowBlur ​

ts
shadowBlur: number;

Defined in: types/desktop-engine.d.ts:1641

Shadow blur


shadowColor ​

ts
shadowColor: string;

Defined in: types/desktop-engine.d.ts:1639

Shadow color, as for fillStyle (reads back as an rgba() string)


shadowOffsetX ​

ts
shadowOffsetX: number;

Defined in: types/desktop-engine.d.ts:1643

Shadow x offset


shadowOffsetY ​

ts
shadowOffsetY: number;

Defined in: types/desktop-engine.d.ts:1645

Shadow y offset


strokeStyle ​

ts
strokeStyle: 
  | string
  | CanvasGradient
  | CanvasPattern;

Defined in: types/desktop-engine.d.ts:1606

Stroke style, colors as for fillStyle; colors read back as an rgba(r,g,b,a) string


textAlign ​

ts
textAlign: CanvasTextAlign;

Defined in: types/desktop-engine.d.ts:1596

Text alignment


textBaseline ​

ts
textBaseline: CanvasTextBaseline;

Defined in: types/desktop-engine.d.ts:1598

Text baseline


wordSpacing ​

ts
wordSpacing: string;

Defined in: types/desktop-engine.d.ts:1633

Space added after each space (and no-break space) besides letterSpacing, as for letterSpacing (API 5)

Methods ​

arc() ​

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

Defined in: types/desktop-engine.d.ts:1737

Arc, clockwise unless counterclockwise; angles 2π or more apart draw the whole circle, equal angles nothing. Throws an IndexSizeError for a negative radius

Parameters ​

ParameterType
xnumber
ynumber
radiusnumber
startAnglenumber
endAnglenumber
counterclockwise?boolean

Returns ​

void


arcTo() ​

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

Defined in: types/desktop-engine.d.ts:1732

Arc tangent to two lines

Parameters ​

ParameterType
x1number
y1number
x2number
y2number
radiusnumber

Returns ​

void


beginPath() ​

ts
beginPath(): void;

Defined in: types/desktop-engine.d.ts:1704

Begins a path

Returns ​

void


bezierCurveTo() ​

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

Defined in: types/desktop-engine.d.ts:1728

Cubic Bézier curve

Parameters ​

ParameterType
cp1xnumber
cp1ynumber
cp2xnumber
cp2ynumber
xnumber
ynumber

Returns ​

void


clearRect() ​

ts
clearRect(
   x, 
   y, 
   w, 
   h
): void;

Defined in: types/desktop-engine.d.ts:1685

Clears a rectangle

Parameters ​

ParameterType
xnumber
ynumber
wnumber
hnumber

Returns ​

void


clip() ​

Call Signature ​

ts
clip(fillRule?): void;

Defined in: types/desktop-engine.d.ts:1750

Clips to the current path

Parameters ​
ParameterType
fillRule?CanvasFillRule
Returns ​

void

Call Signature ​

ts
clip(path, fillRule?): void;

Defined in: types/desktop-engine.d.ts:1752

Clips to a Path2D (API 5)

Parameters ​
ParameterType
pathPath2D
fillRule?CanvasFillRule
Returns ​

void


closePath() ​

ts
closePath(): void;

Defined in: types/desktop-engine.d.ts:1706

Closes the path

Returns ​

void


createConicGradient() ​

ts
createConicGradient(
   startAngle, 
   x, 
   y
): CanvasGradient;

Defined in: types/desktop-engine.d.ts:1700

Creates a gradient around a point, starting at startAngle (radians, clockwise from the x axis): the offset of a color stop is the turn (0 to 1) (API 5)

Parameters ​

ParameterType
startAnglenumber
xnumber
ynumber

Returns ​

CanvasGradient


createImageData() ​

ts
createImageData(
   sw, 
   sh, 
   data?
): ImageData;

Defined in: types/desktop-engine.d.ts:1689

Creates pixel data, optionally initialized from a Uint8ClampedArray (non-standard third argument)

Parameters ​

ParameterType
swnumber
shnumber
data?Uint8ClampedArray<ArrayBufferLike>

Returns ​

ImageData


createLinearGradient() ​

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

Defined in: types/desktop-engine.d.ts:1693

Creates a linear gradient

Parameters ​

ParameterType
x0number
y0number
x1number
y1number

Returns ​

CanvasGradient


createPattern() ​

ts
createPattern(image, repetition?): CanvasPattern | undefined;

Defined in: types/desktop-engine.d.ts:1702

Creates a pattern; defaults to repeat

Parameters ​

ParameterType
imageCanvasDrawable
repetition?CanvasPatternRepetition

Returns ​

CanvasPattern | undefined


createRadialGradient() ​

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

Defined in: types/desktop-engine.d.ts:1695

Creates a radial gradient

Parameters ​

ParameterType
x0number
y0number
r0number
x1number
y1number
r1number

Returns ​

CanvasGradient


drawImage() ​

Call Signature ​

ts
drawImage(
   image, 
   dx, 
   dy
): void;

Defined in: types/desktop-engine.d.ts:1675

Draws an image at (dx, dy)

Parameters ​
ParameterType
imageCanvasDrawable
dxnumber
dynumber
Returns ​

void

Call Signature ​

ts
drawImage(
   image, 
   dx, 
   dy, 
   dw, 
   dh
): void;

Defined in: types/desktop-engine.d.ts:1677

Draws an image into the given rectangle

Parameters ​
ParameterType
imageCanvasDrawable
dxnumber
dynumber
dwnumber
dhnumber
Returns ​

void

Call Signature ​

ts
drawImage(
   image, 
   sx, 
   sy, 
   sw, 
   sh, 
   dx, 
   dy, 
   dw, 
   dh
): void;

Defined in: types/desktop-engine.d.ts:1679

Crops the source rectangle and draws it into the destination rectangle

Parameters ​
ParameterType
imageCanvasDrawable
sxnumber
synumber
swnumber
shnumber
dxnumber
dynumber
dwnumber
dhnumber
Returns ​

void


ellipse() ​

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

Defined in: types/desktop-engine.d.ts:1742

Elliptical arc around (x, y), turned by rotation (radians), its angles as arc's (API 5). Throws an IndexSizeError for a negative radius

Parameters ​

ParameterType
xnumber
ynumber
radiusXnumber
radiusYnumber
rotationnumber
startAnglenumber
endAnglenumber
counterclockwise?boolean

Returns ​

void


fill() ​

Call Signature ​

ts
fill(fillRule?): void;

Defined in: types/desktop-engine.d.ts:1708

Fills the current path

Parameters ​
ParameterType
fillRule?CanvasFillRule
Returns ​

void

Call Signature ​

ts
fill(path, fillRule?): void;

Defined in: types/desktop-engine.d.ts:1710

Fills a Path2D; the current path stays as it is (API 5)

Parameters ​
ParameterType
pathPath2D
fillRule?CanvasFillRule
Returns ​

void


fillRect() ​

ts
fillRect(
   x, 
   y, 
   w, 
   h
): void;

Defined in: types/desktop-engine.d.ts:1681

Fills a rectangle

Parameters ​

ParameterType
xnumber
ynumber
wnumber
hnumber

Returns ​

void


fillText() ​

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

Defined in: types/desktop-engine.d.ts:1746

Fills text

Parameters ​

ParameterType
textstring
xnumber
ynumber
maxWidth?number

Returns ​

void


getImageData() ​

ts
getImageData(
   sx, 
   sy, 
   sw, 
   sh
): ImageData;

Defined in: types/desktop-engine.d.ts:1687

Reads pixels

Parameters ​

ParameterType
sxnumber
synumber
swnumber
shnumber

Returns ​

ImageData


getLineDash() ​

ts
getLineDash(): number[];

Defined in: types/desktop-engine.d.ts:1756

Gets the line dash pattern

Returns ​

number[]


getTransform() ​

ts
getTransform(): TransformMatrix;

Defined in: types/desktop-engine.d.ts:1671

The transform's matrix, a new object each time (API 5)

Returns ​

TransformMatrix


isContextLost() ​

ts
isContextLost(): boolean;

Defined in: types/desktop-engine.d.ts:1648

Whether the context was lost: the GPU stopped its work (see the canvas' contextlost event). It isn't restored

Returns ​

boolean


isPointInPath() ​

Call Signature ​

ts
isPointInPath(
   x, 
   y, 
   fillRule?
): boolean;

Defined in: types/desktop-engine.d.ts:1763

Whether the point (in the canvas' pixels, whatever the transform) is inside the current path filled with the rule, or on its edge (API 5). Curves are flattened to within a fraction of a pixel, as they're drawn

Parameters ​
ParameterType
xnumber
ynumber
fillRule?CanvasFillRule
Returns ​

boolean

Call Signature ​

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

Defined in: types/desktop-engine.d.ts:1765

The same for a Path2D, transformed by the context's transform (API 5)

Parameters ​
ParameterType
pathPath2D
xnumber
ynumber
fillRule?CanvasFillRule
Returns ​

boolean


isPointInStroke() ​

Call Signature ​

ts
isPointInStroke(x, y): boolean;

Defined in: types/desktop-engine.d.ts:1770

Whether the point (in the canvas' pixels) is on what stroke() would draw of the current path, with the line's width, caps, joins and dash (API 5)

Parameters ​
ParameterType
xnumber
ynumber
Returns ​

boolean

Call Signature ​

ts
isPointInStroke(
   path, 
   x, 
   y
): boolean;

Defined in: types/desktop-engine.d.ts:1772

The same for a Path2D (API 5)

Parameters ​
ParameterType
pathPath2D
xnumber
ynumber
Returns ​

boolean


lineTo() ​

ts
lineTo(x, y): void;

Defined in: types/desktop-engine.d.ts:1716

Draws a straight line

Parameters ​

ParameterType
xnumber
ynumber

Returns ​

void


measureText() ​

ts
measureText(text): TextMetrics;

Defined in: types/desktop-engine.d.ts:1744

Measures text

Parameters ​

ParameterType
textstring

Returns ​

TextMetrics


moveTo() ​

ts
moveTo(x, y): void;

Defined in: types/desktop-engine.d.ts:1714

Moves the pen

Parameters ​

ParameterType
xnumber
ynumber

Returns ​

void


putImageData() ​

ts
putImageData(
   imageData, 
   dx, 
   dy
): void;

Defined in: types/desktop-engine.d.ts:1691

Writes pixels

Parameters ​

ParameterType
imageDataImageData
dxnumber
dynumber

Returns ​

void


quadraticCurveTo() ​

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

Defined in: types/desktop-engine.d.ts:1730

Quadratic Bézier curve

Parameters ​

ParameterType
cpxnumber
cpynumber
xnumber
ynumber

Returns ​

void


rect() ​

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

Defined in: types/desktop-engine.d.ts:1718

Adds a rectangle to the path

Parameters ​

ParameterType
xnumber
ynumber
wnumber
hnumber

Returns ​

void


resetClip() ​

ts
resetClip(): void;

Defined in: types/desktop-engine.d.ts:1754

Clears the clip (non-standard)

Returns ​

void


resetCurrentState() ​

ts
resetCurrentState(): void;

Defined in: types/desktop-engine.d.ts:1654

Resets the current state (non-standard)

Returns ​

void


resetTransform() ​

ts
resetTransform(): void;

Defined in: types/desktop-engine.d.ts:1673

Resets the transform matrix to the identity

Returns ​

void


restore() ​

ts
restore(): void;

Defined in: types/desktop-engine.d.ts:1652

Restores the state

Returns ​

void


rotate() ​

ts
rotate(angle): void;

Defined in: types/desktop-engine.d.ts:1656

Rotates (radians)

Parameters ​

ParameterType
anglenumber

Returns ​

void


roundRect() ​

ts
roundRect(
   x, 
   y, 
   w, 
   h, 
   radii?
): void;

Defined in: types/desktop-engine.d.ts:1726

Adds a rectangle with rounded corners to the path, as a closed subpath, then starts a new one at (x, y) (API 5). radii is one radius or a list of 1 to 4: all corners; top left and bottom right, top right and bottom left; top left, top right and bottom left, bottom right; or each corner clockwise from the top left. A radius is a number, or { x, y } for an elliptical corner. Radii that don't fit are scaled down together. Throws a RangeError for a negative radius or another count

Parameters ​

ParameterType
xnumber
ynumber
wnumber
hnumber
radii?| number | DOMPointLike | (number | DOMPointLike)[]

Returns ​

void


save() ​

ts
save(): void;

Defined in: types/desktop-engine.d.ts:1650

Saves the state

Returns ​

void


scale() ​

ts
scale(x, y): void;

Defined in: types/desktop-engine.d.ts:1660

Scales

Parameters ​

ParameterType
xnumber
ynumber

Returns ​

void


setLineDash() ​

ts
setLineDash(segments): void;

Defined in: types/desktop-engine.d.ts:1758

Sets the line dash pattern

Parameters ​

ParameterType
segments(string | number)[]

Returns ​

void


setTransform() ​

Call Signature ​

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

Defined in: types/desktop-engine.d.ts:1664

Sets the transform matrix; a value that isn't finite leaves the transform as it is

Parameters ​
ParameterType
anumber
bnumber
cnumber
dnumber
enumber
fnumber
Returns ​

void

Call Signature ​

ts
setTransform(transform?): void;

Defined in: types/desktop-engine.d.ts:1669

Sets the transform to a matrix, such as one getTransform() gave; without one, to the identity (API 5). Throws a TypeError when a value's two names (a and m11…) differ

Parameters ​
ParameterType
transform?DOMMatrix2DInit | null
Returns ​

void


stroke() ​

ts
stroke(path?): void;

Defined in: types/desktop-engine.d.ts:1712

Strokes the current path, or a Path2D (API 5)

Parameters ​

ParameterType
path?Path2D

Returns ​

void


strokeRect() ​

ts
strokeRect(
   x, 
   y, 
   w, 
   h
): void;

Defined in: types/desktop-engine.d.ts:1683

Strokes a rectangle

Parameters ​

ParameterType
xnumber
ynumber
wnumber
hnumber

Returns ​

void


strokeText() ​

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

Defined in: types/desktop-engine.d.ts:1748

Strokes text

Parameters ​

ParameterType
textstring
xnumber
ynumber
maxWidth?number

Returns ​

void


transform() ​

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

Defined in: types/desktop-engine.d.ts:1662

Multiplies the current transform matrix

Parameters ​

ParameterType
anumber
bnumber
cnumber
dnumber
enumber
fnumber

Returns ​

void


translate() ​

ts
translate(x, y): void;

Defined in: types/desktop-engine.d.ts:1658

Translates

Parameters ​

ParameterType
xnumber
ynumber

Returns ​

void

The SDK is released under the Apache License 2.0