Added share button to create link to setup... Still needs to be parsed though, so it's effectively useless for now

This commit is contained in:
Taylor Courage 2025-01-24 11:48:15 -05:00
parent a10c84d077
commit 4f34826304
2 changed files with 62 additions and 0 deletions

View File

@ -11,6 +11,17 @@ var dataDelta = [];
///// MATHS FUNCTIONS /////
// Next two functions are for timers... ripped from geeksforgeeks.org
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function waitAndReset() {
await sleep(2000);
var tooltip = document.getElementById("shareButton");
tooltip.innerHTML = "Share";
}
// Calculate Reduction of Area
function getReduction(startSize, finalSize) {
var startArea = Math.PI * ((startSize / 2) * (startSize / 2));
@ -569,3 +580,53 @@ function formatCheck(value) {
}
return value;
}
function createLink() {
// Set our domain to create our link. We will default to the URL hosting the page
var domain = window.location.protocol + "//" + window.location.host + "/";
var shareLink = new URL(domain);
// Create the starting point for the setup manually
var size = document.getElementById("die0").value;
size = formatCheck(size);
// Check if the metric option is checked and save that
if (i == 1 && document.getElementById("metric").checked == true) {
var isMetric = "true";
} else {
var isMetric = "false";
}
// Add the starting information to the link
shareLink.searchParams.append("die", "0");
shareLink.searchParams.append("size0", size);
shareLink.searchParams.append("metric", isMetric);
// Next iterate through all available reductions and add them as well, including their angle
for (var i = 1; i <= numDies; i++) {
var dieNum = "die" + i;
if (!null)
var angleNum = "angle" + i;
size = document.getElementById(dieNum).value;
size = formatCheck(size);
var angle = document.getElementById(angleNum).value;
console.log(dieNum + "=" + size);
console.log(angleNum + "=" + angle);
shareLink.searchParams.append("die", i);
shareLink.searchParams.append("size" + i, size);
shareLink.searchParams.append("angle" + i, angle);
}
// Display the link to the user... I want to make this prettier some day but this crude solution is GoodEnough™
//window.prompt("Your share link: (Note: it has already been copied to your clipboard!)", shareLink.href);
navigator.clipboard.writeText(shareLink.href);
var tooltip = document.getElementById("shareButton");
tooltip.innerHTML = "Copied";
waitAndReset();
}
function readLink() {
//TODO
}

View File

@ -45,6 +45,7 @@
<p></p>
<button name="Calculate" id="calculateButton" onclick="doMath()">Recalculate</button>
<button name="Clear" id="clearButton" onclick="clearScreen()">Clear</button>
<button name="Share" id="shareButton" onclick="createLink()" style="width:60px;">Share</button>
<button name="Print" id="printButton" onclick="printScreen()">Print</button>
<script>addReduction()</script>
<script>addReduction()</script>