@image/packer
Version:
image packer
100 lines (99 loc) • 3.83 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var ScaledSprite = (function () {
function ScaledSprite(sprite, convertOptions, cache, imageProcessor) {
this.sprite = sprite;
this.convertOptions = convertOptions;
this.cache = cache;
this.imageProcessor = imageProcessor;
}
Object.defineProperty(ScaledSprite.prototype, "scaleHash", {
get: function () {
var c = this.convertOptions;
return (c.trim ? 't' : '') + "_" + c.maxHeight + "_" + c.maxWidth + "_" + c.scaleX + "_" + c.scaleY + "_" + c.dontExtent;
},
enumerable: true,
configurable: true
});
Object.defineProperty(ScaledSprite.prototype, "basename", {
get: function () {
return "scaled_sprite_" + this.sprite.hash + "_" + this.scaleHash + ".png";
},
enumerable: true,
configurable: true
});
ScaledSprite.prototype.cacheMiss = function () {
var _this = this;
var c = this.convertOptions;
var scaleX = c.scaleX === undefined || c.scaleY == null ? 1 : c.scaleX;
var scaleY = c.scaleY === undefined || c.scaleY == null ? 1 : c.scaleY;
var dontExtent = !!c.dontExtent;
var maxWidth = c.maxWidth === undefined || c.maxWidth == null ? 0 : c.maxWidth;
var maxHeight = c.maxHeight === undefined || c.maxHeight == null ? 0 : c.maxHeight;
var w = this.sprite.width * scaleX;
var h = this.sprite.height * scaleY;
if (maxWidth > 0 && maxHeight > 0) {
if (!dontExtent) {
if (w < maxWidth) {
h = maxWidth * h / w;
w = maxWidth;
}
if (h < maxHeight) {
w = maxHeight * w / h;
h = maxHeight;
}
}
if (w > maxWidth) {
h = maxWidth * h / w;
w = maxWidth;
}
if (h > maxHeight) {
w = maxHeight * w / h;
h = maxHeight;
}
}
var width = Math.round(w);
var height = Math.round(h);
var path = this.cache.getCachePath(this.basename);
return this.imageProcessor
.scale(this.sprite.path, path, { width: width, height: height })
.then(function () {
if (_this.convertOptions.trim) {
return _this.imageProcessor.trim(path, path);
}
else {
return { x: 0, y: 0, height: height, width: width };
}
})
.then(function (trim) {
var t = trim;
if (trim.x === 0 && trim.y === 0 && trim.width === width && trim.height === height) {
t = undefined;
}
return { width: width, height: height, trim: t, path: path };
});
};
Object.defineProperty(ScaledSprite.prototype, "hash", {
get: function () {
return this.sprite.hash + "_" + this.scaleHash;
},
enumerable: true,
configurable: true
});
ScaledSprite.prototype.cacheInterpret = function (data) {
this.width = data.width;
this.height = data.height;
this.trim = data.trim;
this.path = data.path;
};
ScaledSprite.prototype.process = function () {
var _this = this;
var key = this.sprite.hash + "_" + this.scaleHash;
return this.cache.lookup('scaledSprite', key, this.cacheMiss.bind(this))
.then(this.cacheInterpret.bind(this))
.then(function () { return _this; });
};
;
return ScaledSprite;
}());
exports.default = ScaledSprite;