-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime-read-clock-tickmarks.js
More file actions
182 lines (150 loc) · 6.09 KB
/
time-read-clock-tickmarks.js
File metadata and controls
182 lines (150 loc) · 6.09 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { animationSystem } from './animations.js';
import { setupScratchpad, shuffleArray } from './shared.js';
let currentProblemData = null;
let problemHistory = [];
let currentHistoryIndex = -1;
const problemTextElement = document.getElementById('problem-text');
const optionsContainer = document.getElementById('options-container');
const nextBtn = document.getElementById('next-btn');
const prevBtn = document.getElementById('prev-btn');
function formatTime(hour, minute) {
return `${hour}:${minute.toString().padStart(2, '0')}`;
}
function calculateShiftedTime(hour, minute, minuteShift) {
const hour24 = hour === 12 ? 0 : hour;
let totalMinutes = hour24 * 60 + minute + minuteShift;
totalMinutes = (totalMinutes % 720 + 720) % 720;
let shiftedHour = Math.floor(totalMinutes / 60) % 12;
if (shiftedHour === 0) {
shiftedHour = 12;
}
const shiftedMinute = totalMinutes % 60;
return { hour: shiftedHour, minute: shiftedMinute };
}
function generateTimeOptions(correctHour, correctMinute) {
const options = new Set([formatTime(correctHour, correctMinute)]);
const minuteShifts = [-20, -15, -10, -5, 5, 10, 15, 20];
while (options.size < 4) {
const shift = minuteShifts[Math.floor(Math.random() * minuteShifts.length)];
const shiftedTime = calculateShiftedTime(correctHour, correctMinute, shift);
options.add(formatTime(shiftedTime.hour, shiftedTime.minute));
}
return shuffleArray(Array.from(options));
}
function generateClockReadProblem() {
const hour = Math.floor(Math.random() * 12) + 1;
const minute = Math.floor(Math.random() * 60);
const correctAnswer = formatTime(hour, minute);
return {
type: 'clockReadTickmarks',
questionText: 'What time does this clock show?',
hour,
minute,
options: generateTimeOptions(hour, minute),
correctAnswer
};
}
function displayClock(hour, minute) {
const clockDiv = document.createElement('div');
clockDiv.className = 'analog-clock';
const faceDiv = document.createElement('div');
faceDiv.className = 'clock-face';
for (let i = 0; i < 60; i++) {
const tick = document.createElement('div');
tick.className = i % 5 === 0 ? 'clock-tick hour-tick' : 'clock-tick minute-tick';
tick.style.transform = `translateX(-50%) rotate(${i * 6}deg)`;
faceDiv.appendChild(tick);
}
for (let i = 1; i <= 12; i++) {
const angle = i * 30 * (Math.PI / 180);
const numberDiv = document.createElement('div');
numberDiv.className = 'clock-number clock-number-static';
numberDiv.textContent = i;
const radius = 90;
const x = radius * Math.sin(angle);
const y = -radius * Math.cos(angle);
numberDiv.style.left = `calc(50% + ${x}px - 15px)`;
numberDiv.style.top = `calc(50% + ${y}px - 15px)`;
faceDiv.appendChild(numberDiv);
}
const hourHand = document.createElement('div');
hourHand.className = 'clock-hand hour-hand';
const hourAngle = (hour % 12 + minute / 60) * 30;
hourHand.style.transform = `translateX(-50%) rotate(${hourAngle}deg)`;
faceDiv.appendChild(hourHand);
const minuteHand = document.createElement('div');
minuteHand.className = 'clock-hand minute-hand';
const minuteAngle = minute * 6;
minuteHand.style.transform = `translateX(-50%) rotate(${minuteAngle}deg)`;
faceDiv.appendChild(minuteHand);
const centerDiv = document.createElement('div');
centerDiv.className = 'clock-center';
faceDiv.appendChild(centerDiv);
clockDiv.appendChild(faceDiv);
return clockDiv;
}
function displayProblem(problemData) {
currentProblemData = problemData;
problemTextElement.textContent = currentProblemData.questionText;
optionsContainer.innerHTML = '';
optionsContainer.classList.remove('options');
optionsContainer.classList.add('clock-read-layout');
const clockContainer = document.createElement('div');
clockContainer.className = 'clock-container';
clockContainer.appendChild(displayClock(currentProblemData.hour, currentProblemData.minute));
const buttonGrid = document.createElement('div');
buttonGrid.className = 'options clock-read-options';
currentProblemData.options.forEach((optionText) => {
const optionButton = document.createElement('button');
optionButton.className = 'option';
optionButton.textContent = optionText;
optionButton.onclick = () => handleOptionClick(optionButton);
buttonGrid.appendChild(optionButton);
});
optionsContainer.appendChild(clockContainer);
optionsContainer.appendChild(buttonGrid);
prevBtn.disabled = currentHistoryIndex <= 0;
nextBtn.disabled = false;
}
function handleOptionClick(selectedOption) {
if (selectedOption.disabled) {
return;
}
const isCorrect = selectedOption.textContent === currentProblemData.correctAnswer;
const allOptions = optionsContainer.querySelectorAll('.option');
if (isCorrect) {
animationSystem.handleCorrectAnswer(selectedOption, allOptions, () => {
generateAndShowNewProblem();
});
} else {
animationSystem.handleWrongAnswer(selectedOption);
}
}
function loadProblemFromHistory(index) {
if (index >= 0 && index < problemHistory.length) {
currentHistoryIndex = index;
displayProblem(problemHistory[currentHistoryIndex]);
}
}
function generateAndShowNewProblem() {
const newProblem = generateClockReadProblem();
problemHistory.push(newProblem);
currentHistoryIndex = problemHistory.length - 1;
displayProblem(newProblem);
}
document.addEventListener('DOMContentLoaded', () => {
setupScratchpad();
generateAndShowNewProblem();
nextBtn.addEventListener('click', () => {
if (currentHistoryIndex < problemHistory.length - 1) {
loadProblemFromHistory(currentHistoryIndex + 1);
} else {
generateAndShowNewProblem();
}
});
prevBtn.addEventListener('click', () => {
if (currentHistoryIndex > 0) {
loadProblemFromHistory(currentHistoryIndex - 1);
}
});
});