Skip to content

Commit

Permalink
start #88 - image.contain
Browse files Browse the repository at this point in the history
  • Loading branch information
EyalAr committed Dec 17, 2014
1 parent 1a267d5 commit c053fac
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
16 changes: 16 additions & 0 deletions examples/contain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Example for using LWIP to contain an image in a canvas.
*/

var path = require('path'),
lwip = require('../');

lwip.open('lena.jpg', function(err, image) {
if (err) return console.log(err);
image.contain(400,700,'green',function(err, image){
image.writeFile('lena_contain.jpg', function(err){
if (err) return console.log(err);
console.log('done');
});
});
});
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require('./lib/ImagePrototypeInit');
require('./lib/BatchPrototypeInit');

require('./lib/ImagePrototypeExtensions');

module.exports = require('./lib/obtain');
34 changes: 34 additions & 0 deletions lib/ImagePrototypeExtensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(function(undefined) {

var Image = require('./Image'),
decree = require('decree'),
defs = require('./defs');

var judges = {
contain: decree(defs.args.contain)
};

Image.prototype.contain = function() {
var that = this;
judges.contain(
arguments,
function(width, height, color, inter, callback) {
var s = Math.min(width / that.width(), height / that.height());
that.scale(s, s, inter, function(err){
if (err) return callback(err);
var padX = (width - that.width()) / 2,
padY = (height - that.height()) / 2;
that.pad(
Math.ceil(padX),
Math.ceil(padY),
Math.floor(padX),
Math.floor(padY),
color,
callback
);
});
}
);
};

})(void 0);
25 changes: 23 additions & 2 deletions lib/defs.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
GIF_DEF_INTERLACED: false,
GIF_DEF_TRANSPARENT: 'auto',
GIF_DEF_THRESHOLD: 50,
DEF_CREATE_COLOR: [0, 0, 0, 0]
DEF_CREATE_COLOR: [0, 0, 0, 0],
DEF_CONTAIN_COLOR: [0, 0, 0, 0]
};

var interpolations = exports.interpolations = {
Expand Down Expand Up @@ -365,7 +366,27 @@
}, {
name: 'callback',
type: 'function'
}]
}],
contain: [{
name: 'width',
type: 'p-number'
}, {
name: 'height',
type: 'p-number'
}, {
name: 'color',
type: 'color',
optional: true,
default: defaults.DEF_CONTAIN_COLOR
}, {
name: 'interpolation',
type: 'interpolation',
optional: true,
default: interpolations[defaults.DEF_INTERPOLATION]
}, {
name: 'callback',
type: 'function'
}],
};

})();

0 comments on commit c053fac

Please sign in to comment.