2012-09-20 02:43:39 +04:00
|
|
|
(function () {
|
2013-08-10 14:11:16 +04:00
|
|
|
var ns = $.namespace("pskl");
|
2012-09-20 02:43:39 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
ns.CanvasUtils = {
|
|
|
|
createCanvas : function (width, height, classList) {
|
|
|
|
var canvas = document.createElement("canvas");
|
|
|
|
canvas.setAttribute("width", width);
|
|
|
|
canvas.setAttribute("height", height);
|
2012-09-20 02:43:39 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
if (typeof classList == "string") {
|
|
|
|
classList = [classList];
|
|
|
|
}
|
|
|
|
if (Array.isArray(classList)) {
|
|
|
|
for (var i = 0 ; i < classList.length ; i++) {
|
|
|
|
canvas.classList.add(classList[i]);
|
|
|
|
}
|
|
|
|
}
|
2013-10-30 01:16:39 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
return canvas;
|
2013-10-30 01:16:39 +04:00
|
|
|
},
|
|
|
|
|
2013-11-15 03:32:18 +04:00
|
|
|
/**
|
|
|
|
* By default, all scaling operations on a Canvas 2D Context are performed using antialiasing.
|
|
|
|
* Resizing a 32x32 image to 320x320 will lead to a blurry output.
|
|
|
|
* On Chrome, FF and IE>=11, this can be disabled by setting a property on the Canvas 2D Context.
|
|
|
|
* In this case the browser will use a nearest-neighbor scaling.
|
|
|
|
* @param {Canvas} canvas
|
|
|
|
*/
|
2013-10-30 01:16:39 +04:00
|
|
|
disableImageSmoothing : function (canvas) {
|
|
|
|
var context = canvas.getContext('2d');
|
|
|
|
context.imageSmoothingEnabled = false;
|
|
|
|
context.mozImageSmoothingEnabled = false;
|
|
|
|
context.oImageSmoothingEnabled = false;
|
|
|
|
context.webkitImageSmoothingEnabled = false;
|
|
|
|
context.msImageSmoothingEnabled = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
clear : function (canvas) {
|
|
|
|
if (canvas) {
|
|
|
|
canvas.getContext("2d").clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
}
|
2013-11-05 03:05:49 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
getImageDataFromCanvas : function (canvas) {
|
|
|
|
var sourceContext = canvas.getContext('2d');
|
|
|
|
return sourceContext.getImageData(0, 0, canvas.width, canvas.height).data;
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
|
|
|
};
|
2012-09-20 02:43:39 +04:00
|
|
|
})();
|