Skip to content

Commit

Permalink
AVIF element images (#18)
Browse files Browse the repository at this point in the history
* use sharp to convert element images to more efficient AVIF in src/fetch-elem-images.ts

* convert existing element images

* ElementPhoto.svelte use .avif extension
  • Loading branch information
janosh authored Mar 16, 2023
1 parent 4773c24 commit a93fee4
Show file tree
Hide file tree
Showing 228 changed files with 30 additions and 21 deletions.
6 changes: 3 additions & 3 deletions contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ npm dev
Before you start committing, create and check out a descriptively named branch:

```sh
git checkout -b <my-cool-new-feature>
git checkout -b my-cool-new-feature
# or
git checkout -b <docs-on-something>
git checkout -b docs-on-something
# or
git checkout -b <test-some-feature>
git checkout -b test-some-feature
```

To ensure your changes didn't break anything, run the full test suite (which also runs in CI):
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"rehype-katex-svelte": "^1.1.2",
"rehype-slug": "^5.1.0",
"remark-math": "3.0.0",
"sharp": "^0.31.3",
"svelte-check": "^3.1.4",
"svelte-preprocess": "^5.0.2",
"svelte-toc": "^0.5.3",
Expand Down
17 changes: 9 additions & 8 deletions src/fetch-elem-images.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/* eslint-disable no-console */
import fs from 'node:fs'
import sharp from 'sharp'
import elements from './lib/element-data.ts'

// to run this script: deno run --allow-all src/fetch-elem-images.ts [report|download|re-download]
// requires brew install deno

// make sure the directory exists
fs.mkdirSync(`./static/elements`, { recursive: true })

Expand All @@ -15,7 +13,6 @@ const fallback_urls: Record<string, string> = {
'107-bohrium': `https://periodiske-system.dk/img/images/lowRes/107.jpg`,
'108-hassium': `https://i0.wp.com/periodic-table.com/wp-content/uploads/2018/12/Hassium.png?w=225&ssl=1`,
'109-meitnerium': `https://www.rsc-cdn.org/www.rsc.org/periodic-table/content/Images/Elements/Meitnerium-L.jpg`,

// '109-meitnerium': `https://cdn1.byjus.com/wp-content/uploads/2018/08/Meitnerium-2.jpg`, // lower res but but looks more like raw crystal
'110-darmstadtium': `https://cdn1.byjus.com/wp-content/uploads/2018/08/Darmstadtium-2.jpg`,
'111-roentgenium': `https://cdn1.byjus.com/wp-content/uploads/2018/08/Roentgenium-2.jpg`,
Expand Down Expand Up @@ -46,23 +43,27 @@ async function download_elem_image(num_name: string) {
}

const buffer = new Uint8Array(await response.arrayBuffer())
fs.writeFileSync(`./static/elements/${num_name}.jpg`, buffer)
// fs.writeFileSync(`./static/elements/${num_name}.jpg`, buffer)
await sharp(buffer).toFile(`./static/elements/${num_name}.avif`)

return url
}

const [action] = Deno.args
const arg = process.argv.find((arg: string) =>
arg.startsWith(`fetch-elem-images:`)
)
const action = arg?.split(`:`)[1]
if (![`report`, `download`, `re-download`].includes(action)) {
throw new Error(
`Usage: deno run --allow-all src/fetch-elem-images.ts [report|download|re-download]`
`Correct usage: vite [dev] fetch-elem-images:[report|download|re-download], got ${arg}\n`
)
}
if (action.endsWith(`download`)) console.log(`Downloading images...`)
if (action === `report`) console.log(`Missing images`)

for (const { name, number } of elements) {
const num_name = `${number}-${name.toLowerCase()}`
const have_img = fs.existsSync(`./static/elements/${num_name}.jpg`)
const have_img = fs.existsSync(`./static/elements/${num_name}.avif`)

if (!have_img || action === `re-download`) {
if (action === `report`) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ElementPhoto.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
$: ({ name, number } = element ?? {})
$: file = `elements/${number}-${name?.toLowerCase()}.jpg`
$: file = `elements/${number}-${name?.toLowerCase()}.avif`
let hidden = false
$: file, (hidden = false) // reset hidden to false when src changes
</script>
Expand Down
18 changes: 9 additions & 9 deletions src/update-site-screenshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ const today = new Date().toISOString().slice(0, 10)

const pages = [
{
url: '/',
url: `/`,
name: `landing-page`,
actions: [['hover', 'a[href="selenium"]']],
actions: [[`hover`, `a[href="selenium"]`]],
},
{
url: '/',
url: `/`,
name: `heatmap`,
actions: [
['click', 'input[placeholder="Select a heat map"]'],
['click', 'ul.options > li:nth-child(2)'],
['click', 'h1'], // close dropdown
['hover', 'a[href="radon"]'],
[`click`, `input[placeholder="Select a heat map"]`],
[`click`, `ul.options > li:nth-child(2)`],
[`click`, `h1`], // close dropdown
[`hover`, `a[href="radon"]`],
],
},
{ url: '/radon', name: `details-page` },
{ url: `/radon`, name: `details-page` },
]

const browser = await puppeteer.launch()
Expand All @@ -32,7 +32,7 @@ for (const { url, name, actions = [] } of pages) {
// increase screenshot resolution
await page.setViewport({ width: 1200, height: 700, deviceScaleFactor: 3 })

await page.goto(`http://localhost:3000${url}`, { waitUntil: 'networkidle2' })
await page.goto(`http://localhost:3000${url}`, { waitUntil: `networkidle2` })
for (const [action, selector] of actions) {
await page.waitForSelector(selector)
await page[action](selector)
Expand Down
Binary file added static/elements/1-hydrogen.avif
Binary file not shown.
Binary file removed static/elements/1-hydrogen.jpg
Binary file not shown.
Binary file added static/elements/10-neon.avif
Binary file not shown.
Binary file removed static/elements/10-neon.jpg
Binary file not shown.
Binary file added static/elements/100-fermium.avif
Binary file not shown.
Binary file removed static/elements/100-fermium.jpg
Binary file not shown.
Binary file added static/elements/101-mendelevium.avif
Binary file not shown.
Binary file removed static/elements/101-mendelevium.jpg
Binary file not shown.
Binary file added static/elements/102-nobelium.avif
Binary file not shown.
Binary file removed static/elements/102-nobelium.jpg
Binary file not shown.
Binary file added static/elements/103-lawrencium.avif
Binary file not shown.
Binary file removed static/elements/103-lawrencium.jpg
Binary file not shown.
Binary file added static/elements/104-rutherfordium.avif
Binary file not shown.
Binary file removed static/elements/104-rutherfordium.jpg
Binary file not shown.
Binary file added static/elements/105-dubnium.avif
Binary file not shown.
Binary file removed static/elements/105-dubnium.jpg
Binary file not shown.
Binary file added static/elements/106-seaborgium.avif
Binary file not shown.
Binary file removed static/elements/106-seaborgium.jpg
Binary file not shown.
Binary file added static/elements/107-bohrium.avif
Binary file not shown.
Binary file removed static/elements/107-bohrium.jpg
Binary file not shown.
Binary file added static/elements/108-hassium.avif
Binary file not shown.
Binary file removed static/elements/108-hassium.jpg
Binary file not shown.
Binary file added static/elements/109-meitnerium.avif
Binary file not shown.
Binary file removed static/elements/109-meitnerium.jpg
Binary file not shown.
Binary file added static/elements/11-sodium.avif
Binary file not shown.
Binary file removed static/elements/11-sodium.jpg
Binary file not shown.
Binary file added static/elements/110-darmstadtium.avif
Binary file not shown.
Binary file removed static/elements/110-darmstadtium.jpg
Binary file not shown.
Binary file added static/elements/111-roentgenium.avif
Binary file not shown.
Binary file removed static/elements/111-roentgenium.jpg
Binary file not shown.
Binary file added static/elements/12-magnesium.avif
Binary file not shown.
Binary file removed static/elements/12-magnesium.jpg
Binary file not shown.
Binary file added static/elements/13-aluminium.avif
Binary file not shown.
Binary file removed static/elements/13-aluminium.jpg
Binary file not shown.
Binary file added static/elements/14-silicon.avif
Binary file not shown.
Binary file removed static/elements/14-silicon.jpg
Binary file not shown.
Binary file added static/elements/15-phosphorus.avif
Binary file not shown.
Binary file removed static/elements/15-phosphorus.jpg
Binary file not shown.
Binary file added static/elements/16-sulfur.avif
Binary file not shown.
Binary file removed static/elements/16-sulfur.jpg
Binary file not shown.
Binary file added static/elements/17-chlorine.avif
Binary file not shown.
Binary file removed static/elements/17-chlorine.jpg
Binary file not shown.
Binary file added static/elements/18-argon.avif
Binary file not shown.
Binary file removed static/elements/18-argon.jpg
Binary file not shown.
Binary file added static/elements/19-potassium.avif
Binary file not shown.
Binary file removed static/elements/19-potassium.jpg
Binary file not shown.
Binary file added static/elements/2-helium.avif
Binary file not shown.
Binary file removed static/elements/2-helium.jpg
Binary file not shown.
Binary file added static/elements/20-calcium.avif
Binary file not shown.
Binary file removed static/elements/20-calcium.jpg
Binary file not shown.
Binary file added static/elements/21-scandium.avif
Binary file not shown.
Binary file removed static/elements/21-scandium.jpg
Diff not rendered.
Binary file added static/elements/22-titanium.avif
Binary file not shown.
Binary file removed static/elements/22-titanium.jpg
Diff not rendered.
Binary file added static/elements/23-vanadium.avif
Binary file not shown.
Binary file removed static/elements/23-vanadium.jpg
Diff not rendered.
Binary file added static/elements/24-chromium.avif
Binary file not shown.
Binary file removed static/elements/24-chromium.jpg
Diff not rendered.
Binary file added static/elements/25-manganese.avif
Binary file not shown.
Binary file removed static/elements/25-manganese.jpg
Diff not rendered.
Binary file added static/elements/26-iron.avif
Binary file not shown.
Binary file removed static/elements/26-iron.jpg
Diff not rendered.
Binary file added static/elements/27-cobalt.avif
Binary file not shown.
Binary file removed static/elements/27-cobalt.jpg
Diff not rendered.
Binary file added static/elements/28-nickel.avif
Binary file not shown.
Binary file removed static/elements/28-nickel.jpg
Diff not rendered.
Binary file added static/elements/29-copper.avif
Binary file not shown.
Binary file removed static/elements/29-copper.jpg
Diff not rendered.
Binary file added static/elements/3-lithium.avif
Binary file not shown.
Binary file removed static/elements/3-lithium.jpg
Diff not rendered.
Binary file added static/elements/30-zinc.avif
Binary file not shown.
Binary file removed static/elements/30-zinc.jpg
Diff not rendered.
Binary file added static/elements/31-gallium.avif
Binary file not shown.
Binary file removed static/elements/31-gallium.jpg
Diff not rendered.
Binary file added static/elements/32-germanium.avif
Binary file not shown.
Binary file removed static/elements/32-germanium.jpg
Diff not rendered.
Binary file added static/elements/33-arsenic.avif
Binary file not shown.
Binary file removed static/elements/33-arsenic.jpg
Diff not rendered.
Binary file added static/elements/34-selenium.avif
Binary file not shown.
Binary file removed static/elements/34-selenium.jpg
Diff not rendered.
Binary file added static/elements/35-bromine.avif
Binary file not shown.
Binary file removed static/elements/35-bromine.jpg
Diff not rendered.
Binary file added static/elements/36-krypton.avif
Binary file not shown.
Binary file removed static/elements/36-krypton.jpg
Diff not rendered.
Binary file added static/elements/37-rubidium.avif
Binary file not shown.
Binary file removed static/elements/37-rubidium.jpg
Diff not rendered.
Binary file added static/elements/38-strontium.avif
Binary file not shown.
Binary file removed static/elements/38-strontium.jpg
Diff not rendered.
Binary file added static/elements/39-yttrium.avif
Binary file not shown.
Binary file removed static/elements/39-yttrium.jpg
Diff not rendered.
Binary file added static/elements/4-beryllium.avif
Binary file not shown.
Binary file removed static/elements/4-beryllium.jpg
Diff not rendered.
Binary file added static/elements/40-zirconium.avif
Binary file not shown.
Binary file removed static/elements/40-zirconium.jpg
Diff not rendered.
Binary file added static/elements/41-niobium.avif
Binary file not shown.
Binary file removed static/elements/41-niobium.jpg
Diff not rendered.
Binary file added static/elements/42-molybdenum.avif
Binary file not shown.
Binary file removed static/elements/42-molybdenum.jpg
Diff not rendered.
Binary file added static/elements/43-technetium.avif
Binary file not shown.
Binary file removed static/elements/43-technetium.jpg
Diff not rendered.
Binary file added static/elements/44-ruthenium.avif
Binary file not shown.
Binary file removed static/elements/44-ruthenium.jpg
Diff not rendered.
Binary file added static/elements/45-rhodium.avif
Binary file not shown.
Binary file removed static/elements/45-rhodium.jpg
Diff not rendered.
Binary file added static/elements/46-palladium.avif
Binary file not shown.
Binary file removed static/elements/46-palladium.jpg
Diff not rendered.
Binary file added static/elements/47-silver.avif
Binary file not shown.
Binary file removed static/elements/47-silver.jpg
Diff not rendered.
Binary file added static/elements/48-cadmium.avif
Binary file not shown.
Binary file removed static/elements/48-cadmium.jpg
Diff not rendered.
Binary file added static/elements/49-indium.avif
Binary file not shown.
Binary file removed static/elements/49-indium.jpg
Diff not rendered.
Binary file added static/elements/5-boron.avif
Binary file not shown.
Binary file removed static/elements/5-boron.jpg
Diff not rendered.
Binary file added static/elements/50-tin.avif
Binary file not shown.
Binary file removed static/elements/50-tin.jpg
Diff not rendered.
Binary file added static/elements/51-antimony.avif
Binary file not shown.
Binary file removed static/elements/51-antimony.jpg
Diff not rendered.
Binary file added static/elements/52-tellurium.avif
Binary file not shown.
Binary file removed static/elements/52-tellurium.jpg
Diff not rendered.
Binary file added static/elements/53-iodine.avif
Binary file not shown.
Binary file removed static/elements/53-iodine.jpg
Diff not rendered.
Binary file added static/elements/54-xenon.avif
Binary file not shown.
Binary file removed static/elements/54-xenon.jpg
Diff not rendered.
Binary file added static/elements/55-cesium.avif
Binary file not shown.
Binary file removed static/elements/55-cesium.jpg
Diff not rendered.
Binary file added static/elements/56-barium.avif
Binary file not shown.
Binary file removed static/elements/56-barium.jpg
Diff not rendered.
Binary file added static/elements/57-lanthanum.avif
Binary file not shown.
Binary file removed static/elements/57-lanthanum.jpg
Diff not rendered.
Binary file added static/elements/58-cerium.avif
Binary file not shown.
Binary file removed static/elements/58-cerium.jpg
Diff not rendered.
Binary file added static/elements/59-praseodymium.avif
Binary file not shown.
Binary file removed static/elements/59-praseodymium.jpg
Diff not rendered.
Binary file added static/elements/6-carbon.avif
Binary file not shown.
Binary file removed static/elements/6-carbon.jpg
Diff not rendered.
Binary file added static/elements/60-neodymium.avif
Binary file not shown.
Binary file removed static/elements/60-neodymium.jpg
Diff not rendered.
Binary file added static/elements/61-promethium.avif
Binary file not shown.
Binary file removed static/elements/61-promethium.jpg
Diff not rendered.
Binary file added static/elements/62-samarium.avif
Binary file not shown.
Binary file removed static/elements/62-samarium.jpg
Diff not rendered.
Binary file added static/elements/63-europium.avif
Binary file not shown.
Binary file removed static/elements/63-europium.jpg
Diff not rendered.
Binary file added static/elements/64-gadolinium.avif
Binary file not shown.
Binary file removed static/elements/64-gadolinium.jpg
Diff not rendered.
Binary file added static/elements/65-terbium.avif
Binary file not shown.
Binary file removed static/elements/65-terbium.jpg
Diff not rendered.
Binary file added static/elements/66-dysprosium.avif
Binary file not shown.
Binary file removed static/elements/66-dysprosium.jpg
Diff not rendered.
Binary file added static/elements/67-holmium.avif
Binary file not shown.
Binary file removed static/elements/67-holmium.jpg
Diff not rendered.
Binary file added static/elements/68-erbium.avif
Binary file not shown.
Binary file removed static/elements/68-erbium.jpg
Diff not rendered.
Binary file added static/elements/69-thulium.avif
Binary file not shown.
Binary file removed static/elements/69-thulium.jpg
Diff not rendered.
Binary file added static/elements/7-nitrogen.avif
Binary file not shown.
Binary file removed static/elements/7-nitrogen.jpg
Diff not rendered.
Binary file added static/elements/70-ytterbium.avif
Binary file not shown.
Binary file removed static/elements/70-ytterbium.jpg
Diff not rendered.
Binary file added static/elements/71-lutetium.avif
Binary file not shown.
Binary file removed static/elements/71-lutetium.jpg
Diff not rendered.
Binary file added static/elements/72-hafnium.avif
Binary file not shown.
Binary file removed static/elements/72-hafnium.jpg
Diff not rendered.
Binary file added static/elements/73-tantalum.avif
Binary file not shown.
Binary file removed static/elements/73-tantalum.jpg
Diff not rendered.
Binary file added static/elements/74-tungsten.avif
Binary file not shown.
Binary file removed static/elements/74-tungsten.jpg
Diff not rendered.
Binary file added static/elements/75-rhenium.avif
Binary file not shown.
Binary file removed static/elements/75-rhenium.jpg
Diff not rendered.
Binary file added static/elements/76-osmium.avif
Binary file not shown.
Binary file removed static/elements/76-osmium.jpg
Diff not rendered.
Binary file added static/elements/77-iridium.avif
Binary file not shown.
Binary file removed static/elements/77-iridium.jpg
Diff not rendered.
Binary file added static/elements/78-platinum.avif
Binary file not shown.
Binary file removed static/elements/78-platinum.jpg
Diff not rendered.
Binary file added static/elements/79-gold.avif
Binary file not shown.
Binary file removed static/elements/79-gold.jpg
Diff not rendered.
Binary file added static/elements/8-oxygen.avif
Binary file not shown.
Binary file removed static/elements/8-oxygen.jpg
Diff not rendered.
Binary file added static/elements/80-mercury.avif
Binary file not shown.
Binary file removed static/elements/80-mercury.jpg
Diff not rendered.
Binary file added static/elements/81-thallium.avif
Binary file not shown.
Binary file removed static/elements/81-thallium.jpg
Diff not rendered.
Binary file added static/elements/82-lead.avif
Binary file not shown.
Binary file removed static/elements/82-lead.jpg
Diff not rendered.
Binary file added static/elements/83-bismuth.avif
Binary file not shown.
Binary file removed static/elements/83-bismuth.jpg
Diff not rendered.
Binary file added static/elements/84-polonium.avif
Binary file not shown.
Binary file removed static/elements/84-polonium.jpg
Diff not rendered.
Binary file added static/elements/85-astatine.avif
Binary file not shown.
Binary file removed static/elements/85-astatine.jpg
Diff not rendered.
Binary file added static/elements/86-radon.avif
Binary file not shown.
Binary file removed static/elements/86-radon.jpg
Diff not rendered.
Binary file added static/elements/87-francium.avif
Binary file not shown.
Binary file removed static/elements/87-francium.jpg
Diff not rendered.
Binary file added static/elements/88-radium.avif
Binary file not shown.
Binary file removed static/elements/88-radium.jpg
Diff not rendered.
Binary file added static/elements/89-actinium.avif
Binary file not shown.
Binary file removed static/elements/89-actinium.jpg
Diff not rendered.
Binary file added static/elements/9-fluorine.avif
Binary file not shown.
Binary file removed static/elements/9-fluorine.jpg
Diff not rendered.
Binary file added static/elements/90-thorium.avif
Binary file not shown.
Binary file removed static/elements/90-thorium.jpg
Diff not rendered.
Binary file added static/elements/91-protactinium.avif
Binary file not shown.
Binary file removed static/elements/91-protactinium.jpg
Diff not rendered.
Binary file added static/elements/92-uranium.avif
Binary file not shown.
Binary file removed static/elements/92-uranium.jpg
Diff not rendered.
Binary file added static/elements/93-neptunium.avif
Binary file not shown.
Binary file removed static/elements/93-neptunium.jpg
Diff not rendered.
Binary file added static/elements/94-plutonium.avif
Binary file not shown.
Binary file removed static/elements/94-plutonium.jpg
Diff not rendered.
Binary file added static/elements/95-americium.avif
Binary file not shown.
Binary file removed static/elements/95-americium.jpg
Diff not rendered.
Binary file added static/elements/96-curium.avif
Binary file not shown.
Binary file removed static/elements/96-curium.jpg
Diff not rendered.
Binary file added static/elements/97-berkelium.avif
Binary file not shown.
Binary file removed static/elements/97-berkelium.jpg
Diff not rendered.
Binary file added static/elements/98-californium.avif
Binary file not shown.
Binary file removed static/elements/98-californium.jpg
Diff not rendered.
Binary file added static/elements/99-einsteinium.avif
Binary file not shown.
Binary file removed static/elements/99-einsteinium.jpg
Diff not rendered.
7 changes: 7 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import mdsvexamples from 'mdsvexamples/vite'
import type { UserConfig } from 'vite'
import type { UserConfig as VitestConfig } from 'vitest'

const run_script = process.argv.some((arg) =>
arg.startsWith(`fetch-elem-images:`)
)
if (run_script) {
await import(`./src/fetch-elem-images.ts`)
}

export default {
plugins: [sveltekit(), mdsvexamples],

Expand Down

0 comments on commit a93fee4

Please sign in to comment.