2013-10-23 03:01:35 +04:00
|
|
|
(function () {
|
|
|
|
var ns = $.namespace('pskl.utils');
|
|
|
|
|
|
|
|
ns.ImageResizer = {
|
2013-10-24 01:34:09 +04:00
|
|
|
resize : function (image, targetWidth, targetHeight, smoothingEnabled) {
|
2013-10-23 03:01:35 +04:00
|
|
|
var canvas = pskl.CanvasUtils.createCanvas(targetWidth, targetHeight);
|
|
|
|
var context = canvas.getContext('2d');
|
|
|
|
context.save();
|
2013-10-24 01:34:09 +04:00
|
|
|
|
|
|
|
if (!smoothingEnabled) {
|
|
|
|
this.disableSmoothingOnContext(context);
|
|
|
|
}
|
|
|
|
|
2013-10-23 03:01:35 +04:00
|
|
|
context.translate(canvas.width / 2, canvas.height / 2);
|
|
|
|
context.scale(targetWidth / image.width, targetHeight / image.height);
|
|
|
|
context.drawImage(image, -image.width / 2, -image.height / 2);
|
|
|
|
context.restore();
|
|
|
|
|
|
|
|
return canvas;
|
2013-10-24 01:34:09 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
disableSmoothingOnContext : function (context) {
|
|
|
|
context.imageSmoothingEnabled = false;
|
|
|
|
context.mozImageSmoothingEnabled = false;
|
|
|
|
context.oImageSmoothingEnabled = false;
|
|
|
|
context.webkitImageSmoothingEnabled = false;
|
|
|
|
context.msImageSmoothingEnabled = false;
|
2013-10-23 03:01:35 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|