This repository has been archived by the owner on Apr 10, 2021. It is now read-only.
Bounce At Boundaries on Swipe #14
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,7 +94,7 @@ define(function(require) { | |
* @param {string|{x: string, y: string}} [options.mode='stop'] | ||
* Determines how to handle boundary violations during interactions: | ||
* 'stop' disallows dragging beyond the boundaries and | ||
* 'slow' slows the drag effect once boundaries are violated. | ||
* 'slow' limits drag and swipe effects beyond boundaries. | ||
* A different effect may be applied to each boundary. | ||
* | ||
* @example | ||
|
@@ -184,6 +184,7 @@ define(function(require) { | |
var event = args.event; | ||
var targetState = args.targetState; | ||
var eventType = event.type; | ||
var originalState; | ||
|
||
switch (eventType) { | ||
case EventTypes.TOUCH: | ||
|
@@ -202,7 +203,6 @@ define(function(require) { | |
else { | ||
this._stopAtBoundaries(event, targetState, 'x'); | ||
} | ||
|
||
if (!event.simulated && this._mode.y === Modes.SLOW) { | ||
this._pullToBoundaries(event, targetState, 'y'); | ||
} | ||
|
@@ -212,7 +212,6 @@ define(function(require) { | |
break; | ||
|
||
case EventTypes.MOUSE_WHEEL: | ||
case EventTypes.SWIPE: | ||
|
||
this._stopAtBoundaries(event, targetState); | ||
break; | ||
|
@@ -222,6 +221,25 @@ define(function(require) { | |
|
||
this._snapToBoundaries(event, targetState); | ||
break; | ||
|
||
case EventTypes.SWIPE: | ||
|
||
originalState = targetState.clone(); | ||
if (this._mode.x === Modes.SLOW) { | ||
this._bounceAtBoundaries(event, targetState, 'x'); | ||
} | ||
else { | ||
this._stopAtBoundaries(event, targetState, 'x'); | ||
} | ||
if (this._mode.y === Modes.SLOW) { | ||
this._bounceAtBoundaries(event, targetState, 'y'); | ||
} | ||
else { | ||
this._stopAtBoundaries(event, targetState, 'y'); | ||
} | ||
this._accelerateAnimationAtBoundaries(event, targetState, originalState); | ||
|
||
break; | ||
} | ||
}, | ||
|
||
|
@@ -265,9 +283,47 @@ define(function(require) { | |
durationFactorY = boundedDelta / originalDelta; | ||
} | ||
|
||
// Apply acceleration up to the default animation duration. | ||
// Apply acceleration. This effect should be relative to, but faster | ||
// than other snap backs. | ||
acceleratedDuration = targetState.duration * Math.min(durationFactorX, durationFactorY); | ||
targetState.duration = Math.max(this._animationDuration, acceleratedDuration); | ||
targetState.duration = Math.max(this._animationDuration / 2, acceleratedDuration); | ||
}, | ||
|
||
/** | ||
* Allow event translation to move content slightly outside of boundaries | ||
* so that when release event occurs the content will snap back to boundaries. | ||
* @param {InteractionEvent} event | ||
* @param {TransformState} targetState | ||
* @param {string} [axis] | ||
*/ | ||
_bounceAtBoundaries: function(event, targetState, axis) { | ||
var originalState = targetState.clone(); | ||
var viewportSize = this._awesomeMap.getViewportDimensions(); | ||
|
||
// Enforce the viewport boundaries. | ||
var boundedPosition = this._getBoundedPosition(targetState); | ||
var bounceDistance; | ||
var direction; | ||
|
||
// If the targetState ends up out of bounds, allow the translation to carry | ||
// beyond the bounds by 10% of the relevant viewport dimension. | ||
// On release event handling, the content will "bounce back". | ||
// Switch the sign of the bounce distance to account for location: | ||
// left/top has the distance added, right/bottom has the distance subtracted. | ||
if (!axis || axis === 'x') { | ||
if (boundedPosition.x !== originalState.translateX) { | ||
bounceDistance = 0.1 * viewportSize.width; | ||
direction = boundedPosition.x < originalState.translateX ? 1 : -1; | ||
targetState.translateX = boundedPosition.x + bounceDistance * direction; | ||
} | ||
} | ||
if (!axis || axis === 'y') { | ||
if (boundedPosition.y !== originalState.translateY) { | ||
bounceDistance = 0.1 * viewportSize.height; | ||
direction = boundedPosition.y < originalState.translateY ? 1 : -1; | ||
targetState.translateY = boundedPosition.y + bounceDistance * direction; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about consolidating this near-duplicate code like so function fixTargetState (axis) {
var translateDir = 'translate' + axis.toUpperCase();
var side = axis === 'x' ? 'width' : 'height';
if (boundedPosition[axis] !== originalState[translateDir]) {
bounceDistance = 0.1 * viewportSize[side];
direction = boundedPosition[axis] < originalState[translateDir] ? 1 : -1;
targetState[translateDir] = boundedPosition[axis] + bounceDistance * direction;
}
}
if (!axis) {
fixTargetState('x');
fixTargetState('y');
} else {
fixTargetState(axis);
} Looking at http://jsperf.com/bracket-vs-dot-notation-read-vs-write, it's clear that using bracket notation (not to mention the function calls) would slow things down a lot! |
||
}, | ||
|
||
/** | ||
|
@@ -426,10 +482,6 @@ define(function(require) { | |
* @private | ||
*/ | ||
_stopAtBoundaries: function(event, targetState, axis) { | ||
// Save off the original values. | ||
var originalState = targetState.clone(); | ||
|
||
// Enforce the viewport boundaries. | ||
var boundedPosition = this._getBoundedPosition(targetState); | ||
|
||
if (!axis || axis === 'x') { | ||
|
@@ -438,15 +490,10 @@ define(function(require) { | |
if (!axis || axis === 'y') { | ||
targetState.translateY = boundedPosition.y; | ||
} | ||
|
||
// Modify the animation duration for swipe events. | ||
if (event.type === EventTypes.SWIPE) { | ||
this._accelerateAnimationAtBoundaries(event, targetState, originalState); | ||
} | ||
} | ||
}; | ||
|
||
_.assign(BoundaryInterceptor.prototype, InterceptorMixin); | ||
|
||
return BoundaryInterceptor; | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -68,7 +68,7 @@ define(function(require) { | |
} | ||
map.addInterceptor(new BoundaryInterceptor({ | ||
centerContent: true, | ||
mode: 'stop' | ||
mode: { x: 'stop', y: 'slow' } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A numerical value might be cooler than 'slow' - very minor thing. |
||
})); | ||
|
||
// Wire up observables. | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why divided by 2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To keep things relative to configured animationDuration, but to speed it up. This effect should be faster than other snap backs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good place for inline comment. We shall never remember after you move on to Netflix