Skip to content

Proportionally scale arbitrary canvas to a fixed width #689

Answered by johannes-wolf
noahjutz asked this question in Q&A
Discussion options

You must be logged in to vote

Something like:

#import "@preview/cetz:0.2.2"
#set page(width: 12cm, height: 10cm)

#let scale-to(body, target-width: 1) = {
  (ctx => {
    import cetz.draw: *

    let (ctx: _, drawables, bounds) = cetz.process.many(ctx, cetz.util.resolve-body(ctx, body))
    
    let bounds = cetz.vector.sub(bounds.high, bounds.low)
    let ratio = bounds.at(1) / bounds.at(0)

    return cetz.process.many(ctx, {
      group({
        set-viewport((0, 0, 0), (target-width, target-width * ratio, 1), bounds: bounds,)
        body
      })
    })
  },)
}

#place(rect(width: 10cm, height: 8cm))

#cetz.canvas(length: 10cm, {
  import cetz.draw: *

  scale-to({
    circle((2,1), fill: red)
    rotate(300deg)
…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by noahjutz
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants