piskel/js/rendering/FrameRenderer.js

89 lines
2.6 KiB
JavaScript
Raw Normal View History

(function () {
var ns = $.namespace("pskl.rendering");
this.dpi = null;
this.canvas = null;
ns.FrameRenderer = function (container, renderingOptions, className) {
if(container == undefined) {
throw "Bad FrameRenderer initialization. <container> undefined.";
}
this.container = container;
if(renderingOptions == undefined || renderingOptions.dpi == undefined || isNaN(dpi)) {
throw "Bad FrameRenderer initialization. <dpi> not well defined.";
}
this.displayGrid = !!renderingOptions.displayGrid;
this.dpi = renderingOptions.dpi;
this.className = className;
2012-09-08 18:32:28 +04:00
// Flag to know if the config was altered
this.canvasConfigDirty = true;
};
ns.FrameRenderer.prototype.init = function (frame) {
this.render(frame);
};
2012-09-08 18:32:28 +04:00
ns.FrameRenderer.prototype.updateDPI = function (newDPI) {
this.dpi = newDPI;
this.canvasConfigDirty = true;
};
ns.FrameRenderer.prototype.render = function (frame) {
for(var col = 0, width = frame.getWidth(); col < width; col++) {
for(var row = 0, height = frame.getHeight(); row < height; row++) {
2012-09-08 18:32:28 +04:00
this.drawPixel(col, row, frame, this.getCanvas_(frame), this.dpi);
}
}
};
ns.FrameRenderer.prototype.drawPixel = function (col, row, frame) {
2012-09-08 18:32:28 +04:00
var context = this.getCanvas_(frame).getContext('2d');
var color = frame.getPixel(col, row);
if(color == Constants.TRANSPARENT_COLOR) {
context.clearRect(col * this.dpi, row * this.dpi, this.dpi, this.dpi);
}
else {
if(color != Constants.SELECTION_TRANSPARENT_COLOR) {
// TODO(vincz): Found a better design to update the palette, it's called too frequently.
$.publish(Events.COLOR_USED, [color]);
}
context.fillStyle = color;
context.fillRect(col * this.dpi, row * this.dpi, this.dpi, this.dpi);
}
};
ns.FrameRenderer.prototype.clear = function (col, row, frame) {
2012-09-08 18:32:28 +04:00
var canvas = this.getCanvas_(frame)
canvas.getContext("2d").clearRect(0, 0, canvas.width, canvas.height);
};
/**
* @private
*/
2012-09-08 18:32:28 +04:00
ns.FrameRenderer.prototype.getCanvas_ = function (frame) {
if(this.canvasConfigDirty) {
$(this.canvas).remove();
var width = frame.getWidth(),
height = frame.getHeight();
2012-09-08 18:32:28 +04:00
var canvas = document.createElement("canvas");
canvas.setAttribute("width", width * this.dpi);
canvas.setAttribute("height", height * this.dpi);
var canvasClassname = "canvas";
if(this.className) {
canvasClassname += " " + this.className;
}
canvas.setAttribute("class", canvasClassname);
this.canvas = canvas;
2012-09-08 18:32:28 +04:00
this.container.appendChild(this.canvas);
this.canvasConfigDirty = false;
}
2012-09-08 18:32:28 +04:00
return this.canvas;
};
2012-09-04 10:35:58 +04:00
})();