|
| 1 | +/* ------------------------------------------------------------- |
| 2 | + Hilfsfunktionen |
| 3 | +------------------------------------------------------------- */ |
| 4 | +function canvasToBlob(canvas) { |
| 5 | + return new Promise(resolve => canvas.toBlob(b => resolve(b), 'image/png')); |
| 6 | +} |
| 7 | + |
| 8 | +/* Kopieren‑Button (einmalig erzeugen) */ |
| 9 | +function createCopyButton() { |
| 10 | + const btn = document.createElement('button'); |
| 11 | + btn.id = 'copyButton'; |
| 12 | + btn.textContent = 'Kopieren'; |
| 13 | + btn.addEventListener('click', async () => { |
| 14 | + const canvas = document.getElementById('coordinateSystem'); |
| 15 | + const blob = await canvasToBlob(canvas); |
| 16 | + try { |
| 17 | + await navigator.clipboard.write([ |
| 18 | + new ClipboardItem({ 'image/png': blob }) |
| 19 | + ]); |
| 20 | + alert('Bild wurde in die Zwischenablage kopiert.'); |
| 21 | + } catch (e) { |
| 22 | + console.error(e); |
| 23 | + alert('Kopieren fehlgeschlagen – Browser unterstützt die Clipboard‑API evtl. nicht.'); |
| 24 | + } |
| 25 | + }); |
| 26 | + return btn; |
| 27 | +} |
| 28 | + |
| 29 | +/* ------------------------------------------------------------- |
| 30 | + Hauptfunktion: Zeichnen |
| 31 | +------------------------------------------------------------- */ |
| 32 | +document.getElementById('drawButton').addEventListener('click', () => { |
| 33 | + /* ---- Eingabewerte einlesen -------------------------------- */ |
| 34 | + const xMin = parseFloat(document.getElementById('xMin').value); |
| 35 | + const xMax = parseFloat(document.getElementById('xMax').value); |
| 36 | + const xStep = parseFloat(document.getElementById('xStep').value); |
| 37 | + const xLabel = document.getElementById('xLabel').value; |
| 38 | + |
| 39 | + const yMin = parseFloat(document.getElementById('yMin').value); |
| 40 | + const yMax = parseFloat(document.getElementById('yMax').value); |
| 41 | + const yStep = parseFloat(document.getElementById('yStep').value); |
| 42 | + const yLabel = document.getElementById('yLabel').value; |
| 43 | + |
| 44 | + const canvas = document.getElementById('coordinateSystem'); |
| 45 | + const ctx = canvas.getContext('2d'); |
| 46 | + |
| 47 | + /* ---- Konstanten (Randbereiche) ----------------------------- */ |
| 48 | + const left = 50; // linke Grenze |
| 49 | + const right = 550; // rechte Grenze |
| 50 | + const top = 50; // obere Grenze |
| 51 | + const bottom = 350; // untere Grenze |
| 52 | + const width = right - left; |
| 53 | + const height = bottom - top; |
| 54 | + |
| 55 | + /* ---- Mapping‑Funktionen ----------------------------------- */ |
| 56 | + const mapX = val => left + ((val - xMin) / (xMax - xMin)) * width; |
| 57 | + const mapY = val => bottom - ((val - yMin) / (yMax - yMin)) * height; |
| 58 | + |
| 59 | + /* ---- Position der Null‑Achsen (falls im Wertebereich) ----- */ |
| 60 | + const xZeroPos = (xMin <= 0 && 0 <= xMax) ? mapX(0) : left; // y‑Achse |
| 61 | + const yZeroPos = (yMin <= 0 && 0 <= yMax) ? mapY(0) : bottom; // x‑Achse |
| 62 | + |
| 63 | + /* ---- Canvas leeren ---------------------------------------- */ |
| 64 | + ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 65 | + |
| 66 | + /* ---- Gitternetz (mittelgrau) ----------------------------- */ |
| 67 | + ctx.strokeStyle = '#505050'; |
| 68 | + ctx.lineWidth = 0.5; |
| 69 | + |
| 70 | + // horizontale Gitterlinien |
| 71 | + for (let y = yMin; y <= yMax; y += yStep) { |
| 72 | + const py = mapY(y); |
| 73 | + ctx.beginPath(); |
| 74 | + ctx.moveTo(left, py); |
| 75 | + ctx.lineTo(right, py); |
| 76 | + ctx.stroke(); |
| 77 | + } |
| 78 | + |
| 79 | + // vertikale Gitterlinien |
| 80 | + for (let x = xMin; x <= xMax; x += xStep) { |
| 81 | + const px = mapX(x); |
| 82 | + ctx.beginPath(); |
| 83 | + ctx.moveTo(px, top); |
| 84 | + ctx.lineTo(px, bottom); |
| 85 | + ctx.stroke(); |
| 86 | + } |
| 87 | + |
| 88 | + /* ---- Achsen (schwarz) ------------------------------------ */ |
| 89 | + ctx.strokeStyle = '#000'; |
| 90 | + ctx.lineWidth = 1; |
| 91 | + ctx.beginPath(); |
| 92 | + // X‑Achse (horizontal) |
| 93 | + ctx.moveTo(left, yZeroPos); |
| 94 | + ctx.lineTo(right, yZeroPos); |
| 95 | + // Y‑Achse (vertikal) |
| 96 | + ctx.moveTo(xZeroPos, top); |
| 97 | + ctx.lineTo(xZeroPos, bottom); |
| 98 | + ctx.stroke(); |
| 99 | + |
| 100 | + /* ---- Beschriftungen X‑Achse ------------------------------ */ |
| 101 | + ctx.fillStyle = '#000'; |
| 102 | + ctx.textBaseline = 'top'; |
| 103 | + ctx.textAlign = 'center'; |
| 104 | + for (let x = xMin; x <= xMax; x += xStep) { |
| 105 | + const px = mapX(x); |
| 106 | + // Tick |
| 107 | + ctx.beginPath(); |
| 108 | + ctx.moveTo(px, yZeroPos - 5); |
| 109 | + ctx.lineTo(px, yZeroPos + 5); |
| 110 | + ctx.stroke(); |
| 111 | + // Zahlen |
| 112 | + if (x!=0){ctx.fillText(x, px, yZeroPos + 8);} |
| 113 | + } |
| 114 | + |
| 115 | + /* ---- Beschriftungen Y‑Achse (rechtsbündig, nah am Tick) */ |
| 116 | + ctx.textAlign = 'right'; |
| 117 | + ctx.textBaseline = 'middle'; |
| 118 | + for (let y = yMin; y <= yMax; y += yStep) { |
| 119 | + const py = mapY(y); |
| 120 | + // Tick |
| 121 | + ctx.beginPath(); |
| 122 | + ctx.moveTo(xZeroPos - 5, py); |
| 123 | + ctx.lineTo(xZeroPos + 5, py); |
| 124 | + ctx.stroke(); |
| 125 | + // Zahlen (5 px Abstand zum Tick) |
| 126 | + if (y!=0){ctx.fillText(y, xZeroPos - 7, py);} |
| 127 | + } |
| 128 | + |
| 129 | + /* ---- Titel X‑Achse --------------------------------------- */ |
| 130 | + ctx.textAlign = 'center'; |
| 131 | + ctx.textBaseline = 'top'; |
| 132 | + ctx.fillText(xLabel, (left + right) / 2, bottom+20); |
| 133 | + |
| 134 | + /* ---- Titel Y‑Achse (zentriert links neben den Zahlen) ---- */ |
| 135 | + const yTitleX = 20; |
| 136 | + const yTitleY = (top + bottom) / 2; // Vertikale Mitte der Achse |
| 137 | + ctx.save(); |
| 138 | + ctx.translate(yTitleX, yTitleY); |
| 139 | + ctx.rotate(-Math.PI / 2); |
| 140 | + ctx.textAlign = 'center'; |
| 141 | + ctx.textBaseline = 'middle'; |
| 142 | + ctx.fillText(yLabel, 0, 0); |
| 143 | + ctx.restore(); |
| 144 | + |
| 145 | + /* ---- Kopieren‑Button einblenden (falls noch nicht vorhanden) */ |
| 146 | + if (!document.getElementById('copyButton')) { |
| 147 | + const copyBtn = createCopyButton(); |
| 148 | + const drawBtn = document.getElementById('drawButton'); |
| 149 | + drawBtn.parentNode.insertBefore(copyBtn, drawBtn.nextSibling); |
| 150 | + } |
| 151 | +}); |
0 commit comments