2012-09-20 02:43:39 +04:00
|
|
|
(function () {
|
2014-09-21 23:56:22 +04:00
|
|
|
var ns = $.namespace('pskl.utils');
|
2012-09-20 02:43:39 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
ns.CanvasUtils = {
|
|
|
|
createCanvas : function (width, height, classList) {
|
2014-09-21 23:56:22 +04:00
|
|
|
var canvas = document.createElement('canvas');
|
|
|
|
canvas.setAttribute('width', width);
|
|
|
|
canvas.setAttribute('height', height);
|
2012-09-20 02:43:39 +04:00
|
|
|
|
2014-09-21 23:56:22 +04:00
|
|
|
if (typeof classList == 'string') {
|
2013-08-10 14:11:16 +04:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2014-04-12 20:40:25 +04:00
|
|
|
createFromImageData : function (imageData) {
|
2014-09-21 23:56:22 +04:00
|
|
|
var canvas = pskl.utils.CanvasUtils.createCanvas(imageData.width, imageData.height);
|
2014-04-12 20:40:25 +04:00
|
|
|
var context = canvas.getContext('2d');
|
|
|
|
context.putImageData(imageData, 0, 0);
|
|
|
|
return canvas;
|
|
|
|
},
|
|
|
|
|
2014-08-22 02:37:35 +04:00
|
|
|
createFromImage : function (image) {
|
2014-09-21 23:56:22 +04:00
|
|
|
var canvas = pskl.utils.CanvasUtils.createCanvas(image.width, image.height);
|
2014-08-22 02:37:35 +04:00
|
|
|
var context = canvas.getContext('2d');
|
|
|
|
context.drawImage(image, 0, 0);
|
|
|
|
return canvas;
|
|
|
|
},
|
|
|
|
|
2015-06-01 20:29:52 +03:00
|
|
|
/**
|
|
|
|
* Splits the specified image into several new canvas elements based on the
|
2015-06-03 07:54:26 +03:00
|
|
|
* supplied offset and frame sizes
|
2015-06-01 20:29:52 +03:00
|
|
|
* @param image The source image that will be split
|
2015-06-03 07:54:26 +03:00
|
|
|
* @param {Number} offsetX The padding from the left side of the source image
|
|
|
|
* @param {Number} offsetY The padding from the top side of the source image
|
|
|
|
* @param {Number} width The width of an individual frame
|
|
|
|
* @param {Number} height The height of an individual frame
|
|
|
|
* @param {Boolean} useHorizonalStrips True if the frames should be layed out from left to
|
|
|
|
* right, False if it should use top to bottom
|
|
|
|
* @param {Boolean} ignoreEmptyFrames True to ignore empty frames, false to keep them
|
2015-06-01 20:29:52 +03:00
|
|
|
* @returns {Array} An array of canvas elements that contain the split frames
|
|
|
|
*/
|
2015-06-03 07:54:26 +03:00
|
|
|
createFramesFromImage : function (image, offsetX, offsetY, width, height, useHorizonalStrips, ignoreEmptyFrames) {
|
2015-06-01 20:29:52 +03:00
|
|
|
var canvasArray = [];
|
2015-06-03 07:54:26 +03:00
|
|
|
var x = offsetX;
|
|
|
|
var y = offsetY;
|
|
|
|
var blankData = pskl.utils.CanvasUtils.createCanvas(width, height).toDataURL();
|
2015-06-01 20:29:52 +03:00
|
|
|
|
2015-06-04 05:48:29 +03:00
|
|
|
while (x + width <= image.width && y + height <= image.height) {
|
2015-06-03 07:54:26 +03:00
|
|
|
// Create a new canvas element
|
|
|
|
var canvas = pskl.utils.CanvasUtils.createCanvas(width, height);
|
|
|
|
var context = canvas.getContext('2d');
|
2015-06-01 20:29:52 +03:00
|
|
|
|
2015-06-03 07:54:26 +03:00
|
|
|
// Blit the correct part of the source image into the new canvas
|
|
|
|
context.drawImage(
|
|
|
|
image,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
width,
|
|
|
|
height);
|
2015-06-01 20:29:52 +03:00
|
|
|
|
2015-06-03 07:54:26 +03:00
|
|
|
if (!ignoreEmptyFrames || canvas.toDataURL() !== blankData) {
|
2015-06-01 20:29:52 +03:00
|
|
|
canvasArray.push(canvas);
|
|
|
|
}
|
2015-06-03 07:54:26 +03:00
|
|
|
|
|
|
|
if (useHorizonalStrips) {
|
|
|
|
// Move from left to right
|
|
|
|
x += width;
|
|
|
|
if (x + width > image.width) {
|
|
|
|
x = offsetX;
|
|
|
|
y += height;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Move from top to bottom
|
|
|
|
y += height;
|
|
|
|
if (y + height > image.height) {
|
|
|
|
x += width;
|
|
|
|
y = offsetY;
|
|
|
|
}
|
|
|
|
}
|
2015-06-01 20:29:52 +03:00
|
|
|
}
|
2015-06-03 07:54:26 +03:00
|
|
|
|
2015-06-01 20:29:52 +03:00
|
|
|
return canvasArray;
|
|
|
|
},
|
|
|
|
|
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) {
|
2015-02-26 02:13:32 +03:00
|
|
|
pskl.utils.CanvasUtils.setImageSmoothing(canvas, false);
|
|
|
|
},
|
|
|
|
|
|
|
|
enableImageSmoothing : function (canvas) {
|
|
|
|
pskl.utils.CanvasUtils.setImageSmoothing(canvas, true);
|
|
|
|
},
|
|
|
|
|
|
|
|
setImageSmoothing : function (canvas, smoothing) {
|
2013-10-30 01:16:39 +04:00
|
|
|
var context = canvas.getContext('2d');
|
2015-02-26 02:13:32 +03:00
|
|
|
context.imageSmoothingEnabled = smoothing;
|
|
|
|
context.mozImageSmoothingEnabled = smoothing;
|
|
|
|
context.oImageSmoothingEnabled = smoothing;
|
|
|
|
context.webkitImageSmoothingEnabled = smoothing;
|
|
|
|
context.msImageSmoothingEnabled = smoothing;
|
2013-10-30 01:16:39 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
clear : function (canvas) {
|
|
|
|
if (canvas) {
|
2014-09-21 23:56:22 +04:00
|
|
|
canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
|
2013-10-30 01:16:39 +04:00
|
|
|
}
|
2013-11-05 03:05:49 +04:00
|
|
|
},
|
|
|
|
|
2014-06-30 22:38:14 +04:00
|
|
|
clone : function (canvas) {
|
2014-09-21 23:56:22 +04:00
|
|
|
var clone = pskl.utils.CanvasUtils.createCanvas(canvas.width, canvas.height);
|
2014-06-30 22:38:14 +04:00
|
|
|
|
|
|
|
//apply the old canvas to the new one
|
|
|
|
clone.getContext('2d').drawImage(canvas, 0, 0);
|
|
|
|
|
|
|
|
//return the new canvas
|
|
|
|
return clone;
|
|
|
|
},
|
|
|
|
|
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;
|
2014-04-11 03:12:01 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
getBase64FromCanvas : function (canvas, format) {
|
2014-09-21 23:56:22 +04:00
|
|
|
format = format || 'png';
|
|
|
|
var data = canvas.toDataURL('image/' + format);
|
2015-04-14 19:02:33 +03:00
|
|
|
return data.substr(data.indexOf(',') + 1);
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
|
|
|
};
|
2015-04-14 19:02:33 +03:00
|
|
|
})();
|