@lottiefiles/dotlottie-svelte 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,16 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 LottieFiles.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6
+ documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
7
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
8
+ persons to whom the Software is furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
11
+ Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
+ OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,210 @@
1
+ # @lottiefiles/dotlottie-svelte
2
+
3
+ ![npm](https://img.shields.io/npm/v/@lottiefiles/dotlottie-svelte)
4
+ ![npm bundle size](https://img.shields.io/bundlephobia/minzip/%40lottiefiles%2Fdotlottie-svelte)
5
+ ![npm](https://img.shields.io/npm/dt/%40lottiefiles/dotlottie-svelte)
6
+ ![NPM](https://img.shields.io/npm/l/@lottiefiles/dotlottie-svelte)
7
+
8
+ <p align="center">
9
+ <img src="https://user-images.githubusercontent.com/23125742/201124166-c2a0bc2a-018b-463b-b291-944fb767b5c2.png" />
10
+ </p>
11
+
12
+ > 🚧 **Beta Alert:** We're still refining! The APIs in this package may undergo changes.
13
+
14
+ ## Contents
15
+
16
+ * [Introduction](#introduction)
17
+ * [What is dotLottie?](#what-is-dotlottie)
18
+ * [Installation](#installation)
19
+ * [Usage](#usage)
20
+ * [Live Examples](#live-examples)
21
+ * [APIs](#apis)
22
+ * [Props](#props)
23
+ * [Custom Playback Controls](#custom-playback-controls)
24
+ * [Listening to Events](#listening-to-events)
25
+ * [Development](#development)
26
+ * [Setup](#setup)
27
+ * [Dev](#dev)
28
+ * [Build](#build)
29
+
30
+ ## Introduction
31
+
32
+ A Svelte component for rendering [Lottie](https://lottiefiles.github.io/lottie-docs/) and [dotLottie](https://dotlottie.io) animations. It acts as a wrapper around the [`dotLottie`](../web/README.md) web player.
33
+
34
+ ### What is dotLottie?
35
+
36
+ dotLottie is an open-source file format that aggregates one or more Lottie files and their associated resources into a single file. They are ZIP archives compressed with the Deflate compression method and carry the file extension of ".lottie".
37
+
38
+ [Learn more about dotLottie](https://dotlottie.io/).
39
+
40
+ ## Installation
41
+
42
+ ```bash
43
+ npm install @lottiefiles/dotlottie-svelte
44
+ ```
45
+
46
+ ## Usage
47
+
48
+ ```svelte
49
+ <script lang="ts">
50
+ import { DotLottieSvelte } from '@lottiefiles/dotlottie-svelte';
51
+ </script>
52
+
53
+ <DotLottieSvelte
54
+ src="path/to/animation.lottie"
55
+ loop
56
+ autoplay
57
+ />
58
+ ```
59
+
60
+ ## Live Examples
61
+
62
+ ## APIs
63
+
64
+ ### Props
65
+
66
+ | Property name | Type | Required | Default | Description | |
67
+ | ----------------------- | ------------------------------------------------------- | :------: | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | - |
68
+ | `autoplay` | boolean | | false | Auto-starts the animation on load. | |
69
+ | `loop` | boolean | | false | Determines if the animation should loop. | |
70
+ | `src` | string | | undefined | URL to the animation data (`.json` or `.lottie`). | |
71
+ | `speed` | number | | 1 | Animation playback speed. 1 is regular speed. | |
72
+ | `data` | string \| ArrayBuffer | | undefined | Animation data provided either as a Lottie JSON string or as an ArrayBuffer for .lottie animations. | |
73
+ | `mode` | string | | "forward" | Animation play mode. Accepts "forward", "reverse", "bounce", "reverse-bounce". | |
74
+ | `backgroundColor` | string | | undefined | Background color of the canvas. Accepts 6-digit or 8-digit hex color string (e.g., "#000000", "#000000FF"), | |
75
+ | `segments` | \[number, number] | | undefined | Animation segments. Accepts an array of two numbers, where the first number is the start frame and the second number is the end frame. | |
76
+ | `renderConfig` | RenderConfig | | undefined | Configuration for rendering the animation. | |
77
+ | `dotLottieRefCallback` | (dotLottie: [DotLottie](../web/README.md#apis)) => void | | undefined | Callback function that receives a reference to the [`dotLottie`](../web/README.md) web player instance once instantiated. | |
78
+ | `useFrameInterpolation` | boolean | | true | Determines if the animation should update on subframes. If set to false, the original AE frame rate will be maintained. If set to true, it will refresh at each requestAnimationFrame, including intermediate values. The default setting is true. | |
79
+ | `autoResizeCanvas` | boolean | | true | Determines if the canvas should resize automatically to its container | |
80
+
81
+ #### RenderConfig
82
+
83
+ The `renderConfig` object accepts the following properties:
84
+
85
+ | Property name | Type | Required | Default | Description |
86
+ | ------------------ | ------ | :------: | ----------------------------- | ----------------------- |
87
+ | `devicePixelRatio` | number | | window\.devicePixelRatio \| 1 | The device pixel ratio. |
88
+
89
+ ## Custom Playback Controls
90
+
91
+ `DotLottieSvelte` component makes it easy to build custom playback controls for the animation. It exposes a `dotLottieRefCallback` prop that can be used to get a reference to the [`dotLottie`](../web/README.md#apis) web player instance. This instance can be used to control the playback of the animation using the methods exposed by the [`dotLottie`](../web/README.md#methods) web player instance.
92
+
93
+ Here is an example:
94
+
95
+ ```svelte
96
+ <script lang="ts">
97
+ import { DotLottieSvelte } from '@lottiefiles/dotlottie-svelte';
98
+ import type { DotLottie } from '@lottiefiles/dotlottie-svelte';
99
+
100
+ let dotLottie: DotLottie | null = null;
101
+
102
+ function play() {
103
+ dotLottie?.play();
104
+ }
105
+
106
+ function pause() {
107
+ dotLottie?.pause();
108
+ }
109
+
110
+ function stop() {
111
+ dotLottie?.stop();
112
+ }
113
+ </script>
114
+
115
+ <DotLottieSvelte
116
+ src="path/to/your/animation.lottie"
117
+ loop={true}
118
+ autoplay={true}
119
+ dotLottieRefCallback={(ref) => dotLottie = ref}
120
+ />
121
+
122
+ <button on:click={play}>Play</button>
123
+ <button on:click={pause}>Pause</button>
124
+ <button on:click={stop}>Stop</button>
125
+ ```
126
+
127
+ You can find the list of methods that can be used to control the playback of the animation [here](../web/README.md#methods).
128
+
129
+ ## Listening to Events
130
+
131
+ `DotLottieSvelte` component can receive a `dotLottieRefCallback` prop that can be used to get a reference to the [`dotLottie`](../web/README.md#apis) web player instance. This reference can be used to listen to player events emitted by the [`dotLottie`](../web/README.md#events) web instance.
132
+
133
+ Here is an example:
134
+
135
+ ```svelte
136
+ <script lang="ts">
137
+ import { DotLottieSvelte } from '@lottiefiles/dotlottie-svelte';
138
+ import type { DotLottie } from '@lottiefiles/dotlottie-svelte';
139
+
140
+ let dotLottie: DotLottie | null = null;
141
+
142
+ function onLoaded() {
143
+ console.log("Animation loaded");
144
+ }
145
+
146
+ function onPlay() {
147
+ console.log("Animation started");
148
+ }
149
+
150
+ function onPause() {
151
+ console.log("Animation paused");
152
+ }
153
+
154
+ function onComplete() {
155
+ console.log("Animation completed");
156
+ }
157
+
158
+ function setupListeners(dotLottieInstance) {
159
+ dotLottieInstance.addEventListener('load', onLoaded);
160
+ dotLottieInstance.addEventListener('play', onPlay);
161
+ dotLottieInstance.addEventListener('pause', onPause);
162
+ dotLottieInstance.addEventListener('complete', onComplete);
163
+ }
164
+
165
+ function removeListeners(dotLottieInstance) {
166
+ dotLottieInstance.removeEventListener('load', onLoaded);
167
+ dotLottieInstance.removeEventListener('play', onPlay);
168
+ dotLottieInstance.removeEventListener('pause', onPause);
169
+ dotLottieInstance.removeEventListener('complete', onComplete);
170
+ }
171
+
172
+ onDestroy(() => {
173
+ if (dotLottie) {
174
+ removeListeners(dotLottie);
175
+ }
176
+ });
177
+ </script>
178
+
179
+ <DotLottieSvelte
180
+ src="path/to/your/animation.lottie"
181
+ loop={true}
182
+ autoplay={true}
183
+ dotLottieRefCallback={(ref) => {
184
+ dotLottie = ref;
185
+ setupListeners(dotLottie);
186
+ }}
187
+ />
188
+ ```
189
+
190
+ [dotLottie](../web/README.md#apis) instance exposes multiple events that can be listened to. You can find the list of events [here](../web/README.md#events).
191
+
192
+ ## Development
193
+
194
+ ### Setup
195
+
196
+ ```bash
197
+ pnpm install
198
+ ```
199
+
200
+ ### Dev
201
+
202
+ ```bash
203
+ pnpm dev
204
+ ```
205
+
206
+ ### Build
207
+
208
+ ```bash
209
+ pnpm build
210
+ ```
@@ -0,0 +1,141 @@
1
+ <script>import { onMount } from "svelte";
2
+ import debounce from "debounce";
3
+ import { DotLottie } from "@lottiefiles/dotlottie-web";
4
+ export let autoplay = false;
5
+ export let backgroundColor = void 0;
6
+ export let data = void 0;
7
+ export let loop = false;
8
+ export let mode = "forward";
9
+ export let renderConfig = void 0;
10
+ export let segments = void 0;
11
+ export let speed = 1;
12
+ export let src = void 0;
13
+ export let useFrameInterpolation = true;
14
+ export let autoResizeCanvas = false;
15
+ export let dotLottieRefCallback = () => {
16
+ };
17
+ let dotLottie;
18
+ let canvas;
19
+ let prevSrc = void 0;
20
+ let prevData = void 0;
21
+ onMount(() => {
22
+ dotLottie = new DotLottie({
23
+ canvas,
24
+ src,
25
+ autoplay,
26
+ loop,
27
+ speed,
28
+ data,
29
+ renderConfig,
30
+ segments,
31
+ useFrameInterpolation,
32
+ backgroundColor,
33
+ mode
34
+ });
35
+ if (dotLottieRefCallback) {
36
+ dotLottieRefCallback(dotLottie);
37
+ }
38
+ const resizeObserver = new ResizeObserver(debounce(() => {
39
+ if (autoResizeCanvas) {
40
+ dotLottie.resize();
41
+ }
42
+ }, 150));
43
+ const intersectionObserver = new IntersectionObserver(debounce((entries) => {
44
+ entries.forEach((entry) => {
45
+ if (entry.isIntersecting) {
46
+ dotLottie.unfreeze();
47
+ } else {
48
+ dotLottie.freeze();
49
+ }
50
+ });
51
+ }, 150), { threshold: 0 });
52
+ if (autoResizeCanvas) {
53
+ resizeObserver.observe(canvas);
54
+ }
55
+ intersectionObserver.observe(canvas);
56
+ return () => {
57
+ resizeObserver.disconnect();
58
+ intersectionObserver.disconnect();
59
+ dotLottie.destroy();
60
+ };
61
+ });
62
+ $: {
63
+ if (dotLottie && dotLottie.isLoaded && typeof speed == "number") {
64
+ dotLottie.setSpeed(speed);
65
+ }
66
+ }
67
+ $: {
68
+ if (dotLottie && dotLottie.isLoaded && typeof useFrameInterpolation == "boolean") {
69
+ dotLottie.setUseFrameInterpolation(useFrameInterpolation);
70
+ }
71
+ }
72
+ $: {
73
+ if (dotLottie && dotLottie.isLoaded && Array.isArray(segments) && segments.length === 2 && typeof segments[0] === "number" && typeof segments[1] === "number") {
74
+ let [start, end] = segments;
75
+ dotLottie.setSegments(start, end);
76
+ }
77
+ }
78
+ $: {
79
+ if (dotLottie && dotLottie.isLoaded && typeof loop == "boolean") {
80
+ dotLottie.setLoop(loop);
81
+ }
82
+ }
83
+ $: {
84
+ if (dotLottie) {
85
+ dotLottie.setBackgroundColor(backgroundColor || "");
86
+ }
87
+ }
88
+ $: {
89
+ if (dotLottie && dotLottie.isLoaded && typeof mode == "string") {
90
+ dotLottie.setMode(mode);
91
+ }
92
+ }
93
+ $:
94
+ if (dotLottie && src !== prevSrc) {
95
+ dotLottie.load({
96
+ src,
97
+ autoplay,
98
+ loop,
99
+ speed,
100
+ data,
101
+ renderConfig,
102
+ segments,
103
+ useFrameInterpolation,
104
+ backgroundColor,
105
+ mode
106
+ });
107
+ prevSrc = src;
108
+ }
109
+ $:
110
+ if (dotLottie && data !== prevData) {
111
+ dotLottie.load({
112
+ src,
113
+ autoplay,
114
+ loop,
115
+ speed,
116
+ data,
117
+ renderConfig,
118
+ segments,
119
+ useFrameInterpolation,
120
+ backgroundColor,
121
+ mode
122
+ });
123
+ prevData = data;
124
+ }
125
+ </script>
126
+
127
+ <div>
128
+ <canvas bind:this={canvas}></canvas>
129
+ </div>
130
+
131
+ <style>
132
+ div {
133
+ width: 100%;
134
+ height: 100%;
135
+ }
136
+ canvas {
137
+ width: 100%;
138
+ height: 100%;
139
+ display: block;
140
+ }
141
+ </style>
@@ -0,0 +1,28 @@
1
+ import { SvelteComponent } from "svelte";
2
+ import { DotLottie, type Config } from '@lottiefiles/dotlottie-web';
3
+ declare const __propDef: {
4
+ props: {
5
+ autoplay?: Config['autoplay'];
6
+ backgroundColor?: Config['backgroundColor'];
7
+ data?: Config['data'];
8
+ loop?: Config['loop'];
9
+ mode?: Config['mode'];
10
+ renderConfig?: Config['renderConfig'];
11
+ segments?: Config['segments'];
12
+ speed?: Config['speed'];
13
+ src?: Config['src'];
14
+ useFrameInterpolation?: Config['useFrameInterpolation'];
15
+ autoResizeCanvas?: boolean | undefined;
16
+ dotLottieRefCallback?: ((dotLottie: DotLottie) => void) | undefined;
17
+ };
18
+ events: {
19
+ [evt: string]: CustomEvent<any>;
20
+ };
21
+ slots: {};
22
+ };
23
+ export type DotlottieProps = typeof __propDef.props;
24
+ export type DotlottieEvents = typeof __propDef.events;
25
+ export type DotlottieSlots = typeof __propDef.slots;
26
+ export default class Dotlottie extends SvelteComponent<DotlottieProps, DotlottieEvents, DotlottieSlots> {
27
+ }
28
+ export {};
@@ -0,0 +1,2 @@
1
+ export type * from '@lottiefiles/dotlottie-web';
2
+ export { default as DotLottieSvelte } from './Dotlottie.svelte';
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { default as DotLottieSvelte } from './Dotlottie.svelte';
package/package.json ADDED
@@ -0,0 +1,76 @@
1
+ {
2
+ "name": "@lottiefiles/dotlottie-svelte",
3
+ "version": "0.0.0",
4
+ "type": "module",
5
+ "description": "Svelte component wrapper around the dotlottie-web library to render Lottie and dotLottie animations",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/LottieFiles/dotlottie-web.git",
9
+ "directory": "packages/svelte"
10
+ },
11
+ "homepage": "https://github.com/LottieFiles/dotlottie-web#readme",
12
+ "bugs": "https://github.com/LottieFiles/dotlottie-web/issues",
13
+ "author": "LottieFiles",
14
+ "contributors": [
15
+ "Abdelrahman Ashraf <a.theashraf@gmail.com>"
16
+ ],
17
+ "maintainers": [
18
+ "Abdelrahman Ashraf <a.theashraf@gmail.com>"
19
+ ],
20
+ "svelte": "./dist/index.js",
21
+ "types": "./dist/index.d.ts",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "svelte": "./dist/index.js"
26
+ }
27
+ },
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "license": "MIT",
32
+ "peerDependencies": {
33
+ "svelte": "^4.0.0"
34
+ },
35
+ "dependencies": {
36
+ "debounce": "^2.0.0",
37
+ "@lottiefiles/dotlottie-web": "0.15.0"
38
+ },
39
+ "devDependencies": {
40
+ "@sveltejs/adapter-auto": "^3.0.0",
41
+ "@sveltejs/kit": "^2.0.0",
42
+ "@sveltejs/package": "^2.0.0",
43
+ "@sveltejs/vite-plugin-svelte": "^3.0.0",
44
+ "@types/eslint": "8.56.0",
45
+ "@typescript-eslint/eslint-plugin": "^6.0.0",
46
+ "@typescript-eslint/parser": "^6.0.0",
47
+ "eslint": "^8.56.0",
48
+ "eslint-config-prettier": "^9.1.0",
49
+ "eslint-plugin-svelte": "^2.36.0-next.4",
50
+ "prettier": "^2.8.8",
51
+ "prettier-plugin-svelte": "^3.1.2",
52
+ "publint": "^0.1.9",
53
+ "svelte": "^5.0.0-next.1",
54
+ "svelte-check": "^3.6.0",
55
+ "tslib": "^2.4.1",
56
+ "typescript": "^5.0.3",
57
+ "vite": "^5.0.11",
58
+ "vitest": "^1.2.2"
59
+ },
60
+ "publishConfig": {
61
+ "access": "public"
62
+ },
63
+ "scripts": {
64
+ "dev": "vite dev",
65
+ "build": "vite build && npm run package",
66
+ "preview": "vite preview",
67
+ "package": "svelte-kit sync && svelte-package && publint",
68
+ "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
69
+ "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
70
+ "lint": "prettier --check . && eslint .",
71
+ "format": "prettier --write .",
72
+ "stats:eslint": "cross-env TIMING=1 eslint .",
73
+ "stats:ts": "tsc -p tsconfig.build.json --extendedDiagnostics",
74
+ "type-check": "tsc --noEmit"
75
+ }
76
+ }