A high-performance, interactive 3D flipbook component for Svelte applications that can process and display PDF documents with smooth page-turning animations and touch support. Built on top of PDF.js for reliable PDF rendering.
# Using npm
npm install svelte-flipbook
# Using pnpm
pnpm add svelte-flipbook
# Using yarn
yarn add svelte-flipbook
<script>
import Flipbook from 'svelte-flipbook';
const pages = [
'/path/to/page1.jpg',
'/path/to/page2.jpg',
'/path/to/page3.jpg',
];
</script>
<Flipbook {pages} />
<script>
import { PdfFlipbook } from 'svelte-flipbook';
</script>
<!-- Load PDF from URL -->
<PdfFlipbook source="/path/to/document.pdf" />
<!-- Or load PDF from Blob/File -->
<PdfFlipbook source={blob} />
The PDF component will automatically handle:
FlipbookFor displaying image galleries with page-turning effects.
| Prop | Type | Default | Description |
|---|---|---|---|
pages |
string[] |
[] |
Array of image URLs to display as pages |
startPage |
number |
0 |
Initial page to display |
nPolygons |
number |
10 |
Number of polygons used for page curl effect |
dragToFlip |
boolean |
true |
Enable/disable drag to flip |
singlePage |
boolean |
false |
Show one page at a time |
forwardDirection |
'left'|'right' |
'right' |
Direction of page turning |
zooms |
number[] |
[1, 2, 4] |
Available zoom levels |
zoomDuration |
number |
500 |
Zoom animation duration in ms |
clickToZoom |
boolean |
true |
Enable/disable click to zoom |
PdfFlipbookFor displaying PDF documents with automatic page rendering.
| Prop | Type | Default | Description |
|---|---|---|---|
source |
string | File | Blob |
- | PDF source (URL, File, or Blob) |
startPage |
number |
0 |
Initial page to display |
nPolygons |
number |
10 |
Number of polygons used for page curl effect |
dragToFlip |
boolean |
true |
Enable/disable drag to flip |
singlePage |
boolean |
false |
Show one page at a time |
forwardDirection |
'left'|'right' |
'right' |
Direction of page turning |
zooms |
number[] |
[1, 2, 4] |
Available zoom levels |
zoomDuration |
number |
500 |
Zoom animation duration in ms |
clickToZoom |
boolean |
true |
Enable/disable click to zoom |
| Event | Payload | Description |
|---|---|---|
pageChange |
{ page: number } |
Triggered when page changes |
zoom |
{ zoom: number } |
Triggered when zoom level changes |
load |
{ loaded: number, total: number } |
Triggered when pages are loaded |
Access methods using bind:this:
<script>
import Flipbook from 'svelte-flipbook';
let flipbook;
function nextPage() {
if (flipbook) flipbook.nextPage();
}
</script>
<Flipbook bind:this={flipbook} />
<button on:click={nextPage}>Next Page</button>
nextPage() - Go to next pageprevPage() - Go to previous pagegoToPage(page: number) - Go to specific pagezoomIn() - Zoom in one levelzoomOut() - Zoom out one levelzoomTo(level: number) - Zoom to specific level<Flipbook {pages} let:flipLeft let:flipRight let:zoomIn let:zoomOut>
<div class="controls">
<button on:click={flipLeft} disabled={!canFlipLeft}>
Previous
</button>
<button on:click={flipRight} disabled={!canFlipRight}>
Next
</button>
<button on:click={zoomIn}>+</button>
<button on:click={zoomOut}>-</button>
</div>
</Flipbook>
<style>
.controls {
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 10px;
z-index: 100;
}
button {
padding: 8px 16px;
border: none;
border-radius: 4px;
background: #333;
color: white;
cursor: pointer;
}
button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
</style>
<Flipbook {pages} let:loading>
{#if loading}
<div class="loading">
Loading pages...
</div>
{/if}
</Flipbook>
<style>
.loading {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 20px;
background: rgba(0, 0, 0, 0.7);
color: white;
border-radius: 8px;
}
</style>
The PdfFlipbook component uses PDF.js under the hood to render PDF documents. It includes a pre-configured worker for optimal performance.
Progressive Loading:
Memory Management:
Built-in Worker:
Error Handling:
When building for production, the PDF worker will be automatically included from the static folder. No additional configuration is needed.
# Install dependencies
pnpm install
# Build for production
pnpm build
To start the development server with hot module replacement:
pnpm dev
pnpm install
pnpm dev
pnpm build
pnpm test