0.3.1 • Published 6 years ago

@ismay/cutout v0.3.1

Weekly downloads
1
License
MIT
Repository
-
Last release
6 years ago

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 inputSvg result
11
22
33
44

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)

ParamTypeDefaultDescription
targetndarrayThe image to render to svg
optionsObjectConfiguration options
options.alphanumber255The opacity of the shapes (0-255)
options.backgroundArray.<number>Optional background color, expressed as an array of four numbers between 0 - 255 for respectively red, green, blue and transparency
options.shapeTypesArray.<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.amountOfShapesnumber1000The number of shapes to try per step
options.amountOfAttemptsnumber100The 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