parent
a9b894d066
commit
5121ffddfa
@ -1,26 +1,40 @@
|
|||||||
import { createSignal } from "solid-js";
|
import { createSignal } from "solid-js";
|
||||||
import { Pannable, PannableOptions, PannableState, Touch } from "../index.js";
|
import { Pannable, PannableOptions, PannableState, Touch } from "../index.js";
|
||||||
|
|
||||||
export function usePannable(config: PannableOptions) {
|
export type UsePannableOptions = {
|
||||||
|
center?: () => [x: number, y: number],
|
||||||
|
};
|
||||||
|
|
||||||
|
export function usePannable(config: PannableOptions & UsePannableOptions) {
|
||||||
const pannable = new Pannable(config);
|
const pannable = new Pannable(config);
|
||||||
const [state, setState] = createSignal<PannableState>(pannable.getState());
|
const [state, setState] = createSignal<PannableState>(pannable.getState());
|
||||||
|
const center = config.center ?? (() => [0, 0]);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
update(touches: Map<number, Touch>) {
|
update(touches: Map<number, Touch>) {
|
||||||
pannable.update(touches);
|
pannable.update(touches, ...center());
|
||||||
},
|
},
|
||||||
move(touches: Map<number, Touch>) {
|
move(touches: Map<number, Touch>) {
|
||||||
pannable.move(touches);
|
pannable.move(touches, ...center());
|
||||||
const newState = pannable.getState();
|
const newState = pannable.getState();
|
||||||
setState(newState);
|
setState(newState);
|
||||||
return newState;
|
return newState;
|
||||||
},
|
},
|
||||||
zoom(amount: number, clientX?: number, clientY?: number) {
|
zoom(amount: number, clientX?: number, clientY?: number) {
|
||||||
pannable.zoom(amount, clientX, clientY);
|
const [cx, cy] = center();
|
||||||
|
pannable.zoom(amount, (clientX ?? 0) - cx, (clientY ?? 0) - cy);
|
||||||
const newState = pannable.getState();
|
const newState = pannable.getState();
|
||||||
setState(newState);
|
setState(newState);
|
||||||
return newState;
|
return newState;
|
||||||
},
|
},
|
||||||
getState: state,
|
getState: () => {
|
||||||
|
const result = state();
|
||||||
|
const [cx, cy] = center();
|
||||||
|
return {
|
||||||
|
...result,
|
||||||
|
dx: result.dx + cx,
|
||||||
|
dy: result.dy + cy,
|
||||||
|
};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue