Skip to contents

Returns the resolved device-pixel geometry of every keyed element: the two endpoints of a segment, the vertices of a line or polygon, the centre of a round mark, the opposite corners of a rect.

Usage

element_geometry(scene)

Arguments

scene

A vl_scene() (or anything with an as_vellum_scene() method).

Value

A data frame of key, kind, vertex (1-based within the element), x and y. Zero rows if the scene has no keyed elements.

Details

This is what a host needs to hit-test exactly without asking the engine per event. scene_model() gives bounding boxes, which are right for a brush and misleading for diagonal or thin marks; this gives the shape the box was standing in for, once, so a browser can compute point-to-geometry distances locally at whatever rate it likes.

Reading the result

One row per vertex, grouped by key. What the vertices mean depends on kind:

kindrowsmeaning
segment2the endpoints
line, polygonkthe vertices, in order; polygon is closed
pathkall rings' vertices concatenated
point1the centre
rect2opposite corners
text, roundrect2the bounding box's corners

Coordinates are device pixels with y growing down, matching scene_model()'s boxes and the rendered SVG's coordinate system.

Examples

s <- vl_scene(4, 3, dpi = 96, bg = "white") |>
  draw(segments_grob(0.1, 0.1, 0.9, 0.9, key = "diagonal"))
element_geometry(s)
#>        key    kind vertex     x     y
#> 1 diagonal segment      1  38.4 259.2
#> 2 diagonal segment      2 345.6  28.8