Skip to content

Commit

Permalink
canvas: Change default transform to remove z (#505)
Browse files Browse the repository at this point in the history
Change the default transformation matrix to have z coordinates of zero
after transformation.
This is actually a fix, as the final 2D projection should not have a
z-value other than zero.

With this change, anchors in 3D space become 2D and thus elements like
rect, Bézier or Catmull, … will correctly work when using them with
anchors to 3D elements. Intersection between 3D elements will also work
(in 2D space).


See: #502 and #493.
  • Loading branch information
johannes-wolf authored Feb 21, 2024
1 parent b55f085 commit bf3ec2f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

## Misc
- The `hide` function now support an additional `bounds:` parameter to enable canvas bounds adjustment for hidden elements
- The default transformation matrix changed

## Libs
### Plot
Expand Down
21 changes: 15 additions & 6 deletions src/canvas.typ
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@

#import util: typst-length

/// Sets up a canvas for drawing on.
/// Sets up a canvas for drawing on.
///
/// The default transformation matrix of the canvas is set to:
/// $mat(1,0,0.5,0;
/// 0,-1,-0.5,0;
/// 0,0,0,0;
/// 0,0,0,1)$
///
/// - length (length,ratio): Used to specify what 1 coordinate unit is. If given a ratio, that ratio is relative to the containing elements width!
/// - body (none,array,element): A code block in which functions from `draw.typ` have been called.
Expand Down Expand Up @@ -41,11 +47,14 @@
prev: (pt: (0, 0, 0)),
em-size: measure(box(width: 1em, height: 1em), st),
style: styles.default,
// Current transform
transform: matrix.mul-mat(
matrix.transform-shear-z(.5),
matrix.transform-scale((x: 1, y: -1, z: 1)),
),
// Current transformation matrix, a lhs coordinate system
// where z is sheared by a half x and y.
// +x = right, +y = up, +z = 1/2 (right + up)
transform:
((1, 0,+.5, 0),
(0,-1,-.5, 0),
(0, 0, .0, 0),
(0, 0, .0, 1)),
// Nodes, stores anchors and paths
nodes: (:),
// group stack
Expand Down
Binary file modified tests/decorations/path/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bf3ec2f

Please sign in to comment.