MCPcopy
hub / github.com/xiaolin/react-image-gallery / calculateMomentum

Function calculateMomentum

src/components/utils/thumbnailMomentum.ts:93–139  ·  view source on GitHub ↗
(config: MomentumConfig)

Source from the content-addressed store, hash-verified

91 * Calculate the full momentum result for a thumbnail swipe
92 */
93export function calculateMomentum(config: MomentumConfig): MomentumResult {
94 const {
95 velocity,
96 direction,
97 isVertical,
98 currentTranslate,
99 scrollSize,
100 wrapperSize,
101 slideDuration,
102 emptySpaceMargin = 0,
103 momentumMultiplier = 150,
104 } = config;
105
106 // Calculate momentum distance and direction
107 const momentumDistance = calculateMomentumDistance(
108 velocity,
109 momentumMultiplier
110 );
111 const directionSign = getMomentumDirection(direction, isVertical);
112 const momentum = momentumDistance * directionSign;
113
114 // Calculate target translate with momentum
115 let targetTranslate = currentTranslate + momentum;
116
117 // Calculate max scroll and clamp
118 const maxScroll = scrollSize - wrapperSize + emptySpaceMargin;
119 if (maxScroll > 0) {
120 targetTranslate = clampTranslate(
121 targetTranslate,
122 maxScroll,
123 emptySpaceMargin
124 );
125 }
126
127 // Calculate transition
128 const transitionDuration = calculateTransitionDuration(
129 velocity,
130 slideDuration
131 );
132 const transitionStyle = `all ${transitionDuration}ms ${MOMENTUM_EASING}`;
133
134 return {
135 targetTranslate,
136 transitionDuration,
137 transitionStyle,
138 };
139}

Callers 2

ImageGallery.tsxFile · 0.90

Calls 4

getMomentumDirectionFunction · 0.85
clampTranslateFunction · 0.85

Tested by

no test coverage detected