Skip to content
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

Remove vsync from parallax #14042

Merged
merged 2 commits into from Mar 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 29 additions & 12 deletions extensions/amp-fx-collection/0.1/providers/parallax.js
Expand Up @@ -45,8 +45,8 @@ export class ParallaxProvider {
/** @private @const {!../../../../src/service/viewport/viewport-impl.Viewport} */
this.viewport_ = Services.viewportForDoc(ampdoc);

/** @private @const {!../../../../src/service/vsync-impl.Vsync} */
this.vsync_ = Services.vsyncFor(this.ampdoc_.win);
/** @private @const {!../../../../src/service/resources-impl.Resources} */
this.resources_ = Services.resourcesForDoc(ampdoc);

installPositionObserverServiceForDoc(ampdoc);

Expand All @@ -61,7 +61,7 @@ export class ParallaxProvider {
installOn(element) {
setStyle(element, 'will-change', 'transform');
const parallaxElement = new ParallaxElement(
element, this.positionObserver_, this.viewport_, this.vsync_);
element, this.positionObserver_, this.viewport_, this.resources_);
parallaxElement.initialize();
}
}
Expand All @@ -74,18 +74,18 @@ class ParallaxElement {
* @param {!Element} element The element to give a parallax effect.
* @param {!../../../../src/service/position-observer/position-observer-impl.PositionObserver} positionObserver
* @param {!../../../../src/service/viewport/viewport-impl.Viewport} viewport
* @param {!../../../../src/service/vsync-impl.Vsync} vsync
* @param {!../../../../src/service/resources-impl.Resources} resources
*/
constructor(element, positionObserver, viewport, vsync) {
constructor(element, positionObserver, viewport, resources) {

/** @private @const {!../../../../src/service/position-observer/position-observer-impl.PositionObserver} */
this.positionObserver_ = positionObserver;

/** @private @const {!../../../../src/service/viewport/viewport-impl.Viewport} */
this.viewport_ = viewport;

/** @const @private {!../../../../src/service/vsync-impl.Vsync} */
this.vsync_ = vsync;
/** @const @private {!../../../../src/service/resources-impl.Resources} */
this.resources_ = resources;

/** @const {string} */
const factorValue = user().assert(element.getAttribute(FACTOR_ATTR),
Expand All @@ -102,6 +102,14 @@ class ParallaxElement {

/** @private @const {!Element} */
this.element_ = element;

/** @private {number} */
this.translateYOffset_ = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

this seems to be only set and never read back


/** @private {boolean} */
this.mutateScheduled_ = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

this doesn't seem to change value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What happened to the commits... Fixed.


this.boundTranslateY_ = this.translateY_.bind(this);
}

/**
Expand Down Expand Up @@ -138,13 +146,22 @@ class ParallaxElement {
// Offset is how much extra to move the element which is position within
// viewport times adjusted factor.
const offset = (this.adjustedViewportHeight_ - top) * adjustedFactor;
this.translateYOffset_ = offset;

if (!this.mutateScheduled_) {
this.mutateScheduled_ = true;
this.resources_.mutateElement(this.element_, this.boundTranslateY_);
}
}

/**
* This must be called inside a mutate phase.
*/
translateY_() {
this.mutateScheduled_ = false;
// Translate the element offset pixels.
// No need for vsync mutate, position observer only calls back at most
// every animation frame and we are only changing `translate` which does
// not cause relayouts.
setStyles(this.element_,
{transform: `translateY(${offset.toFixed(0)}px)`}
{transform: `translateY(${this.translateYOffset_.toFixed(0)}px)`}
);
}

Expand Down Expand Up @@ -174,7 +191,7 @@ class ParallaxElement {
* @private
*/
getAdjustedViewportHeight_() {
return this.vsync_.measurePromise(() => {
return this.resources_.measureElement(() => {
const viewportHeight = this.viewport_.getHeight();

let offsetTop = 0;
Expand Down