MCPcopy Create free account
hub / github.com/bernaferrari/FigmaToCode / createCanvasImageUrl

Function createCanvasImageUrl

packages/plugin-ui/src/lib/utils.ts:8–33  ·  view source on GitHub ↗
(width: number, height: number)

Source from the content-addressed store, hash-verified

6}
7
8const createCanvasImageUrl = (width: number, height: number): string => {
9 const canvas = document.createElement("canvas");
10 canvas.width = width;
11 canvas.height = height;
12
13 const ctx = canvas.getContext("2d");
14 if (!ctx) {
15 return `https://placehold.co/${width}x${height}`;
16 }
17
18 ctx.fillStyle = "#f5f5f5";
19 ctx.fillRect(0, 0, width, height);
20
21 const fontSize = Math.max(12, Math.floor(width * 0.15));
22 ctx.font = `bold ${fontSize}px Inter, Arial, Helvetica, sans-serif`;
23 ctx.fillStyle = "#888888";
24
25 const text = `${width} x ${height}`;
26 const textWidth = ctx.measureText(text).width;
27 const x = (width - textWidth) / 2;
28 const y = (height + fontSize) / 2;
29
30 ctx.fillText(text, x, y);
31
32 return canvas.toDataURL();
33};
34
35export function replaceExternalImagesWithCanvas(html: string): string {
36 return html.replace(

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected