Skip to content

Commit

Permalink
feat: thumbhash
Browse files Browse the repository at this point in the history
  • Loading branch information
Vilsol committed Aug 27, 2024
1 parent c929f35 commit a72e711
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ $RECYCLE.BIN/
/functions
/.graphqlconfig
/schema.graphql
/graphql.schema.json
/graphql.schema.*
/.svelte-kit
/.pnpm-store
.direnv
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"semver": "^7.6.3",
"socket.io-client": "^4.7.5",
"tslib": "^2.6.3",
"thumbhash": "^0.1.1",
"wonka": "^6.3.4",
"zod": "^3.23.8"
},
Expand Down
1 change: 1 addition & 0 deletions src/gql/home/mods.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ query GetMods($offset: Int!, $limit: Int!, $search: String, $order: Order, $orde
mod_reference
name
logo
logo_thumbhash
views
downloads
short_description
Expand Down
66 changes: 64 additions & 2 deletions src/lib/components/general/FicsitCard.svelte
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
<script lang="ts">
import { assets } from '$app/paths';
import { goto, preloadData } from '$app/navigation';
import { thumbHashToDataURL } from 'thumbhash';
import { fade } from 'svelte/transition';
export let name = '';
export let logo = assets + '/images/no_image.webp';
export let description = '';
export let link = '/';
export let fake = false;
export let thumbhash = '2/eFDQIsFmh9h4BreKeAeQqYBxd3d3J4Jw';
$: renderedLogo = logo || assets + '/images/no_image.webp';
$: renderedName = name || (fake && 'Card Name');
$: renderedDescription = description || (fake && 'Short card description');
$: thumbHashData = (() => {
try {
return thumbHashToDataURL(
new Uint8Array(
atob(thumbhash)
.split('')
.map((x) => x.charCodeAt(0))
)
);
} catch (e) {
console.error(e);
}
})();
let preloaded = false;
let timeoutHandle: number;
Expand All @@ -34,6 +50,8 @@
};
let actionButtons: HTMLElement;
let imageLoaded = false;
</script>

<div
Expand All @@ -48,11 +66,27 @@
class:font-flow={fake}
class="grid-max-auto grid grid-cols-1 justify-items-center sm:grid-cols-2">
<div class="card-image-container cursor-pointer">
<a href={link} on:keypress={() => goto(link)} tabindex="0">
<a
href={link}
on:keypress={() => goto(link)}
tabindex="0"
class="relative block max-h-full min-h-full min-w-full max-w-full">
{#if fake}
<div class="logo max-h-full min-h-full min-w-full max-w-full bg-neutral-500" />
{:else}
<img src={renderedLogo} alt="{renderedName} Logo" class="logo max-h-full min-h-full min-w-full max-w-full" />
<img
class="logo absolute max-h-full min-h-full min-w-full max-w-full"
src={renderedLogo}
alt="{renderedName} Logo"
on:load={() => (imageLoaded = true)} />
{#if !imageLoaded}
<img
class="logo absolute max-h-full min-h-full min-w-full max-w-full bg-neutral-500"
src={thumbHashData}
alt="{renderedName} Logo"
width="100%"
out:fade={{ duration: 200 }} />
{/if}
{/if}
</a>
</div>
Expand Down Expand Up @@ -117,4 +151,32 @@
grid-template-columns: max-content auto;
}
}
.elementToFadeInAndOut {
width: 200px;
height: 200px;
background: red;
-webkit-animation: fadeinout 4s linear forwards;
animation: fadeinout 4s linear forwards;
}
@-webkit-keyframes fadeinout {
0%,
100% {
opacity: 0;
}
50% {
opacity: 1;
}
}
@keyframes fadeinout {
0%,
100% {
opacity: 0;
}
50% {
opacity: 1;
}
}
</style>
12 changes: 11 additions & 1 deletion src/lib/components/mods/ModCard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@
export let mod: Pick<
Mod,
'id' | 'mod_reference' | 'name' | 'logo' | 'views' | 'downloads' | 'short_description' | 'compatibility' | 'tags'
| 'id'
| 'mod_reference'
| 'name'
| 'logo'
| 'views'
| 'downloads'
| 'short_description'
| 'compatibility'
| 'tags'
| 'logo_thumbhash'
> & {
latestVersions: {
alpha?: Maybe<Pick<Version, 'id'>>;
Expand All @@ -27,6 +36,7 @@
name={mod.name}
link={base + '/mod/' + mod.mod_reference}
logo={mod.logo}
thumbhash={mod.logo_thumbhash}
description={mod.short_description}>
<div slot="stats" class="flex flex-row items-center gap-2">
<span><span class="material-icons mr-1 align-middle text-sm">visibility</span>{prettyNumber(mod.views)}</span>
Expand Down

0 comments on commit a72e711

Please sign in to comment.