2013-04-07 22:16:47 +04:00
|
|
|
(function () {
|
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
var ns = $.namespace("pskl.rendering");
|
|
|
|
ns.CanvasRenderer = function (frame, dpi) {
|
|
|
|
this.frame = frame;
|
|
|
|
this.dpi = dpi;
|
2013-10-09 01:44:06 +04:00
|
|
|
this.transparentColor_ = 'white';
|
2013-08-10 14:11:16 +04:00
|
|
|
};
|
2013-04-07 22:16:47 +04:00
|
|
|
|
2013-10-09 01:44:06 +04:00
|
|
|
/**
|
|
|
|
* Decide which color should be used to represent transparent pixels
|
|
|
|
* Default : white
|
|
|
|
* @param {String} color the color to use either as '#ABCDEF' or 'red' or 'rgb(x,y,z)' or 'rgba(x,y,z,a)'
|
|
|
|
*/
|
|
|
|
ns.CanvasRenderer.prototype.drawTransparentAs = function (color) {
|
|
|
|
this.transparentColor_ = color;
|
|
|
|
};
|
|
|
|
|
|
|
|
ns.CanvasRenderer.prototype.render = function () {
|
2013-08-10 14:11:16 +04:00
|
|
|
var canvas = this.createCanvas_();
|
|
|
|
var context = canvas.getContext('2d');
|
|
|
|
for(var col = 0, width = this.frame.getWidth(); col < width; col++) {
|
|
|
|
for(var row = 0, height = this.frame.getHeight(); row < height; row++) {
|
|
|
|
var color = this.frame.getPixel(col, row);
|
|
|
|
this.renderPixel_(color, col, row, context);
|
|
|
|
}
|
|
|
|
}
|
2013-04-07 22:16:47 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
return context;
|
|
|
|
};
|
2013-04-07 22:16:47 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
ns.CanvasRenderer.prototype.renderPixel_ = function (color, col, row, context) {
|
2013-10-09 01:44:06 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
if(color == Constants.TRANSPARENT_COLOR) {
|
2013-10-09 01:44:06 +04:00
|
|
|
color = this.transparentColor_;
|
2013-08-10 14:11:16 +04:00
|
|
|
}
|
|
|
|
context.fillStyle = color;
|
2013-10-09 01:44:06 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
context.fillRect(col * this.dpi, row * this.dpi, this.dpi, this.dpi);
|
|
|
|
};
|
2013-04-07 22:16:47 +04:00
|
|
|
|
2013-08-10 14:11:16 +04:00
|
|
|
ns.CanvasRenderer.prototype.createCanvas_ = function () {
|
|
|
|
var width = this.frame.getWidth() * this.dpi;
|
|
|
|
var height = this.frame.getHeight() * this.dpi;
|
|
|
|
return pskl.CanvasUtils.createCanvas(width, height);
|
|
|
|
};
|
2013-05-28 01:42:53 +04:00
|
|
|
})();
|