JSFiddle - React, Tailwind, and code Playground
by Pankaj Kargirwar
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ellipse Tangents</title>
<style>
#svg-container {
width: 400px;
height: 400px;
border: 1px solid black;
}
.ellipse {
fill: none;
stroke: black;
stroke-width: 2;
}
.tangent {
stroke: red;
stroke-width: 2;
}
</style>
</head>
<body>
<div id="svg-container">
<svg id="music-notation" xmlns="http://www.w3.org/2000/svg" width="400" height="400" viewBox="0 0 400 400">
<!-- Ellipse will be drawn here -->
<ellipse id="ellipse" class="ellipse" cx="200" cy="200" rx="100" ry="50" transform="rotate(-30 200 200)"/>
<!-- Tangents (Initially empty) -->
<line id="left-tangent" class="tangent"/>
<line id="right-tangent" class="tangent"/>
</svg>
</div>
<script>
function drawEllipseAndTangents() {
const svg = document.getElementById('music-notation');
const ellipse = document.getElementById('ellipse');
const leftTangent = document.getElementById('left-tangent');
const rightTangent = document.getElementById('right-tangent');
// Get ellipse center and rotation
const cx = parseFloat(ellipse.getAttribute('cx'));
const cy = parseFloat(ellipse.getAttribute('cy'));
const rx = parseFloat(ellipse.getAttribute('rx'));
const ry = parseFloat(ellipse.getAttribute('ry'));
const rotationAngle = -30; // Rotate counterclockwise by 30 degrees
const angleRad = rotationAngle * (Math.PI / 180); // Convert to radians
// Extremities of the ellipse (leftmost and rightmost points)
const leftX = cx - rx * Math.cos(angleRad);
const leftY = cy - ry * Math.sin(angleRad);
const rightX = cx + rx * Math.cos(angleRad);
const rightY = cy + ry * Math.sin(angleRad);
// Tangent at the leftmost point
// The tangent will be vertical at the...