Now let's build the core engine files.
Now the render helper (mesh warp drawing):
Now let's add geometry helper functions to the engine and then build the Canvas component.
Now let's build the usePhysicsWorld hook:
Now let's build the sidebar/control panel, toolbar, and region list components.
Now the Toolbar component:
Now let's assemble the main App.tsx:
Now let's update index.html title and build the project.
Build succeeded. Let's review the TS types more carefully for potential subtle bugs — particularly the RegionWithMeta vs Region typing used across files, and double check setResolution naming collision in the hook (imported setResolution from engine but also defining method setResolution in the returned object — should be fine as different scopes, but let's verify).
Good — the default image is correctly copied to dist/images/. The build is clean and complete. Let me do a final review of the full CanvasStage file to make sure everything is coherent.
File is clean and correct, no duplication (the count included blank/final lines from earlier formatting). Everything looks structurally sound. The build already passed. Let's do one more build pass to be fully certain after review.
The build completed successfully. Here's a summary of what I built:
SoftLab — Soft-Body Physics Testing Engine
Architecture:
-
src/engine/ — a self-contained physics engine:
softbody.ts — Verlet-integration mass-spring soft-body simulation with structural/shear/bend constraints, configurable mass, stiffness, damping, elasticity, age-based material fatigue, tightness-driven solver iterations, and edge collision with restitution. Includes helpers to translate/rotate/resize regions live while preserving current deformation state.
texture.ts — extracts a rotated/clipped (rect, ellipse, capsule) sub-image patch per region as a baked texture.
render.ts — canvas 2D mesh-warping renderer using the classic affine-triangle-mapping technique to texture-map the deforming grid in real time.
factory.ts / types.ts — region defaults and shared types.
-
src/hooks/usePhysicsWorld.ts — a controller hook holding regions in a ref (for 60 FPS performance without React re-renders) while exposing reactive setters for the UI.
-
src/components/
CanvasStage.tsx — the interactive canvas: fixed-timestep physics loop with accumulator (stable at any refresh rate), responsive/DPR-aware rendering, and full mouse/touch/keyboard interaction (drag to poke soft body, move/resize/rotate handles, arrow keys to nudge, [/] to rotate, -/+ to resize, Delete to remove, Space to play/pause).
Sidebar.tsx — per-region editing: shape, width/height, orientation, mesh resolution, mass, stiffness, damping, elasticity, age, tightness, collision & gravity toggles, duplicate/delete/reset.
Toolbar.tsx — image upload, add-region tool, play/pause, gravity/wireframe toggles, live FPS counter.
I also generated a default sample image (apple) so the demo works immediately, and updated the page title. The project builds cleanly with npm run build.
Files built: src/engine/types.ts, src/engine/texture.ts, src/engine/softbody.ts, src/engine/render.ts, src/engine/factory.ts, src/hooks/usePhysicsWorld.ts, src/components/CanvasStage.tsx, src/components/ui.tsx, src/components/Sidebar.tsx, src/components/Toolbar.tsx, src/App.tsx, index.html