Skip to content

Implement momentum panning - #280

Open
zmeeeeeva wants to merge 3 commits into
d3:mainfrom
zmeeeeeva:momentum-panning
Open

Implement momentum panning#280
zmeeeeeva wants to merge 3 commits into
d3:mainfrom
zmeeeeeva:momentum-panning

Conversation

@zmeeeeeva

@zmeeeeeva zmeeeeeva commented Feb 6, 2025

Copy link
Copy Markdown

#67

Demo: https://jsfiddle.net/zmeeva/6urw1s9d/

Usage:

let myZoom = zoom()
  .momentum(true)
  .on('zoom', handleZoom)

@zmeeeeeva
zmeeeeeva marked this pull request as draft February 6, 2025 18:31

@curran curran left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great concept! I took a quick look through the code and noticed some minor formatting issues.

In your demo, I notice some unexpected behavior:

  • If I drag, then stop moving, then release the mouse, the momentum is way higher than I would expect
  • If I drag, then stop moving, then release the mouse, I would expect that the momentum should be zero

Perhaps a time component is needed here, as in, when the drag starts, start monitoring the movement with a setInterval or similar, then take the average motion over the last 3 intervals, or something like that.

Comment thread src/zoom.js Outdated
startX = x0;
startY = y0;

cancelMomentumTracking();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor formatting inconsistency

Suggested change
cancelMomentumTracking();
cancelMomentumTracking();

Comment thread src/zoom.js Outdated
t = pointer(t, this);
if (Math.hypot(touchfirst[0] - t[0], touchfirst[1] - t[1]) < tapDistance) {
var p = select(this).on("dblclick.zoom");
var p = select(this).on('dblclick.zoom');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var p = select(this).on('dblclick.zoom');
var p = select(this).on("dblclick.zoom");

@zmeeeeeva

Copy link
Copy Markdown
Author

@curran
Thanks for your input!
I implemented a timer for velocity calculation. The demo is also updated.

@zmeeeeeva
zmeeeeeva marked this pull request as ready for review February 14, 2025 08:53
@Fil

Fil commented Feb 14, 2025

Copy link
Copy Markdown
Member

I'm seeing the same issues that @curran mentioned earlier, where the gesture has accumulated an idea of a movement, even when the user has stopped moving. See this capture:

momentum.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants