Make lyrics automagically fit to size

This commit is contained in:
Michael Thomas 2021-09-28 21:39:41 -04:00
parent 0ac66945d5
commit 6d4bd317a8
2 changed files with 48 additions and 3 deletions

View File

@ -1,20 +1,48 @@
<template> <template>
<p class="lyrics" v-html="lyrics">}</p> <p class="lyrics fit-text" v-html="lyrics"></p>
</template> </template>
<script> <script>
import FitText from "@/helpers/FitText";
let f;
export default { export default {
props: [ props: [
'lyrics' 'lyrics'
] ],
mounted() {
f = new FitText(document.querySelector(".fit-text"), 4, 0.1);
f.fit();
window.addEventListener('resize', this.fit);
},
watch: {
lyrics: function () {
this.$nextTick(function () {
f.fit();
});
}
},
methods: {
fit: function () {
f.fit();
}
}
} }
</script> </script>
<style> <style>
.lyrics { .lyrics {
font-size: 5em; font-size: 4rem;
font-weight: 800; font-weight: 800;
text-align: center; text-align: center;
overflow: hidden;
text-shadow: 2px 2px 5px rgba(0,0,0,0.6); text-shadow: 2px 2px 5px rgba(0,0,0,0.6);
} }
.bible {
display: block;
/* font-size: 0.5em; */
/* line-height: 1.5em; */
}
.bible sup {
font-size: 0.5em;
}
</style> </style>

View File

@ -0,0 +1,17 @@
export default class FitText {
constructor(el, defaultSize, increment) {
this.element = el;
this.defaultSize = defaultSize;
this.increment = increment;
}
fit() {
this.element.style.fontSize = this.defaultSize + "rem";
this.recalc();
}
recalc(shouldRecalc) {
if (shouldRecalc || this.element.clientHeight < this.element.scrollHeight) {
this.element.style.fontSize = (parseFloat(this.element.style.fontSize.replace(/[^0-9.]/g,'')) - this.increment) + "rem";
this.recalc();
}
}
}