From 561c35c8824cd062ecc504e3d75469ea954565c2 Mon Sep 17 00:00:00 2001 From: juliandescottes Date: Tue, 4 Sep 2012 08:35:58 +0200 Subject: [PATCH] Adding Frame Renderer utility --- js/rendering/FrameRenderer.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 js/rendering/FrameRenderer.js diff --git a/js/rendering/FrameRenderer.js b/js/rendering/FrameRenderer.js new file mode 100644 index 00000000..f5c07c54 --- /dev/null +++ b/js/rendering/FrameRenderer.js @@ -0,0 +1,31 @@ +(function () { + var ns = $.namespace("pskl.rendering"); + ns.FrameRenderer = function () {}; + + ns.FrameRenderer.prototype.render = function (frame, canvas, dpi) { + var color; + for(var col = 0, num_col = frame.length; col < num_col; col++) { + for(var row = 0, num_row = frame[col].length; row < num_row; row++) { + color = frame[col][row]; + this.drawPixelInCanvas(col, row, canvas, color, dpi); + } + } + }; + + ns.FrameRenderer.prototype.drawPixelInCanvas = function () { + var context = canvas.getContext('2d'); + if(color == Constants.TRANSPARENT_COLOR) { + context.clearRect(col * dpi, row * dpi, dpi, 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 * dpi, row * dpi, dpi, dpi); + } + + } + +})(); \ No newline at end of file