27
loading...
This website collects cookies to deliver better user experience
interface BaseConfig {
text?: string
background?: string
baseElement?: HTMLElement | Element
backgroundImg?: string
animationSpeed?: 'slow' | 'regular' | 'fast'
customElement?: HTMLElement | Element | string,
triggerTime?: number,
}
class JsScreensaver {
private config: BaseConfig = baseConfig;
private windowDimensions: IDimensions = {width : 0, height : 0};
private playAnimation: boolean = true;
private screensaverElement: HTMLElement = document.body;
private eventsList: string[] = ['keydown', 'mousemove'];
private defaultScreensaver: string = `
<div class="screensaver__element-wrapper">
<div class="screensaver__element-content">
<p class="screensaver__element-text"></p>
</div>
</div>
`
BaseConfig
interface, I listed all options that may be passed into the screensaver configuration. start()
method. If there are no options passed as an argument, the baseConfig
is loaded.start(config?: BaseConfig): void {
this.config = {...baseConfig, ...config};
this.setActionsListeners();
}
triggerTime
property. The default value is set to 2 seconds. For each of the events in the array (keyup and mousemove) the addEventListener
is set, with a callback function that creates the screensaver container after a certain time. If the event is triggered, the timeout is cleared and the screensaver element is removed.private stopScreensaverListener() {
this.eventsList.forEach(event => window.addEventListener(event, (e) => {
e.preventDefault();
this.playAnimation = false;
this.screensaverElement.remove();
}));
}
private setActionsListeners() {
let mouseMoveTimer: ReturnType<typeof setTimeout>;
this.eventsList.forEach(event => window.addEventListener(event, () => {
clearTimeout(mouseMoveTimer);
mouseMoveTimer = setTimeout(() => {
this.createContainer();
}, this.config.triggerTime)
}))
}
stopScreensaverListener
method is triggered from the createContainer
. The latter creates a DOM element with appropriate classes and styling. The screensaver container and element (a rectangle in this case) are appended to the body as a default, but we can define any other container, passing it into a configuration in a baseElement
property.requestAnimationFrame
API which I described in my previous post. In that post I showed the same animation. private runAnimation(element: HTMLElement): void {
this.playAnimation = true;
element.style.position = 'absolute';
let positionX = this.windowDimensions.width / 2;
let positionY = this.windowDimensions.height / 2;
let movementX = this.config.animationSpeed ? speedOptions[this.config.animationSpeed] : speedOptions.regular;
let movementY = this.config.animationSpeed ? speedOptions[this.config.animationSpeed] : speedOptions.regular;
const animateElements = () => {
positionY += movementY
positionX += movementX
if (positionY < 0 || positionY >= this.windowDimensions.height - element.offsetHeight) {
movementY = -movementY;
}
if (positionX <= 0 || positionX >= this.windowDimensions.width - element.clientWidth) {
movementX = -movementX;
}
element.style.top = positionY + 'px';
element.style.left = positionX + 'px';
if (this.playAnimation) {
requestAnimationFrame(animateElements);
}
}
requestAnimationFrame(animateElements)
}
positionX
and positionY
variables. The movement
represents the number of pixels that the object will move in every frame. Here I used the values from the configuration, letting the user set the speed of movement. In every frame, the position of the rectangle is checked, whether it's inside the container or if it hits the border of the container. If the breakpoint values are reached, the movement values are set to the opposite, which generates the motion in the opposite direction.const classInstance = new JsScreensaver();
export { classInstance as JsScreensaver };
import { JsScreensaver } from "../js-screensaver";
start()
method with the configuration (or leave the config blank).JsScreensaver.start({
text: "Hello Screensaver",
customElement: document.querySelector('.screen-saver'),
triggerTime: 4000,
animationSpeed: 'slow'
});
customElement
property lets you create the screensaver from the HTML or component in your own project. So you can inject any customized element with styling that sits in your project.