DrawScope

sealed interface DrawScope

The receiver scope for drawing operations inside org.jetbrains.compose.swing.foundation.Canvas, aligned with androidx's DrawScope.

Provides high-level, declarative drawing primitives operating on floating-point coordinates while exposing the underlying Swing Graphics2D via graphics for direct Java2D integration.

Inheritors

Properties

Link copied to clipboard
open val center: Point2D

The center of the drawing surface, at half its width and half its height.

Link copied to clipboard
abstract val graphics: Graphics2D

Direct access to the underlying Swing Graphics2D surface.

Link copied to clipboard
open val height: Int

Convenience accessor for the integer height of the drawing surface.

Link copied to clipboard
abstract val size: Dimension

The size of the canvas drawing surface. Each read answers with a fresh Dimension.

Link copied to clipboard
open val width: Int

Convenience accessor for the integer width of the drawing surface.

Functions

Link copied to clipboard
inline fun DrawScope.clipPath(shape: Shape, block: DrawScope.() -> Unit)

Intersects the clip with shape for the duration of block.

Link copied to clipboard
inline fun DrawScope.clipRect(topLeft: Point2D, size: Dimension2D, block: DrawScope.() -> Unit)

Intersects the current clip with a rectangle defined by topLeft and size.

inline fun DrawScope.clipRect(x: Float = 0.0f, y: Float = 0.0f, width: Float = size.width.toFloat() - x, height: Float = size.height.toFloat() - y, block: DrawScope.() -> Unit)

Intersects the clip with a rectangle defined by x, y, width, height for the duration of block.

Link copied to clipboard
abstract fun drawArc(paint: Paint, startAngle: Float, sweepAngle: Float, useCenter: Boolean, x: Float = 0.0f, y: Float = 0.0f, width: Float = size.width.toFloat() - x, height: Float = size.height.toFloat() - y, style: DrawStyle = DrawStyle.Fill, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws an arc from startAngle sweeping sweepAngle degrees.

Link copied to clipboard
fun DrawScope.drawArc(paint: Paint, startAngle: Float, sweepAngle: Float, useCenter: Boolean, topLeft: Point2D, size: Dimension2D, style: DrawStyle = DrawStyle.Fill, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws an arc positioned within the bounding box defined by topLeft and size.

Link copied to clipboard
abstract fun drawCircle(paint: Paint, radius: Float = minOf(size.width, size.height) / 2.0f, centerX: Float = size.width / 2.0f, centerY: Float = size.height / 2.0f, style: DrawStyle = DrawStyle.Fill, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws a circle centered at (centerX, centerY) with the given radius.

Link copied to clipboard
fun DrawScope.drawCircle(paint: Paint, radius: Float = minOf(size.width, size.height) / 2.0f, center: Point2D, style: DrawStyle = DrawStyle.Fill, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws a circle with radius centered at center.

Link copied to clipboard
abstract fun drawImage(image: Image, x: Float = 0.0f, y: Float = 0.0f, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws an image at its own size with its top-left corner at (x, y). An image still loading draws once it has loaded, as Graphics.drawImage does with its component as the observer.

abstract fun drawImage(image: Image, x: Float, y: Float, width: Float, height: Float, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws an image scaled into the specified rectangle. An image still loading draws once it has loaded, as Graphics.drawImage does with its component as the observer.

Link copied to clipboard
fun DrawScope.drawImage(image: Image, topLeft: Point2D, size: Dimension2D? = null, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws an image positioned at topLeft, optionally scaled to size.

Link copied to clipboard
abstract fun drawLine(paint: Paint, x1: Float, y1: Float, x2: Float, y2: Float, stroke: Stroke = DrawStyle.DefaultStroke.stroke, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws a straight line between (x1, y1) and (x2, y2).

Link copied to clipboard
fun DrawScope.drawLine(paint: Paint, start: Point2D, end: Point2D, stroke: Stroke = DrawStyle.DefaultStroke.stroke, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws a line between start and end.

fun DrawScope.drawLine(paint: Paint, x1: Float, y1: Float, x2: Float, y2: Float, strokeWidth: Float, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws a straight line between (x1, y1) and (x2, y2) using a line of width strokeWidth.

Link copied to clipboard
abstract fun drawOval(paint: Paint, x: Float = 0.0f, y: Float = 0.0f, width: Float = size.width.toFloat() - x, height: Float = size.height.toFloat() - y, style: DrawStyle = DrawStyle.Fill, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws an oval fitting within the bounding box defined by x, y, width, and height.

Link copied to clipboard
fun DrawScope.drawOval(paint: Paint, topLeft: Point2D, size: Dimension2D, style: DrawStyle = DrawStyle.Fill, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws an oval within the bounding box defined by topLeft and size.

Link copied to clipboard
abstract fun drawPath(path: Shape, paint: Paint, style: DrawStyle = DrawStyle.Fill, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws an arbitrary geometric path or Shape (e.g. Path2D, Polygon, GeneralPath).

Link copied to clipboard
abstract fun drawRect(paint: Paint, x: Float = 0.0f, y: Float = 0.0f, width: Float = size.width.toFloat() - x, height: Float = size.height.toFloat() - y, style: DrawStyle = DrawStyle.Fill, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws a rectangle with the specified bounds. Defaults to covering the entire surface.

Link copied to clipboard
fun DrawScope.drawRect(paint: Paint, topLeft: Point2D, size: Dimension2D, style: DrawStyle = DrawStyle.Fill, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws a rectangle positioned at topLeft with size size.

Link copied to clipboard
abstract fun drawRoundRect(paint: Paint, x: Float = 0.0f, y: Float = 0.0f, width: Float = size.width.toFloat() - x, height: Float = size.height.toFloat() - y, cornerRadiusX: Float = 0.0f, cornerRadiusY: Float = cornerRadiusX, style: DrawStyle = DrawStyle.Fill, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws a rounded rectangle whose corners are elliptical arcs with radii cornerRadiusX and cornerRadiusY.

Link copied to clipboard
fun DrawScope.drawRoundRect(paint: Paint, topLeft: Point2D, size: Dimension2D, cornerRadiusX: Float = 0.0f, cornerRadiusY: Float = cornerRadiusX, style: DrawStyle = DrawStyle.Fill, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws a rounded rectangle positioned at topLeft with size size.

Link copied to clipboard
abstract fun drawText(textLayout: TextLayout, x: Float = 0.0f, y: Float = 0.0f, paint: Paint? = null, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws a pre-measured Java2D textLayout with its top-left corner positioned at (x, y).

Link copied to clipboard
fun DrawScope.drawText(textLayout: TextLayout, topLeft: Point2D, paint: Paint? = null, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws textLayout with its top-left corner positioned at topLeft.

fun DrawScope.drawText(text: String, topLeft: Point2D, paint: Paint? = null, font: Font? = null, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws text with its top-left corner positioned at topLeft.

fun DrawScope.drawText(text: String, x: Float = 0.0f, y: Float = 0.0f, paint: Paint? = null, font: Font? = null, @FloatRange(from = 0.0, to = 1.0) alpha: Float = 1.0f)

Draws text with its top-left corner positioned at (x, y).

Link copied to clipboard
inline fun DrawScope.inset(inset: Float, block: DrawScope.() -> Unit)

Insets every side of the drawing area by inset for the duration of block; see the four-sided inset.

inline fun DrawScope.inset(horizontal: Float = 0.0f, vertical: Float = 0.0f, block: DrawScope.() -> Unit)

Insets the left and right of the drawing area by horizontal and its top and bottom by vertical for the duration of block; see the four-sided inset.

inline fun DrawScope.inset(left: Float, top: Float, right: Float, bottom: Float, block: DrawScope.() -> Unit)

Insets the drawing area by left, top, right and bottom for the duration of block: the origin moves to (left, top), and DrawScope.size shrinks by the insets, rounded down. Nothing is clipped.

Link copied to clipboard
fun DrawScope.measureText(text: String, font: Font? = null): TextLayout

Measures the layout of text under font (or the current font if null) using the canvas's font render context.

Link copied to clipboard
fun DrawScope.record(layer: ImageLayer, block: DrawScope.() -> Unit)

Records block into layer, the counterpart of androidx's GraphicsLayer.record inside a DrawScope: block draws in this scope's coordinates and size, onto a recording aligned to the device pixels this scope draws on, so a drawContent() inside it records the content where it would have painted. Draw it back with ImageLayer.draw on DrawScope.graphics. Where the clip leaves nothing to draw, block does not run and layer is left empty.

Link copied to clipboard
inline fun DrawScope.rotate(degrees: Float, pivotX: Float = size.width / 2f, pivotY: Float = size.height / 2f, block: DrawScope.() -> Unit)

Rotates the drawing space by degrees around pivot (pivotX, pivotY) for the duration of block.

Link copied to clipboard
inline fun DrawScope.scale(scaleX: Float, scaleY: Float = scaleX, pivotX: Float = size.width / 2f, pivotY: Float = size.height / 2f, block: DrawScope.() -> Unit)

Scales the drawing space by scaleX and scaleY around pivot (pivotX, pivotY) for the duration of block.

Link copied to clipboard
inline fun DrawScope.translate(dx: Float, dy: Float, block: DrawScope.() -> Unit)

Translates the drawing origin by dx and dy for the duration of block.

Link copied to clipboard
inline fun DrawScope.withTransform(transformBlock: DrawTransform.() -> Unit, drawBlock: DrawScope.() -> Unit)

Executes drawBlock with transformations applied in transformBlock, restoring the previous transform and clip state afterwards.