-
Notifications
You must be signed in to change notification settings - Fork 0
/
manual.typ
328 lines (276 loc) · 7.94 KB
/
manual.typ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#import "canvas.typ": canvas
#let canvas-background = gray.lighten(75%)
#let example(body, source, ..args) = {
table(
columns: (4cm, auto),
fill: (x, y) => (canvas-background, none).at(x),
align: (x, y) => (center, left).at(x),
stroke: none,
canvas(body, ..args),
source
)
}
#let br() = {
v(0.5cm)
line(length: 100%)
}
#set page(
numbering: "1/1",
header: align(right)[The `canvas` package],
)
#set heading(numbering: "1.")
#align(center, text(16pt)[*The `canvas` package*])
#let linkurl(url, t) = {
link(url)[#underline(text(fill: blue, t))]
}
#align(center)[
Johannes Wolf and fenjalien \
#linkurl("https://github.com/johannes-wolf/typst-canvas", "https://github.com/johannes-wolf/typst-canvas") \
#linkurl("https://github.com/fenjalien/typst-canvas", "https://github.com/fenjalien/typst-canvas")
]
#set par(justify: true)
#outline(indent: true)
#pagebreak(weak: true)
= Introduction
This package provides a way to draw stuff using a similar API to #linkurl("https://processing.org/", "Processing") but with relative coordinates and anchors from #linkurl("https://tikz.dev/", "Tikz"). You also won't have to worry about accidentally drawing over other content as the canvas will automatically resize. And remember: up is positive!
= Usage
This is the minimal starting point:
```typ
#import "typst-canvas/canvas.typ": canvas
#canvas({
import "typst-canvas/draw.typ": *
...
})
```
Note that draw functions are imported inside the scope of the `canvas` block. This is recommended as draw functions override Typst's functions such as `line`.
== Coordinates
There are four different ways to specify coordinates.
+ Absolute: `(x,y)` \
"`x` units to the right and `y` units down from the origin."
+ Relative: `(rel: (x,y))` \
"`x` units to the right and `y` units down from the previous coordinate."
+ Previous: `()` \
"The previous coordinate."
+ Anchor: `(node: "name", at: "example")` or `"name.example"` \
"The position of anchor `"example"` on node with name `"name"`." \
See @anchors
+ Angle + Distance: `(45deg, 1)` \
"Angle and distance from `(0, 0)`"
== Anchors <anchors>
Anchors are named positions relative to named elements.
To use an anchor of an element, you must give the element a name using the `name` parameter.
#example({
import "draw.typ": *
circle((0,0), name: "circle")
fill(red)
stroke(none)
circle("circle.left", radius: 0.3)
},
[
```typ
#canvas({
import "typst-canvas/draw.typ": *
// Name the circle
circle((0,0), name: "circle")
// Draw a smaller red circle at "circle"'s left anchor
fill(red)
stroke(none)
circle("circle.left", radius: 0.3)
})
```
]
)
All elements will have default anchors based on their bounding box, they are: `center`, `left`, `right`, `above`/`top` and `below`/`bottom`, `top-left`, `top-right`, `bottom-left`, `bottom-right`. Some elements will have their own anchors.
Elements can be placed relative to their own anchors.
#example({
import "draw.typ": *
circle((0,0), anchor: "left")
fill(red)
stroke(none)
circle((0,0), radius: 0.3)
},
[
```typ
#canvas({
import "typst-canvas/draw.typ": *
// An element does not have to be named
// in order to use its own anchors.
circle((0,0), anchor: "left")
// Draw a smaller red circle at the origin
fill(red)
stroke(none)
circle((0,0), radius: 0.3)
})
```
]
)
#v(1cm)
= Reference
#set terms(indent: 4em)
```typ
#canvas(background: none, length: 1cm, debug: false, body)
```
/ `background`: A `color` to be used for the background of the canvas.
/ `length`: The `length` used to specify what 1 coordinate unit is.
/ `debug`: Shows the bounding boxes of each element when `true`.
/ `body`: A code block in which functions from `draw.typ` have been called.
== Elements
#br()
```typ
#line(start, end, mark-begin: none, mark-end: none, name: none)
```
Draws a line (a direct path between two points) to the canvas.
/ `start`: The coordinate to start drawing the line from
/ `end`: The coordinate to draw the line to.
/ `mark-begin`: The type of arrow to draw at the start of the line. See @arrow-heads
/ `mark-end`: The type of arrow to draw at the end of the line.
/ `mark-size`: Size of the marks (defaults to `.15`)
#example({
import "draw.typ": *
line((-1.5, 0), (rel: (3, 0)))
line((0, -1.5), (rel: (0, 3)))
},
[
```typ
#canvas({
import "typst-canvas/draw.typ": *
line((-1.5, 0), (rel: (3, 0)))
line((0, -1.5), (rel: (0, 3)))
})
```
])
#br()
```typ
#rect(a, b, name: none)
```
Draws a rectangle to the canvas.
/ a: The top left coordinate of the rectangle.
/ b: The bottom right coordinate of the rectangle.
#example({
import "draw.typ": *
rect((-1.5, 1.5), (1.5, -1.5))
},
[
```typ
#canvas({
import "typst-canvas/draw.typ": *
rect((-1.5, 1.5), (1.5, -1.5))
})
```
])
#br()
```typ
#arc(position, start, stop, radius: 1, name: none, anchor: none)
```
Draws an arc to the canvas.
/ position: The coordinate to start drawing the arc from.
/ start: The angle to start the arc.
/ stop: The angle to stop the arc.
/ radius: The radius of the arc's circle.
#example({
import "draw.typ": *
arc((0,0), 45deg, 135deg)
},
[```typ
#canvas({
import "typst-canvas/draw.typ": *
arc((0,0), 45deg, 135deg)
})
```]
)
#br()
```typ
#circle(center, radius: 1, name: none, anchor: none)
```
Draws an approximated circle to the canvas.
/ center: The coordinate of the circle's origin.
/ radius: The circle's radius or xy radius pair.
#example({
import "draw.typ": *
circle((0,1.5))
circle((0,0), radius: (1, .25))
},
[```typ
#canvas({
import "typst-canvas/draw.typ": *
circle((0,1.5))
circle((0,0), radius: (1, .25))
})
```]
)
#br()
```typ
#bezier(start, end, ..ctrl, samples: 100, name: none)
```
Draws a bezier curve with 1 or 2 control points to the canvas.
/ `start`: The coordinate to start drawing the line from
/ `end`: The coordinate to draw the line to.
/ `..ctrl`: List of control points
/ `samples`: Number of lines used to constuct the curve
#example({
import "draw.typ": *
bezier((0, 0), (2, 0), (1, 1))
bezier((0, -1), (2, -1), (.5, -2), (1.5, 0))
},
[
```typ
#canvas({
import "typst-canvas/draw.typ": *
bezier((0, 0), (2, 0), (1, 1))
bezier((0, -1), (2, -1), (.5, -2), (1.5, 0))
})
```
])
#br()
```typ
#content(pt, ct, angle: 0deg, name: none, anchor: none)
```
Draws a content block to the canvas.
/ pt: The coordinate of the center of the content block.
/ ct: The content block.
/ angle: The angle to rotate the content block by. Uses Typst's `rotate` function.
#example({
import "draw.typ": *
content((0,0), [Hello World!])
},
[```typ
#canvas({
import "typst-canvas/draw.typ": *
content((0,0), [Hello World!])
})
```]
)
== Styles
```typst fill(color) ```
Set current fill color or none.
```typst stroke(stroke) ```
Set current stroke style or none.
== Arrow Heads <arrow-heads>
Arrow heads -- _marks_ -- can be drawn using the `arrow-head` function
or as start/end marks of paths (`line`). Arrow heads are filled using the
current fill color.
#example({
import "draw.typ": *
stroke((paint: gray, dash: "dotted"))
line((1,-1), (1, 5))
stroke(black); fill(black)
line((0, 0), (1, 0), mark-end: ">", mark-size: .5)
line((0, 1), (1, 1), mark-end: "<", mark-size: .5)
line((0, 2), (1, 2), mark-end: "|", mark-size: .5)
line((0, 3), (1, 3), mark-end: "o", mark-size: .5)
line((0, 4), (1, 4), mark-end: "<>", mark-size: .5)
},
[```typ
#canvas({
import "typst-canvas/draw.typ": *
stroke((paint: gray, dash: "dotted"))
line((1,-1), (1, 5))
stroke(black); fill(black)
line((0, 4), (1, 4), mark-end: "<>")
line((0, 3), (1, 3), mark-end: "o")
line((0, 2), (1, 2), mark-end: "|")
line((0, 1), (1, 1), mark-end: "<")
line((0, 0), (1, 0), mark-end: ">")
})
```]
)