Draw a Floor Plan to Scale with HTML Canvas
A minimal browser example for drawing a room to scale, mapping pointer positions back to metres, and checking whether furniture fits. A floor-plan sketch can look right while its measurements are wrong. If a sofa is sto
A minimal browser example for drawing a room to scale, mapping pointer positions back to metres, and checking whether furniture fits.
A floor-plan sketch can look right while its measurements are wrong. If a sofa is stored as 120 screen pixels, changing the zoom also changes what that number appears to mean. Store its size in metres instead, and make the screen view a conversion of that data.
Disclosure: An AI assistant drafted this tutorial and tested the example in a browser. It mentions Room Meadow, a product operated by the team behind this account. The code below is a standalone example, not a description of Room Meadow's implementation.
Start with a model in real units
The example represents a rectangular room and a rectangular desk. All positions and dimensions are in metres; the origin is the room's upper-left corner. This keeps the model independent of canvas size, zoom, and screen density. Put the HTML in a page and the JavaScript snippets, in order, inside a script tag after the canvas.
<canvas id="plan" width="640" height="400">
A 6 m by 4 m room with a desk placed 0.6 m from the left wall.
</canvas>
<p id="position" aria-live="polite"></p>
const canvas = document.getElementById('plan');
const ctx = canvas.getContext('2d');
const position = document.getElementById('position');
const room = { width: 6, height: 4 };
const desk = { x: 0.6, y: 1, width: 1.4, height: 0.7 };
const view = { pixelsPerMetre: 80, left: 40, top: 40 };
function worldToCanvas(x, y) {
return {
x: view.left + x * view.pixelsPerMetre,
y: view.top + y * view.pixelsPerMetre,
};
}
function drawPlan() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
const origin = worldToCanvas(0, 0);
ctx.strokeStyle = '#263238';
ctx.lineWidth = 3;
ctx.strokeRect(
origin.x, origin.y,
room.width * view.pixelsPerMetre,
room.height * view.pixelsPerMetre,
);
const deskOrigin = worldToCanvas(desk.x, desk.y);
ctx.fillStyle = '#84a98c';
ctx.fillRect(
deskOrigin.x, deskOrigin.y,
desk.width * view.pixelsPerMetre,
desk.height * view.pixelsPerMetre,
);
}
drawPlan();
The scale is 80 canvas pixels per metre. The room is therefore drawn as 480 Γ 320 canvas pixels, beginning at (40, 40). Change pixelsPerMetre to zoom; the desk's 1.4 m width in the model stays the same. Canvas also supports transforms for more complicated views; MDN's Canvas transformations guide explains translation and scaling.
Map a pointer back to the model
A browser can display a canvas at a different CSS size from its drawing-buffer size. Read its displayed rectangle and convert both axes before undoing the view's offset and scale. Add this after the previous code:
function pointerToWorld(event) {
const rect = canvas.getBoundingClientRect();
const canvasX = (event.clientX - rect.left) * canvas.width / rect.width;
const canvasY = (event.clientY - rect.top) * canvas.height / rect.height;
return {
x: (canvasX - view.left) / view.pixelsPerMetre,
y: (canvasY - view.top) / view.pixelsPerMetre,
};
}
canvas.addEventListener('pointermove', (event) => {
const point = pointerToWorld(event);
position.textContent = `Pointer: ${point.x.toFixed(2)} m, ${point.y.toFixed(2)} m`;
});
This snippet assumes an axis-aligned canvas with no CSS border or padding; put decoration on a surrounding element, or account for it when calculating the drawable rectangle. Rotated or skewed CSS transforms need an inverse transformation rather than this simple width-and-height ratio. It displays pointer coordinates even outside the room. An editor would decide whether to clamp a dragged object, reject it, or show a warning.
Check the geometry before drawing conclusions
For this simple axis-aligned example, a desk fits inside the room when all four edges remain within its boundaries:
function fitsInside(item, bounds) {
return item.x >= 0 && item.y >= 0 &&
item.x + item.width <= bounds.width &&
item.y + item.height <= bounds.height;
}
console.log(fitsInside(desk, room)); // true
console.log(room.width - (desk.x + desk.width)); // 4 metres to right wall
console.log(fitsInside({ ...desk, x: 5 }, room)); // false
Those are geometric checks, not a building-code assessment. A real room may have angled walls, door swings, radiators, trim, or other obstructions; the example does not model any of them. Required clearances depend on the situation and local rules, so do not present a rectangle-fit check as a permit or safety decision.
Make the plan usable beyond a mouse
The canvas is a visual output, not a complete accessible editor. For an interactive version, provide labelled numeric controls for each object's position and dimensions, a textual list of room contents, and keyboard controls for any action that can be done by dragging. Update those controls from the same model used by the drawing rather than keeping separate measurements in the UI.
If the reader's goal is to try a layout instead of building an editor, Room Meadow's floor-plan maker lets people enter dimensions and place furniture in a browser. Its photo redesign feature produces an image, so that output should not be treated as a measured plan.
Try setting the canvas CSS width to 320px while leaving its drawing-buffer width at 640. Move the pointer to the middle of the drawn room: it should report approximately 3 m, 2 m. Change the visual scale and confirm that the stored desk dimensions still read 1.4 m Γ 0.7 m.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.