DrawContext
2D drawing context for creating images programmatically
DrawContext provides a canvas for drawing shapes, text, and images.
Use DrawContext to:
Examples
let ctx = new DrawContext();
ctx.size = new Size(200, 200);
ctx.setFillColor(new Color("#ff0000"));
ctx.fillRect(new Rect(10, 10, 100, 100));
let image = ctx.getImage();let ctx = new DrawContext();
ctx.size = new Size(200, 50);
ctx.setFont(Font.boldSystemFont(20));
ctx.setTextColor(new Color("#000000"));
ctx.drawText("Hello", new Point(10, 10));Properties
size
- Type:
JSValue
Canvas size
Set the dimensions of the drawing canvas.
ctx.size = new Size(200, 200)opaque
- Type:
Bool
Whether the canvas is opaque
If true, the canvas has no transparency. Default is true.
ctx.opaque = falserespectScreenScale
- Type:
Bool
Whether to use screen scale
If true, renders at device screen scale for sharper images. Default is true.
ctx.respectScreenScale = trueMethods
setFillColor
Set fill color for shapes
setFillColor(color): voidcolor- Color as hex string or Color object
Example:
ctx.setFillColor(new Color("#ff0000"))setStrokeColor
Set stroke color for outlines
setStrokeColor(color): voidcolor- Color as hex string or Color object
Example:
ctx.setStrokeColor(new Color("#0000ff"))setLineWidth
Set line width for strokes
setLineWidth(width): voidwidth- Line width in points
Example:
ctx.setLineWidth(2)addPath
Add a path to the context
addPath(path): voidpath- Path object to add
Example:
ctx.addPath(path)fillPath
Fill the current path
Fills the previously added path with the current fill color.
fillPath(): voidExample:
ctx.fillPath()strokePath
Stroke the current path
Draws the outline of the previously added path.
strokePath(): voidExample:
ctx.strokePath()fill
Fill a rectangle (alias for fillRect)
fill(rect): voidrect- Rectangle to fill
Example:
ctx.fill(rect)fillRect
Fill a rectangle
fillRect(rect): voidrect- Rectangle to fill
Example:
ctx.fillRect(new Rect(0, 0, 100, 100))fillEllipse
Fill an ellipse
Fills an ellipse inscribed in the given rectangle.
fillEllipse(rect): voidrect- Bounding rectangle for the ellipse
Example:
ctx.fillEllipse(new Rect(0, 0, 100, 100))stroke
Stroke a rectangle (alias for strokeRect)
stroke(rect): voidrect- Rectangle to stroke
Example:
ctx.stroke(rect)strokeRect
Stroke a rectangle outline
strokeRect(rect): voidrect- Rectangle to stroke
Example:
ctx.strokeRect(new Rect(0, 0, 100, 100))strokeEllipse
Stroke an ellipse outline
strokeEllipse(rect): voidrect- Bounding rectangle for the ellipse
Example:
ctx.strokeEllipse(new Rect(0, 0, 100, 100))drawImageInRect
Draw image in a rectangle
Draws an image scaled to fit the specified rectangle.
drawImageInRect(imageData, rect): voidimageData- Image object to drawrect- Destination rectangle
Example:
ctx.drawImageInRect(image, new Rect(0, 0, 100, 100))drawImageAtPoint
Draw image at a point
Draws an image at its original size at the specified point.
drawImageAtPoint(imageData, point): voidimageData- Image object to drawpoint- Top-left position for the image
Example:
ctx.drawImageAtPoint(image, new Point(10, 10))drawText
Draw text at a point
Draws text at the specified position using current font and color.
drawText(text, pos): voidtext- Text string to drawpos- Position for the text baseline
Example:
ctx.drawText("Hello", new Point(10, 10))drawTextInRect
Draw text in a rectangle
Draws text constrained to a rectangle with word wrapping.
drawTextInRect(text, rect): voidtext- Text string to drawrect- Bounding rectangle for text layout
Example:
ctx.drawTextInRect("Hello World", new Rect(0, 0, 100, 50))setFont
Set the font for text drawing
setFont(font): voidfont- Font object with size and weight
Example:
ctx.setFont(Font.boldSystemFont(20))setFontSize
Set font size (deprecated)
Use setFont() instead for better control.
setFontSize(size): voidsize- Font size in points
Example:
ctx.setFontSize(14)setTextColor
Set text color
setTextColor(color): voidcolor- Color as hex string
Example:
ctx.setTextColor("#000000")setTextAlignedLeft
Align text to the left
setTextAlignedLeft(): voidExample:
ctx.setTextAlignedLeft()setTextAlignedCenter
Align text to the center
setTextAlignedCenter(): voidExample:
ctx.setTextAlignedCenter()setTextAlignedRight
Align text to the right
setTextAlignedRight(): voidExample:
ctx.setTextAlignedRight()getImage
Get the rendered image
Returns the drawing as an image object.
getImage(): objectReturns: Image object with base64 data, width, and height
Example:
let image = ctx.getImage()