-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.ts
123 lines (115 loc) · 3.65 KB
/
renderer.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
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
import type { Renderer } from ".";
export interface RenderContext<T> {
ctx: CanvasRenderingContext2D;
item: T;
renderItemWidth: number;
renderItemHeight: number;
rowGap: number;
columnGap: number;
rowCount: number;
columnCount: number;
width: number;
height: number;
angle: number;
degree: number;
originX: number;
originY: number;
}
export interface CreateTextRendererOptions {
font?: string;
customRender?: (context: RenderContext<string>) => void;
}
export function textRender(context: RenderContext<string>) {
const { ctx, item, renderItemWidth, renderItemHeight, rowGap, columnGap, columnCount, rowCount } = context;
for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
for (let columnIndex = 0; columnIndex < columnCount; columnIndex++) {
const x = columnIndex * (renderItemWidth + columnGap);
const y = rowIndex * (renderItemHeight + rowGap);
ctx.fillText(item, x, y);
}
}
}
export function createTextRenderer(text: string, options?: CreateTextRendererOptions) {
const { font, customRender } = options || {};
const render = customRender ?? textRender;
const renderer: Renderer = (context) => {
const { ctx, ratio, rowGap, columnGap, measureText, degree, angle, calculateRenderCount, calculateTranslate, width, height } = context;
if (font) {
ctx.font = font;
}
ctx.textBaseline = "top";
const { x, y } = calculateTranslate();
ctx.save();
ctx.scale(ratio, ratio);
ctx.translate(x, y);
ctx.rotate(angle);
const { width: renderItemWidth, height: renderItemHeight } = measureText(text);
const [rowCount, columnCount] = calculateRenderCount(renderItemWidth, renderItemHeight);
const renderContext: RenderContext<string> = {
ctx,
item: text,
renderItemWidth,
renderItemHeight,
rowGap,
columnGap,
rowCount,
columnCount,
width,
height,
angle,
degree,
originX: x,
originY: y,
};
render(renderContext);
ctx.restore();
};
return renderer;
}
export interface CreateImageRendererOptions {
customRender?: (context: RenderContext<HTMLImageElement>) => void;
}
export function imageRender(context: RenderContext<HTMLImageElement>) {
const { ctx, item, renderItemWidth, renderItemHeight, rowGap, columnGap, columnCount, rowCount } = context;
for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
for (let columnIndex = 0; columnIndex < columnCount; columnIndex++) {
const x = columnIndex * (renderItemWidth + columnGap);
const y = rowIndex * (renderItemHeight + rowGap);
ctx.drawImage(item, x, y, renderItemWidth, renderItemHeight);
}
}
}
export function createImageRenderer(img: HTMLImageElement, options?: CreateImageRendererOptions) {
const { customRender } = options || {};
const render = customRender ?? imageRender;
const renderer: Renderer = (context) => {
const { ctx, ratio, angle, width, height, rowGap, columnGap, degree, calculateTranslate, calculateRenderCount } = context;
const { x, y } = calculateTranslate();
ctx.save();
ctx.scale(ratio, ratio);
ctx.translate(x, y);
ctx.rotate(angle);
const renderItemWidth = img.width;
const renderItemHeight = img.height;
const [rowCount, columnCount] = calculateRenderCount(renderItemWidth, renderItemHeight);
const renderContext: RenderContext<HTMLImageElement> = {
ctx,
item: img,
renderItemWidth,
renderItemHeight,
rowGap,
columnGap,
rowCount,
columnCount,
width,
height,
angle,
degree,
originX: x,
originY: y,
};
render(renderContext);
ctx.restore();
};
return renderer;
}