-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Skin3D component for enhanced player avatar visualization i…
…n stats layout
- Loading branch information
Showing
2 changed files
with
74 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<script lang="ts"> | ||
import { cn } from "$lib/shared/utils"; | ||
import type { Stats as StatsType } from "$lib/types/stats"; | ||
import * as skinview3d from "skinview3d"; | ||
import { getContext, onDestroy, onMount } from "svelte"; | ||
const profile = getContext<StatsType>("profile"); | ||
let { class: className }: { class: string | undefined } = $props(); | ||
let viewer: skinview3d.SkinViewer; | ||
let minecraftAvatar: HTMLCanvasElement; | ||
let canvasIsLoading = $state<boolean>(true); | ||
onMount(async () => { | ||
const minecraftAvatarContainerDimensions = minecraftAvatar.getBoundingClientRect(); | ||
const cape = await fetch(`https://crafatar.com/capes/${profile.uuid}`, { | ||
method: "HEAD" | ||
}).catch(() => ({ ok: false })); | ||
viewer = new skinview3d.SkinViewer({ | ||
canvas: minecraftAvatar, | ||
width: minecraftAvatarContainerDimensions.width, | ||
height: minecraftAvatarContainerDimensions.height, | ||
skin: `https://crafatar.com/skins/${profile.uuid}`, | ||
cape: cape.ok ? `https://crafatar.com/capes/${profile.uuid}` : undefined, | ||
animation: new skinview3d.IdleAnimation() | ||
}); | ||
viewer.camera.position.set(-18, -3, 78); | ||
viewer.controls.enableZoom = true; | ||
viewer.controls.enablePan = true; | ||
viewer.controls.enableRotate = true; | ||
viewer.canvas.removeAttribute("tabindex"); | ||
canvasIsLoading = false; | ||
return new Promise((resolve) => { | ||
resolve(() => { | ||
viewer.dispose(); | ||
}); | ||
}); | ||
}); | ||
onDestroy(() => { | ||
canvasIsLoading = true; | ||
viewer.dispose(); | ||
}); | ||
</script> | ||
|
||
<canvas bind:this={minecraftAvatar} class={cn("size-full transform-gpu overflow-hidden transition-opacity duration-[3s]", className)} class:opacity-100={!canvasIsLoading} class:opacity-0={canvasIsLoading}></canvas> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters