feat: add findClosestNumber for better cache
This commit is contained in:
1 parent
6256d6107d
commit
c4e12b5b30
2 files changed
+8
-1
No files matched your search
@@ -1,7 +1,10 @@
|
||||
<script lang="ts">
|
||||
import { findClosestNumber } from '$lib/util.js'
|
||||
import { createAvatar } from '@dicebear/core'
|
||||
import * as initials from '@dicebear/initials'
|
||||
|
||||
const sizes = [48, 64, 128, 256, 512]
|
||||
|
||||
export let url: string | undefined
|
||||
export let alt: string = ''
|
||||
|
||||
@@ -10,7 +13,7 @@
|
||||
|
||||
{#if url}
|
||||
<img
|
||||
src="{url}?format=webp&thumbnail={width * 2}"
|
||||
src="{url}?format=webp&thumbnail={findClosestNumber(sizes, width)}"
|
||||
{alt}
|
||||
{width}
|
||||
loading="lazy"
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
export const findClosestNumber = (numbers: number[], target: number): number =>
|
||||
numbers.reduce((prev, curr) =>
|
||||
Math.abs(curr - target) < Math.abs(prev - target) ? curr : prev
|
||||
)
|
||||
Reference in new issue
Block a user