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

Svelte 5 - Sprite animation #33

Open
wants to merge 8 commits into
base: svelte-5
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/forty-pears-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@288-toolkit/sprite-animation': major
---

Breaking: upgrade to svelte-5
3 changes: 2 additions & 1 deletion packages/components/dismissable/vitest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { svelteInlineComponent } from '@288-toolkit/vite-plugin-svelte-inline-component';
import { sveltekit } from '@sveltejs/kit/vite';
import { svelteTesting } from '@testing-library/svelte/vite';
import { defineProject, mergeConfig } from 'vitest/config';
import baseConfig from '../../../vitest.shared';

Expand All @@ -10,6 +11,6 @@ export default mergeConfig(
setupFiles: ['./test/setup.ts'],
alias: [{ find: /^svelte$/, replacement: 'svelte/internal' }]
},
plugins: [sveltekit(), svelteInlineComponent()]
plugins: [sveltekit(), svelteTesting(), svelteInlineComponent()]
})
);
71 changes: 8 additions & 63 deletions packages/components/sprite-animation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,69 +19,14 @@ less than one, it won't start the animation.

## Props

### url

The URL of the sprite.

```ts
export let url: string;
```

### width

The width in pixels of a single frame in the sprite.

```ts
export let width: number;
```

### height

The height in pixels of a single frame in the sprite.

```ts
export let height: number;
```

### speed

The speed in milliseconds of the animation. Default: 100ms.

```ts
export let speed = 100;
```

### cols

The number of columns in the sprite.

```ts
export let cols = 1;
```

### rows

The number of rows in the sprite.

```ts
export let rows = 1;
```

### loop

Whether the animation should loop.

```ts
export let loop = true;
```

### still

The still image to show when the animation is not running.

```ts
export let still: Maybe<string> = null;
```
- `url` - `string`: The URL of the sprite.
- `width` - `number`: The width in pixels of a single frame in the sprite.
- `height` - `number`: The height in pixels of a single frame in the sprite.
- `speed` - `number`: The speed in milliseconds of the animation. Default: 100ms.
- `cols` - `number`: The number of columns in the sprite.
- `rows` - `number`: The number of rows in the sprite.
- `loop` - `boolean`: Whether the animation should loop.
- `still` - `string`: The still image to show when the animation is not running.

## Example

Expand Down
10 changes: 5 additions & 5 deletions packages/components/sprite-animation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@
}
},
"peerDependencies": {
"svelte": "4.x || 5.x",
"motion": "10.x"
"motion": "11.x",
"svelte": "5.x"
},
"dependencies": {
"@288-toolkit/device": "workspace:^",
"@288-toolkit/types": "workspace:^",
"esm-env": "1.0.0"
"esm-env": "1.2.2"
},
"devDependencies": {
"@288-toolkit/vite-plugin-svelte-inline-component": "workspace:^",
"@testing-library/svelte": "^5.1.0",
"svelte-preprocess": "5.1.4"
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/svelte": "^5.2.6"
}
}
109 changes: 62 additions & 47 deletions packages/components/sprite-animation/src/lib/SpriteAnimation.svelte
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script context="module" lang="ts">
<script module lang="ts">
import { estimatedAvailableMegaBytesPerSeconds } from './estimatedAvailableMegaBytesPerSeconds.js';
import { BROWSER, DEV } from 'esm-env';

Expand All @@ -23,42 +23,55 @@
import { inView } from 'motion';
import type { Maybe } from '@288-toolkit/types';

/**
* The url of the sprite.
*/
export let url: string;
/**
* The width in px of a single frame in the sprite.
*/
export let width: number;
/**
* The height in px of a single frame in the sprite.
*/
export let height: number;
/**
* The speed in ms of the animation.
*/
export let speed = 100;
/**
* The number of columns in the sprite.
*/
export let cols = 1;
/**
* The number of rows in the sprite.
*/
export let rows = 1;
/**
* Wether the animation should loop.
*/
export let loop = true;
/**
* The still image to show when the animation is not running.
*/
export let still: Maybe<string> = null;
interface Props {
/**
* The url of the sprite.
*/
url: string;
/**
* The width in px of a single frame in the sprite.
*/
width: number;
/**
* The height in px of a single frame in the sprite.
*/
height: number;
/**
* The speed in ms of the animation.
*/
speed?: number;
/**
* The number of columns in the sprite.
*/
cols?: number;
/**
* The number of rows in the sprite.
*/
rows?: number;
/**
* Wether the animation should loop.
*/
loop?: boolean;
/**
* The still image to show when the animation is not running.
*/
still?: Maybe<string>;
}

let {
url,
width,
height,
speed = 100,
cols = 1,
rows = 1,
loop = true,
still = null
}: Props = $props();

const count = cols * rows;
const paddingRatio = (height / width) * 100;
let running = !still;
let running = $state(!still);
let animation: Animation;

const createBackgroundPosition = (x: number, y: number) => {
Expand Down Expand Up @@ -163,18 +176,20 @@
};
};

$: renderedUrl = running || !still ? url : still;
let renderedUrl = $derived(running || !still ? url : still);

$: if (DEV) {
if (!url) {
throw new Error('Cannot have a sprite with no url');
}
if (cols < 1 || rows < 1) {
throw new Error('Cannot have a sprite with no cols or no rows');
}
if (width < 1 || height < 1) {
throw new Error('Cannot have a sprite with no width or no height');
}
if (DEV) {
$effect(() => {
if (!url) {
throw new Error('Cannot have a sprite with no url');
}
if (cols < 1 || rows < 1) {
throw new Error('Cannot have a sprite with no cols or no rows');
}
if (width < 1 || height < 1) {
throw new Error('Cannot have a sprite with no width or no height');
}
});
}
</script>

Expand All @@ -189,11 +204,11 @@
--background-size: {cols * 100}% {rows * 100}%;
--padding-bottom: {paddingRatio}%;
"
/>
></div>
{/if}

<style>
:global(._sprite-animation) {
._sprite-animation {
width: 100%;
height: 0;
padding-bottom: var(--padding-bottom, 100%);
Expand Down
4 changes: 2 additions & 2 deletions packages/components/sprite-animation/svelte.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import preprocess from 'svelte-preprocess';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

/** @type {import('@sveltejs/kit').Config} */
export default {
compilerOptions: {
accessors: !!process.env.VITEST
},
preprocess: [preprocess()]
preprocess: [vitePreprocess()]
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render } from '@testing-library/svelte';
import { render } from '@testing-library/svelte/svelte5';
import { afterEach, beforeEach, expect, test, vi } from 'vitest';
import SpriteAnimation from '../src/lib/SpriteAnimation.svelte';

Expand Down
1 change: 1 addition & 0 deletions packages/components/sprite-animation/test/setup.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import '@testing-library/jest-dom/vitest';
import '../../../../shared/test/mocks/sveltekit';
6 changes: 3 additions & 3 deletions packages/components/sprite-animation/vitest.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { svelteInlineComponent } from '@288-toolkit/vite-plugin-svelte-inline-component';
import { sveltekit } from '@sveltejs/kit/vite';
import { svelteTesting } from '@testing-library/svelte/vite';
import { defineProject, mergeConfig } from 'vitest/config';
import baseConfig from '../../../vitest.shared';

export default mergeConfig(
baseConfig,
defineProject({
test: {
setupFiles: ['./test/setup.ts'],
alias: [{ find: /^svelte$/, replacement: 'svelte/internal' }]
setupFiles: ['./test/setup.ts']
},
plugins: [sveltekit(), svelteInlineComponent()]
plugins: [sveltekit(), svelteTesting(), svelteInlineComponent()]
})
);
Loading
Loading