WASM Bindings
Using the solver from JavaScript via WebAssembly.
WASM Bindings
The solver compiles to WebAssembly, allowing direct use from JavaScript in the browser or Node.js.
API
solve_sketch(json: string) → string
Takes a JSON-encoded SketchSolveRequest, returns a JSON-encoded SketchSolveResponse.
const request = {
points: [{ x: 0.0, y: 0.0 }, { x: 5.0, y: 0.0 }],
constraints: [{ type: "Horizontal", p1: 0, p2: 1 }],
algorithm: "DogLeg"
};
const result = JSON.parse(solve_sketch(JSON.stringify(request)));
solve_sketch_v2(json: string) → string
Enhanced version with SystemView output, diagnostics, and tracing data.
Types
SketchSolveRequest:
interface SketchSolveRequest {
points: Array<{ x: number; y: number }>;
lines?: Array<{ x1: number; y1: number; x2: number; y2: number }>;
circles?: Array<{ cx: number; cy: number; r: number }>;
arcs?: Array<{ cx: number; cy: number; r: number; start: number; end: number }>;
constraints: Array<{
type: string;
[key: string]: any; // constraint-specific params
}>;
algorithm?: "LM" | "DogLeg" | "BFGS" | "L-BFGS" | "SparseCG" | "Auto";
max_iterations?: number;
tolerance?: number;
}
SketchSolveResponse:
interface SketchSolveResponse {
success: boolean;
points: Array<{ x: number; y: number }>;
residual_norm: number;
iterations: number;
algorithm_used: string;
diagnostics?: string;
trace?: Array<{ iteration: number; residual_norm: number }>;
}
SystemView — a clean representation of the solved system without internal state, suitable for serialization and display.
Build Process
wasm-pack build --target web --out-name solver_test --release
This produces:
pkg/solver_test.js— JavaScript wrapper with TypeScript declarationspkg/solver_test_bg.wasm— WebAssembly binarypkg/solver_test.d.ts— TypeScript type declarations
Usage from JavaScript
Browser
import init, { solve_sketch, solve_sketch_v2 } from './pkg/solver_test.js';
async function run() {
await init();
const request = {
points: [
{ x: 0.0, y: 0.0 },
{ x: 10.0, y: 0.0 },
{ x: 5.0, y: 5.0 }
],
constraints: [
{ type: "Distance", p1: 0, p2: 1, value: 10.0 },
{ type: "Horizontal", p1: 0, p2: 1 },
{ type: "Distance", p1: 0, p2: 2, value: 7.07 }
],
algorithm: "Auto"
};
const result = JSON.parse(solve_sketch(JSON.stringify(request)));
console.log("Success:", result.success);
console.log("Solved points:", result.points);
}
Node.js
const { solve_sketch } = require('./pkg/solver_test.js');
const result = JSON.parse(solve_sketch(JSON.stringify({
points: [{ x: 0, y: 0 }, { x: 5, y: 5 }],
constraints: [{ type: "Distance", p1: 0, p2: 1, value: 7.07 }]
})));
console.log(result);
WASM-Specific Notes
- No timing: The
Timerstruct returns0.0for all duration measurements sincestd::time::Instantis unavailable in WASM. Useperformance.now()from JavaScript instead. - No proptests on wasm32: Proptest relies on process forking which is unavailable in WASM. Proptests are gated with
#[cfg(not(target_arch = "wasm32"))]. - console_error_panic_hook: The WASM build includes
console_error_panic_hookso Rust panics appear as JavaScript console errors with full stack traces. - Memory: Large sketches (1000+ parameters) may hit WASM linear memory limits. Increase the browser's WASM memory if needed.