Skip to content

Commit 2747618

Browse files
committed
perf: preload thumbnail images
1 parent 05875e0 commit 2747618

File tree

1 file changed

+53
-41
lines changed

1 file changed

+53
-41
lines changed

routes/stage-select/+page.svelte

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ import { onMount } from "svelte";
44
import "@/ui-components/menu/menu.css";
55
import { replaceState } from "$app/navigation";
66
7-
let w: string | null = null;
7+
let world: string | null = null;
88
let selected: number | null = null;
99
1010
onMount(() => {
1111
const params = new URLSearchParams(window.location.search);
12-
w = params.get("w") ?? "1";
12+
world = params.get("w") ?? "1";
1313
selected = Number(params.get("s") ?? "1") - 1;
1414
});
1515
1616
$: blocks = [
17-
{ label: "1", link: `/game?stage=${w}-1` },
18-
{ label: "2", link: `/game?stage=${w}-2` },
19-
{ label: "3", link: `/game?stage=${w}-3` },
20-
{ label: "4", link: `/game?stage=${w}-4` },
17+
{ label: "1", link: `/game?stage=${world}-1` },
18+
{ label: "2", link: `/game?stage=${world}-2` },
19+
{ label: "3", link: `/game?stage=${world}-3` },
20+
{ label: "4", link: `/game?stage=${world}-4` },
2121
];
2222
2323
let lastKeyTime = 0;
@@ -26,16 +26,16 @@ const KEY_REPEAT_DELAY = 180; // ms
2626
2727
function prevWorld() {
2828
// wを数値として1減らす(1未満にはしない)
29-
w = String(Math.max(1, Number(w) - 1));
29+
world = String(Math.max(1, Number(world) - 1));
3030
const url = new URL(window.location.href);
31-
url.searchParams.set("w", w);
31+
url.searchParams.set("w", world);
3232
replaceState(url.toString(), {});
3333
}
3434
function nextWorld() {
3535
// wを数値として1増やす(4より大きくしない)
36-
w = String(Math.min(4, Number(w) + 1));
36+
world = String(Math.min(4, Number(world) + 1));
3737
const url = new URL(window.location.href);
38-
url.searchParams.set("w", w);
38+
url.searchParams.set("w", world);
3939
replaceState(url.toString(), {});
4040
}
4141
function select(index: number): void {
@@ -61,7 +61,7 @@ function handleKey(e: KeyboardEvent): void {
6161
6262
if (e.key === "ArrowRight") {
6363
if (selected === blocks.length - 1) {
64-
if (w !== "4") {
64+
if (world !== "4") {
6565
nextWorld();
6666
select(0);
6767
}
@@ -70,7 +70,7 @@ function handleKey(e: KeyboardEvent): void {
7070
}
7171
} else if (e.key === "ArrowLeft") {
7272
if (selected === 0) {
73-
if (w !== "1") {
73+
if (world !== "1") {
7474
prevWorld();
7575
select(blocks.length - 1);
7676
}
@@ -99,46 +99,47 @@ onMount(() => {
9999
</script>
100100

101101
<div id="container" class="fixed inset-0">
102-
<div class="w-full h-full py-8 backdrop-blur-xs flex flex-col ">
103-
102+
<div class="w-full h-full py-8 backdrop-blur-xs flex flex-col">
104103
<div class="">
105-
<button class="btn modal-btn text-xl ml-8 mb-6 w-max! px-4! ">
106-
<a href="/">&lt; Back to Main Menu</a>
107-
</button>
108-
</div>
104+
<button class="btn modal-btn text-xl ml-8 mb-6 w-max! px-4!">
105+
<a href="/">&lt; Back to Main Menu</a>
106+
</button>
107+
</div>
109108
<div class="text-7xl text-center flex items-center justify-center gap-8">
110109
<!-- 左矢印ボタン -->
111110
<button
112-
class="px-4 select-none cursor-pointer {Number(w) <= 1 ? "invisible" : ""} hover:-translate-y-1 hover:text-gray-700 active:translate-y-0 active:text-black "
111+
class="px-4 select-none cursor-pointer {Number(world) <= 1
112+
? 'invisible'
113+
: ''} hover:-translate-y-1 hover:text-gray-700 active:translate-y-0 active:text-black"
113114
aria-label="前のワールド"
114115
on:click={prevWorld}
115-
disabled={Number(w) <= 1}
116+
disabled={Number(world) <= 1}
116117
>
117118
&lt;
118119
</button>
119-
<span>World {w}</span>
120+
<span>World {world}</span>
120121
<!-- 右矢印ボタン -->
121122
<button
122-
class="px-4 select-none cursor-pointer {Number(w) >= 4 ? "invisible" : ""} hover:-translate-y-1 hover:text-gray-700 active:translate-y-0 active:text-black "
123+
class="px-4 select-none cursor-pointer {Number(world) >= 4
124+
? 'invisible'
125+
: ''} hover:-translate-y-1 hover:text-gray-700 active:translate-y-0 active:text-black"
123126
aria-label="次のワールド"
124127
on:click={nextWorld}
125-
disabled={Number(w) >= 4}
128+
disabled={Number(world) >= 4}
126129
>
127130
&gt;
128131
</button>
129132
</div>
130133

131-
<div class="flex justify-center items-center grow-1 ">
132-
<div
133-
role="button"
134-
tabindex="0"
135-
class="flex outline-none items-center"
136-
>
134+
<div class="flex justify-center items-center grow-1">
135+
<div role="button" tabindex="0" class="flex outline-none items-center">
137136
{#each blocks as block, i}
138137
<button
139138
type="button"
140139
class={`appearance-none focus:outline-none bg-white border-6 pt-8 pb-6 pl-8 pr-6 transition-colors duration-200 text-7xl cursor-pointer ${
141-
selected === i ? 'border-red-500 ring ring-red-500 bg-amber-100!' : 'border-base'
140+
selected === i
141+
? "border-red-500 ring ring-red-500 bg-amber-100!"
142+
: "border-base"
142143
}`}
143144
on:click={() => select(i)}
144145
>
@@ -151,19 +152,27 @@ onMount(() => {
151152
{/each}
152153
</div>
153154
</div>
154-
<div class="flex justify-center items-center basis-2/5 min-h-0 shrink grow-2">
155+
<div
156+
class="flex justify-center items-center basis-2/5 min-h-0 shrink grow-2"
157+
>
155158
<!-- 画像を中央に配置 -->
156-
<div class="h-full ">
159+
<div class="h-full">
157160
{#if selected !== null}
158-
<img
159-
src="/assets/thumbnail{w}-{selected+1}.png"
160-
alt=""
161-
class="h-full "
162-
/>
161+
{#key world}
162+
{#each { length: 4 } as _, idx}
163+
<img
164+
src="/assets/thumbnail{world}-{idx + 1}.png"
165+
alt=""
166+
class={["h-full skeleton", idx !== selected && "hidden"]}
167+
/>
168+
{/each}
169+
{/key}
163170
{/if}
164171
</div>
165172
<!-- テキストを画像の右側に配置 -->
166-
<div class="flex-none w-max max-h-full flex flex-col items-start bg-white/90 p-4 m-4 rounded-lg border-2">
173+
<div
174+
class="flex-none w-max max-h-full flex flex-col items-start bg-white/90 p-4 m-4 rounded-lg border-2"
175+
>
167176
Press <Key key="Enter" enabled /> or <Key key="Space" enabled /> to start
168177
</div>
169178
</div>
@@ -173,10 +182,13 @@ onMount(() => {
173182
<style>
174183
#container {
175184
background-color: black;
176-
background-image: url('/assets/home.png'), url('/assets/home-block.png');
185+
background-image: url("/assets/home.png"), url("/assets/home-block.png");
177186
background-size: 100%, 100%;
178187
background-repeat: no-repeat, repeat-y;
179-
background-position: left top, left, top;
188+
background-position:
189+
left top,
190+
left,
191+
top;
180192
backdrop-filter: blur(10px);
181193
}
182-
</style>
194+
</style>

0 commit comments

Comments
 (0)