-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.ts
67 lines (63 loc) · 1.32 KB
/
block.ts
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
import { BlockInfo } from "../layout";
import { DrawLineOptions, drawLine } from "./line";
import { transformPosition2D } from "./position-math";
import {
Axis,
Placement1D,
Placement2D,
Position2D,
placement1Dto2D,
} from "./types";
/**
* Options for {@link drawBlock}
*
* @group Draw
*/
export interface DrawBlockOptions extends DrawLineOptions {
/**
* Alignment of the text within the block
*
* @default Placement1D.Left
*/
textAlignment?: Placement1D;
}
/**
* Takes a laid-out block of text lines from {@link layoutBlock},
* and draws it onto the canvas.
*
* @group Draw
*/
export const drawBlock = (
ctx: CanvasRenderingContext2D,
block: BlockInfo,
position: Position2D,
{
textAlignment = Placement1D.Start,
drawFn,
alignment,
containerSize,
origin,
}: DrawBlockOptions = {},
) => {
const { x: blockX, y: initialBlockY } = transformPosition2D(position, block, {
alignment,
containerSize,
origin,
});
let accY = initialBlockY;
for (const line of block.lines) {
drawLine(
ctx,
line,
{ x: blockX, y: accY },
{
drawFn,
alignment:
Placement2D.Top | placement1Dto2D(textAlignment, Axis.Horizontal),
containerSize,
origin: Placement2D.TopLeft,
},
);
accY += line.height;
}
};