Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | import * as dom from '../dom/dom';
import {DomWatcher} from '../dom/dom-watcher';
import {
type ElementVisibilityObject,
elementVisibility,
} from '../dom/element-visibility';
import {CssVarInterpolate} from '../interpolate/css-var-interpolate';
import type {interpolateSettings} from '../interpolate/multi-interpolate';
import * as mathf from '../mathf/mathf';
import {Raf} from '../raf/raf';
import {cssUnit} from '../string/css-unit';
import * as stringf from '../string/stringf';
import type {InviewProgress} from './inview-progress';
export interface ViewportCssParallaxSettings {
rootElement: HTMLElement;
// Optional css write element, defaults to rootElement.
cssWriteElement?: HTMLElement;
// Optional raf ev element, defaults to rootElement.
rafEvElement?: HTMLElement;
debug: boolean;
// http://degu.surge.sh/classes/mathf.mathf-1.html#damp
lerp: number;
damp: number;
// Whether to force clamp the progress to 0-1 range. Defaults to true.
clamp?: boolean;
// The precision rounding on the lerp. Used to cull / avoid layout thrashes.
precision?: number;
// The element baseline is the location in which we should use to
// check the current element position. Since we want to check
// where in a viewport an element is, we need to know what point to use
// in the element. Should we use the top (0), middle (0.5) or bottom of the
// element.
elementBaseline?: number;
// Options to basically a setting where by, it shifts the
// elementBaseline as the scroll progresses.
// The element top is used at the bottom of the screen and element
// bottom is used for the top of the screen.
// Using this option, overrides the settings of elementBaseline.
elementBaselineFromTopToBottom?: boolean;
// The rafEvOptions so that you can add rootMargin etc to the base raf.
rafEvOptions?: Object;
// Optionally pass inviewProgress.
// This can be used to trigger css classes at specific breakpoints.
inviewProgress?: InviewProgress | null;
}
export interface ViewportCssParallaxConfig {
settings: ViewportCssParallaxSettings;
}
/**
* A class that creat a viewport css parallax. This is the base controller for
* viewport-css-parallax so check documentation there.
* ```
*
* // Create the instance and set the root element
* const parallaxer = new ViewportCssParallaxer();
*
*
* // Add your settings.
* const settings = {
* rootElement: document.getElementById('hoho'),
* debug: false,
* lerp: 0.18,
* damp: 0.18,
* elmenentBaseline: 0
* }
*
* parallaxer.init(settings,
* [
* {
* id: '--x',
* progress: [
* {from: 0, to: 1, start: '0px', end: '0px'}
* ]
* }
* ]
* )
*
*
* // Later Dispose.
* parallaxer.dispose();
* ```
*
*/
export class ViewportCssParallax {
/**
* The root element to observe the current viewport position.
*/
private rootElement!: HTMLElement;
/**
* The element to write out the css variables to. Defaults to the rootElement
*/
private cssWriteElement!: HTMLElement;
private rafEvElement!: HTMLElement;
private domWatcher!: DomWatcher;
private rafEv!: ElementVisibilityObject;
private raf!: Raf;
private id!: string;
private interpolator!: CssVarInterpolate;
/**
* The css parallax settings.
*/
private parallaxData!: ViewportCssParallaxConfig;
private settingsData!: ViewportCssParallaxSettings;
private interpolationsData!: Array<interpolateSettings>;
private currentProgress = 0;
private lastWriteProgress: number | null = -1;
public initialized = false;
constructor() {}
public init(
settings: ViewportCssParallaxSettings,
interpolations?: Array<interpolateSettings>
) {
if (this.initialized) {
return;
}
this.initialized = true;
this.rootElement = settings.rootElement;
this.cssWriteElement = settings.cssWriteElement
? settings.cssWriteElement
: this.rootElement;
this.rafEvElement = settings.rafEvElement
? settings.rafEvElement
: this.rootElement;
this.id = stringf.uuid();
this.raf = new Raf(this.onRaf.bind(this));
this.parallaxData = JSON.parse(
this.rootElement.getAttribute('viewport-css-parallax')!
);
this.domWatcher = new DomWatcher();
this.domWatcher.add({
element: window,
on: 'smartResize',
callback: () => this.onWindowResize(),
});
this.updateSettings(settings);
this.interpolator = new CssVarInterpolate(this.rootElement, {
interpolations: interpolations || [],
});
this.interpolator.useBatchUpdate(true);
this.interpolator.useSubPixelRendering(false);
// Use no write mode as we will use interpolator to just calculate values.
this.interpolator.useNoWrite(true);
// On load, we need to initially, bring the animation to
// start position.
this.updateImmediately();
this.rootElement.classList.add('viewport-css-parallax-ready');
// Start and stop raf when the element comes into view.
this.rafEv = elementVisibility.inview(
this.rafEvElement,
this.settingsData.rafEvOptions,
(element: HTMLElement, changes: IntersectionObserverEntry) => {
if (changes.isIntersecting) {
this.raf.start();
} else {
this.raf.stop();
this.updateImmediately();
}
}
);
}
public updateInterpolations(interpolations: Array<interpolateSettings>) {
if (!this.initialized) {
throw new Error(
'You must initialize viewport-css-parallax first before calling updateInterpolations'
);
}
this.interpolator.setInterpolations({interpolations: interpolations});
}
public updateSettings(settings?: ViewportCssParallaxSettings) {
// If we haven't set the setting data yet, apply defaults.
if (!this.settingsData) {
this.settingsData = {
...{
rootElement: this.rootElement,
debug: false,
clamp: true,
lerp: 1,
damp: 1,
// Default to top of element.
elementBaseline: 0,
elementBaselineFromTopToBottom: false,
precision: 3,
rafEvOptions: {
rootMargin: '300px 0px 300px 0px',
},
inviewProgress: null,
},
...(settings || {}),
};
} else {
// If we have already set once.
this.settingsData = {
...this.settingsData,
...(settings || {}),
};
}
}
/**
* Takes a css string declaration such as '100px', '100vh' or '100%'
* and converts that into a relative pixel number.
* @param cssUnitObject
*/
protected getPixelValue(cssValue: string): number {
const unit = cssUnit.parse(cssValue);
let base = 1;
if (unit.unit === '%') {
base = this.rootElement.offsetHeight;
return base * ((unit.value as number) / 100);
}
if (unit.unit === 'vh') {
base = window.innerHeight;
return base * ((unit.value as number) / 100);
}
return base * (unit.value as number);
}
/**
* Calculates the current progress and returns a value between 0-1.
*/
protected updateProgress(lerp: number, damp: number): number {
// Viewport css parallax needs to calculate where on the viewport a given
// elemenet resides.
//
// Since generally, since we think in terms of scrolling down, 0 - 1 would
// be represented as:
// 1 ---> top of screen
// 0.5 --> middle of screen
// 0 --> bottom of screen
//
// Therefore, progress is represented as 0-1 where it goes from the bottom
// of the screen to the top.
//
//
// Additionally, we need to know, what point in the element should be
// use to see where the element resides. We could use the top,
// center or bottom.
//
// The elementBaseline is used to factor this in. The default state
// is calculated from teh top of the element.
const box = this.rootElement.getBoundingClientRect();
const elementBaseline =
box.top + this.settingsData.elementBaseline! * box.height;
let percent = mathf.inverseLerp(0, window.innerHeight, elementBaseline);
// From top to bottom is basically a setting where by, it shifts the
// elementBaseline as the scroll progresses.
// The element top is used at the bottom of the screen and element
// bottom is used for the top of the screen.
if (this.settingsData.elementBaselineFromTopToBottom) {
const elementBaselineTop = box.top;
percent = mathf.inverseLerp(
-this.rootElement.offsetHeight,
window.innerHeight + box.height,
elementBaselineTop
);
}
// Invert it so that 0 is considered bottom.
percent = 1 - percent;
if (lerp === 1 && damp === 1) {
this.currentProgress = percent;
} else {
this.currentProgress = mathf.damp(
this.currentProgress,
percent,
lerp,
damp
);
}
if (this.settingsData.clamp) {
this.currentProgress = mathf.clamp01(this.currentProgress);
}
// Update inviewProgress if provided.
if (this.settingsData!.inviewProgress) {
this.settingsData!.inviewProgress.setProgress(this.currentProgress);
}
if (this.settingsData.debug) {
console.log(this.currentProgress);
}
return this.currentProgress;
}
/**
* Updates the current progress immediately.
*/
public updateImmediately() {
this.flush();
this.updateProgress(1, 1);
this.interpolator.update(this.currentProgress);
}
/**
* Flushes internal cache that culls two consecutive writes.
*/
public flush() {
this.lastWriteProgress = null;
}
public onRaf(): void {
this.raf.read(() => {
this.updateProgress(this.settingsData.lerp, this.settingsData.damp);
});
this.raf.write(() => {
// Use a rounded progress to pass to css var interpolate which
// will cull updates that are repetitive.
const roundedProgress = mathf.roundToPrecision(
this.currentProgress,
this.settingsData.precision!
);
this.interpolator.update(roundedProgress);
if (roundedProgress !== this.lastWriteProgress) {
// Write values.
const values = this.interpolator.getValues();
dom.setCssVariables(this.cssWriteElement, values);
}
this.lastWriteProgress = roundedProgress;
});
}
protected onWindowResize() {}
public dispose(): void {
this.rafEv && this.rafEv.dispose();
this.raf && this.raf.dispose();
this.domWatcher && this.domWatcher.dispose();
}
}
|