Skip to content

Choose drawing size after placing contents? #260

@evanfields

Description

@evanfields

Toy example: let's say I wanted to draw a bunch of circles in a row, but I don't know a priori how many.

@drawsvg begin
    background("antiquewhite")
    n_circles = rand(1:20)
    for _ in 1:n_circles
        circle(O, :10, :stroke)
        translate(30, 0)
    end
end 200 50

image

If n_circles is too big, some of the circles won't fit in the drawing. Is there a way to draw things on some abstract canvas and then choose the drawing size?

Of course in this example n_circles could be precomputed outside the @drawsvg macro and from there a width and height computed. But in practice it may not be so easy to know the width and height needed before hand, for example if objects are placed using Luxor's drawing-specific geometric tools like relative translations.

Also, even in this toy example, precomputing width and height offloads to the user some manual geometric calculation (how far does the right side of the drawing need to be? Well, the first circle at centered-origin is centered at x = width/2 from the left side ... then each successive circle ... plus the stroke width ... plus some buffer ...).

So in summary, I'm hoping for a Luxor-idiomatic way to do two related but separate things:

  1. Find the bounding box of all objects drawn
  2. [more importantly] resize the drawing surface before rendering/serialization to cover a specified bounding box, whether that box comes from a magic function or is simply manually calculated by the user

Thanks in advance for any help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    stalestale/inactive/superseded

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions