Make the stamps wall bounce at every page size #8

Merged
adamf merged 1 commits from worktree-stamps-always-bounce into main 2026-10-01 15:20:31 +01:00
+78 -21
View File
@@ -5,10 +5,15 @@
* The order is shuffled on module load so the wall looks different each visit. * The order is shuffled on module load so the wall looks different each visit.
* The rAF loop then drifts the scroll position diagonally and bounces off each * The rAF loop then drifts the scroll position diagonally and bounces off each
* edge — it drives the DOM's scrollLeft/scrollTop directly rather than any * edge — it drives the DOM's scrollLeft/scrollTop directly rather than any
* reactive state, and reads scrollWidth/clientWidth each frame because images * reactive state, and reads scrollWidth/clientWidth each frame because the
* load in progressively and change the bounds. * wall is re-sized whenever the cell is.
*
* The wall is a fixed-track grid whose column and row counts are derived from
* the cell's own size (see fitWall), so it always overflows on both axes. A
* wrapping flex row only overflowed when the cell happened to be narrower than
* the wall, which left it motionless at tablet widths.
*/ */
import { ref, onMounted, onUnmounted } from "vue"; import { ref, computed, onMounted, onUnmounted } from "vue";
import Touchscreen from "@/components/util/Touchscreen.vue"; import Touchscreen from "@/components/util/Touchscreen.vue";
import Link from "@/components/text/Link.vue"; import Link from "@/components/text/Link.vue";
@@ -34,7 +39,56 @@ let srcs = [
// page load but stable while the widget is mounted. Mutates in place. // page load but stable while the widget is mounted. Mutates in place.
shuffleArray(srcs); shuffleArray(srcs);
// The two linked stamps stay first; the shuffled local ones follow.
const stamps = [
{
src: "https://www.adam-french.co.uk/img/stamps/mine.gif",
alt: "adam-french.co.uk",
href: "https://www.adam-french.co.uk",
},
{
src: "https://jacobbarron.xyz/Banneh.gif",
alt: "jacobbarron.xyz",
href: "https://jacobbarron.xyz",
},
...srcs.map((src) => ({ src, alt: src.split("/").pop().split(".")[0] })),
];
// Must match the grid track sizes in the <style> block.
const STAMP_W = 89;
const STAMP_H = 59;
const touchscreen = ref(null); const touchscreen = ref(null);
const cols = ref(4);
const rows = ref(4);
// Every grid slot is filled, cycling through the stamps when the wall needs
// more slots than there are stamps — a ragged last row would leave a blank
// strip drifting into view.
const cells = computed(() =>
Array.from(
{ length: cols.value * rows.value },
(_, i) => stamps[i % stamps.length],
),
);
/**
* Size the wall to between one and two stamps larger than the cell on each
* axis, so there is always something to bounce across whatever the page size.
* Rows are also raised, if need be, until every stamp appears at least once.
*/
function fitWall() {
const el = touchscreen.value?.$el;
if (!el) return;
cols.value = Math.ceil(el.clientWidth / STAMP_W) + 1;
rows.value = Math.max(
Math.ceil(el.clientHeight / STAMP_H) + 1,
Math.ceil(stamps.length / cols.value),
);
}
let resizeObserver = null;
let animId = null; let animId = null;
let posX = 0; let posX = 0;
let posY = 0; let posY = 0;
@@ -46,7 +100,7 @@ let dy = 0.12;
* *
* Writes scrollLeft/scrollTop on the DOM node directly (no reactive state, so * Writes scrollLeft/scrollTop on the DOM node directly (no reactive state, so
* no re-render per frame), and re-reads scrollWidth/clientWidth every frame * no re-render per frame), and re-reads scrollWidth/clientWidth every frame
* because the stamp images load in progressively and keep changing the bounds. * because fitWall changes the bounds whenever the cell is resized.
*/ */
function bounce() { function bounce() {
const el = touchscreen.value?.$el; const el = touchscreen.value?.$el;
@@ -82,43 +136,46 @@ function bounce() {
} }
onMounted(() => { onMounted(() => {
fitWall();
// The cell's size follows the home grid's breakpoints (and the scrollbars
// appearing inside it), not just window resizes, so observe the element.
resizeObserver = new ResizeObserver(fitWall);
resizeObserver.observe(touchscreen.value.$el);
animId = requestAnimationFrame(bounce); animId = requestAnimationFrame(bounce);
}); });
onUnmounted(() => { onUnmounted(() => {
if (animId) cancelAnimationFrame(animId); if (animId) cancelAnimationFrame(animId);
resizeObserver?.disconnect();
}); });
</script> </script>
<template> <template>
<Touchscreen ref="touchscreen"> <Touchscreen ref="touchscreen">
<div class="flex flex-wrap tst"> <div class="wall" :style="{ '--cols': cols }">
<Link bare href="https://www.adam-french.co.uk"> <template v-for="(stamp, i) in cells" :key="i">
<img <Link v-if="stamp.href" bare :href="stamp.href">
src="https://www.adam-french.co.uk/img/stamps/mine.gif" <img :src="stamp.src" :alt="stamp.alt" />
alt="adam-french.co.uk"
/>
</Link> </Link>
<Link bare href="https://jacobbarron.xyz"> <img v-else :src="stamp.src" :alt="stamp.alt" loading="lazy" />
<img src="https://jacobbarron.xyz/Banneh.gif" alt="jacobbarron.xyz" /> </template>
</Link>
<img
v-for="src in srcs"
:src="src"
:alt="src.split('/').pop().split('.')[0]"
loading="lazy"
/>
</div> </div>
</Touchscreen> </Touchscreen>
</template> </template>
<style scoped> <style scoped>
img { img {
display: block;
width: 89px; width: 89px;
height: 59px; height: 59px;
} }
.tst { /* Fixed tracks rather than flex-wrap: the wall's size comes from --cols (set
min-width: calc(89px * 4); by fitWall), never from how much room the cell happens to have. */
.wall {
display: grid;
grid-template-columns: repeat(var(--cols), 89px);
grid-auto-rows: 59px;
width: max-content;
} }
</style> </style>