How the ImageTracerJS tracing process works
masterImageTracerJS converts raster images to vector graphics through a multi-step pipeline. Understanding this process can help you troubleshoot unexpected results or optimize your input images:
- Color Quantization: The
colorquantizationfunction reduces the image to an indexed color set using a K-means or K-medians clustering variant. - Layer Separation & Edge Detection: The
layeringfunction separates colors into arrays and calculates edge node types (using a variant of the Marching Squares algorithm). - Pathscan: The
pathscanfunction identifies chains of edge nodes to form paths. - Interpolation: The
internodesfunction interpolates edge node coordinates, assigning one of 8 directions (E, NE, N, NW, W, SW, S, SE) to each segment. - Tracing:
tracepathsplits interpolated paths into directional sequences.fitseqattempts to fit straight lines or quadratic splines to these sequences. If the error exceeds the defined threshold, it uses a divide-and-conquer approach to split the sequence and recursively fit smaller segments.
- SVG Rendering: The
getsvgstringfunction converts the final coordinates into SVG Path data.