mirror of
https://github.com/nestriness/nestri.git
synced 2025-12-13 01:05:37 +02:00
✨ feat: Add api (#112)
Add the api route -> https://nexus.nestri.workers.dev/
This commit is contained in:
33
packages/api/src/utils/create-avatar.tsx
Normal file
33
packages/api/src/utils/create-avatar.tsx
Normal 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>
|
||||
);
|
||||
24
packages/api/src/utils/gradient.ts
Normal file
24
packages/api/src/utils/gradient.ts
Normal 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(),
|
||||
};
|
||||
}
|
||||
2
packages/api/src/utils/index.ts
Normal file
2
packages/api/src/utils/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './gradient'
|
||||
export * from './create-avatar'
|
||||
Reference in New Issue
Block a user