-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas-writer.test.ts
More file actions
184 lines (167 loc) · 6.15 KB
/
canvas-writer.test.ts
File metadata and controls
184 lines (167 loc) · 6.15 KB
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
import { test, expect } from "@playwright/test";
import { setupPage } from "../helpers.js";
function expectMostlyRed(pixel: number[]): void {
expect(pixel[0]).toBeGreaterThan(180);
expect(pixel[1]).toBeLessThan(80);
expect(pixel[2]).toBeLessThan(80);
expect(pixel[3]).toBeGreaterThan(200);
}
function expectMostlyWhite(pixel: number[]): void {
expect(pixel[0]).toBeGreaterThan(220);
expect(pixel[1]).toBeGreaterThan(220);
expect(pixel[2]).toBeGreaterThan(220);
expect(pixel[3]).toBeGreaterThan(200);
}
test.describe("CanvasWriter", () => {
test("renders IR directly to an HTML canvas", async ({ page }) => {
await setupPage(
page,
`<html><body style="margin:0;padding:0;">
<div id="target" style="width:40px;height:20px;background:#ff0000;"></div>
</body></html>`
);
const result = await page.evaluate(async () => {
const el = document.getElementById("target")!;
const ir = await (window as any).__HC.extractIR(el, {
boxType: "border",
includeText: false,
});
const writer = new (window as any).__HC.CanvasWriter({ width: 40, height: 20 });
const canvas = await (window as any).__HC.renderIR(ir, writer);
const pixel = Array.from(canvas.getContext("2d")!.getImageData(20, 10, 1, 1).data);
return { width: canvas.width, height: canvas.height, pixel };
});
expect(result.width).toBe(40);
expect(result.height).toBe(20);
expectMostlyRed(result.pixel);
});
test("preserves the full CSS font-family stack when rendering text", async ({ page }) => {
await setupPage(page, `<html><body style="margin:0;padding:0;"></body></html>`);
const result = await page.evaluate(async () => {
const originalFillText = CanvasRenderingContext2D.prototype.fillText;
let capturedFont = "";
CanvasRenderingContext2D.prototype.fillText = function (...args) {
capturedFont = this.font;
return originalFillText.apply(this, args as [string, number, number, number?]);
};
try {
const writer = new (window as any).__HC.CanvasWriter({ width: 220, height: 80 });
await writer.begin();
await writer.drawText(
[
{ x: 20, y: 20 },
{ x: 200, y: 20 },
{ x: 200, y: 52 },
{ x: 20, y: 52 },
],
"Stack",
{
color: "rgb(0, 0, 0)",
fontFamily: '"Mona Sans", "Segoe UI", sans-serif',
fontSize: "24px",
fontWeight: "700",
fontStyle: "italic",
},
);
const canvas = await writer.end();
return {
capturedFont,
width: canvas.width,
};
} finally {
CanvasRenderingContext2D.prototype.fillText = originalFillText;
}
});
expect(result.width).toBe(220);
expect(result.capturedFont).toContain('italic');
expect(result.capturedFont).toMatch(/\b(?:700|bold)\b/);
expect(result.capturedFont).toContain('"Mona Sans", "Segoe UI", sans-serif');
});
test("renders gradient strokes on polylines", async ({ page }) => {
await setupPage(page, `<html><body style="margin:0;padding:0;"></body></html>`);
const result = await page.evaluate(async () => {
const writer = new (window as any).__HC.CanvasWriter({ width: 120, height: 30 });
await writer.begin();
await writer.drawPolyline(
[
{ x: 10, y: 15 },
{ x: 110, y: 15 },
],
false,
{
stroke: "rgb(255, 0, 0)",
strokeImage: "linear-gradient(90deg, rgb(255, 0, 0) 0%, rgb(0, 0, 255) 100%)",
strokeWidth: "8px",
},
);
const canvas = await writer.end();
const ctx = canvas.getContext("2d")!;
return {
left: Array.from(ctx.getImageData(15, 15, 1, 1).data),
right: Array.from(ctx.getImageData(95, 15, 1, 1).data),
};
});
expect(result.left[0]).toBeGreaterThan(result.left[2]);
expect(result.right[2]).toBeGreaterThan(result.right[0]);
expect(result.left[3]).toBeGreaterThan(200);
expect(result.right[3]).toBeGreaterThan(200);
});
const clipCases = [
{
name: "inset()",
clipPath: "inset(20px 10px 30px 40px round 8px)",
inside: [50, 30],
outside: [5, 5],
},
{
name: "circle()",
clipPath: "circle(30px at 50px 50px)",
inside: [50, 50],
outside: [10, 10],
},
{
name: "ellipse()",
clipPath: "ellipse(20px 30px at 50px 50px)",
inside: [50, 50],
outside: [10, 10],
},
{
name: "polygon()",
clipPath: "polygon(50% 0%, 100% 100%, 0% 100%)",
inside: [50, 80],
outside: [10, 10],
},
{
name: "path()",
clipPath: "path(evenodd, 'M0 0 H100 V100 H0 Z M25 25 H75 V75 H25 Z')",
inside: [10, 10],
outside: [50, 50],
},
] as const;
for (const clipCase of clipCases) {
test(`applies ${clipCase.name} clip-path when rendering to canvas`, async ({ page }) => {
await setupPage(
page,
`<html><body style="margin:0;padding:0;">
<div id="target" style="width:100px;height:100px;background:#ff0000;clip-path:${clipCase.clipPath};"></div>
</body></html>`
);
const result = await page.evaluate(async ({ inside, outside }) => {
const el = document.getElementById("target")!;
const ir = await (window as any).__HC.extractIR(el, {
boxType: "border",
includeText: false,
});
const writer = new (window as any).__HC.CanvasWriter({ width: 100, height: 100 });
const canvas = await (window as any).__HC.renderIR(ir, writer);
const ctx = canvas.getContext("2d")!;
return {
inside: Array.from(ctx.getImageData(inside[0], inside[1], 1, 1).data),
outside: Array.from(ctx.getImageData(outside[0], outside[1], 1, 1).data),
};
}, { inside: clipCase.inside, outside: clipCase.outside });
expectMostlyRed(result.inside);
expectMostlyWhite(result.outside);
});
}
});