I build PatentStroke, a tool that makes patent drawings. Patent offices want black-and-white line art, and a lot of what inventors upload is a PNG scan or a screenshot. So one feature turns a raster drawing into vectors: SVG for illustration tools, DXF for CAD, and a vector PDF sheet. All of the tracing runs client-side. Here is how it works and what I learned.
1. Crop to the ink first
Scans come with huge white margins. Before anything else, I compute the luminance of each pixel (0.299 R + 0.587 G + 0.114 B), find the bounding box of everything darker than the user's threshold, and crop to it with a few pixels of padding. Less area means faster tracing and a figure that sits where you expect it on the sheet.
2. Don't trace pixels, trace the gray
The obvious approach is to threshold to pure black and white and walk the pixel edges. That gives you the staircase: every diagonal becomes a row of tiny steps, and it looks worse the larger you print it.

Anti-aliased images already tell you where the edge is. A pixel that is 40% gray is 40% covered. So instead of thresholding first, I run marching squares on the grayscale values with linear interpolation (d3-contour does this well) and take the contour at the threshold level. The edge lands between pixels, at the sub-pixel position where the gray crosses the threshold. Doubling the image before tracing gives the interpolation a bit more to work with.
3. Simplify, then curve, but keep the corners

Marching squares gives you a point for every pixel boundary, which is far too many. Each ring goes through Ramer–Douglas–Peucker with a tolerance of about 0.6 px, so it never strays visibly from the traced edge. Then the points are redrawn as Catmull–Rom curves converted to cubic Béziers.
Curving everything rounds off the corners of boxes and arrowheads, which is wrong for a technical drawing. So any turn sharper than about one radian stays a hard corner, and only the gentle stretches are smoothed.
4. Holes and fills
A letter "O" or a washer is an outline with a hole. d3-contour returns polygons as outer ring plus holes. I check the signed area of each ring and reverse any hole that winds the same way as its outline. With opposite windings, the default nonzero fill rule in SVG, PDF and canvas keeps the holes open, so nothing else needs special handling.
Strokes come out as filled outlines rather than centerlines. For patent drawings that is what you want: each line keeps the thickness it was drawn with.
5. DXF is just text
I write AutoCAD R12 ASCII DXF (AC1009), which every CAD program I've tried opens: AutoCAD, Fusion 360, SolidWorks, LibreCAD. Each outline becomes a closed POLYLINE, Bézier curves are flattened into 8 segments each, units are millimetres, and the y axis is flipped because DXF's points up. The whole writer is about 150 lines.
6. Know when to give up
Tracing fails on photos, renders and shaded drawings: you get blotchy outlines. For those, PatentStroke first redraws the image as clean line art with an image model, then traces the result. And if a trace is too heavy (thousands of tiny paths from noise), it falls back to a 2× black-and-white bitmap, which still prints above 300 DPI on the sheet.
Small text also traces badly at any setting, so reference numerals are added afterwards as real text, not traced.
If you want to try it on your own drawings, it's at https://patentstroke.com/vectorize (you need an account; new accounts get free credits). I'm happy to answer questions about the tracing, or about what patent offices actually require from drawings.
``
