Svelte port of facehash by cossistant.
The current version ports the library code as of commit fce5dc5b65102754798d9d717fe5951d2d45d0c3.
Deterministic avatar faces from any string. Zero dependencies, works with Svelte 5 and SvelteKit.
bun install facehash-sv
<script>
import { Facehash } from 'facehash-sv';
</script>
<Facehash name="[email protected]" />
Generate SVG avatar images via API endpoint
// src/routes/api/avatar/+server.ts
import { toFacehashHandler } from 'facehash-sv/sveltekit';
export const GET = toFacehashHandler();
Then use it:
GET /api/[email protected]
GET /api/avatar?name=john&size=200&variant=solid
Returns an SVG image. Cached for 1 year by default.
| Prop | Type | Default | Description |
|---|---|---|---|
name |
string |
Required | String to generate face from |
size |
number | string |
40 |
Size in pixels or CSS units |
colors |
string[] |
- | Array of hex/hsl colors |
colorClasses |
string[] |
- | Array of Tailwind classes (use instead of colors) |
variant |
"gradient" | "solid" |
"gradient" |
Background style |
intensity3d |
"none" | "subtle" | "medium" | "dramatic" |
"dramatic" |
3D rotation effect |
interactive |
boolean |
true |
Animate on hover |
showInitial |
boolean |
true |
Show first letter below face |
<Facehash name="alice" colors={['#264653', '#2a9d8f', '#e9c46a', '#f4a261', '#e76f51']} />
<Facehash
name="bob"
colorClasses={['bg-pink-500', 'bg-blue-500', 'bg-green-500']}
class="rounded-full"
/>
<Facehash name="charlie" intensity3d="none" variant="solid" />
<Facehash name="diana" showInitial={false} />
For image avatars that fall back to Facehash when the image fails:
<script>
import { Avatar, AvatarImage, AvatarFallback } from 'facehash-sv';
</script>
<Avatar style="width: 40px; height: 40px; border-radius: 50%; overflow: hidden;">
<AvatarImage src="/photo.jpg" alt="User" />
<AvatarFallback name="[email protected]" />
</Avatar>
AvatarFallback renders a Facehash by default. For initials instead:
<AvatarFallback name="John Doe" facehash={false} />
The facehash-sv/sveltekit export provides a route handler factory for generating avatar images server-side. This is useful for:
// src/routes/api/avatar/+server.ts
import { toFacehashHandler } from 'facehash-sv/sveltekit';
export const GET = toFacehashHandler();
export const GET = toFacehashHandler({
size: 200, // Default image size (default: 400)
variant: 'solid', // "gradient" | "solid" (default: "gradient")
showInitial: false, // Show first letter (default: true)
colors: ['#ff6b6b', '#4ecdc4', '#45b7d1'], // Custom color palette
cacheControl: 'public, max-age=86400' // Custom cache header
});
All options can be overridden via URL query parameters:
| Parameter | Type | Description |
|---|---|---|
name |
string |
Required. String to generate avatar from |
size |
number |
Image size in pixels (16-2000) |
variant |
string |
"gradient" or "solid" |
showInitial |
boolean |
"true" or "false" |
colors |
string |
Comma-separated hex colors (e.g., #ff0000,#00ff00) |
/api/[email protected]
/api/avatar?name=Alice&size=128
/api/avatar?name=Bob&variant=solid&showInitial=false
/api/avatar?name=Team&colors=%23ff6b6b,%234ecdc4,%2345b7d1
By default, responses include Cache-Control: public, max-age=31536000, immutable (1 year). Same name always generates the same image, so aggressive caching is safe.
// Main component
import { Facehash } from 'facehash-sv';
// Avatar compound components
import { Avatar, AvatarImage, AvatarFallback } from 'facehash-sv';
// Individual face components
import { RoundFace, CrossFace, LineFace, CurvedFace, FACES } from 'facehash-sv';
// Core utilities
import { stringHash, computeFacehash, getColor, DEFAULT_COLORS } from 'facehash-sv';
// Types
import type { FacehashProps, Variant, Intensity3D } from 'facehash-sv';
import type { AvatarContextValue, ImageLoadingStatus } from 'facehash-sv';
// SvelteKit route handler
import { toFacehashHandler } from 'facehash-sv/sveltekit';
import type { FacehashHandlerOptions } from 'facehash-sv/sveltekit';