imagetracerjs

repository·master·Indexed 23 days ago

https://github.com/jankovicsandras/imagetracerjs

A JavaScript raster image tracer and vectorizer (version 1.2.6) that converts bitmap images such as PNG or JPG into SVG vectors or structured tracing data. It supports both browser and Node.js environments, providing APIs to trace images via URLs or ImageData objects. The library includes a Node.js CLI, various tracing option presets (e.g., 'curvy', 'sharp', 'detailed'), and browser helper functions for DOM integration and image loading.

Tokens
4.4K
Snippets
9
Records
34
Agent score
80%

What's inside imagetracerjs

  1. How the ImageTracerJS tracing process works

    master

    ImageTracerJS 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:

    1. Color Quantization: The colorquantization function reduces the image to an indexed color set using a K-means or K-medians clustering variant.
    2. Layer Separation & Edge Detection: The layering function separates colors into arrays and calculates edge node types (using a variant of the Marching Squares algorithm).
    3. Pathscan: The pathscan function identifies chains of edge nodes to form paths.
    4. Interpolation: The internodes function interpolates edge node coordinates, assigning one of 8 directions (E, NE, N, NW, W, SW, S, SE) to each segment.
    5. Tracing:
      • tracepath splits interpolated paths into directional sequences.
      • fitseq attempts 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.
    6. SVG Rendering: The getsvgstring function converts the final coordinates into SVG Path data.
  2. Use predefined option presets

    master

    ImageTracerjs provides several built-in presets to achieve specific visual styles without manually configuring every parameter. Common presets include:

    • posterized1: Low color count (2 colors), no sampling.
    • curvy: High precision for curves (ltres: 0.01) with line filtering.
    • sharp: High precision for quadratic splines (qtres: 0.01).
    • detailed: High color count (64) and high coordinate precision.
    • smoothed: Uses Gaussian blur preprocessing.
    • grayscale: Generates a 7-color grayscale palette.
    • artistic1 through artistic4: Various stylized settings varying in blur, stroke width, and color complexity.
    // Example of the structure of available presets
    this.optionpresets = {
    	'default': {
    		// Tracing
    		corsenabled : false,
    		ltres : 1,
    		qtres : 1,
    		pathomit : 8,
    		rightangleenhance : true,
    		// ... other defaults
    	},
    	'posterized1': { colorsampling:0, numberofcolors:2 },
    	'curvy': { ltres:0.01, linefilter:true, rightangleenhance:false },
    	'sharp': { qtres:0.01, linefilter:false },
    	'detailed': { pathomit:0, roundcoords:2, ltres:0.5, qtres:0.5, numberofcolors:64 },
    	'smoothed': { blurradius:5, blurdelta: 64 },
    	'grayscale': { colorsampling:0, colorquantcycles:1, numberofcolors:7 },
    	// ... more presets
    };
  3. Setup imagetracerjs with Node.js

    master

    In a Node.js environment, you can use the library by requiring it. Since Node.js lacks built-in DOM and Canvas support, you must provide an ImageData object, typically by using an external library (like pngjs) to parse image files into a compatible format.

    var ImageTracer = require( __dirname + '/../imagetracer_v1.2.6' );
  4. Setup imagetracerjs in the Browser

    master

    To use imagetracerjs in a web browser, include the script tag in your HTML. You can then use the ImageTracer object to trace images asynchronously using URLs.

    <script src="imagetracer_v1.2.6.js"></script>
    <script src="imagetracer_v1.2.6.js"></script>
  5. Use tracing option presets

    master

    Instead of manually configuring every parameter, you can pass a string representing a predefined preset to the options argument of any API method. The library will use the settings from that preset and fill in any missing values with the default settings.

    Available Presets:

    • 'default': Standard tracing settings.
    • 'posterized1': Low color count (2 colors).
    • 'posterized2': Low color count (4 colors) with blur.
    • 'curvy': Smooth lines, no right-angle enhancement.
    • 'sharp': High precision for quadratic splines.
    • 'detailed': High path detail and color count.
    • 'smoothed': High blur for a soft look.
    • 'grayscale': Grayscale output.
    • 'fixedpalette': Uses a fixed number of colors.
    • 'randomsampling1': Random color sampling (8 colors).
    • 'randomsampling2': Random color sampling (64 colors).
    • 'artistic1': Artistic style with blur and specific line settings.
    • 'artistic2': Artistic style with very few colors.
    • 'artistic3': Artistic style with high error tolerance.
    • 'artistic4': Artistic style with high detail and blur.
    • 'posterized3': Specific posterized look with a custom palette.
  6. Configure tracing with Option Presets

    master

    Instead of providing a full configuration object, you can pass a string representing a preset to control the tracing and rendering process. Available presets include:

    'default'
    'posterized1'
    'posterized2'
    'posterized3'
    'curvy'
    'sharp'
    'detailed'
    'smoothed'
    'grayscale'
    'fixedpalette'
    'randomsampling1'
    'randomsampling2'
    'artistic1'
    'artistic2'
    'artistic3'
    'artistic4'
  7. Configure deterministic output and right angle enhancement

    master

    As of version 1.2.0, the library provides several new options for controlling output quality and determinism:

    • options.colorsampling: Set to 2 for deterministic color sampling (now the default).
    • options.mincolorratio: Set to 0 for deterministic results (now the default).
    • options.rightangleenhance: Enables right angle enhancement (defaults to true).
    • options.strokewidth: Sets a custom stroke width (defaults to 1).
    • options.linefilter: Enables a line filter (defaults to false).
  8. Use Gaussian blur preprocessing for noise reduction

    master

    Version 1.1.0 introduced Gaussian blur preprocessing to filter noise and improve quality. This feature is now independent of the DOM and canvas, making it compatible with Node.js (as of 1.2.1).

    • options.blurradius: Set a value between 1 and 5 to apply the blur.
    • options.blurdelta: Sets the threshold for the selective Gaussian blur.
  9. Optimize SVG file size with roundcoords and desc

    master

    To reduce the length of the generated SVG files, you can use the following options (introduced in version 1.1.0):

    • options.roundcoords: Rounds coordinates to a specific decimal place. This can reduce SVG length by more than 20% with minimal precision loss.
    • options.desc: Set to false to turn off path descriptions, further reducing SVG length (defaults to false as of version 1.2.0).