Skip to content
A standalone canvas manipulation library for JS.
JavaScript Other
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
dist
example
lib
test
.gitignore
Gruntfile.js
LICENSE.md
README.md
circle.yml
package.json

README.md

Carapace

Build Status

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 carapace

Browser

Download:

API

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:

  • id String (required) A unique identifier for the job.
  • runSync Function A synchronous canvas manipulation. Should return a Canvas.
  • run Function An 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:

  • id String (required) A unique identifier for the filter.
  • pixel Function An RGBA pixel manipulation. Should return an Array of 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) // true

uil.isCanvas(obj)

Returns true if the object is a Canvas.

Carapace.util.isCanvas(canvasA) // true

uil.isCarapace(obj)

Returns true if the object is a Carapace.

Carapace.util.isCarapace(yourCarapace) // true

uil.isImage(obj)

Returns true if the object is an Image.

Carapace.util.isImage(yourImage) // true

Example

Run the example server at http://127.0.0.1:3000:

$ npm run example

Tests

$ grunt test
$ grunt test:server
$ grunt test:browser

Builds

Build standalone dist/carapace.js and dist/carapace.min.js files:

$ grunt dist

License

MIT License, see LICENSE for details.

Something went wrong with that request. Please try again.