
教室アプリ13:打楽器

画面下の「🖼️」を押して好きな画像を選び、「♪」で音源を取り込みます。準備はこれだけです。あとは画像をタップするたびに音が響き、自分だけの打楽器に早変わり。説明を削ぎ落とした直感的な操作が可能です。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>教室アプリ:打楽器</title>
<style>
body {
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f0f0f0 ;
overflow: hidden;
font-family: sans-serif;
}
#display -area {
flex-grow: 1;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
}
#target -image {
max-width: 90%;
max-height: 80vh;
object-fit: contain;
user-select: none;
-webkit-tap-highlight-color: transparent;
}
.controls {
display: flex;
gap: 40px;
padding: 20px;
background: rgba(255, 255, 255, 0.8);
border-radius: 50px;
margin-bottom: 20px;
}
.btn-label {
cursor: pointer;
font-size: 50px;
transition: transform 0.1s;
display: flex;
align-items: center;
justify-content: center;
}
.btn-label:active {
transform: scale(0.9);
}
input[type="file"] {
display: none;
}
/* 視覚的シンボル */
.icon-image::before { content: "🖼️"; }
.icon-music::before { content: "♪"; }
</style>
</head>
<body>
<div id="display-area">
<img id="target-image" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='100' viewBox='0 0 24 24' fill='none' stroke='%23ccc' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='3' y='3' width='18' height='18' rx='2' ry='2'/%3E%3Ccircle cx='8.5' cy='8.5' r='1.5'/%3E%3Cpolyline points='21 15 16 10 5 21'/%3E%3C/svg%3E" alt="">
</div>
<div class="controls">
<label class="btn-label icon-image">
<input type="file" id="image-input" accept="image/*">
</label>
<label class="btn-label icon-music" style="color: #ff4081 ; font-weight: bold;">
<input type="file" id="audio-input" accept="audio/*">
</label>
</div>
<audio id="audio-player"></audio>
<script>
const imageInput = document.getElementById('image-input');
const audioInput = document.getElementById('audio-input');
const targetImage = document.getElementById('target-image');
const displayArea = document.getElementById('display-area');
const audioPlayer = document.getElementById('audio-player');
// 画像選択
imageInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (event) => {
targetImage.src = event.target.result;
};
reader.readAsDataURL(file);
}
});
// 音声選択
audioInput.addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
const url = URL.createObjectURL(file);
audioPlayer.src = url;
}
});
// タップ時の動作
const playSound = (e) => {
e.preventDefault();
if (audioPlayer.src) {
audioPlayer.currentTime = 0;
audioPlayer.play();
// 視覚的フィードバック(わずかに動く)
targetImage.style.transform = "scale(0.95)";
setTimeout(() => {
targetImage.style.transform = "scale(1)";
}, 100);
}
};
displayArea.addEventListener('mousedown', playSound);
displayArea.addEventListener('touchstart', playSound);
</script>
</body>
</html>