Skip to content

Commit df07e5f

Browse files
authored
Merge pull request #2372 from WillCMcC/feat/exponential-scroll
Add boolean flag for exponential scroll to MouseRangeManiplator
2 parents 4346fe9 + 8955782 commit df07e5f

File tree

1 file changed

+36
-7
lines changed
  • Sources/Interaction/Manipulators/MouseRangeManipulator

1 file changed

+36
-7
lines changed

Sources/Interaction/Manipulators/MouseRangeManipulator/index.js

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,16 @@ function vtkMouseRangeManipulator(publicAPI, model) {
2525
function processDelta(listener, delta) {
2626
const oldValue = listener.getValue();
2727

28-
// Apply scale and cached delta to current delta
29-
const newDelta = delta * listener.scale + incrementalDelta.get(listener);
28+
// if exponential scroll is enabled, we raise our scale to the
29+
// exponent of the net delta of the interaction. The further away
30+
// the user's cursor is from the start of the interaction, the more
31+
// their movements will effect the value.
32+
const scalingFactor = listener.exponentialScroll
33+
? listener.scale ** Math.log2(Math.abs(model.interactionNetDelta) + 2)
34+
: listener.scale;
35+
36+
const newDelta = delta * scalingFactor + incrementalDelta.get(listener);
37+
3038
let value = oldValue + newDelta;
3139

3240
// Compute new value based on step
@@ -67,7 +75,8 @@ function vtkMouseRangeManipulator(publicAPI, model) {
6775
step,
6876
getValue,
6977
setValue,
70-
scale = 1
78+
scale = 1,
79+
exponentialScroll = false
7180
) => {
7281
const getFn = Number.isFinite(getValue) ? () => getValue : getValue;
7382
model.horizontalListener = {
@@ -77,6 +86,7 @@ function vtkMouseRangeManipulator(publicAPI, model) {
7786
getValue: getFn,
7887
setValue,
7988
scale,
89+
exponentialScroll,
8090
};
8191
incrementalDelta.set(model.horizontalListener, 0);
8292
publicAPI.modified();
@@ -89,7 +99,8 @@ function vtkMouseRangeManipulator(publicAPI, model) {
8999
step,
90100
getValue,
91101
setValue,
92-
scale = 1
102+
scale = 1,
103+
exponentialScroll = false
93104
) => {
94105
const getFn = Number.isFinite(getValue) ? () => getValue : getValue;
95106
model.verticalListener = {
@@ -99,6 +110,7 @@ function vtkMouseRangeManipulator(publicAPI, model) {
99110
getValue: getFn,
100111
setValue,
101112
scale,
113+
exponentialScroll,
102114
};
103115
incrementalDelta.set(model.verticalListener, 0);
104116
publicAPI.modified();
@@ -111,10 +123,19 @@ function vtkMouseRangeManipulator(publicAPI, model) {
111123
step,
112124
getValue,
113125
setValue,
114-
scale = 1
126+
scale = 1,
127+
exponentialScroll = false
115128
) => {
116129
const getFn = Number.isFinite(getValue) ? () => getValue : getValue;
117-
model.scrollListener = { min, max, step, getValue: getFn, setValue, scale };
130+
model.scrollListener = {
131+
min,
132+
max,
133+
step,
134+
getValue: getFn,
135+
setValue,
136+
scale,
137+
exponentialScroll,
138+
};
118139
incrementalDelta.set(model.scrollListener, 0);
119140
publicAPI.modified();
120141
};
@@ -156,6 +177,7 @@ function vtkMouseRangeManipulator(publicAPI, model) {
156177
//-------------------------------------------------------------------------
157178
publicAPI.onButtonDown = (interactor, renderer, position) => {
158179
model.previousPosition = position;
180+
model.interactionNetDelta = 0;
159181
const glRenderWindow = interactor.getView();
160182
// Ratio is the dom size vs renderwindow size
161183
const ratio =
@@ -232,12 +254,14 @@ function vtkMouseRangeManipulator(publicAPI, model) {
232254
const dxNorm =
233255
(position.x - model.previousPosition.x) / model.containerSize[0];
234256
const dx = scaleDeltaToRange(model.horizontalListener, dxNorm);
257+
model.interactionNetDelta += dx;
235258
processDelta(model.horizontalListener, dx);
236259
}
237260
if (model.verticalListener) {
238261
const dyNorm =
239262
(position.y - model.previousPosition.y) / model.containerSize[1];
240263
const dy = scaleDeltaToRange(model.verticalListener, dyNorm);
264+
model.interactionNetDelta += dy;
241265
processDelta(model.verticalListener, dy);
242266
}
243267

@@ -249,9 +273,14 @@ function vtkMouseRangeManipulator(publicAPI, model) {
249273
if (!model.scrollListener || !delta) {
250274
return;
251275
}
276+
model.interactionNetDelta += delta * model.scrollListener.step;
252277
processDelta(model.scrollListener, delta * model.scrollListener.step);
253278
};
254-
publicAPI.onStartScroll = publicAPI.onScroll;
279+
280+
publicAPI.onStartScroll = (payload) => {
281+
model.interactionNetDelta = 0;
282+
publicAPI.onScroll(payload);
283+
};
255284
}
256285

257286
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)