⚠️ Work In Progress: While this library is functional and usable in its current state, it's still under active development. Breaking changes may occur in future versions.
A powerful and flexible zooming user interface library for Svelte applications. Create infinite canvas experiences with intuitive pan and zoom controls.
npm install svelte-zooming-ui
The main container component that provides the zooming canvas functionality:
<script>
import { ZoomingUIComponent } from 'svelte-zooming-ui';
let lookAt;
</script>
<ZoomingUIComponent bind:lookAt debug={false}>
<!-- Your zoomable content here -->
</ZoomingUIComponent>
A component for positioning elements within the zooming canvas:
<script>
import { ZoomingUIComponent, Positionable } from 'svelte-zooming-ui';
import Decimal from 'decimal.js';
</script>
<ZoomingUIComponent>
<Positionable
x={Decimal(0)}
y={Decimal(0)}
width={Decimal(100)}
height={Decimal(100)}
depth={Decimal(1)}
>
<div>Your content here</div>
</Positionable>
</ZoomingUIComponent>
The following is exported:
lookAt
: Function to programmatically move the camera (x, y, scale, duration?, easing?)
The following are available through Svelte context:
screen
: A writable store containing viewport dimensions and position {x, y, w, h}
camera
: A writable store containing view transformation state {x, y, z, scale, w, h, fov}
focusOn
: Function to focus on a specific area (x, y, w, h, duration?, easing?, ratio?)
You can control the camera programmatically using the exported lookAt
function, or access camera state through context:
<script>
import { ZoomingUIComponent, lookAt } from 'svelte-zooming-ui';
import { getContext } from 'svelte';
import Decimal from 'decimal.js';
const camera = getContext('camera');
// Subscribe to camera changes
$: console.log('Current scale:', $camera.scale.toString());
function zoomIn() {
// Double the current zoom level
lookAt(
$camera.x,
$camera.y,
$camera.scale.times(2)
);
}
</script>
<ZoomingUIComponent>
<button on:click={zoomIn}>Zoom In</button>
<div>Current zoom: {$camera.scale.toFixed(2)}x</div>
</ZoomingUIComponent>
The camera context provides real-time access to:
<script>
let lookAt;
function centerOn(x, y, scale) {
lookAt(x, y, scale);
}
</script>
<ZoomingUIComponent bind:lookAt>
<button on:click={() => centerOn(Decimal(0), Decimal(0), Decimal(1))}>
Reset View
</button>
</ZoomingUIComponent>
Manage content detail based on zoom level:
<Positionable x={x} y={y} width={width} height={height}>
{#if $frame.ratio <= 20.0}
<HighDetailContent />
{:else}
<LowDetailContent />
{/if}
</Positionable>
debug
(boolean): Enables debug visualizationlookAt
(function): Bind to control camera positionx
(Decimal): X coordinatey
(Decimal): Y coordinatewidth
(Decimal): Widthheight
(Decimal): Heightdepth
(Decimal): Z-index depthdebug
(boolean): Enables debug visualizationCheck out our example components:
Clickable.svelte
: Interactive area exampleLOD.svelte
: Level of detail implementationEmbedded.svelte
: Nested content exampleContributions are welcome! Please see our contributing guidelines for details.
MIT License - see LICENSE.md for details