Add some panzoom utils

This commit is contained in:
ZXY101
2023-08-01 18:05:39 +02:00
parent 995c6a98c5
commit 00f438a290
4 changed files with 87 additions and 93 deletions

View File

@@ -32,78 +32,6 @@
currentPage: 9, currentPage: 9,
totalPages: 59 totalPages: 59
}, },
{
title: 'sdhfjksdh',
cover: image5,
currentPage: 19,
totalPages: 200
},
{
title: 'sdhfjksdh',
cover: image5,
currentPage: 19,
totalPages: 200
},
{
title: 'sdhfjksdh',
cover: image5,
currentPage: 19,
totalPages: 200
},
{
title: 'sdhfjksdh',
cover: image5,
currentPage: 19,
totalPages: 200
},
{
title: 'sdhfjksdh',
cover: image5,
currentPage: 19,
totalPages: 200
},
{
title: 'sdhfjksdh',
cover: image5,
currentPage: 19,
totalPages: 200
},
{
title: 'sdhfjksdh',
cover: image5,
currentPage: 19,
totalPages: 200
},
{
title: 'sdhfjksdh',
cover: image5,
currentPage: 19,
totalPages: 200
},
{
title: 'sdhfjksdh',
cover: image5,
currentPage: 19,
totalPages: 200
},
{
title: 'sdhfjksdh',
cover: image5,
currentPage: 19,
totalPages: 200
},
{
title: 'sdhfjksdh',
cover: image5,
currentPage: 19,
totalPages: 200
},
{
title: 'sdhfjksdh',
cover: image5,
currentPage: 19,
totalPages: 200
},
{ {
title: 'sdhfjksdh', title: 'sdhfjksdh',
cover: image5, cover: image5,

View File

@@ -3,7 +3,14 @@
</script> </script>
<div> <div>
<div use:initPanzoom> <div class="container" use:initPanzoom>
<slot /> <slot />
</div> </div>
</div> </div>
<style>
.container {
display: flex;
justify-content: center;
}
</style>

View File

@@ -2,11 +2,14 @@ import panzoom from 'panzoom';
import type { PanZoom } from 'panzoom'; import type { PanZoom } from 'panzoom';
import { writable } from 'svelte/store'; import { writable } from 'svelte/store';
export const pz = writable<PanZoom | undefined>(undefined); let pz: PanZoom | undefined;
let container: HTMLElement | undefined;
export const panzoomStore = writable<PanZoom | undefined>(undefined);
export function initPanzoom(node: HTMLElement) { export function initPanzoom(node: HTMLElement) {
pz.set( container = node;
panzoom(node, { pz = panzoom(node, {
bounds: false, bounds: false,
maxZoom: 10, maxZoom: 10,
minZoom: 0.1, minZoom: 0.1,
@@ -18,6 +21,57 @@ export function initPanzoom(node: HTMLElement) {
}, },
beforeWheel: (e) => e.altKey, beforeWheel: (e) => e.altKey,
onTouch: (e) => e.touches.length > 1 onTouch: (e) => e.touches.length > 1
}) });
);
panzoomStore.set(pz)
}
type PanX = 'left' | 'center' | 'right'
type PanY = 'top' | 'center' | 'bottom'
export function panAlign(alignX: PanX, alignY: PanY) {
if (!pz || !container) {
return
}
const { scale } = pz.getTransform();
const { innerWidth, innerHeight } = window
const { offsetWidth, offsetHeight } = container
let x = 0;
let y = 0;
switch (alignX) {
case 'left':
x = 0;
break;
case 'center':
x = (innerWidth - offsetWidth * scale) / 2;
break;
case 'right':
x = (innerWidth - offsetWidth * scale);
break;
}
switch (alignY) {
case 'top':
y = 0;
break;
case 'center':
y = (innerHeight - offsetHeight * scale) / 2;
break;
case 'bottom':
y = (innerHeight - offsetHeight * scale);
break;
}
console.log(x, y);
pz?.moveTo(x, y)
}
export function zoomOriginal() {
pz?.moveTo(0, 0);
pz?.zoomTo(0, 0, 1 / pz.getTransform().scale);
panAlign('center', 'center');
} }

View File

@@ -1,22 +1,27 @@
<script lang="ts"> <script lang="ts">
import Panzoom from '$lib/panzoom/Panzoom.svelte';
import { currentManga } from '$lib/catalog'; import { currentManga } from '$lib/catalog';
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import Button from '$lib/components/Button.svelte';
import { panAlign, Panzoom, zoomOriginal } from '$lib/panzoom';
const manga = $currentManga; const manga = $currentManga;
</script> </script>
{#if manga} {#if manga}
<div>
<Button on:click={zoomOriginal}>Reset Zoom</Button>
</div>
<Panzoom> <Panzoom>
<img draggable="false" src={manga.cover} alt={manga.title} /> <img draggable="false" src={manga.cover} alt={manga.title} />
</Panzoom> </Panzoom>
{/if} {/if}
<style> <style>
h2 { div {
z-index: 1; position: absolute;
position: fixed; bottom: 10px;
left: 10px; left: 10px;
z-index: 1;
} }
</style> </style>