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

Add: optional noise filter, Add: solid color support, Fix: Text size for single letters, Fix: vertical centering #41

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 24 additions & 5 deletions pages/api/avatar/[name].tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ImageResponse } from "@vercel/og";
import { NextRequest, NextResponse } from "next/server";
import { renderToReadableStream } from "react-dom/server";
import { generateGradient } from "../../../utils/gradient";
import { generateGradient, generateSolidColor } from "../../../utils/gradient";

export const config = {
runtime: "experimental-edge",
Expand All @@ -11,12 +11,14 @@ export default async function (req: NextRequest, res: NextResponse) {
const url = new URL(req.url);
const name = url.searchParams.get("name");
const text = url.searchParams.get("text");
const noise = url.searchParams.get("noise");
const solid = url.searchParams.get("solid");
const size = Number(url.searchParams.get("size") || "120");
const [username, type] = name?.split(".") || [];
const fileType = type?.includes("svg") ? "svg" : "png";

const gradient = generateGradient(username || Math.random() + "");

const solidColor = generateSolidColor(username || Math.random() + "");
const avatar = (
<svg
width={size}
Expand All @@ -31,18 +33,35 @@ export default async function (req: NextRequest, res: NextResponse) {
<stop offset="0%" stopColor={gradient.fromColor} />
<stop offset="100%" stopColor={gradient.toColor} />
</linearGradient>

<filter id="noise">
<feTurbulence
type="fractalNoise"
baseFrequency={Math.min(Math.max(size / 150, 0.6), 2)}
result="noisy"
/>
<feColorMatrix type="saturate" values="0" />
<feBlend in="SourceGraphic" in2="noisy" mode="multiply" />
</filter>
</defs>
<rect fill="url(#gradient)" x="0" y="0" width={size} height={size} />
<rect
fill={solid != null ? solidColor.color : "url(#gradient)"}
x="0"
y="0"
width={size}
height={size}
filter={fileType === "svg" && noise !== null ? "url(#noise)" : ""}
/>
{fileType === "svg" && text && (
<text
x="50%"
y="50%"
y={text.length > 1 ? "54%" : "55.5%"}
alignmentBaseline="central"
dominantBaseline="central"
textAnchor="middle"
fill="#fff"
fontFamily="sans-serif"
fontSize={(size * 0.9) / text.length}
fontSize={(size * 0.7) / text.length}
>
{text}
</text>
Expand Down
10 changes: 9 additions & 1 deletion utils/gradient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ export function djb2(str: string) {
}

export function generateGradient(username: string) {
const c1 = color({ h: djb2(username) % 360, s: 0.95, l: 0.5 });
const c1 = color({ h: djb2(username) % 360, s: 0.8, l: 0.6 });
const second = c1.triad()[1].toHexString();

return {
fromColor: c1.toHexString(),
toColor: second,
};
}

export function generateSolidColor(username: string) {
const c = color({ h: djb2(username) % 360, s: 0.7, l: 0.65 });

return {
color: c.toHexString(),
};
}