Files
Learn_System/frontend/js/phys.js
T
Maxim Dolgolyov 573de62963 feat(phys10 phase0): skeleton + миграция + phys.js модуль (37 §, 6 глав)
- Миграция 030_physics_10_hub.sql: hub physics-10 + 6 ch (color amber, sort 11, 37 §)
- frontend/textbooks/physics_10_hub.html (hub, yellow/amber palette, 6 chapter cards, финал placeholder)
- 6 ch-файлов physics_10_ch{1..6}.html: skeleton с PARAS, sec-nodes, SIDEBARS, TIPS,
  STUB-builder'ами для всех 37 §§ + 6 финалов, POLISH CSS, ICONS, 2D-хелперы,
  подключения phys.js + g3d.js
- frontend/js/phys.js: новый модуль window.PHYS с 21 экспортом —
  drawArrow, fieldLinesPointCharge, chargeMark, magneticFieldGrid, molecule,
  createGasSim, batteryEMF, resistor, capacitorSymbol, ammeterSymbol,
  voltmeterSymbol, lightbulbSymbol, inductorSymbol, wire, CONST + 6 конвертеров единиц

Все ch следуют паттерну algebra_11_ch1.html (Wave 5). Авторы не указаны.
Phase 1+ — наполнение содержанием по учебнику «Физика 10» (Беларусь, 2019).

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
2026-05-29 16:12:08 +03:00

253 lines
11 KiB
JavaScript

// phys.js — модуль физических хелперов для учебника Физика 10
// Экспорт в window.PHYS = { ... }
(function(){
'use strict';
// === Стрелка вектора (2D) ===
function drawArrow(x1, y1, x2, y2, color, width, headSize) {
width = width || 2;
headSize = headSize || 10;
const dx = x2 - x1, dy = y2 - y1;
const len = Math.sqrt(dx*dx + dy*dy);
if (len < 1e-6) return '';
const ux = dx / len, uy = dy / len;
const px = -uy, py = ux;
const h = headSize, w = headSize * 0.6;
const bx = x2 - ux*h, by = y2 - uy*h;
const lx = bx + px*w, ly = by + py*w;
const rx = bx - px*w, ry = by - py*w;
return `<line x1="${x1.toFixed(1)}" y1="${y1.toFixed(1)}" x2="${bx.toFixed(1)}" y2="${by.toFixed(1)}" stroke="${color}" stroke-width="${width}" stroke-linecap="round"/>`
+ `<polygon points="${x2.toFixed(1)},${y2.toFixed(1)} ${lx.toFixed(1)},${ly.toFixed(1)} ${rx.toFixed(1)},${ry.toFixed(1)}" fill="${color}"/>`;
}
// === Линии электрического поля от точечного заряда ===
function fieldLinesPointCharge(cx, cy, sign, scale, numLines) {
numLines = numLines || 16;
scale = scale || 80;
let s = '';
const color = sign > 0 ? '#dc2626' : '#2563eb';
for (let i = 0; i < numLines; i++) {
const a = 2 * Math.PI * i / numLines;
const r1 = 18, r2 = scale;
const x1 = cx + r1*Math.cos(a), y1 = cy + r1*Math.sin(a);
const x2 = cx + r2*Math.cos(a), y2 = cy + r2*Math.sin(a);
if (sign > 0) s += drawArrow(x1, y1, x2, y2, color, 1.4, 7);
else s += drawArrow(x2, y2, x1, y1, color, 1.4, 7);
}
return s;
}
// === Обозначение заряда (кружок с +/-) ===
function chargeMark(cx, cy, sign, r, label) {
r = r || 14;
const color = sign > 0 ? '#dc2626' : '#2563eb';
const fill = sign > 0 ? '#fecaca' : '#bfdbfe';
let s = '';
s += `<circle cx="${cx}" cy="${cy}" r="${r}" fill="${fill}" stroke="${color}" stroke-width="2"/>`;
if (sign > 0) {
s += `<line x1="${cx-r*0.5}" y1="${cy}" x2="${cx+r*0.5}" y2="${cy}" stroke="${color}" stroke-width="2.5"/>`;
s += `<line x1="${cx}" y1="${cy-r*0.5}" x2="${cx}" y2="${cy+r*0.5}" stroke="${color}" stroke-width="2.5"/>`;
} else {
s += `<line x1="${cx-r*0.5}" y1="${cy}" x2="${cx+r*0.5}" y2="${cy}" stroke="${color}" stroke-width="2.5"/>`;
}
if (label) {
s += `<text x="${cx+r+4}" y="${cy+4}" font-family="Inter,sans-serif" font-size="13" font-weight="700" fill="${color}">${label}</text>`;
}
return s;
}
// === Магнитное поле сквозь экран (сетка крестиков или точек) ===
function magneticFieldGrid(x0, y0, w, h, cols, rows, direction) {
// direction: 'in' = крест (× — вошло в плоскость), 'out' = точка (• — вышло)
let s = '';
const dx = w / (cols - 1), dy = h / (rows - 1);
const color = '#7c3aed';
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
const cx = x0 + i * dx, cy = y0 + j * dy;
s += `<circle cx="${cx}" cy="${cy}" r="6" fill="white" stroke="${color}" stroke-width="1.3"/>`;
if (direction === 'in') {
s += `<line x1="${cx-4}" y1="${cy-4}" x2="${cx+4}" y2="${cy+4}" stroke="${color}" stroke-width="1.5"/>`;
s += `<line x1="${cx-4}" y1="${cy+4}" x2="${cx+4}" y2="${cy-4}" stroke="${color}" stroke-width="1.5"/>`;
} else {
s += `<circle cx="${cx}" cy="${cy}" r="2.2" fill="${color}"/>`;
}
}
}
return s;
}
// === Молекула газа (частица) ===
function molecule(x, y, r, color) {
r = r || 4;
color = color || '#2563eb';
return `<circle cx="${x.toFixed(1)}" cy="${y.toFixed(1)}" r="${r}" fill="${color}" stroke="#0f172a" stroke-width="0.6"/>`;
}
// === Симуляция газа (упругое столкновение со стенками) ===
function createGasSim(opts) {
opts = opts || {};
const N = opts.N || 30;
const W = opts.W || 320;
const H = opts.H || 220;
const baseSpeed = opts.speed || 60; // px/s
const r = opts.r || 4;
const particles = [];
for (let i = 0; i < N; i++) {
particles.push({
x: r + Math.random() * (W - 2*r),
y: r + Math.random() * (H - 2*r),
vx: (Math.random() - 0.5) * 2 * baseSpeed,
vy: (Math.random() - 0.5) * 2 * baseSpeed
});
}
return {
W: W, H: H, r: r, particles: particles,
step(dt) {
for (const p of particles) {
p.x += p.vx * dt; p.y += p.vy * dt;
if (p.x < r) { p.x = r; p.vx = -p.vx; }
if (p.x > W - r) { p.x = W - r; p.vx = -p.vx; }
if (p.y < r) { p.y = r; p.vy = -p.vy; }
if (p.y > H - r) { p.y = H - r; p.vy = -p.vy; }
}
},
render(color) {
color = color || '#2563eb';
return particles.map(p => molecule(p.x, p.y, r, color)).join('');
},
setSpeed(scale) {
for (const p of particles) { p.vx *= scale; p.vy *= scale; }
}
};
}
// === Электрические схемы: компоненты ===
// orientation: 'h' (горизонтально, по умолчанию) или 'v' (вертикально)
function batteryEMF(x, y, EMF, orientation) {
orientation = orientation || 'h';
let s = '';
if (orientation === 'h') {
s += `<line x1="${x-3}" y1="${y-18}" x2="${x-3}" y2="${y+18}" stroke="#0f172a" stroke-width="2.5"/>`;
s += `<line x1="${x+3}" y1="${y-9}" x2="${x+3}" y2="${y+9}" stroke="#0f172a" stroke-width="2.5"/>`;
s += `<text x="${x-3}" y="${y-24}" text-anchor="middle" font-family="Inter,sans-serif" font-size="13" font-weight="700" fill="#0f172a">+</text>`;
s += `<text x="${x+3}" y="${y-24}" text-anchor="middle" font-family="Inter,sans-serif" font-size="13" font-weight="700" fill="#0f172a">&#8722;</text>`;
if (EMF !== undefined) s += `<text x="${x}" y="${y+34}" text-anchor="middle" font-family="JetBrains Mono,monospace" font-size="12" fill="#0f172a">&#949; = ${EMF}</text>`;
} else {
s += `<line x1="${x-18}" y1="${y-3}" x2="${x+18}" y2="${y-3}" stroke="#0f172a" stroke-width="2.5"/>`;
s += `<line x1="${x-9}" y1="${y+3}" x2="${x+9}" y2="${y+3}" stroke="#0f172a" stroke-width="2.5"/>`;
if (EMF !== undefined) s += `<text x="${x+24}" y="${y+4}" font-family="JetBrains Mono,monospace" font-size="12" fill="#0f172a">&#949; = ${EMF}</text>`;
}
return s;
}
function resistor(x, y, R, orientation) {
orientation = orientation || 'h';
let s = '';
if (orientation === 'h') {
s += `<rect x="${x-20}" y="${y-7}" width="40" height="14" fill="white" stroke="#0f172a" stroke-width="1.6"/>`;
if (R !== undefined) s += `<text x="${x}" y="${y+25}" text-anchor="middle" font-family="JetBrains Mono,monospace" font-size="12" fill="#0f172a">R = ${R}</text>`;
} else {
s += `<rect x="${x-7}" y="${y-20}" width="14" height="40" fill="white" stroke="#0f172a" stroke-width="1.6"/>`;
if (R !== undefined) s += `<text x="${x+18}" y="${y+4}" font-family="JetBrains Mono,monospace" font-size="12" fill="#0f172a">R = ${R}</text>`;
}
return s;
}
function capacitorSymbol(x, y, C, orientation) {
orientation = orientation || 'h';
let s = '';
if (orientation === 'h') {
s += `<line x1="${x-3}" y1="${y-14}" x2="${x-3}" y2="${y+14}" stroke="#0f172a" stroke-width="2.5"/>`;
s += `<line x1="${x+3}" y1="${y-14}" x2="${x+3}" y2="${y+14}" stroke="#0f172a" stroke-width="2.5"/>`;
if (C !== undefined) s += `<text x="${x}" y="${y+30}" text-anchor="middle" font-family="JetBrains Mono,monospace" font-size="12" fill="#0f172a">C = ${C}</text>`;
}
return s;
}
function ammeterSymbol(x, y, r) {
r = r || 14;
return `<circle cx="${x}" cy="${y}" r="${r}" fill="white" stroke="#0f172a" stroke-width="1.6"/>`
+ `<text x="${x}" y="${y+5}" text-anchor="middle" font-family="Inter,sans-serif" font-size="13" font-weight="700" fill="#d97706">A</text>`;
}
function voltmeterSymbol(x, y, r) {
r = r || 14;
return `<circle cx="${x}" cy="${y}" r="${r}" fill="white" stroke="#0f172a" stroke-width="1.6"/>`
+ `<text x="${x}" y="${y+5}" text-anchor="middle" font-family="Inter,sans-serif" font-size="13" font-weight="700" fill="#2563eb">V</text>`;
}
function lightbulbSymbol(x, y, r) {
r = r || 14;
let s = '';
s += `<circle cx="${x}" cy="${y}" r="${r}" fill="#fef3c7" stroke="#0f172a" stroke-width="1.6"/>`;
s += `<line x1="${x-r*0.7}" y1="${y-r*0.7}" x2="${x+r*0.7}" y2="${y+r*0.7}" stroke="#0f172a" stroke-width="1.4"/>`;
s += `<line x1="${x-r*0.7}" y1="${y+r*0.7}" x2="${x+r*0.7}" y2="${y-r*0.7}" stroke="#0f172a" stroke-width="1.4"/>`;
return s;
}
function inductorSymbol(x, y, L, orientation) {
orientation = orientation || 'h';
let s = '';
if (orientation === 'h') {
for (let i = 0; i < 4; i++) {
const cx = x - 15 + i * 10;
s += `<path d="M ${cx-5} ${y} A 5 5 0 0 1 ${cx+5} ${y}" fill="none" stroke="#0f172a" stroke-width="1.6"/>`;
}
if (L !== undefined) s += `<text x="${x}" y="${y+22}" text-anchor="middle" font-family="JetBrains Mono,monospace" font-size="12" fill="#0f172a">L = ${L}</text>`;
}
return s;
}
function wire(x1, y1, x2, y2) {
return `<line x1="${x1}" y1="${y1}" x2="${x2}" y2="${y2}" stroke="#0f172a" stroke-width="1.6"/>`;
}
// === Эталонные константы ===
const CONST = {
k: 9e9, // Кулона
e: 1.6e-19, // элементарный заряд
eps0: 8.85e-12, // электрическая постоянная
kB: 1.38e-23, // Больцмана
NA: 6.022e23, // Авогадро
R: 8.314, // универсальная газовая
c: 3e8, // скорость света
g: 9.8, // ускорение свободного падения
atm: 101325, // 1 атм в Па
T0: 273.15 // ноль Цельсия в К
};
// === Конвертеры единиц ===
function celsiusToKelvin(t) { return t + 273.15; }
function kelvinToCelsius(T) { return T - 273.15; }
function atmToPa(p) { return p * 101325; }
function paToAtm(p) { return p / 101325; }
function litersToM3(V) { return V / 1000; }
function m3ToLiters(V) { return V * 1000; }
// === Экспорт ===
window.PHYS = {
drawArrow: drawArrow,
fieldLinesPointCharge: fieldLinesPointCharge,
chargeMark: chargeMark,
magneticFieldGrid: magneticFieldGrid,
molecule: molecule,
createGasSim: createGasSim,
batteryEMF: batteryEMF,
resistor: resistor,
capacitorSymbol: capacitorSymbol,
ammeterSymbol: ammeterSymbol,
voltmeterSymbol: voltmeterSymbol,
lightbulbSymbol: lightbulbSymbol,
inductorSymbol: inductorSymbol,
wire: wire,
CONST: CONST,
celsiusToKelvin: celsiusToKelvin,
kelvinToCelsius: kelvinToCelsius,
atmToPa: atmToPa,
paToAtm: paToAtm,
litersToM3: litersToM3,
m3ToLiters: m3ToLiters
};
})();