Spaces:
Running
Running
feat: loading state
Browse files- svelte/src/routes/+page.svelte +56 -22
svelte/src/routes/+page.svelte
CHANGED
|
@@ -6,14 +6,25 @@
|
|
| 6 |
import { onMount } from "svelte";
|
| 7 |
|
| 8 |
// Variables
|
| 9 |
-
let
|
| 10 |
let isDragging = false;
|
| 11 |
let x = 0;
|
| 12 |
let y = 0;
|
| 13 |
let lastX = 0;
|
| 14 |
let lastY = 0;
|
| 15 |
|
| 16 |
-
onMount(() =>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
function handleMouseDown(e: MouseEvent) {
|
| 19 |
isDragging = true;
|
|
@@ -29,26 +40,35 @@
|
|
| 29 |
y += deltaY;
|
| 30 |
lastX = e.clientX;
|
| 31 |
lastY = e.clientY;
|
| 32 |
-
|
| 33 |
if (canvas) canvas.style.transform = `translate(${x}px, ${y}px)`;
|
| 34 |
}
|
| 35 |
}
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
</script>
|
| 42 |
|
| 43 |
<!-- Main container -->
|
| 44 |
<div class="flex flex-col justify-center items-center w-full">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
<!-- Window -->
|
| 46 |
<div id="window" class="flex flex-col justify-center items-center mt-40">
|
| 47 |
<!-- Title container -->
|
| 48 |
<button
|
| 49 |
on:mousedown={handleMouseDown}
|
| 50 |
-
|
| 51 |
-
|
| 52 |
class="cursor-default relative flex flex-row justify-between items-center w-full bg-[#393B3D] border-b border-b-[#000002] border border-[#626264] rounded-xl rounded-b-none"
|
| 53 |
>
|
| 54 |
<!-- macOS dots container -->
|
|
@@ -62,13 +82,23 @@
|
|
| 62 |
</button>
|
| 63 |
|
| 64 |
<!-- Canvas container -->
|
| 65 |
-
<
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
</div>
|
| 73 |
|
| 74 |
<!-- Instructions -->
|
|
@@ -76,10 +106,14 @@
|
|
| 76 |
<img src="images/instructs.svg" alt="Instructions" width="250" />
|
| 77 |
</div>
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
</div>
|
|
|
|
| 6 |
import { onMount } from "svelte";
|
| 7 |
|
| 8 |
// Variables
|
| 9 |
+
let loading = false;
|
| 10 |
let isDragging = false;
|
| 11 |
let x = 0;
|
| 12 |
let y = 0;
|
| 13 |
let lastX = 0;
|
| 14 |
let lastY = 0;
|
| 15 |
|
| 16 |
+
onMount(async () => {
|
| 17 |
+
try {
|
| 18 |
+
loading = true;
|
| 19 |
+
await init();
|
| 20 |
+
loading = false;
|
| 21 |
+
} catch (error) {
|
| 22 |
+
console.log("[ERROR]:");
|
| 23 |
+
console.error(error);
|
| 24 |
+
} finally {
|
| 25 |
+
loading = false;
|
| 26 |
+
}
|
| 27 |
+
});
|
| 28 |
|
| 29 |
function handleMouseDown(e: MouseEvent) {
|
| 30 |
isDragging = true;
|
|
|
|
| 40 |
y += deltaY;
|
| 41 |
lastX = e.clientX;
|
| 42 |
lastY = e.clientY;
|
| 43 |
+
const canvas = document.getElementById("window");
|
| 44 |
if (canvas) canvas.style.transform = `translate(${x}px, ${y}px)`;
|
| 45 |
}
|
| 46 |
}
|
| 47 |
|
| 48 |
+
function handleMouseUp() {
|
| 49 |
+
isDragging = false;
|
| 50 |
+
}
|
|
|
|
| 51 |
</script>
|
| 52 |
|
| 53 |
<!-- Main container -->
|
| 54 |
<div class="flex flex-col justify-center items-center w-full">
|
| 55 |
+
|
| 56 |
+
<!-- Hack to pre-load Bevy assets to avoid the tiny delay-->
|
| 57 |
+
<div class="fixed opacity-0">
|
| 58 |
+
<img src="assets/blood_1.png" alt="blood1">
|
| 59 |
+
<img src="assets/blood_2.png" alt="blood1">
|
| 60 |
+
<img src="assets/bone_1.png" alt="bone1">
|
| 61 |
+
<img src="assets/bone_2.png" alt="bone2">
|
| 62 |
+
<img src="assets/bone_3.png" alt="bone3">
|
| 63 |
+
</div>
|
| 64 |
+
|
| 65 |
<!-- Window -->
|
| 66 |
<div id="window" class="flex flex-col justify-center items-center mt-40">
|
| 67 |
<!-- Title container -->
|
| 68 |
<button
|
| 69 |
on:mousedown={handleMouseDown}
|
| 70 |
+
on:mousemove={handleMouseMove}
|
| 71 |
+
on:mouseup={handleMouseUp}
|
| 72 |
class="cursor-default relative flex flex-row justify-between items-center w-full bg-[#393B3D] border-b border-b-[#000002] border border-[#626264] rounded-xl rounded-b-none"
|
| 73 |
>
|
| 74 |
<!-- macOS dots container -->
|
|
|
|
| 82 |
</button>
|
| 83 |
|
| 84 |
<!-- Canvas container -->
|
| 85 |
+
<div class="relative">
|
| 86 |
+
<canvas
|
| 87 |
+
id="bevy-canvas"
|
| 88 |
+
on:contextmenu|preventDefault={() => {}}
|
| 89 |
+
class="shadow-2xl z-10 shadow-black/50 overflow-hidden rounded-xl border border-[#626264] border-t-0 rounded-t-none"
|
| 90 |
+
height="480"
|
| 91 |
+
width="640"
|
| 92 |
+
style="image-rendering: pixelated;"
|
| 93 |
+
/>
|
| 94 |
+
{#if loading}
|
| 95 |
+
<div class="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 space-x-3 flex flex-row justify-center items-center">
|
| 96 |
+
<div class="h-2 w-2 bg-[#B6B8B9] animate-pulse" />
|
| 97 |
+
<div class="h-2 w-2 bg-[#B6B8B9] animate-pulse" />
|
| 98 |
+
<div class="h-2 w-2 bg-[#B6B8B9] animate-pulse" />
|
| 99 |
+
</div>
|
| 100 |
+
{/if}
|
| 101 |
+
</div>
|
| 102 |
</div>
|
| 103 |
|
| 104 |
<!-- Instructions -->
|
|
|
|
| 106 |
<img src="images/instructs.svg" alt="Instructions" width="250" />
|
| 107 |
</div>
|
| 108 |
|
| 109 |
+
<!-- Credits -->
|
| 110 |
+
<div class="mt-12 text-sm text-[#636669] flex flex-col justify-center items-center space-y-1">
|
| 111 |
+
<p>
|
| 112 |
+
Made by <a href="https://www.hugoduprez.com/" target="_blank" class="underline">Hugo</a> with
|
| 113 |
+
<a href="https://www.rust-lang.org/" target="_blank" class="underline">Rust</a>
|
| 114 |
+
and <a href="https://kit.svelte.dev/" target="_blank" class="underline">Svelte</a>
|
| 115 |
+
</p>
|
| 116 |
+
<a href="https://github.com/Hugo-Dz/rust-sandbox" target="_blank" class="underline">How to create games with Rust</a
|
| 117 |
+
>
|
| 118 |
+
</div>
|
| 119 |
</div>
|