Skip to content

✂️ Render a raster image to svg

Notifications You must be signed in to change notification settings

piwodlaiwo/cutout

Repository files navigation

cutout

build status coverage status greenkeeper

render a raster image to svg

This library renders raster images to an svg image. It does so by repeatedly generating and mutating shapes, keeping only the best ones. The code has largely been ported from Primitive. Primitive works just fine, but I wanted something that runs in javascript to make it easier to tinker with the logic. Besides that, my goals with this library were:

  • No native, non-javascript dependencies (so no node-canvas, as that relies on Cairo)
  • No browser specific APIs (even though it can be bundled for the browser without any problems)
  • Modular and not tied to a single implementation, so it can fit in any project

Examples

Raster input Svg result
1 1
2 2
3 3
4 4

Installation

npm install -g @ismay/cutout

Example

const fs = require('fs');
const baboon = require('baboon-image');
const Cutout = require('@ismay/cutout');

// Render the image in lines and squares
const cutout = new Cutout(baboon, {
  alpha: 128,
  shapeTypes: ['Line', 'Square']
})

// Draw a 100 shapes
for (var i = 0; i < 100; i++) {
  cutout.step();
}

fs.writeFileSync('./baboon.svg', cutout.svg);

API

Cutout

Render a raster image to a collection of shapes

Kind: global class

new Cutout(target, [options])

Param Type Default Description
target ndarray The image to render to svg
[options] Object Configuration options
[options.alpha] number 255 The opacity of the shapes (0-255)
[options.background] Array.<number> Optional background color, expressed as an array of four numbers between 0 - 255 for respectively red, green, blue and transparency
[options.shapeTypes] Array.<string> The types of shapes to use when generating the image, available are: Circle, Cubic, RotatedEllipse, Ellipse, Line, Quadratic, Rect, RotatedRect, Square and Triangle
[options.amountOfShapes] number 1000 The number of shapes to try per step
[options.amountOfAttempts] number 100 The number of times to mutate each candidate shape

cutout.image ⇒ ndarray

Get the current image

Kind: instance property of Cutout Returns: ndarray - The current image

cutout.svg ⇒ string

Get the current svg

Kind: instance property of Cutout Returns: string - The current svg

cutout.difference ⇒ number

Get the current difference

Kind: instance property of Cutout Returns: number - The current difference

cutout.step() ⇒ this

Add a single new shape

Kind: instance method of Cutout Returns: this - The class instance

Credits

License

MIT

About

✂️ Render a raster image to svg

Resources

Stars

Watchers

Forks

Packages

No packages published