Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to do scale-aware vector operations #795

Open
JonPichel opened this issue Jan 16, 2025 · 0 comments
Open

How to do scale-aware vector operations #795

JonPichel opened this issue Jan 16, 2025 · 0 comments
Labels
question ❔ Further information is requested

Comments

@JonPichel
Copy link

Hi,

I have defined this function to draw nodes in a diagram:

/// Draw a block on the canvas.
///
/// - pos (coordinate): Center coordinate of the block.
/// - width (int): Width of the block.
/// - height (int): Height of the block.
/// - name (none, str): Reference name for the block.
/// - label (content): Text to display inside the block.
/// ->
#let node(pos, width, height, label, name: none) = {
  import cetz.draw: *

  let box-options = (
    stroke: 2pt,
    width: 100%,
    height: 100%,
    radius: 2pt,
  )

  get-ctx(ctx => {
    let (_, pos) = cetz.coordinate.resolve(ctx, pos)
    let start = cetz.vector.add(pos, (-width / 2, -height / 2))
    let end = cetz.vector.add(start, (width, height))

    content(
      start,
      end,
      box(
        ..box-options,
        align(center + horizon, label),
      ),
      name: name,
    )
  })
}

It works fine, but then when I try to scale diagrams that make use of this function using cetz.draw.scale, I find that the size of the nodes created with this function do not change. I know it is because of the way I compute the start and end positions using cetz.vector.add. Is there a way to add these vectors using the context's transform or something?

Thanks

@johannes-wolf johannes-wolf added the question ❔ Further information is requested label Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question ❔ Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants