Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Play a bit with og-image #1166

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions packages/og-image/src/handler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,28 @@ export async function handler(request: Request): Promise<Response> {

const rawSvg = await toSVG(
<div tw="flex bg-neutral-900 h-full flex-col w-full items-center justify-center text-center text-white p-10">
<LeftCircle tw="absolute left-0 top-0" color={product.primaryColor} />
<RightCircle tw="absolute right-0" color={product.primaryColor} />
<LeftCircle tw="absolute left-[-25px] top-[-120px]" color={product.primaryColor} />
<RightCircle tw="absolute right-[-100px]" color={product.primaryColor} />
<RightSmallCircle
tw="absolute right-0 opacity-80"
tw="absolute left-[-25px] bottom-[-200px] opacity-80"
color={product.primaryColor && shade(product.primaryColor, 100)}
/>
<product.logo style={{ width: 102, height: 108 }} />
<span tw="font-bold text-7xl my-5">{product.name}</span>
<div tw="flex flex-row items-center justify-center my-12">
<product.logo style={{ width: 102, height: 108 }} />
<div tw="font-semibold text-7xl ml-6">{product.name}</div>
</div>
{title && (
// @ts-expect-error This isn't a valid CSS property supported by browsers yet.
<span tw="font-bold text-5xl" style={{ textWrap: title.includes(' ') ? 'balance' : '' }}>
<span
tw="font-regular text-5xl mt-4"
style={{ textWrap: title.includes(' ') ? 'balance' : '' }}
>
{title}
</span>
)}
{extra && (
<span
tw="font-bold text-2xl mt-4"
tw="font-regular text-2xl mt-4"
// @ts-expect-error This isn't a valid CSS property supported by browsers yet.
style={{ textWrap: extra.includes(' ') ? 'balance' : '' }}
>
Expand Down
24 changes: 18 additions & 6 deletions packages/og-image/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,33 @@ export async function loadGoogleFont({
return fetch(fontUrl).then(res => res.arrayBuffer());
}

let notoSans: Promise<ArrayBuffer>;
let font400: Promise<ArrayBuffer>;
let font600: Promise<ArrayBuffer>;
let init = false;

export async function toSVG(node: ReactNode): Promise<string> {
if (!init) {
notoSans = await loadGoogleFont({
family: 'Noto Sans JP',
weight: 400,
});
const [f400, f600] = await Promise.all([
loadGoogleFont({
family: 'Inter',
weight: 400,
}),
loadGoogleFont({
family: 'Inter',
weight: 600,
}),
]);
font400 = f400;
font600 = f600;
await initWasm(resvgWasm);
init = true;
}
return satori(node, {
width: 1200,
height: 600,
fonts: [{ name: 'NotoSansJP', data: notoSans, weight: 400 }],
fonts: [
{ name: 'Inter', data: font400, weight: 400 },
{ name: 'Inter', data: font600, weight: 600 },
],
});
}