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

carapace

v1.0.2

Published

Manipulate images with JavaScript

Downloads

19

Readme

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.