-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
150 lines (132 loc) · 5.19 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Fraction Visualizer 1.0.4</title>
<style>
body {
font-family: 'Arial', sans-serif;
margin: 20px;
background-color: #f9f9f9;
color: #333;
}
h2 {
color: #444;
}
.container {
max-width: 600px;
margin: auto;
padding: 20px;
background-color: #fff;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
border-radius: 8px;
}
label, input, button {
display: block;
margin-top: 10px;
}
input, button {
padding: 10px;
margin-bottom: 10px;
}
button {
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
width: auto;
}
button:hover {
background-color: #0056b3;
}
canvas {
display: block;
margin: 20px auto;
border: 1px solid #ddd;
background-color: #fafafa;
}
#errorMessage {
color: red;
}
</style>
</head>
<body>
<div class="container">
<h2>Fraction Visualizer 1.0.4</h2>
<label for="fraction1">Fraction 1 (numerator/denominator):</label>
<input type="text" id="fraction1" placeholder="e.g., 17/5">
<label for="fraction2">Fraction 2 (numerator/denominator):</label>
<input type="text" id="fraction2" placeholder="e.g., 28/6">
<button id="drawButton">Draw Fractions</button>
<p id="errorMessage"></p>
</div>
<canvas id="fractionCanvas" width="1200" height="200"></canvas>
<script>
const canvas = document.getElementById('fractionCanvas');
const ctx = canvas.getContext('2d');
const errorMessage = document.getElementById('errorMessage');
function validateFraction(numerator, denominator) {
return Math.ceil(numerator / denominator) <= 5;
}
function drawFraction(startX, centerY, radius, numerator, denominator, fillColor, fractionText) {
let partsToDraw = Math.min(Math.ceil(numerator / denominator), 5) * denominator;
let offsetX = 0;
let totalOffsetX = 0;
ctx.fillStyle = '#000';
ctx.font = '16px Arial';
ctx.fillText(fractionText, startX, centerY - (radius + 20));
for (let part = 0; part < partsToDraw; part++) {
const anglePerPart = 2 * Math.PI / denominator;
const startAngle = anglePerPart * (part % denominator) - Math.PI / 2;
const endAngle = startAngle + anglePerPart;
ctx.beginPath();
ctx.moveTo(startX + offsetX, centerY);
ctx.arc(startX + offsetX, centerY, radius, startAngle, endAngle);
ctx.lineTo(startX + offsetX, centerY);
ctx.closePath();
ctx.fillStyle = part < numerator ? fillColor : 'transparent';
ctx.fill();
ctx.stroke();
if ((part + 1) % denominator === 0) {
offsetX += radius * 2.5;
totalOffsetX = offsetX;
}
}
for (let circle = 0; circle < Math.ceil(partsToDraw / denominator); circle++) {
ctx.beginPath();
ctx.arc(startX + (radius * 2.5 * circle), centerY, radius, 0, Math.PI * 2);
ctx.stroke();
}
return totalOffsetX;
}
function visualizeFractions() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
errorMessage.textContent = "";
const fraction1 = document.getElementById('fraction1').value.split('/');
const fraction2 = document.getElementById('fraction2').value.split('/');
const isValidFraction1 = validateFraction(parseInt(fraction1[0]), parseInt(fraction1[1]));
const isValidFraction2 = validateFraction(parseInt(fraction2[0]), parseInt(fraction2[1]));
if (!isValidFraction1 || !isValidFraction2) {
errorMessage.textContent = "Error: Each fraction can result in a maximum of 5 circles.";
return;
}
const totalOffsetX1 = drawFraction(50, 100, 40, parseInt(fraction1[0]), parseInt(fraction1[1]), 'red', fraction1.join('/'));
const offsetForFraction2 = 50 + totalOffsetX1 + 100;
drawFraction(offsetForFraction2, 100, 40, parseInt(fraction2[0]), parseInt(fraction2[1]), 'blue', fraction2.join('/'));
}
document.getElementById('drawButton').addEventListener('click', visualizeFractions);
document.querySelectorAll('input').forEach(input => {
input.addEventListener('input', function() {
errorMessage.textContent = "";
});
input.addEventListener('keypress', function(event) {
if (event.key === 'Enter') {
event.preventDefault();
visualizeFractions();
}
});
});
</script>
</body>
</html>