Updated background component

This commit is contained in:
Patrick Alvin Alcala 2025-08-26 18:00:27 +08:00
parent aa50f18dc5
commit ff96ee6ace

View file

@ -1,21 +1,40 @@
import './Background.sass' import './Background.sass'
import { Show } from 'solid-js' import { Show, createSignal } from 'solid-js'
import backgroundAvif from '../../assets/images/background.avif' // import backgroundAvif from '../../assets/images/background.avif'
import backgroundWebp from '../../assets/images/background.webp' // import backgroundWebp from '../../assets/images/background.webp'
import sharp from 'sharp'
import fs from 'fs'
interface Props { interface Props {
image?: boolean image?: boolean
color?: string color?: string
} }
const convertBackground = async (props: Props) => {
const webpOutputPath = 'src/assets/compressed-images/background.webp'
const avifOutputPath = 'src/assets/compressed-images/background.avif'
const inputPath = 'src/assets/images/background.png'
if (!fs.existsSync(webpOutputPath) || !fs.existsSync(avifOutputPath)) {
const webpBuffer = await sharp(inputPath).webp({ quality: 75 }).resize(1920).toBuffer()
await sharp(webpBuffer).toFile(webpOutputPath)
const avifBuffer = await sharp(inputPath).avif({ quality: 60 }).resize(1920).toBuffer()
await sharp(avifBuffer).toFile(avifOutputPath)
}
}
export default (props: Props) => { export default (props: Props) => {
let [imageSrc] = createSignal('src/assets/compressed-images/background.webp')
convertBackground(props)
return ( return (
<> <>
<Show when={props.image}> <Show when={props.image}>
<picture class="fullscreen"> <picture class="fullscreen">
<source srcset={backgroundAvif.src} type="image/avif" /> <source srcset={imageSrc().replace(/\.webp$/, '.avif')} type="image/avif" />
<source srcset={backgroundWebp.src} type="image/webp" /> <source srcset={imageSrc()} type="image/webp" />
<img src={backgroundWebp.src} width="1920" height="auto" decoding="async" loading="lazy" alt="An image background" /> <img src={imageSrc()} width="1920" height="auto" decoding="async" loading="lazy" alt="An image background" />
</picture> </picture>
</Show> </Show>