feat(templates): ability to disable animation effect in loading page (#161)

This commit is contained in:
Anthony Fu 2022-09-19 17:48:39 +08:00 committed by GitHub
parent 25b950a717
commit 82cd59ef05

View File

@ -33,6 +33,17 @@
filter: blur(100px); filter: blur(100px);
opacity: 0.5; opacity: 0.5;
} }
#animation-toggle {
position: fixed;
padding: 10px;
top: 0;
right: 0;
transition: opacity 0.4s ease-in;
opacity: 0;
}
#animation-toggle:hover {
opacity: 0.8;
}
@keyframes gradient { @keyframes gradient {
0% { 0% {
background-position: 0 0; background-position: 0 0;
@ -53,6 +64,10 @@
} }
} }
@media (prefers-color-scheme: dark) { @media (prefers-color-scheme: dark) {
html, body {
color: white;
color-scheme: dark;
}
#nuxtImageDigit3 { #nuxtImageDigit3 {
fill: #00DC82 !important; fill: #00DC82 !important;
} }
@ -82,8 +97,8 @@
fill="currentColor" /> fill="currentColor" />
</g> </g>
</svg> </svg>
</a> </a>
<button id="animation-toggle">Animation Enabled</button>
<div class="spotlight-wrapper"> <div class="spotlight-wrapper">
<div class="fixed left-0 right-0 spotlight spotlight-top z-10"></div> <div class="fixed left-0 right-0 spotlight spotlight-top z-10"></div>
</div> </div>
@ -92,23 +107,38 @@
</div> </div>
<div class="nuxt-loader-bar"></div> <div class="nuxt-loader-bar"></div>
<script> <script>
const nuxtImg = window let ANIMATION_KEY = 'nuxt-loading-enable-animation'
.document let isLowPerformance = checkIsLowPerformance()
.getElementById('nuxtImg') let enableAnimation = localStorage.getItem(ANIMATION_KEY) === 'false'
? false
: localStorage.getItem(ANIMATION_KEY) === 'true'
? true
: !isLowPerformance
const nuxtImg = window.document.getElementById('nuxtImg')
const animationToggle = window.document.getElementById('animation-toggle')
const body = window.document.body const body = window.document.body
const mouseLightContainer = window
.document
.getElementById('mouseLight')
let bodyRect let bodyRect
function checkIsLowPerformance() {
return window.matchMedia('(prefers-reduced-motion: reduce)').matches
|| navigator.hardwareConcurrency < 2
|| navigator.deviceMemory < 1
}
function calculateDistance(elem, mouseX, mouseY) { function calculateDistance(elem, mouseX, mouseY) {
return Math.floor(Math.sqrt(Math.pow(mouseX - (elem.x + (elem.width / 2)), 2) + Math.pow(mouseY - (elem.top + (elem.height / 2)), 2))); return Math.floor(Math.sqrt(Math.pow(mouseX - (elem.x + (elem.width / 2)), 2) + Math.pow(mouseY - (elem.top + (elem.height / 2)), 2)));
} }
const hide = () => { function onFocusOut() {
if (!enableAnimation) {
return
}
mouseLight.style.opacity = 0 mouseLight.style.opacity = 0
nuxtImg.style.opacity = 0 nuxtImg.style.opacity = 0
} }
const mouseMove = (e) => { function onMouseMove(e) {
if (!enableAnimation) {
return
}
const pointerRect = nuxtImg.getBoundingClientRect() const pointerRect = nuxtImg.getBoundingClientRect()
if (!bodyRect) { if (!bodyRect) {
bodyRect = body.getBoundingClientRect() bodyRect = body.getBoundingClientRect()
@ -130,15 +160,27 @@
nuxtImg.style.opacity = Math.min(Math.max(size / 4, 0.6), 1) nuxtImg.style.opacity = Math.min(Math.max(size / 4, 0.6), 1)
} }
hide() function toggleAnimation(value = !enableAnimation) {
enableAnimation = value
body.addEventListener('mousemove', mouseMove) if (value) {
body.addEventListener('mouseleave', hide) onFocusOut()
animationToggle.innerText = 'Animation Enabled'
window.onunload = function () {
body.removeEventListener('mousemove', mouseMove);
body.removeEventListener('mouseleave', hide);
} }
else {
mouseLight.style.opacity = 0
nuxtImg.style.opacity = 1
nuxtImg.style['mask-image'] = ''
nuxtImg.style['-webkit-mask-image'] = ''
animationToggle.innerText = 'Animation Disabled'
}
localStorage.setItem(ANIMATION_KEY, enableAnimation ? 'true' : 'false')
}
animationToggle.addEventListener('click', () => toggleAnimation(), { passive: true})
body.addEventListener('mousemove', onMouseMove, { passive: true })
body.addEventListener('mouseleave', onFocusOut, { passive: true })
toggleAnimation(enableAnimation)
if (typeof window.fetch === 'undefined') { if (typeof window.fetch === 'undefined') {
setTimeout(() => window.location.reload(), 1000) setTimeout(() => window.location.reload(), 1000)