UNPKG

opacity

Version:

A simple tool used to progressively reduce the opacity (get it?) of a website from any given date.

36 lines (35 loc) 1.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.enable = void 0; var date_fns_1 = require("date-fns"); function set(from, to) { var now = new Date(); var total = (0, date_fns_1.differenceInMilliseconds)(to, from); var current = (0, date_fns_1.differenceInMilliseconds)(to, now); var opacity = Math.min(Math.max(current / total, 0), 1); window.document.body.style.opacity = opacity.toFixed(5); // window.document.body.style.transition = "opacity 0.5s linear"; return opacity === 0; } function enable(from, to, animated) { if (typeof window === 'undefined') { return; } window.document.body.style.opacity = "0"; var animation; function update() { var finished = set(from, to instanceof Date ? to : (0, date_fns_1.add)(from, to)); if (!animated || finished) { return; } animation = window.requestAnimationFrame(update); } update(); return function () { if (animation !== undefined) { cancelAnimationFrame(animation); } }; } exports.enable = enable; exports.default = enable;