Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve spline loading #128

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@appwrite.io/pink": "0.1.0-next.9",
"@appwrite.io/pink-icons": "0.1.0-next.9",
"@appwrite.io/repo": "github:appwrite/appwrite",
"@fontsource/inter": "^5.0.8",
"@splinetool/viewer": "0.9.455",
"highlight.js": "^11.8.0",
"markdown-it": "^13.0.1",
"meilisearch": "^0.35.0",
Expand Down
58 changes: 48 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
}
}
</script>
<script type="module" src="https://unpkg.com/@splinetool/[email protected]/build/spline-viewer.js"></script>
<div id="top" style="display: contents">%sveltekit.body%</div>
</body>

Expand Down
80 changes: 32 additions & 48 deletions src/lib/components/Spline.svelte
Original file line number Diff line number Diff line change
@@ -1,51 +1,35 @@
<script lang="ts">
import { writable } from 'svelte/store';

let loaded = writable(false);

type SplineElement = HTMLElement & {
_loaded: boolean;
};

function isSplineElement(el: HTMLElement): el is SplineElement {
return '_loaded' in el;
}

const viewer = (node: HTMLElement) => {
if (!isSplineElement(node)) {
throw new Error('Spline element not found');
}

const onLoad = () => {
loaded.set(true);
node.shadowRoot?.querySelector('#logo')?.remove(); // Remove Spline logo
};

node?.addEventListener('load-complete', onLoad);

if (node._loaded) {
onLoad();
}

return {
destroy() {
node?.removeEventListener('load-complete', onLoad);
}
};
};

const fallback = (node: HTMLElement) => {
const destroy = loaded.subscribe((l) => {
if (!l) return;
setTimeout(() => {
node?.style.setProperty('display', 'none');
}, 150);
});

return {
destroy
};
};
import { onMount } from 'svelte';
import { writable } from 'svelte/store';
import type { SplineViewer } from '@splinetool/viewer';
import { fade } from 'svelte/transition';

export let url: SplineViewer['url'];
export let width: SplineViewer['width'] = undefined;
export let height: SplineViewer['height'] = undefined;
export let loading: SplineViewer['loading'] = 'auto';

let loaded = writable(false);
let spline: SplineViewer;

onMount(async () => {
await import('@splinetool/viewer');
const onLoad = () => {
spline.shadowRoot?.querySelector('#logo')?.remove(); // Remove Spline logo

setTimeout(() => {
loaded.set(true);
}, 150);
};

spline.addEventListener('load-complete', onLoad);
});
</script>

<slot {viewer} {fallback} />
<spline-viewer style="position: absolute;" {url} {width} {height} {loading} bind:this={spline} />

{#if $$slots?.default && !$loaded}
<div out:fade={{duration: 50}}>
<slot />
</div>
{/if}
69 changes: 29 additions & 40 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -410,31 +410,28 @@
</div>

<div class="aw-big-padding-section-level-1 u-position-relative u-overflow-hidden">
<Spline let:fallback let:viewer>
<div
class="u-position-absolute aw-is-not-mobile"
style:inline-size="768px"
style:block-size="768px"
style:inset-block-start="0rem"
style:z-index="-1"
style:left="calc(50% - 384px + 350px)"
>
<img
src="/images/animations/tech-dark.png"
width="768"
height="768"
alt=""
style="position: absolute; display: block;"
use:fallback
/>
<spline-viewer
url="/images/animations/dark-scene.splinecode"
width="768"
height="768"
use:viewer
/>
</div>
<div
class="u-position-absolute aw-is-not-mobile"
style:inline-size="768px"
style:block-size="768px"
style:inset-block-start="0rem"
style:z-index="-1"
style:left="calc(50% - 384px + 350px)"
>
<Spline
url="/images/animations/dark-scene.splinecode"
loading="eager"
width={768}
height={768}>
<img
src="/images/animations/tech-dark.png"
width="768"
height="768"
alt=""
style="position: absolute; display: block;"
/>
</Spline>
</div>
<div class="aw-big-padding-section-level-2 is-margin-replace-padding">
<div class="aw-container u-position-relative">
<section class="aw-hero is-align-start">
Expand Down Expand Up @@ -467,23 +464,15 @@
</div>
</div>
<div class="aw-big-padding-section-level-2 u-position-relative u-overflow-hidden">
<Spline let:viewer>
<div
class="u-position-absolute u-z-index-0 aw-is-not-mobile"
style:width="50%"
style:height="100%"
style:left="0"
>
<div style:display="grid" style:place-items="center" style:height="100%">
<spline-viewer
url="https://prod.spline.design/OQpkUefWdEWkbi4d/scene.splinecode"
loading="eager"
use:viewer
/>
</div>
<div
class="u-position-absolute u-z-index-0 aw-is-not-mobile"
style:width="50%"
style:height="100%"
style:left="0">
<div style:display="grid" style:place-items="center" style:height="100%">
<Spline loading="eager" url="/images/animations/lines.splinecode"/>
</div>
</Spline>

</div>
<div class="aw-container u-position-relative">
<div class="grid-1-1">
<section class="aw-hero is-align-start">
Expand Down
59 changes: 15 additions & 44 deletions src/routes/docs/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -160,57 +160,28 @@
style="position: absolute; display: block;"
/>
</div>
<!-- <Spline let:fallback let:viewer>
<div
class="u-position-absolute aw-is-not-mobile u-only-light"
style:inline-size="768px"
style:block-size="768px"
style:inset-block-start="26rem"
style:z-index="-1"
style:left="calc(50% - 384px + 600px)"
>
<img
src="/images/animations/tech-light.png"
width="768"
height="768"
alt=""
style="position: absolute; display: block;"
use:fallback
/>
<spline-viewer
url="/images/animations/light-scene.splinecode"
width="768"
height="768"
use:viewer
/>
</div>
</Spline> -->

<Spline let:fallback let:viewer>
<div
class="u-position-absolute aw-is-not-mobile u-only-dark"
style:inline-size="768px"
style:block-size="768px"
style:inset-block-start="26rem"
style:z-index="-1"
style:left="calc(50% - 384px + 600px)"
>
<div
class="u-position-absolute aw-is-not-mobile u-only-dark"
style:inline-size="768px"
style:block-size="768px"
style:inset-block-start="26rem"
style:z-index="-1"
style:left="calc(50% - 384px + 600px)"
>
<Spline
url="/images/animations/dark-scene.splinecode"
width={768}
height={768}>
<img
src="/images/animations/tech-dark.png"
width="768"
height="768"
alt=""
style="position: absolute; display: block;"
use:fallback
/>
<spline-viewer
url="/images/animations/dark-scene.splinecode"
width="768"
height="768"
use:viewer
style="position: absolute;"
/>
</div>
</Spline>
</Spline>
</div>

<main class="aw-main-section u-position-relative">
<div
Expand Down
Binary file added static/images/animations/lines.splinecode
Binary file not shown.