My idea is to have a div showcase how two text lines will look compared to each other depending on the text length and font size.
The problem is, I want to showcase this in a div with a fixed width.
So the widest text line (not necessarily the one with the biggest font nor the one with most characthers) should be resized to always go to the edge of the div - on one line.
Meanwhile the font size of the not-so-wide text line should also be resized to keep the ratio between the two lines.
I have just written this code:
Script
<script>
function typeFunction() {
document.getElementById('boxOne').innerHTML = document.getElementById("lineOne").value;
document.getElementById('boxTwo').innerHTML = document.getElementById("lineTwo").value;
document.getElementById('boxPreviewOne').innerHTML = document.getElementById("lineOne").value;
document.getElementById('boxPreviewTwo').innerHTML = document.getElementById("lineTwo").value;
checkWidth();
}
function fontSize(fontsize,target) {
target.style.fontSize = fontsize;
checkWidth();
}
function checkWidth() {
ratio = document.getElementById("sizeOne").value.replace(/\D/g, '')/document.getElementById("sizeTwo").value.replace(/\D/g, '');
widthOne = document.getElementById("boxOne").offsetWidth;
widthTwo = document.getElementById("boxTwo").offsetWidth;
if (widthOne >= widthTwo) {
document.getElementById('boxPreviewOne').style.fontSize = "MAX_FONT_SIZE";
if (ratio>=1) {
document.getElementById('boxPreviewTwo').style.fontSize = "MAX_FONT_SIZE/ratio";
} else {
document.getElementById('boxPreviewTwo').style.fontSize = "MAX_FONT_SIZE*ratio";
}
} else {
document.getElementById('boxPreviewTwo').style.fontSize = "MAX_FONT_SIZE";
if (ratio>=1) {
document.getElementById('boxPreviewOne').style.fontSize = "MAX_FONT_SIZE*ratio";
} else {
document.getElementById('boxPreviewOne').style.fontSize = "MAX_FONT_SIZE/ratio";
}
}
}
</script>
HTML
Line One: <input id="lineOne" onKeyUp="typeFunction()" value="This is line one">
<select id="sizeOne" onchange="fontSize(this.value,boxOne);">
<option value="10px">10 cm</option>
<option value="20px" selected="selected">20 cm</option>
<option value="30px">30 cm</option>
</select>
<br>
Line Two: <input id="lineTwo" onKeyUp="typeFunction()" value="This is line two">
<select id="sizeTwo" onchange="fontSize(this.value,boxTwo);">
<option value="10px" selected="selected">10 cm</option>
<option value="20px">20 cm</option>
<option value="30px">30 cm</option>
</select>
<br>
<br>
<div style="width:700px;">
<span id="boxOne" style="font-size:20px">This is line one</span><br>
<span id="boxTwo" style="font-size:10px">This is line two</span>
</div>
<br><br>
Preview:<br>
<div class="Mainbox" style="width:400px;border:1px solid black">
<span id="boxPreviewOne" style="font-size:64px">This is line one</span><br>
<span id="boxPreviewTwo" style="font-size:32px">This is line two</span>
</div>
Line One: <input id="lineOne" onKeyUp="typeFunction()" value="This is line one">
<select id="sizeOne" onchange="fontSize(this.value,boxOne);">
<option value="10px">10 cm</option>
<option value="20px" selected="selected">20 cm</option>
<option value="30px">30 cm</option>
</select>
<br>
Line Two: <input id="lineTwo" onKeyUp="typeFunction()" value="This is line two">
<select id="sizeTwo" onchange="fontSize(this.value,boxTwo);">
<option value="10px" selected="selected">10 cm</option>
<option value="20px">20 cm</option>
<option value="30px">30 cm</option>
</select>
<br>
<br>
<div style="width:700px;">
<span id="boxOne" style="font-size:20px">This is line one</span><br>
<span id="boxTwo" style="font-size:10px">This is line two</span>
</div>
<br><br>
Preview:<br>
<div class="Mainbox" style="width:400px;border:1px solid black">
<span id="boxPreviewOne" style="font-size:64px">This is line one</span><br>
<span id="boxPreviewTwo" style="font-size:32px">This is line two</span>
</div>
And here's an example image of the code + image of concept scenario:
That's as far as I can go on my own, I am afraid.
Obviously there's still quite some way to go.
- I would prefer to only have the text-input mirrored once, but I don't think it'd then be possible to find the widest line.
- I have no idea how to get my-called "MAX_FONT_SIZE"