/* HK Planting Board: procedural tree skeleton + foliage renderers (our own code). Skeleton: recursive axes in 3D (x right, y up, z toward viewer), metres. Architectural-model ideas after Hallé & Oldeman (1978) and Barthélémy & Caraglio (2007): apical dominance (monopodial vs sympodial), rhythmic tiers, plagiotropy, reiteration by forking. */ 'use strict'; function rng(seed) { let a = (seed * 2654435761) >>> 0; return function () { a |= 0; a = a + 0x6D2B79F5 | 0; let t = Math.imul(a ^ a >>> 15, 1 | a); t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t; return ((t ^ t >>> 14) >>> 0) / 4294967296; }; } const V = { add: (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]], mul: (a, s) => [a[0] * s, a[1] * s, a[2] * s], len: a => Math.hypot(a[0], a[1], a[2]), norm: a => { const l = Math.hypot(a[0], a[1], a[2]) || 1; return [a[0] / l, a[1] / l, a[2] / l]; }, cross: (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]], dot: (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2], }; function rotAround(v, axis, ang) { // Rodrigues const k = V.norm(axis), c = Math.cos(ang), s = Math.sin(ang); return V.add(V.add(V.mul(v, c), V.mul(V.cross(k, v), s)), V.mul(k, V.dot(k, v) * (1 - c))); } function perp(d) { const t = Math.abs(d[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0]; return V.norm(V.cross(d, t)); } /* Generic parameter sets for library plants without their own model */ const GENERIC = { 'g-round': { apical: 0.4, tiers: false, plagio: 0.35, angle: 48, angle2: 42, lenRatio: 0.7, droop: 0.02, children: 3, orders: 4, crown: 'round', trunkClear: 0.25 }, 'g-oval': { apical: 0.65, tiers: false, plagio: 0.2, angle: 40, angle2: 38, lenRatio: 0.65, droop: 0.02, children: 3, orders: 4, crown: 'oval', trunkClear: 0.25 }, 'g-umbrella': { apical: 0.15, tiers: false, plagio: 0.75, angle: 62, angle2: 48, lenRatio: 0.72, droop: 0.03, children: 3, orders: 4, crown: 'umbrella', trunkClear: 0.3 }, 'g-columnar': { apical: 0.9, tiers: false, plagio: 0.05, angle: 26, angle2: 28, lenRatio: 0.55, droop: 0.0, children: 3, orders: 3, crown: 'columnar', trunkClear: 0.1 }, 'g-conical': { apical: 0.95, tiers: true, plagio: 0.3, angle: 60, angle2: 40, lenRatio: 0.6, droop: 0.02, children: 3, orders: 3, crown: 'conical', trunkClear: 0.12 }, 's-mound': { shrub: true, stems: 6, apical: 0.3, plagio: 0.2, angle: 35, angle2: 40, lenRatio: 0.65, droop: 0.04, children: 2, orders: 3, crown: 'round', trunkClear: 0 }, 's-upright': { shrub: true, stems: 5, apical: 0.7, plagio: 0.0, angle: 14, angle2: 30, lenRatio: 0.55, droop: 0.01, children: 2, orders: 2, crown: 'columnar', trunkClear: 0 }, 'ground': { ground: true }, }; /* Monthly state. Returns 'L','N','F','LF','B','A' */ function phenology(p, month /*1-12*/) { const sp = speciesById(p.id); if (sp && sp.phen) return sp.phen[month - 1]; const fl = (p.fl || []).includes(month); if (p.leaf === 'deciduous') { const bare = [1, 2].includes(month); if (bare) return fl ? 'F' : (p.aut && month === 1 ? 'A' : 'B'); if (month === 3) return fl ? 'LF' : 'N'; if (p.aut && month === 12) return 'A'; } return fl ? 'LF' : (month === 3 || month === 4 ? 'N' : 'L'); } function paramsFor(p) { // p: LIB entry or SPECIES entry const sp = speciesById(p.form || p.id); if (sp) return sp; const g = GENERIC[p.form] || GENERIC['g-round']; const H = (p.h[0] + p.h[1]) / 2, S = (p.s[0] + p.s[1]) / 2; return Object.assign({ id: p.id, Hmax: H, Smax: S, k: p.type === 'tree' ? 0.09 : 0.5, leaf: p.col || '#5d8a4a', leafDark: shade(p.col || '#5d8a4a', -0.35), flower: p.flc || null, autumn: p.aut || null, bark: '#6f655a' }, g); } /* ---------- build skeleton ---------- */ function buildTree(sp, o) { // o: {H, S, seed, lean, bend, irregular, jitter, light (0..1), lightAz (deg), missing} const R = rng(o.seed || 1); const segs = [], tips = [], roots = []; const H = o.H, S = o.S; const irr = o.irregular || 0, jit = o.jitter || 0, miss = o.missing || 0; const lightAz = (o.lightAz || 0) * Math.PI / 180; const L = [Math.cos(lightAz), 0, Math.sin(lightAz)]; // horizontal light direction const lightK = o.light || 0; let budget = o.budget || 3500; if (sp.palm) return buildPalm(sp, o, R); if (sp.ground) return { segs, tips: groundTips(o, R), roots, H: o.H, S: o.S, ground: true, crownBase: 0, baseR: 0.02 }; const leanA = (o.lean || 0) * Math.PI / 180; const trunkDir = V.norm([Math.sin(leanA), Math.cos(leanA), 0]); const baseR = Math.max(0.03, H * 0.022); const queue = []; const grow = (p0, dir, len, r0, order) => queue.push([p0, dir, len, r0, order]); function axis(p0, dir, len, r0, order) { const nseg = Math.max(2, Math.min(14 - order * 3, Math.round(len / Math.max(0.18, H / 40)))); const sl = len / nseg; let p = p0, d = dir, r = r0; const pts = [p]; const startFrac = order === 0 ? (sp.trunkClear || 0) : 0.18 + 0.1 * R(); const nodes = []; for (let i = 0; i < nseg && budget > 0; i++) { budget--; // tropisms if (order === 0) { const bendA = (o.bend || 0) * Math.PI / 180 / nseg; d = rotAround(d, [0, 0, 1], -bendA * (i < nseg / 2 ? 1 : -0.4)); } else { const dro = (sp.droop || 0) * sl * (1 + order * 0.5) * (1 + 0.4 * i / nseg); d = V.norm(V.add(d, [0, -dro, 0])); } if (lightK > 0 && order > 0) d = V.norm(V.add(d, V.mul([L[0], 0.25, L[2]], lightK * 0.06 * sl))); if (jit > 0) d = V.norm(V.add(d, [(R() - .5) * jit * 0.5, (R() - .5) * jit * 0.25, (R() - .5) * jit * 0.5])); let q = V.add(p, V.mul(d, sl)); if (q[1] < 0.35 + 0.1 * order) { d = V.norm([d[0], Math.max(d[1], 0.2), d[2]]); q = V.add(p, V.mul(d, sl)); q[1] = Math.max(q[1], 0.35 + 0.1 * order); } const r1 = r0 * (1 - 0.75 * (i + 1) / nseg) + 0.004; segs.push({ a: p, b: q, r0: r, r1, order }); nodes.push({ p: q, d, t: (i + 1) / nseg, r: r1 }); p = q; r = r1; pts.push(q); } if (order >= sp.orders || budget <= 0) { tips.push({ p, d, order, r: len }); return; } // laterals const lat = nodes.filter(n => n.t >= startFrac && n.t < 0.985); let az = R() * Math.PI * 2; const tierEvery = sp.tiers && order === 0 ? Math.max(1, Math.round(lat.length / (3 + Math.min(4, H / 4)))) : 1; lat.forEach((n, idx) => { if (sp.tiers && order === 0 && idx % tierEvery !== 0) return; if (order > 0 && R() < 0.35) return; const nChild = sp.tiers && order === 0 ? Math.max(2, sp.children + 1) : (R() < 0.55 ? 1 : 2); for (let c = 0; c < nChild; c++) { if (R() < miss * (order === 0 ? 0.9 : 0.5)) continue; az += sp.tiers && order === 0 ? (Math.PI * 2 / nChild) : 2.399963; // 137.5° const tt = (n.t - startFrac) / Math.max(0.05, 1 - startFrac); let prof; switch (sp.crown) { case 'conical': case 'conical-oval': prof = 1 - 0.75 * tt; break; case 'columnar': prof = 0.55 - 0.2 * tt; break; case 'umbrella': case 'spreading': prof = 0.6 + 0.5 * (1 - Math.abs(tt - 0.25)); break; case 'tiered': prof = 1 - 0.7 * tt; break; case 'oval': prof = 0.5 + 0.6 * Math.sin(Math.PI * Math.min(1, tt * 0.9 + 0.1)); break; default: prof = 0.45 + 0.7 * Math.sin(Math.PI * Math.min(1, tt * 0.85 + 0.12)); } let cl = len * sp.lenRatio * prof * (1 + (R() - 0.5) * (0.35 + irr)); if (order === 0) cl = Math.max(cl, H * 0.12); // light: longer on the lit side const ang0 = (order === 0 ? sp.angle : sp.angle2) * Math.PI / 180 * (1 + (R() - .5) * (0.25 + irr * 0.8)); const side = perp(n.d); let cd = rotAround(n.d, rotAround(side, n.d, az), ang0); if (order === 0 && sp.plagio) { // towards horizontal const hz = V.norm([cd[0], 0, cd[2]]); cd = V.norm(V.add(V.mul(cd, 1 - sp.plagio), V.mul(hz, sp.plagio))); } if (lightK > 0) { const f = V.dot(V.norm([cd[0], 0, cd[2]]), L); cl *= 1 + lightK * 0.55 * f; } grow(n.p, cd, Math.max(0.05, cl), n.r * Math.sqrt(Math.max(0.1, cl / len)) * 0.85, order + 1); } }); // terminal: leader continues (monopodial) or forks (sympodial) const sym = order === 0 ? (1 - sp.apical) : 0.5; if (order < sp.orders && R() < sym && budget > 0) { const k = order === 0 ? 2 + (R() < 0.5 ? 1 : 0) : 2; for (let c = 0; c < k; c++) { const a = (order === 0 ? sp.angle * 0.6 : sp.angle2 * 0.7) * Math.PI / 180 * (0.7 + 0.6 * R()); const cd = rotAround(d, rotAround(perp(d), d, R() * 6.283), a); grow(p, cd, len * (order === 0 ? 0.5 : sp.lenRatio * 0.8) * (0.8 + 0.4 * R()), r * 0.9, order + 1); } } else { tips.push({ p, d, order, r: len }); } } if (sp.shrub) { const n = sp.stems || 5; for (let i = 0; i < n; i++) { const a = (i / n) * 6.283 + R(); const tilt = (sp.angle * Math.PI / 180) * (0.5 + 0.5 * R()); const d = V.norm([Math.sin(tilt) * Math.cos(a), Math.cos(tilt), Math.sin(tilt) * Math.sin(a)]); grow([0, 0, 0], d, H * (0.75 + 0.25 * R()), baseR * 0.5, 1); } } else { const trunkLen = sp.apical > 0.5 ? H * 0.92 : H * Math.max(sp.trunkClear + 0.12, 0.35); grow([0, 0, 0], trunkDir, trunkLen, baseR, 0); } while (queue.length) { const [p0, dir, len, r0, order] = queue.shift(); if (budget <= 0) { tips.push({ p: V.add(p0, V.mul(dir, len)), d: dir, order, r: len }); continue; } axis(p0, dir, len, r0, order); } // fit to target height and spread (calibration step) let minX = 1e9, maxX = -1e9, maxY = 0, minZ = 1e9, maxZ = -1e9; for (const s of segs) for (const q of [s.a, s.b]) { minX = Math.min(minX, q[0]); maxX = Math.max(maxX, q[0]); minZ = Math.min(minZ, q[2]); maxZ = Math.max(maxZ, q[2]); maxY = Math.max(maxY, q[1]); } const w = Math.max(maxX - minX, maxZ - minZ, 0.1); const fy = H * 0.9 / Math.max(maxY, 0.1); // foliage adds ~10% on top const fx = Math.min(1.8, Math.max(0.5, (S * 0.82) / w)); const cx = 0; // keep the trunk base at the origin const tf = q => [(q[0] - cx) * fx, q[1] * fy, q[2] * fx]; for (const s of segs) { s.a = tf(s.a); s.b = tf(s.b); s.r0 *= Math.sqrt(fx); s.r1 *= Math.sqrt(fx); } for (const t of tips) t.p = tf(t.p); // aerial roots (Ficus): hang from lower first-order branches if (sp.aerialRoots) { segs.filter(s => s.order === 1 && s.b[1] < H * 0.55 && R() < 0.12).slice(0, 18).forEach(s => { const drop = s.b[1] * (0.3 + 0.7 * R()) * Math.min(1, H / 10); roots.push({ a: s.b, b: [s.b[0] + (R() - .5) * 0.1, Math.max(0, s.b[1] - drop), s.b[2]], r: 0.01 + 0.02 * R() }); }); } // crown base = lowest foliage tip const crownBase = Math.max(0, tips.length ? Math.min(...tips.map(t => t.p[1])) : H * 0.3); return { segs, tips, roots, H, S, crownBase, buttress: sp.buttress || 0, baseR: baseR * Math.sqrt(fx) }; } function buildPalm(sp, o, R) { const H = o.H, segs = [], tips = []; const n = 16, leanA = ((o.lean || 0) + (R() - .5) * 6) * Math.PI / 180; let p = [0, 0, 0], d = [Math.sin(leanA), Math.cos(leanA), 0]; const stemH = H * 0.82; for (let i = 0; i < n; i++) { const bend = (o.bend || 0) * Math.PI / 180 / n; d = rotAround(d, [0, 0, 1], -bend); const q = V.add(p, V.mul(d, stemH / n)); const r = Math.max(0.12, H * 0.018) * (1 - 0.15 * i / n); segs.push({ a: p, b: q, r0: r, r1: r, order: 0 }); p = q; } const fr = 18, S = o.S; for (let i = 0; i < fr; i++) { const az = i * 2.399963 + R() * 0.3, el = (i / fr) * 1.4 - 0.3 + (R() - .5) * 0.3; // elevation let q = p, dir = V.norm([Math.cos(az) * Math.cos(el), Math.sin(el) + 0.4, Math.sin(az) * Math.cos(el)]); const fl = S * 0.55 * (0.8 + 0.3 * R()); for (let j = 0; j < 6; j++) { dir = V.norm(V.add(dir, [0, -0.18 * j / 6 - 0.05, 0])); const q2 = V.add(q, V.mul(dir, fl / 6)); segs.push({ a: q, b: q2, r0: 0.03, r1: 0.02, order: 1 }); q = q2; } tips.push({ p: q, d: dir, order: 2, r: fl, frond: true, from: p }); } return { segs, tips, roots: [], H, S, crownBase: stemH * 0.85, palm: true, baseR: 0.15 }; } function groundTips(o, R) { const tips = [], n = 16; for (let i = 0; i < n; i++) { const a = R() * 6.283, rr = Math.sqrt(R()) * o.S / 2; tips.push({ p: [Math.cos(a) * rr, o.H * (0.4 + 0.6 * R()), Math.sin(a) * rr], d: [0, 1, 0], order: 3, r: 0.1 }); } return tips; } /* ---------- colour helpers ---------- */ function hex2rgb(h) { h = h.replace('#', ''); return [parseInt(h.slice(0, 2), 16), parseInt(h.slice(2, 4), 16), parseInt(h.slice(4, 6), 16)]; } function rgb2hex(c) { return '#' + c.map(v => Math.max(0, Math.min(255, Math.round(v))).toString(16).padStart(2, '0')).join(''); } function mix(a, b, t) { const x = hex2rgb(a), y = hex2rgb(b); return rgb2hex(x.map((v, i) => v + (y[i] - v) * t)); } function shade(h, t) { return t < 0 ? mix(h, '#10180e', -t) : mix(h, '#fbfaf0', t); } function rgba(h, a) { const c = hex2rgb(h); return `rgba(${c[0]},${c[1]},${c[2]},${a})`; } /* ---------- foliage clusters ---------- */ function clusters(tree, sp, amount, R) { const out = []; if (tree.ground) { tree.tips.forEach(t => out.push({ p: t.p, r: Math.max(0.12, tree.S * 0.12) * (0.6 + amount * 0.6) })); return out; } const base = Math.max(0.18, Math.min(tree.S, tree.H) * 0.085) * (0.55 + amount * 0.75); for (const t of tree.tips) { if (t.frond) { out.push({ p: t.p, r: tree.S * 0.1, frond: true, from: t.from }); continue; } out.push({ p: t.p, r: base * (0.7 + 0.6 * R()) }); if (R() < amount * 0.8) out.push({ p: V.add(t.p, V.mul(t.d, -base * 0.9)), r: base * (0.6 + 0.4 * R()) }); } // extra fill along outer small branches if (amount > 0.4) for (const s of tree.segs) if (s.order >= (sp.orders || 3) - 1 && R() < (amount - 0.4) * 0.35) out.push({ p: s.b, r: base * (0.5 + 0.4 * R()) }); const CAP = 650; if (out.length > CAP) { const k = Math.sqrt(out.length / CAP); const keep = out.filter(() => R() < CAP / out.length); keep.forEach(c => c.r *= Math.min(1.6, k)); return keep; } return out; } /* group many small clusters into a few painterly masses (for the wash / layered styles) */ function massBlobs(cl, tree, R) { if (cl.length < 14) return cl.map(c => Object.assign({}, c, { r: c.r * 1.2 })); const xs = cl.map(c => c.p[0]), ys = cl.map(c => c.p[1]); const x0 = Math.min(...xs), x1 = Math.max(...xs), y0 = Math.min(...ys), y1 = Math.max(...ys); const nx = 4, ny = 4, g = new Map(); for (const c of cl) { const i = Math.min(nx - 1, Math.floor((c.p[0] - x0) / Math.max(0.01, x1 - x0) * nx)), j = Math.min(ny - 1, Math.floor((c.p[1] - y0) / Math.max(0.01, y1 - y0) * ny)); const k = i + ',' + j; if (!g.has(k)) g.set(k, []); g.get(k).push(c); } const out = []; for (const arr of g.values()) { if (arr.length < 2) continue; const cx = arr.reduce((a, c) => a + c.p[0], 0) / arr.length, cy = arr.reduce((a, c) => a + c.p[1], 0) / arr.length, cz = arr.reduce((a, c) => a + c.p[2], 0) / arr.length; const spread = Math.max(...arr.map(c => Math.hypot(c.p[0] - cx, c.p[1] - cy) + c.r)); out.push({ p: [cx, cy, cz], r: spread * 0.92, n: arr.length }); } return out.sort((a, b) => a.p[2] - b.p[2]); } /* ---------- render ---------- */ const STYLES = [ { id: 'real', zh: '細葉寫實', en: 'Leaf detail', hint: '逐片細葉,受光面與背光面分明' }, { id: 'wash', zh: '水彩渲染', en: 'Wash', hint: '大片透明渲染,邊緣自然化開' }, { id: 'layer', zh: '三層套色', en: 'Three tones', hint: '淺、中、深三層顏色套疊' }, { id: 'pen', zh: '針筆線描加彩', en: 'Line + tint', hint: '針筆輪廓線加一層淡彩' }, { id: 'dry', zh: '炭筆乾擦', en: 'Dry brush', hint: '短筆乾擦,留出紙白' }, ]; function blobPath(ctx, x, y, r, R, k = 12, wob = 0.28) { ctx.beginPath(); const ph = R() * 6.283; for (let i = 0; i <= k; i++) { const a = ph + i / k * 6.283; const rr = r * (1 - wob / 2 + wob * R()); const px = x + Math.cos(a) * rr, py = y + Math.sin(a) * rr * 0.9; i ? ctx.lineTo(px, py) : ctx.moveTo(px, py); } ctx.closePath(); } /* draws a tree onto ctx with base at (bx,by), px per metre = sc */ function drawTree(ctx, tree, sp, opt) { const { bx, by, sc } = opt; const style = opt.style || 'wash', month = opt.month || 6, amount = opt.foliage ?? 0.7, intensity = opt.intensity ?? 0.7; const R = rng((opt.seed || 1) * 7 + 3); const state = opt.state || 'L'; const X = p => bx + p[0] * sc, Y = p => by - p[1] * sc; const lightAz = (opt.lightAz || 30) * Math.PI / 180; const Lx = Math.cos(lightAz); // ground shadow if (!opt.noShadow) { ctx.save(); ctx.fillStyle = 'rgba(80,80,70,0.10)'; ctx.beginPath(); ctx.ellipse(bx - Lx * tree.S * sc * 0.15, by, Math.max(4, tree.S * sc * 0.55), Math.max(2, tree.S * sc * 0.08), 0, 0, 6.283); ctx.fill(); ctx.restore(); } const leafOn = state !== 'B' && state !== 'F'; let leafC = sp.leaf, darkC = sp.leafDark; if (state === 'N') { leafC = mix(leafC, '#c6de8a', 0.45); darkC = mix(darkC, '#8fb060', 0.3); } if (state === 'A') { leafC = mix(leafC, sp.autumn || '#c2552f', 0.8); darkC = mix(sp.autumn || '#c2552f', '#6a3a20', 0.45); } const cl = tree.palm || tree.ground ? clusters(tree, sp, amount, R) : clusters(tree, sp, leafOn ? amount : 0.25, R); cl.sort((a, b) => a.p[2] - b.p[2]); const dens = tree.ground ? 0.38 : Math.min(1, Math.max(0.3, 13 / Math.sqrt(Math.max(1, cl.length)))); const painterly = (style === 'wash' || style === 'layer' || style === 'pen') && !tree.palm; const masses = painterly ? massBlobs(cl, tree, R) : null; const cy = tree.crownBase + (tree.H - tree.crownBase) / 2; const shadeOf = c => { // 0 lit .. 1 dark const dx = c.p[0] / Math.max(0.5, tree.S / 2), dy = (c.p[1] - cy) / Math.max(0.5, tree.H / 2); return Math.max(0, Math.min(1, 0.5 - 0.35 * dx * Lx - 0.35 * dy - 0.15 * c.p[2] / Math.max(0.5, tree.S / 2))); }; const ink = style === 'pen' ? '#2b2620' : sp.bark; function drawWood(alpha) { ctx.save(); ctx.lineCap = 'round'; ctx.strokeStyle = ink; ctx.globalAlpha = alpha; for (const s of tree.segs) { if (tree.palm && s.order > 0) continue; ctx.lineWidth = Math.max(style === 'pen' ? 0.6 : 0.5, (s.r0 + s.r1) * sc * (s.order === 0 ? 1 : 0.9)); ctx.beginPath(); ctx.moveTo(X(s.a), Y(s.a)); ctx.lineTo(X(s.b), Y(s.b)); ctx.stroke(); } if (tree.buttress && !tree.palm) { ctx.fillStyle = ink; const w = tree.baseR * sc * (1 + tree.buttress * 2.2); ctx.beginPath(); ctx.moveTo(bx - w * 1.4, by); ctx.quadraticCurveTo(bx - w * 0.3, by - w * 0.3, bx - tree.baseR * sc, by - w * 2.2); ctx.lineTo(bx + tree.baseR * sc, by - w * 2.2); ctx.quadraticCurveTo(bx + w * 0.3, by - w * 0.3, bx + w * 1.4, by); ctx.closePath(); ctx.fill(); } ctx.lineWidth = Math.max(0.5, 0.02 * sc); ctx.globalAlpha = alpha * 0.8; for (const r of tree.roots) { ctx.lineWidth = Math.max(0.5, r.r * sc); ctx.beginPath(); ctx.moveTo(X(r.a), Y(r.a)); ctx.lineTo(X(r.b), Y(r.b)); ctx.stroke(); } // bark highlight on the lit side for trunk if (style !== 'pen') { ctx.globalAlpha = alpha * 0.25; ctx.strokeStyle = '#f4efe2'; for (const s of tree.segs) if (s.order === 0) { ctx.lineWidth = Math.max(0.5, s.r0 * sc * 0.5); ctx.beginPath(); ctx.moveTo(X(s.a) + Lx * s.r0 * sc * 0.4, Y(s.a)); ctx.lineTo(X(s.b) + Lx * s.r1 * sc * 0.4, Y(s.b)); ctx.stroke(); } } ctx.restore(); } function frond(c, col) { ctx.save(); ctx.strokeStyle = col; ctx.lineWidth = Math.max(0.6, 0.05 * sc); ctx.globalAlpha = 0.8; const n = 9, fx = X(c.from), fy = Y(c.from), tx = X(c.p), ty = Y(c.p); for (let i = 0; i < n; i++) { const t = i / n, mx = fx + (tx - fx) * (0.5 + t * 0.5), my = fy + (ty - fy) * (0.5 + t * 0.5); ctx.beginPath(); ctx.moveTo(fx, fy); ctx.quadraticCurveTo(mx, my - 6, tx + (R() - .5) * c.r * sc * 0.8, ty + (R() - .2) * c.r * sc * 0.8); ctx.stroke(); } ctx.restore(); } function foliage(c, pass) { const x = X(c.p), y = Y(c.p), r = c.r * sc, sh = shadeOf(c); if (c.frond) { frond(c, mix(leafC, darkC, sh)); return; } const col = mix(leafC, darkC, sh); switch (style) { case 'real': { ctx.fillStyle = rgba(darkC, 0.25 * intensity); blobPath(ctx, x, y + r * 0.1, r * 0.9, R); ctx.fill(); const n = Math.round(6 + 16 * amount); for (let i = 0; i < n; i++) { const a = R() * 6.283, d = Math.sqrt(R()) * r; const lx = x + Math.cos(a) * d, ly = y + Math.sin(a) * d * 0.85; const loc = Math.max(0, Math.min(1, sh + (Math.sin(a) * 0.25) + (R() - .5) * 0.3)); ctx.fillStyle = rgba(mix(shade(leafC, 0.15), darkC, loc), 0.55 + 0.4 * intensity); ctx.beginPath(); ctx.ellipse(lx, ly, Math.max(0.8, r * 0.16), Math.max(0.5, r * 0.09), R() * 3.14, 0, 6.283); ctx.fill(); } break; } case 'wash': { // c is a mass: two light washes, a shadow wash low and away from the light, a soft edge const a0 = pass === 9 ? 0.5 : 1; // front masses thinner so wood shows through ctx.fillStyle = rgba(shade(leafC, 0.3), (0.2 + 0.2 * intensity) * a0); blobPath(ctx, x, y, r, R, 16, 0.3); ctx.fill(); ctx.fillStyle = rgba(leafC, (0.14 + 0.16 * intensity) * a0); blobPath(ctx, x - r * 0.08, y + r * 0.05, r * 0.82, R, 14, 0.3); ctx.fill(); if (sh > 0.35) { ctx.fillStyle = rgba(darkC, (0.1 + 0.2 * intensity) * sh * a0); blobPath(ctx, x + Lx * -r * 0.2, y + r * 0.28, r * 0.62, R, 12, 0.35); ctx.fill(); } ctx.strokeStyle = rgba(darkC, 0.12 * intensity * a0); ctx.lineWidth = 1; blobPath(ctx, x, y, r * 1.02, R, 16, 0.3); ctx.stroke(); break; } case 'layer': { const cols = [shade(leafC, 0.35), leafC, darkC]; if (pass === 0) { ctx.fillStyle = rgba(cols[0], 0.35 + 0.25 * intensity); blobPath(ctx, x, y, r * 1.05, R, 12, 0.3); ctx.fill(); } if (pass === 1 && sh > 0.25) { ctx.fillStyle = rgba(cols[1], 0.3 + 0.25 * intensity); blobPath(ctx, x + r * 0.12, y + r * 0.18, r * 0.75, R); ctx.fill(); } if (pass === 2 && sh > 0.5) { ctx.fillStyle = rgba(cols[2], 0.3 + 0.25 * intensity); blobPath(ctx, x + r * 0.18, y + r * 0.32, r * 0.45, R); ctx.fill(); } break; } case 'pen': { if (pass === 0) { ctx.fillStyle = rgba(shade(col, 0.35), 0.22 + 0.2 * intensity); blobPath(ctx, x, y, r * 0.95, R, 12, 0.25); ctx.fill(); } if (pass === 1) { ctx.strokeStyle = 'rgba(40,36,30,' + (0.25 + 0.35 * intensity) + ')'; ctx.lineWidth = 0.6; ctx.beginPath(); // scalloped outline const k = 9, ph = R() * 6.283; for (let i = 0; i <= k; i++) { const a = ph + i / k * 6.283, a2 = ph + (i + 0.5) / k * 6.283; const px = x + Math.cos(a) * r * 0.85, py = y + Math.sin(a) * r * 0.75; if (!i) ctx.moveTo(px, py); else ctx.quadraticCurveTo(x + Math.cos(a2 - 0.35) * r * 1.02, y + Math.sin(a2 - 0.35) * r * 0.9, px, py); } if (sh > 0.4) { for (let i = 0; i < 6; i++) { const hx = x - r * 0.45 + i * r * 0.16; ctx.moveTo(hx, y + r * 0.2); ctx.lineTo(hx + r * 0.22, y + r * 0.55); } } ctx.stroke(); } break; } case 'dry': { ctx.lineCap = 'round'; const n = Math.round(5 + 10 * amount); for (let i = 0; i < n; i++) { const a = R() * 6.283, d = Math.sqrt(R()) * r * 1.1; const sx = x + Math.cos(a) * d, sy = y + Math.sin(a) * d * 0.85, dir = -0.6 + (R() - .5) * 0.8; ctx.strokeStyle = rgba(mix(leafC, darkC, Math.min(1, sh + (R() - .5) * 0.4)), 0.3 + 0.35 * intensity); ctx.lineWidth = Math.max(0.8, r * (0.12 + 0.1 * R())); ctx.beginPath(); ctx.moveTo(sx, sy); ctx.lineTo(sx + Math.cos(dir) * r * 0.7, sy + Math.sin(dir) * r * 0.7); ctx.stroke(); } break; } } } function flowers(c) { const x = X(c.p), y = Y(c.p), r = c.r * sc; const n = state === 'F' ? 4 : 3; for (let i = 0; i < n; i++) { const a = R() * 6.283, d = Math.sqrt(R()) * r * (state === 'F' ? 0.55 : 1.0); ctx.fillStyle = rgba(i % 3 ? sp.flower : shade(sp.flower, -0.2), 0.8); ctx.beginPath(); ctx.arc(x + Math.cos(a) * d, y + Math.sin(a) * d * 0.85, Math.max(0.8, Math.min(0.09 * sc, r * (state === 'F' ? 0.1 : 0.09))), 0, 6.283); ctx.fill(); } } const back = cl.filter(c => c.p[2] < 0), front = cl.filter(c => c.p[2] >= 0); const passes = style === 'layer' ? [0, 1, 2] : style === 'pen' ? [0, 1] : [0]; const woodA = leafOn ? (style === 'pen' ? 0.95 : 0.9) : 1; if ((leafOn || tree.ground) && painterly) { const mb = masses.filter(m => m.p[2] < 0), mf = masses.filter(m => m.p[2] >= 0); for (const pz of passes) mb.forEach(c => foliage(c, pz)); drawWood(0.85); for (const pz of passes) mf.forEach(c => foliage(c, style === 'wash' ? 9 : pz)); // a little leaf texture ctx.save(); cl.forEach(c => { if (R() < 0.12) { ctx.fillStyle = rgba(mix(leafC, darkC, shadeOf(c)), 0.28 * intensity); ctx.beginPath(); ctx.ellipse(X(c.p), Y(c.p), Math.max(1, c.r * sc * 0.3), Math.max(0.8, c.r * sc * 0.18), R() * 3, 0, 6.283); ctx.fill(); } }); ctx.restore(); } else if (leafOn || tree.palm || tree.ground) { for (const pz of passes) back.forEach(c => foliage(c, pz)); drawWood(woodA); for (const pz of passes) front.forEach(c => foliage(c, pz)); } else { drawWood(1); } if (sp.flower && (state === 'F' || state === 'LF')) { ctx.save(); const fp = Math.min(1, (state === 'F' ? 70 : 45) / Math.max(1, cl.length)); cl.forEach(c => { if (R() < fp) flowers(c); }); ctx.restore(); } } /* skeleton coloured by branch order */ const ORDER_COL = ['#2e2e30', '#c4763c', '#6d6a64', '#a8a49a', '#cbc7bd']; /* trunk graphite, 1st order AW terracotta, then lighter graphite */ function drawSkeleton(ctx, tree, opt) { const { bx, by, sc } = opt; const X = p => bx + p[0] * sc, Y = p => by - p[1] * sc; ctx.save(); ctx.lineCap = 'round'; for (const s of tree.segs) { ctx.strokeStyle = ORDER_COL[Math.min(4, s.order)]; ctx.globalAlpha = opt.alpha ?? 1; ctx.lineWidth = Math.max(0.8, (s.r0 + s.r1) * sc * 0.9); ctx.beginPath(); ctx.moveTo(X(s.a), Y(s.a)); ctx.lineTo(X(s.b), Y(s.b)); ctx.stroke(); } ctx.strokeStyle = ORDER_COL[1]; ctx.setLineDash([3, 3]); for (const r of tree.roots) { ctx.lineWidth = 1; ctx.beginPath(); ctx.moveTo(X(r.a), Y(r.a)); ctx.lineTo(X(r.b), Y(r.b)); ctx.stroke(); } ctx.restore(); } /* ---------- size at age ---------- */ function sizeAtAge(sp, age) { // species tab (years) const f = 1 - Math.exp(-sp.k * (age + 1.5)); return { H: Math.max(1.2, sp.Hmax * f), S: Math.max(0.6, sp.Smax * Math.pow(f, 1.1)) }; } function sizeAtYears(p, yrs) { // board: planted -> mature const Hm = (p.h[0] + p.h[1]) / 2, Sm = (p.s[0] + p.s[1]) / 2; // planted size, then an initial growth rate (m/yr) that slows towards maturity (design assumption) let Hp, Sp, gh, gs; if (p.type === 'tree') { Hp = Math.min(3.5, Hm * 0.35); Sp = Math.min(1.6, Sm * 0.25); gh = 0.75; gs = 0.55; } else if (p.type === 'palm') { Hp = Math.min(3, Hm * 0.35); Sp = Math.min(2.2, Sm * 0.6); gh = 0.4; gs = 0.2; } else if (p.type === 'shrub') { Hp = Hm * 0.45; Sp = Sm * 0.45; gh = 0.3; gs = 0.3; } else { Hp = Hm * 0.6; Sp = Sm * 0.5; gh = 0.3; gs = 0.4; } const ease = (a, b, g) => b <= a ? b : a + (b - a) * (1 - Math.exp(-yrs * g / (b - a))); return { H: ease(Hp, Hm, gh), S: ease(Sp, Sm, gs) }; } /* sprite cache for the board */ const _spriteCache = new Map(); function plantSprite(p, yrs, month, style, seed, pxPerM) { pxPerM = pxPerM <= 30 ? 30 : pxPerM <= 60 ? 60 : 100; const sz = sizeAtYears(p, yrs); const hb = Math.round(sz.H * 4) / 4; const key = [p.id, hb, month, style, seed, Math.round(pxPerM)].join('|'); if (_spriteCache.has(key)) return _spriteCache.get(key); const sp = paramsFor(p); const tree = buildTree(sp, { H: sz.H, S: sz.S, seed, budget: 1800 }); const w = Math.ceil(sz.S * pxPerM * 1.5 + 10), h = Math.ceil(sz.H * pxPerM * 1.15 + 10); const cv = document.createElement('canvas'); cv.width = w; cv.height = h; const ctx = cv.getContext('2d'); drawTree(ctx, tree, sp, { bx: w / 2, by: h - 4, sc: pxPerM, style, month, state: phenology(p, month), seed, foliage: 0.75, intensity: 0.75 }); const out = { cv, ax: w / 2, ay: h - 4, H: sz.H, S: sz.S, px: pxPerM }; if (_spriteCache.size > 400) _spriteCache.clear(); _spriteCache.set(key, out); return out; }