-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
3 additions
and
59 deletions.
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
59 changes: 2 additions & 57 deletions
59
sites/fast-website/src/app/components/background-design/background-design.ts
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 |
---|---|---|
@@ -1,58 +1,3 @@ | ||
import { FASTElement, observable } from "@microsoft/fast-element"; | ||
import { FASTElement } from "@microsoft/fast-element"; | ||
|
||
export class BackgroundDesign extends FASTElement { | ||
private animationFrame: number | void; | ||
private cachedClientHeight: number = this.clientHeight; | ||
private resizeTimeout: number | void; | ||
private scrollTimeout: number | void; | ||
|
||
@observable | ||
public blurAmount: number = 0; | ||
|
||
constructor() { | ||
super(); | ||
|
||
window.addEventListener("resize", this.resizeHandler, { passive: true }); | ||
window.addEventListener("scroll", this.scrollHandler, { passive: true }); | ||
|
||
// trigger both events in case we're viewing a page refresh | ||
window.dispatchEvent(new Event("resize")); | ||
window.dispatchEvent(new Event("scroll")); | ||
} | ||
|
||
private resizeHandler = (): void => { | ||
if (this.resizeTimeout) { | ||
this.resizeTimeout = window.clearTimeout(this.resizeTimeout); | ||
} | ||
|
||
this.resizeTimeout = window.setTimeout(this.setCachedClientHeight, 150); | ||
}; | ||
|
||
private setCachedClientHeight = (): void => { | ||
this.cachedClientHeight = this.clientHeight; | ||
}; | ||
|
||
private render = (): void => { | ||
const amount = window.scrollY / this.cachedClientHeight; | ||
this.blurAmount = amount < 1 ? amount : 1; | ||
this.animationFrame = window.requestAnimationFrame(this.render); | ||
}; | ||
|
||
private cancelAnimationLoop = (): void => { | ||
if (this.animationFrame) { | ||
this.animationFrame = window.cancelAnimationFrame(this.animationFrame); | ||
} | ||
}; | ||
|
||
private scrollHandler = (): void => { | ||
if (this.scrollTimeout) { | ||
window.clearTimeout(this.scrollTimeout); | ||
} | ||
|
||
if (!this.animationFrame) { | ||
this.animationFrame = window.requestAnimationFrame(this.render); | ||
} | ||
|
||
this.scrollTimeout = window.setTimeout(this.cancelAnimationLoop, 150); | ||
}; | ||
} | ||
export class BackgroundDesign extends FASTElement {} |