Skip to content

Commit 73bfdb5

Browse files
committed
Updates to lottie wrapper
1 parent 31e5b3e commit 73bfdb5

File tree

3 files changed

+52
-30
lines changed

3 files changed

+52
-30
lines changed

src/components/Animations/About.svelte

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@
33
import LottieWrapper from '$lib/vendor/LottieWrapper.svelte';
44
</script>
55

6-
<LottieWrapper src="{base}/animation/about-dot.lottie" autoplay layout={{ fit: 'contain', align: [0.5, 0.5] }} />
6+
<LottieWrapper
7+
src="{base}/animation/about-dot.lottie"
8+
autoplay
9+
playOnVisible
10+
layout={{ fit: 'contain', align: [0.5, 0.5] }}
11+
renderConfig={{ autoResize: true }}
12+
/>

src/components/Animations/DevPortal.svelte

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@
33
import LottieWrapper from '$lib/vendor/LottieWrapper.svelte';
44
</script>
55

6-
<LottieWrapper src="{base}/animation/dev-portal-dot.lottie" autoplay layout={{ fit: 'contain', align: [0.5, 0.5] }} />
6+
<LottieWrapper
7+
src="{base}/animation/dev-portal-dot.lottie"
8+
autoplay
9+
playOnVisible
10+
layout={{ fit: 'contain', align: [0.5, 0.5] }}
11+
renderConfig={{ autoResize: true }}
12+
/>

src/lib/vendor/LottieWrapper.svelte

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
<script lang="ts">
2-
// FORKED to support Svelte 5 cleanly and have access to the latest @lottiefiles/dotlottie-web
3-
// CHANGED STYLES: To support better responsiveness
4-
// ORGINAL SOURCE: https://github.com/LottieFiles/dotlottie-web/blob/main/packages/svelte/src/lib/Dotlottie.svelte
5-
// MIT LICENSED
2+
// FORKED to support:
3+
// - Svelte 5 cleanly and have access to the latest @lottiefiles/dotlottie-web
4+
// - Styles to support better responsiveness
5+
// - WebWorker for performance `DotLottieWorker as DotLottie`, src to full url if needed for the Worker, and workerId.
6+
// - playOnVisible: Added support for viewport playing
7+
// ORGINAL SOURCE (MIT LICENSED): https://github.com/LottieFiles/dotlottie-web/blob/main/packages/svelte/src/lib/Dotlottie.svelte
68
import { onMount } from 'svelte';
7-
import { DotLottie, type Config } from '@lottiefiles/dotlottie-web';
9+
import { DotLottieWorker as DotLottie, type Config } from '@lottiefiles/dotlottie-web';
10+
import viewport from '$lib/util/useViewportAction';
811
912
export function setWasmUrl(url: string): void {
1013
DotLottie.setWasmUrl(url);
@@ -28,6 +31,8 @@
2831
export let themeId: string = '';
2932
export let themeData: string = '';
3033
34+
export let playOnVisible: boolean = false;
35+
3136
export let dotLottieRefCallback: (dotLottie: DotLottie) => void = () => {};
3237
3338
const hoverHandler = (event: MouseEvent) => {
@@ -44,21 +49,25 @@
4449
let canvas: HTMLCanvasElement;
4550
let prevSrc: string | undefined = undefined;
4651
let prevData: Config['data'] = undefined;
52+
// Render each different src in a different worker
53+
let workerId = 'lottie-' + src.replace('/', '-');
4754
4855
onMount(() => {
4956
const shouldAutoplay = autoplay && !playOnHover;
5057
dotLottie = new DotLottie({
5158
canvas,
52-
src,
59+
src: src.includes('://') ? src : new URL(src, self.location.href).toString(),
5360
autoplay: shouldAutoplay,
5461
loop,
5562
speed,
5663
data,
57-
renderConfig,
64+
renderConfig: playOnVisible ? { freezeOnOffscreen: false, ...renderConfig } : renderConfig,
5865
segment,
5966
useFrameInterpolation,
6067
backgroundColor,
6168
mode,
69+
layout,
70+
workerId,
6271
});
6372
6473
if (dotLottieRefCallback) {
@@ -133,18 +142,19 @@
133142
134143
$: if (dotLottie && src !== prevSrc) {
135144
dotLottie.load({
136-
src,
145+
src: src.includes('://') ? src : new URL(src, self.location.href).toString(),
137146
autoplay,
138147
loop,
139148
speed,
140149
data,
141-
renderConfig,
150+
renderConfig: playOnVisible ? { freezeOnOffscreen: false, ...renderConfig } : renderConfig,
142151
segment,
143152
useFrameInterpolation,
144153
backgroundColor,
145154
mode,
146155
marker,
147156
layout,
157+
workerId,
148158
});
149159
prevSrc = src;
150160
}
@@ -156,13 +166,14 @@
156166
loop,
157167
speed,
158168
data,
159-
renderConfig,
169+
renderConfig: playOnVisible ? { freezeOnOffscreen: false, ...renderConfig } : renderConfig,
160170
segment,
161171
useFrameInterpolation,
162172
backgroundColor,
163173
mode,
164174
marker,
165175
layout,
176+
workerId,
166177
});
167178
prevData = data;
168179
}
@@ -180,21 +191,20 @@
180191
}
181192
</script>
182193

183-
<div>
184-
<canvas bind:this={canvas}></canvas>
185-
</div>
186-
187-
<style>
188-
div {
189-
width: 100%;
190-
height: auto;
191-
display: flex;
192-
justify-content: center;
193-
}
194-
canvas {
195-
width: 100%;
196-
height: auto;
197-
display: block;
198-
object-fit: contain;
199-
}
200-
</style>
194+
{#if playOnVisible}
195+
<canvas
196+
class={`block h-full w-full ${$$restProps.class}`}
197+
bind:this={canvas}
198+
use:viewport={{ threshold: 0.3 }}
199+
on:enterViewport={() => {
200+
dotLottie?.setFrame(0);
201+
dotLottie?.play();
202+
}}
203+
on:exitViewport={() => {
204+
dotLottie?.stop();
205+
}}
206+
>
207+
</canvas>
208+
{:else}
209+
<canvas class={$$restProps.class} bind:this={canvas}></canvas>
210+
{/if}

0 commit comments

Comments
 (0)