When we set out to build 50 interactive US remodel & building code calculators covering IRC framing spans, NEC electrical panel loads, NKBA kitchen clearances, IBC commercial occupancy, and 2010 ADA ramp slopes, we made one strict engineering rule: zero round-trip API calls, zero floating-point rounding drift, and a live scaled SVG diagram on every page.
Every calculation runs deterministically in the visitor's browser using Svelte 5 runes ($state and $derived) backed by plain TypeScript rule modules. Here is the exact pattern that let us ship 50 fast, testable calculators without duplicating UI boilerplate or breaking US imperial fractions.
1. Never Format US Feet-and-Inches with Naive toFixed()
Construction math in the United States lives in feet, inches, and sixteenths (1/16"). A classic bug in web calculators is doing feet = Math.floor(totalIn / 12) and inches = Math.round(totalIn % 12) without handling rollover when inches === 12 (producing ugly strings like 9'-12" instead of 10'-0").
Here is the exact rollover-safe imperial formatter we share across all 50 calculators:
export function formatFeetInches(totalInches: number, precisionDenom = 16): string {
const safeInches = Math.max(0, Number.isFinite(totalInches) ? totalInches : 0);
const roundedTicks = Math.round(safeInches * precisionDenom);
const totalWholeInches = Math.floor(roundedTicks / precisionDenom);
const remTicks = roundedTicks % precisionDenom;
const feet = Math.floor(totalWholeInches / 12);
const inches = totalWholeInches % 12;
if (remTicks === 0) {
return `${feet}'-${inches}"`;
}
// Reduce fraction to lowest terms (e.g. 8/16 -> 1/2)
let num = remTicks;
let den = precisionDenom;
while (num % 2 === 0 && den % 2 === 0) {
num /= 2;
den /= 2;
}
return `${feet}'-${inches} ${num}/${den}"`;
}
Edge case check: formatFeetInches(119.99) rounds to 1920 / 16 = 120 whole inches -> 10'-0" cleanly, never 9'-12".
2. Separating Pure Code Rules from Svelte 5 Reactive SVG Views
Every calculator follows a strict two-layer split:
-
Pure TypeScript rule module (
src/lib/calculators/<name>.ts): takes typed inputs, returns structured pass/fail code checks, material quantities, and SVG coordinate geometry. Zero DOM or Svelte imports, so we test boundaries directly with Node's built-in test runner (node --experimental-strip-types --test). -
Thin Svelte 5 page (
src/routes/calculators/<slug>/+page.svelte): binds sliders/number inputs via$stateand computes both the readout table and the scaled<svg viewBox="...">via$derived.
export interface ClearCheck {
codeRef: string;
label: string;
actualIn: number;
requiredIn: number;
pass: boolean;
}
export function evaluateBathroomClearances(
toiletCenterlineIn: number,
toiletFrontClearIn: number,
doorWidthIn: number
): ClearCheck[] {
return [
{
codeRef: 'IRC R307.1',
label: 'Toilet Centerline to Side Wall / Vanity',
actualIn: toiletCenterlineIn,
requiredIn: 15,
pass: toiletCenterlineIn >= 15,
},
{
codeRef: 'IRC R307.1',
label: 'Clear Floor Space in Front of Toilet',
actualIn: toiletFrontClearIn,
requiredIn: 21,
pass: toiletFrontClearIn >= 21,
},
{
codeRef: 'NKBA / Common Practice',
label: 'Bathroom Door Leaf Width',
actualIn: doorWidthIn,
requiredIn: 28,
pass: doorWidthIn >= 28,
},
];
}
Because the calculation function is pure and synchronous, dragging a slider updates the pass/fail badge, dimension leader lines, and material takeoff at 60 FPS with zero network latency or Cloudflare Worker CPU overhead.
3. Turning Calculator Inputs Straight into a 2D/3D Floor Plan
The biggest flaw with standalone web calculators is that once a homeowner or contractor confirms a span or clearance, they still have to re-draw the room in CAD from scratch. Every calculator in our 50 Free US Code Calculators Hub hands off cleanly into matching pre-scaled CAD templates (for example, our Electrical Panel Load Calculator, Commercial Office Occupancy Calculator, and ADA Wheelchair Ramp Slope Calculator).
Inside PlanCaliber, drafting in 2D/3D, running all 50 calculators, opening all 40 US templates, uploading up to 2 sketches/PDFs per day, and saving a watermarked proof PNG are completely free. When you need clean deliverables for a bid or review, you unlock once with no subscription: $9 Print Pass (unwatermarked 2-sheet vector PDF + 2400px PNG + SVG + unbranded 3D share link) or $19 Single Plan (5-sheet 1/4"=1'-0" vector PDF + 8-layer AutoCAD R12 .DXF + 3D .OBJ + CSV takeoff, backed by a 14-day instant no-questions-asked refund).
Note: All 50 calculators and PlanCaliber floor plan exports are built for preliminary space planning, feasibility checks, and estimating only. Always field-verify dimensions on site and confirm code requirements with your local Authority Having Jurisdiction (AHJ).
Top comments (0)