DrawContext
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
Section titled “Properties”opaque
Section titled “opaque”Determines whether the context is opaque.
opaque: boolWhen enabled your image will be rendered opaque. Default is true.
respectScreenScale
Section titled “respectScreenScale”Enable to respect the scale of the screen.
respectScreenScale: boolDevices 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 of canvas.
size: SizeSpecifies 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
Section titled “Methods”
new DrawContext()
Section titled “new DrawContext()”Constructs a canvas to draw on.
new DrawContext()Constructs a new canvas to draw images, shapes and texts on.
addPath(path)
Section titled “addPath(path)”Adds a path to the context.
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
Section titled “Parameters”path Path
Path to add to the context.
drawImageAtPoint(image, point)
Section titled “drawImageAtPoint(image, point)”Draws an image at the specified point.
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
Section titled “Parameters”image Image
Image to draw.
point Point
Point at which to draw top-left corner of the image.
drawImageInRect(image, rect, rotation)
Section titled “drawImageInRect(image, rect, rotation)”Draws an image in the specified rect.
drawImageInRect(image: Image, rect: Rect, rotation?: number)Draws the image in the rectangle. The image will be scaled to fit within the rectangle.
Parameters
Section titled “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.
drawText(text, pos)
Section titled “drawText(text, pos)”Draws text at a position.
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
Section titled “Parameters”text string
Text to draw.
pos Point
Position to draw the top-left of the text.
drawTextInRect(text, rect)
Section titled “drawTextInRect(text, rect)”Draws text in a rectangle.
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
Section titled “Parameters”text string
Text to draw.
rect Rect
Rectangle to draw text in.
fill(rect)
Section titled “fill(rect)”Fills a rectangle.
fill(rect: Rect)Fills the rectangle with the color set when calling setFillColor.
Parameters
Section titled “Parameters”rect Rect
Rectangle to fill.
fillEllipse(rect)
Section titled “fillEllipse(rect)”Fills an ellipse.
fillEllipse(rect: Rect)Fills the ellipse that fits within the supplied rectangle with the color set when calling setFillColor.
Parameters
Section titled “Parameters”rect Rect
Rectangle incapsulating the ellipse to fill.
fillPath()
Section titled “fillPath()”Fills the path that was added the latest.
fillPath()The path that was latest added to the context is filled with the color set using setFillColor.
fillRect(rect)
Section titled “fillRect(rect)”Fills a rectangle.
fillRect(rect: Rect)Fills the rectangle with the color set when calling setFillColor.
Parameters
Section titled “Parameters”rect Rect
Rectangle to fill.
getImage()
Section titled “getImage()”Retrieves the image.
getImage(): ImageCall this to retrieve the image you have drawn to the context.
Return value
Section titled “Return value”Image
The image drawn to the context.
setFillColor(color)
Section titled “setFillColor(color)”Sets the fill color.
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
Section titled “Parameters”color Color
Color to set for filling.
setFont(font)
Section titled “setFont(font)”Sets the font to use when drawing text.
setFont(font: Font)Sets the font to be used when drawing texts to the context.
Parameters
Section titled “Parameters”font Font
Font to use when drawing text.
setFontSize(size)
Section titled “setFontSize(size)”Sets the font size used when drawing text.
setFontSize(size: number)Sets the font size to be used when drawing texts to the context.
Parameters
Section titled “Parameters”size number
Font size to use when drawing text.
setLineWidth(width)
Section titled “setLineWidth(width)”Sets the line width for stroking.
setLineWidth(width: number)Sets the line width to be used when performing a stroke operation.
Parameters
Section titled “Parameters”width number
Line width to use for stroking.
setStrokeColor(color)
Section titled “setStrokeColor(color)”Sets the stroke color.
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
Section titled “Parameters”color Color
Color to set for stroking.
setTextAlignedCenter()
Section titled “setTextAlignedCenter()”Specifies that texts should be center aligned.
setTextAlignedCenter()Sets text alignment to center. Texts drawn after calling this will be center aligned inside the provided rectangle.
setTextAlignedLeft()
Section titled “setTextAlignedLeft()”Specifies that texts should be left aligned.
setTextAlignedLeft()Sets text alignment to left. Texts drawn after calling this will be left aligned inside the provided rectangle.
setTextAlignedRight()
Section titled “setTextAlignedRight()”Specifies that texts should be right aligned.
setTextAlignedRight()Sets text alignment to right. Texts drawn after calling this will be right aligned inside the provided rectangle.
setTextColor(color)
Section titled “setTextColor(color)”Sets the text color used when drawing text.
setTextColor(color: Color)Sets the text color to be used when drawing text strings to the context.
Parameters
Section titled “Parameters”color Color
Color to use when drawing text.
stroke(rect)
Section titled “stroke(rect)”Strokes a rectangle.
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
Section titled “Parameters”rect Rect
Rectangle to stroke.
strokeEllipse(rect)
Section titled “strokeEllipse(rect)”Strokes an ellipse.
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
Section titled “Parameters”rect Rect
Rectangle incapsulating the ellipse to stroke.
strokePath()
Section titled “strokePath()”Strokes the path that was added the latest.
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.
strokeRect(rect)
Section titled “strokeRect(rect)”Strokes a rectangle.
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
Section titled “Parameters”rect Rect
Rectangle to stroke.