Certainly! I can help you create an HTML application that meets your requirements. We'll build a web page that displays two right triangles side by side:
- Left Side: A fixed right triangle with a horizontal side of 6 units, a vertical side of 8 units, and a hypotenuse of 10 units.
- Right Side: A dynamic right triangle where the horizontal side is fixed at 6 units, and you can input the vertical side length. The triangle updates immediately as you change the input.
Let's go through the steps to create this application using HTML, CSS, and JavaScript.
1. HTML Structure
We'll start by setting up the basic HTML structure.
<!DOCTYPE html>
<html>
<head>
<title>Dynamic Right Triangle App</title>
<style>
/* CSS styles will go here */
</style>
</head>
<body>
<div id="container">
<div id="left-side">
<!-- Fixed triangle will go here -->
</div>
<div id="right-side">
<!-- Dynamic triangle and input will go here -->
</div>
</div>
<script>
// JavaScript code will go here
</script>
</body>
</html>
2. CSS Styling
We'll divide the screen into two sections and style them.
#container {
display: flex;
}
#left-side, #right-side {
flex: 1;
padding: 20px;
box-sizing: border-box;
border-right: 1px solid #ccc;
}
#right-side {
border-right: none;
}
h2 {
text-align: center;
}
svg {
display: block;
margin: 0 auto;
border: 1px solid #ddd;
}
input {
display: block;
margin: 10px auto;
padding: 5px;
font-size: 16px;
}
3. Left Side (Fixed Triangle)
We'll use SVG to draw the fixed triangle.
<!-- Inside #left-side div -->
<h2>Fixed Triangle</h2>
<svg id="fixed-triangle" width="100" height="100">
<polygon points="10,10 10,90 70,90" style="fill: #8ecae6; stroke: #023047; stroke-width: 2;" />
</svg>
<p>Horizontal Side: 6 units (60px)</p>
<p>Vertical Side: 8 units (80px)</p>
<p>Hypotenuse: 10 units (100px)</p>
Explanation:
- SVG Element: We're creating an SVG canvas with a width and height sufficient to display the triangle.
- Polygon Points: The points
(10,10), (10,90), and (70,90) define the corners of the triangle.
(10,90) and (70,90) define the base (horizontal side of 60px).
(10,10) and (10,90) define the vertical side (vertical side of 80px).
- We add an offset (10px) to ensure the triangle isn't flush against the border.
4. Right Side (Dynamic Triangle)
We'll add an input field and an SVG area where the triangle will be drawn dynamically.
<!-- Inside #right-side div -->
<h2>Dynamic Triangle</h2>
<label for="vertical-input">Vertical Side Length (units):</label>
<input type="number" id="vertical-input" placeholder="Enter vertical side length" min="0" step="0.1">
<svg id="dynamic-triangle" width="100" height="100">
<!-- Triangle will be drawn here -->
</svg>
<p id="hypotenuse-length"></p>
5. JavaScript for Interactivity
We'll write a script that will update the triangle and calculate the hypotenuse whenever the input changes.
<script>
// Constants
const unitSize = 10; // 1 unit = 10px
const horizontalUnits = 6;
const horizontalPx = horizontalUnits * unitSize;
// Initial draw
drawDynamicTriangle(0);
// Event listener
document.getElementById('vertical-input').addEventListener('input', function() {
const verticalUnits = parseFloat(this.value) || 0;
drawDynamicTriangle(verticalUnits);
});
function drawDynamicTriangle(verticalUnits) {
const verticalPx = verticalUnits * unitSize;
const hypotenuseUnits = Math.sqrt(horizontalUnits ** 2 + verticalUnits ** 2);
const hypotenusePx = hypotenuseUnits * unitSize;
const svg = document.getElementById('dynamic-triangle');
svg.setAttribute('width', horizontalPx + 20);
svg.setAttribute('height', verticalPx + 20);
// Clear previous content
svg.innerHTML = '';
// Draw triangle
const points = `10,10 10,${10 + verticalPx} ${10 + horizontalPx},${10 + verticalPx}`;
const triangle = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
triangle.setAttribute('points', points);
triangle.setAttribute('style', 'fill: #fb8500; stroke: #023047; stroke-width: 2;');
svg.appendChild(triangle);
// Update hypotenuse length
document.getElementById('hypotenuse-length').innerText = `Hypotenuse: ${hypotenuseUnits.toFixed(2)} units (${hypotenusePx.toFixed(0)}px)`;
}
</script>
Explanation:
- Constants: We define the unit size and horizontal side in units and pixels.
- Event Listener: We listen for input changes and redraw the triangle accordingly.
- Drawing Function: The
drawDynamicTriangle function calculates the new dimensions and redraws the triangle.
- Hypotenuse Calculation: We calculate the hypotenuse using the Pythagorean theorem.
6. Full Code
Putting it all together, here's the complete HTML document.
<!DOCTYPE html>
<html>
<head>
<title>Dynamic Right Triangle App</title>
<style>
#container {
display: flex;
}
#left-side, #right-side {
flex: 1;
padding: 20px;
box-sizing: border-box;
border-right: 1px solid #ccc;
}
#right-side {
border-right: none;
}
h2 {
text-align: center;
}
svg {
display: block;
margin: 0 auto;
border: 1px solid #ddd;
}
input {
display: block;
margin: 10px auto;
padding: 5px;
font-size: 16px;
}
p {
text-align: center;
}
</style>
</head>
<body>
<div id="container">
<div id="left-side">
<h2>Fixed Triangle</h2>
<svg id="fixed-triangle" width="100" height="100">
<polygon points="10,10 10,90 70,90" style="fill: #8ecae6; stroke: #023047; stroke-width: 2;" />
</svg>
<p>Horizontal Side: 6 units (60px)</p>
<p>Vertical Side: 8 units (80px)</p>
<p>Hypotenuse: 10 units (100px)</p>
</div>
<div id="right-side">
<h2>Dynamic Triangle</h2>
<label for="vertical-input">Vertical Side Length (units):</label>
<input type="number" id="vertical-input" placeholder="Enter vertical side length" min="0" step="0.1">
<svg id="dynamic-triangle" width="100" height="100">
<!-- Triangle will be drawn here -->
</svg>
<p id="hypotenuse-length"></p>
</div>
</div>
<script>
// Constants
const unitSize = 10; // 1 unit = 10px
const horizontalUnits = 6;
const horizontalPx = horizontalUnits * unitSize;
// Initial draw
drawDynamicTriangle(0);
// Event listener
document.getElementById('vertical-input').addEventListener('input', function() {
const verticalUnits = parseFloat(this.value) || 0;
drawDynamicTriangle(verticalUnits);
});
function drawDynamicTriangle(verticalUnits) {
const verticalPx = verticalUnits * unitSize;
const hypotenuseUnits = Math.sqrt(horizontalUnits ** 2 + verticalUnits ** 2);
const hypotenusePx = hypotenuseUnits * unitSize;
const svg = document.getElementById('dynamic-triangle');
svg.setAttribute('width', horizontalPx + 20);
svg.setAttribute('height', verticalPx + 20);
// Clear previous content
svg.innerHTML = '';
// Draw triangle
const points = `10,10 10,${10 + verticalPx} ${10 + horizontalPx},${10 + verticalPx}`;
const triangle = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
triangle.setAttribute('points', points);
triangle.setAttribute('style', 'fill: #fb8500; stroke: #023047; stroke-width: 2;');
svg.appendChild(triangle);
// Update hypotenuse length
document.getElementById('hypotenuse-length').innerText = `Hypotenuse: ${hypotenuseUnits.toFixed(2)} units (${hypotenusePx.toFixed(0)}px)`;
}
</script>
</body>
</html>
Additional Notes
- Unit Consistency: We've maintained consistency by using units throughout the calculations. One unit equals 10 pixels.
- Responsive Design: The SVG elements adjust their size based on input, ensuring the triangles are always visible.
- Immediate Update: As you change the vertical side length in the input field, the triangle updates immediately.
With this code, you have an interactive HTML application where users can input the vertical side length, and the triangle on the right will update in real-time to reflect the changes. It also calculates and displays the hypotenuse length based on the input.
Feel free to customize styles, colors, and dimensions to suit your preferences!
If you have any questions or need further assistance, please let me know.