// HK Sun Shadow · 香港日照陰影 (anzonwong.com Lab). Plain ES module, no bundler, no build step.
// Place a block anywhere in Hong Kong, pick a day and a time, see the shadow it casts on its neighbours,
// and a whole-day "hours in shadow" map. Teaching sketch only.
//
// Data, all fetched live in the browser (CORS open, no key), the same sources as HK Wind Sketch / HK Site Analysis:
// LandsD Building (CSDI Portal FeatureServer, HK1980 Grid), LandsD 5 m DTM as the HK Site Model 1 km tiles,
// place search = map.gov.hk locationSearch (HK1980 x/y) + Address Lookup Service (ALS).
// Sun: NOAA Solar Calculator equations (sun.js), HKT = UTC+8.
// Shadows: a 2 m height raster (terrain + block prisms); for each sun position one line sweep along the sun's
// bearing gives the "shadow height" over every cell (max over the cells toward the sun of z - distance x tan(elev)).
// A point is in shade when it sits lower than that. One sweep for the existing city, one for the new block alone:
// new shade = shaded by the new block and not already in shade.
import { sunAtHKT, sunTimes, fmtHM } from './sun.js';
const L = window.L, proj4 = window.proj4;
proj4.defs('EPSG:2326', '+proj=tmerc +lat_0=22.31213333333334 +lon_0=114.1785555555556 +k=1 +x_0=836694.05 +y_0=819069.8 +ellps=intl +towgs84=-162.619,-276.959,-161.764,0.067753,-2.243649,-1.158827,-1.094246 +units=m +no_defs');
const toEN = (lat, lon) => proj4('EPSG:4326', 'EPSG:2326', [lon, lat]);
const toLL = (E, N) => { const p = proj4('EPSG:2326', 'EPSG:4326', [E, N]); return [p[1], p[0]]; };
const CSDI = 'https://portal.csdi.gov.hk/server/rest/services/common/';
const BLD_URL = CSDI + 'landsd_rcd_1637211194312_35158/FeatureServer/0/query';
const ALS_URL = 'https://www.als.gov.hk/lookup';
const PLACE_URL = 'https://www.map.gov.hk/gs/api/v1.0.0/locationSearch?q=';
const DTM_URL = /(^|\.)anzonwong\.com$/.test(location.hostname)
? new URL('../hk-site-model/dtm/', import.meta.url).href : 'https://anzonwong.com/lab/hk-site-model/dtm/';
const DTM_CELL = 5, DTM_TILE = 201;
const BOUNDS = { E: [801000, 862500], N: [801000, 847000] };
const FRAME = 400, BUF = 200, RES = 2; // analysis square (m), ring loaded around it for shade casters, raster cell (m)
const SIDE = FRAME + 2 * BUF, NR = SIDE / RES; // raster covers SIDE x SIDE
const NF = FRAME / RES, F0 = BUF / RES; // analysis cells per side, first analysis cell index
const MROT = Math.ceil(SIDE * Math.SQRT2 / RES) + 2;
const STEP_MIN = 10, MIN_ELEV = 5, TOL = 0.3, WALL_OFF = 2.5;
const YEAR = 2026;
const PRESETS = { winter: [12, 21], equinox: [3, 20], summer: [6, 21] };
const COL = { paper: '#f7f4ee', ground: '#fbfaf6', ink: '#1d1a16', rule: '#c9c1b3', accent: '#c2410c', exist: [58, 74, 99], new: [194, 65, 12] };
const HCLASS = [[10, '#ebe6dc'], [30, '#dcd4c4'], [60, '#c9bda7'], [100, '#b2a289'], [150, '#968469'], [Infinity, '#7a6a52']];
const HOURS_STEPS = [0.5, 1, 2, 3, 4, 6];
const CREDITS = 'Data: Lands Department, The Government of the Hong Kong SAR: Building (CSDI Portal, live) and 5 m Digital Terrain Model (DATA.GOV.HK). Intellectual property rights in the data belong to the Government of the HKSAR. Place search: map.gov.hk and Address Lookup Service (ALS). Sun: NOAA Solar Calculator equations. Base map © Lands Department. Personal hobby project, not official, for teaching only. 資料:香港特別行政區政府地政總署。個人興趣項目,只供教學參考。';
const $ = (id) => document.getElementById(id);
const S = {
site: null, // { E0, N0, lat, lon, label, blds, terr, surfB, ... }
shape: 'rect', block: { x: 0, y: 0, w: 30, d: 20, rot: 0, h: 100, ring: null, drawn: null },
date: [12, 21], tmin: 720, layer: 'now', hk: 'new', tab: 'map',
now: null, day: null, dirtyDay: true, playing: false,
};
window.HSS = { S, get view() { return view; } };
// ---------------------------------------------------------------- small helpers
function status(t, cls) { const e = $('hssStatus'); e.textContent = t || ''; e.className = 'hss-status' + (cls ? ' ' + cls : ''); }
function busy(t) { const e = $('hssBusy'); e.textContent = t || ''; e.hidden = !t; }
const tick = () => new Promise((r) => setTimeout(r, 0));
function pip(x, y, ring) {
let ins = false;
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
const [xi, yi] = ring[i], [xj, yj] = ring[j];
if ((yi > y) !== (yj > y) && x < (xj - xi) * (y - yi) / (yj - yi) + xi) ins = !ins;
}
return ins;
}
function area(ring) { let a = 0; for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) a += ring[j][0] * ring[i][1] - ring[i][0] * ring[j][1]; return a / 2; } // > 0 = anticlockwise
const cellOf = (x, y) => { const i = Math.floor((x + SIDE / 2) / RES), j = Math.floor((y + SIDE / 2) / RES); return i >= 0 && j >= 0 && i < NR && j < NR ? j * NR + i : -1; };
const cx = (i) => -SIDE / 2 + (i + 0.5) * RES;
function cellsInRing(ring) {
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
for (const [x, y] of ring) { x0 = Math.min(x0, x); y0 = Math.min(y0, y); x1 = Math.max(x1, x); y1 = Math.max(y1, y); }
const i0 = Math.max(0, Math.floor((x0 + SIDE / 2) / RES)), i1 = Math.min(NR - 1, Math.floor((x1 + SIDE / 2) / RES));
const j0 = Math.max(0, Math.floor((y0 + SIDE / 2) / RES)), j1 = Math.min(NR - 1, Math.floor((y1 + SIDE / 2) / RES));
const out = [];
for (let j = j0; j <= j1; j++) for (let i = i0; i <= i1; i++) if (pip(cx(i), cx(j), ring)) out.push(j * NR + i);
return out;
}
function hm(min) { return fmtHM(min); }
function fmtH(h) { return h >= 1 ? h.toFixed(1) + ' h' : Math.round(h * 60) + ' min'; }
// ---------------------------------------------------------------- data
async function fetchJSON(url, opt, tries = 2) {
for (let t = 1; ; t++) {
const ctl = new AbortController(), timer = setTimeout(() => ctl.abort(), 45000);
try {
const r = await fetch(url, Object.assign({ signal: ctl.signal }, opt || {}));
if (!r.ok) throw new Error('HTTP ' + r.status);
const j = await r.json();
if (j && j.error) throw new Error(j.error.message || 'service error');
return j;
} catch (e) { if (t >= tries) throw e; } finally { clearTimeout(timer); }
}
}
async function queryBuildings(E0, N0, E1, N1, onPage) {
const out = [], n = 2000;
for (let off = 0; ; off += n) {
const q = new URLSearchParams({
where: '1=1', geometry: `${E0},${N0},${E1},${N1}`, geometryType: 'esriGeometryEnvelope', inSR: '2326',
spatialRel: 'esriSpatialRelIntersects', outFields: 'OBJECTID,BuildingBlockType,Status,BaseHeight,TopHeight,Storeys', outSR: '2326',
returnGeometry: 'true', orderByFields: 'OBJECTID', resultOffset: String(off), resultRecordCount: String(n), f: 'geojson',
});
const j = await fetchJSON(BLD_URL + '?' + q);
const fs = j.features || [];
out.push(...fs); if (onPage) onPage(out.length);
const more = (j.properties && j.properties.exceededTransferLimit) || j.exceededTransferLimit;
if (!fs.length || (fs.length < n && !more)) break;
}
return out;
}
let dtmIndex = null;
const tileCache = new Map();
async function loadTile(key) {
if (tileCache.has(key)) return tileCache.get(key);
const p = (async () => {
if (!dtmIndex.has(key)) return null; // no tile = sea, 0 mPD
const r = await fetch(DTM_URL + key + '.bin');
if (!r.ok) throw new Error('DTM tile ' + key + ' ' + r.status);
const buf = await new Response(r.body.pipeThrough(new DecompressionStream('deflate'))).arrayBuffer();
const flag = new Uint8Array(buf, 0, 1)[0], a = new Int16Array(buf.slice(1));
const out = new Float32Array(DTM_TILE * DTM_TILE);
for (let j = 0; j < DTM_TILE; j++) {
let acc = 0;
for (let i = 0; i < DTM_TILE; i++) { const k = j * DTM_TILE + i; acc = flag || i === 0 ? a[k] : acc + a[k]; out[k] = acc === -32768 ? 0 : acc / 10; }
}
return out;
})();
tileCache.set(key, p); p.catch(() => tileCache.delete(key));
return p;
}
async function loadDEM(xa, ya, xb, yb) { // HK1980 Grid; as HK Site Analysis
xa = Math.floor(xa / DTM_CELL) * DTM_CELL; ya = Math.floor(ya / DTM_CELL) * DTM_CELL; xb = Math.ceil(xb / DTM_CELL) * DTM_CELL; yb = Math.ceil(yb / DTM_CELL) * DTM_CELL;
if (!dtmIndex) dtmIndex = new Set((await fetchJSON(DTM_URL + 'index.json')).tiles);
const nx = Math.round((xb - xa) / DTM_CELL) + 1, ny = Math.round((yb - ya) / DTM_CELL) + 1;
const z = new Float32Array(nx * ny), keys = [];
for (let ek = Math.floor(xa / 1000); ek * 1000 <= xb; ek++) for (let nk = Math.floor(ya / 1000); nk * 1000 <= yb; nk++) keys.push([ek, nk]);
const tiles = await Promise.all(keys.map(([ek, nk]) => loadTile(`${ek}_${nk}`)));
keys.forEach(([ek, nk], t) => {
const tl = tiles[t]; if (!tl) return;
const tx = ek * 1000, ty = nk * 1000;
const i0 = Math.max(0, Math.round((tx - xa) / DTM_CELL)), i1 = Math.min(nx - 1, Math.round((tx + 1000 - xa) / DTM_CELL));
const j0 = Math.max(0, Math.round((ty - ya) / DTM_CELL)), j1 = Math.min(ny - 1, Math.round((ty + 1000 - ya) / DTM_CELL));
for (let j = j0; j <= j1; j++) { const tj = Math.round((ya + j * DTM_CELL - ty) / DTM_CELL); for (let i = i0; i <= i1; i++) z[j * nx + i] = tl[tj * DTM_TILE + Math.round((xa + i * DTM_CELL - tx) / DTM_CELL)]; }
});
const h = (E, N) => {
let fx = (E - xa) / DTM_CELL, fy = (N - ya) / DTM_CELL;
fx = Math.min(nx - 1.0001, Math.max(0, fx)); fy = Math.min(ny - 1.0001, Math.max(0, fy));
const i = Math.floor(fx), j = Math.floor(fy), u = fx - i, v = fy - j, k = j * nx + i;
return (z[k] * (1 - u) + z[k + 1] * u) * (1 - v) + (z[k + nx] * (1 - u) + z[k + nx + 1] * u) * v;
};
return { xa, ya, nx, ny, z, h };
}
function trueNorthGrid(E, N) { // grid bearing of true north (deg), HK1980 is a TM: tiny
const [lat, lon] = toLL(E, N), a = toEN(lat - 0.002, lon), b = toEN(lat + 0.002, lon);
return Math.atan2(b[0] - a[0], b[1] - a[1]) * 180 / Math.PI;
}
async function loadSite(lat, lon, label) {
const [E0, N0] = toEN(lat, lon);
if (E0 < BOUNDS.E[0] || E0 > BOUNDS.E[1] || N0 < BOUNDS.N[0] || N0 > BOUNDS.N[1]) { status('That point is outside Hong Kong. 請選擇香港境內的地點。', 'err'); return; }
S.playing = false; syncPlay();
busy('Loading buildings and terrain… 載入建築物及地形…');
status('Loading buildings and terrain… 載入中…');
try {
const h = SIDE / 2;
const [feats, dem] = await Promise.all([
queryBuildings(E0 - h, N0 - h, E0 + h, N0 + h, (k) => busy(`Loading buildings… ${k} 載入建築物…`)),
loadDEM(E0 - h - 10, N0 - h - 10, E0 + h + 10, N0 + h + 10),
]);
busy('Preparing the model… 準備模型…'); await tick();
const site = buildSite(E0, N0, lat, lon, label, feats, dem);
S.site = site;
S.block.x = 0; S.block.y = 0; S.block.drawn = null; S.shape = 'rect'; syncShapeButtons();
applyURLBlock();
rebuildBlock();
document.querySelectorAll('.needs-site').forEach((e) => e.classList.remove('locked'));
status(`Loaded ${site.blds.length} blocks around ${label || 'the picked point'}; ground ${site.gmin.toFixed(1)} to ${site.gmax.toFixed(1)} mPD. 已載入 ${site.blds.length} 座建築。`);
setTab('plan');
fitPlan();
if (view) view.setSite(site, S);
update();
} catch (e) {
console.error(e);
status(`Could not load the data (${e.message}). The government service may be busy; try again in a minute. 未能載入資料,請稍後再試。`, 'err');
} finally { busy(''); }
}
function buildSite(E0, N0, lat, lon, label, feats, dem) {
// terrain raster
const terr = new Float32Array(NR * NR);
let gmin = Infinity, gmax = -Infinity;
for (let j = 0; j < NR; j++) for (let i = 0; i < NR; i++) {
const z = dem.h(E0 + cx(i), N0 + cx(j)); terr[j * NR + i] = z;
if (i >= F0 && i < F0 + NF && j >= F0 && j < F0 + NF) { gmin = Math.min(gmin, z); gmax = Math.max(gmax, z); }
}
const blds = [], seen = new Set();
for (const f of feats) {
const p = f.properties || {}, g = f.geometry || {};
if (p.Status != null && String(p.Status).toLowerCase() !== 'active' && p.Status !== 1) continue;
if (p.TopHeight == null) continue;
if (seen.has(p.OBJECTID)) continue; seen.add(p.OBJECTID);
const polys = g.type === 'Polygon' ? [g.coordinates] : g.type === 'MultiPolygon' ? g.coordinates : [];
for (const poly of polys) {
if (!poly || !poly[0] || poly[0].length < 4) continue;
const ring = poly[0].slice(0, -1).map(([E, N]) => [E - E0, N - N0]);
const cells = cellsInRing(ring);
if (!cells.length) continue;
const top = +p.TopHeight;
let gz = 0; for (const k of cells) gz += terr[k]; gz /= cells.length;
const base = p.BaseHeight != null ? Math.min(+p.BaseHeight, gz) : gz;
if (!(top > base + 1)) continue;
let ax = 0, ay = 0; for (const [x, y] of ring) { ax += x; ay += y; } ax /= ring.length; ay /= ring.length;
blds.push({ id: blds.length, oid: p.OBJECTID, ring, cells: Int32Array.from(cells), top, base, h: top - base, cx: ax, cy: ay, type: p.BuildingBlockType || '' });
}
}
const surfB = new Float32Array(NR * NR);
const site = { E0, N0, lat, lon, label, blds, terr, gmin, gmax, dem, surfB, conv: trueNorthGrid(E0, N0), cleared: new Set(), wallPts: null };
return site;
}
function rebuildSurfaceB(site) {
const s = site.surfB; s.set(site.terr);
for (const b of site.blds) { if (site.cleared.has(b.id)) continue; for (const k of b.cells) if (b.top > s[k]) s[k] = b.top; }
}
// ---------------------------------------------------------------- the proposed block
function rectRing(b) {
const c = Math.cos(b.rot * Math.PI / 180), s = Math.sin(b.rot * Math.PI / 180), hw = b.w / 2, hd = b.d / 2;
return [[-hw, -hd], [hw, -hd], [hw, hd], [-hw, hd]].map(([u, v]) => [b.x + u * c + v * s, b.y - u * s + v * c]);
}
function rebuildBlock() {
const site = S.site; if (!site) return;
const b = S.block;
b.ring = S.shape === 'draw' && b.drawn ? b.drawn.map(([x, y]) => [x + b.x, y + b.y]) : rectRing(b);
b.cells = Int32Array.from(cellsInRing(b.ring));
let gz = Infinity; for (const k of b.cells) gz = Math.min(gz, site.terr[k]);
b.base = isFinite(gz) ? gz : 0; b.top = b.base + b.h;
// existing blocks mostly under the new footprint are taken as cleared
const mine = new Set(b.cells);
site.cleared = new Set();
for (const e of site.blds) {
let n = 0; for (const k of e.cells) if (mine.has(k)) n++;
if (n / e.cells.length > 0.3 || pip(e.cx, e.cy, b.ring)) site.cleared.add(e.id);
}
rebuildSurfaceB(site);
site.wallPts = buildWallPoints(site);
const fa = Math.abs(area(b.ring));
$('hssBlockFacts').textContent = `Footprint ${Math.round(fa)} m², ground ${b.base.toFixed(1)} mPD, top ${b.top.toFixed(1)} mPD` +
(site.cleared.size ? `, ${site.cleared.size} existing block(s) under it cleared` : '') + `. 佔地 ${Math.round(fa)} 平方米。`;
S.dirtyDay = true;
}
// sample points 2.5 m in front of every neighbour wall, at several heights
function buildWallPoints(site) {
const xs = [], ys = [], zs = [], nxs = [], nys = [], bi = [];
const lim = FRAME / 2 + 40;
for (const b of site.blds) {
if (site.cleared.has(b.id)) continue;
if (Math.abs(b.cx) > lim || Math.abs(b.cy) > lim) continue;
const ring = b.ring, ccw = area(ring) > 0;
const levels = [];
const n = Math.min(10, Math.max(1, Math.floor((b.h - 2) / 6) + 1));
for (let k = 0; k < n; k++) levels.push(b.base + 2 + (n === 1 ? 0 : k * (b.h - 4) / (n - 1)));
for (let i = 0; i < ring.length; i++) {
const [x1, y1] = ring[i], [x2, y2] = ring[(i + 1) % ring.length];
const len = Math.hypot(x2 - x1, y2 - y1); if (len < 2) continue;
const tx = (x2 - x1) / len, ty = (y2 - y1) / len;
const nx = ccw ? ty : -ty, ny = ccw ? -tx : tx; // outward normal
const m = Math.max(1, Math.round(len / 4));
for (let s = 0; s < m; s++) {
const t = (s + 0.5) / m, px = x1 + (x2 - x1) * t + nx * WALL_OFF, py = y1 + (y2 - y1) * t + ny * WALL_OFF;
const k = cellOf(px, py); if (k < 0) continue;
for (const z of levels) {
if (site.surfB[k] > z) continue; // inside or under another block
xs.push(px); ys.push(py); zs.push(z); nxs.push(nx); nys.push(ny); bi.push(b.id);
}
}
}
}
return { x: Float32Array.from(xs), y: Float32Array.from(ys), z: Float32Array.from(zs), nx: Float32Array.from(nxs), ny: Float32Array.from(nys), b: Int32Array.from(bi), n: xs.length };
}
// ---------------------------------------------------------------- shadow sweep
const SH_E = new Float32Array(MROT * MROT), SH_P = new Float32Array(MROT * MROT);
let propSurf = null;
function propRaster() {
if (!propSurf) propSurf = new Float32Array(NR * NR);
propSurf.fill(-1e9);
const b = S.block; for (const k of b.cells) propSurf[k] = b.top;
return propSurf;
}
// shadow-height field for one sun position. azG = grid bearing toward the sun (deg), tanE = tan(elevation)
function sweep(surf, azG, tanE, out) {
const a = azG * Math.PI / 180, sx = -Math.sin(a), sy = -Math.cos(a); // shadow direction (away from the sun)
const px = -sy, py = sx, half = MROT / 2 - 0.5, drop = RES * tanE;
for (let r = 0; r < MROT; r++) {
const v = (r - half) * RES;
let h = -1e9, row = r * MROT;
let x = -half * RES * sx + v * px, y = -half * RES * sy + v * py;
const dx = RES * sx, dy = RES * sy;
for (let c = 0; c < MROT; c++, x += dx, y += dy) {
out[row + c] = h;
const i = Math.floor((x + SIDE / 2) / RES), j = Math.floor((y + SIDE / 2) / RES);
if (i >= 0 && j >= 0 && i < NR && j < NR) { const z = surf[j * NR + i]; if (z > h) h = z; }
h -= drop;
}
}
return { sx, sy, px, py, half };
}
function shAt(out, g, x, y) {
const c = Math.round((x * g.sx + y * g.sy) / RES + g.half), r = Math.round((x * g.px + y * g.py) / RES + g.half);
return c >= 0 && r >= 0 && c < MROT && r < MROT ? out[r * MROT + c] : -1e9;
}
function sunAt(tmin) {
const site = S.site, [m, d] = S.date;
const p = sunAtHKT(YEAR, m, d, tmin, site.lat, site.lon);
return Object.assign(p, { azG: p.az + site.conv });
}
// shade flags over the analysis square for one time: 0 sun, 1 existing shade, 2 new shade from the block, 3 block roof, 4 night
function shadeOnce(sun, flags, wallHit) {
const site = S.site, b = S.block, sb = site.surfB;
if (sun.elev <= 0.2) { flags.fill(4); if (wallHit) wallHit.fill(0); return; }
const tanE = Math.tan(sun.elev * Math.PI / 180);
const ge = sweep(sb, sun.azG, tanE, SH_E), gp = sweep(propRaster(), sun.azG, tanE, SH_P);
const ps = propSurf;
for (let j = 0; j < NF; j++) {
const y = cx(F0 + j);
for (let i = 0; i < NF; i++) {
const k = (F0 + j) * NR + F0 + i, x = cx(F0 + i), q = j * NF + i;
if (ps[k] > -1e8) { flags[q] = shAt(SH_E, ge, x, y) > ps[k] + TOL ? 1 : 3; continue; }
const z = sb[k];
const e = shAt(SH_E, ge, x, y) > z + TOL;
flags[q] = e ? 1 : shAt(SH_P, gp, x, y) > z + TOL ? 2 : 0;
}
}
if (wallHit) {
const W = site.wallPts, a = sun.azG * Math.PI / 180, tx = Math.sin(a), ty = Math.cos(a);
for (let n = 0; n < W.n; n++) {
wallHit[n] = 0;
if (W.nx[n] * tx + W.ny[n] * ty < 0.1) continue; // wall faces away from the sun: no sun to lose
const x = W.x[n], y = W.y[n], z = W.z[n];
if (shAt(SH_P, gp, x, y) > z + TOL && !(shAt(SH_E, ge, x, y) > z + TOL)) wallHit[n] = 1;
}
}
}
function computeNow() {
if (!S.site) return;
const sun = sunAt(S.tmin);
const flags = new Uint8Array(NF * NF), wall = new Uint8Array(S.site.wallPts.n);
shadeOnce(sun, flags, wall);
S.now = { sun, flags, wall };
}
async function computeDay() {
if (!S.site) return;
const [m, d] = S.date, site = S.site;
const t = sunTimes(YEAR, m, d, site.lat, site.lon);
const hNew = new Float32Array(NF * NF), hAll = new Float32Array(NF * NF);
const W = site.wallPts, wMin = new Float32Array(W.n);
const flags = new Uint8Array(NF * NF), wall = new Uint8Array(W.n);
const nb = new Map(); // building id -> { steps:Set }
let counted = 0, first = null, last = null;
const t0 = performance.now();
for (let tm = Math.ceil(t.sunrise / STEP_MIN) * STEP_MIN; tm <= t.sunset; tm += STEP_MIN) {
const sun = sunAt(tm); if (sun.elev < MIN_ELEV) continue;
if (first == null) first = tm; last = tm;
counted++;
shadeOnce(sun, flags, wall);
const dt = STEP_MIN / 60;
for (let q = 0; q < flags.length; q++) { const f = flags[q]; if (f === 2) { hNew[q] += dt; hAll[q] += dt; } else if (f === 1) hAll[q] += dt; }
for (let n = 0; n < W.n; n++) if (wall[n]) {
wMin[n] += STEP_MIN;
const id = W.b[n]; let r = nb.get(id); if (!r) nb.set(id, r = { steps: new Set(), pts: 0 }); r.steps.add(tm);
}
}
const rows = [];
for (const [id, r] of nb) {
const bl = site.blds[id];
let mx = 0; for (let n = 0; n < W.n; n++) if (W.b[n] === id && wMin[n] > mx) mx = wMin[n];
const steps = [...r.steps].sort((a, b) => a - b), segs = [];
for (const s of steps) { const g = segs[segs.length - 1]; if (g && s - g[1] <= STEP_MIN) g[1] = s; else segs.push([s, s]); }
rows.push({ id, b: bl, maxH: mx / 60, segs, dist: Math.hypot(bl.cx - S.block.x, bl.cy - S.block.y) });
}
rows.sort((a, b) => b.maxH - a.maxH || a.dist - b.dist);
rows.forEach((r, i) => { r.label = 'N' + (i + 1); });
// the 2 m raster puts a one-cell strip of false shade along the block's own walls: leave cells within 4 m out of the maximum
const near = new Uint8Array(NF * NF);
for (const k of S.block.cells) {
const i = (k % NR) - F0, j = Math.floor(k / NR) - F0;
for (let dj = -2; dj <= 2; dj++) for (let di = -2; di <= 2; di++) { const a = i + di, c = j + dj; if (a >= 0 && c >= 0 && a < NF && c < NF) near[c * NF + a] = 1; }
}
let maxNew = 0; for (let q = 0; q < hNew.length; q++) if (!near[q] && hNew[q] > maxNew) maxNew = hNew[q];
S.day = { hNew, hAll, wMin, rows, counted, hours: counted * STEP_MIN / 60, first, last, times: t, maxNew, ms: performance.now() - t0 };
S.dirtyDay = false;
renderNeighbours();
}
// ---------------------------------------------------------------- plan view
const cv = $('hssPlan'), ctx = cv.getContext('2d');
const P = { cx: 0, cy: 0, s: 2 }; // view centre (m) and pixels per metre (CSS px)
let dpr = 1;
function sizeCanvas() {
const r = cv.getBoundingClientRect(); dpr = Math.min(2, window.devicePixelRatio || 1);
cv.width = Math.max(10, Math.round(r.width * dpr)); cv.height = Math.max(10, Math.round(r.height * dpr));
}
function fitPlan() { sizeCanvas(); const r = cv.getBoundingClientRect(); P.cx = 0; P.cy = 0; P.s = Math.min(r.width, r.height) / (FRAME + 30); drawPlan(); }
const W2S = (x, y) => { const r = cv.getBoundingClientRect(); return [r.width / 2 + (x - P.cx) * P.s, r.height / 2 - (y - P.cy) * P.s]; };
const S2W = (sx, sy) => { const r = cv.getBoundingClientRect(); return [P.cx + (sx - r.width / 2) / P.s, P.cy - (sy - r.height / 2) / P.s]; };
const off = document.createElement('canvas'); off.width = NF; off.height = NF;
const offCtx = off.getContext('2d');
function hoursColour(h) { // sequential, light to deep
const ramp = [[253, 224, 197], [251, 176, 120], [240, 120, 60], [204, 72, 20], [150, 40, 10], [90, 20, 8]];
let k = 0; while (k < HOURS_STEPS.length - 1 && h >= HOURS_STEPS[k + 1]) k++;
return ramp[k];
}
function paintLayer() {
const img = offCtx.createImageData(NF, NF), D = img.data;
const put = (q, c, a) => { const j = NF - 1 - Math.floor(q / NF), i = q % NF, o = (j * NF + i) * 4; D[o] = c[0]; D[o + 1] = c[1]; D[o + 2] = c[2]; D[o + 3] = a; };
if (S.layer === 'now' && S.now) {
const f = S.now.flags;
for (let q = 0; q < f.length; q++) {
if (f[q] === 1) put(q, COL.exist, 74); else if (f[q] === 2) put(q, COL.new, 165); else if (f[q] === 4) put(q, [20, 24, 40], 150);
}
} else if (S.layer === 'hours' && S.day) {
const H = S.hk === 'new' ? S.day.hNew : S.day.hAll;
for (let q = 0; q < H.length; q++) { const h = H[q]; if (h >= HOURS_STEPS[0] - 1e-6) put(q, S.hk === 'new' ? hoursColour(h) : greyColour(h), S.hk === 'new' ? 200 : 170); }
}
offCtx.putImageData(img, 0, 0);
}
function greyColour(h) { const t = Math.min(1, h / Math.max(1, S.day ? S.day.hours : 10)); const v = Math.round(225 - 170 * t); return [v - 10, v - 4, v + 12]; }
function path(ring) { ctx.beginPath(); ring.forEach(([x, y], i) => { const [a, b] = W2S(x, y); if (i) ctx.lineTo(a, b); else ctx.moveTo(a, b); }); ctx.closePath(); }
function drawPlan() {
if (!cv.width) return;
const r = cv.getBoundingClientRect(), Wd = r.width, Ht = r.height;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.fillStyle = COL.paper; ctx.fillRect(0, 0, Wd, Ht);
const site = S.site; if (!site) return;
// ground inside the square
const [qx0, qy0] = W2S(-FRAME / 2, FRAME / 2);
ctx.fillStyle = COL.ground; ctx.fillRect(qx0, qy0, FRAME * P.s, FRAME * P.s);
// blocks (roof tone by height)
for (const b of site.blds) {
if (site.cleared.has(b.id)) continue;
let c = HCLASS[0][1]; for (const [mx, col] of HCLASS) { if (b.h < mx) { c = col; break; } }
path(b.ring); ctx.fillStyle = c; ctx.fill();
}
// shade layer over ground and roofs
paintLayer();
ctx.imageSmoothingEnabled = false;
ctx.drawImage(off, qx0, qy0, FRAME * P.s, FRAME * P.s);
// outlines
ctx.lineWidth = 0.8; ctx.strokeStyle = 'rgba(40,34,28,.55)';
for (const b of site.blds) { if (site.cleared.has(b.id)) continue; path(b.ring); ctx.stroke(); }
ctx.setLineDash([4, 3]); ctx.strokeStyle = 'rgba(120,110,95,.8)';
for (const id of site.cleared) { path(site.blds[id].ring); ctx.stroke(); }
ctx.setLineDash([]);
// the block
const bk = S.block;
path(bk.ring); ctx.fillStyle = 'rgba(194,65,12,.88)'; ctx.fill(); ctx.lineWidth = 2; ctx.strokeStyle = '#7c2d12'; ctx.stroke();
// walls newly shaded at this time
if (S.layer === 'now' && S.now && S.now.wall) {
const W = site.wallPts; ctx.fillStyle = '#b91c1c';
const seen = new Set();
for (let n = 0; n < W.n; n++) if (S.now.wall[n]) {
const key = Math.round(W.x[n]) + ',' + Math.round(W.y[n]); if (seen.has(key)) continue; seen.add(key);
const [a, b2] = W2S(W.x[n] - W.nx[n] * WALL_OFF, W.y[n] - W.ny[n] * WALL_OFF);
ctx.beginPath(); ctx.arc(a, b2, 2.6, 0, 7); ctx.fill();
}
}
// neighbour labels
if (S.day && !S.dirtyDay) {
ctx.font = '700 11px Arial, sans-serif'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
for (const row of S.day.rows.slice(0, 9)) {
const [a, b2] = W2S(row.b.cx, row.b.cy);
ctx.fillStyle = '#fff'; ctx.strokeStyle = '#b91c1c'; ctx.lineWidth = 1.5;
ctx.beginPath(); ctx.roundRect(a - 13, b2 - 8, 26, 16, 8); ctx.fill(); ctx.stroke();
ctx.fillStyle = '#b91c1c'; ctx.fillText(row.label, a, b2 + 0.5);
}
}
// block label
{
const [a, b2] = W2S(bk.x, bk.y);
ctx.font = '700 12px Arial, sans-serif'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
const t = `${Math.round(bk.h)} m`; const w = ctx.measureText(t).width + 10;
ctx.fillStyle = 'rgba(255,255,255,.92)'; ctx.fillRect(a - w / 2, b2 - 9, w, 18); ctx.fillStyle = '#7c2d12'; ctx.fillText(t, a, b2);
}
// draw-in-progress
if (drawPts.length) {
ctx.strokeStyle = COL.accent; ctx.lineWidth = 2; ctx.beginPath();
drawPts.forEach(([x, y], i) => { const [a, b2] = W2S(x, y); if (i) ctx.lineTo(a, b2); else ctx.moveTo(a, b2); });
if (hoverPt) { const [a, b2] = W2S(...hoverPt); ctx.lineTo(a, b2); }
ctx.stroke();
for (const [x, y] of drawPts) { const [a, b2] = W2S(x, y); ctx.fillStyle = '#fff'; ctx.beginPath(); ctx.arc(a, b2, 4, 0, 7); ctx.fill(); ctx.stroke(); }
}
// analysis square
ctx.strokeStyle = 'rgba(29,26,22,.45)'; ctx.setLineDash([6, 4]); ctx.lineWidth = 1; ctx.strokeRect(qx0, qy0, FRAME * P.s, FRAME * P.s); ctx.setLineDash([]);
drawFurniture(Wd, Ht);
}
function drawFurniture(Wd, Ht) {
const site = S.site;
// north arrow (grid north up; true north differs by site.conv, a few hundredths of a degree)
const nx = Wd - 34, ny = 44;
ctx.fillStyle = COL.ink; ctx.beginPath(); ctx.moveTo(nx, ny - 20); ctx.lineTo(nx + 8, ny + 6); ctx.lineTo(nx, ny); ctx.lineTo(nx - 8, ny + 6); ctx.closePath(); ctx.fill();
ctx.font = '700 12px Arial'; ctx.textAlign = 'center'; ctx.fillText('N', nx, ny + 18);
// sun compass
if (S.layer === 'now' && S.now) {
const sun = S.now.sun, R = 34, ox = Wd - 34 - 70, oy = 50;
ctx.strokeStyle = 'rgba(29,26,22,.35)'; ctx.lineWidth = 1; ctx.beginPath(); ctx.arc(ox, oy, R, 0, 7); ctx.stroke();
if (sun.elev > 0) {
const a = sun.azG * Math.PI / 180, rr = R * (1 - sun.elev / 90);
const sx = ox + Math.sin(a) * rr, sy = oy - Math.cos(a) * rr;
ctx.strokeStyle = 'rgba(58,74,99,.8)'; ctx.lineWidth = 2; ctx.beginPath(); ctx.moveTo(ox, oy); ctx.lineTo(ox - Math.sin(a) * R, oy + Math.cos(a) * R); ctx.stroke();
ctx.fillStyle = '#e9a400'; ctx.beginPath(); ctx.arc(sx, sy, 7, 0, 7); ctx.fill(); ctx.strokeStyle = '#8a5a00'; ctx.lineWidth = 1; ctx.stroke();
}
ctx.fillStyle = COL.ink; ctx.font = '11px Arial'; ctx.fillText('Sun 太陽', ox, oy + R + 13);
}
// scale bar
const len = P.s * 100 > 160 ? 50 : 100, bx = 18, by = Ht - 22;
ctx.fillStyle = COL.ink; ctx.fillRect(bx, by, len * P.s, 4);
ctx.fillStyle = '#fff'; ctx.fillRect(bx + len * P.s / 2, by + 1, len * P.s / 2 - 1, 2);
ctx.fillStyle = COL.ink; ctx.font = '11px Arial'; ctx.textAlign = 'left'; ctx.fillText(`${len} m`, bx + len * P.s + 6, by + 5);
// time stamp
ctx.textAlign = 'left'; ctx.font = '700 13px Arial';
const [m, d] = S.date, dd = `${d} ${['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][m - 1]} ${YEAR}`;
const txt = S.layer === 'now' ? `${dd} · ${hm(S.tmin)} HKT` : `${dd} · ${S.hk === 'new' ? 'extra hours in shade from the block 新建築帶來的陰影時數' : 'all hours in shade 全日陰影時數'}`;
const w = ctx.measureText(txt).width + 16;
ctx.fillStyle = 'rgba(255,255,255,.92)'; ctx.fillRect(12, 12, w, 24); ctx.fillStyle = COL.ink; ctx.fillText(txt, 20, 29);
}
// ---------------------------------------------------------------- plan interaction
let drag = null, drawPts = [], hoverPt = null;
function inBlock(x, y) { return S.block.ring && pip(x, y, S.block.ring); }
cv.addEventListener('pointerdown', (e) => {
if (!S.site) return;
const r = cv.getBoundingClientRect(), [x, y] = S2W(e.clientX - r.left, e.clientY - r.top);
cv.setPointerCapture(e.pointerId);
if (S.shape === 'draw' && !S.block.drawn) {
if (drawPts.length >= 3) { const [a, b] = W2S(...drawPts[0]), [c, d] = [e.clientX - r.left, e.clientY - r.top]; if (Math.hypot(a - c, b - d) < 10) { finishDraw(); return; } }
drawPts.push([x, y]); drawPlan(); return;
}
if (inBlock(x, y)) drag = { kind: 'block', x0: x, y0: y, bx: S.block.x, by: S.block.y };
else drag = { kind: 'pan', sx: e.clientX, sy: e.clientY, cx: P.cx, cy: P.cy };
});
cv.addEventListener('pointermove', (e) => {
const r = cv.getBoundingClientRect(), [x, y] = S2W(e.clientX - r.left, e.clientY - r.top);
if (S.shape === 'draw' && drawPts.length) { hoverPt = [x, y]; drawPlan(); }
if (!drag) { cv.style.cursor = S.site && inBlock(x, y) ? 'move' : (S.shape === 'draw' && !S.block.drawn ? 'crosshair' : 'grab'); return; }
if (drag.kind === 'block') {
const lim = FRAME / 2 - 10;
S.block.x = Math.max(-lim, Math.min(lim, drag.bx + x - drag.x0)); S.block.y = Math.max(-lim, Math.min(lim, drag.by + y - drag.y0));
S.block.ring = S.shape === 'draw' && S.block.drawn ? S.block.drawn.map(([a, b]) => [a + S.block.x, b + S.block.y]) : rectRing(S.block);
S.block.cells = Int32Array.from(cellsInRing(S.block.ring)); quickUpdate();
} else {
P.cx = drag.cx - (e.clientX - drag.sx) / P.s; P.cy = drag.cy + (e.clientY - drag.sy) / P.s; drawPlan();
}
});
cv.addEventListener('pointerup', () => { if (drag && drag.kind === 'block') { rebuildBlock(); update(); } drag = null; });
cv.addEventListener('dblclick', () => { if (S.shape === 'draw' && drawPts.length >= 3) finishDraw(); });
cv.addEventListener('wheel', (e) => {
e.preventDefault(); const r = cv.getBoundingClientRect(), sx = e.clientX - r.left, sy = e.clientY - r.top;
const [x, y] = S2W(sx, sy); P.s = Math.max(0.5, Math.min(12, P.s * (e.deltaY < 0 ? 1.15 : 1 / 1.15)));
const [x2, y2] = S2W(sx, sy); P.cx += x - x2; P.cy += y - y2; drawPlan();
}, { passive: false });
function finishDraw() {
let ax = 0, ay = 0; for (const [x, y] of drawPts) { ax += x; ay += y; } ax /= drawPts.length; ay /= drawPts.length;
S.block.drawn = drawPts.map(([x, y]) => [x - ax, y - ay]); S.block.x = ax; S.block.y = ay;
drawPts = []; hoverPt = null; rebuildBlock(); update();
}
let qTimer = 0;
function quickUpdate() { // during a drag: shade now only, no day map
if (S.layer === 'now') { computeNow(); drawPlan(); if (view) view.update(S); }
else drawPlan();
clearTimeout(qTimer);
}
// ---------------------------------------------------------------- update cycle
let dayTimer = 0;
function update() {
if (!S.site) return;
computeNow(); renderSun(); renderLegend(); drawPlan();
if (view) view.update(S);
clearTimeout(dayTimer);
if (S.dirtyDay) dayTimer = setTimeout(async () => { busy('Working out the whole day… 計算全日陰影…'); await tick(); await computeDay(); busy(''); drawPlan(); renderLegend(); if (view) view.update(S); }, 250);
}
function renderSun() {
const sun = S.now && S.now.sun; if (!sun) return;
const [m, d] = S.date, t = sunTimes(YEAR, m, d, S.site.lat, S.site.lon);
const shadowLen = sun.elev > 0.5 ? (S.block.h / Math.tan(sun.elev * Math.PI / 180)) : Infinity;
const dir = (az) => ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW'][Math.round(az / 22.5) % 16];
$('hssSun').innerHTML = sun.elev > 0
? `Sun 太陽: ${sun.elev.toFixed(1)}° up, from ${dir(sun.az)} (${sun.az.toFixed(1)}°). A ${Math.round(S.block.h)} m block casts a shadow about ${shadowLen < 5000 ? Math.round(shadowLen) + ' m' : 'very'} long, pointing ${dir((sun.az + 180) % 360)}.
Sunrise 日出 ${hm(t.sunrise)} · solar noon 正午 ${hm(t.noon)} · sunset 日落 ${hm(t.sunset)} HKT`
: `The sun is down. 太陽已落山。 Sunrise 日出 ${hm(t.sunrise)} · sunset 日落 ${hm(t.sunset)} HKT`;
}
function renderLegend() {
const L2 = $('hssLegend');
if (S.layer === 'now') {
L2.innerHTML = `