@image/packer
Version:
image packer
46 lines (45 loc) • 2.11 kB
JavaScript
import Spritesheet from './Spritesheet';
import { MultiBinPacker } from './MultiBinPacker';
var AtlasGroup = (function () {
function AtlasGroup(scaledSprites, layoutConfig, exportConfig, cache, imageProcessor, log) {
this.scaledSprites = scaledSprites;
this.layoutConfig = layoutConfig;
this.exportConfig = exportConfig;
this.cache = cache;
this.imageProcessor = imageProcessor;
this.log = log;
this.spritesheets = [];
this.hash = this.calculateHash();
}
AtlasGroup.prototype.calculateHash = function () {
var str = this.scaledSprites.map(function (i) { return i.hash; }).sort().join(" ");
str += JSON.stringify(this.layoutConfig, null, 2);
str += JSON.stringify(this.exportConfig, null, 2);
return this.cache.createHash(str);
};
AtlasGroup.prototype.process = function (queue) {
var _this = this;
var packer = new MultiBinPacker(this.layoutConfig.max_width, this.layoutConfig.max_height, this.layoutConfig.padding);
packer.addArray(this.scaledSprites.map(function (scaledSprite) {
return {
width: scaledSprite.trim ? scaledSprite.trim.width : scaledSprite.width,
height: scaledSprite.trim ? scaledSprite.trim.height : scaledSprite.height,
data: scaledSprite
};
}));
if (this.layoutConfig.oversized_warning) {
packer.oversizedElements.map(function (bin) {
_this.log.warn('Oversized sprite: ' + bin.data.sprite.path +
' with size ' + bin.width + 'x' + bin.height);
});
}
this.spritesheets = packer.bins.map(function (bin) {
return new Spritesheet(_this, bin, _this.exportConfig, _this.cache, _this.imageProcessor);
});
return Promise.all(this.spritesheets.map(function (spritesheet) {
return queue.add(function () { return spritesheet.process(); });
}));
};
return AtlasGroup;
}());
export default AtlasGroup;