/* Solar Go/No-Go: tabs, roof views (plan, 3D, tables), checks and the "Show me how" tour. */ (function () { 'use strict'; const S = window.SGO, E = window.SGOE; const $ = id => document.getElementById(id); const esc = t => String(t).replace(/[&<>"]/g, c => ({"&":"&","<":"<",">":">",'"':"""}[c])); const DIR = {180: "South", 90: "East (street)", 270: "West (rear)", 0: "North"}; const NAMES = Object.fromEntries(S.designs.map(d => [d.k, d.name])); let design = "E"; /* ---------- tabs ---------- */ const tabs = [...document.querySelectorAll("#tabs button")]; function show(t, push) { if (!$(t)) return; tabs.forEach(b => b.setAttribute("aria-selected", b.dataset.t === t ? "true" : "false")); document.querySelectorAll("section.tab").forEach(s => s.classList.toggle("on", s.id === t)); if (t === "roof") draw(); if (push) { try { history.replaceState(null, "", "#" + t); } catch (e) { /* sandboxed */ } const nav = document.querySelector("nav.tabs"), top = nav.getBoundingClientRect().top + window.scrollY; if (Math.abs(window.scrollY - top) > 2) window.scrollTo(0, top); } } tabs.forEach(b => b.addEventListener("click", () => show(b.dataset.t, true))); document.getElementById("tabs").addEventListener("keydown", e => { const i = tabs.findIndex(b => b.getAttribute("aria-selected") === "true"); if (e.key === "ArrowRight" || e.key === "ArrowLeft") { const n = tabs[(i + (e.key === "ArrowRight" ? 1 : tabs.length - 1)) % tabs.length]; show(n.dataset.t, true); n.focus(); } }); /* ---------- roof: design switch ---------- */ const dseg = $("dseg"); dseg.innerHTML = S.designs.map(d => ``).join(""); dseg.querySelectorAll("button").forEach(b => b.addEventListener("click", () => setDesign(b.dataset.v))); function setDesign(k) { design = k; dseg.querySelectorAll("button").forEach(b => b.setAttribute("aria-pressed", b.dataset.v === k ? "true" : "false")); const d = S.designs.find(x => x.k === k); $("planImg").src = "./plan-" + k + ".svg"; $("planImg").alt = "Roof plan from above, design " + k + " (" + d.name + "), " + d.n + " panels"; $("dnote").innerHTML = `${k} · ${esc(d.name)}: ${d.n} panels, ${d.kw} kW, about ${d.kwh.toLocaleString("en-US")} kWh a year. ${esc(d.rule)}. ${d.street} panels face the street, ${d.angle} are seen from it at an angle.`; $("lsName").textContent = k + " · " + d.name; modTable(); draw(); } function faceTable() { const ks = S.designs.map(d => d.k); let h = "FaceDescriptionFaces" + ks.map(k => `${k}`).join("") + ""; Object.entries(S.faces).forEach(([id, f]) => { h += `${id}${esc(f.desc)}${f.free ? " (kept free)" : ""}${DIR[f.az]}` + S.designs.map(d => `${d.per[id] || 0}`).join("") + ""; }); h += `Total` + S.designs.map(d => `${d.n} · ${d.kw} kW`).join("") + ""; $("faceTable").innerHTML = h; } const HEAD = ["ID","Face","Orient.","Long side °","East m","North m","Height m","Pitch °","Azimuth °","Pitch","Height","Outline","Size mm","W","Street","Shade %"]; function modTable() { const rows = S.csv[design].trim().split("\n").slice(1).map(r => r.split(",")); $("modTable").innerHTML = "" + HEAD.map(h => `${h}`).join("") + "" + rows.map(r => "" + r.map(c => `${esc(c === "" ? "–" : c)}`).join("") + "").join("") + ""; } $("copyCsv").addEventListener("click", async () => { try { await navigator.clipboard.writeText(S.csv[design]); $("copied").textContent = "Copied design " + design + "."; } catch (e) { $("copied").textContent = "Copy was blocked: select the table and copy it instead."; } }); /* ---------- 3D (painter's algorithm on a 2D canvas) ---------- */ const cv = $("cv"), cx = cv.getContext("2d"), B = S.scene.body; let yaw = -0.75, pit = 0.5; const CAMS = {street: [-1.5, 0.32], south: [0.15, 0.42], rear: [1.65, 0.45], top: [0, 1.5]}; document.querySelectorAll("[data-cam]").forEach(b => b.addEventListener("click", () => { [yaw, pit] = CAMS[b.dataset.cam]; draw(); })); const WALL = [236, 232, 224]; function walls(poly, h0, h1, closed) { const o = []; const n = closed ? poly.length : poly.length - 1; for (let i = 0; i < n; i++) { const a = poly[i], b = poly[(i + 1) % poly.length]; o.push({p: [[a[0], a[1], h0], [b[0], b[1], h0], [b[0], b[1], h1], [a[0], a[1], h1]], c: WALL}); } return o; } function build(k) { let P = [{p: B.ground, c: [226, 214, 190], g: 1}, {p: B.street, c: [200, 198, 192], g: 1}]; B.walls.forEach(w => { P = P.concat(walls(w.poly, w.h0, w.h1, w.closed)); }); B.flats.forEach(f => P.push({p: f, c: [150, 132, 116]})); B.tris.forEach(t => P.push({p: t, c: WALL})); S.scene.designs[k].faces.forEach(f => P.push({p: f.pts, c: [112, 94, 82], roof: 1})); S.scene.designs[k].mods.forEach(m => P.push({p: m.c, c: [28, 36, 50], mod: 1})); return P; } function draw() { if (!cv.offsetParent) return; const W = cv.width, H = cv.height; cx.clearRect(0, 0, W, H); const cy_ = Math.cos(yaw), sy = Math.sin(yaw), cp = Math.cos(pit), sp = Math.sin(pit), C = B.centre; const pr = q => { const x = q[0] - C[0], y = q[1] - C[1], z = q[2] - C[2]; const X = x * cy_ - y * sy, Y = x * sy + y * cy_; return [X, Y * sp + z * cp, Y * cp - z * sp]; }; const s = Math.min(W, H) / 26, ox = W / 2, oy = H * 0.56, L = [-0.4, -0.5, 0.77]; const polys = build(design).map(o => { const pp = o.p.map(pr); let dep = pp.reduce((a, b) => a + b[2], 0) / pp.length; if (o.mod) dep -= 0.15; if (o.roof) dep -= 0.02; if (o.g) dep = 1e9; return Object.assign({}, o, {pp, dep}); }); polys.sort((a, b) => b.dep - a.dep); for (const o of polys) { const a = o.p[0], b = o.p[1], c = o.p[2], u = [b[0] - a[0], b[1] - a[1], b[2] - a[2]], v = [c[0] - a[0], c[1] - a[1], c[2] - a[2]]; let n = [u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]]; const nl = Math.hypot(n[0], n[1], n[2]) || 1; n = n.map(x => x / nl); const lam = 0.62 + 0.38 * Math.abs(n[0] * L[0] + n[1] * L[1] + n[2] * L[2]); cx.beginPath(); o.pp.forEach((q, i) => { const X = ox + q[0] * s, Y = oy - q[1] * s; i ? cx.lineTo(X, Y) : cx.moveTo(X, Y); }); cx.closePath(); cx.fillStyle = `rgb(${o.c.map(x => Math.round(x * lam)).join(",")})`; cx.fill(); cx.strokeStyle = o.mod ? "#8d99a8" : "rgba(60,50,40,.35)"; cx.lineWidth = o.mod ? 1 : 0.7; cx.stroke(); } const fs = Math.round(Math.min(40, 15 * W / Math.max(1, cv.clientWidth))); cx.fillStyle = "#4a443a"; cx.font = `600 ${fs}px system-ui,sans-serif`; cx.textAlign = "center"; const st = pr(B.street_label); cx.fillText("Street", ox + st[0] * s, oy - st[1] * s); const nx = B.ground.reduce((a, q) => Math.min(a, q[0]), 1e9) + 1.5, n1 = pr([nx, -4.5, 0]), n2 = pr([nx, -2, 0]); cx.strokeStyle = "#24211c"; cx.lineWidth = Math.max(2, fs / 7); cx.beginPath(); cx.moveTo(ox + n1[0] * s, oy - n1[1] * s); cx.lineTo(ox + n2[0] * s, oy - n2[1] * s); cx.stroke(); cx.fillStyle = "#24211c"; cx.fillText("N", ox + n2[0] * s + (n2[0] >= n1[0] ? 10 : -10), oy - n2[1] * s - fs * 0.4); } let drag = null; cv.addEventListener("pointerdown", e => { drag = [e.clientX, e.clientY, yaw, pit]; try { cv.setPointerCapture(e.pointerId); } catch (x) { /* ok */ } }); cv.addEventListener("pointermove", e => { if (!drag) return; yaw = drag[2] - (e.clientX - drag[0]) * 0.008; pit = Math.max(0.05, Math.min(1.5, drag[3] + (e.clientY - drag[1]) * 0.006)); draw(); }); ["pointerup", "pointercancel", "lostpointercapture"].forEach(t => cv.addEventListener(t, () => { drag = null; })); /* ---------- checks ---------- */ function checks() { const Q = S.qc, ds = S.designs, by = k => ds.find(d => d.k === k); const rows = [ ["Confirmed", `Counts re-derived from each module list: ${ds.map(d => `${d.k} = ${d.n}`).join(", ")}. kW = panels × 0.43. Plans, 3D, tables and money all read the same files.`, ""], ["Confirmed", `Every module rebuilt from its centre, long-side bearing, orientation, pitch and azimuth by separate code: ${Q.overlaps} overlaps, ${Q.edges} edge-clearance misses, ${Q.strips} modules in a pathway strip, smallest gap ${Q.gap.toFixed(2)} m.`, ""], ["Confirmed", `Heights: every module corner sits on its roof plane (worst ${Q.height.toFixed(3)} m off, limit 0.05 m). Rows square to the eave (worst ${Q.square}°, limit 2°).`, ""], ["Confirmed", `Fire access (2021 IRC R324.6): array share of the ${S.roof_m2} m² plan roof: ${ds.map(d => `${d.k} ${d.pct}%`).join(", ")}. D is over 33%, so its ridge setback is 36 in. A, B, C and E keep the street hip and the north slope empty as pathways.`, ""], ["Confirmed", `Shading: spots that lose over 20% of a year's direct sun are left empty (${ds.map(d => `${d.k} ${S.dropped[d.k]}`).join(", ")} dropped). The worst kept panel loses ${Math.max(...ds.map(d => d.shade)).toFixed(0)}%, on the lower rear roof in the morning shadow of the wing.`, ""], ["Issue", `Design D covers ${by("D").vents} roof vents. It only works if a roofer reroutes or combines them under the array.`, "Price vent work in the quote, or use C."], ["Issue", `C (≈ ${by("C").ac} kW AC) and D (≈ ${by("D").ac} kW AC) are over what a 200 A panel with a 200 A main can take (≈ 7.7 kW AC, 120% rule).`, "Supply-side tap, smaller main or panel upgrade; priced at $2,150."], ["Issue", "C, D and E use 6 in at eaves, rakes, hips and valleys. The 2021 IRC asks for 18 in only at ridges plus the 36 in pathways; some reviewers still ask for 18 in along hips and valleys.", "Ask the counter; A keeps 18 in everywhere."], ["Blocker", "Pitch 5:12 and eave heights are assumed for the sample house; on a real house they are measured.", "Measure on site or get the installer's survey."], ["Blocker", "No electrical single-line, conductor sizing or structural attachment design: no city issues a permit without them.", "Installer or a Texas PE supplies them."], ["Can't verify", "The 60-minute peak demand (5 kW summer, 3.5 kW winter) drives the solar demand charge and is assumed.", "Read the real figure from the utility's interval data."] ]; const cls = {"Confirmed": "ok", "Issue": "warn", "Blocker": "bad", "Can't verify": "warn"}; $("qcTable").innerHTML = "ResultEvidenceFix" + rows.map(r => `${r[0]}${esc(r[1])}${esc(r[2] || "–")}`).join("") + ""; $("roofM2").textContent = S.roof_m2; const tot = S.use.reduce((a, b) => a + b, 0); $("useRow").textContent = "Dec–Nov: " + S.use.map(x => x.toLocaleString("en-US")).join(" · ") + " = " + tot.toLocaleString("en-US") + " kWh"; } /* ---------- tour ---------- */ const STEPS = [ {tab: "decision", el: "heroPanel", h: "The answer first", p: "This is the verdict for the cell picked below: Go, Go if you stay, or No-go, with the money behind it. It starts on design E, sized to the house's use, paid in cash."}, {tab: "decision", el: "mxPanel", h: "Every size, every way to pay", p: "Five roof layouts down the side, four ways to pay across the top. Tap any cell to open it in the verdict above. Green pays back while you live there; red never does."}, {tab: "decision", el: "asmPanel", h: "Make it yours", p: "Change the price per watt, how long you will stay, the demand you pull, or add a battery. Every cell, chart and table recalculates. Reset brings back the dated values.", open: true}, {tab: "roof", el: "planPanel", h: "Where the panels go", p: "Pick a design above the plan. Red dashes are setbacks measured on the slope, hatched areas are fire pathways, circles are vents. The 3D view and module list below follow the same pick."}, {tab: "money", el: "soldPanel", h: "Why bigger is not better here", p: "El Paso Electric buys extra power at about 1.6¢ a kWh, while a kWh used at home saves about 13¢. Panels past the house's use mostly sell cheap, so the last panels in C and D earn little."}, {tab: "checks", el: "qcPanel", h: "What a reviewer will ask", p: "Geometry checks run on every module with separate code. The blockers list what a real house still needs before filing: measured pitches and the electrical and structural sheets."} ]; let ti = -1, lit = null; function tourGo(i) { if (lit) lit.classList.remove("tour-lit"); if (i < 0 || i >= STEPS.length) { ti = -1; $("tour").hidden = true; lit = null; return; } ti = i; const st = STEPS[i]; show(st.tab, false); if (st.open) $("asm").open = true; lit = $(st.el); lit.classList.add("tour-lit"); $("tourH").textContent = st.h; $("tourP").textContent = st.p; $("tourN").textContent = `${i + 1} of ${STEPS.length}`; $("tourBack").disabled = i === 0; $("tourNext").textContent = i === STEPS.length - 1 ? "Done" : "Next"; $("tour").hidden = false; const reduce = window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches; lit.scrollIntoView({block: "start", behavior: reduce ? "auto" : "smooth"}); requestAnimationFrame(place); setTimeout(place, 450); } function place() { if (!lit) return; const r = lit.getBoundingClientRect(), t = $("tour"), vh = window.innerHeight; t.classList.remove("up"); const tr = t.getBoundingClientRect(); if (r.top < vh * 0.4 && r.bottom > tr.top) { /* card would cover the lit part: keep it at the bottom only if the top is visible */ } if (r.top > tr.height + 30 && r.top > vh * 0.45) t.classList.add("up"); } $("tourBtn").addEventListener("click", () => tourGo(0)); $("tourNext").addEventListener("click", () => tourGo(ti + 1)); $("tourBack").addEventListener("click", () => tourGo(ti - 1)); $("tourEnd").addEventListener("click", () => tourGo(-1)); document.addEventListener("keydown", e => { if (ti >= 0 && e.key === "Escape") tourGo(-1); }); window.addEventListener("resize", () => { place(); draw(); }); /* ---------- start ---------- */ faceTable(); setDesign("E"); checks(); const h = (location.hash || "").slice(1); if (h && $(h) && $(h).classList.contains("tab")) show(h, false); if (/[?&]tour=1/.test(location.search)) tourGo(0); window.SGOUI = {show, setDesign, tourGo, draw}; })();