
教室アプリ27:学習時計

算数セットの学習時計を再現!赤い長針をドラッグすると、青い短針がリアルに連動して動きます。余計な機能を削ぎ落としたシンプルなデザインで、子供たちが直感的に時間の仕組みを学べる、授業に最適なアプリです。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>算数セットの学習時計</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f0f4f8 ;
font-family: 'Helvetica Neue', Arial, sans-serif;
}
#clock {
position: relative;
width: 400px;
height: 400px;
border: 15px solid #ff6b6b ;
border-radius: 50%;
background-color: white;
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
touch-action: none; /* スマホ・タブレットのスクロール防止 */
}
/* 文字盤の数字 */
.number {
position: absolute;
width: 40px;
height: 40px;
line-height: 40px;
text-align: center;
font-size: 28px;
font-weight: bold;
color: #333 ;
}
/* 目盛り */
.tick {
position: absolute;
width: 2px;
height: 10px;
background-color: #888 ;
left: 199px;
transform-origin: bottom center;
}
.tick.hour {
width: 4px;
height: 15px;
background-color: #333 ;
}
/* 中心点 */
#center {
position: absolute;
width: 20px;
height: 20px;
background-color: #333 ;
border-radius: 50%;
top: 190px;
left: 190px;
z-index: 10;
}
/* 針の共通設定 */
.hand {
position: absolute;
bottom: 50%;
left: 50%;
transform-origin: bottom center;
border-radius: 4px;
}
/* 短針(時針)- 青 */
#hour -hand {
width: 10px;
height: 110px;
background-color: #1e3799 ;
margin-left: -5px;
z-index: 3;
}
/* 長針(分針)- 赤(ドラッグ可能) */
#minute -hand {
width: 6px;
height: 150px;
background-color: #eb2f06 ;
margin-left: -3px;
z-index: 5;
cursor: pointer;
}
/* デジタル表示 */
#digital {
margin-top: 30px;
font-size: 32px;
font-weight: bold;
color: #444 ;
background: white;
padding: 10px 20px;
border-radius: 10px;
border: 2px solid #ddd ;
}
</style>
</head>
<body>
<div id="clock">
<div id="center"></div>
<div id="hour-hand" class="hand"></div>
<div id="minute-hand" class="hand"></div>
</div>
<div id="digital">12 : 00</div>
<script>
const clock = document.getElementById('clock');
const hourHand = document.getElementById('hour-hand');
const minuteHand = document.getElementById('minute-hand');
const digitalDisplay = document.getElementById('digital');
let currentMinutes = 0; // 0 から 719 (12時間分)
const centerX = 200;
const centerY = 200;
// 文字盤の数字と目盛りを配置
function createClockFace() {
for (let i = 1; i <= 12; i++) {
const number = document.createElement('div');
number.className = 'number';
number.innerText = i;
const angle = (i * 30) * Math.PI / 180;
const radius = 150;
number.style.left = `${centerX + radius * Math.sin(angle) - 20}px`;
number.style.top = `${centerY - radius * Math.cos(angle) - 20}px`;
clock.appendChild(number);
}
for (let i = 0; i < 60; i++) {
const tick = document.createElement('div');
tick.className = `tick ${i % 5 === 0 ? 'hour' : ''}`;
tick.style.transform = `rotate(${i * 6}deg)`;
tick.style.transformOrigin = `0px ${centerY}px`;
tick.style.top = '0px';
tick.style.left = `${centerX}px`;
clock.appendChild(tick);
}
}
// 針の表示とデジタル時計を更新
function updateClock() {
const minutes = currentMinutes % 60;
let hours = Math.floor(currentMinutes / 60) % 12;
if (hours === 0) hours = 12;
const minuteAngle = minutes * 6;
const hourAngle = (currentMinutes * 0.5);
minuteHand.style.transform = `rotate(${minuteAngle}deg)`;
hourHand.style.transform = `rotate(${hourAngle}deg)`;
// デジタル表示の更新
const displayMinutes = minutes < 10 ? '0' + minutes : minutes;
digitalDisplay.innerText = `${hours} : ${displayMinutes}`;
}
// ドラッグ処理(マウス・タッチ共通)
let isDragging = false;
function getAngle(x, y) {
const rect = clock.getBoundingClientRect();
const clientX = x - rect.left - centerX;
const clientY = y - rect.top - centerY;
let angle = Math.atan2(clientX, -clientY) * (180 / Math.PI);
if (angle < 0) angle += 360;
return angle;
}
function handleStart() {
isDragging = true;
}
let lastAngle = 0;
function handleMove(e) {
if (!isDragging) return;
const clientX = e.touches ? e.touches[0].clientX : e.clientX;
const clientY = e.touches ? e.touches[0].clientY : e.clientY;
const newAngle = getAngle(clientX, clientY);
// 1目盛り(6度)ごとに動かす
const targetMinutes = Math.round(newAngle / 6) % 60;
const currentHour = Math.floor(currentMinutes / 60);
let proposedMinutes = currentHour * 60 + targetMinutes;
// 12時をまたぐ時の処理(なめらかな連動のため)
const diff = proposedMinutes - currentMinutes;
if (diff < -30) proposedMinutes += 60;
if (diff > 30) proposedMinutes -= 60;
if (proposedMinutes < 0) proposedMinutes += 720;
currentMinutes = proposedMinutes % 720;
updateClock();
}
function handleEnd() {
isDragging = false;
}
// イベントリスナーの登録
minuteHand.addEventListener('mousedown', handleStart);
window.addEventListener('mousemove', handleMove);
window.addEventListener('mouseup', handleEnd);
minuteHand.addEventListener('touchstart', handleStart);
window.addEventListener('touchmove', handleMove);
window.addEventListener('touchend', handleEnd);
// 初期化
createClockFace();
updateClock();
</script>
</body>
</html>