Skip to content

Commit 6aacefd

Browse files
authored
Stabilize the homepage palm animation (#1579)
* Add the stabilized homepage palm clip * Stabilize the homepage palm animation
1 parent eee9664 commit 6aacefd

4 files changed

Lines changed: 44 additions & 59 deletions

File tree

‎public/images/hero-palm-loop.mp4‎

1.61 MB
Binary file not shown.

‎src/components/home/HeroPalmMedia.tsx‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import * as React from 'react'
2-
import { preload } from 'react-dom'
32
import { Squircle } from '~/components/Squircle'
43
import { useMediaQuery } from '~/utils/useMediaQuery'
5-
import still from './hero-palm-cutout-still.webp?inline'
4+
import still from './hero-palm-loop-still.webp?inline'
65

7-
const motion = '/images/hero-palm-cutout.webp'
6+
const motion = '/images/hero-palm-loop.mp4'
87
const allowMotion = '(prefers-reduced-motion: no-preference)'
98

109
export function HeroPalmMedia() {
11-
preload(motion, { as: 'image', type: 'image/webp', media: allowMotion })
12-
1310
const permitsMotion = useMediaQuery(allowMotion)
1411
const [motionReady, setMotionReady] = React.useState(false)
15-
const showMotion = permitsMotion && motionReady
1612

1713
return (
1814
<Squircle
@@ -22,25 +18,26 @@ export function HeroPalmMedia() {
2218
{/* Keep the palm aligned with the original 944 × 416 composition. */}
2319
<div className="home-palm-sky relative aspect-[944/416] w-[max(100cqw,calc(100cqh*944/416))]">
2420
<div className="absolute inset-0 bg-[url('/images/hero-sky-grain.png')]" />
25-
<div className="absolute bottom-0 left-[calc(100%*360/944)] w-[calc(100%*260/944)]">
26-
{/* Keep the inlined first frame visible during the animation request. */}
21+
{/* White pixels leave the CSS sky untouched, even between the moving fronds. */}
22+
<div className="absolute bottom-0 left-[calc(100%*360/944)] w-[calc(100%*260/944)] mix-blend-multiply">
2723
<img
2824
src={still}
2925
alt=""
3026
width={260}
3127
height={348}
32-
hidden={showMotion}
3328
className="h-auto w-full"
3429
/>
35-
{(permitsMotion || motionReady) && (
36-
<img
30+
{permitsMotion && (
31+
<video
3732
src={motion}
38-
alt=""
39-
width={260}
40-
height={348}
41-
hidden={!showMotion}
42-
onLoad={() => setMotionReady(true)}
43-
className="h-auto w-full"
33+
autoPlay
34+
loop
35+
muted
36+
playsInline
37+
onLoadStart={() => setMotionReady(false)}
38+
onPlaying={() => setMotionReady(true)}
39+
onError={() => setMotionReady(false)}
40+
className={`absolute inset-0 size-full ${motionReady ? '' : 'opacity-0'}`}
4441
/>
4542
)}
4643
</div>
21.7 KB
Loading

‎tests/hero-media.test.ts‎

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import sharp from 'sharp'
1313
import { createServer } from 'vite'
1414

1515
const repoRoot = path.resolve(import.meta.dirname, '..')
16-
const motion = '/images/hero-palm-cutout.webp'
16+
const motion = '/images/hero-palm-loop.mp4'
1717
const motionFile = path.join(repoRoot, 'public', motion)
1818
const stillFile = path.join(
1919
repoRoot,
20-
'src/components/home/hero-palm-cutout-still.webp',
20+
'src/components/home/hero-palm-loop-still.webp',
2121
)
2222
const stillPrefix = 'data:image/webp;base64,'
2323
const vite = await createServer({
@@ -45,46 +45,23 @@ const chromePath = [
4545
chromium.executablePath(),
4646
].find((candidate) => candidate && fs.existsSync(candidate))
4747

48-
test('the palm loop stays at 24 fps, matches its still, and fits the media budget', async () => {
49-
const animation = await sharp(motionFile).metadata()
50-
const poster = await sharp(stillFile).metadata()
51-
// The source has 121 frames at 24 fps. WebP stores whole milliseconds.
52-
const delays = Array.from(
53-
{ length: 121 },
54-
(_, i) => Math.round(((i + 1) * 1000) / 24) - Math.round((i * 1000) / 24),
55-
)
56-
assert.deepEqual(animation.delay, delays)
57-
// The MP4 and its posters transferred 1.91 MB in the mobile baseline.
48+
test('the palm video and still fit the media budget', async () => {
5849
assert.ok(
5950
fs.statSync(motionFile).size + fs.statSync(stillFile).size < 1_900_000,
6051
'the combined palm assets should stay under 1.90 MB',
6152
)
62-
assert.equal(animation.loop, 0)
63-
assert.ok(animation.hasAlpha && poster.hasAlpha)
64-
assert.equal(animation.width, poster.width)
65-
assert.equal(animation.height, poster.height)
66-
const firstFrame = await sharp(motionFile).ensureAlpha().raw().toBuffer()
67-
const stillFrame = await sharp(stillFile).ensureAlpha().raw().toBuffer()
68-
assert.ok(
69-
firstFrame.equals(stillFrame),
70-
'the still must match the first frame',
71-
)
53+
const poster = await sharp(stillFile).metadata()
54+
assert.equal(poster.hasAlpha, false)
7255
})
7356

74-
test('the server inlines one visible still and preloads motion when allowed', () => {
57+
test('the server inlines one visible still', () => {
7558
assert.doesNotMatch(serverMarkup, /<source\b|<video\b/)
7659
assert.equal(serverMarkup.match(/<img\b/g)?.length, 1)
7760
assert.ok(
7861
serverMarkup.includes(
7962
`src="${stillPrefix}${fs.readFileSync(stillFile, 'base64')}"`,
8063
),
8164
)
82-
const [preload, ...extraPreloads] =
83-
serverMarkup.match(/<link[^>]+rel="preload"[^>]*>/g) ?? []
84-
assert.ok(preload)
85-
assert.equal(extraPreloads.length, 0)
86-
assert.ok(preload.includes(`href="${motion}"`))
87-
assert.match(preload, /media="\(prefers-reduced-motion: no-preference\)"/)
8865
})
8966

9067
test(
@@ -135,7 +112,7 @@ test(
135112
if (url.pathname === '/') {
136113
return route.fulfill({
137114
contentType: 'text/html',
138-
body: `<!doctype html><div id="root">${serverMarkup}</div>`,
115+
body: `<!doctype html><style>.opacity-0{opacity:0}</style><div id="root">${serverMarkup}</div>`,
139116
})
140117
}
141118
requests.push(url.pathname)
@@ -157,12 +134,12 @@ test(
157134
requests,
158135
hydrate,
159136
still: page.locator(`img[src^="${stillPrefix}"]`),
160-
motion: page.locator(`img[src="${motion}"]`),
137+
motion: page.locator(`video[src="${motion}"]`),
161138
releaseAnimation: () => animation.emit('release'),
162139
}
163140
}
164141

165-
for (const width of [390, 844, 1024, 1512]) {
142+
for (const width of [390, 1512]) {
166143
const hero = await openHero(width)
167144
await hero.still.waitFor({ state: 'visible' })
168145
assert.ok(
@@ -172,26 +149,34 @@ test(
172149
)
173150
await hero.hydrate()
174151
await hero.motion.waitFor({ state: 'attached' })
175-
assert.equal(await hero.motion.isHidden(), true)
176-
assert.equal(await hero.still.isVisible(), true)
152+
assert.equal(
153+
await hero.motion.evaluate((video) => getComputedStyle(video).opacity),
154+
'0',
155+
)
177156
hero.releaseAnimation()
178-
await hero.motion.waitFor({ state: 'visible' })
179-
assert.deepEqual(hero.requests, [motion])
180-
assert.equal(await hero.still.isHidden(), true)
157+
await hero.page.waitForFunction(() => {
158+
const video = document.querySelector('video')
159+
return (
160+
video &&
161+
video.currentTime > 0 &&
162+
getComputedStyle(video).opacity === '1'
163+
)
164+
})
165+
assert.ok(hero.requests.length > 0)
166+
assert.ok(hero.requests.every((request) => request === motion))
181167
await hero.page.close()
182168
}
183169

184170
const reduced = await openHero(390, true)
185171
reduced.releaseAnimation()
186-
await reduced.still.waitFor({ state: 'visible' })
187172
await reduced.hydrate()
188173
assert.equal(await reduced.motion.count(), 0)
189174
assert.deepEqual(reduced.requests, [])
190175
await reduced.page.emulateMedia({ reducedMotion: 'no-preference' })
191-
await reduced.motion.waitFor({ state: 'visible' })
176+
await reduced.motion.waitFor({ state: 'attached' })
192177
await reduced.page.emulateMedia({ reducedMotion: 'reduce' })
193-
await reduced.still.waitFor({ state: 'visible' })
194-
assert.equal(await reduced.motion.isHidden(), true)
178+
await reduced.motion.waitFor({ state: 'detached' })
179+
assert.equal(await reduced.still.isVisible(), true)
195180
await reduced.page.close()
196181

197182
const failed = await openHero(1512, false, true)
@@ -202,7 +187,10 @@ test(
202187
)
203188
failed.releaseAnimation()
204189
await requestFailed
205-
assert.equal(await failed.motion.isHidden(), true)
190+
assert.equal(
191+
await failed.motion.evaluate((video) => getComputedStyle(video).opacity),
192+
'0',
193+
)
206194
assert.equal(await failed.still.isVisible(), true)
207195
await failed.page.close()
208196
},

0 commit comments

Comments
 (0)