Spaces:
Running
Running
Update index.html
Browse files- index.html +334 -136
index.html
CHANGED
@@ -3,207 +3,405 @@
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
6 |
-
<title>
|
|
|
7 |
<style>
|
8 |
body {
|
9 |
margin: 0;
|
10 |
height: 100vh;
|
11 |
-
background-color: #
|
12 |
display: flex;
|
13 |
flex-direction: column;
|
14 |
-
justify-content: flex-end;
|
15 |
align-items: center;
|
16 |
overflow: hidden;
|
17 |
-
font-family: sans-serif;
|
18 |
}
|
19 |
|
20 |
-
.piano-
|
21 |
-
|
22 |
-
width:
|
23 |
-
|
24 |
-
height:
|
25 |
-
min-height: 200px; /* ์ต์ ๊ฑด๋ฐ ๋์ด */
|
26 |
display: flex;
|
27 |
justify-content: center;
|
28 |
align-items: flex-end;
|
29 |
-
|
|
|
30 |
}
|
31 |
|
32 |
.piano {
|
33 |
display: flex;
|
34 |
position: relative;
|
35 |
-
background: linear-gradient(to bottom, #
|
36 |
-
padding:
|
37 |
-
border-radius:
|
38 |
-
box-shadow: 0 0
|
39 |
-
0 0
|
40 |
-
border:
|
41 |
border-bottom: none;
|
|
|
42 |
}
|
43 |
|
44 |
.key {
|
45 |
box-sizing: border-box;
|
46 |
cursor: pointer;
|
47 |
-
user-select: none;
|
48 |
-
-webkit-tap-highlight-color: transparent;
|
49 |
-
transition:
|
|
|
50 |
}
|
51 |
|
52 |
.white-key {
|
53 |
-
width:
|
54 |
-
height:
|
55 |
-
background: linear-gradient(to bottom, #
|
56 |
-
border: 1px solid #
|
57 |
-
border-bottom:
|
58 |
-
border-radius: 0 0
|
59 |
margin-right: 1px;
|
60 |
-
box-shadow: 0
|
61 |
-
position: relative;
|
62 |
z-index: 1;
|
63 |
}
|
64 |
-
.white-key:last-child {
|
65 |
-
margin-right: 0;
|
66 |
-
}
|
67 |
|
68 |
.black-key {
|
69 |
-
width:
|
70 |
-
height:
|
71 |
-
background: linear-gradient(to bottom, #
|
72 |
-
border: 1px solid #
|
73 |
-
border-bottom: 5px solid #
|
74 |
-
border-radius: 0 0
|
75 |
position: absolute;
|
76 |
z-index: 2;
|
77 |
-
box-shadow: 0 2px
|
78 |
}
|
79 |
|
80 |
-
/* ํฐ ๊ฑด๋ฐ ๋๋ ธ์ ๋ ํจ๊ณผ */
|
81 |
.white-key.active {
|
82 |
-
background: linear-gradient(to bottom, #e0e0e0, #
|
83 |
-
transform: translateY(
|
84 |
border-bottom-width: 3px;
|
85 |
-
box-shadow: 0 0
|
86 |
-
inset 0 -1px 1px rgba(255,255,255,0.
|
87 |
}
|
88 |
|
89 |
-
/* ๊ฒ์ ๊ฑด๋ฐ ๋๋ ธ์ ๋ ํจ๊ณผ */
|
90 |
.black-key.active {
|
91 |
-
background: linear-gradient(to bottom, #
|
92 |
transform: translateY(2px);
|
93 |
border-bottom-width: 3px;
|
94 |
-
box-shadow: 0 0
|
95 |
-
inset 0 -1px 1px rgba(100,100,100,0.
|
96 |
}
|
97 |
-
|
98 |
-
/* ๊ฑด๋ฐ ๋ผ๋ฒจ (์ ํ ์ฌํญ) */
|
99 |
.key-label {
|
100 |
position: absolute;
|
101 |
-
bottom:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
left: 50%;
|
103 |
transform: translateX(-50%);
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
}
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
109 |
}
|
110 |
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
}
|
115 |
|
116 |
</style>
|
117 |
</head>
|
118 |
<body>
|
119 |
-
<div
|
|
|
|
|
|
|
120 |
<div class="piano" id="piano">
|
121 |
-
<!-- JavaScript๋ก
|
122 |
</div>
|
123 |
</div>
|
124 |
-
|
125 |
-
|
126 |
-
|
|
|
127 |
|
128 |
<script>
|
129 |
-
const
|
130 |
-
const
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
{ type:
|
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 |
-
keyElement.dataset.note = keyData.note;
|
171 |
-
|
172 |
-
// const labelElement = document.createElement('span');
|
173 |
-
// labelElement.classList.add('key-label');
|
174 |
-
// labelElement.textContent = keyData.label;
|
175 |
-
// keyElement.appendChild(labelElement);
|
176 |
-
|
177 |
-
if (keyData.type === 'black') {
|
178 |
-
// ๊ฒ์ ๊ฑด๋ฐ์ offset์ ์ด์ ํฐ ๊ฑด๋ฐ์ ์์ ๋ฐ๋ผ ๊ณ์ฐ
|
179 |
-
keyElement.style.left = (keyData.offset * (whiteKeyWidth + 1)) + 'px'; // +1์ margin-right ๊ณ ๋ ค
|
180 |
-
} else {
|
181 |
-
whiteKeyIndex++;
|
182 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
const activateKey = (event) => {
|
188 |
-
event.preventDefault(); // ์คํฌ๋กค ๋ฑ ๊ธฐ๋ณธ ๋์ ๋ฐฉ์ง
|
189 |
-
keyElement.classList.add('active');
|
190 |
-
// ๋๋ฆฐ ํค์ ๋ฐ๋ผ ๋ค๋ฅธ ์์์ ๋น ํจ๊ณผ (์ ํ ์ฌํญ)
|
191 |
-
// const randomColor = `rgba(${Math.random()*255}, ${Math.random()*255}, ${Math.random()*255}, 0.8)`;
|
192 |
-
// dynamicStyles.innerHTML = `.key.active { --key-glow-color: ${randomColor}; }`;
|
193 |
-
};
|
194 |
-
|
195 |
-
const deactivateKey = (event) => {
|
196 |
-
event.preventDefault();
|
197 |
-
keyElement.classList.remove('active');
|
198 |
-
};
|
199 |
-
|
200 |
-
keyElement.addEventListener('mousedown', activateKey);
|
201 |
-
keyElement.addEventListener('mouseup', deactivateKey);
|
202 |
-
keyElement.addEventListener('mouseleave', deactivateKey); // ๋ง์ฐ์ค๊ฐ ๊ฑด๋ฐ ๋ฐ์ผ๋ก ๋๊ฐ๋ฉด ํจ๊ณผ ์ ๊ฑฐ
|
203 |
-
|
204 |
-
keyElement.addEventListener('touchstart', activateKey, { passive: false });
|
205 |
-
keyElement.addEventListener('touchend', deactivateKey);
|
206 |
-
keyElement.addEventListener('touchcancel', deactivateKey);
|
207 |
});
|
208 |
|
209 |
</script>
|
|
|
3 |
<head>
|
4 |
<meta charset="UTF-8">
|
5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
6 |
+
<title>Interactive Piano with Falling Notes</title>
|
7 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.49/Tone.js"></script>
|
8 |
<style>
|
9 |
body {
|
10 |
margin: 0;
|
11 |
height: 100vh;
|
12 |
+
background-color: #0a0a0a; /* ์ด๋์ด ๋ฐฐ๊ฒฝ */
|
13 |
display: flex;
|
14 |
flex-direction: column;
|
15 |
+
justify-content: flex-end;
|
16 |
align-items: center;
|
17 |
overflow: hidden;
|
18 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
19 |
}
|
20 |
|
21 |
+
.piano-container {
|
22 |
+
width: 95vw;
|
23 |
+
max-width: 1400px; /* ๊ฑด๋ฐ์ด ๋๋ฌด ์์์ง์ง ์๋๋ก ์ต๋ ๋๋น ์ ํ */
|
24 |
+
height: 30vh; /* ๊ฑด๋ฐ ๋์ด ๋น์จ */
|
25 |
+
min-height: 180px;
|
|
|
26 |
display: flex;
|
27 |
justify-content: center;
|
28 |
align-items: flex-end;
|
29 |
+
position: relative; /* ๋
ธํธ ๋จ์ด์ง๋ ์์ญ์ ๊ธฐ์ค */
|
30 |
+
padding-bottom: 10px;
|
31 |
}
|
32 |
|
33 |
.piano {
|
34 |
display: flex;
|
35 |
position: relative;
|
36 |
+
background: linear-gradient(to bottom, #282828, #181818);
|
37 |
+
padding: 15px 15px 0 15px;
|
38 |
+
border-radius: 12px 12px 0 0;
|
39 |
+
box-shadow: 0 0 40px rgba(173, 216, 230, 0.3), /* ์ฐํ ํ๋์ ๋น */
|
40 |
+
0 0 60px rgba(255, 182, 193, 0.2); /* ์ฐํ ํํฌ์ ๋น */
|
41 |
+
border: 3px solid #3a3a3a;
|
42 |
border-bottom: none;
|
43 |
+
height: 100%; /* piano-container ๋์ด์ ๋ง์ถค */
|
44 |
}
|
45 |
|
46 |
.key {
|
47 |
box-sizing: border-box;
|
48 |
cursor: pointer;
|
49 |
+
user-select: none;
|
50 |
+
-webkit-tap-highlight-color: transparent;
|
51 |
+
transition: all 0.05s ease-out;
|
52 |
+
position: relative; /* ๋ผ๋ฒจ ์์น ๊ธฐ์ค */
|
53 |
}
|
54 |
|
55 |
.white-key {
|
56 |
+
width: 40px; /* ๋ ๋ง์ ๊ฑด๋ฐ์ ์ํด ๋๋น ์ฝ๊ฐ ์ค์ */
|
57 |
+
height: 100%;
|
58 |
+
background: linear-gradient(to bottom, #fdfdfd, #e8e8e8);
|
59 |
+
border: 1px solid #999;
|
60 |
+
border-bottom: 6px solid #b0b0b0;
|
61 |
+
border-radius: 0 0 6px 6px;
|
62 |
margin-right: 1px;
|
63 |
+
box-shadow: 0 3px 5px rgba(0,0,0,0.25), inset 0 -3px 3px rgba(255,255,255,0.6);
|
|
|
64 |
z-index: 1;
|
65 |
}
|
66 |
+
.white-key:last-child { margin-right: 0; }
|
|
|
|
|
67 |
|
68 |
.black-key {
|
69 |
+
width: 24px;
|
70 |
+
height: 60%; /* ํฐ ๊ฑด๋ฐ ๋์ด์ 60% */
|
71 |
+
background: linear-gradient(to bottom, #444, #222);
|
72 |
+
border: 1px solid #111;
|
73 |
+
border-bottom: 5px solid #333;
|
74 |
+
border-radius: 0 0 4px 4px;
|
75 |
position: absolute;
|
76 |
z-index: 2;
|
77 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.5), inset 0 -2px 2px rgba(120,120,120,0.4);
|
78 |
}
|
79 |
|
|
|
80 |
.white-key.active {
|
81 |
+
background: linear-gradient(to bottom, #e0e0e0, #cccccc);
|
82 |
+
transform: translateY(3px);
|
83 |
border-bottom-width: 3px;
|
84 |
+
box-shadow: 0 0 25px 7px var(--key-glow-color, rgba(135, 206, 250, 0.9)), /* ํ๋์ ๋น */
|
85 |
+
inset 0 -1px 1px rgba(255,255,255,0.4);
|
86 |
}
|
87 |
|
|
|
88 |
.black-key.active {
|
89 |
+
background: linear-gradient(to bottom, #333, #111);
|
90 |
transform: translateY(2px);
|
91 |
border-bottom-width: 3px;
|
92 |
+
box-shadow: 0 0 25px 7px var(--key-glow-color, rgba(135, 206, 250, 0.9)), /* ํ๋์ ๋น */
|
93 |
+
inset 0 -1px 1px rgba(100,100,100,0.2);
|
94 |
}
|
95 |
+
|
|
|
96 |
.key-label {
|
97 |
position: absolute;
|
98 |
+
bottom: 8px;
|
99 |
+
left: 50%;
|
100 |
+
transform: translateX(-50%);
|
101 |
+
font-size: 10px;
|
102 |
+
color: #666;
|
103 |
+
pointer-events: none; /* ๋ผ๋ฒจ์ด ํฐ์น ์ด๋ฒคํธ ๋ฐฉํดํ์ง ์๋๋ก */
|
104 |
+
}
|
105 |
+
.black-key .key-label { color: #bbb; font-size: 9px; }
|
106 |
+
|
107 |
+
#note-fall-area {
|
108 |
+
width: 100%;
|
109 |
+
height: calc(100vh - 30vh - 30px); /* ํผ์๋
ธ ๋์ด, ๋ฐ๋ฅ ์ฌ๋ฐฑ ์ ์ธ */
|
110 |
+
position: absolute;
|
111 |
+
top: 0;
|
112 |
+
left: 50%;
|
113 |
+
transform: translateX(-50%); /* ์ค์ ์ ๋ ฌ */
|
114 |
+
/* background-color: rgba(20,20,20,0.5); */ /* ๊ฐ๋ฐ ์ ์์ญ ํ์ธ์ฉ */
|
115 |
+
pointer-events: none; /* ๋
ธํธ๊ฐ ๋ง์ฐ์ค/ํฐ์น ์ด๋ฒคํธ ๊ฐ๋ก์ฑ์ง ์๋๋ก */
|
116 |
+
}
|
117 |
+
|
118 |
+
.note-bar {
|
119 |
+
position: absolute;
|
120 |
+
box-sizing: border-box;
|
121 |
+
border-radius: 3px;
|
122 |
+
opacity: 0.9;
|
123 |
+
background: linear-gradient(to bottom, var(--note-color-start, #00ff00), var(--note-color-end, #008800));
|
124 |
+
box-shadow: 0 0 10px var(--note-color-start, #00ff00);
|
125 |
+
border: 1px solid rgba(255, 255, 255, 0.3);
|
126 |
+
}
|
127 |
+
|
128 |
+
#hit-line {
|
129 |
+
position: absolute;
|
130 |
+
bottom: calc(30vh + 10px); /* ํผ์๋
ธ ์ปจํ
์ด๋ ํ๋จ์ ๋ง์ถค */
|
131 |
left: 50%;
|
132 |
transform: translateX(-50%);
|
133 |
+
width: 95vw; /* ํผ์๋
ธ ์ปจํ
์ด๋ ๋๋น์ ์ ์ฌํ๊ฒ */
|
134 |
+
max-width: 1400px;
|
135 |
+
height: 4px;
|
136 |
+
background: linear-gradient(to right, transparent, rgba(255,255,255,0.7), transparent);
|
137 |
+
border-radius: 2px;
|
138 |
+
z-index: 3;
|
139 |
+
box-shadow: 0 0 15px rgba(200, 200, 255, 0.6);
|
140 |
+
}
|
141 |
+
|
142 |
+
.particle {
|
143 |
+
position: absolute;
|
144 |
+
width: 5px;
|
145 |
+
height: 5px;
|
146 |
+
background-color: var(--particle-color, white);
|
147 |
+
border-radius: 50%;
|
148 |
+
opacity: 1;
|
149 |
+
pointer-events: none;
|
150 |
+
animation: fadeOutAndRise 0.5s ease-out forwards;
|
151 |
}
|
152 |
+
|
153 |
+
@keyframes fadeOutAndRise {
|
154 |
+
to {
|
155 |
+
transform: translateY(-30px) scale(0.5);
|
156 |
+
opacity: 0;
|
157 |
+
}
|
158 |
}
|
159 |
|
160 |
+
#controls {
|
161 |
+
position: absolute;
|
162 |
+
top: 20px;
|
163 |
+
left: 20px;
|
164 |
+
z-index: 10;
|
165 |
+
}
|
166 |
+
#controls button {
|
167 |
+
padding: 8px 12px;
|
168 |
+
font-size: 14px;
|
169 |
+
cursor: pointer;
|
170 |
+
background-color: #333;
|
171 |
+
color: white;
|
172 |
+
border: 1px solid #555;
|
173 |
+
border-radius: 4px;
|
174 |
+
}
|
175 |
+
#controls button:hover {
|
176 |
+
background-color: #555;
|
177 |
}
|
178 |
|
179 |
</style>
|
180 |
</head>
|
181 |
<body>
|
182 |
+
<div id="note-fall-area"></div>
|
183 |
+
<div id="hit-line"></div>
|
184 |
+
|
185 |
+
<div class="piano-container">
|
186 |
<div class="piano" id="piano">
|
187 |
+
<!-- ๊ฑด๋ฐ์ JavaScript๋ก ์์ฑ -->
|
188 |
</div>
|
189 |
</div>
|
190 |
+
|
191 |
+
<div id="controls">
|
192 |
+
<button id="playButton">Play Sample</button>
|
193 |
+
</div>
|
194 |
|
195 |
<script>
|
196 |
+
const pianoElement = document.getElementById('piano');
|
197 |
+
const noteFallArea = document.getElementById('note-fall-area');
|
198 |
+
const playButton = document.getElementById('playButton');
|
199 |
+
|
200 |
+
// ํผ์๋
ธ Synth ์ด๊ธฐํ (Tone.js)
|
201 |
+
const synth = new Tone.PolySynth(Tone.Synth, {
|
202 |
+
oscillator: { type: "sine" }, // ๊ธฐ๋ณธ ์ฌ์ธํ, ์ข ๋ ํผ์๋
ธ์ฒ๋ผ ํ๋ ค๋ฉด 'triangle8' ๋ฑ๋ ๊ณ ๋ ค
|
203 |
+
envelope: {
|
204 |
+
attack: 0.005,
|
205 |
+
decay: 0.1,
|
206 |
+
sustain: 0.3,
|
207 |
+
release: 1
|
208 |
+
},
|
209 |
+
volume: -6 // ๋ณผ๋ฅจ ์กฐ์
|
210 |
+
}).toDestination();
|
211 |
+
|
212 |
+
const whiteKeyNotes = ['C', 'D', 'E', 'F', 'G', 'A', 'B'];
|
213 |
+
const blackKeyNotes = ['C#', 'D#', 'F#', 'G#', 'A#'];
|
214 |
+
const notesWithSharps = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'];
|
215 |
+
|
216 |
+
const baseOctave = 2; // ์์ ์ฅํ๋ธ
|
217 |
+
const numOctaves = 4; // ํ์ํ ์ฅํ๋ธ ์
|
218 |
+
let totalWhiteKeys = 0;
|
219 |
+
|
220 |
+
// ๊ฑด๋ฐ ์์ฑ
|
221 |
+
for (let octave = baseOctave; octave < baseOctave + numOctaves; octave++) {
|
222 |
+
notesWithSharps.forEach((noteName, index) => {
|
223 |
+
const keyElement = document.createElement('div');
|
224 |
+
keyElement.classList.add('key');
|
225 |
+
const fullNoteName = noteName + octave;
|
226 |
+
keyElement.dataset.note = fullNoteName;
|
227 |
+
|
228 |
+
// const labelElement = document.createElement('span');
|
229 |
+
// labelElement.classList.add('key-label');
|
230 |
+
// labelElement.textContent = noteName.length === 1 ? noteName : noteName.charAt(0); // C# -> C
|
231 |
+
// keyElement.appendChild(labelElement);
|
232 |
+
|
233 |
+
if (noteName.includes('#')) { // ๊ฒ์ ๊ฑด๋ฐ
|
234 |
+
keyElement.classList.add('black-key');
|
235 |
+
// ๊ฒ์ ๊ฑด๋ฐ ์์น ๊ณ์ฐ: (์ด์ ํฐ ๊ฑด๋ฐ ๊ฐ์ * ํฐ ๊ฑด๋ฐ ๋๋น) + ์คํ์
|
236 |
+
// C#์ C ๋ค, D#์ D ๋ค...
|
237 |
+
let offsetMultiplier = totalWhiteKeys - 0.35; // ๊ฒ์ ๊ฑด๋ฐ์ ํฐ ๊ฑด๋ฐ ์ฌ์ด์ ์์น์ํค๊ธฐ ์ํ ์กฐ์ ๊ฐ
|
238 |
+
keyElement.style.left = (offsetMultiplier * 41) + 'px'; // 40(๋๋น) + 1(๋ง์ง)
|
239 |
+
keyElement.style.top = '0px';
|
240 |
+
} else { // ํฐ ๊ฑด๋ฐ
|
241 |
+
keyElement.classList.add('white-key');
|
242 |
+
totalWhiteKeys++;
|
243 |
+
}
|
244 |
+
pianoElement.appendChild(keyElement);
|
245 |
+
|
246 |
+
// ์ด๋ฒคํธ ๋ฆฌ์ค๋
|
247 |
+
const playNoteHandler = (event) => {
|
248 |
+
if (event.type === 'touchstart') event.preventDefault();
|
249 |
+
keyElement.classList.add('active');
|
250 |
+
const randomHue = Math.random() * 360;
|
251 |
+
keyElement.style.setProperty('--key-glow-color', `hsla(${randomHue}, 100%, 70%, 0.9)`);
|
252 |
+
synth.triggerAttackRelease(fullNoteName, "8n"); // "8n"์ 8๋ถ์ํ ๊ธธ์ด
|
253 |
+
createParticles(keyElement, `hsla(${randomHue}, 100%, 70%, 0.9)`);
|
254 |
+
};
|
255 |
+
const stopNoteHandler = (event) => {
|
256 |
+
if (event.type === 'touchend') event.preventDefault();
|
257 |
+
keyElement.classList.remove('active');
|
258 |
+
};
|
259 |
+
|
260 |
+
keyElement.addEventListener('mousedown', playNoteHandler);
|
261 |
+
keyElement.addEventListener('mouseup', stopNoteHandler);
|
262 |
+
keyElement.addEventListener('mouseleave', stopNoteHandler);
|
263 |
+
keyElement.addEventListener('touchstart', playNoteHandler, { passive: false });
|
264 |
+
keyElement.addEventListener('touchend', stopNoteHandler);
|
265 |
+
keyElement.addEventListener('touchcancel', stopNoteHandler);
|
266 |
+
});
|
267 |
+
}
|
268 |
+
// ๋ง์ง๋ง C ๊ฑด๋ฐ ์ถ๊ฐ
|
269 |
+
const lastCOctave = baseOctave + numOctaves;
|
270 |
+
const lastCKey = document.createElement('div');
|
271 |
+
lastCKey.classList.add('key', 'white-key');
|
272 |
+
lastCKey.dataset.note = 'C' + lastCOctave;
|
273 |
+
// const lastCLabel = document.createElement('span');
|
274 |
+
// lastCLabel.classList.add('key-label');
|
275 |
+
// lastCLabel.textContent = 'C';
|
276 |
+
// lastCKey.appendChild(lastCLabel);
|
277 |
+
pianoElement.appendChild(lastCKey);
|
278 |
+
const lastCNoteName = 'C' + lastCOctave;
|
279 |
+
lastCKey.addEventListener('mousedown', (e) => { e.preventDefault(); lastCKey.classList.add('active'); synth.triggerAttackRelease(lastCNoteName, "8n"); createParticles(lastCKey); });
|
280 |
+
lastCKey.addEventListener('mouseup', (e) => { e.preventDefault(); lastCKey.classList.remove('active'); });
|
281 |
+
lastCKey.addEventListener('mouseleave', () => lastCKey.classList.remove('active'));
|
282 |
+
lastCKey.addEventListener('touchstart', (e) => { e.preventDefault(); lastCKey.classList.add('active'); synth.triggerAttackRelease(lastCNoteName, "8n"); createParticles(lastCKey);}, { passive: false });
|
283 |
+
lastCKey.addEventListener('touchend', (e) => { e.preventDefault(); lastCKey.classList.remove('active'); });
|
284 |
+
lastCKey.addEventListener('touchcancel', () => lastCKey.classList.remove('active'));
|
285 |
+
|
286 |
+
|
287 |
+
// ํํฐํด ์์ฑ ํจ์
|
288 |
+
function createParticles(keyElement, color = 'white') {
|
289 |
+
const rect = keyElement.getBoundingClientRect();
|
290 |
+
const pianoRect = pianoElement.getBoundingClientRect();
|
291 |
+
for (let i = 0; i < 10; i++) { // 10๊ฐ์ ํํฐํด ์์ฑ
|
292 |
+
const particle = document.createElement('div');
|
293 |
+
particle.classList.add('particle');
|
294 |
+
particle.style.setProperty('--particle-color', color);
|
295 |
+
|
296 |
+
// ํผ์๋
ธ ์ปจํ
์ด๋ ๋ด์ ์๋์ ์์น๋ก ์ค์
|
297 |
+
particle.style.left = (rect.left - pianoRect.left + rect.width / 2 + (Math.random() - 0.5) * 20) + 'px';
|
298 |
+
particle.style.bottom = (pianoRect.height - (rect.top - pianoRect.top) - rect.height / 2) + 'px'; // Y์ถ ๋ฐ์ ํ์ฌ bottom ๊ธฐ์ค์ผ๋ก ์ค์
|
299 |
+
|
300 |
+
noteFallArea.appendChild(particle);
|
301 |
+
setTimeout(() => {
|
302 |
+
particle.remove();
|
303 |
+
}, 500); // ์ ๋๋ฉ์ด์
์๊ฐ๊ณผ ๋์ผํ๊ฒ ์ค์
|
304 |
+
}
|
305 |
+
}
|
306 |
+
|
307 |
+
|
308 |
+
// --- ๋
ธํธ ๋จ์ด์ง๋ ํจ๊ณผ ---
|
309 |
+
const hitLineY = noteFallArea.offsetHeight; // ํ์ ์ ์์น (๋
ธํธ ๋จ์ด์ง๋ ์์ญ์ ๋ฐ๋ฅ)
|
310 |
+
const fallDurationSeconds = 2; // ๋
ธํธ๊ฐ ๋จ์ด์ง๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ (์ด)
|
311 |
+
|
312 |
+
// ์ํ ์
๋ณด ๋ฐ์ดํฐ: { time: ์์์๊ฐ(์ด), note: ์์ด๋ฆ, duration: ๊ธธ์ด(์ด), color: [r,g,b] }
|
313 |
+
const sampleSong = [
|
314 |
+
{ time: 0, note: 'C4', duration: 0.4, color: [0, 255, 0] },
|
315 |
+
{ time: 0.5, note: 'C4', duration: 0.4, color: [0, 255, 0] },
|
316 |
+
{ time: 1, note: 'G4', duration: 0.4, color: [255, 255, 0] },
|
317 |
+
{ time: 1.5, note: 'G4', duration: 0.4, color: [255, 255, 0] },
|
318 |
+
{ time: 2, note: 'A4', duration: 0.4, color: [255, 0, 255] },
|
319 |
+
{ time: 2.5, note: 'A4', duration: 0.4, color: [255, 0, 255] },
|
320 |
+
{ time: 3, note: 'G4', duration: 0.8, color: [255, 255, 0] },
|
321 |
+
{ time: 4, note: 'F4', duration: 0.4, color: [0, 255, 255] },
|
322 |
+
{ time: 4.5, note: 'F4', duration: 0.4, color: [0, 255, 255] },
|
323 |
+
{ time: 5, note: 'E4', duration: 0.4, color: [255, 165, 0] },
|
324 |
+
{ time: 5.5, note: 'E4', duration: 0.4, color: [255, 165, 0] },
|
325 |
+
{ time: 6, note: 'D4', duration: 0.4, color: [0, 0, 255] },
|
326 |
+
{ time: 6.5, note: 'D4', duration: 0.4, color: [0, 0, 255] },
|
327 |
+
{ time: 7, note: 'C4', duration: 0.8, color: [0, 255, 0] },
|
328 |
];
|
329 |
|
330 |
+
function playSampleSong() {
|
331 |
+
if (Tone.Transport.state === 'started') {
|
332 |
+
Tone.Transport.stop();
|
333 |
+
// ์ด๋ฏธ ์์ฑ๋ ๋
ธํธ๋ค ์ ๊ฑฐ
|
334 |
+
document.querySelectorAll('.note-bar').forEach(n => n.remove());
|
335 |
+
playButton.textContent = 'Play Sample';
|
336 |
+
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
337 |
}
|
338 |
+
|
339 |
+
playButton.textContent = 'Stop';
|
340 |
+
Tone.Transport.bpm.value = 100; // ๊ณก์ BPM ์ค์
|
341 |
+
|
342 |
+
sampleSong.forEach(noteData => {
|
343 |
+
Tone.Transport.scheduleOnce(time => {
|
344 |
+
// ๋
ธํธ ๋ฐ ์์ฑ
|
345 |
+
const noteElement = document.createElement('div');
|
346 |
+
noteElement.classList.add('note-bar');
|
347 |
+
|
348 |
+
const targetKeyElement = pianoElement.querySelector(`.key[data-note="${noteData.note}"]`);
|
349 |
+
if (targetKeyElement) {
|
350 |
+
const keyRect = targetKeyElement.getBoundingClientRect();
|
351 |
+
const pianoRect = pianoElement.getBoundingClientRect();
|
352 |
+
|
353 |
+
noteElement.style.width = keyRect.width - 2 + 'px'; // -2 for border
|
354 |
+
noteElement.style.left = (keyRect.left - pianoRect.left + 1) + 'px';
|
355 |
+
noteElement.style.height = (noteData.duration * 100) + 'px'; // ๊ธธ์ด์ ๋ฐ๋ผ ๋์ด ์กฐ์ (์์์ ๋น์จ)
|
356 |
+
|
357 |
+
// ์์ ์ค์
|
358 |
+
const [r, g, b] = noteData.color;
|
359 |
+
noteElement.style.setProperty('--note-color-start', `rgba(${r}, ${g}, ${b}, 0.9)`);
|
360 |
+
noteElement.style.setProperty('--note-color-end', `rgba(${r*0.7}, ${g*0.7}, ${b*0.7}, 0.9)`);
|
361 |
+
|
362 |
+
|
363 |
+
noteElement.style.top = `-${noteElement.style.height}`; // ํ๋ฉด ์์์ ์์
|
364 |
+
noteFallArea.appendChild(noteElement);
|
365 |
+
|
366 |
+
// ์ ๋๋ฉ์ด์
|
367 |
+
noteElement.animate([
|
368 |
+
{ transform: `translateY(0px)` }, // ์์ ์์น (CSS์์ top: -height๋ก ์ค์ ํ์ผ๋ฏ๋ก Y๋ 0๋ถํฐ ์์)
|
369 |
+
{ transform: `translateY(${hitLineY + parseFloat(noteElement.style.height)}px)` } // ํ์ ์ ์๋๋ก ์์ ํ ์ง๋๊ฐ๋๋ก
|
370 |
+
], {
|
371 |
+
duration: fallDurationSeconds * 1000,
|
372 |
+
easing: 'linear'
|
373 |
+
});
|
374 |
+
|
375 |
+
// ํ์ ์์ ์ ์๋ฆฌ ์ฌ์ ๋ฐ ํจ๊ณผ
|
376 |
+
Tone.Transport.scheduleOnce(hitTime => {
|
377 |
+
synth.triggerAttackRelease(noteData.note, noteData.duration, hitTime);
|
378 |
+
targetKeyElement.classList.add('active');
|
379 |
+
targetKeyElement.style.setProperty('--key-glow-color', `rgba(${r}, ${g}, ${b}, 0.9)`);
|
380 |
+
createParticles(targetKeyElement, `rgba(${r}, ${g}, ${b}, 0.9)`);
|
381 |
+
|
382 |
+
// ๋
ธํธ ์ ๊ฑฐ ๋ฐ ๊ฑด๋ฐ ๋นํ์ฑํ ํ์ด๋จธ
|
383 |
+
setTimeout(() => {
|
384 |
+
targetKeyElement.classList.remove('active');
|
385 |
+
}, noteData.duration * 1000 * (60 / Tone.Transport.bpm.value)); // BPM ๊ณ ๋ ค
|
386 |
+
|
387 |
+
// ๋
ธํธ ๋ฐ ์ ๊ฑฐ (์ ๋๋ฉ์ด์
๋๋ ํ)
|
388 |
+
setTimeout(() => {
|
389 |
+
if (noteElement.parentNode) {
|
390 |
+
noteElement.remove();
|
391 |
+
}
|
392 |
+
}, fallDurationSeconds * 1000);
|
393 |
+
|
394 |
+
}, time + fallDurationSeconds); // ๋
ธํธ๊ฐ ๋จ์ด์ง๋ ์๊ฐ๋งํผ ๋ค์ ์๋ฆฌ ์ฌ์
|
395 |
+
}
|
396 |
+
}, noteData.time);
|
397 |
+
});
|
398 |
+
|
399 |
+
Tone.Transport.start();
|
400 |
+
}
|
401 |
|
402 |
+
playButton.addEventListener('click', () => {
|
403 |
+
Tone.start(); // ์ฌ์ฉ์ ์ธํฐ๋์
ํ ์ค๋์ค ์ปจํ
์คํธ ์์
|
404 |
+
playSampleSong();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
405 |
});
|
406 |
|
407 |
</script>
|