Skip to content

Commit cff0be6

Browse files
committed
Messing with the testbed, rounding crops
1 parent fdf6fe2 commit cff0be6

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

examples/style.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,14 @@ input[type=range] {
1212
width: 300px;
1313
}
1414
img, canvas { padding: 8px; box-sizing: border-box; max-width: 100%; margin: auto; }
15+
16+
.output {
17+
width: 75%;
18+
float: left;
19+
}
20+
.sidebar {
21+
width: 25%;
22+
float: left;
23+
height: 600px;
24+
overflow: auto;
25+
}

examples/testbed.html

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ <h1>smartcrop.js testbed</h1>
1515
</form>
1616
</div>
1717
<h2>Drop images on this page to analyze them.</h2>
18-
<canvas id="c"></canvas>
19-
<div id=debug></div>
18+
<div class=output>
19+
<canvas id="c"></canvas>
20+
<div id=debug></div>
21+
</div>
22+
<div class=sidebar>
23+
<div class=crops>
24+
</div>
25+
</div>
2026
<script src=jquery.js></script>
2127
<script src=underscore.js></script>
2228
<script src="../smartcrop.js"></script>

examples/testbed.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
var canvas = $('canvas')[0],
33
form = document.forms[0],
44
ctx = canvas.getContext('2d'),
5-
img;
5+
img, crop;
66
$('html')
77
.on('dragover', function(e) {e.preventDefault(); return false;})
88
.on('drop', function(e) {
@@ -13,33 +13,53 @@ $('html')
1313
var reader = new FileReader();
1414
// Note: addEventListener doesn't work in Google Chrome for this event
1515
reader.onload = function (evt) {
16-
img = new Image();
17-
img.onload = function(){
18-
analyze();
19-
};
20-
img.src = evt.target.result;
21-
};
16+
load(evt.target.result);
17+
};
2218
reader.readAsDataURL(file);
2319
}
2420
}
2521
return false;
2622
});
23+
load('images/flickr/kitty.jpg');
2724
$('input[type=range]').change(_.debounce(function(){
2825
$(this).next('.value').text($(this).val());
2926
analyze();
3027
}));
28+
function load(src){
29+
img = new Image();
30+
img.onload = function(){
31+
analyze();
32+
};
33+
img.src = src;
34+
35+
}
3136
function analyze(){
3237
if(!img) return;
3338
SmartCrop.crop(img, {width: form.width.value, height: form.height.value, debug: true}, draw);
3439
}
3540
function draw(result){
41+
selectedCrop = result.topCrop;
42+
$('.crops').append(_.sortBy(result.crops, function(c){return -c.score.total;}).map(function(crop){
43+
return $('<p>')
44+
.text('Score: ' + ~~(crop.score.total*10000000) + ', ' + crop.x+'x'+crop.y)
45+
.hover(function(){
46+
drawCrop(crop);
47+
}, function(){
48+
drawCrop(selectedCrop);
49+
})
50+
.click(function(){ selectedCrop = crop; drawCrop(selectedCrop); })
51+
.data('crop', crop);
52+
}));
53+
drawCrop(selectedCrop);
54+
$('#debug').empty().append(result.debugCanvas);
55+
}
56+
function drawCrop(crop){
3657
canvas.width = img.width;
3758
canvas.height = img.height;
3859
ctx.drawImage(img, 0, 0);
3960
ctx.strokeStyle = 'red';
4061
ctx.lineWidth = 4;
41-
ctx.strokeRect(result.topCrop.x, result.topCrop.y, result.topCrop.width, result.topCrop.height);
42-
$('#debug').empty().append(result.debugCanvas);
62+
ctx.strokeRect(crop.x, crop.y, crop.width, crop.height);
4363
}
4464

4565
})();

smartcrop.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ SmartCrop.crop = function(image, options, callback){
109109
var result = smartCrop.analyse(image);
110110
for(var i = 0, i_len = result.crops.length; i < i_len; i++) {
111111
var crop = result.crops[i];
112-
crop.x /= prescale;
113-
crop.y /= prescale;
114-
crop.width /= prescale;
115-
crop.height /= prescale;
112+
crop.x = ~~(crop.x/prescale);
113+
crop.y = ~~(crop.y/prescale);
114+
crop.width = ~~(crop.width/prescale);
115+
crop.height = ~~(crop.height/prescale);
116116
}
117117
callback(result);
118118
return result;

0 commit comments

Comments
 (0)