|
1 | | -import { Town } from '../town/_common' |
2 | | - |
3 | 1 | type Illustration = |
4 | 2 | | 'general-store-illustration' |
5 | 3 | | 'tavern-illustration' |
6 | 4 | | 'town-illustration' |
7 | 5 | | 'city-illustration' |
8 | 6 |
|
9 | | -export const getIllustration = (illustration: Illustration, alt: string) => { |
10 | | - let img = '<img ' |
11 | | - img += 'id="illustration" ' |
12 | | - const path = getPath() |
13 | | - |
14 | | - img += `src="${path}src/Resources/img/hero/${illustration}.jpg" ` |
15 | | - // If it's in production, then we can add the srcset options, otherwise we might as well omit it. |
16 | | - if (process.env.NODE_ENV === 'production' && location.origin !== 'file://') img += `srcset="${path}src/Resources/img/hero/${illustration}-x360.jpg 360w, ${path}src/Resources/img/hero/${illustration}-x411.jpg 411w, ${path}src/Resources/img/hero/${illustration}-x500.jpg 500w, ${path}src/Resources/img/hero/${illustration}-x576.jpg 576w, ${path}src/Resources/img/hero/${illustration}-x768.jpg 768w, ${path}src/Resources/img/hero/${illustration}-x992.jpg 992w, ${path}src/Resources/img/hero/${illustration}-x1200.jpg 1200w, ${path}src/Resources/img/hero/${illustration}.jpg" ` |
17 | | - img += `alt="${alt || `An image depicting ${lib.articles.output(illustration)}, created by artist Juho Huttunen.`}" ` |
18 | | - img += '/>' |
19 | | - return img |
20 | | -} |
21 | | - |
22 | 7 | const getPath = () => { |
23 | 8 | if (process.env.NODE_ENV === 'production' && location.origin !== 'file://') { |
24 | 9 | return './' |
25 | 10 | } |
26 | 11 | return '../' |
27 | 12 | } |
28 | 13 |
|
29 | | -// It would obviously be preferable to output actual <img>s instead of having it render via SugarCube. |
30 | | -// Unfortunately, I am not a clever man, and cannot figure it out. |
31 | | -export const getImage = (illustration: Illustration) => { |
| 14 | +const addLocalSourceSet = (illustration: Illustration, sizes: string[] = ['360', '411', '500', '576', '768', '992', '1200']) => { |
| 15 | + let img = '' |
| 16 | + const path = getPath() |
| 17 | + for (const size of sizes) { |
| 18 | + img += `${path}src/Resources/img/hero/${illustration}-x${size}.jpg ${size}w, ` |
| 19 | + } |
| 20 | + return img |
| 21 | +} |
| 22 | + |
| 23 | +export const getCustomImage = (url: URL) => { |
| 24 | + const img = document.createElement('img') |
| 25 | + img.id = 'illustration' |
| 26 | + img.src = url as unknown as string |
| 27 | + img.alt = 'A custom-defined image.' |
| 28 | + return img.outerHTML |
| 29 | +} |
| 30 | + |
| 31 | +export const getLocalImage = (illustration: Illustration) => { |
32 | 32 | const img = document.createElement('img') |
33 | 33 | const path = getPath() |
34 | 34 | img.id = 'illustration' |
35 | 35 | img.src = `${path}src/Resources/img/hero/${illustration}.jpg` |
36 | | - img.srcset = `${path}src/Resources/img/hero/${illustration}-x360.jpg 360w, ${path}src/Resources/img/hero/${illustration}-x411.jpg 411w, ${path}src/Resources/img/hero/${illustration}-x500.jpg 500w, ${path}src/Resources/img/hero/${illustration}-x576.jpg 576w, ${path}src/Resources/img/hero/${illustration}-x768.jpg 768w, ${path}src/Resources/img/hero/${illustration}-x992.jpg 992w, ${path}src/Resources/img/hero/${illustration}-x1200.jpg 1200w, ${path}src/Resources/img/hero/${illustration}.jpg` |
| 36 | + if (process.env.NODE_ENV === 'production' && location.origin !== 'file://') img.srcset = `${addLocalSourceSet(illustration)} ${path}src/Resources/img/hero/${illustration}.jpg` |
37 | 37 | img.alt = `An image depicting ${lib.articles.output(illustration)}, created by artist Juho Huttunen.` |
38 | | - return img |
39 | | -} |
40 | | - |
41 | | -export const townOrCity = (town: Town) => { |
42 | | - if (town.type === 'city' || town.type === 'town') return 'city-illustration' |
43 | | - return 'town-illustration' |
| 38 | + return img.outerHTML |
44 | 39 | } |
0 commit comments