Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
glitch-canvas
Advanced tools
JavaScript library for applying a glitch effect to a canvas element
$ npm install glitch-canvas
glitch-canvas is a javascript library for applying a glitch effect to a canvas element. it can be used to transform images to look like this:
for a live example, you can check out my jpg-glitch editor online.
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:
glitch( { seed: 25, quality: 30 } )
.fromImage( image )
.toDataURL()
.then( function( dataURL ) {
var glitchedImg = new Image();
glitchedImg.src = dataURL;
} );
as you can see, there are three calls happening:
glitch()
is called with the glitch parametersfromImage()
is called with the input image as parametertoDataURL()
is called to output a dataURL.when using the library, always follow these three steps:
all input and output methods are asynchronous and use promises for flow control.
for an explanation of all available methods and parameters, check out the reference section below.
you can find more examples for both node and the browser in the examples folder of this repository.
glitch()
fromImage()
, fromImageData()
, fromBuffer()
, fromStream()
toDataURL()
, toImageData()
, toBuffer()
, toJPGStream()
, toPNGStream()
glitch()
can take the following parameters that control how the glitched image is going to look:
// the parameters listed are the default params
var glitchParams = {
seed: 25, // integer between 0 and 99
quality: 30, // integer between 0 and 99
amount: 35, // integer between 0 and 99
iterations: 20 // integer
};
please note: depending on the size, quality and contents of the source image and the number of iterations, the visual effect of the seed
and amount
parameters can be marginal or not even noticeable.
it returns an object containing all input methods.
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 () {
glitch()
.fromImage( image )
.toDataURL()
.then( function( dataURL ) {
var glitchedImg = new Image();
glitchedImg.src = dataURL;
document.body.appendChild( glitchedImg );
} );
};
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 glitching it.
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 );
glitch()
.fromImageData( imageData )
.toDataURL()
.then( function( dataURL ) {
var glitchedImg = new Image();
glitchedImg.src = dataURL;
document.body.appendChild( glitchedImg );
} );
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; }
glitch()
.fromBuffer( buffer )
.toBuffer()
.then( function ( glitchedBuffer ) {
fs.writeFile( __dirname + '/glitched-lincoln.png', glitchedBuffer, function ( err ) {
if ( err ) { throw err; }
console.log( 'file glitched.' );
} );
} );
} );
please note: this method is only available in node.
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( './glitched-lincoln.png' );
glitch()
.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 contains an image.
toDataURL()
does not take any parameters. it returns a String
containing the base64-encoded image url.
example:
var image = new Image();
image.onload = function () {
glitch()
.fromImage( image )
.toDataURL()
.then( function( dataURL ) {
var glitchedImg = new Image();
glitchedImg.src = dataURL;
document.body.appendChild( glitchedImg );
} );
};
image.src = 'lincoln.jpg'
toImageData()
does not take any parameters. it returns an ImageData
object.
example:
var image = new Image();
image.onload = function () {
glitch()
.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'
toImage()
does not take any parameters. it returns an Image
object.
example:
var image = new Image();
image.onload = function () {
glitch()
.fromImage( image )
.toImage()
.then( function ( glitchedImage ) {
document.body.appendChild( glitchedImage );
} );
};
image.src = 'lincoln.jpg'
please note: this method is only available in the browser.
toBuffer()
doesn't take any parameters. 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; }
glitch()
.fromBuffer( buffer )
.toBuffer()
.then( function ( imageBuffer ) {
fs.writeFile( './glitched-lincoln.png', imageBuffer, function ( err ) {
if ( err ) { throw err; }
console.log( 'created a pdf file.' );
} );
} );
} );
please note: this method is only available in node.
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
};
it uses canvas#jpegstream() from node-canvas internally.
it returns a JPEGStream
object.
example:
var fs = require('fs');
fs.readFile( __dirname + '/lincoln.jpg', function ( err, buffer ) {
if ( err ) {
throw err;
}
var fileStream = fs.createWriteStream( __dirname + '/glitched-lincoln.jpg' );
glitch()
.fromBuffer( buffer )
.toJPGStream()
.then( function ( jpgStream ) {
jpgStream.on( 'data', function ( chunk ) { fileStream.write( chunk ); } );
jpgStream.on( 'end', function () { console.log( 'file glitched.' ); } );
} );
} );
please note: this method is only available in node.
see toJPGStream()
.
toPNGStream()
doesn't take any parameters. it uses canvas#pngstream() from node-canvas internally.
it returns a PNGStream
object.
example:
var fs = require('fs');
fs.readFile( __dirname + '/lincoln.jpg', function ( err, buffer ) {
if ( err ) { throw err; }
var fileStream = fs.createWriteStream( __dirname + '/glitched-lincoln.png' );
glitch()
.fromBuffer( buffer )
.toPNGStream()
.then( function ( pngStream ) {
pngStream.on( 'data', function ( chunk ) { fileStream.write( chunk ); } );
pngStream.on( 'end', function () { console.log( 'file glitched.' ); } );
}, function( err ) {
console.log( 'There was an error', err );
} );
} );
please note: this method is only available in node.
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.
you can find the source code for the library in the src
folder. it is using es6-style syntax.
most of the folder structure and the npm script setup were taken from nonalnawson's hello javascript repo (Apache-2.0 license).
dependencies:
if you want to add your site or project to one of these lists, you can create a pull request or send me an email.
found a bug? missing a feature? instructions unclear? are you using this library in an interesting project? maybe have a look at the issues, open a pull request and let me know. thanks!
thank you for taking a look at this repo. have a great day :)
FAQs
JavaScript library for applying a glitch effect to a canvas element
We found that glitch-canvas demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.