Engineering a Zero-Install 2D CAD Studio in React & HTML5 Canvas (Lessons from SKCAD)
How coordinate transformations, object snapping, and layered rendering work inside modern web browsers.
Saddam Kathat
Founder & Creator of SKCAD
Executive Summary & Key Takeaways
- Separate world-space engineering coordinates from screen-space pixel coordinates to support smooth zoom and pan.
- Keep high-frequency mouse movement rendering inside an HTML5 Canvas loop rather than triggering React re-renders on every pixel.
- Use Spatial Grid or bounding-box proximity checks for instant endpoint, midpoint, and center object snapping.
1. World Coordinates vs. Screen Pixels
In a technical drafting application like SKCAD, a line drawn by a civil engineer represents real-world dimensions—not raw browser pixels. Hardcoding canvas pixel positions makes zooming, panning, and accurate measurement impossible.
By maintaining a camera transform state (panX, panY, zoomScale), every pointer event is converted from screen space into world space before snapping or committing geometry to the drawing store.
export function screenToWorld(
screenX: number,
screenY: number,
camera: { panX: number; panY: number; zoom: number }
) {
return {
x: (screenX - camera.panX) / camera.zoom,
y: (screenY - camera.panY) / camera.zoom,
};
}2. Preventing React Render Bottlenecks on Canvas
Moving a cursor across a drafting viewport fires dozens of pointer events per second. Storing live cursor preview coordinates in top-level React state causes unnecessary component tree re-renders.
Instead, React manages toolbars, layer visibility panels, and committed entity lists, while transient rubber-band previews and snap crosshairs render directly on the HTML5 Canvas layer at 60fps.
Need Implementation Help?
Custom Web & SaaS Development
Scalable full-stack web applications, SaaS platforms, and interactive browser tools engineered with Next.js, React, and TypeScript.
