
教室アプリ14:月齢

表示される正確な月の形を、夜空の月と見比べましょう。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>教室アプリ:月齢</title>
<style>
:root {
--bg-color: #050a1a ;
--card-bg: rgba(255, 255, 255, 0.05);
--text-color: #f0f0f0 ;
--moon-color: #fceabb ;
--today-border: #f7d774 ;
--header-color: rgba(247, 215, 116, 0.8);
}
body {
background-color: var(--bg-color);
color: var(--text-color);
font-family: 'Hiragino Mincho ProN', 'MS Mincho', serif;
margin: 0;
padding: 20px;
display: flex;
flex-direction: column;
align-items: center;
}
.month-header {
color: var(--header-color);
font-size: 1.8rem;
font-weight: normal;
letter-spacing: 0.3em;
margin: 40px 0 20px;
border-bottom: 1px solid var(--header-color);
width: 100%;
max-width: 500px;
text-align: center;
padding-bottom: 5px;
}
.month-header:first-of-type { margin-top: 10px; }
.calendar-container {
width: 100%;
max-width: 500px;
display: grid;
gap: 12px;
}
.day-card {
background: var(--card-bg);
border-radius: 12px;
padding: 15px 25px;
display: flex;
align-items: center;
justify-content: space-between;
border: 1px solid rgba(255, 255, 255, 0.05);
}
.day-card.today {
border: 1px solid var(--today-border);
background: rgba(247, 215, 116, 0.08);
box-shadow: 0 0 15px rgba(247, 215, 116, 0.1);
}
.date-info { flex: 1; }
.date-text { font-size: 1.2rem; }
.moon-canvas {
background: #000 ;
border-radius: 50%;
}
.age-text {
font-size: 0.9rem;
opacity: 0.6;
min-width: 100px;
text-align: right;
letter-spacing: 0.05em;
}
</style>
</head>
<body>
<div id="main-container" style="width: 100%; display: flex; flex-direction: column; align-items: center;">
</div>
<script>
// 高精度月齢計算
function getPreciseMoonAge(year, month, day) {
const baseDate = new Date(Date.UTC(2000, 0, 6, 18, 14, 0));
const targetDate = new Date(Date.UTC(year, month - 1, day, 12, 0, 0));
const diffDays = (targetDate - baseDate) / (1000 * 60 * 60 * 24);
const cycle = 29.530588853;
let age = diffDays % cycle;
return age < 0 ? age + cycle : age;
}
// 月の描画
function drawMoon(canvas, age) {
const ctx = canvas.getContext('2d');
const w = canvas.width;
const r = w / 2;
const c = w / 2;
ctx.clearRect(0, 0, w, w);
ctx.fillStyle = '#111';
ctx.beginPath();
ctx.arc(c, c, r, 0, Math.PI * 2);
ctx.fill();
ctx.fillStyle = '#fceabb';
const s = Math.cos((age / 29.530588853) * 2 * Math.PI + Math.PI);
ctx.beginPath();
if (age < 14.765) {
ctx.arc(c, c, r, -Math.PI/2, Math.PI/2, false);
ctx.ellipse(c, c, r * Math.abs(s), r, 0, Math.PI/2, -Math.PI/2, (s < 0));
} else {
ctx.arc(c, c, r, Math.PI/2, -Math.PI/2, false);
ctx.ellipse(c, c, r * Math.abs(s), r, 0, -Math.PI/2, Math.PI/2, (s < 0));
}
ctx.fill();
}
function createMonthCalendar(year, month, isCurrentMonth) {
const mainContainer = document.getElementById('main-container');
const now = new Date();
const todayDay = now.getDate();
const weekDays = ["日", "月", "火", "水", "木", "金", "土"];
const header = document.createElement('div');
header.className = 'month-header';
header.innerText = `${year}年 ${month}月`;
mainContainer.appendChild(header);
const container = document.createElement('div');
container.className = 'calendar-container';
mainContainer.appendChild(container);
const lastDay = new Date(year, month, 0).getDate();
for (let d = 1; d <= lastDay; d++) {
const age = getPreciseMoonAge(year, month, d);
const dateObj = new Date(year, month - 1, d);
const isToday = isCurrentMonth && d === todayDay && now.getMonth() + 1 === month;
const card = document.createElement('div');
card.className = `day-card ${isToday ? 'today' : ''}`;
if (isToday) card.id = 'today-scroll';
card.innerHTML = `
<div class="date-info">
<div class="date-text">${d}日 <span style="font-size:0.9rem; opacity:0.7;">(${weekDays[dateObj.getDay()]})</span></div>
</div>
<canvas class="moon-canvas" width="60" height="60"></canvas>
<div class="age-text">月齢 ${age.toFixed(1)}</div>
`;
container.appendChild(card);
drawMoon(card.querySelector('.moon-canvas'), age);
}
}
function init() {
const now = new Date();
const currentYear = now.getFullYear();
const currentMonth = now.getMonth() + 1;
// 今月
createMonthCalendar(currentYear, currentMonth, true);
// 来月
let nextYear = currentYear;
let nextMonth = currentMonth + 1;
if (nextMonth > 12) {
nextMonth = 1;
nextYear++;
}
createMonthCalendar(nextYear, nextMonth, false);
// 今日の位置へスクロール
setTimeout(() => {
const todayCard = document.getElementById('today-scroll');
if (todayCard) {
todayCard.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
}, 500);
}
window.onload = init;
</script>
</body>
</html>