carapace v1.0.2
Carapace
Manipulate images with JavaScript
Carapace makes it easy to create reusable image editing operations. For example:
- apply custom filters to a photograph
- scale, crop and rotate
- overlay fonts and graphics
Operations can be combined as needed, and shared in the browser and on the server.
Usage
Load an image into Carapace and run some operations on it:
var Carapace = require('carapace')
var Crop = require('carapace-crop')
Carapace
.create(imageEl)
.run([
Crop.create({ width: 100, height: 100, left: 100, top: 100 })
], function(err, canvas){
canvas // [object HTMLCanvasElement 100x100]
})Jobs and Filters are reusable canvas operations created with Carapace.Job.extend() and Carapace.Filter.extend(), respectively. Extend these objects to create custom canvas operations and we'll publish them here.
Jobs
Filters
COMING SOON
Installation
node.js
Carapace for node.js depends on node-canvas which depends on some other stuff. IMPORTANT Follow the installation instructions for your system prior to installing Carapace.
Once you've installed the dependencies, install Carapace with npm:
$ npm install carapaceBrowser
Download:
API
Carapace.register()Carapace.create()Canvas.create()Font.create()Image.create()Job.extend()Filter.extend()Queue.create()util
register(string|fn|arr)
Registers a job with Carapace. This method will pre-load and cache jobs for later use. All jobs should be registered at the start of the app.
Carapace.register('./your-filter')
Carapace.register(YourJob)
Carapace.register([
YourOtherJob,
'./your-other-filter'
])create([imageEl|canvasEl|buffer|canvas|carapace])
Creates a carapace object.
In node.js:
fs.readFile('./your-image.png', function(err, buf){
Carapace.create(buf)
})
Carapace.create(Carapace.Canvas.create())In the browser:
Carapace.Image
.create('your-image.png')
.load(function(err, image){
Carapace.create(image)
})
Carapace.create(document.getElementById('your-canvas'))carapace.run(queue|arr[, callback])
Executes a job queue on a carapace object. This method is asynchronous and will invoke an optional callback when execution is complete.
carapace.run(queue, function(err, canvas){
// done!
})
carapace.run([
{ id: 'resize', options: { width: 500 } }
], function(err, canvas){
// done!
})Canvas.create([imageEl|canvasEl|buffer|canvas|carapace])
Creates a canvas.
var canvas = Carapace.Canvas.create()Font.create(family, src[, weight][, style])
Creates a Font for node-canvas.
var font = Carapace.Font.create('Open Sans', './path/to/your-font.ttf')Image.create(src|buffer)
Creates an image.
var image = Carapace.Image.create('your-image.png')Job.extend(options)
Extends the Job object. Extend Job to define general canvas manipulations.
The options object should have the following properties:
idString(required) A unique identifier for the job.runSyncFunctionA synchronous canvas manipulation. Should return aCanvas.runFunctionAn asynchronous canvas manipulation. Accepts a
var YourJob = Carapace.Job.extend({
id: 'your-job',
runSync: function (canvas, options) {
return canvas
},
run: function (canvas, options, callback) {
return callback(null, canvas)
}
})Filter.extend(options)
Extends the Filter object. Extend Filter to define pixel-wise canvas manipulations.
The options object should have the following properties:
idString(required) A unique identifier for the filter.pixelFunctionAn RGBA pixel manipulation. Should return anArrayof RGBA values.
var YourFilter = Carapace.Filter.extend({
id: 'your-job',
pixel: function (r, g, b) {
return [255, 255, 255, 255]
}
})Queue.create([arr])
Creates a queue object. Optionally accepts a serialized queue array.
var queue = Carapace.Queue.create([{
id: 'resize',
options: { width: 500 }
}])queue.add(id[, options]|job)
Adds a job to the end of the queue.
queue
.add('resize', { width: 500 })
.add(Sepia.create({ adjust: 50 }))queue.remove()
Removes a job from the end of the queue.
queue.remove()queue.serialize()
Serializes a queue to an array of jobs.
var arr = queue.serialize() // [{ id: 'resize', options: { width: 500 } }]uil.compare(canvas, canvas)
Compares two canvas. Returns true if the canvases have idential contents.
Carapace.util.compare(canvasA, canvasB) // trueuil.isCanvas(obj)
Returns true if the object is a Canvas.
Carapace.util.isCanvas(canvasA) // trueuil.isCarapace(obj)
Returns true if the object is a Carapace.
Carapace.util.isCarapace(yourCarapace) // trueuil.isImage(obj)
Returns true if the object is an Image.
Carapace.util.isImage(yourImage) // trueExample
Run the example server at http://127.0.0.1:3000:
$ npm run exampleTests
$ grunt test
$ grunt test:server
$ grunt test:browserBuilds
Build standalone dist/carapace.js and dist/carapace.min.js files:
$ grunt distLicense
MIT License, see LICENSE for details.
