feat: Add api (#112)

Add the api route -> https://nexus.nestri.workers.dev/
This commit is contained in:
Wanjohi
2024-09-10 22:38:35 +03:00
committed by GitHub
parent df9f1cfe95
commit 1b1bedff36
21 changed files with 37 additions and 8 deletions

View File

@@ -0,0 +1,33 @@
export const createAvatarSvg = (size: number, gradient: { fromColor: string, toColor: string }, fileType?: string, text?: string) => (
<svg
width={size}
height={size}
viewBox={`0 0 ${size} ${size}`}
version="1.1"
xmlns="http://www.w3.org/2000/svg"
>
<g>
<defs>
<linearGradient id="gradient" x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" stopColor={gradient.fromColor} />
<stop offset="100%" stopColor={gradient.toColor} />
</linearGradient>
</defs>
<rect fill="url(#gradient)" x="0" y="0" width={size} height={size} />
{fileType === "svg" && text && (
<text
x="50%"
y="50%"
alignmentBaseline="central"
dominantBaseline="central"
textAnchor="middle"
fill="#fff"
fontFamily="sans-serif"
fontSize={(size * 0.9) / text.length}
>
{text}
</text>
)}
</g>
</svg>
);

View File

@@ -0,0 +1,24 @@
import color from "tinycolor2";
function fnv1a(str: string) {
let hash = 0x811c9dc5;
for (let i = 0; i < str.length; i++) {
hash ^= str.charCodeAt(i);
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
}
return hash >>> 0;
}
export function generateGradient(username: string) {
const hash = fnv1a(username);
const hue1 = hash % 360;
const hue2 = (hash >> 16) % 360;
const c1 = color({ h: hue1, s: 0.8, l: 0.6 });
const c2 = color({ h: hue2, s: 0.8, l: 0.5 });
return {
fromColor: c1.toHexString(),
toColor: c2.toHexString(),
};
}

View File

@@ -0,0 +1,2 @@
export * from './gradient'
export * from './create-avatar'