步骤 2b:
linear_partition 算法将采用一组照片权重(计算为它们的纵横比乘以 100,四舍五入)以及您想要排列它们的行数。然后它将返回一个二维数组,其中第一维是一个数组行和第二个维度是行的内容。像这样:
linear_partition([9,2,6,3,8,5,8,1,7,3,4], 3) => [[9,2,6,3],[8,5,8],[1,7,3,4]]
第 3 步:
在这一步中,他们遍历分区并使用该数据来正确调整照片的大小,以使它们均匀分布。请参阅下面的评论。
weights = photos.map(function (p) {
return parseInt(p.get('aspect_ratio') * 100);
});
// Call the partition module and get a 2-dimensional array as return value
partition = linear_partition(weights, rows);
index = 0;
// Create a new collection to temporarily hold data
row_buffer = new Backbone.Collection();
// Iterate through each row
_.each(partition, function (row) {
var summed_ratios;
row_buffer.reset(); // reset the collection
// Iterate through each photo in the row and add
// the photo at that index to our temp collection
_.each(row, function () {
return row_buffer.add(photos.at(index++));
});
// The reduce function will sum the aspect ratio of all photos in the row
// Read more about the reduce function at http://underscorejs.org/#reduce
summed_ratios = row_buffer.reduce((function (sum, p) {
return sum += p.get('aspect_ratio');
}), 0);
// Iterate through all the photos in the row (in temp collection)
// For each photo, call the resize() function which I assume is their custom
// function which sets the correct size for each photo
return row_buffer.each(function (photo) {
return photo.view.resize(parseInt(viewport_width / summed_ratios * photo.get('aspect_ratio')), parseInt(viewport_width / summed_ratios));
});
});