@bitbybit-dev/create-app 1.3.2-rc.1 → 1.4.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +47 -124
- package/dist/index.js +253 -21
- package/dist/index.js.map +1 -1
- package/dist/scaffold.d.ts +30 -0
- package/dist/scaffold.d.ts.map +1 -0
- package/dist/scaffold.js +107 -0
- package/dist/scaffold.js.map +1 -0
- package/package.json +4 -3
- package/src/index.test.ts +309 -8
- package/src/index.ts +285 -24
- package/src/scaffold.test.ts +177 -0
- package/src/scaffold.ts +99 -0
- package/templates/_agent/AGENTS.md +39 -0
- package/templates/_agent/CLAUDE.md +1 -0
- package/templates/_agent/_dot-cursor/mcp.json +5 -0
- package/templates/_agent/_dot-gitignore +7 -0
- package/templates/_agent/_dot-mcp.json +5 -0
- package/templates/_agent/_dot-vscode/mcp.json +5 -0
- package/templates/apps/drone-assembly/AGENTS.section.md +20 -0
- package/templates/apps/drone-assembly/README.md +35 -0
- package/templates/apps/drone-assembly/eslint.config.js +47 -0
- package/templates/apps/drone-assembly/index.html +17 -0
- package/templates/apps/drone-assembly/package.json +31 -0
- package/templates/apps/drone-assembly/public/favicon.ico +0 -0
- package/templates/apps/drone-assembly/public/logo.png +0 -0
- package/templates/apps/drone-assembly/scripts/smoke.ts +172 -0
- package/templates/apps/drone-assembly/src/assembly.ts +99 -0
- package/templates/apps/drone-assembly/src/exports.ts +10 -0
- package/templates/apps/drone-assembly/src/kernel.ts +14 -0
- package/templates/apps/drone-assembly/src/main.ts +298 -0
- package/templates/apps/drone-assembly/src/materials.ts +58 -0
- package/templates/apps/drone-assembly/src/model.ts +280 -0
- package/templates/apps/drone-assembly/src/parts.ts +343 -0
- package/templates/apps/drone-assembly/src/style.css +219 -0
- package/templates/apps/drone-assembly/src/ui.ts +180 -0
- package/templates/apps/drone-assembly/src/vite-env.d.ts +1 -0
- package/templates/apps/drone-assembly/tsconfig.json +29 -0
- package/templates/apps/laser-cut-box/AGENTS.section.md +17 -0
- package/templates/apps/laser-cut-box/README.md +34 -0
- package/templates/apps/laser-cut-box/eslint.config.js +47 -0
- package/templates/apps/laser-cut-box/index.html +17 -0
- package/templates/apps/laser-cut-box/package.json +30 -0
- package/templates/apps/laser-cut-box/public/favicon.ico +0 -0
- package/templates/apps/laser-cut-box/public/logo.png +0 -0
- package/templates/apps/laser-cut-box/scripts/smoke.ts +106 -0
- package/templates/apps/laser-cut-box/src/exports.ts +28 -0
- package/templates/apps/laser-cut-box/src/main.ts +176 -0
- package/templates/apps/laser-cut-box/src/model.ts +92 -0
- package/templates/apps/laser-cut-box/src/panels.ts +229 -0
- package/templates/apps/laser-cut-box/src/style.css +223 -0
- package/templates/apps/laser-cut-box/src/ui.ts +136 -0
- package/templates/apps/laser-cut-box/src/vite-env.d.ts +1 -0
- package/templates/apps/laser-cut-box/tsconfig.json +29 -0
- package/templates/apps/product-configurator/AGENTS.section.md +19 -0
- package/templates/apps/product-configurator/README.md +33 -0
- package/templates/apps/product-configurator/eslint.config.js +47 -0
- package/templates/apps/product-configurator/index.html +17 -0
- package/templates/apps/product-configurator/package.json +30 -0
- package/templates/apps/product-configurator/public/favicon.ico +0 -0
- package/templates/apps/product-configurator/public/logo.png +0 -0
- package/templates/apps/product-configurator/scripts/smoke.ts +69 -0
- package/templates/apps/product-configurator/src/catalog.ts +56 -0
- package/templates/apps/product-configurator/src/exports.ts +19 -0
- package/templates/apps/product-configurator/src/main.ts +134 -0
- package/templates/apps/product-configurator/src/model.ts +153 -0
- package/templates/apps/product-configurator/src/style.css +177 -0
- package/templates/apps/product-configurator/src/ui.ts +150 -0
- package/templates/apps/product-configurator/src/vite-env.d.ts +1 -0
- package/templates/apps/product-configurator/tsconfig.json +29 -0
- package/templates/apps/sheet-metal-unfold/AGENTS.section.md +26 -0
- package/templates/apps/sheet-metal-unfold/README.md +41 -0
- package/templates/apps/sheet-metal-unfold/backend/_dot-env.example +5 -0
- package/templates/apps/sheet-metal-unfold/backend/eslint.config.js +47 -0
- package/templates/apps/sheet-metal-unfold/backend/package.json +28 -0
- package/templates/apps/sheet-metal-unfold/backend/scripts/smoke.ts +52 -0
- package/templates/apps/sheet-metal-unfold/backend/src/app.ts +65 -0
- package/templates/apps/sheet-metal-unfold/backend/src/cloud.ts +42 -0
- package/templates/apps/sheet-metal-unfold/backend/src/index.ts +13 -0
- package/templates/apps/sheet-metal-unfold/backend/tsconfig.build.json +9 -0
- package/templates/apps/sheet-metal-unfold/backend/tsconfig.json +27 -0
- package/templates/apps/sheet-metal-unfold/frontend/eslint.config.js +47 -0
- package/templates/apps/sheet-metal-unfold/frontend/index.html +17 -0
- package/templates/apps/sheet-metal-unfold/frontend/package.json +30 -0
- package/templates/apps/sheet-metal-unfold/frontend/public/favicon.ico +0 -0
- package/templates/apps/sheet-metal-unfold/frontend/public/logo.png +0 -0
- package/templates/apps/sheet-metal-unfold/frontend/scripts/smoke.ts +84 -0
- package/templates/apps/sheet-metal-unfold/frontend/src/cloud-access.ts +37 -0
- package/templates/apps/sheet-metal-unfold/frontend/src/exports.ts +10 -0
- package/templates/apps/sheet-metal-unfold/frontend/src/flat.ts +71 -0
- package/templates/apps/sheet-metal-unfold/frontend/src/main.ts +193 -0
- package/templates/apps/sheet-metal-unfold/frontend/src/model.ts +166 -0
- package/templates/apps/sheet-metal-unfold/frontend/src/style.css +291 -0
- package/templates/apps/sheet-metal-unfold/frontend/src/ui.ts +150 -0
- package/templates/apps/sheet-metal-unfold/frontend/src/vite-env.d.ts +1 -0
- package/templates/apps/sheet-metal-unfold/frontend/tsconfig.json +29 -0
- package/templates/apps/sheet-metal-unfold/frontend/vite.config.ts +9 -0
- package/templates/apps/sheet-metal-unfold/package.json +18 -0
- package/templates/apps/step-to-gltf-cli/AGENTS.section.md +20 -0
- package/templates/apps/step-to-gltf-cli/README.md +34 -0
- package/templates/apps/step-to-gltf-cli/eslint.config.js +47 -0
- package/templates/apps/step-to-gltf-cli/package.json +26 -0
- package/templates/apps/step-to-gltf-cli/samples/bracket.step +2545 -0
- package/templates/apps/step-to-gltf-cli/scripts/smoke.ts +49 -0
- package/templates/apps/step-to-gltf-cli/src/cli.ts +106 -0
- package/templates/apps/step-to-gltf-cli/src/cloud.ts +34 -0
- package/templates/apps/step-to-gltf-cli/src/convert.ts +65 -0
- package/templates/apps/step-to-gltf-cli/src/kernel.ts +14 -0
- package/templates/apps/step-to-gltf-cli/tsconfig.json +27 -0
- package/templates/cloud/AGENTS.section.md +22 -0
- package/templates/cloud/backends/dotnet-rest/BitbybitClient.cs +22 -50
- package/templates/cloud/backends/dotnet-rest/Program.cs +1 -11
- package/templates/cloud/backends/dotnet-rest/dotnet-rest.csproj +4 -1
- package/templates/cloud/backends/hono-rest/eslint.config.js +47 -0
- package/templates/cloud/backends/hono-rest/package.json +11 -4
- package/templates/cloud/backends/hono-rest/src/bitbybit-client.ts +34 -45
- package/templates/cloud/backends/hono-rest/src/index.ts +1 -11
- package/templates/cloud/backends/hono-rest/tsconfig.json +19 -8
- package/templates/cloud/backends/hono-sdk/eslint.config.js +47 -0
- package/templates/cloud/backends/hono-sdk/package.json +12 -5
- package/templates/cloud/backends/hono-sdk/src/bitbybit-client.ts +6 -37
- package/templates/cloud/backends/hono-sdk/src/index.ts +1 -12
- package/templates/cloud/backends/hono-sdk/tsconfig.json +19 -8
- package/templates/cloud/backends/nodejs-rest/eslint.config.js +47 -0
- package/templates/cloud/backends/nodejs-rest/package.json +12 -5
- package/templates/cloud/backends/nodejs-rest/src/bitbybit-client.ts +6 -35
- package/templates/cloud/backends/nodejs-rest/src/index.ts +6 -16
- package/templates/cloud/backends/nodejs-rest/tsconfig.build.json +9 -0
- package/templates/cloud/backends/nodejs-rest/tsconfig.json +17 -7
- package/templates/cloud/backends/nodejs-sdk/eslint.config.js +47 -0
- package/templates/cloud/backends/nodejs-sdk/package.json +14 -7
- package/templates/cloud/backends/nodejs-sdk/src/bitbybit-client.ts +8 -33
- package/templates/cloud/backends/nodejs-sdk/src/index.ts +5 -16
- package/templates/cloud/backends/nodejs-sdk/tsconfig.build.json +9 -0
- package/templates/cloud/backends/nodejs-sdk/tsconfig.json +19 -9
- package/templates/cloud/frontend/eslint.config.js +50 -0
- package/templates/cloud/frontend/index.html +3 -1
- package/templates/cloud/frontend/package.json +13 -4
- package/templates/cloud/frontend/public/favicon.ico +0 -0
- package/templates/cloud/frontend/public/logo.png +0 -0
- package/templates/cloud/frontend/src/App.tsx +11 -9
- package/templates/cloud/frontend/src/api.ts +31 -0
- package/templates/cloud/frontend/src/components/ApiKeyWarning.tsx +28 -25
- package/templates/cloud/frontend/src/components/Header.tsx +4 -2
- package/templates/cloud/frontend/src/components/Tabs.tsx +4 -3
- package/templates/cloud/frontend/src/components/Viewer.tsx +57 -77
- package/templates/cloud/frontend/src/main.tsx +6 -1
- package/templates/cloud/frontend/src/panels/ModelsPanel.tsx +65 -73
- package/templates/cloud/frontend/src/panels/PipelinesPanel.tsx +82 -83
- package/templates/cloud/frontend/src/vite-env.d.ts +1 -0
- package/templates/cloud/frontend/tsconfig.json +19 -7
- package/templates/vite/babylonjs/typescript/AGENTS.section.md +13 -0
- package/templates/vite/babylonjs/typescript/eslint.config.js +47 -0
- package/templates/vite/babylonjs/typescript/index.html +4 -2
- package/templates/vite/babylonjs/typescript/package.json +14 -4
- package/templates/vite/babylonjs/typescript/public/favicon.ico +0 -0
- package/templates/vite/babylonjs/typescript/public/logo.png +0 -0
- package/templates/vite/babylonjs/typescript/scripts/smoke.ts +41 -0
- package/templates/vite/babylonjs/typescript/src/main.ts +8 -17
- package/templates/vite/babylonjs/typescript/src/model.ts +24 -0
- package/templates/vite/babylonjs/typescript/tsconfig.json +16 -12
- package/templates/vite/playcanvas/typescript/AGENTS.section.md +13 -0
- package/templates/vite/playcanvas/typescript/eslint.config.js +47 -0
- package/templates/vite/playcanvas/typescript/index.html +4 -2
- package/templates/vite/playcanvas/typescript/package.json +14 -4
- package/templates/vite/playcanvas/typescript/public/favicon.ico +0 -0
- package/templates/vite/playcanvas/typescript/public/logo.png +0 -0
- package/templates/vite/playcanvas/typescript/scripts/smoke.ts +41 -0
- package/templates/vite/playcanvas/typescript/src/main.ts +11 -22
- package/templates/vite/playcanvas/typescript/src/model.ts +24 -0
- package/templates/vite/playcanvas/typescript/tsconfig.json +16 -12
- package/templates/vite/threejs/typescript/AGENTS.section.md +13 -0
- package/templates/vite/threejs/typescript/eslint.config.js +47 -0
- package/templates/vite/threejs/typescript/index.html +4 -2
- package/templates/vite/threejs/typescript/package.json +16 -6
- package/templates/vite/threejs/typescript/public/favicon.ico +0 -0
- package/templates/vite/threejs/typescript/public/logo.png +0 -0
- package/templates/vite/threejs/typescript/scripts/smoke.ts +41 -0
- package/templates/vite/threejs/typescript/src/main.ts +11 -22
- package/templates/vite/threejs/typescript/src/model.ts +24 -0
- package/templates/vite/threejs/typescript/tsconfig.json +16 -12
- package/templates/vite/babylonjs/typescript/public/vite.svg +0 -1
- package/templates/vite/playcanvas/typescript/public/vite.svg +0 -1
- package/templates/vite/threejs/typescript/public/vite.svg +0 -1
- /package/templates/cloud/backends/dotnet-rest/{_gitignore → _dot-gitignore} +0 -0
- /package/templates/cloud/backends/hono-rest/{_gitignore → _dot-gitignore} +0 -0
- /package/templates/cloud/backends/hono-sdk/{_gitignore → _dot-gitignore} +0 -0
- /package/templates/cloud/backends/nodejs-rest/{.env.example → _dot-env.example} +0 -0
- /package/templates/cloud/backends/nodejs-rest/{_gitignore → _dot-gitignore} +0 -0
- /package/templates/cloud/backends/nodejs-sdk/{.env.example → _dot-env.example} +0 -0
- /package/templates/cloud/backends/nodejs-sdk/{_gitignore → _dot-gitignore} +0 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { MaterialId, PlanterModel, PlanterParams } from "./model";
|
|
2
|
+
|
|
3
|
+
export interface Material {
|
|
4
|
+
label: string;
|
|
5
|
+
pricePerLitre: number;
|
|
6
|
+
colour: string;
|
|
7
|
+
|
|
8
|
+
roughness: number;
|
|
9
|
+
|
|
10
|
+
clearcoat: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const MATERIALS: Record<MaterialId, Material> = {
|
|
14
|
+
terracotta: { label: "Terracotta", pricePerLitre: 6.5, colour: "#b5583a", roughness: 0.85, clearcoat: 0 },
|
|
15
|
+
concrete: { label: "Concrete", pricePerLitre: 4.0, colour: "#8d8d86", roughness: 0.95, clearcoat: 0 },
|
|
16
|
+
oak: { label: "Oak", pricePerLitre: 14.0, colour: "#c8a36b", roughness: 0.5, clearcoat: 0.4 },
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const PRESETS = {
|
|
20
|
+
windowsill: { label: "Windowsill", params: { width: 40, depth: 14, height: 12, wallThickness: 1, cornerRadius: 2, drainageHoles: 3, material: "terracotta" } },
|
|
21
|
+
balcony: { label: "Balcony", params: { width: 60, depth: 24, height: 22, wallThickness: 1.5, cornerRadius: 4, drainageHoles: 4, material: "concrete" } },
|
|
22
|
+
patio: { label: "Patio", params: { width: 90, depth: 40, height: 40, wallThickness: 2.5, cornerRadius: 6, drainageHoles: 6, material: "oak" } },
|
|
23
|
+
} satisfies Record<string, { label: string; params: PlanterParams }>;
|
|
24
|
+
|
|
25
|
+
export type PresetId = keyof typeof PRESETS;
|
|
26
|
+
|
|
27
|
+
export const CURRENCY = "EUR";
|
|
28
|
+
export const BASE_PRICE = 12;
|
|
29
|
+
export const FINISH_PER_SQUARE_METRE = 18;
|
|
30
|
+
export const HOLE_SURCHARGE = 0.5;
|
|
31
|
+
|
|
32
|
+
export interface Quote {
|
|
33
|
+
litres: number;
|
|
34
|
+
material: number;
|
|
35
|
+
finish: number;
|
|
36
|
+
holes: number;
|
|
37
|
+
total: number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function quoteFor(params: PlanterParams, model: Pick<PlanterModel, "volume" | "surfaceArea">): Quote {
|
|
41
|
+
const litres = model.volume / 1000;
|
|
42
|
+
const material = MATERIALS[params.material].pricePerLitre * litres;
|
|
43
|
+
const finish = (model.surfaceArea / 10000) * FINISH_PER_SQUARE_METRE;
|
|
44
|
+
const holes = params.drainageHoles * HOLE_SURCHARGE;
|
|
45
|
+
const total = BASE_PRICE + material + finish + holes;
|
|
46
|
+
return { litres: round(litres, 2), material: round(material), finish: round(finish), holes: round(holes), total: round(total) };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function formatPrice(amount: number): string {
|
|
50
|
+
return new Intl.NumberFormat("en", { style: "currency", currency: CURRENCY }).format(amount);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function round(value: number, digits = 2): number {
|
|
54
|
+
const factor = 10 ** digits;
|
|
55
|
+
return Math.round(value * factor) / factor;
|
|
56
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type * as THREE from "three";
|
|
2
|
+
import { GLTFExporter } from "three/addons/exporters/GLTFExporter.js";
|
|
3
|
+
|
|
4
|
+
export function downloadBlob(blob: Blob, fileName: string): void {
|
|
5
|
+
const url = URL.createObjectURL(blob);
|
|
6
|
+
const link = document.createElement("a");
|
|
7
|
+
link.href = url;
|
|
8
|
+
link.download = fileName;
|
|
9
|
+
document.body.appendChild(link);
|
|
10
|
+
link.click();
|
|
11
|
+
link.remove();
|
|
12
|
+
setTimeout(() => { URL.revokeObjectURL(url); }, 1000);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function downloadGlb(object: THREE.Object3D, fileName: string): Promise<void> {
|
|
16
|
+
const exporter = new GLTFExporter();
|
|
17
|
+
const glb = await exporter.parseAsync(object, { binary: true });
|
|
18
|
+
downloadBlob(new Blob([glb as ArrayBuffer], { type: "model/gltf-binary" }), fileName);
|
|
19
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import "./style.css";
|
|
2
|
+
import type * as THREE from "three";
|
|
3
|
+
import { ACESFilmicToneMapping, Color, MeshPhysicalMaterial, PMREMGenerator, Vector3 } from "three";
|
|
4
|
+
import { RoomEnvironment } from "three/addons/environments/RoomEnvironment.js";
|
|
5
|
+
import { BitByBitBase, Inputs, initBitByBit, initThreeJS, type InitBitByBitOptions } from "@bitbybit-dev/threejs";
|
|
6
|
+
import { MATERIALS, quoteFor } from "./catalog";
|
|
7
|
+
import { downloadGlb } from "./exports";
|
|
8
|
+
import { buildModel, defaultParams, type MaterialId, type PlanterModel, type PlanterParams } from "./model";
|
|
9
|
+
import { mountPoint, mountUi, type ExportFormat } from "./ui";
|
|
10
|
+
|
|
11
|
+
void start();
|
|
12
|
+
|
|
13
|
+
function isMesh(object: THREE.Object3D): object is THREE.Mesh {
|
|
14
|
+
return "isMesh" in object && object.isMesh === true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function cameraOptions(sceneSize: number): Inputs.ThreeJSCamera.OrbitCameraDto {
|
|
18
|
+
const camera = new Inputs.ThreeJSCamera.OrbitCameraDto();
|
|
19
|
+
camera.distance = sceneSize * Math.SQRT2;
|
|
20
|
+
camera.distanceMin = sceneSize * 0.02;
|
|
21
|
+
camera.distanceMax = sceneSize * 10;
|
|
22
|
+
return camera;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function start(): Promise<void> {
|
|
26
|
+
const sceneOptions = new Inputs.ThreeJSScene.InitThreeJSDto();
|
|
27
|
+
sceneOptions.canvasId = "three-canvas";
|
|
28
|
+
sceneOptions.sceneSize = 120;
|
|
29
|
+
sceneOptions.enableGround = true;
|
|
30
|
+
sceneOptions.groundColor = "#2a2d31";
|
|
31
|
+
sceneOptions.enableShadows = true;
|
|
32
|
+
sceneOptions.orbitCameraOptions = cameraOptions(sceneOptions.sceneSize);
|
|
33
|
+
const { scene, renderer, orbitCamera, startAnimationLoop } = initThreeJS(sceneOptions);
|
|
34
|
+
startAnimationLoop();
|
|
35
|
+
|
|
36
|
+
renderer.toneMapping = ACESFilmicToneMapping;
|
|
37
|
+
renderer.toneMappingExposure = 1.05;
|
|
38
|
+
scene.environment = new PMREMGenerator(renderer).fromScene(new RoomEnvironment(), 0.04).texture;
|
|
39
|
+
scene.environmentIntensity = 0.45;
|
|
40
|
+
const materials = new Map<MaterialId, MeshPhysicalMaterial>();
|
|
41
|
+
|
|
42
|
+
const bitbybit = new BitByBitBase();
|
|
43
|
+
const options: InitBitByBitOptions = {
|
|
44
|
+
enableOCCT: true,
|
|
45
|
+
enableJSCAD: false,
|
|
46
|
+
enableManifold: false,
|
|
47
|
+
};
|
|
48
|
+
await initBitByBit(scene, bitbybit, options);
|
|
49
|
+
|
|
50
|
+
let params: PlanterParams = { ...defaultParams };
|
|
51
|
+
let current: { model: PlanterModel; drawn: THREE.Group } | undefined;
|
|
52
|
+
let generation = 0;
|
|
53
|
+
let timer: number | undefined;
|
|
54
|
+
let framedFor = 0;
|
|
55
|
+
|
|
56
|
+
const ui = mountUi(mountPoint("panel"), params, {
|
|
57
|
+
onChange: (next) => {
|
|
58
|
+
params = next;
|
|
59
|
+
window.clearTimeout(timer);
|
|
60
|
+
timer = window.setTimeout(() => void rebuild(), 120);
|
|
61
|
+
},
|
|
62
|
+
onExport: (format) => void exportModel(format),
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
async function rebuild(): Promise<void> {
|
|
66
|
+
const mine = ++generation;
|
|
67
|
+
ui.setStatus("Building...");
|
|
68
|
+
try {
|
|
69
|
+
const model = await buildModel(bitbybit.occt, params);
|
|
70
|
+
if (mine !== generation) return;
|
|
71
|
+
const drawOptions = new Inputs.Draw.DrawOcctShapeOptions();
|
|
72
|
+
drawOptions.faceMaterial = materialFor(params.material);
|
|
73
|
+
drawOptions.drawEdges = false;
|
|
74
|
+
drawOptions.drawTwoSided = false;
|
|
75
|
+
drawOptions.precision = 0.02;
|
|
76
|
+
const drawn = await bitbybit.draw.drawAnyAsync({ entity: model.shape, options: drawOptions });
|
|
77
|
+
if (mine !== generation) { dispose(drawn); return; }
|
|
78
|
+
if (current) dispose(current.drawn);
|
|
79
|
+
current = { model, drawn };
|
|
80
|
+
frame();
|
|
81
|
+
ui.setQuote(quoteFor(params, model));
|
|
82
|
+
ui.setStatus(`${model.volume.toFixed(0)} cm³ of ${MATERIALS[params.material].label.toLowerCase()}, ${(model.surfaceArea / 10000).toFixed(2)} m² of surface`);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
ui.setStatus(`Could not build this planter: ${error instanceof Error ? error.message : String(error)}`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function exportModel(format: ExportFormat): Promise<void> {
|
|
89
|
+
if (!current) return;
|
|
90
|
+
const name = `planter-${params.width}x${params.depth}x${params.height}`;
|
|
91
|
+
ui.setStatus(`Exporting ${format.toUpperCase()}...`);
|
|
92
|
+
if (format === "step") {
|
|
93
|
+
await bitbybit.occt.io.saveShapeSTEP({ shape: current.model.shape, fileName: `${name}.step`, adjustYtoZ: true, tryDownload: true });
|
|
94
|
+
} else if (format === "stl") {
|
|
95
|
+
await bitbybit.occt.io.saveShapeStl({ shape: current.model.shape, fileName: `${name}.stl`, precision: 0.01, adjustYtoZ: true, tryDownload: true, binary: true });
|
|
96
|
+
} else {
|
|
97
|
+
await downloadGlb(current.drawn, `${name}.glb`);
|
|
98
|
+
}
|
|
99
|
+
ui.setStatus(`${format.toUpperCase()} downloaded.`);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function materialFor(id: MaterialId): MeshPhysicalMaterial {
|
|
103
|
+
let material = materials.get(id);
|
|
104
|
+
if (!material) {
|
|
105
|
+
const spec = MATERIALS[id];
|
|
106
|
+
material = new MeshPhysicalMaterial({ color: new Color(spec.colour), roughness: spec.roughness, metalness: 0, clearcoat: spec.clearcoat, clearcoatRoughness: 0.2 });
|
|
107
|
+
materials.set(id, material);
|
|
108
|
+
}
|
|
109
|
+
return material;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function frame(): void {
|
|
113
|
+
const extent = Math.max(params.width, params.depth, params.height);
|
|
114
|
+
if (!orbitCamera || (framedFor > 0 && Math.abs(extent - framedFor) / framedFor < 0.4)) return;
|
|
115
|
+
framedFor = extent;
|
|
116
|
+
const distance = extent * 2.4;
|
|
117
|
+
const target = new Vector3(0, params.height / 2, 0);
|
|
118
|
+
orbitCamera.orbitCamera.resetAndLookAtPoint(new Vector3(distance * 0.6, target.y + distance * 0.45, distance * 0.6), target);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function dispose(group: THREE.Group): void {
|
|
122
|
+
scene.remove(group);
|
|
123
|
+
const shared = new Set<THREE.Material>(materials.values());
|
|
124
|
+
group.traverse((object) => {
|
|
125
|
+
if (!isMesh(object)) return;
|
|
126
|
+
object.geometry.dispose();
|
|
127
|
+
for (const material of Array.isArray(object.material) ? object.material : [object.material]) {
|
|
128
|
+
if (!shared.has(material)) material.dispose();
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
await rebuild();
|
|
134
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import type { BitByBitBase } from "@bitbybit-dev/threejs";
|
|
2
|
+
|
|
3
|
+
export type Occt = BitByBitBase["occt"];
|
|
4
|
+
|
|
5
|
+
export type MaterialId = "terracotta" | "concrete" | "oak";
|
|
6
|
+
|
|
7
|
+
export interface PlanterParams {
|
|
8
|
+
width: number;
|
|
9
|
+
depth: number;
|
|
10
|
+
height: number;
|
|
11
|
+
wallThickness: number;
|
|
12
|
+
cornerRadius: number;
|
|
13
|
+
drainageHoles: number;
|
|
14
|
+
material: MaterialId;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface PlanterModel {
|
|
18
|
+
shape: Awaited<ReturnType<Occt["booleans"]["difference"]>>;
|
|
19
|
+
volume: number;
|
|
20
|
+
surfaceArea: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type Shape = Awaited<ReturnType<Occt["operations"]["extrude"]>>;
|
|
24
|
+
|
|
25
|
+
export const UNITS = "cm";
|
|
26
|
+
|
|
27
|
+
export const LIMITS = {
|
|
28
|
+
width: { min: 10, max: 120, step: 1 },
|
|
29
|
+
depth: { min: 10, max: 80, step: 1 },
|
|
30
|
+
height: { min: 8, max: 80, step: 1 },
|
|
31
|
+
wallThickness: { min: 0.6, max: 4, step: 0.1 },
|
|
32
|
+
cornerRadius: { min: 0, max: 20, step: 0.5 },
|
|
33
|
+
drainageHoles: { min: 0, max: 9, step: 1 },
|
|
34
|
+
} as const;
|
|
35
|
+
|
|
36
|
+
export const HOLE_RADIUS = 0.8;
|
|
37
|
+
|
|
38
|
+
export const STYLE = { taperAngle: 4, lipHeight: 2.2, lipStep: 0.6, rimRadius: 0.5, footInset: 3, footDepth: 1.5 } as const;
|
|
39
|
+
|
|
40
|
+
export const SMALLEST_DRAFTED_CORNER = 1;
|
|
41
|
+
|
|
42
|
+
const draftedCorner = (radius: number): number => (radius >= SMALLEST_DRAFTED_CORNER ? radius : 0);
|
|
43
|
+
|
|
44
|
+
export const defaultParams: PlanterParams = {
|
|
45
|
+
width: 40,
|
|
46
|
+
depth: 20,
|
|
47
|
+
height: 18,
|
|
48
|
+
wallThickness: 1.2,
|
|
49
|
+
cornerRadius: 3,
|
|
50
|
+
drainageHoles: 3,
|
|
51
|
+
material: "terracotta",
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export function clampParams(params: PlanterParams): PlanterParams {
|
|
55
|
+
const clamp = (value: number, limit: { min: number; max: number }): number => Math.min(Math.max(value, limit.min), limit.max);
|
|
56
|
+
const width = clamp(params.width, LIMITS.width);
|
|
57
|
+
const depth = clamp(params.depth, LIMITS.depth);
|
|
58
|
+
const height = clamp(params.height, LIMITS.height);
|
|
59
|
+
const smallestSide = Math.min(width, depth);
|
|
60
|
+
const wallThickness = Math.min(clamp(params.wallThickness, LIMITS.wallThickness), smallestSide / 4);
|
|
61
|
+
const cornerRadius = Math.min(clamp(params.cornerRadius, LIMITS.cornerRadius), smallestSide / 2 - wallThickness - STYLE.lipStep);
|
|
62
|
+
const drainageHoles = Math.round(clamp(params.drainageHoles, LIMITS.drainageHoles));
|
|
63
|
+
return { ...params, width, depth, height, wallThickness, cornerRadius, drainageHoles };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function taperAngle(p: PlanterParams): number {
|
|
67
|
+
const wallHeight = p.height - STYLE.lipHeight;
|
|
68
|
+
const largest = (Math.atan((0.25 * Math.min(p.width, p.depth)) / wallHeight) * 180) / Math.PI;
|
|
69
|
+
return Math.min(STYLE.taperAngle, largest);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function taperInset(p: PlanterParams): number {
|
|
73
|
+
return STYLE.lipStep + (p.height - STYLE.lipHeight) * Math.tan((taperAngle(p) * Math.PI) / 180);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function bottomSize(p: PlanterParams): { width: number; depth: number } {
|
|
77
|
+
const inset = taperInset(p);
|
|
78
|
+
return { width: p.width - 2 * inset, depth: p.depth - 2 * inset };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function buildModel(occt: Occt, params: PlanterParams): Promise<PlanterModel> {
|
|
82
|
+
const p = clampParams(params);
|
|
83
|
+
const t = p.wallThickness;
|
|
84
|
+
const s = STYLE.lipStep;
|
|
85
|
+
const wallHeight = p.height - STYLE.lipHeight;
|
|
86
|
+
const angle = taperAngle(p);
|
|
87
|
+
const walls = await tapered(occt, await roundedBlock(occt, p.width - 2 * s, p.depth - 2 * s, draftedCorner(p.cornerRadius - s), 0, wallHeight), angle, wallHeight);
|
|
88
|
+
const lip = await roundedBlock(occt, p.width, p.depth, p.cornerRadius, wallHeight, STYLE.lipHeight);
|
|
89
|
+
const outer = await occt.booleans.union({ shapes: [walls, lip], keepEdges: false });
|
|
90
|
+
const cavity = await tapered(occt, await roundedBlock(occt, p.width - 2 * (t + s), p.depth - 2 * (t + s), draftedCorner(p.cornerRadius - t - s), t, p.height), angle, wallHeight);
|
|
91
|
+
const hollow = await occt.booleans.difference({ shape: outer, shapes: [cavity], keepEdges: false });
|
|
92
|
+
const rimmed = await roundRim(occt, hollow, p.height);
|
|
93
|
+
const bottom = bottomSize(p);
|
|
94
|
+
const footDepth = Math.min(STYLE.footDepth, t / 2);
|
|
95
|
+
const recess = await roundedBlock(occt, bottom.width - 2 * STYLE.footInset, bottom.depth - 2 * STYLE.footInset, Math.max(p.cornerRadius - taperInset(p) - STYLE.footInset, 0), -1, 1 + footDepth);
|
|
96
|
+
const shape = await occt.booleans.difference({ shape: rimmed, shapes: [recess, ...await drainageHoles(occt, p)], keepEdges: false });
|
|
97
|
+
const volume = await occt.shapes.solid.getSolidVolume({ shape });
|
|
98
|
+
const surfaceArea = await occt.shapes.solid.getSolidSurfaceArea({ shape });
|
|
99
|
+
return { shape, volume, surfaceArea };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async function roundedBlock(occt: Occt, width: number, depth: number, radius: number, bottom: number, height: number): Promise<Shape> {
|
|
103
|
+
const rectangle = await occt.shapes.wire.createRectangleWire({ width, length: depth, center: [0, bottom, 0], direction: [0, 1, 0] });
|
|
104
|
+
const outline = radius > 0 ? await occt.fillets.fillet2d({ shape: rectangle, radius }) : rectangle;
|
|
105
|
+
const face = await occt.shapes.face.createFaceFromWire({ shape: outline, planar: true });
|
|
106
|
+
return occt.operations.extrude({ shape: face, direction: [0, height, 0] });
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function tapered(occt: Occt, block: Shape, angle: number, top: number): Promise<Shape> {
|
|
110
|
+
if (angle <= 0) return block;
|
|
111
|
+
const faces = await occt.shapes.face.getFaces({ shape: block });
|
|
112
|
+
const sides = [];
|
|
113
|
+
for (const face of faces) {
|
|
114
|
+
const normal = await occt.shapes.face.normalOnUV({ shape: face, paramU: 0.5, paramV: 0.5 });
|
|
115
|
+
if (Math.abs(normal[1]) < 0.5) sides.push(face);
|
|
116
|
+
}
|
|
117
|
+
return occt.draft.draftAngle({ shape: block, faces: sides, direction: [0, 1, 0], angle: -angle, neutralPlaneOrigin: [0, top, 0], neutralPlaneDirection: [0, 1, 0], flag: true });
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function roundRim(occt: Occt, shape: Shape, top: number): Promise<Shape> {
|
|
121
|
+
const edges = await occt.shapes.edge.getEdges({ shape });
|
|
122
|
+
const centers = await occt.shapes.edge.getEdgesCentersOfMass({ shapes: edges });
|
|
123
|
+
const indexes = centers.map((center, index) => (Math.abs(center[1] - top) < 1e-4 ? index : -1)).filter((index) => index >= 0);
|
|
124
|
+
return occt.fillets.filletEdges({ shape, radius: STYLE.rimRadius, indexes });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function drainageHoleCenters(params: PlanterParams): [number, number, number][] {
|
|
128
|
+
const count = params.drainageHoles;
|
|
129
|
+
if (count === 0) return [];
|
|
130
|
+
const bottom = bottomSize(params);
|
|
131
|
+
const inset = params.wallThickness + STYLE.footInset + HOLE_RADIUS * 2;
|
|
132
|
+
const usableWidth = Math.max(bottom.width - 2 * inset, 0);
|
|
133
|
+
const usableDepth = Math.max(bottom.depth - 2 * inset, 0);
|
|
134
|
+
const columns = Math.ceil(Math.sqrt(count * Math.max(usableWidth / Math.max(usableDepth, 1), 1)));
|
|
135
|
+
const rows = Math.ceil(count / columns);
|
|
136
|
+
const centers: [number, number, number][] = [];
|
|
137
|
+
for (let row = 0; row < rows && centers.length < count; row++) {
|
|
138
|
+
for (let column = 0; column < columns && centers.length < count; column++) {
|
|
139
|
+
const x = columns === 1 ? 0 : -usableWidth / 2 + (usableWidth * column) / (columns - 1);
|
|
140
|
+
const z = rows === 1 ? 0 : -usableDepth / 2 + (usableDepth * row) / (rows - 1);
|
|
141
|
+
centers.push([x, -1, z]);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return centers;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async function drainageHoles(occt: Occt, params: PlanterParams): Promise<Shape[]> {
|
|
148
|
+
const holes: Shape[] = [];
|
|
149
|
+
for (const center of drainageHoleCenters(params)) {
|
|
150
|
+
holes.push(await occt.shapes.solid.createCylinder({ radius: HOLE_RADIUS, height: params.wallThickness + 2, center, direction: [0, 1, 0] }));
|
|
151
|
+
}
|
|
152
|
+
return holes;
|
|
153
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
:root {
|
|
2
|
+
--panel: rgba(24, 26, 30, 0.92);
|
|
3
|
+
--line: rgba(255, 255, 255, 0.12);
|
|
4
|
+
--text: #f2f2f2;
|
|
5
|
+
--muted: #a5a8ad;
|
|
6
|
+
--accent: #f0cebb;
|
|
7
|
+
font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
|
|
8
|
+
color: var(--text);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
body {
|
|
12
|
+
margin: 0;
|
|
13
|
+
overflow: hidden;
|
|
14
|
+
background: #1a1c1f;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
#three-canvas {
|
|
18
|
+
position: fixed;
|
|
19
|
+
inset: 0;
|
|
20
|
+
width: 100%;
|
|
21
|
+
height: 100%;
|
|
22
|
+
display: block;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
#panel {
|
|
26
|
+
position: fixed;
|
|
27
|
+
top: 16px;
|
|
28
|
+
left: 16px;
|
|
29
|
+
width: 300px;
|
|
30
|
+
max-height: calc(100vh - 32px);
|
|
31
|
+
overflow-y: auto;
|
|
32
|
+
padding: 18px;
|
|
33
|
+
box-sizing: border-box;
|
|
34
|
+
background: var(--panel);
|
|
35
|
+
border: 1px solid var(--line);
|
|
36
|
+
border-radius: 12px;
|
|
37
|
+
backdrop-filter: blur(12px);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.brand {
|
|
41
|
+
display: flex;
|
|
42
|
+
align-items: center;
|
|
43
|
+
gap: 10px;
|
|
44
|
+
margin: 0 0 4px;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.brand img {
|
|
48
|
+
width: 28px;
|
|
49
|
+
height: 28px;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
#panel h1 {
|
|
53
|
+
font-size: 18px;
|
|
54
|
+
margin: 0;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#panel .lead {
|
|
58
|
+
color: var(--muted);
|
|
59
|
+
font-size: 13px;
|
|
60
|
+
margin: 0 0 14px;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.presets {
|
|
64
|
+
display: flex;
|
|
65
|
+
gap: 6px;
|
|
66
|
+
margin-bottom: 14px;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
.presets button, .exports button {
|
|
70
|
+
flex: 1;
|
|
71
|
+
padding: 7px 8px;
|
|
72
|
+
background: transparent;
|
|
73
|
+
color: var(--text);
|
|
74
|
+
border: 1px solid var(--line);
|
|
75
|
+
border-radius: 8px;
|
|
76
|
+
cursor: pointer;
|
|
77
|
+
font-size: 13px;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
.presets button:hover, .exports button:hover {
|
|
81
|
+
border-color: var(--accent);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.field {
|
|
85
|
+
display: grid;
|
|
86
|
+
grid-template-columns: 1fr auto;
|
|
87
|
+
gap: 2px 8px;
|
|
88
|
+
align-items: center;
|
|
89
|
+
margin-bottom: 10px;
|
|
90
|
+
font-size: 13px;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
.field label {
|
|
94
|
+
color: var(--muted);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
.field output {
|
|
98
|
+
text-align: right;
|
|
99
|
+
font-variant-numeric: tabular-nums;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.field input[type="range"] {
|
|
103
|
+
grid-column: 1 / 3;
|
|
104
|
+
width: 100%;
|
|
105
|
+
accent-color: var(--accent);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
.field select {
|
|
109
|
+
grid-column: 1 / 3;
|
|
110
|
+
padding: 6px;
|
|
111
|
+
background: #101114;
|
|
112
|
+
color: var(--text);
|
|
113
|
+
border: 1px solid var(--line);
|
|
114
|
+
border-radius: 6px;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
.quote {
|
|
118
|
+
margin: 14px 0;
|
|
119
|
+
padding: 12px;
|
|
120
|
+
border: 1px solid var(--line);
|
|
121
|
+
border-radius: 8px;
|
|
122
|
+
font-size: 13px;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
.quote .total {
|
|
126
|
+
display: flex;
|
|
127
|
+
justify-content: space-between;
|
|
128
|
+
font-size: 18px;
|
|
129
|
+
font-weight: 600;
|
|
130
|
+
margin-bottom: 6px;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
.quote .line {
|
|
134
|
+
display: flex;
|
|
135
|
+
justify-content: space-between;
|
|
136
|
+
color: var(--muted);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
.exports {
|
|
140
|
+
display: flex;
|
|
141
|
+
gap: 6px;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.status {
|
|
145
|
+
min-height: 18px;
|
|
146
|
+
margin-top: 10px;
|
|
147
|
+
font-size: 12px;
|
|
148
|
+
color: var(--muted);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
.credit {
|
|
152
|
+
position: fixed;
|
|
153
|
+
right: 16px;
|
|
154
|
+
bottom: 12px;
|
|
155
|
+
display: flex;
|
|
156
|
+
align-items: center;
|
|
157
|
+
gap: 6px;
|
|
158
|
+
color: var(--muted);
|
|
159
|
+
font-size: 12px;
|
|
160
|
+
text-decoration: none;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
.credit img {
|
|
164
|
+
width: 16px;
|
|
165
|
+
height: 16px;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
@media (max-width: 640px) {
|
|
169
|
+
#panel {
|
|
170
|
+
top: auto;
|
|
171
|
+
bottom: 0;
|
|
172
|
+
left: 0;
|
|
173
|
+
width: 100%;
|
|
174
|
+
max-height: 55vh;
|
|
175
|
+
border-radius: 12px 12px 0 0;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { MATERIALS, PRESETS, formatPrice, type Quote } from "./catalog";
|
|
2
|
+
import { LIMITS, UNITS, type MaterialId, type PlanterParams } from "./model";
|
|
3
|
+
|
|
4
|
+
type NumericKey = keyof typeof LIMITS;
|
|
5
|
+
|
|
6
|
+
const FIELDS: { key: NumericKey; label: string }[] = [
|
|
7
|
+
{ key: "width", label: `Width (${UNITS})` },
|
|
8
|
+
{ key: "depth", label: `Depth (${UNITS})` },
|
|
9
|
+
{ key: "height", label: `Height (${UNITS})` },
|
|
10
|
+
{ key: "wallThickness", label: `Wall thickness (${UNITS})` },
|
|
11
|
+
{ key: "cornerRadius", label: `Corner radius (${UNITS})` },
|
|
12
|
+
{ key: "drainageHoles", label: "Drainage holes" },
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
export type ExportFormat = "step" | "stl" | "glb";
|
|
16
|
+
|
|
17
|
+
export interface UiHandlers {
|
|
18
|
+
onChange: (params: PlanterParams) => void;
|
|
19
|
+
onExport: (format: ExportFormat) => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface UiHandle {
|
|
23
|
+
setQuote: (quote: Quote) => void;
|
|
24
|
+
setStatus: (text: string) => void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function mountPoint(id: string): HTMLElement {
|
|
28
|
+
const element = document.getElementById(id);
|
|
29
|
+
if (!element) throw new Error(`index.html has no #${id} element to mount the panel in`);
|
|
30
|
+
return element;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function query<T extends Element>(root: ParentNode, selector: string, kind: new () => T): T {
|
|
34
|
+
const found = root.querySelector(selector);
|
|
35
|
+
if (!(found instanceof kind)) throw new Error(`the panel markup has no ${selector} element of the expected kind`);
|
|
36
|
+
return found;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function mountUi(root: HTMLElement, initial: PlanterParams, handlers: UiHandlers): UiHandle {
|
|
40
|
+
let params = { ...initial };
|
|
41
|
+
const outputs = new Map<NumericKey, HTMLOutputElement>();
|
|
42
|
+
const sliders = new Map<NumericKey, HTMLInputElement>();
|
|
43
|
+
|
|
44
|
+
root.innerHTML = `
|
|
45
|
+
<header class="brand"><img src="/logo.png" alt="bitbybit.dev" /><h1>Planter configurator</h1></header>
|
|
46
|
+
<p class="lead">Every dimension rebuilds the solid; the price comes from its measured volume and surface.</p>
|
|
47
|
+
<div class="presets"></div>
|
|
48
|
+
<form class="fields"></form>
|
|
49
|
+
<div class="quote">
|
|
50
|
+
<div class="total"><span>Price</span><span class="amount">-</span></div>
|
|
51
|
+
<div class="line"><span>Material</span><span class="material">-</span></div>
|
|
52
|
+
<div class="line"><span>Finish</span><span class="finish">-</span></div>
|
|
53
|
+
<div class="line"><span>Drainage</span><span class="holes">-</span></div>
|
|
54
|
+
</div>
|
|
55
|
+
<div class="exports">
|
|
56
|
+
<button type="button" data-format="step">STEP</button>
|
|
57
|
+
<button type="button" data-format="stl">STL</button>
|
|
58
|
+
<button type="button" data-format="glb">GLB</button>
|
|
59
|
+
</div>
|
|
60
|
+
<p class="status"></p>
|
|
61
|
+
`;
|
|
62
|
+
|
|
63
|
+
const presets = query(root, ".presets", HTMLElement);
|
|
64
|
+
for (const preset of Object.values(PRESETS)) {
|
|
65
|
+
const button = document.createElement("button");
|
|
66
|
+
button.type = "button";
|
|
67
|
+
button.textContent = preset.label;
|
|
68
|
+
button.addEventListener("click", () => {
|
|
69
|
+
params = { ...preset.params };
|
|
70
|
+
syncInputs();
|
|
71
|
+
handlers.onChange(params);
|
|
72
|
+
});
|
|
73
|
+
presets.appendChild(button);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const form = query(root, ".fields", HTMLFormElement);
|
|
77
|
+
form.addEventListener("submit", (event) => { event.preventDefault(); });
|
|
78
|
+
for (const field of FIELDS) {
|
|
79
|
+
const limit = LIMITS[field.key];
|
|
80
|
+
const wrapper = document.createElement("div");
|
|
81
|
+
wrapper.className = "field";
|
|
82
|
+
const label = document.createElement("label");
|
|
83
|
+
label.textContent = field.label;
|
|
84
|
+
label.htmlFor = field.key;
|
|
85
|
+
const output = document.createElement("output");
|
|
86
|
+
const input = document.createElement("input");
|
|
87
|
+
input.type = "range";
|
|
88
|
+
input.id = field.key;
|
|
89
|
+
input.min = String(limit.min);
|
|
90
|
+
input.max = String(limit.max);
|
|
91
|
+
input.step = String(limit.step);
|
|
92
|
+
input.addEventListener("input", () => {
|
|
93
|
+
params = { ...params, [field.key]: Number(input.value) };
|
|
94
|
+
output.value = input.value;
|
|
95
|
+
handlers.onChange(params);
|
|
96
|
+
});
|
|
97
|
+
wrapper.append(label, output, input);
|
|
98
|
+
form.appendChild(wrapper);
|
|
99
|
+
outputs.set(field.key, output);
|
|
100
|
+
sliders.set(field.key, input);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const materialWrapper = document.createElement("div");
|
|
104
|
+
materialWrapper.className = "field";
|
|
105
|
+
const materialLabel = document.createElement("label");
|
|
106
|
+
materialLabel.textContent = "Material";
|
|
107
|
+
materialLabel.htmlFor = "material";
|
|
108
|
+
const materialSelect = document.createElement("select");
|
|
109
|
+
materialSelect.id = "material";
|
|
110
|
+
for (const [id, material] of Object.entries(MATERIALS)) {
|
|
111
|
+
const option = document.createElement("option");
|
|
112
|
+
option.value = id;
|
|
113
|
+
option.textContent = material.label;
|
|
114
|
+
materialSelect.appendChild(option);
|
|
115
|
+
}
|
|
116
|
+
materialSelect.addEventListener("change", () => {
|
|
117
|
+
params = { ...params, material: materialSelect.value as MaterialId };
|
|
118
|
+
handlers.onChange(params);
|
|
119
|
+
});
|
|
120
|
+
materialWrapper.append(materialLabel, document.createElement("span"), materialSelect);
|
|
121
|
+
form.appendChild(materialWrapper);
|
|
122
|
+
|
|
123
|
+
for (const button of root.querySelectorAll<HTMLButtonElement>(".exports button")) {
|
|
124
|
+
button.addEventListener("click", () => { handlers.onExport(button.dataset["format"] as ExportFormat); });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const amount = query(root, ".amount", HTMLElement);
|
|
128
|
+
const material = query(root, ".material", HTMLElement);
|
|
129
|
+
const finish = query(root, ".finish", HTMLElement);
|
|
130
|
+
const holes = query(root, ".holes", HTMLElement);
|
|
131
|
+
const status = query(root, ".status", HTMLElement);
|
|
132
|
+
|
|
133
|
+
function syncInputs(): void {
|
|
134
|
+
for (const [key, slider] of sliders) slider.value = String(params[key]);
|
|
135
|
+
for (const [key, output] of outputs) output.value = String(params[key]);
|
|
136
|
+
materialSelect.value = params.material;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
syncInputs();
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
setQuote: (quote) => {
|
|
143
|
+
amount.textContent = formatPrice(quote.total);
|
|
144
|
+
material.textContent = `${formatPrice(quote.material)} for ${quote.litres} l`;
|
|
145
|
+
finish.textContent = formatPrice(quote.finish);
|
|
146
|
+
holes.textContent = formatPrice(quote.holes);
|
|
147
|
+
},
|
|
148
|
+
setStatus: (text) => { status.textContent = text; },
|
|
149
|
+
};
|
|
150
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/// <reference types="vite/client" />
|