cetz-package / cetz

CeTZ: ein Typst Zeichenpaket - A library for drawing stuff with Typst.
https://cetz-package.github.io
GNU Lesser General Public License v3.0
894 stars 36 forks source link

Rotate Content #692

Closed Loewe1000 closed 1 month ago

Loewe1000 commented 1 month ago

Hi, I would like to be able to rotate content so that the bike (image as svg) can be rotated here to match the gradient.

I seem to remember that content cannot be rotated. Do you see any other way how I can rotate the image?

Does this functionality already exist? Thanks for your help!

image
johannes-wolf commented 1 month ago

Cetz' content function has an angle: argument, that takes an angle or a second coordinate used to calculate the angle between the that one and the contents position.

johannes-wolf commented 1 month ago

Example:

#import "@preview/cetz:0.2.2"

#let draw-bike(from, to) = {
  import cetz.draw: *

  group(ctx => {
    let (_, abs-pos, abs-dir) = cetz.coordinate.resolve(ctx, from, to)
    let d = cetz.vector.scale(cetz.vector.norm(cetz.vector.sub(abs-dir, abs-pos)), .5)

    line((rel: cetz.vector.scale(d, -1), to: from), (rel: d, to:  from),
      stroke: red)

    // HERE: Pass a second coordinate to `angle:`
    content(from, angle: to, image("rad.svg", width: 1cm), anchor: "south")
  })
}

#cetz.canvas({
  import cetz.draw: *

  line((0,-1), (0, 5), mark: (end: ">"))
  line((-1,0), (10, 0), mark: (end: ">"))

  catmull((0, 3), (3, 4), (6, 2.5), (9, 2), name: "slope")

  draw-bike("slope.1", "slope.1.1")
  draw-bike("slope.3", "slope.3.05")
  draw-bike("slope.5", "slope.5.1")
  draw-bike("slope.8", "slope.8.1")
})

grafik