Skip to content

Commit

Permalink
feat: zoom in/out of segments using mouse wheel
Browse files Browse the repository at this point in the history
  • Loading branch information
mamane10 committed May 23, 2024
1 parent f211673 commit 037fea3
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/timelines.js
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,36 @@ export default Kapsule({
})
)

state.graph.on("wheel", event => {
event.stopPropagation();
event.preventDefault();
const getPointerCoords = event => d3Pointer(event, state.graph.node());
if (event.altKey && event.ctrlKey) {

const pointerCoords = getPointerCoords(event);

let newDomainX;
if (event.deltaY < 0) {
// scrolling up
newDomainX = [pointerCoords[0] - event.pageY, pointerCoords[0] + event.pageY].sort(d3Ascending).map(state.xScale.invert);
} else {
// scrolling down
const minRange = d3Min(state.completeFlatData, d => d.timeRange[0]);
const maxRange = d3Max(state.completeFlatData, d => d.timeRange[1]);
let x0 = +state.zoomX[0] - event.pageX;
let x1 = +state.zoomX[1] + event.pageX;
x0 = x0 < minRange ? minRange : x0;
x1 = x1 > maxRange ? maxRange : x1;
newDomainX = [x0, x1];
}

state.svg.dispatch('zoom', { detail: {
zoomX: newDomainX,
}});

}
})

timelines = timelines.merge(newSegments);

timelines.transition().duration(state.transDuration)
Expand Down

0 comments on commit 037fea3

Please sign in to comment.