JSFiddle - React, Tailwind, and code Playground
by RavilG
HTML
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Phasor Diagramm</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Phasor Diagramm</h1>
<div id="legend">Winkel: 0°, Magnitude: 0</div>
<canvas id="phasorCanvas" width="600" height="600"></canvas>
<div id="phasorMenu" style="display: none;">
<button id="deletePhasor">Löschen</button>
<!-- Weitere Buttons können hier hinzugefügt werden -->
</div>
<div>
<label for="selectedPhasor">Ausgewählter Phasor:</label>
<select id="selectedPhasor"></select>
<label for="newMagnitude">Neue Magnitude:</label>
<input type="number" id="newMagnitude" step="0.1">
<label for="newAngle">Neuer Winkel (Grad):</label>
<input type="number" id="newAngle" step="1">
<button id="updatePhasor">Phasor aktualisieren</button>
</div>
<script src="script.js"></script>
</body>
</html>
CSS
body {
font-family: Arial, sans-serif;
text-align: center;
}
canvas {
border: 1px solid black;
margin-top: 20px;
}
#legend {
margin-top: 10px;
font-size: 18px;
}
div {
margin-top: 20px;
}
label, select, input, button {
margin: 5px;
}
#phasorMenu {
position: absolute;
background-color: white;
border: 1px solid black;
padding: 10px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}
JavaScript
const canvas = document.getElementById('phasorCanvas');
const ctx = canvas.getContext('2d');
const legend = document.getElementById('legend');
const selectedPhasorSelect = document.getElementById('selectedPhasor');
const newMagnitudeInput = document.getElementById('newMagnitude');
const newAngleInput = document.getElementById('newAngle');
const updatePhasorButton = document.getElementById('updatePhasor');
const phasorMenu = document.getElementById('phasorMenu');
const deletePhasorButton = document.getElementById('deletePhasor');
let phasors = [];
let isDrawing = false;
let isDragging = false;
let isResizing = false;
let currentPhasor = null;
let selectedPhasor = null;
let resizeEnd = null;
canvas.addEventListener('mousedown', startDrawingOrDragging);
canvas.addEventListener('mousemove', drawOrDrag);
canvas.addEventListener('mouseup', stopDrawingOrDragging);
canvas.addEventListener('mousemove', handleHover);
canvas.addEventListener('click', handleClick);
updatePhasorButton.addEventListener('click', updateSelectedPhasor);
deletePhasorButton.addEventListener('click', deleteSelectedPhasor);
document.addEventListener('click', handleDocumentClick);
function startDrawingOrDragging(event) {
const { offsetX, offsetY } = event;
const clickedPhasor = getClickedPhasor(offsetX, offsetY);
if (clickedPhasor) {
const clickedEnd = getClickedEnd(clickedPhasor, offsetX, offsetY);
if (clickedEnd) {
isResizing = true;
selectedPhasor = clickedPhasor;
resizeEnd = clickedEnd;
} else {
isDragging = true;
selectedPhasor = clickedPhasor;
}
updateSelectedPhasorSelect();
} else {
isDrawing = true;
currentPhasor = { startX: offsetX, startY: offsetY, endX: offsetX, endY: offsetY };
phasors.push(currentPhasor);
updateSelectedPhasorSelect();
}
}
function drawOrDrag(event) {
const { offsetX, offsetY } = event;
if (isDrawing) {
...