index.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <!DOCTYPE html>
  2. <html lang="ru">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Механические волны и Фурье v1.1</title>
  7. <style>
  8. body {
  9. font-family: Arial, sans-serif;
  10. background: #fdfaf6;
  11. margin: 0;
  12. padding: 10px;
  13. color: #333;
  14. }
  15. .grid-container {
  16. display: grid;
  17. grid-template-columns: 1fr 1fr;
  18. grid-template-rows: auto auto;
  19. gap: 15px;
  20. height: 90vh;
  21. }
  22. .panel {
  23. background: white;
  24. border: 1px solid #ccc;
  25. padding: 15px;
  26. border-radius: 5px;
  27. box-shadow: 0 4px 6px rgba(0,0,0,0.05);
  28. display: flex;
  29. flex-direction: column;
  30. }
  31. canvas {
  32. width: 100%;
  33. height: 200px;
  34. background: #fafafa;
  35. border: 1px solid #ddd;
  36. margin-top: 10px;
  37. }
  38. .formulas {
  39. font-family: 'Courier New', Courier, monospace;
  40. background: #eef7e8;
  41. padding: 10px;
  42. text-align: center;
  43. font-weight: bold;
  44. border: 1px dashed #4CAF50;
  45. margin-bottom: 10px;
  46. }
  47. .tabs {
  48. display: flex;
  49. gap: 5px;
  50. margin-bottom: 15px;
  51. }
  52. .tab {
  53. padding: 8px 15px;
  54. cursor: pointer;
  55. background: #eee;
  56. border: 1px solid #ccc;
  57. border-radius: 4px;
  58. }
  59. .tab.active {
  60. background: #4CAF50;
  61. color: white;
  62. font-weight: bold;
  63. border-color: #4CAF50;
  64. }
  65. .control-row {
  66. margin-bottom: 15px;
  67. display: flex;
  68. align-items: center;
  69. }
  70. label {
  71. display: inline-block;
  72. width: 140px;
  73. font-weight: bold;
  74. }
  75. input[type=range] {
  76. flex-grow: 1;
  77. margin-right: 15px;
  78. }
  79. .value-display {
  80. width: 60px;
  81. text-align: right;
  82. }
  83. .btn {
  84. padding: 10px 15px;
  85. border: none;
  86. border-radius: 4px;
  87. cursor: pointer;
  88. font-weight: bold;
  89. color: white;
  90. margin-right: 10px;
  91. margin-bottom: 10px;
  92. display: inline-block;
  93. }
  94. .btn-audio { background: #2196F3; }
  95. .btn-dist { background: #9C27B0; }
  96. .btn-reset { background: #607D8B; } /* Новая кнопка */
  97. .btn-audio.active { background: #f44336; }
  98. .btn-dist.active { background: #d32f2f; }
  99. </style>
  100. </head>
  101. <body>
  102. <div class="grid-container">
  103. <!-- 1. Левая верхняя часть -->
  104. <div class="panel">
  105. <h3 style="margin:0;">1. Базовое колебание (выбранное)</h3>
  106. <canvas id="canvas-single"></canvas>
  107. </div>
  108. <!-- 3. Правая верхняя часть -->
  109. <div class="panel">
  110. <h3 style="margin:0;">3. Теорема Фурье (Сумма колебаний)</h3>
  111. <p style="font-size: 0.85em; font-style: italic; color: #666; margin: 5px 0 0 0;">
  112. Любое колебание может быть представлено в виде суммы гармонических колебаний.
  113. </p>
  114. <canvas id="canvas-sum"></canvas>
  115. </div>
  116. <!-- 2. Левая нижняя часть -->
  117. <div class="panel">
  118. <h3 style="margin:0 0 10px 0;">2. Элементы управления</h3>
  119. <div class="formulas">
  120. y = A · cos(wt + θ) &nbsp;&nbsp;|&nbsp;&nbsp; V = 1/T &nbsp;&nbsp;|&nbsp;&nbsp; w = 2πV
  121. </div>
  122. <div class="tabs">
  123. <div class="tab active" onclick="switchTab(0)">V1 (Волна 1)</div>
  124. <div class="tab" onclick="switchTab(1)">V2 (Волна 2)</div>
  125. <div class="tab" onclick="switchTab(2)">V3 (Волна 3)</div>
  126. </div>
  127. <div class="control-row">
  128. <label>Шаг частоты:</label>
  129. <input type="radio" name="step" value="10" onchange="updateStep(10)"> 10 Гц
  130. <input type="radio" name="step" value="100" onchange="updateStep(100)" checked> 100 Гц
  131. </div>
  132. <div class="control-row">
  133. <label>Частота V (до 20 кГц):</label>
  134. <input type="range" id="freq" min="0" max="20000" step="100" value="440" oninput="updateWave()">
  135. <div class="value-display"><span id="valV">440</span> Гц</div>
  136. </div>
  137. <div class="control-row">
  138. <label>Амплитуда A:</label>
  139. <input type="range" id="amp" min="0" max="100" value="80" oninput="updateWave()">
  140. <div class="value-display"><span id="valA">80</span>%</div>
  141. </div>
  142. <div style="margin-top: 10px;">
  143. <button class="btn btn-audio" onclick="toggleAudio()" id="btnAudio">🔊 Вывод на динамике</button>
  144. <button class="btn btn-dist" onclick="toggleClip()" id="btnClip">Добавить нелинейное преобразование</button>
  145. <button class="btn btn-reset" onclick="resetAll()">🔄 Сбросить все</button> <!-- Новая фича -->
  146. </div>
  147. </div>
  148. <!-- 4. Правая нижняя часть -->
  149. <div class="panel">
  150. <h3 style="margin:0;">4. Спектр</h3>
  151. <canvas id="canvas-spectrum"></canvas>
  152. </div>
  153. </div>
  154. <script>
  155. let waves = [
  156. { f: 440, a: 80 },
  157. { f: 880, a: 0 },
  158. { f: 1320, a: 0 }
  159. ];
  160. let currentWave = 0;
  161. let audioCtx = null;
  162. let oscillators = [];
  163. let isAudioOn = false;
  164. let isClipped = false;
  165. let freqStep = 100;
  166. let time = 0;
  167. function switchTab(index) {
  168. currentWave = index;
  169. document.querySelectorAll('.tab').forEach((el, i) => {
  170. el.classList.toggle('active', i === index);
  171. });
  172. document.getElementById('freq').value = waves[index].f;
  173. document.getElementById('amp').value = waves[index].a;
  174. updateLabels();
  175. }
  176. function updateStep(step) {
  177. freqStep = step;
  178. document.getElementById('freq').step = step;
  179. }
  180. function updateLabels() {
  181. document.getElementById('valV').innerText = waves[currentWave].f;
  182. document.getElementById('valA').innerText = waves[currentWave].a;
  183. }
  184. function updateWave() {
  185. waves[currentWave].f = parseInt(document.getElementById('freq').value);
  186. waves[currentWave].a = parseInt(document.getElementById('amp').value);
  187. updateLabels();
  188. updateAudio();
  189. }
  190. function toggleClip() {
  191. isClipped = !isClipped;
  192. let btn = document.getElementById('btnClip');
  193. if (isClipped) {
  194. btn.classList.add('active');
  195. btn.innerText = "Убрать нелинейное преобразование";
  196. } else {
  197. btn.classList.remove('active');
  198. btn.innerText = "Добавить нелинейное преобразование";
  199. }
  200. updateAudio();
  201. }
  202. // --- ИСПРАВЛЕНИЕ: Функция сброса ---
  203. function resetAll() {
  204. waves = [
  205. { f: 440, a: 80 },
  206. { f: 880, a: 0 },
  207. { f: 1320, a: 0 }
  208. ];
  209. isClipped = false;
  210. let btnClip = document.getElementById('btnClip');
  211. btnClip.classList.remove('active');
  212. btnClip.innerText = "Добавить нелинейное преобразование";
  213. switchTab(0);
  214. updateAudio();
  215. }
  216. function toggleAudio() {
  217. if (!audioCtx) {
  218. audioCtx = new (window.AudioContext || window.webkitAudioContext)();
  219. for (let i = 0; i < 3; i++) {
  220. let osc = audioCtx.createOscillator();
  221. let gain = audioCtx.createGain();
  222. osc.type = 'sine';
  223. osc.connect(gain);
  224. gain.connect(audioCtx.destination);
  225. osc.start();
  226. oscillators.push({ osc: osc, gain: gain });
  227. }
  228. }
  229. isAudioOn = !isAudioOn;
  230. let btn = document.getElementById('btnAudio');
  231. if (isAudioOn) {
  232. audioCtx.resume();
  233. btn.classList.add('active');
  234. btn.innerText = "🔇 Выключить звук";
  235. } else {
  236. audioCtx.suspend();
  237. btn.classList.remove('active');
  238. btn.innerText = "🔊 Вывод на динамике";
  239. }
  240. updateAudio();
  241. }
  242. function updateAudio() {
  243. if (!audioCtx) return;
  244. for (let i = 0; i < 3; i++) {
  245. oscillators[i].osc.frequency.setValueAtTime(waves[i].f, audioCtx.currentTime);
  246. let vol = isAudioOn ? (waves[i].a * 0.005) : 0;
  247. if (isClipped && isAudioOn && waves[i].a > 0) vol *= 2.5;
  248. // --- ИСПРАВЛЕНИЕ: Плавное изменение звука (без щелчка) ---
  249. oscillators[i].gain.gain.setTargetAtTime(vol, audioCtx.currentTime, 0.05);
  250. oscillators[i].osc.type = isClipped ? 'square' : 'sine';
  251. }
  252. }
  253. function draw() {
  254. time += 0.08;
  255. let c1 = document.getElementById('canvas-single');
  256. let ctx1 = c1.getContext('2d');
  257. ctx1.clearRect(0, 0, c1.width, c1.height);
  258. ctx1.beginPath(); ctx1.strokeStyle = "#ccc";
  259. ctx1.moveTo(0, c1.height/2); ctx1.lineTo(c1.width, c1.height/2); ctx1.stroke();
  260. ctx1.beginPath();
  261. ctx1.strokeStyle = "#2196F3";
  262. ctx1.lineWidth = 2;
  263. for (let x = 0; x < c1.width; x++) {
  264. let visualF = (waves[currentWave].f / 200) || 1;
  265. let y = Math.cos(x * 0.02 * visualF - time) * (waves[currentWave].a / 100) * (c1.height/2.5);
  266. if(x===0) ctx1.moveTo(x, c1.height/2 - y);
  267. else ctx1.lineTo(x, c1.height/2 - y);
  268. }
  269. ctx1.stroke();
  270. let c2 = document.getElementById('canvas-sum');
  271. let ctx2 = c2.getContext('2d');
  272. ctx2.clearRect(0, 0, c2.width, c2.height);
  273. ctx2.beginPath(); ctx2.strokeStyle = "#ccc";
  274. ctx2.moveTo(0, c2.height/2); ctx2.lineTo(c2.width, c2.height/2); ctx2.stroke();
  275. ctx2.beginPath();
  276. ctx2.strokeStyle = "#E91E63";
  277. ctx2.lineWidth = 2;
  278. for (let x = 0; x < c2.width; x++) {
  279. let sumY = 0;
  280. for (let i = 0; i < 3; i++) {
  281. let visualF = (waves[i].f / 200) || 1;
  282. sumY += Math.cos(x * 0.02 * visualF - time) * (waves[i].a / 100) * (c2.height/2.5);
  283. }
  284. if (isClipped) {
  285. let limit = (c2.height/4);
  286. if (sumY > limit) sumY = limit;
  287. if (sumY < -limit) sumY = -limit;
  288. }
  289. if(x===0) ctx2.moveTo(x, c2.height/2 - sumY/1.5);
  290. else ctx2.lineTo(x, c2.height/2 - sumY/1.5);
  291. }
  292. ctx2.stroke();
  293. let c3 = document.getElementById('canvas-spectrum');
  294. let ctx3 = c3.getContext('2d');
  295. ctx3.clearRect(0, 0, c3.width, c3.height);
  296. ctx3.beginPath(); ctx3.strokeStyle = "#ccc";
  297. ctx3.moveTo(0, c3.height - 15); ctx3.lineTo(c3.width, c3.height - 15); ctx3.stroke();
  298. // --- ИСПРАВЛЕНИЕ: Разметка частот спектра ---
  299. ctx3.fillStyle = "#999";
  300. ctx3.font = "10px Arial";
  301. ctx3.fillText("0", 5, c3.height - 2);
  302. ctx3.fillText("5кГц", (5000 / 20000) * c3.width, c3.height - 2);
  303. ctx3.fillText("10кГц", (10000 / 20000) * c3.width, c3.height - 2);
  304. ctx3.fillText("15кГц", (15000 / 20000) * c3.width, c3.height - 2);
  305. ctx3.fillStyle = "#4CAF50";
  306. for (let i = 0; i < 3; i++) {
  307. if (waves[i].a > 0) {
  308. let xPos = (waves[i].f / 20000) * c3.width;
  309. let h = (waves[i].a / 100) * (c3.height - 30);
  310. ctx3.fillRect(Math.max(xPos, 5), c3.height - 15 - h, 10, h);
  311. ctx3.fillStyle = "#333";
  312. ctx3.font = "10px Arial";
  313. ctx3.fillText(waves[i].f, Math.max(xPos-5, 0), c3.height - 25 - h);
  314. ctx3.fillStyle = "#4CAF50";
  315. }
  316. }
  317. if (isClipped && waves[0].a > 0) {
  318. ctx3.fillStyle = "rgba(156, 39, 176, 0.6)";
  319. for (let i = 3; i <= 9; i += 2) {
  320. let fakeF = waves[0].f * i;
  321. if (fakeF > 20000) break;
  322. let xPos = (fakeF / 20000) * c3.width;
  323. let h = (waves[0].a / (i*2) / 100) * (c3.height - 30);
  324. ctx3.fillRect(Math.max(xPos, 5), c3.height - 15 - h, 6, h);
  325. }
  326. }
  327. requestAnimationFrame(draw);
  328. }
  329. function resizeCanvases() {
  330. document.querySelectorAll('canvas').forEach(c => {
  331. c.width = c.parentElement.clientWidth - 30;
  332. });
  333. }
  334. window.addEventListener('resize', resizeCanvases);
  335. resizeCanvases();
  336. draw();
  337. updateLabels();
  338. </script>
  339. </body>
  340. </html>