-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mouse wheel for scrollbar (#3606)
* fix(scrollbar): fix failed tests * feat(scrollbar): implement mouse wheel scroll behaviour * test(scrollbar): add mouse wheel tests * docs(scrollbar): describe mouse wheel scrolling options * docs(scrollbar): update demo
- Loading branch information
Showing
6 changed files
with
195 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { clamp, size, valuesOfKey } from "@antv/util"; | ||
import { COMPONENT_TYPE } from "../../../constant"; | ||
import { Action } from '..'; | ||
import { LooseObject } from "../../../interface"; | ||
|
||
function isWheelDown(event: LooseObject) { | ||
const wheelEvent = event.gEvent.originalEvent as WheelEvent; | ||
return wheelEvent.deltaY > 0; | ||
} | ||
|
||
const DEFAULT_WHEELDELTA = 1; | ||
class MousewheelScroll extends Action { | ||
|
||
public scroll(arg?) { | ||
const { view, event } = this.context; | ||
|
||
if (!view.getOptions().scrollbar) { | ||
return; | ||
} | ||
|
||
const wheelDelta = arg?.wheelDelta || DEFAULT_WHEELDELTA; | ||
const scrollbarController = view.getController('scrollbar'); | ||
|
||
const xScale = view.getXScale(); | ||
const data = view.getOptions().data; | ||
const dataSize = size(valuesOfKey(data, xScale.field)); | ||
const step = size(xScale.values); | ||
|
||
const currentRatio = scrollbarController.getValue(); | ||
const currentStart = Math.floor((dataSize - step) * currentRatio); | ||
|
||
const nextStart = currentStart + (isWheelDown(event) ? wheelDelta : -wheelDelta); | ||
const correction = wheelDelta / (dataSize - step) / 10000; | ||
const nextRatio = clamp(nextStart / (dataSize - step) + correction, 0, 1); | ||
scrollbarController.setValue(nextRatio); | ||
} | ||
} | ||
|
||
export default MousewheelScroll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters