-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[6.7] Zoom out: maintain scroll position (#61465) #66343
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5f2adab
Refactor scaleValue out of useEffect
ajlende 4bf8abf
Zoom out: maintain scroll position
ellatrix e2b4bf5
WIP zoom at center with animations
ajlende 972d763
Add comments
ajlende d5957f9
Try different ways of setting the scroll position
ajlende File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
packages/block-editor/src/components/iframe/bezier-easing.js
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,120 @@ | ||
/** | ||
* https://github.com/gre/bezier-easing | ||
* BezierEasing - use bezier curve for transition easing function | ||
* by Gaëtan Renaudeau 2014 - 2015 – MIT License | ||
*/ | ||
|
||
const NEWTON_ITERATIONS = 4; | ||
const NEWTON_MIN_SLOPE = 0.001; | ||
const SUBDIVISION_PRECISION = 0.0000001; | ||
const SUBDIVISION_MAX_ITERATIONS = 10; | ||
|
||
const kSplineTableSize = 11; | ||
const kSampleStepSize = 1.0 / ( kSplineTableSize - 1.0 ); | ||
|
||
function A( aA1, aA2 ) { | ||
return 1.0 - 3.0 * aA2 + 3.0 * aA1; | ||
} | ||
function B( aA1, aA2 ) { | ||
return 3.0 * aA2 - 6.0 * aA1; | ||
} | ||
function C( aA1 ) { | ||
return 3.0 * aA1; | ||
} | ||
|
||
// Returns x(t) given t, x1, and x2, or y(t) given t, y1, and y2. | ||
function calcBezier( aT, aA1, aA2 ) { | ||
return ( ( A( aA1, aA2 ) * aT + B( aA1, aA2 ) ) * aT + C( aA1 ) ) * aT; | ||
} | ||
|
||
// Returns dx/dt given t, x1, and x2, or dy/dt given t, y1, and y2. | ||
function getSlope( aT, aA1, aA2 ) { | ||
return 3.0 * A( aA1, aA2 ) * aT * aT + 2.0 * B( aA1, aA2 ) * aT + C( aA1 ); | ||
} | ||
|
||
function binarySubdivide( aX, aA, aB, mX1, mX2 ) { | ||
let currentX, | ||
currentT, | ||
i = 0; | ||
do { | ||
currentT = aA + ( aB - aA ) / 2.0; | ||
currentX = calcBezier( currentT, mX1, mX2 ) - aX; | ||
if ( currentX > 0.0 ) { | ||
aB = currentT; | ||
} else { | ||
aA = currentT; | ||
} | ||
} while ( | ||
Math.abs( currentX ) > SUBDIVISION_PRECISION && | ||
++i < SUBDIVISION_MAX_ITERATIONS | ||
); | ||
return currentT; | ||
} | ||
|
||
function newtonRaphsonIterate( aX, aGuessT, mX1, mX2 ) { | ||
for ( let i = 0; i < NEWTON_ITERATIONS; ++i ) { | ||
const currentSlope = getSlope( aGuessT, mX1, mX2 ); | ||
if ( currentSlope === 0.0 ) { | ||
return aGuessT; | ||
} | ||
const currentX = calcBezier( aGuessT, mX1, mX2 ) - aX; | ||
aGuessT -= currentX / currentSlope; | ||
} | ||
return aGuessT; | ||
} | ||
|
||
export default function cubicBezier( mX1, mY1, mX2, mY2 ) { | ||
if ( mX1 === mY1 && mX2 === mY2 ) { | ||
return function linearEasing( x ) { | ||
return x; | ||
}; | ||
} | ||
|
||
const sampleValues = new Float32Array( kSplineTableSize ); | ||
for ( let i = 0; i < kSplineTableSize; ++i ) { | ||
sampleValues[ i ] = calcBezier( i * kSampleStepSize, mX1, mX2 ); | ||
} | ||
|
||
function getTForX( aX ) { | ||
let intervalStart = 0.0; | ||
let currentSample = 1; | ||
const lastSample = kSplineTableSize - 1; | ||
|
||
for ( | ||
; | ||
currentSample !== lastSample && sampleValues[ currentSample ] <= aX; | ||
++currentSample | ||
) { | ||
intervalStart += kSampleStepSize; | ||
} | ||
--currentSample; | ||
|
||
const dist = | ||
( aX - sampleValues[ currentSample ] ) / | ||
( sampleValues[ currentSample + 1 ] - | ||
sampleValues[ currentSample ] ); | ||
|
||
const guessForT = intervalStart + dist * kSampleStepSize; | ||
|
||
const initialSlope = getSlope( guessForT, mX1, mX2 ); | ||
if ( initialSlope >= NEWTON_MIN_SLOPE ) { | ||
return newtonRaphsonIterate( aX, guessForT, mX1, mX2 ); | ||
} else if ( initialSlope === 0.0 ) { | ||
return guessForT; | ||
} | ||
return binarySubdivide( | ||
aX, | ||
intervalStart, | ||
intervalStart + kSampleStepSize, | ||
mX1, | ||
mX2 | ||
); | ||
} | ||
|
||
return function bezierEasing( x ) { | ||
if ( x === 0 || x === 1 ) { | ||
return x; | ||
} | ||
return calcBezier( getTForX( x ), mY1, mY2 ); | ||
}; | ||
} |
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
Oops, something went wrong.
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.
Should/could this happen within the same useEffect as the useEffect that runs when toggling zoom out (that adds the animation classnames with handleZoomOutAnimationClassname)?