---
title: DrawContext
description: Context for drawing images.
---

Context for drawing images.

An instance of DrawContext is a canvas on which you can draw an image using shapes, texts and other images. You must specify the size of your canvas by setting the size property. At any point after beginning your drawing and before ending your drawing can you call getImage() to get an image object of your drawing.

## Properties

### opaque

Determines whether the context is opaque.

```typescript
opaque: bool
```

When enabled your image will be rendered opaque. Default is true.

### respectScreenScale

Enable to respect the scale of the screen.

```typescript
respectScreenScale: bool
```

Devices have a screen scale that is used to convert between the logical coordinate space and the device coordinate space. For example, retina screens have a screen scale of 2 or 3 meaning that one point in the logical coordinate space is represented by four or nine pixels. Respecting the screen scale will multiply the specified size of the canvas by the screen scale. For example a canvas of size 200 by 200 will be 600 by 600 when the image is rendered on a retina screen with a screen scale of 3.
When respecting the screen scale is disabled, you may experience that your images looks blurry because essentially the size you have specified will be stretched when rendered on the screen. Default is false.

### size

Size of canvas.

```typescript
size: Size
```

Specifies the size of the canvas on which you are drawing. The image returned by getImage() will have this exact size,
except if respectScreenScale is true.

## Methods

<span id="-new-drawcontext" class="api-anchor"></span>

### new DrawContext()

Constructs a canvas to draw on.

```typescript
new DrawContext()
```

Constructs a new canvas to draw images, shapes and texts on.

<span id="-addpath" class="api-anchor"></span>

### addPath(path)

Adds a path to the context.

```typescript
addPath(path: Path)
```

After adding a path to the context, the path can be stroked or filled by calling strokePath and fillPath. Note that only the path that was added latest will be affected by calls to strokePath and fillPath.

#### Parameters

**`path` `Path`**

Path to add to the context.

<span id="-drawimageatpoint" class="api-anchor"></span>

### drawImageAtPoint(image, point)

Draws an image at the specified point.

```typescript
drawImageAtPoint(image: Image, point: Point)
```

Draws the image at the point. The top-left corner of the image will be drawn at the specified point.

#### Parameters

**`image` `Image`**

Image to draw.

**`point` `Point`**

Point at which to draw top-left corner of the image.

<span id="-drawimageinrect" class="api-anchor"></span>

### drawImageInRect(image, rect, rotation)

Draws an image in the specified rect.

```typescript
drawImageInRect(image: Image, rect: Rect, rotation?: number)
```

Draws the image in the rectangle. The image will be scaled to fit within the rectangle.

#### Parameters

**`image` `Image`**

Image to draw.

**`rect` `Rect`**

Rectangle to draw the image in.

**`rotation` `number`**

Optional. Clockwise rotation in degrees around the center of the rectangle. Defaults to 0.

<span id="-drawtext" class="api-anchor"></span>

### drawText(text, pos)

Draws text at a position.

```typescript
drawText(text: string, pos: Point)
```

Call this to draw a text string to the context. The top-left of the text will be drawn at the specified position.

#### Parameters

**`text` `string`**

Text to draw.

**`pos` `Point`**

Position to draw the top-left of the text.

<span id="-drawtextinrect" class="api-anchor"></span>

### drawTextInRect(text, rect)

Draws text in a rectangle.

```typescript
drawTextInRect(text: string, rect: Rect)
```

Call this to draw a text string in a rectangle. Specify how the text should be aligned within the rectangle by calling setTextAlignedLeft, setTextAlignedCenter or setTextAlignedRight before drawing the text.

#### Parameters

**`text` `string`**

Text to draw.

**`rect` `Rect`**

Rectangle to draw text in.

<span id="-fill" class="api-anchor"></span>

### fill(rect)

Fills a rectangle.

```typescript
fill(rect: Rect)
```

Fills the rectangle with the color set when calling setFillColor.

#### Parameters

**`rect` `Rect`**

Rectangle to fill.

<span id="-fillellipse" class="api-anchor"></span>

### fillEllipse(rect)

Fills an ellipse.

```typescript
fillEllipse(rect: Rect)
```

Fills the ellipse that fits within the supplied rectangle with the color set when calling setFillColor.

#### Parameters

**`rect` `Rect`**

Rectangle incapsulating the ellipse to fill.

<span id="-fillpath" class="api-anchor"></span>

### fillPath()

Fills the path that was added the latest.

```typescript
fillPath()
```

The path that was latest added to the context is filled with the color set using setFillColor.

<span id="-fillrect" class="api-anchor"></span>

### fillRect(rect)

Fills a rectangle.

```typescript
fillRect(rect: Rect)
```

Fills the rectangle with the color set when calling setFillColor.

#### Parameters

**`rect` `Rect`**

Rectangle to fill.

<span id="-getimage" class="api-anchor"></span>

### getImage()

Retrieves the image.

```typescript
getImage(): Image
```

Call this to retrieve the image you have drawn to the context.

#### Return value

**`Image`**

The image drawn to the context.

<span id="-setfillcolor" class="api-anchor"></span>

### setFillColor(color)

Sets the fill color.

```typescript
setFillColor(color: Color)
```

Sets the fill color to be used when performing a fill operation. Any fill operation performed afterwards will fill with the specified color until another call to setFillColor is made.

#### Parameters

**`color` `Color`**

Color to set for filling.

<span id="-setfont" class="api-anchor"></span>

### setFont(font)

Sets the font to use when drawing text.

```typescript
setFont(font: Font)
```

Sets the font to be used when drawing texts to the context.

#### Parameters

**`font` `Font`**

Font to use when drawing text.

<span id="-setfontsize" class="api-anchor"></span>

### setFontSize(size)

Sets the font size used when drawing text.

```typescript
setFontSize(size: number)
```

Sets the font size to be used when drawing texts to the context.

#### Parameters

**`size` `number`**

Font size to use when drawing text.

<span id="-setlinewidth" class="api-anchor"></span>

### setLineWidth(width)

Sets the line width for stroking.

```typescript
setLineWidth(width: number)
```

Sets the line width to be used when performing a stroke operation.

#### Parameters

**`width` `number`**

Line width to use for stroking.

<span id="-setstrokecolor" class="api-anchor"></span>

### setStrokeColor(color)

Sets the stroke color.

```typescript
setStrokeColor(color: Color)
```

Sets the stroke color to be used when performing a stroke operation. Any stroke operation performed afterwards will stroke with the specified color until another call to setStrokeColor is made.

#### Parameters

**`color` `Color`**

Color to set for stroking.

<span id="-settextalignedcenter" class="api-anchor"></span>

### setTextAlignedCenter()

Specifies that texts should be center aligned.

```typescript
setTextAlignedCenter()
```

Sets text alignment to center. Texts drawn after calling this will be center aligned inside the provided rectangle.

<span id="-settextalignedleft" class="api-anchor"></span>

### setTextAlignedLeft()

Specifies that texts should be left aligned.

```typescript
setTextAlignedLeft()
```

Sets text alignment to left. Texts drawn after calling this will be left aligned inside the provided rectangle.

<span id="-settextalignedright" class="api-anchor"></span>

### setTextAlignedRight()

Specifies that texts should be right aligned.

```typescript
setTextAlignedRight()
```

Sets text alignment to right. Texts drawn after calling this will be right aligned inside the provided rectangle.

<span id="-settextcolor" class="api-anchor"></span>

### setTextColor(color)

Sets the text color used when drawing text.

```typescript
setTextColor(color: Color)
```

Sets the text color to be used when drawing text strings to the context.

#### Parameters

**`color` `Color`**

Color to use when drawing text.

<span id="-stroke" class="api-anchor"></span>

### stroke(rect)

Strokes a rectangle.

```typescript
stroke(rect: Rect)
```

Draws a line around the rectangle using the color set when calling setStrokeColor. The line will have the width set when calling setLineWidth.

#### Parameters

**`rect` `Rect`**

Rectangle to stroke.

<span id="-strokeellipse" class="api-anchor"></span>

### strokeEllipse(rect)

Strokes an ellipse.

```typescript
strokeEllipse(rect: Rect)
```

Draws a line around the ellipse that fits within the supplied rectangle. The line will have the color set when calling setStrokeColor and the width set when calling setLineWidth.

#### Parameters

**`rect` `Rect`**

Rectangle incapsulating the ellipse to stroke.

<span id="-strokepath" class="api-anchor"></span>

### strokePath()

Strokes the path that was added the latest.

```typescript
strokePath()
```

The path that was added the latest to the context is stroked with the color set using setStrokeColor and the line width set using setLineWidth.

<span id="-strokerect" class="api-anchor"></span>

### strokeRect(rect)

Strokes a rectangle.

```typescript
strokeRect(rect: Rect)
```

Draws a line around the rectangle using the color set when calling setStrokeColor. The line will have the width set when calling setLineWidth.

#### Parameters

**`rect` `Rect`**

Rectangle to stroke.