Make the stamps wall bounce at every page size
The wall was a wrapping flex row with a fixed 4-stamp min-width, so it only overflowed (and so only bounced) when its cell was narrower than that. At tablet widths the cell is wider, the wall fitted, and horizontal motion stopped. Lay it out as a fixed-track grid whose column and row counts come from the cell's own size via a ResizeObserver, always one to two stamps larger than the cell on both axes, cycling the stamps to fill every slot. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -5,10 +5,15 @@
|
||||
* 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
|
||||
* edge — it drives the DOM's scrollLeft/scrollTop directly rather than any
|
||||
* reactive state, and reads scrollWidth/clientWidth each frame because images
|
||||
* load in progressively and change the bounds.
|
||||
* reactive state, and reads scrollWidth/clientWidth each frame because the
|
||||
* 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 Link from "@/components/text/Link.vue";
|
||||
@@ -34,7 +39,56 @@ let srcs = [
|
||||
// page load but stable while the widget is mounted. Mutates in place.
|
||||
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 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 posX = 0;
|
||||
let posY = 0;
|
||||
@@ -46,7 +100,7 @@ let dy = 0.12;
|
||||
*
|
||||
* Writes scrollLeft/scrollTop on the DOM node directly (no reactive state, so
|
||||
* 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() {
|
||||
const el = touchscreen.value?.$el;
|
||||
@@ -82,43 +136,46 @@ function bounce() {
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
if (animId) cancelAnimationFrame(animId);
|
||||
resizeObserver?.disconnect();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Touchscreen ref="touchscreen">
|
||||
<div class="flex flex-wrap tst">
|
||||
<Link bare href="https://www.adam-french.co.uk">
|
||||
<img
|
||||
src="https://www.adam-french.co.uk/img/stamps/mine.gif"
|
||||
alt="adam-french.co.uk"
|
||||
/>
|
||||
</Link>
|
||||
<Link bare href="https://jacobbarron.xyz">
|
||||
<img src="https://jacobbarron.xyz/Banneh.gif" alt="jacobbarron.xyz" />
|
||||
</Link>
|
||||
<img
|
||||
v-for="src in srcs"
|
||||
:src="src"
|
||||
:alt="src.split('/').pop().split('.')[0]"
|
||||
loading="lazy"
|
||||
/>
|
||||
<div class="wall" :style="{ '--cols': cols }">
|
||||
<template v-for="(stamp, i) in cells" :key="i">
|
||||
<Link v-if="stamp.href" bare :href="stamp.href">
|
||||
<img :src="stamp.src" :alt="stamp.alt" />
|
||||
</Link>
|
||||
<img v-else :src="stamp.src" :alt="stamp.alt" loading="lazy" />
|
||||
</template>
|
||||
</div>
|
||||
</Touchscreen>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
img {
|
||||
display: block;
|
||||
width: 89px;
|
||||
height: 59px;
|
||||
}
|
||||
|
||||
.tst {
|
||||
min-width: calc(89px * 4);
|
||||
/* Fixed tracks rather than flex-wrap: the wall's size comes from --cols (set
|
||||
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>
|
||||
|
||||
Reference in New Issue
Block a user