site-web-alliance-boreale/public/js/planisphere.js

434 lines
20 KiB
JavaScript
Raw Normal View History

/* Alliance Boréale — le planisphère.
Le ciel tel qu'on le voit du Québec, à l'heure de la visite et depuis la
région choisie, en regardant vers le nord : le pôle céleste au centre, le zénith en haut, l'horizon nord
en bas. Projection stéréographique polaire. Les étoiles à l'intérieur du
cercle pointillé (déclinaison > 90° − latitude) ne passent jamais sous l'horizon :
ce sont les circumpolaires, et c'est parmi elles que les sites de
l'Alliance prennent leur nom.
Données : window.CIEL (ciel-donnees.js). Aucune dépendance, aucun tiers. */
(function () {
"use strict";
var CIEL = window.CIEL;
if (!CIEL) return;
/* Les régions : le choix change la latitude, donc le cercle des étoiles qui ne se couchent
jamais (plus on monte vers le nord, plus il s'élargit) et l'horizon. Toutes sont à l'heure
de l'Est. Québec, la capitale, par défaut. */
var REGIONS = [
["gatineau", "Gatineau", 45.48, -75.70], ["montreal", "Montréal", 45.50, -73.57],
["sherbrooke", "Sherbrooke", 45.40, -71.89], ["trois-rivieres", "Trois-Rivières", 46.34, -72.54],
["quebec", "Québec", 46.81, -71.21], ["saguenay", "Saguenay", 48.43, -71.07],
["rouyn-noranda", "Rouyn-Noranda", 48.24, -79.02], ["rimouski", "Rimouski", 48.45, -68.52],
["gaspe", "Gaspé", 48.83, -64.48], ["baie-comeau", "Baie-Comeau", 49.22, -68.15],
["chibougamau", "Chibougamau", 49.91, -74.37], ["sept-iles", "Sept-Îles", 50.22, -66.38],
["kuujjuaq", "Kuujjuaq", 58.10, -68.40]
];
var LAT = 46.81, LON = -71.21, LIEU = "Québec";
var DEC_MIN = 8; // bord de la carte : le ciel boréal
var RAD = Math.PI / 180;
var reduit = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
/* Les sites de la constellation. L'étoile vient de la table figée de
Set-OPS (scripts/etoiles.py) ; aucune adresse ne figure ici. */
var MEMBRES = [
{ etoile: "Miram", ra: 42.67, dec: 55.90, mag: 3.77, desig: "η de Persée",
qui: "Chezlepro", role: "membre fondateur" },
{ etoile: "Achird", ra: 12.28, dec: 57.82, mag: 3.46, desig: "η de Cassiopée",
qui: "TechnoLibre", role: "membre partenaire" }
];
var COUL = {
nuit: "#0A0E1C", nuit2: "#121831", filet: "#2C3560",
astre: "#F2ECDD", astre2: "#BEC0D4", astre3: "#8389AC",
or: "#F2C878", teal: "#4FE3C1", cyan: "#56C2FF", violet: "#A98BFF"
};
var F_TITRE = '"IM Fell English SC", Georgia, serif';
var F_ORNE = 'italic 400 {t}px "IM Fell English", Georgia, serif';
var F_MONO = '"JetBrains Mono", ui-monospace, monospace';
/* ---------- Astronomie ---------- */
function tempsSideral(date) { // temps sidéral local, en degrés
var jd = date.getTime() / 86400000 + 2440587.5;
var gmst = 280.46061837 + 360.98564736629 * (jd - 2451545.0);
return (((gmst + LON) % 360) + 360) % 360;
}
function hauteur(ra, dec, tsl) { // sinus de la hauteur sur l'horizon
var h = (tsl - ra) * RAD, d = dec * RAD, p = LAT * RAD;
return Math.sin(p) * Math.sin(d) + Math.cos(p) * Math.cos(d) * Math.cos(h);
}
function couleurBV(bv) { // indice de couleur → teinte d'astre
if (bv < 0) return [196, 214, 255];
if (bv < 0.3) return [226, 233, 255];
if (bv < 0.6) return [248, 246, 240];
if (bv < 1.0) return [255, 236, 200];
if (bv < 1.4) return [255, 214, 160];
return [255, 190, 140];
}
function Planisphere(racine) {
this.racine = racine;
this.disque = racine.querySelector(".disque");
this.canvas = racine.querySelector("canvas");
this.ctx = this.canvas.getContext("2d");
this.decalage = 0; // minutes ajoutées à « maintenant »
this.choisi = null; // index du membre en vue
this.surligne = null; // [ra, dec, nom] d'une place survolée
this.boutons = [];
this.construire();
var self = this;
if (window.ResizeObserver) new ResizeObserver(function () { self.dimensionner(); }).observe(this.disque);
else window.addEventListener("resize", function () { self.dimensionner(); });
this.dimensionner();
if (document.fonts && document.fonts.ready) document.fonts.ready.then(function () { self.dessiner(); });
setInterval(function () { if (!self.joue) self.dessiner(); }, 60000);
}
Planisphere.prototype.construire = function () {
var self = this;
// Points d'accès au clavier et au toucher, posés sur chaque étoile membre.
MEMBRES.forEach(function (m, i) {
var b = document.createElement("button");
b.type = "button";
b.className = "astre-membre";
b.setAttribute("aria-label", m.etoile + ", " + m.desig + " : le site de " + m.qui);
b.addEventListener("click", function () { self.choisir(i); });
b.addEventListener("mouseenter", function () { self.choisir(i, true); });
b.addEventListener("focus", function () { self.choisir(i, true); });
self.disque.appendChild(b);
self.boutons.push(b);
});
this.racine.querySelectorAll("[data-membre]").forEach(function (b) {
var i = +b.getAttribute("data-membre");
b.addEventListener("click", function () { self.choisir(i); });
b.addEventListener("mouseenter", function () { self.choisir(i, true); });
b.addEventListener("focus", function () { self.choisir(i, true); });
});
this.lieu = this.racine.querySelector("[data-lieu]");
var choix = this.racine.querySelector("select[data-region]");
if (choix) {
var garde = null;
try { garde = window.localStorage.getItem("ab-region"); } catch (e) { garde = null; }
REGIONS.forEach(function (r) {
var o = document.createElement("option");
o.value = r[0]; o.textContent = r[1];
choix.appendChild(o);
});
var appliquer = function (cle) {
var r = REGIONS.filter(function (x) { return x[0] === cle; })[0] || REGIONS[4];
LAT = r[2]; LON = r[3]; LIEU = r[1]; choix.value = r[0];
self.dessiner();
};
choix.addEventListener("change", function () {
try { window.localStorage.setItem("ab-region", choix.value); } catch (e) { /* aucun stockage : sans effet */ }
appliquer(choix.value);
});
appliquer(garde || "quebec");
}
this.curseur = this.racine.querySelector("input[type=range]");
this.sortie = this.racine.querySelector("output");
this.jouer = this.racine.querySelector("[data-jouer]");
this.maintenant = this.racine.querySelector("[data-maintenant]");
if (this.curseur) this.curseur.addEventListener("input", function () {
self.arreter(); self.decalage = +self.curseur.value; self.dessiner();
});
if (this.maintenant) this.maintenant.addEventListener("click", function () {
self.arreter(); self.decalage = 0; if (self.curseur) self.curseur.value = 0; self.dessiner();
});
if (this.jouer) this.jouer.addEventListener("click", function () {
if (self.joue) self.arreter(); else self.lancer();
});
};
Planisphere.prototype.choisir = function (i, survol) {
if (survol && this.fige) return;
if (!survol) this.fige = this.choisi === i && this.fige ? false : true;
this.choisi = i;
this.racine.querySelectorAll("[data-membre]").forEach(function (b) {
b.setAttribute("aria-pressed", +b.getAttribute("data-membre") === i ? "true" : "false");
});
this.dessiner();
};
Planisphere.prototype.surligner = function (place) { this.surligne = place; this.dessiner(); };
/* Une nuit entière en 24 secondes, lancée par la personne seulement. */
Planisphere.prototype.lancer = function () {
var self = this, debut = null, depart = this.decalage;
this.joue = true;
if (this.jouer) { this.jouer.setAttribute("aria-pressed", "true"); this.jouer.textContent = "❚❚ Arrêter"; }
function pas(t) {
if (!self.joue) return;
if (debut === null) debut = t;
var minutes = ((t - debut) / 24000) * 1440;
self.decalage = Math.round(depart + minutes) % 1440;
if (self.curseur) self.curseur.value = self.decalage;
self.dessiner();
if (minutes >= 1440) { self.arreter(); return; }
requestAnimationFrame(pas);
}
requestAnimationFrame(pas);
};
Planisphere.prototype.arreter = function () {
this.joue = false;
if (this.jouer) { this.jouer.setAttribute("aria-pressed", "false"); this.jouer.textContent = "▶ Une nuit en 24 s"; }
};
Planisphere.prototype.dimensionner = function () {
var w = this.disque.clientWidth;
if (!w) return;
var dpr = Math.min(window.devicePixelRatio || 1, 2);
this.canvas.width = Math.round(w * dpr);
this.canvas.height = Math.round(w * dpr);
this.ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
this.taille = w;
this.dessiner();
};
Planisphere.prototype.dessiner = function () {
var w = this.taille;
if (!w) return;
var ctx = this.ctx;
var date = new Date(Date.now() + this.decalage * 60000);
var tsl = tempsSideral(date);
var marge = Math.max(36, w * 0.06);
var cx = w / 2, cy = w / 2, R = w / 2 - marge;
var echelle = R / Math.tan(((90 - DEC_MIN) / 2) * RAD);
var k = Math.max(0.75, w / 640); // grossissement des tracés
function proj(ra, dec) {
var r = echelle * Math.tan(((90 - dec) / 2) * RAD);
var a = (ra - tsl) * RAD;
return [cx + r * Math.sin(a), cy - r * Math.cos(a)];
}
function rayonDec(dec) { return echelle * Math.tan(((90 - dec) / 2) * RAD); }
ctx.clearRect(0, 0, w, w);
/* Fond du disque */
var g = ctx.createRadialGradient(cx, cy, 0, cx, cy, R);
g.addColorStop(0, "#141c3c"); g.addColorStop(1, COUL.nuit);
ctx.save();
ctx.beginPath(); ctx.arc(cx, cy, R, 0, Math.PI * 2); ctx.fillStyle = g; ctx.fill();
ctx.clip();
/* Graticule : parallèles tous les 30°, méridiens toutes les 2 h */
ctx.lineWidth = 0.7; ctx.strokeStyle = "rgba(131,137,172,.22)";
[0, 30, 60].forEach(function (d) { ctx.beginPath(); ctx.arc(cx, cy, rayonDec(d), 0, Math.PI * 2); ctx.stroke(); });
for (var h = 0; h < 360; h += 30) {
var p0 = proj(h, 80), p1 = proj(h, DEC_MIN - 5);
ctx.beginPath(); ctx.moveTo(p0[0], p0[1]); ctx.lineTo(p1[0], p1[1]); ctx.stroke();
}
/* Cercle de perpétuelle apparition */
ctx.save();
ctx.setLineDash([3 * k, 5 * k]); ctx.lineWidth = 1.1 * k; ctx.strokeStyle = "rgba(242,200,120,.55)";
ctx.beginPath(); ctx.arc(cx, cy, rayonDec((90 - LAT)), 0, Math.PI * 2); ctx.stroke();
ctx.restore();
/* Lignes des constellations */
ctx.lineWidth = 0.8 * k; ctx.lineCap = "round";
CIEL.constellations.forEach(function (c) {
c.s.forEach(function (seg) {
for (var i = 1; i < seg.length; i++) {
var a = seg[i - 1], b = seg[i];
if (Math.abs(a[0] - b[0]) > 180) continue;
var vis = hauteur((a[0] + b[0]) / 2, (a[1] + b[1]) / 2, tsl) > 0;
ctx.strokeStyle = vis ? "rgba(242,200,120,.30)" : "rgba(131,137,172,.13)";
var pa = proj(a[0], a[1]), pb = proj(b[0], b[1]);
ctx.beginPath(); ctx.moveTo(pa[0], pa[1]); ctx.lineTo(pb[0], pb[1]); ctx.stroke();
}
});
});
/* Étoiles */
CIEL.etoiles.forEach(function (e) {
var p = proj(e[0], e[1]);
var r = Math.max(0.35, (5.9 - e[2]) * 0.42) * k;
var vis = hauteur(e[0], e[1], tsl) > 0;
var c = couleurBV(e[3]);
ctx.fillStyle = "rgba(" + c[0] + "," + c[1] + "," + c[2] + "," + (vis ? Math.min(1, 0.45 + (5.9 - e[2]) * 0.16) : 0.2) + ")";
ctx.beginPath(); ctx.arc(p[0], p[1], r, 0, Math.PI * 2); ctx.fill();
});
/* Horizon du lieu choisi : au-delà, la nuit est sous nos pieds */
var horizon = [];
for (var az = 0; az <= 360; az += 2) {
var A = az * RAD, phi = LAT * RAD;
var sd = Math.cos(phi) * Math.cos(A);
var d = Math.asin(sd);
var cosH = (-Math.sin(phi) * sd) / (Math.cos(phi) * Math.cos(d));
var H = Math.acos(Math.max(-1, Math.min(1, cosH)));
if (Math.sin(A) > 0) H = -H; // à l'est : avant le méridien
horizon.push(proj(tsl - H / RAD, d / RAD));
}
ctx.beginPath();
ctx.rect(0, 0, w, w);
horizon.forEach(function (p, i) { if (i) ctx.lineTo(p[0], p[1]); else ctx.moveTo(p[0], p[1]); });
ctx.closePath();
ctx.fillStyle = "rgba(6,8,18,.58)";
ctx.fill("evenodd");
ctx.beginPath();
horizon.forEach(function (p, i) { if (i) ctx.lineTo(p[0], p[1]); else ctx.moveTo(p[0], p[1]); });
ctx.lineWidth = 1.2 * k; ctx.strokeStyle = "rgba(190,192,212,.55)"; ctx.stroke();
/* Noms des constellations et des astres brillants */
ctx.textAlign = "center"; ctx.textBaseline = "middle";
ctx.font = F_ORNE.replace("{t}", Math.round(11.5 * k));
CIEL.constellations.forEach(function (c) {
if (c.l[1] < DEC_MIN + 6) return;
var p = proj(c.l[0], c.l[1]);
ctx.fillStyle = hauteur(c.l[0], c.l[1], tsl) > 0 ? "rgba(190,192,212,.62)" : "rgba(131,137,172,.3)";
ctx.fillText(c.nom, p[0], p[1]);
});
ctx.font = "300 " + Math.round(10.5 * k) + "px Spectral, Georgia, serif";
ctx.textAlign = "left";
CIEL.brillantes.forEach(function (b) {
var p = proj(b.p[0], b.p[1]);
ctx.fillStyle = hauteur(b.p[0], b.p[1], tsl) > 0 ? "rgba(242,236,221,.75)" : "rgba(131,137,172,.35)";
ctx.fillText(b.n, p[0] + 6 * k, p[1] + 1);
});
/* La place survolée dans le registre des 70 */
if (this.surligne) {
var ps = proj(this.surligne[0], this.surligne[1]);
ctx.strokeStyle = COUL.or; ctx.lineWidth = 1.2 * k;
ctx.beginPath(); ctx.arc(ps[0], ps[1], 10 * k, 0, Math.PI * 2); ctx.stroke();
ctx.font = Math.round(15 * k) + "px " + F_TITRE; ctx.fillStyle = COUL.or; ctx.textAlign = "left";
ctx.fillText(this.surligne[2], ps[0] + 14 * k, ps[1]);
}
/* Le lien entre les sites : une aurore d'une étoile à l'autre */
var pm = MEMBRES.map(function (m) { return proj(m.ra, m.dec); });
if (pm.length > 1) {
var ga = ctx.createLinearGradient(pm[0][0], pm[0][1], pm[1][0], pm[1][1]);
ga.addColorStop(0, COUL.teal); ga.addColorStop(0.5, COUL.cyan); ga.addColorStop(1, COUL.violet);
var mx = (pm[0][0] + pm[1][0]) / 2, my = (pm[0][1] + pm[1][1]) / 2;
var vx = mx - cx, vy = my - cy, n = Math.hypot(vx, vy) || 1;
var bomb = Math.hypot(pm[0][0] - pm[1][0], pm[0][1] - pm[1][1]) * 0.28;
ctx.save();
ctx.shadowColor = "rgba(86,194,255,.8)"; ctx.shadowBlur = 10 * k;
ctx.strokeStyle = ga; ctx.lineWidth = 2.4 * k;
ctx.beginPath(); ctx.moveTo(pm[0][0], pm[0][1]);
ctx.quadraticCurveTo(mx + (vx / n) * bomb, my + (vy / n) * bomb, pm[1][0], pm[1][1]);
ctx.stroke();
ctx.restore();
}
/* Les étoiles des sites */
var self = this;
MEMBRES.forEach(function (m, i) {
var p = pm[i];
var en = self.choisi === i;
var halo = ctx.createRadialGradient(p[0], p[1], 0, p[0], p[1], (en ? 30 : 22) * k);
halo.addColorStop(0, "rgba(242,200,120,.55)"); halo.addColorStop(1, "rgba(242,200,120,0)");
ctx.fillStyle = halo; ctx.beginPath(); ctx.arc(p[0], p[1], (en ? 30 : 22) * k, 0, Math.PI * 2); ctx.fill();
etoileQuatre(ctx, p[0], p[1], (en ? 9 : 7) * k, COUL.astre);
// L'étiquette s'écarte du pôle, vers l'extérieur : le centre reste lisible.
var ux = p[0] - cx, uy = p[1] - cy, un = Math.hypot(ux, uy) || 1;
ux /= un; uy /= un;
var lx = p[0] + ux * 16 * k, ly = p[1] + uy * 16 * k;
ctx.textAlign = ux >= 0 ? "left" : "right";
ctx.font = Math.round(18 * k) + "px " + F_TITRE; ctx.fillStyle = COUL.or;
ctx.fillText(m.etoile, lx, ly - 7 * k);
ctx.font = F_ORNE.replace("{t}", Math.round(12.5 * k)); ctx.fillStyle = COUL.astre2;
ctx.fillText(m.qui, lx, ly + 9 * k);
var b = self.boutons[i];
if (b) { b.style.left = (p[0] / w * 100) + "%"; b.style.top = (p[1] / w * 100) + "%"; }
});
ctx.restore(); // fin de la découpe du disque
/* Couronne : heures d'ascension droite, qui tournent avec le ciel */
ctx.strokeStyle = "rgba(131,137,172,.55)"; ctx.lineWidth = 1;
ctx.beginPath(); ctx.arc(cx, cy, R, 0, Math.PI * 2); ctx.stroke();
ctx.beginPath(); ctx.arc(cx, cy, R + marge * 0.42, 0, Math.PI * 2); ctx.strokeStyle = "rgba(44,53,96,.9)"; ctx.stroke();
for (var m = 0; m < 24 * 6; m++) {
var ang = (m * 2.5 - tsl) * RAD;
var long = m % 6 === 0 ? 0.42 : (m % 3 === 0 ? 0.26 : 0.14);
ctx.beginPath();
ctx.moveTo(cx + R * Math.sin(ang), cy - R * Math.cos(ang));
ctx.lineTo(cx + (R + marge * long) * Math.sin(ang), cy - (R + marge * long) * Math.cos(ang));
ctx.strokeStyle = m % 6 === 0 ? "rgba(242,200,120,.7)" : "rgba(131,137,172,.5)";
ctx.stroke();
}
ctx.font = Math.round(Math.max(9, 10 * k)) + "px " + F_MONO;
ctx.fillStyle = COUL.astre3; ctx.textAlign = "center"; ctx.textBaseline = "middle";
for (var hh = 0; hh < 24; hh += 2) {
var an = (hh * 15 - tsl) * RAD, rr = R + marge * 0.72;
ctx.fillText(hh + " h", cx + rr * Math.sin(an), cy - rr * Math.cos(an));
}
/* Repères fixes : le zénith en haut, le nord en bas */
var pz = [cx, cy - rayonDec(LAT)];
ctx.strokeStyle = "rgba(242,236,221,.55)"; ctx.lineWidth = 1;
ctx.beginPath(); ctx.moveTo(pz[0] - 5, pz[1]); ctx.lineTo(pz[0] + 5, pz[1]); ctx.moveTo(pz[0], pz[1] - 5); ctx.lineTo(pz[0], pz[1] + 5); ctx.stroke();
ctx.font = Math.round(Math.max(9, 9.5 * k)) + "px " + F_MONO; ctx.fillStyle = COUL.astre3;
ctx.fillText("ZÉNITH", pz[0], pz[1] - 11 * k);
var pn = [cx, cy + rayonDec((90 - LAT))];
ctx.fillText("HORIZON NORD", pn[0], pn[1] + 13 * k);
/* Le lieu et son seuil, dans la légende */
if (this.lieu) {
var nf = function (x) { return x.toFixed(1).replace(".", ","); };
this.lieu.textContent = "Vu de " + LIEU + " (" + nf(LAT) + "° N), toute étoile à plus de " +
nf(90 - LAT) + "° de déclinaison ne se couche jamais.";
}
/* L'heure affichée */
if (this.sortie) {
var txt = date.toLocaleString("fr-CA", { timeZone: "America/Toronto", weekday: "short", hour: "2-digit", minute: "2-digit" });
this.sortie.textContent = (this.decalage === 0 ? "maintenant · " : "") + txt;
}
};
function etoileQuatre(ctx, x, y, r, coul) {
var i = r * 0.22;
ctx.save();
ctx.shadowColor = "rgba(242,236,221,.9)"; ctx.shadowBlur = r * 1.4;
ctx.fillStyle = coul;
ctx.beginPath();
ctx.moveTo(x, y - r);
ctx.quadraticCurveTo(x + i, y - i, x + r, y);
ctx.quadraticCurveTo(x + i, y + i, x, y + r);
ctx.quadraticCurveTo(x - i, y + i, x - r, y);
ctx.quadraticCurveTo(x - i, y - i, x, y - r);
ctx.fill();
ctx.restore();
}
/* ---------- Mise en route ---------- */
var instances = [];
document.querySelectorAll("[data-planisphere]").forEach(function (el) { instances.push(new Planisphere(el)); });
/* Le registre des 70 circumpolaires (page La Constellation). */
var registre = document.querySelector("[data-places]");
if (registre && instances.length) {
var prises = {}; MEMBRES.forEach(function (m) { prises[m.etoile] = m.qui; });
var CONST = { UMa: "Gde Ourse", UMi: "Pte Ourse", Cas: "Cassiopée", Cep: "Céphée", Dra: "Dragon", Per: "Persée",
Aur: "Cocher", Cyg: "Cygne", Cam: "Girafe", Lyn: "Lynx", And: "Andromède", Lac: "Lézard", CVn: "Chiens de chasse",
Boo: "Bouvier", Her: "Hercule", Lyr: "Lyre", Tri: "Triangle", LMi: "Pte Lionne" };
CIEL.circumpolaires.forEach(function (c) {
var li = document.createElement("li");
var b = document.createElement("button");
b.type = "button";
var titre = c.n + (c.b ? " · " + c.b + " " + (CONST[c.c] || c.c) : "") + (prises[c.n] ? " · le site de " + prises[c.n] : "");
b.title = titre;
b.setAttribute("aria-label", titre + (c.p ? "" : " (trop faible pour l'œil nu)"));
var n = document.createElement("span"); n.className = "n"; n.textContent = c.n;
var s = document.createElement("span"); s.className = "c"; s.textContent = c.p ? c.p[2].toFixed(1).replace(".", ",") : "—";
b.appendChild(n); b.appendChild(s);
if (prises[c.n]) li.className = "prise";
if (c.p) {
b.setAttribute("data-place", "");
var on = function () { instances[0].surligner([c.p[0], c.p[1], c.n]); };
b.addEventListener("mouseenter", on); b.addEventListener("focus", on);
b.addEventListener("mouseleave", function () { instances[0].surligner(null); });
b.addEventListener("blur", function () { instances[0].surligner(null); });
}
li.appendChild(b); registre.appendChild(li);
});
}
})();