npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

triangulate-image

v0.6.6

Published

Converts images in to triangular polygons.

Downloads

9

Readme

build status greenkeeper badge

triangulate-image

installation

npm install triangulate-image

jspm install triangulate-image

what is it?

a javascript module that converts images to triangular polygons:

triangulated image of a. lincoln

for a live example, you can check out my triangulation editor online. there's also a list with example images, and transparency examples included in this repository.

how to use it

this library can be used in web browsers as well as in node.

it supports loading as an AMD module, as a CommonJS module or a global variable.

a simple example:

var params = { blur: 110, vertexCount: 700 };
var image = new Image();

image.onload = function () {
	triangulate(params)
		.fromImage(image)
		.toSVG()
		.then( function( svgMarkup ) {
			document.body.innerHTML = svgMarkup;
		} );
};

image.src = 'lincoln.jpg'

as you can see, there are three calls happening:

  1. triangulate() is called with the triangulation parameters
  2. then fromImage() is called with the input image as parameter
  3. and finally toSVG() is called to output svg markup.

when using the library, always follow these three steps: triangulation, input, output.

for an explanation of all available methods and parameters, check out the reference section below.

by default, all input and output methods are asynchronous and use promises for flow control. for most methods, there synchronous versions available as well. this library uses web workers for processing the image data where possible.

you can find more examples for both node and browser in the examples folder of this repository.

reference

triangulate()

triangulate() can take the following parameters


// the parameters listed are the default params

var triangulationParams = {
	accuracy: 0.7,    		// float between 0 and 1
	blur: 40,         		// positive integer
	threshold: 50,    		// integer between 1 and 100
	vertexCount: 700, 		// positive integer
	fill: true,       		// boolean or string with css color (e.g '#bada55', 'red', rgba(100,100,100,0.5))
	stroke: true,     		// boolean or string with css color (e.g '#bada55', 'red', hsla(0, 50%, 52%, 0.5))
	strokeWidth: 0.5, 		// positive float
	gradients: true,  		// boolean
	gradientStops: 4, 		// positive integer >= 2
	lineJoin: 'miter',		// 'miter', 'round', or 'bevel'
	transparentColor: false // boolean false or string with css color
};

it returns an object containing all input methods.

fromImage()

fromImage() expects an Image object as its only parameter. it returns an object containing all input methods.

example:

var image = new Image();

image.onload = function () {
	triangulate()
		.fromImage( image )
		.toSVG()
		.then ( function ( svgMarkup ) {
			document.body.innerHTML = svgMarkup;
		} );
};

image.src = 'lincoln.jpg'

please note: this method is not available in node. important: when using the library in a browser, make sure the image was loaded before triangulating it.

fromImageSync()

fromImageSync() expects an Image object as its only parameter. it returns an object containing all input methods. it is the synchronous version of fromImage().

example:

var image = new Image();

image.onload = function () {
	document.body.innerHTML = triangulate().fromImageSync( image ).toSVG();
};

image.src = 'lincoln.jpg'

please note: this method is not available in node. important: when using the library in a browser, make sure the image was loaded before triangulating it.

fromImageData()

fromImageData() expects an ImageData object as its only parameter. it returns an object containing all input methods.

example:

var canvas = document.createElement( 'canvas' );
var ctx = canvas.getContext( '2d' );

ctx.fillStyle = 'red';
ctx.fillRect( 30, 30, 90, 45 );
ctx.fillStyle = 'green';
ctx.fillRect( 10, 20, 50, 60 );

var imageData = ctx.getImageData( 0, 0, canvas.width, canvas.height );

triangulate()
	.fromImageData( imageData )
	.toSVG()
	.then( function ( svgMarkup ) {
		document.body.innerHTML = svgMarkup;
	} );

fromImageDataSync()

fromImageDataSync() expects an ImageData object as its only parameter. it returns an object containing all input methods. it is the synchronous version of fromImageData().

example:

var canvas = document.createElement( 'canvas' );
var ctx = canvas.getContext( '2d' );

ctx.fillStyle = 'red';
ctx.fillRect( 30, 30, 90, 45 );
ctx.fillStyle = 'green';
ctx.fillRect( 10, 20, 50, 60 );

var imageData = ctx.getImageData( 0, 0, canvas.width, canvas.height );

document.body.innerHTML = triangulate().fromImageDataSync( imageData ).toSVG();

fromBuffer()

fromBuffer() expects a Buffer object as its only parameter. it returns an object containing all input methods.

it uses image#src=buffer from node-canvas internally.

example:

var fs = require('fs');

fs.readFile( './lincoln.jpg', function ( err, buffer ) {
	if ( err ) { throw err; }
		
	triangulate()
		.fromBuffer( buffer )
		.toSVG()
		.then( function ( svgMarkup ) {
			fs.writeFile( './lincoln.svg', svgMarkup, function ( err ) {
				if ( err ) { throw err; }
				console.log( 'created an svg file.' );
			} );
		} );
} );

please note: this method is only available in node.

fromBufferSync()

fromBufferSync() expects a Buffer object as its only parameter. it returns an object containing all input methods. it is the synchronous version of fromBuffer().

it uses image#src=buffer from node-canvas internally.

example:

var fs = require('fs');

fs.readFile( './lincoln.jpg', function ( err, buffer ) {
	if ( err ) { throw err; }
		
	var svgMarkup = triangulate().fromBufferSync( buffer ).toSVGSync();

	fs.writeFile( './lincoln.svg', svgMarkup, function ( err ) {
		if ( err ) { throw err; }
		console.log( 'created an svg file.' );
	} );
} );

please note: this method is only available in node.

fromStream()

fromStream() expects a ReadableStream object as its only parameter. it returns an object containing all input methods.

it uses image#src=buffer from node-canvas internally.

example:

var fs = require('fs');

var inputStream = fs.createReadStream( './lincoln.jpg' );
var outputStream = fs.createWriteStream( './lincoln-triangulated.png' );

triangulate( params )
	.fromStream( inputStream )
	.toPNGStream()
	.then( function ( pngStream ) {
		pngStream.on( 'data', function ( chunk ) { outputStream.write( chunk ); } );
		pngStream.on( 'end', function () { console.log( 'png file saved.' ); } );
	} );

please note: this method is only available in node. currently, theres no input sanitation for this method, so you'll want to make sure that the input stream is an image.

toDataURL()

toDataURL() can take the following parameters:

var dataUrlParams = {
	dpr: 1                   // positive number, short for devicePixelRatio,
	backgroundColor: 'green' // background color of image. not set: transparent background
};

it returns a String containing the base64-encoded image url.

example:

var image = new Image();

image.onload = function () {
	triangulate()
		.fromImage( image )
		.toDataURL()
		.then( function ( dataURL ) {
			document.body.style.background = 'url(' + dataURL + ')';
		} );
};

image.src = 'lincoln.jpg'

toDataURLSync()

toDataURLSync() can take the following parameters:

var dataUrlParams = {
	dpr: 1                   // positive number, short for devicePixelRatio,
	backgroundColor: 'green' // background color of image. not set: transparent background
};

it is the synchronous version of toDataUrl().

it returns a String containing the base64-encoded image url.

example:

var image = new Image();

image.onload = function () {
	var dataURL = triangulate()
		.fromImageSync( image )
		.toDataURLSync();

	document.body.style.background = 'url(' + dataURL + ')';
};

image.src = 'lincoln.jpg'

toImageData()

toImageData() can take the following parameters:

var imageDataParams = {
	dpr: 1                   // positive number, short for devicePixelRatio,
	backgroundColor: 'green' // background color of image. not set: transparent background
};

it returns an ImageData object.

example:

var image = new Image();

image.onload = function () {
	triangulate()
		.fromImage( image )
		.toImageData()
		.then( function ( imageData ) {
			var canvas = document.createElement( 'canvas' );
			var ctx = canvas.getContext( '2d' );
			ctx.putImageData( imageData, 0, 0 );

			document.body.appendChild( canvas );
		} );
};

image.src = 'lincoln.jpg'

toImageDataSync()

toImageDataSync() can take the following parameters:

var imageDataParams = {
	dpr: 1                   // positive number, short for devicePixelRatio,
	backgroundColor: 'green' // background color of image. not set: transparent background
};

it returns an ImageData object.

it is the synchronous version of toImageData().

example:

var image = new Image();

image.onload = function () {
	var imageData = triangulate()
		.fromImageSync( image )
		.toImageDataSync();
	
	var canvas = document.createElement( 'canvas' );
	var ctx = canvas.getContext( '2d' );
	ctx.putImageData( imageData, 0, 0 );

	document.body.appendChild( canvas );
};

image.src = 'lincoln.jpg'

toSVG()

toSVG() does not take any parameters. it returns a String with svg markup.

example:

var params = { blur: 110, vertexCount: 700 };
var image = new Image();

image.onload = function () {
	triangulate( params )
		.fromImage( image )
		.toSVG()
		.then( function ( svgMarkup ) {
			document.body.innerHTML = svgMarkup;
		} );
};

image.src = 'lincoln.jpg'

toSVGSync()

toSVGSync() does not take any parameters. it returns a String with svg markup. it is the synchronous version of toSVG().

example:

var params = { blur: 110, vertexCount: 700 };
var image = new Image();

image.onload = function () {
	document.body.innerHTML = triangulate( params ).fromImageSync( image ).toSVGSync();
};

image.src = 'lincoln.jpg'

toData()

toData() does not take any parameters. it returns an Array with the data for all polygons.

example:

var image = new Image();

image.onload = function () {
	triangulate()
		.fromImage( image )
		.toData()
		.then( function ( polygonData ) {
			console.log( polygonData );
		} );
};

image.src = 'lincoln.jpg'

toDataSync()

toDataSync() does not take any parameters. it returns an Array with the data for all polygons. it is the synchronous version of toData().

example:

var image = new Image();

image.onload = function () {
	console.log( triangulate().fromImageSync( image ).toDataSync() );
};

image.src = 'lincoln.jpg'

toBuffer()

toBuffer() can take the following parameters:

var bufferParams = {
	format: 'svg' // 'svg', 'pdf' or undefined
};

it uses canvas#tobuffer from node-canvas internally.

it returns a Buffer object.

example:

var fs = require('fs');

fs.readFile( './lincoln.jpg', function ( err, buffer ) {
	if ( err ) { throw err; }
		
	triangulate()
		.fromBuffer( buffer )
		.toBuffer( { format: 'pdf' } )
		.then( function ( imageBuffer ) {
			fs.writeFile( './lincoln.pdf', imageBuffer, function ( err ) {
				if ( err ) { throw err; }
				console.log( 'created a pdf file.' );
			} );
		} );
} );

please note: this method is only available in node.

toBufferSync()

toBufferSync() can take the following parameters:

var bufferParams = {
	format: 'svg' // 'svg', 'pdf' or undefined
};

it is the synchronous version of toBuffer(). it uses canvas#tobuffer from node-canvas internally.

it returns a Buffer object.

example:

var fs = require('fs');

fs.readFile( './lincoln.jpg', function ( err, buffer ) {
	if ( err ) { throw err; }
		
	var imageBuffer = triangulate().fromBufferSync( buffer ).toBufferSync( { format: 'pdf' } );

	fs.writeFile( './lincoln.pdf', imageBuffer, function ( err ) {
		if ( err ) { throw err; }
		console.log( 'created a pdf file.' );
	} );
} );

please note: this method is only available in node.

toJPGStream()

toJPGStream() can take the following parameters:

var jpgStreamParams = {
	bufsize: 4096,          // output buffer size in bytes
	quality: 75,            // jpg quality (0-100) default: 75
	progressive: false,     // true for progressive compression
	dpr: 1,                 // positive number, short for devicePixelRatio,
	backgroundColor: '#fff' // btw: there are no transparent backgrounds in jpg.
};

it uses canvas#jpegstream() from node-canvas internally.

it returns a JPEGStream object.

example:

var fs = require('fs');
fs.readFile( '.lincoln.jpg', function ( err, buffer ) {
	if ( err ) { throw err; }

	var fileStream = fs.createWriteStream( './triangulatedLincoln.jpg' );
	var jpgStream = triangulate().fromBuffer( buffer ).toJPGStream( { backgroundColor: 'red' } );
	
	jpgStream.on( 'data', function ( chunk ) { fileStream.write( chunk ); } );
	jpgStream.on( 'end', function () { console.log( 'jpg file created.' ); } );
} );

please note: this method is only available in node.

toJPEGStream()

see toJPGStream().

toPNGStream()

toPNGStream() can take the following parameters:

var jpgStreamParams = {
	dpr: 1,                 // positive number, short for devicePixelRatio,
	backgroundColor: '#fff' // background color of image. not set: transparent background
};

it uses canvas#pngstream() from node-canvas internally.

it returns a PNGStream object.

example:

var fs = require('fs');
fs.readFile( '.lincoln.jpg', function ( err, buffer ) {
	if ( err ) { throw err; }

	var fileStream = fs.createWriteStream( './triangulatedLincoln.png' );
	var pngStream = triangulate().fromBuffer( buffer ).toPNGStream( { backgroundColor: 'blue' } );
	
	pngStream.on( 'data', function ( chunk ) { fileStream.write( chunk ); } );
	pngStream.on( 'end', function () { console.log( 'png file created.' ); } );
} );

please note: this method is only available in node.

toSVGStream()

toSVGStream() can't take any parameters.

it returns a ReadableStream object.

example:

var fs = require('fs');
fs.readFile( '.lincoln.jpg', function ( err, buffer ) {
	if ( err ) { throw err; }

	var svgStream = triangulate().fromBuffer( buffer ).toSVGStream();
	svgStream.pipe( process.stdout );
} );

please note: this method is only available in node.

in node

when used in node, this library has a dependency on the node-canvas module. node-canvas may require some extra steps to get it to work on some platforms.

development

npm run build will build the node-ready and browser-ready versions, which are saved to the dist-node and dist directories.

npm run test will run the tests in both the browser and node.

license

mit

third party code

dependencies:

more information

this repository contains a changelog and an authors file with even more information.

missing something?

found a bug? missing a feature? are you using this library in an interesting project? take a look at the issues, open a pull request and let me know.

most importantly

thank you for taking a look at this repo. have a great day :)