-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
80 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,107 @@ | ||
import { type } from "os"; | ||
import { cacheable } from "./cacheable"; | ||
|
||
type Size = { | ||
width: number; | ||
height: number; | ||
width: number; | ||
height: number; | ||
}; | ||
|
||
type ImageSchema = { | ||
id: number; | ||
alt: string; | ||
src: string; | ||
width: number; | ||
height: number; | ||
id: number; | ||
alt: string; | ||
src: string; | ||
width: number; | ||
height: number; | ||
}; | ||
|
||
type Slide = { | ||
id?: string | number; | ||
src?: string; | ||
alt?: string; | ||
width?: string | number; | ||
height?: string | number; | ||
[key: string]: unknown; | ||
id?: string | number; | ||
src?: string; | ||
alt?: string; | ||
width?: string | number; | ||
height?: string | number; | ||
[key: string]: unknown; | ||
}; | ||
|
||
export const images = createImages(); | ||
|
||
function createImages() { | ||
type API = [string, number, string]; | ||
const { subscribe, set, get, update } = cacheable<Array<API>>( | ||
"County_Images", | ||
[], | ||
true, | ||
); | ||
type API = [string, number, string]; | ||
const { subscribe, set, get, update } = cacheable<Array<API>>( | ||
"County_Images", | ||
[], | ||
true, | ||
); | ||
|
||
async function load() { | ||
if (!get().length) { | ||
const url = "./assets/photos.json"; | ||
const res = await fetch(url); | ||
set(await res.json()); | ||
async function load() { | ||
if (!get().length) { | ||
const url = "./assets/photos.json"; | ||
const res = await fetch(url); | ||
set(await res.json()); | ||
} | ||
back(); | ||
} | ||
back(); | ||
} | ||
|
||
function back() { | ||
const [{ src, alt }] = prepare(); | ||
// await addImageProcess(src) | ||
// const img = new Image(); | ||
// img.src = src | ||
// await img.decode(); | ||
document.documentElement.style.cssText = ` | ||
function back() { | ||
const [{ src, alt }] = prepare(); | ||
// await addImageProcess(src) | ||
// const img = new Image(); | ||
// img.src = src | ||
// await img.decode(); | ||
document.documentElement.style.cssText = ` | ||
background: url(${src}) center no-repeat; | ||
background-size: cover; | ||
`; | ||
document.documentElement.title = alt; | ||
} | ||
document.documentElement.title = alt; | ||
} | ||
|
||
function prepare( | ||
limit = 1, | ||
size = { width: window.innerWidth, height: window.innerHeight }, | ||
) { | ||
const indexes = Array.from({ length: limit }, () => | ||
Math.floor(Math.random() * 24644), | ||
); | ||
return get().reduce( | ||
(acc: ImageSchema[], [src, aspectRatio, author], id) => { | ||
if (indexes.includes(id)) { | ||
const source = { | ||
width: size.height * (aspectRatio / 10), | ||
height: size.height, | ||
}; | ||
const max = { width: size.width, height: size.height }; | ||
const query = `?w=${ratio(applyRatio(source, max).width)}`; | ||
function prepare( | ||
limit = 1, | ||
size = { width: window.innerWidth, height: window.innerHeight }, | ||
) { | ||
const indexes = Array.from({ length: limit }, () => | ||
Math.floor(Math.random() * 24644), | ||
); | ||
return get().reduce( | ||
(acc: ImageSchema[], [src, aspectRatio, author], id) => { | ||
if (indexes.includes(id)) { | ||
const source = { | ||
width: size.height * (aspectRatio / 10), | ||
height: size.height, | ||
}; | ||
const max = { width: size.width, height: size.height }; | ||
const query = `?w=${ratio(applyRatio(source, max).width)}`; | ||
|
||
acc.push({ | ||
id, | ||
src: `https://images.unsplash.com/photo-${src}${query}`, | ||
alt: `Image by ${author} from Unsplash`, | ||
...applyRatio(source, max), | ||
}); | ||
} | ||
return acc; | ||
}, | ||
[], | ||
); | ||
acc.push({ | ||
id, | ||
src: `https://images.unsplash.com/photo-${src}${query}`, | ||
alt: `Image by ${author} from Unsplash`, | ||
...applyRatio(source, max), | ||
}); | ||
} | ||
return acc; | ||
}, | ||
[], | ||
); | ||
|
||
function ratio(size: number) { | ||
return size * devicePixelRatio; | ||
} | ||
function ratio(size: number) { | ||
return size * devicePixelRatio; | ||
} | ||
|
||
function applyRatio(src: Size, size: Size): Size { | ||
const ratio = Math.min(size.width / src.width, size.height, src.height); | ||
return { | ||
width: Math.round(src.width * ratio), | ||
height: Math.round(src.height * ratio), | ||
}; | ||
function applyRatio(src: Size, size: Size): Size { | ||
const ratio = Math.min(size.width / src.width, size.height, src.height); | ||
return { | ||
width: Math.round(src.width * ratio), | ||
height: Math.round(src.height * ratio), | ||
}; | ||
} | ||
} | ||
} | ||
|
||
return { | ||
subscribe, | ||
set, | ||
update, | ||
load, | ||
prepare, | ||
back, | ||
}; | ||
return { | ||
subscribe, | ||
set, | ||
update, | ||
load, | ||
prepare, | ||
back, | ||
}; | ||
} |