Element

sealed class Element

Base sealed class for all drawable elements on the canvas.

The Element type system allows the renderer to handle different drawing primitives uniformly. Use pattern matching or when expressions to handle different element types.

See also

Inheritors

Types

Link copied to clipboard
data class Image(val id: String = Uuid.random().toString(), val type: String = "Image", val bytes: ByteArray, val intrinsicSize: Size, val points: List<Offset>, val opacity: Float = 1.0f, val zIndex: Int = 0, val rotation: Float = 0.0f, val createdAt: Long = 0, val modifiedAt: Long = 0) : Element

Bitmap element placed on the canvas. bytes is the raw encoded payload (PNG / JPEG / WebP) and the source of truth — the renderer decodes it into an androidx.compose.ui.graphics.ImageBitmap on demand and caches the result keyed by id. Resize / rotate / move / select reuse the standard two-point AABB infrastructure.

Link copied to clipboard
data class Path(val id: String = Uuid.random().toString(), val type: String = "Path", val samples: List<Element.PathSample>, val strokeColor: Color, val strokeWidth: Float, val alpha: Float, val zIndex: Int = 0, val rotation: Float = 0.0f, val strokeStyle: StrokeStyle = StrokeStyle.SOLID, val createdAt: Long = 0, val modifiedAt: Long = 0) : Element
Link copied to clipboard
data class PathSample(val position: Offset, val width: Float, val tilt: Float? = null, val azimuth: Float? = null)

One sample along a freehand pen stroke.

Link copied to clipboard
data class Shape(val id: String = Uuid.random().toString(), val type: String = "Shape", val shapeType: ShapeType, val points: List<Offset>, val strokeColor: Color, val fillColor: Color? = null, val strokeEnabled: Boolean = true, val strokeWidth: Float, val zIndex: Int = 0, val rotation: Float = 0.0f, val cornerRadius: Float = 0.0f, val strokeStyle: StrokeStyle = StrokeStyle.SOLID, val bend: Offset = Offset.Zero, val startBinding: String? = null, val endBinding: String? = null, val createdAt: Long = 0, val modifiedAt: Long = 0) : Element
Link copied to clipboard
data class Text(val id: String = Uuid.random().toString(), val type: String = "Text", val text: String, val fontFamilyKey: String = DEFAULT_FONT_FAMILY_KEY, val fontSize: Float, val color: Color, val alignment: TextAlignment = TextAlignment.LEFT, val topLeft: Offset, val wrapWidth: Float, val measuredHeight: Float, val opacity: Float = 1.0f, val zIndex: Int = 0, val rotation: Float = 0.0f, val createdAt: Long = 0, val modifiedAt: Long = 0) : Element

Block-level plain-text element. Single style applies to the whole block; rich-text features (inline bold / italic spans, links, cursor-within-text editing) are intentionally NOT in scope here and are tracked separately.

Properties

Link copied to clipboard
abstract val createdAt: Long

Epoch milliseconds at which this element was first created. Used for ordered replay (and later as a tie-break source for collab clocks). 0L means "unknown" — element predates the timestamp field; consumers that sort by createdAt should treat 0 as "earliest".

Link copied to clipboard
abstract val id: String

Unique identifier for this element

Link copied to clipboard
abstract val modifiedAt: Long

Epoch milliseconds at which any property of this element last changed. On creation, equals createdAt. Bumped via touched on every mutation path so sync layers can resolve concurrent edits with LWW semantics.

Link copied to clipboard
abstract val rotation: Float

Rotation in degrees, applied around the element's axis-aligned bounding box center at render time. Points stored on the element are in the unrotated (logical) coordinate space.

Link copied to clipboard
abstract val type: String

String identifier of the element type for serialization

Link copied to clipboard
abstract val zIndex: Int

Layering order (higher values appear on top)

Functions

Link copied to clipboard

Axis-aligned bounding box of the element in unrotated logical space.

Link copied to clipboard
fun Element.hitTest(point: Offset, tolerance: Float = 8.0f): Boolean

Hit test against a world-space point. Inverse-rotates the point into the element's logical space, then applies a per-type test that accounts for stroke width and the provided pick tolerance.

Link copied to clipboard
Link copied to clipboard
fun Element.touched(now: Long = kotlin.time.Clock.System.now().toEpochMilliseconds()): Element

Stamp Element.modifiedAt with now (defaults to current epoch ms). Call this on every mutation site so sync layers see a fresh LWW timestamp.

Link copied to clipboard

Translate all points by delta. Rotation unchanged.

Link copied to clipboard
fun Element.withBounds(newBounds: Rect): Element

Scale points so the bounding box matches newBounds. For two-point shapes this snaps start/end to the new bbox corners (preserving orientation of the original drag direction). For freedraw paths every point is scaled linearly. Rotation unchanged.

Link copied to clipboard

Absolute rotation in degrees.