|
| 1 | + |
| 2 | +function resizeCanvas() { |
| 3 | + canvas.width = Number(window.innerWidth * 0.98); |
| 4 | + canvas.height = canvas.width * (4/9); |
| 5 | +} |
| 6 | + |
| 7 | +const canvas = document.getElementById("main"); |
| 8 | +const xCordinateLabel = document.getElementById("cursorCordinateX"); |
| 9 | +const yCordinateLabel = document.getElementById("cursorCordinateY"); |
| 10 | +resizeCanvas(); |
| 11 | +window.addEventListener("resize", resizeCanvas) |
| 12 | +const ctx = canvas.getContext("2d", { alpha: false }); |
| 13 | +var running = true; |
| 14 | +var loopRate = 10; |
| 15 | +var fieldColor = "#008507"; |
| 16 | +var yardlineColor = "#FFFFFF"; |
| 17 | +var hashColor = "#FFFFFF"; |
| 18 | +var numbersColor = "#FFFFFF"; |
| 19 | +var fieldCursorColor = "#5000a1"; |
| 20 | +var canvasMouseX = new Number(0); |
| 21 | +var canvasMouseY = new Number(0); |
| 22 | +var stepsPer5Yards = 8; // If Change Reset yardLineArrayinSteps Var |
| 23 | +var yardLineArrayinSteps = [yardsToSteps(10), yardsToSteps(15), yardsToSteps(20), yardsToSteps(25), yardsToSteps(30), yardsToSteps(35), yardsToSteps(40), yardsToSteps(45), yardsToSteps(50), yardsToSteps(55), yardsToSteps(60), yardsToSteps(65), yardsToSteps(70), yardsToSteps(75), yardsToSteps(80), yardsToSteps(85), yardsToSteps(90)]; |
| 24 | + |
| 25 | +function stepsToHumanReadableCordsX(steps) { |
| 26 | + let yardline = yardLineArrayinSteps.reduce(function(prev, curr) {return (Math.abs(curr - steps) < Math.abs(prev - steps) ? curr : prev);}); |
| 27 | + if (yardline > 50) { |
| 28 | + if (steps > yardline) { |
| 29 | + return String(steps - yardline) + " steps outside the " + String(50 - ((stepToYard(yardline) - 10) - 50)) + " yardline"; |
| 30 | + } else if (steps < yardline) { |
| 31 | + return String(yardline - steps) + " steps inside the " + String(50 - ((stepToYard(yardline) - 10) - 50)) + " yardline"; |
| 32 | + } else { |
| 33 | + return "On the " + String(50 - ((stepToYard(yardline) - 10) - 50)); |
| 34 | + } |
| 35 | + } |
| 36 | + if (steps > yardline) { |
| 37 | + return String(steps - yardline) + " steps inside the " + String(stepToYard(yardline) - 10) + " yardline"; |
| 38 | + } else if (steps < yardline) { |
| 39 | + return String(yardline - steps) + " steps outside the " + String(stepToYard(yardline) - 10) + " yardline"; |
| 40 | + } else { |
| 41 | + return "On the " + String(stepToYard(yardline) - 10); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +function yardsToPixles(number) { |
| 46 | + return number * canvas.width/120; |
| 47 | +} |
| 48 | + |
| 49 | +function stepsToPixles(steps) { |
| 50 | + return steps * canvas.width/(24*stepsPer5Yards); |
| 51 | +} |
| 52 | + |
| 53 | +function pixleToClosestStep(y) { |
| 54 | + return Math.round(y/(canvas.width/(24*stepsPer5Yards))); |
| 55 | +} |
| 56 | + |
| 57 | +function stepToYard(steps) { |
| 58 | + return steps * (5/stepsPer5Yards) |
| 59 | +} |
| 60 | + |
| 61 | +function yardsToSteps(yards) { |
| 62 | + return yards/(5/stepsPer5Yards); |
| 63 | +} |
| 64 | + |
| 65 | +function drawYardlines() { |
| 66 | + ctx.strokeStyle = yardlineColor; |
| 67 | + for (let i = 10; i < 111; i = i + 5) { |
| 68 | + ctx.beginPath(); |
| 69 | + ctx.moveTo(yardsToPixles(i), 0); |
| 70 | + ctx.lineTo(yardsToPixles(i), canvas.height); |
| 71 | + ctx.stroke(); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +function drawHashes() { |
| 76 | + ctx.strokeStyle = hashColor; |
| 77 | + for (let i = 10; i < 111; i++) { |
| 78 | + ctx.beginPath(); |
| 79 | + ctx.moveTo(yardsToPixles(i), yardsToPixles(160/9)); |
| 80 | + ctx.lineTo(yardsToPixles(i), yardsToPixles(166/9)); |
| 81 | + ctx.stroke(); |
| 82 | + } |
| 83 | + for (let i = 10; i < 111; i++) { |
| 84 | + ctx.beginPath(); |
| 85 | + ctx.moveTo(yardsToPixles(i), yardsToPixles(320/9)); |
| 86 | + ctx.lineTo(yardsToPixles(i), yardsToPixles(314/9)); |
| 87 | + ctx.stroke(); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +function drawNumbers() { |
| 92 | + ctx.fillStyle = numbersColor; |
| 93 | + for (let i = 20; i < 110; i = i + 10) { |
| 94 | + if (i-10 > 50) { |
| 95 | + ctx.fillText(String(50-((i-10)-50)), yardsToPixles(i-(2/3)), yardsToPixles(133/3), yardsToPixles(4/3)); |
| 96 | + } else { |
| 97 | + ctx.fillText(String(i-10), yardsToPixles(i-(2/3)), yardsToPixles(133/3), yardsToPixles(4/3)); |
| 98 | + } |
| 99 | + } |
| 100 | + for (let i = 20; i < 110; i = i + 10) { |
| 101 | + if (i-10 > 50) { |
| 102 | + ctx.fillText(String(50-((i-10)-50)), yardsToPixles(i-(2/3)), yardsToPixles(9), yardsToPixles(4/3)); |
| 103 | + } else { |
| 104 | + ctx.fillText(String(i-10), yardsToPixles(i-(2/3)), yardsToPixles(9), yardsToPixles(4/3)); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +canvas.addEventListener("mousemove", function(e) { |
| 110 | + var cRect = canvas.getBoundingClientRect(); |
| 111 | + canvasMouseX = Math.round(e.clientX - cRect.left); |
| 112 | + canvasMouseY = Math.round(e.clientY - cRect.top); |
| 113 | +}); |
| 114 | + |
| 115 | +function drawMouse() { |
| 116 | + ctx.fillStyle = fieldCursorColor; |
| 117 | + ctx.beginPath(); |
| 118 | + ctx.arc(stepsToPixles(pixleToClosestStep(canvasMouseX)), stepsToPixles(pixleToClosestStep(canvasMouseY)), yardsToPixles(0.5), 0, 2*Math.PI); |
| 119 | + ctx.fill(); |
| 120 | + xCordinateLabel.innerText = "X Cordinate: " + stepsToHumanReadableCordsX(pixleToClosestStep(canvasMouseX)); |
| 121 | + yCordinateLabel.innerText = "Y Cordinate: " + String(pixleToClosestStep(canvasMouseY)); |
| 122 | +} |
| 123 | + |
| 124 | +function drawLoop() { |
| 125 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 126 | + ctx.fillStyle = fieldColor; |
| 127 | + ctx.rect(0, 0, canvas.width, canvas.height); |
| 128 | + ctx.fill(); |
| 129 | + drawYardlines(); |
| 130 | + drawHashes(); |
| 131 | + drawNumbers(); |
| 132 | + drawMouse(); |
| 133 | +} |
| 134 | + |
| 135 | +setInterval(drawLoop, loopRate); |
0 commit comments