Replace FruitCluster with FruitImageCluster component using species-specific sprites, add maturity-driven fruit sizing, and define sensor sweep data types

Replace abstract FruitCluster with new FruitImageCluster that renders actual species photos/illustrations on bags. Remove generic pinning circles in favor of species-specific sprites at tiny scale. Add computeFruitingMaturity function to calculate 0→1 progress through fruiting window based on estimated_ready_ts, driving fruit image sizing so
This commit is contained in:
grove5_admin committed 2026-04-27 18:39:38 -07:00
1 parent 3dbb68f8fb
commit 270fd4c1e1
6 files changed
+372 -60

No files matched your search

+20 -39
View File
@@ -1,6 +1,6 @@
<script lang="ts">
import FruitCluster from './FruitCluster.svelte';
import { profileFor, imageForOrFallback } from './species';
import FruitImageCluster from './FruitImageCluster.svelte';
import { profileFor, imageForOrFallback, computeFruitingMaturity } from './species';
import type { BagAnnotation } from '../types';
interface Props {
@@ -11,6 +11,8 @@
strainCode?: string;
inoculatedTs?: number;
strainName?: string;
/** Projected harvest-ready timestamp (seconds). Drives fruit sizing. */
estimatedReadyTs?: number;
}
const {
@@ -21,8 +23,13 @@
strainCode = '',
inoculatedTs,
strainName = '',
estimatedReadyTs,
}: Props = $props();
const fruitMaturity = $derived(
estimatedReadyTs ? computeFruitingMaturity(estimatedReadyTs) : 0.5
);
const profile = $derived(profileFor(strainName));
const speciesImg = $derived(imageForOrFallback(strainName));
const coverage = $derived(Math.max(0, Math.min(100, colonizationPct)) / 100);
@@ -59,8 +66,7 @@
contaminationRisk === 'medium' ? '#c97a30' : '#d99050'
);
const showFruits = $derived(stage === 'fruiting' || stage === 'harvest');
const showPins = $derived(stage === 'pinning');
const showFruits = $derived(stage === 'pinning' || stage === 'fruiting' || stage === 'harvest');
const showContam = $derived(contaminationRisk !== 'none');
function hashSeed(s: string): number {
@@ -272,22 +278,8 @@
/>
{/if}
<!-- ── Pinning primordia ── -->
{#if showPins}
{#each [0.22, 0.40, 0.60, 0.78] as fx, fi (fi)}
<circle
cx={BX + fx * BW} cy={BY + 16}
r="4.5"
fill="#d6e8ed" opacity="0.92"
class="pin-pop"
style="animation-delay: {fi * 0.35}s"
/>
<circle
cx={BX + fx * BW} cy={BY + 15}
r="1.8" fill="#fff" opacity="0.58"
/>
{/each}
{/if}
<!-- (Pinning primordia are now rendered by FruitImageCluster below
using species-specific sprites at tiny scale.) -->
<!-- Clear plastic overlays -->
<rect x={BX} y={BY} width={BW} height={BH} fill="url(#bhc-plastic)"/>
@@ -325,15 +317,13 @@
bag face; scale prop multiplies fruit body sizes to match. -->
{#if showFruits}
<g clip-path="url(#bhc-clip)">
<g transform="translate({BX}, {BY + 8})">
<FruitCluster
profile={profile}
br={{ x: 0, y: 0, w: BW, h: BH * 0.72 }}
seed={seed}
scale={2.4}
stage={stage as 'fruiting' | 'harvest'}
/>
</g>
<FruitImageCluster
strainName={strainName}
br={{ x: BX, y: BY + 8, w: BW, h: BH * 0.72 }}
seed={seed}
stage={stage as 'pinning' | 'fruiting' | 'harvest'}
maturity={fruitMaturity}
/>
</g>
{/if}
@@ -561,15 +551,6 @@
50% { stroke-opacity: 0.18; }
}
.pin-pop {
animation: pin-pop 2.6s ease-in-out infinite;
transform-box: fill-box;
transform-origin: center;
}
@keyframes pin-pop {
0%, 100% { opacity: 0.72; transform: scale(1); }
50% { opacity: 1; transform: scale(1.14); }
}
.harvest-ring {
animation: harvest-breathe 3.4s ease-in-out infinite;
@@ -584,6 +565,6 @@
.portrait-img { transition: none; }
.contam-breathe,
.contam-hi, .contam-lo,
.pin-pop, .harvest-ring { animation: none; }
.harvest-ring { animation: none; }
}
</style>
+7 -13
View File
@@ -2,9 +2,9 @@
import { fade } from 'svelte/transition';
import { cultivar } from '../store.svelte';
import type { Chamber, BagState } from '../types';
import FruitCluster from './FruitCluster.svelte';
import FruitImageCluster from './FruitImageCluster.svelte';
import GalleryFrame, { type FrameSpec } from './GalleryFrame.svelte';
import { profileFor, imageFor } from './species';
import { imageFor, computeFruitingMaturity } from './species';
interface Props {
onBagClick?: (bagId: string, el: HTMLElement) => void;
@@ -769,7 +769,7 @@
{@const cc = contamColor(bag)}
{@const pct = bag.vision.colonization_pct}
{@const fh = Math.max(4, (br.h - 4) * pct / 100)}
{@const showFruits = pct >= 100 && (bag.vision.stage === 'fruiting' || bag.vision.stage === 'harvest')}
{@const showFruits = pct >= 100 && (bag.vision.stage === 'pinning' || bag.vision.stage === 'fruiting' || bag.vision.stage === 'harvest')}
<g
role="button"
tabindex="0"
@@ -786,11 +786,12 @@
fill="var(--bag-mycelium)" fill-opacity="0.95"/>
{#if showFruits}
<FruitCluster
profile={profileFor(bag.tag.strain_name)}
<FruitImageCluster
strainName={bag.tag.strain_name}
br={br}
seed={hashSeed(bag.slot_id)}
stage={bag.vision.stage as 'fruiting' | 'harvest'}
stage={bag.vision.stage as 'pinning' | 'fruiting' | 'harvest'}
maturity={computeFruitingMaturity(bag.vision.projection.estimated_ready_ts)}
/>
{/if}
</g>
@@ -1305,13 +1306,6 @@
opacity: 1;
font-size: 12.5px;
}
.lc-sep { opacity: 0.3; color: var(--ink); }
.lc-desc {
font-style: italic;
font-family: var(--font-serif);
color: var(--ink-muted);
opacity: 0.8;
}
@media (max-width: 640px) {
.labels { gap: 2px; margin-bottom: -62px; }
+161
View File
@@ -0,0 +1,161 @@
<script lang="ts">
import { imageForOrFallback, clusterTuneFor } from './species';
type Rect = { x: number; y: number; w: number; h: number };
type Props = {
strainName: string;
br: Rect;
seed: number;
stage: 'pinning' | 'fruiting' | 'harvest';
/**
* Fraction of progress through the fruiting window, 0 → 1.
* - 0 = just entered fruiting (tiny baby fruits)
* - 1 = about to be harvest-ready (full-size)
* Ignored for `pinning` (always small) and `harvest` (always full+).
* Default 0.5 if omitted.
*/
maturity?: number;
/** Scene scale multiplier. Default 1. Values >1 are hero/close-ups. */
scale?: number;
};
const { strainName, br, seed, stage, maturity = 0.5, scale = 1 }: Props = $props();
const imgUrl = $derived(imageForOrFallback(strainName));
const tune = $derived(clusterTuneFor(strainName));
// Deterministic RNG so a given bag/strain always looks the same across renders.
function makeRng(s: number) {
let x = (s || 1) >>> 0;
return () => {
x = (x * 9301 + 49297) % 233280;
return x / 233280;
};
}
function clamp01(v: number): number {
return v < 0 ? 0 : v > 1 ? 1 : v;
}
// Growth curve — intentionally dramatic so bags visibly "grow" over time.
// pinning → tiny, ~0.14 (primordia just formed)
// fruiting → 0.30 → 1.05 across maturity (barely there → nearly harvest-big)
// harvest → 1.60 (overflowing the bag; fully mature flush)
const vigor = $derived.by(() => {
if (stage === 'harvest') return 1.60;
if (stage === 'pinning') return 0.14;
// Slight ease-in (t^1.6) so early fruiting stays small longer, then
// accelerates into the last third of the window.
const t = clamp01(maturity);
const eased = Math.pow(t, 1.6);
return 0.30 + 0.75 * eased;
});
type Sprite = {
cx: number; cy: number;
size: number;
rot: number;
flip: boolean;
op: number;
};
const sprites = $derived.by<Sprite[]>(() => {
const rng = makeRng(seed);
const baseSize = Math.min(br.w * tune.primarySize, br.h * 0.62);
// ── Pinning layout ──────────────────────────────────────────
// Several very small sprites scattered near the filter-patch area.
// No dominant primary; they read as a patch of primordia.
if (stage === 'pinning') {
const count = Math.max(3, Math.min(6, tune.count + 2));
const pinSize = baseSize * vigor; // ~0.18 × baseSize
const out: Sprite[] = [];
for (let i = 0; i < count; i++) {
// Distribute horizontally across the filter-patch band.
const t = (i + 0.5) / count;
const jitterX = (rng() - 0.5) * 0.12;
const jitterY = (rng() - 0.5) * 0.10;
out.push({
cx: br.x + br.w * (0.15 + 0.70 * t + jitterX),
cy: br.y + br.h * (0.16 + jitterY),
size: pinSize * (0.8 + rng() * 0.45),
rot: (rng() - 0.5) * 20,
flip: tune.flip && rng() < 0.5,
op: 0.88 + rng() * 0.12,
});
}
return out;
}
// ── Fruiting / Harvest layout ───────────────────────────────
// Primary sprite anchors near the top of the bag so fruits read as
// "emerging from the filter patch area". Secondaries offset around it.
// Harvest-ready bags get 1–2 extra specimens in the cluster — a full
// flush has more mushrooms, not just bigger ones. Species with count=1
// (shiitake / reishi / cordyceps — portraits with visible substrate)
// are kept at 1 so the baked-in base doesn't repeat.
const harvestBonus = stage === 'harvest' && tune.count >= 2 ? 2 : 0;
const count = Math.max(1, Math.min(5, tune.count + harvestBonus));
const primarySize = baseSize * vigor;
const primaryCx = br.x + br.w * (0.48 + (rng() - 0.5) * 0.08);
// As fruits grow, their center lifts slightly (they're taller).
const centerYFrac = stage === 'harvest' ? 0.26 : 0.32 - 0.04 * clamp01(maturity);
const primaryCy = br.y + br.h * (centerYFrac + rng() * 0.04);
const primaryRot = (rng() - 0.5) * tune.rotate * 0.6;
const out: Sprite[] = [];
// Secondary/tertiary sprites are placed BEHIND the primary (drawn first).
// They peek out from behind at smaller scale and stronger rotation.
for (let i = 1; i < count; i++) {
const side = rng() < 0.5 ? -1 : 1;
const sizeFrac = 0.55 + rng() * 0.18; // 55–73% of primary
const size = primarySize * sizeFrac;
// Offset sideways and slightly lower so it looks nestled.
const dx = side * (0.22 + rng() * 0.18) * primarySize;
const dy = (0.08 + rng() * 0.14) * primarySize;
out.push({
cx: primaryCx + dx,
cy: primaryCy + dy,
size,
rot: (rng() - 0.5) * tune.rotate * 2,
flip: tune.flip && rng() < 0.55,
op: 0.92,
});
}
// Primary, drawn last → on top.
out.push({
cx: primaryCx,
cy: primaryCy,
size: primarySize,
rot: primaryRot,
flip: false,
op: 1,
});
return out;
});
// `scale` is accepted for forward compatibility with callers that pass
// it (e.g. hero card). Sizing is derived from `br` directly.
</script>
{#each sprites as s, i (i)}
{@const half = s.size / 2}
<g
transform="translate({s.cx} {s.cy}) rotate({s.rot}) scale({s.flip ? -1 : 1} 1)"
opacity={s.op}
style="pointer-events: none;"
>
<image
href={imgUrl}
x={-half}
y={-half}
width={s.size}
height={s.size}
preserveAspectRatio="xMidYMid meet"
/>
</g>
{/each}
+104 -8
View File
@@ -106,25 +106,121 @@ export function profileFor(strainName: string): SpeciesProfile {
// /static/img/species. Used by the wall-gallery, bag popover,
// and anywhere else we want a proper "portrait" of a species.
const IMAGES: Record<string, string> = {
// ── Oyster family ─────────────────────────────────────────
'Blue Oyster': '/img/species/blue_oyster.webp',
'Pearl Oyster': '/img/species/blue_oyster.webp',
'Phoenix Oyster': '/img/species/blue_oyster.webp',
'Pearl Oyster': '/img/species/pearl_oyster.webp',
'Phoenix Oyster': '/img/species/phoenix_oyster.webp',
'Golden Oyster': '/img/species/golden_oyster.webp',
'Pink Oyster': '/img/species/pink_oyster.webp',
'Elm Oyster': '/img/species/blue_oyster.webp',
'Tarragon Oyster': '/img/species/blue_oyster.webp',
'Abalone Mushroom': '/img/species/blue_oyster.webp',
'Black Pearl King Oyster': '/img/species/king_oyster.webp',
'Bitter Oyster (Bioluminescent)': '/img/species/blue_oyster.webp',
'Elm Oyster': '/img/species/elm_oyster.webp',
'Tarragon Oyster': '/img/species/tarragon_oyster.webp',
'Abalone Mushroom': '/img/species/abalone.webp',
'Black Pearl King Oyster': '/img/species/black_pearl_king_oyster.webp',
'Bitter Oyster (Bioluminescent)': '/img/species/bitter_oyster.webp',
// ── Lion's Mane / Coral ───────────────────────────────────
"Lion's Mane": '/img/species/lions_mane.webp',
'Coral Tooth': '/img/species/coral_tooth.webp',
// ── Capped ────────────────────────────────────────────────
'Shiitake': '/img/species/shiitake.webp',
'Chestnut Mushroom': '/img/species/chestnut.webp',
'Pioppino': '/img/species/pioppino.webp',
'Nameko': '/img/species/nameko.webp',
// ── Beech ─────────────────────────────────────────────────
'White Beech': '/img/species/white_beech.webp',
'Brown Beech': '/img/species/brown_beech.webp',
// ── Shelf / club / rosette ────────────────────────────────
'Reishi': '/img/species/reishi.webp',
'Red Reishi': '/img/species/reishi.webp',
'Turkey Tail': '/img/species/turkey_tail.webp',
'Maitake': '/img/species/maitake.webp',
'King Trumpet': '/img/species/king_oyster.webp',
'Maitake': '/img/species/hen_of_woods.webp',
'Cordyceps': '/img/species/cordyceps.webp',
};
/**
* Per-strain tuning for the on-bag image cluster. Controls how many copies
* of the species image are placed on the bag top at fruiting/harvest, and
* how much they can rotate / flip. Species that are already rendered as a
* full bouquet portrait use low counts (1–2); individuals that look right
* repeated get higher counts.
*
* Images that bake substrate/wood into the portrait (shiitake log, reishi
* wood chunk, cordyceps oat nest) use `count: 1` + flip off so you don't
* see the base repeated.
*/
export type ImageClusterTune = {
/** How many image copies to place (1–3). Default 2. */
count: number;
/** Max ± rotation in degrees. Default 14. */
rotate: number;
/** Allow horizontal mirroring on secondary copies. Default true. */
flip: boolean;
/** Fraction of bag width the primary copy fills. Default 0.88. */
primarySize: number;
};
const CLUSTER_TUNE: Record<string, Partial<ImageClusterTune>> = {
// Bouquet portraits — one strong primary with one small supporting copy.
"Lion's Mane": { count: 2, rotate: 10, primarySize: 0.90 },
'Coral Tooth': { count: 2, rotate: 12, primarySize: 0.88 },
'Maitake': { count: 1, rotate: 6, primarySize: 0.95 },
'Turkey Tail': { count: 2, rotate: 25, primarySize: 0.70 },
'Pink Oyster': { count: 2, rotate: 12, primarySize: 0.88 },
// Portraits with substrate/wood baked in — single copy only to avoid
// repeating the visible base.
'Shiitake': { count: 1, rotate: 4, flip: false, primarySize: 0.92 },
'Reishi': { count: 1, rotate: 4, flip: false, primarySize: 0.88 },
'Red Reishi': { count: 1, rotate: 4, flip: false, primarySize: 0.88 },
'Cordyceps': { count: 1, rotate: 2, flip: false, primarySize: 0.92 },
// Small vertical stalk bundles — cluster of 2–3 looks great.
'Enoki': { count: 3, rotate: 10, primarySize: 0.72 },
'White Beech': { count: 2, rotate: 12, primarySize: 0.82 },
'Brown Beech': { count: 2, rotate: 12, primarySize: 0.82 },
// Capped individuals that happen to be drawn as small groups.
'Chestnut Mushroom': { count: 2, rotate: 14, primarySize: 0.80 },
'Pioppino': { count: 2, rotate: 14, primarySize: 0.80 },
'Nameko': { count: 2, rotate: 14, primarySize: 0.80 },
'King Trumpet': { count: 2, rotate: 10, primarySize: 0.86 },
};
const DEFAULT_TUNE: ImageClusterTune = {
count: 2,
rotate: 14,
flip: true,
primarySize: 0.85,
};
export function clusterTuneFor(strainName: string): ImageClusterTune {
return { ...DEFAULT_TUNE, ...(CLUSTER_TUNE[strainName] ?? {}) };
}
/**
* Estimate how far along a bag is through its fruiting window, 0 → 1.
* Used to drive on-bag mushroom image sizing so fruits visibly grow as a
* bag ages toward harvest.
*
* We don't track a per-bag "entered fruiting" timestamp yet, so we use the
* time remaining until the projected harvest date, normalized against a
* nominal fruiting-window length (7 days). Good enough for now; when we
* get a real stage_started_ts per bag this can switch to that.
*/
export function computeFruitingMaturity(
estimatedReadyTs: number,
nowSec: number = Date.now() / 1000,
fruitingWindowDays: number = 7
): number {
const daysToReady = (estimatedReadyTs - nowSec) / 86400;
const t = 1 - daysToReady / fruitingWindowDays;
return t < 0 ? 0 : t > 1 ? 1 : t;
}
const FALLBACK_IMAGE = '/img/species/mycelial_hyphae.webp';
/** Lookup a photo/illustration for this strain. Returns null if none. */
+79
View File
@@ -156,3 +156,82 @@ export const STAGE_LABELS: Record<BagVision['stage'], string> = {
fruiting: 'Fruiting',
harvest: 'Ready',
};
// ── Sensor sweep data ─────────────────────────────────────────────────────
export type GasSweepPoint = {
heater_temp_c: number;
heater_dur_ms: number;
zone: 1 | 2 | 3;
resistance_raw: number;
resistance_corr: number | null; // humidity corrected
resistance_norm: number | null; // baseline normalized
heater_stable: boolean;
valid: boolean;
};
export type ZoneScores = {
Z1: number | null; // baseline zone — humidity/CO2 proxy
Z2: number | null; // biological zone — mycelium/contamination
Z3: number | null; // heavy zone — ammonia/sulfur
};
export type SweepBaseline = {
established: boolean;
established_at: number | null;
n_samples: number;
R: Record<number, number>; // heater_temp_c → baseline resistance
humidity_ref: number;
temp_ref: number;
co2_ref: number | null;
};
export type SensorHealth = {
scd43_present: boolean;
temp_delta: number | null;
humidity_delta: number | null;
temp_ok: boolean;
humidity_ok: boolean;
};
export type EnvPayload = {
ts: number;
chamber_id: string;
ambient: {
temp_c: number;
humidity_pct: number;
pressure_hpa: number;
valid: boolean;
};
co2: {
ppm: number;
valid: boolean;
} | null;
co2e_ppm: number | null; // derived on Linux side
co2e_confidence: 'uncalibrated' | 'low' | 'medium' | 'high';
voc_ppb: number | null; // Zone 2 score scaled
sweep: GasSweepPoint[];
zone_scores: ZoneScores;
baseline: SweepBaseline;
health: SensorHealth;
anomalies: EnvAnomaly[];
};
export type EnvAnomalyType =
| 'z2_deviation' // biological zone shifted from baseline
| 'z3_spike' // ammonia/sulfur detected
| 'co2_threshold' // species CO2 limit exceeded
| 'humidity_drift' // sensor cross-validation failed
| 'temp_drift' // sensor cross-validation failed
| 'baseline_unstable'; // baseline variance too high
export type EnvAnomaly = {
id: string;
type: EnvAnomalyType;
severity: 'info' | 'warn' | 'critical';
chamber_id: string;
message: string; // human-readable, Cultivar-voice
contaminant_hint?: string; // CONT-xxx id if applicable
ts: number;
dismissed: boolean;
};
+1
View File
@@ -134,6 +134,7 @@
strainCode={bag.tag.strain_code}
inoculatedTs={bag.tag.inoculated_ts}
strainName={bag.tag.strain_name}
estimatedReadyTs={bag.vision.projection.estimated_ready_ts}
/>
{#if activeSnapshot && activeSnapshot.contamination_risk !== 'none'}