DEV: Use performance.timing as a baseline for the splash (#17275)

* update styles

* remove unused code

* use request time as a baseline
This commit is contained in:
Joe 2022-06-29 18:01:46 +08:00 committed by GitHub
parent 6ecfdc8f55
commit 6146da5eb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 31 deletions

View File

@ -81,15 +81,6 @@ const Discourse = Application.extend({
initialize: () => withPluginApi(cb.version, cb.code), initialize: () => withPluginApi(cb.version, cb.code),
}); });
}); });
window.addEventListener(
"load",
() => {
// The app booted. Remove the splash screen
document.querySelector("#d-splash")?.remove();
},
{ once: true }
);
}, },
_registerPluginCode(version, code) { _registerPluginCode(version, code) {

View File

@ -9,13 +9,14 @@
display: grid; display: grid;
place-items: center; place-items: center;
backface-visibility: hidden; backface-visibility: hidden;
background: var(--secondary);
position: absolute; position: absolute;
left: 0; left: 0;
top: 0; top: 0;
height: 100%; height: 100%;
width: 100%; width: 100%;
z-index: 1001; /* above the header */ z-index: 1001;
background: var(--secondary); --animation-state: paused;
} }
#d-splash .preloader-image { #d-splash .preloader-image {
@ -25,22 +26,26 @@
#d-splash .preloader-text { #d-splash .preloader-text {
padding-top: 4em; padding-top: 4em;
position: absolute; position: absolute;
opacity: 0; opacity: 0;
animation: fade-in 0.5s ease-in-out; animation: fade-in 0.5s ease-in-out;
animation-delay: 2.5s; animation-delay: 1s;
animation-fill-mode: forwards; animation-fill-mode: forwards;
color: var(--primary); animation-play-state: var(--animation-state);
color: var(--primary);
} }
#d-splash .preloader-text:after { #d-splash .preloader-text:after {
animation: loading-text 3s infinite; animation: loading-text 3s infinite;
content: ""; content: "";
position: absolute; position: absolute;
top: 4em;
margin: 0 0.1em; margin: 0 0.1em;
left: 100%;
} }
.rtl #d-splash .preloader-text:after { .rtl #d-splash .preloader-text:after {
left: 0;
right: 100%; right: 100%;
} }
@ -71,7 +76,7 @@
<img <img
class="preloader-image" class="preloader-image"
src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' version='1.1' data-ember-extension='1'%3E%3Cstyle%3E /* these need to be injected dynamicly to match theme colors */ :root %7B --primary: %23222222; --secondary: %23ffffff; --tertiary: %23f15c21; --highlight: %23f0ea88; --quaternary: %2365ccff; --success: %23009900; %7D /* these styles need to live here because the SVG has a different scope */ .dots %7B animation-name: loader; animation-timing-function: ease-in-out; animation-duration: 3s; animation-iteration-count: infinite; animation-delay: 1.5s; stroke: %23fff; stroke-width: 0.5px; transform-origin: center; opacity: 0; r: max(1vw, 11px); cy: 50%25; %7D .dots:first-child %7B fill: var(--tertiary); animation-delay: 1.25s; %7D .dots:nth-child(2) %7B fill: var(--tertiary); animation-delay: 1.35s; %7D .dots:nth-child(3) %7B fill: var(--highlight); animation-delay: 1.45s; %7D .dots:nth-child(4) %7B fill: var(--quaternary); animation-delay: 1.55s; %7D .dots:nth-child(5) %7B fill: var(--quaternary); animation-delay: 1.65s; %7D @keyframes loader %7B 0%25 %7B opacity: 0; transform: scale(1); %7D 45%25 %7B opacity: 1; transform: scale(0.7); %7D 65%25 %7B opacity: 1; transform: scale(0.7); %7D 100%25 %7B opacity: 0; transform: scale(1); %7D %7D %3C/style%3E%3Cg class='container'%3E%3Ccircle class='dots' cx='30vw'/%3E%3Ccircle class='dots' cx='40vw'/%3E%3Ccircle class='dots' cx='50vw'/%3E%3Ccircle class='dots' cx='60vw'/%3E%3Ccircle class='dots' cx='70vw'/%3E%3C/g%3E%3C/svg%3E%0A" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' version='1.1'%0A%3E%3Cstyle%3E /* these need to be injected dynamicly to match theme colors */ :root %7B --primary: %23222222; --secondary: %23ffffff; --tertiary: %23f15c21; --highlight: %23f0ea88; --quaternary: %2365ccff; --success: %23009900; --animation-state: paused; %7D /* these styles need to live here because the SVG has a different scope */ .dots %7B animation-name: loader; animation-timing-function: ease-in-out; animation-duration: 3s; animation-iteration-count: infinite; animation-play-state: var(--animation-state); stroke: %23fff; stroke-width: 0.5px; transform-origin: center; opacity: 0; r: max(1vw, 11px); cy: 50%25; %7D .dots:first-child %7B fill: var(--tertiary); %7D .dots:nth-child(2) %7B fill: var(--tertiary); animation-delay: 0.15s; %7D .dots:nth-child(3) %7B fill: var(--highlight); animation-delay: 0.3s; %7D .dots:nth-child(4) %7B fill: var(--quaternary); animation-delay: 0.45s; %7D .dots:nth-child(5) %7B fill: var(--quaternary); animation-delay: 0.6s; %7D @keyframes loader %7B 0%25 %7B opacity: 0; transform: scale(1); %7D 45%25 %7B opacity: 1; transform: scale(0.7); %7D 65%25 %7B opacity: 1; transform: scale(0.7); %7D 100%25 %7B opacity: 0; transform: scale(1); %7D %7D %3C/style%3E%3Cg class='container'%3E%3Ccircle class='dots' cx='30vw' /%3E%3Ccircle class='dots' cx='40vw' /%3E%3Ccircle class='dots' cx='50vw' /%3E%3Ccircle class='dots' cx='60vw' /%3E%3Ccircle class='dots' cx='70vw' /%3E%3C/g%3E%3C/svg%3E%0A"
alt="<%=SiteSetting.title%>" alt="<%=SiteSetting.title%>"
/> />
@ -90,5 +95,56 @@
} }
</style> </style>
</noscript> </noscript>
<script>
const connectStart = performance?.timing?.connectStart || 0;
const SPLASH_DELAY = connectStart ? 2000 : 0;
const POLLING_INTERVAL = 50;
const targetTime = connectStart + SPLASH_DELAY;
const splashWrapper = document.querySelector("#d-splash");
const image = splashWrapper?.querySelector(".preloader-image");
let splashInterval;
let windowLoaded;
const swapSplash = () => {
splashWrapper?.style.setProperty("--animation-state", "running");
image.src = image.src.replace(
"--animation-state: paused;",
"--animation-state: running;"
);
clearSplashInterval();
};
const clearSplashInterval = () => {
clearInterval(splashInterval);
splashInterval = null;
};
(() => {
splashInterval = setInterval(() => {
if (windowLoaded) {
clearSplashInterval();
}
if (Date.now() > targetTime) {
swapSplash();
}
}, POLLING_INTERVAL);
})();
window.addEventListener(
"load",
() => {
windowLoaded = true;
splashWrapper?.remove();
},
{ once: true }
);
</script>
</section> </section>
<%- end %> <%- end %>

View File

@ -1,6 +1,9 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" data-ember-extension="1"> <svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
version="1.1"
>
<style> <style>
/* these need to be injected dynamicly to match theme colors */ /* these need to be injected dynamicly to match theme colors */
:root { :root {
--primary: #222222; --primary: #222222;
@ -9,6 +12,7 @@
--highlight: #f0ea88; --highlight: #f0ea88;
--quaternary: #65ccff; --quaternary: #65ccff;
--success: #009900; --success: #009900;
--animation-state: paused;
} }
/* these styles need to live here because the SVG has a different scope */ /* these styles need to live here because the SVG has a different scope */
@ -17,7 +21,7 @@
animation-timing-function: ease-in-out; animation-timing-function: ease-in-out;
animation-duration: 3s; animation-duration: 3s;
animation-iteration-count: infinite; animation-iteration-count: infinite;
animation-delay: 1.5s; animation-play-state: var(--animation-state);
stroke: #fff; stroke: #fff;
stroke-width: 0.5px; stroke-width: 0.5px;
transform-origin: center; transform-origin: center;
@ -28,27 +32,26 @@
.dots:first-child { .dots:first-child {
fill: var(--tertiary); fill: var(--tertiary);
animation-delay: 1.25s;
} }
.dots:nth-child(2) { .dots:nth-child(2) {
fill: var(--tertiary); fill: var(--tertiary);
animation-delay: 1.35s; animation-delay: 0.15s;
} }
.dots:nth-child(3) { .dots:nth-child(3) {
fill: var(--highlight); fill: var(--highlight);
animation-delay: 1.45s; animation-delay: 0.3s;
} }
.dots:nth-child(4) { .dots:nth-child(4) {
fill: var(--quaternary); fill: var(--quaternary);
animation-delay: 1.55s; animation-delay: 0.45s;
} }
.dots:nth-child(5) { .dots:nth-child(5) {
fill: var(--quaternary); fill: var(--quaternary);
animation-delay: 1.65s; animation-delay: 0.6s;
} }
@keyframes loader { @keyframes loader {
@ -72,10 +75,10 @@
</style> </style>
<g class="container"> <g class="container">
<circle class="dots" cx="30vw"/> <circle class="dots" cx="30vw" />
<circle class="dots" cx="40vw"/> <circle class="dots" cx="40vw" />
<circle class="dots" cx="50vw"/> <circle class="dots" cx="50vw" />
<circle class="dots" cx="60vw"/> <circle class="dots" cx="60vw" />
<circle class="dots" cx="70vw"/> <circle class="dots" cx="70vw" />
</g> </g>
</svg> </svg>

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB