react-plot
Version:
Library of React components to render SVG 2D plots.
25 lines • 1.2 kB
JavaScript
import { usePlotControls, usePlotEvents, } from '../contexts/plotController/plotControllerContext.js';
export function useAxisWheelZoom(options = {}) {
const { direction = 'vertical', axisId = direction === 'horizontal' ? 'x' : 'y', center = 0, factor = 1, invert = false, } = options;
const plotControls = usePlotControls(options);
usePlotEvents({
onWheel({ event, coordinates: { [axisId]: pointerPosition }, domains: { [axisId]: [oldMin, oldMax], }, }) {
event.preventDefault();
const position = center === 'pointer' ? pointerPosition : center;
const delta = event.deltaY * (invert ? -0.001 : 0.001) * factor;
const ratio = delta < 0 ? -1 / (delta - 1) : 1 + delta;
const min = position - (position - oldMin) * ratio;
const max = position + (oldMax - position) * ratio;
plotControls.setAxis(axisId, {
min,
max,
});
},
onDoubleClick({ event: { button } }) {
if (button !== 0)
return;
plotControls.resetAxis(axisId);
},
}, options);
}
//# sourceMappingURL=useAxisWheelZoom.js.map