Skip to content

Commit 084ff3a

Browse files
committed
improved tests, lowered saturationBrightnessMin, added isAvailable
1 parent b4d89e7 commit 084ff3a

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

smartcrop.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ SmartCrop.DEFAULTS = {
4242
skinBrightnessMax: 1.0,
4343
skinThreshold: 0.8,
4444
skinWeight: 1.8,
45-
saturationBrightnessMin: 0.25,
45+
saturationBrightnessMin: 0.05,
4646
saturationBrightnessMax: 0.9,
4747
saturationThreshold: 0.4,
4848
saturationBias: 0.2,
@@ -116,6 +116,18 @@ SmartCrop.crop = function(image, options, callback){
116116
callback(result);
117117
return result;
118118
};
119+
// check if all the dependencies are there
120+
SmartCrop.isAvailable = function(options){
121+
try {
122+
var s = new this(options),
123+
c = s.canvas(16, 16);
124+
if(typeof c.getContext !== 'function') return false;
125+
return true;
126+
}
127+
catch(e){
128+
return false;
129+
}
130+
};
119131
SmartCrop.prototype = {
120132
canvas: function(w, h){
121133
if(this.options.canvasFactory !== null){

test/smartcrop.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,34 @@ describe("SmartCrop", function() {
1313
expect(result.topCrop.width).to.be.within(1, img.width);
1414
expect(result.topCrop.height).to.be.within(1, img.height);
1515
}
16+
describe("isAvailable", function(){
17+
it("should return true when canvas is available", function(){
18+
expect(SmartCrop.isAvailable()).to.equal(true);
19+
});
20+
it("should return false when canvas is not available", function(){
21+
expect(SmartCrop.isAvailable({canvasFactory: function(){}})).to.equal(false);
22+
});
23+
});
1624
describe("crop", function() {
25+
it("should do something sane", function(done){
26+
var c = document.createElement('canvas'),
27+
ctx = c.getContext('2d');
28+
c.width = 128;
29+
c.height = 64;
30+
ctx.fillStyle = 'white';
31+
ctx.fillRect(0, 0, 128, 64);
32+
ctx.fillStyle = 'red';
33+
ctx.fillRect(96, 32, 16, 16);
34+
SmartCrop.crop(c, {debug: false}, function(result){
35+
//document.body.appendChild(c);
36+
//document.body.appendChild(result.debugCanvas);
37+
expect(result.topCrop.x).to.be.lessThan(96);
38+
expect(result.topCrop.y).to.be.lessThan(32);
39+
expect(result.topCrop.x+result.topCrop.width).to.be.greaterThan(112);
40+
expect(result.topCrop.y+result.topCrop.height).to.be.greaterThan(48);
41+
done();
42+
});
43+
});
1744
it("should adhere to minScale", function(done) {
1845
SmartCrop.crop(img, {minScale: 1}, function(result){
1946
validResult(result);

0 commit comments

Comments
 (0)