misc: switch back to diecbear

This commit is contained in:
Xylight committed 2026-02-07 22:01:17 -08:00
1 parent f5dd385e11
commit cc04eca248
6 files changed
+47 -85

No files matched your search

+36 -80
View File
@@ -1,90 +1,16 @@
<script lang="ts">
import { findClosestNumber } from '$lib/app/util.svelte'
import { optimizeImageURL } from '$lib/feature/post'
import { createAvatar } from '@dicebear/core'
import * as initials from '@dicebear/initials'
import type { ClassValue } from 'svelte/elements'
const sizes = [16, 24, 32, 48, 64, 96, 128, 256, 512]
const colors = [
'7B68EE',
'FF6347',
'20B2AA',
'DDA0DD',
'F0E68C',
'FF1493',
'4682B4',
'32CD32',
'FFB6C1',
'8B4513',
'00CED1',
'9370DB',
'FFA500',
'2E8B57',
'DC143C',
'BA55D3',
'708090',
'ADFF2F',
'CD853F',
'48D1CC',
]
function simpleHash(str: string): number {
let hash = 0
for (let i = 0; i < str.length; i++) {
hash = (hash << 5) - hash + str.charCodeAt(i)
hash |= 0
}
return Math.abs(hash)
}
function createInitialAvatar(seed: string): string {
const hash = simpleHash(seed)
const color1 = colors[hash % colors.length]
const color2 = colors[(hash + 7) % colors.length]
const initial = (seed[0] || '?').toUpperCase()
const textColor = hash % 2 === 0 ? 'fff' : '000'
return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<defs>
<linearGradient id="bg${hash}" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" style="stop-color:#${color1}"/>
<stop offset="100%" style="stop-color:#${color2}"/>
</linearGradient>
</defs>
<rect width="100" height="100" fill="url(#bg${hash})"/>
<text x="50" y="50" dy=".35em" text-anchor="middle" fill="#${textColor}" font-size="50" font-weight="800" font-family="sans-serif">${initial}</text>
</svg>`
}
const optimizeUrl = (
url: string | undefined,
res: number,
): string | undefined => {
if (url === undefined) return
try {
const urlObj = new URL(url)
urlObj.searchParams.append('format', 'webp')
if (res > -1) {
urlObj.searchParams.append(
'thumbnail',
findClosestNumber(sizes, res).toString(),
)
}
return urlObj.toString()
} catch {
return undefined
}
}
interface Props {
url: string | undefined
url?: string
alt?: string
title?: string
circle?: boolean | null
width: number
res?: number | undefined
res?: number
style?: string
class?: ClassValue
}
@@ -95,7 +21,7 @@
title = '',
circle = true,
width,
res = undefined,
res,
style = '',
class: clazz = '',
...rest
@@ -132,6 +58,36 @@
clazz,
]}
>
{@html createInitialAvatar(alt)}
{@html createAvatar(initials, {
seed: alt,
backgroundType: ['gradientLinear'],
fontWeight: 800,
randomizeIds: true,
chars: 1,
scale: 125,
textColor: ['fff', '000'],
backgroundColor: [
'7B68EE',
'FF6347',
'20B2AA',
'DDA0DD',
'F0E68C',
'FF1493',
'4682B4',
'32CD32',
'FFB6C1',
'8B4513',
'00CED1',
'9370DB',
'FFA500',
'2E8B57',
'DC143C',
'BA55D3',
'708090',
'ADFF2F',
'CD853F',
'48D1CC',
],
}).toString()}
</div>
{/if}