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. It is recorded from the render itself, so a segment or line drawn with an offset or end caps is reported where it is drawn.

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), ring (1-based within the element), x, y and rule. 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; ring separates them
hexagon, sectorkthe outline, one closed ring
point1the centre
rect, roundrect2the bounding box's opposite corners
text4the label's box, as a closed ring (turned with a rotated label)

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

ring groups the vertices of a multi-ring path – a polygon with a hole, or a multipart feature – so a host can hit-test it the way the engine does. Without it the concatenated vertices are lossy: treating them as one closed ring invents a phantom edge from each ring's last vertex to the next ring's first. Split on ring for the edges. A label set along a path has one ring per glyph box; every other kind is a single ring, so ring is 1 throughout.

rule is the fill rule an element's rings are filled under, one value per element repeated on each of its rows: "nonzero" or "evenodd" for a shape with an interior, NA for one without (a segment, a line, a point). Containment is decided once across all of an element's rings under that rule – even-odd parity of the crossings, or a nonzero sum of winding numbers – so a point in a hole is outside. That is how vl_nearest() decides it, and how the shape is painted.

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 ring     x     y rule
#> 1 diagonal segment      1    1  38.4 259.2 <NA>
#> 2 diagonal segment      2    1 345.6  28.8 <NA>