diff --git a/src/components/Account/Avatar.tsx b/src/components/Account/Avatar.tsx index 2d6a677..30250be 100644 --- a/src/components/Account/Avatar.tsx +++ b/src/components/Account/Avatar.tsx @@ -31,6 +31,33 @@ function getPlaceholderBgClass(seed: string | null): string { return PLACEHOLDER_BG_CLASSES[index] } +/** Centre-crop to a square and downscale before upload, so a phone photo doesn't ship at full size. */ +async function prepareAvatar(file: File, maxSize: number): Promise { + try { + if (typeof createImageBitmap !== 'function') return file + const bitmap = await createImageBitmap(file) + const side = Math.min(bitmap.width, bitmap.height) + const out = Math.min(maxSize * 2, side) + const canvas = document.createElement('canvas') + canvas.width = out + canvas.height = out + const ctx = canvas.getContext('2d') + if (!ctx) { + bitmap.close() + return file + } + ctx.imageSmoothingEnabled = true + ctx.imageSmoothingQuality = 'high' + ctx.drawImage(bitmap, (bitmap.width - side) / 2, (bitmap.height - side) / 2, side, side, 0, 0, out, out) + bitmap.close() + const blob = await new Promise((resolve) => canvas.toBlob(resolve, 'image/png')) + if (!blob) return file + return new File([blob], `${file.name.replace(/\.[^.]+$/, '')}.png`, { type: 'image/png' }) + } catch { + return file + } +} + export default function Avatar({ uid, url, @@ -75,7 +102,7 @@ export default function Avatar({ throw new Error('You must select an image to upload.') } - const file = event.target.files[0] + const file = await prepareAvatar(event.target.files[0], size) const fileExt = file.name.split('.').pop() const filePath = `${uid}-${Math.random()}.${fileExt}` diff --git a/src/components/HackCard.tsx b/src/components/HackCard.tsx index 990185d..066e857 100644 --- a/src/components/HackCard.tsx +++ b/src/components/HackCard.tsx @@ -169,20 +169,27 @@ function Shots({ images, platform, fill }: { images: string[]; platform?: Platfo > {fill && ( - {images.map((src, i) => ( - { - backdropEls.current[i] = el; - }} - src={src} - alt="" - loading="lazy" - draggable={false} - style={{ "--fade": i === 0 ? 1 : 0 } as CSSProperties} - className="absolute -inset-6 h-[calc(100%+48px)] w-[calc(100%+48px)] max-w-none object-cover blur-[18px] saturate-[1.15] [opacity:calc(var(--fade)*.45)]" - /> - ))} + {images.map((src, i) => { + // Only the active shot and its neighbours can be seen through the backdrop. Mounting the + // rest would fetch every screenshot in the catalog to fade in something already at zero. + const distance = Math.abs(i - index); + if (Math.min(distance, images.length - distance) > 2) return null; + return ( + { + backdropEls.current[i] = el; + }} + src={src} + alt="" + loading="lazy" + decoding="async" + draggable={false} + style={{ "--fade": i === index ? 1 : 0 } as CSSProperties} + className="absolute -inset-6 h-[calc(100%+48px)] w-[calc(100%+48px)] max-w-none object-cover blur-[18px] saturate-[1.15] [opacity:calc(var(--fade)*.45)]" + /> + ); + })} )} @@ -200,6 +207,7 @@ function Shots({ images, platform, fill }: { images: string[]; platform?: Platfo src={src} alt="" loading="lazy" + decoding="async" draggable={false} onLoad={(e) => snapRendering(e.currentTarget)} className={`block h-auto w-full ${ratio} rounded-frame object-cover object-top ${fill ? "max-md:shadow-[0_4px_14px_rgba(0,0,0,.35)]" : ""}`}